Ejemplo n.º 1
0
    header("Content-type: text/json");
    // api_type
    $api_type = $app->request()->params("api_type");
    if (!is_string($api_type) || $api_type == "") {
        return false;
    } else {
        if (!isset($GLOBALS["api_type_list"][$api_type])) {
            return false;
        }
    }
    // id
    $id = $app->request()->params("id");
    if (!is_string($id) || $id == "") {
        return false;
    }
    $result = call_color_scheme_api($api_type, $id);
    if (!$result) {
        die("null");
    }
    print json_encode($result);
});
$app->get("/api/package/?", function () use($app) {
    header("Content-type: text/json");
    if (!check_colors($app->request())) {
        die("null");
    }
    $api_type = $app->request()->params("api_type");
    $id = $app->request()->params("id");
    $c = $app->request()->params("c");
    $share = $app->request()->params("share");
    $result_generate = generate($api_type, $id, $c);
Ejemplo n.º 2
0
function save_theme($api_type, $id, $c, $share)
{
    $api_result = call_color_scheme_api($api_type, $id);
    if (!$api_result || $api_result["record"] == 0) {
        return false;
    }
    $now = date("Y-m-d H:i:s");
    ksort($c);
    $colors = implode(",", $c);
    $save_taggings = false;
    $dbh = connect_db();
    $dbh->beginTransaction();
    try {
        $sql = "select * from themes where api_type = ? and cs_id = ? and colors = ?";
        $stmt = $dbh->prepare($sql);
        $stmt->bindValue(1, $api_type);
        $stmt->bindValue(2, $id);
        $stmt->bindValue(3, $colors);
        $stmt->execute();
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!empty($row)) {
            $theme_id = $row["theme_id"];
            if ($row["share"] == 0 && $share) {
                $sql = "update themes set cs_name = ?, share = 1, ip_addr = ?, created_at = now(), updated_at = now() where theme_id = ?";
                $stmt = $dbh->prepare($sql);
                $stmt->bindValue(1, $api_result["title"]);
                $stmt->bindValue(2, $_SERVER["REMOTE_ADDR"]);
                $stmt->bindValue(3, $theme_id);
                $stmt->execute();
                $save_taggings = true;
            }
        } else {
            $sql = "insert into themes (api_type, cs_id, cs_name, colors, share, ip_addr, created_at, updated_at) values (?, ?, ?, ?, ?, ?, now(), now())";
            $stmt = $dbh->prepare($sql);
            $stmt->bindValue(1, $api_type);
            $stmt->bindValue(2, $id);
            $stmt->bindValue(3, $api_result["title"]);
            $stmt->bindValue(4, $colors);
            $stmt->bindValue(5, $share ? 1 : 0);
            $stmt->bindValue(6, $_SERVER["REMOTE_ADDR"]);
            $stmt->execute();
            $theme_id = $dbh->lastInsertId();
            if ($share) {
                $save_taggings = true;
            }
        }
        if ($save_taggings) {
            $sql = "delete from taggings where theme_id = ?";
            $stmt = $dbh->prepare($sql);
            $stmt->bindValue(1, $theme_id);
            $stmt->execute();
            $tagging_tag_names = calculate_taggings($c);
            $tags = find_tags();
            $sql = "insert into taggings (theme_id, tag_id, created_at) values (?, ?, now())";
            $stmt = $dbh->prepare($sql);
            foreach ($tagging_tag_names as $tagging_tag_name) {
                $tag_id = array_search($tagging_tag_name, $tags);
                if ($tag_id === false) {
                    throw new Exception("Tag not found");
                }
                $stmt->bindValue(1, $theme_id);
                $stmt->bindValue(2, $tag_id);
                $stmt->execute();
            }
        }
        $dbh->commit();
    } catch (Exception $e) {
        var_dump($e);
        $dbh->rollback();
        return false;
    }
    return $theme_id;
}