function create_pack($sql_details, $info_details)
{
    global $l;
    $info_details = xml_escape_string($info_details);
    //get temp file
    $fname = $sql_details['document_root'] . $sql_details['timestamp'] . "/tmp";
    //cut this package
    if ($size = @filesize($fname)) {
        $handle = fopen($fname, "rb");
        $read = 0;
        for ($i = 1; $i < $sql_details['nbfrags']; $i++) {
            $contents = fread($handle, $size / $sql_details['nbfrags']);
            $read += strlen($contents);
            $handfrag = fopen($sql_details['document_root'] . $sql_details['timestamp'] . "/" . $sql_details['timestamp'] . "-" . $i, "w+b");
            fwrite($handfrag, $contents);
            fclose($handfrag);
        }
        $contents = fread($handle, $size - $read);
        $read += strlen($contents);
        $handfrag = fopen($sql_details['document_root'] . $sql_details['timestamp'] . "/" . $sql_details['timestamp'] . "-" . $i, "w+b");
        fwrite($handfrag, $contents);
        fclose($handfrag);
        fclose($handle);
        unlink($sql_details['document_root'] . $sql_details['timestamp'] . "/tmp");
    } else {
        if (!file_exists($sql_details['document_root'] . $sql_details['timestamp'])) {
            mkdir($sql_details['document_root'] . $sql_details['timestamp']);
        }
    }
    //if $info_details['DIGEST'] is null =>  no file to deploy, only execute commande in info file
    // so nb_frag=0
    if (!isset($info_details['DIGEST']) or $info_details['DIGEST'] == "") {
        $sql_details['nbfrags'] = 0;
    }
    //create info
    $info = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    $info .= "<DOWNLOAD ID=\"" . $sql_details['timestamp'] . "\" " . "PRI=\"" . $info_details['PRI'] . "\" " . "ACT=\"" . $info_details['ACT'] . "\" " . "DIGEST=\"" . $info_details['DIGEST'] . "\" " . "PROTO=\"" . $info_details['PROTO'] . "\" " . "FRAGS=\"" . $sql_details['nbfrags'] . "\" " . "DIGEST_ALGO=\"" . $info_details['DIGEST_ALGO'] . "\" " . "DIGEST_ENCODE=\"" . $info_details['DIGEST_ENCODE'] . "\" ";
    if ($info_details['ACT'] == 'STORE') {
        $info .= "PATH=\"" . $info_details['PATH'] . "\" ";
    }
    if ($info_details['ACT'] == 'LAUNCH') {
        $info .= "NAME=\"" . $info_details['NAME'] . "\" ";
    }
    if ($info_details['ACT'] == 'EXECUTE') {
        $info .= "COMMAND=\"" . $info_details['COMMAND'] . "\" ";
    }
    $info .= "NOTIFY_USER=\"" . $info_details['NOTIFY_USER'] . "\" " . "NOTIFY_TEXT=\"" . $info_details['NOTIFY_TEXT'] . "\" " . "NOTIFY_COUNTDOWN=\"" . $info_details['NOTIFY_COUNTDOWN'] . "\" " . "NOTIFY_CAN_ABORT=\"" . $info_details['NOTIFY_CAN_ABORT'] . "\" " . "NOTIFY_CAN_DELAY=\"" . $info_details['NOTIFY_CAN_DELAY'] . "\" " . "NEED_DONE_ACTION=\"" . $info_details['NEED_DONE_ACTION'] . "\" " . "NEED_DONE_ACTION_TEXT=\"" . $info_details['NEED_DONE_ACTION_TEXT'] . "\" " . "GARDEFOU=\"" . $info_details['GARDEFOU'] . "\" />\n";
    $handinfo = fopen($sql_details['document_root'] . $sql_details['timestamp'] . "/info", "w+");
    fwrite($handinfo, utf8_decode($info));
    fclose($handinfo);
    //delete all package with the same id
    mysql2_query_secure("DELETE FROM download_available WHERE FILEID='%s'", $_SESSION['OCS']["writeServer"], $sql_details['timestamp']);
    //insert new package
    $req = "INSERT INTO download_available(FILEID, NAME, PRIORITY, FRAGMENTS, SIZE, OSNAME, COMMENT,ID_WK) VALUES\n\t\t( '%s', '%s','%s', '%s','%s', '%s', '%s','%s' )";
    $arg = array($sql_details['timestamp'], $sql_details['name'], $info_details['PRI'], $sql_details['nbfrags'], $sql_details['size'], $sql_details['os'], $sql_details['description'], $sql_details['id_wk']);
    mysql2_query_secure($req, $_SESSION['OCS']["writeServer"], $arg);
    addLog($l->g(512), $l->g(617) . " " . $sql_details['timestamp']);
    //info message
    msg_success($l->g(437) . " " . $sql_details['document_root'] . $sql_details['timestamp']);
    //delete cache for activation
    unset($_SESSION['OCS']['DATA_CACHE']['LIST_PACK']);
    unset($_SESSION['OCS']['NUM_ROW']['LIST_PACK']);
}
function write_yaml($in, $out_file, $options = array())
{
    if (!isset($options["pretty"])) {
        $options["pretty"] = true;
    }
    if (!isset($options["offset"])) {
        $options["offset"] = "0";
    }
    $pretty = $options["pretty"];
    $offset = $options["offset"] + 0;
    $force = $options["force"];
    if (file_exists($out_file) && !$force) {
        fputs(STDERR, "Error[{$out_file}]: The file already exists. Use -f option to overwrite it.\n");
        return;
    }
    if (($out = fopen($out_file, "w")) === false) {
        fputs(STDERR, "Error[{$out_file}]: Failed to open an output file.\n");
        return;
    }
    while (($line = fgets($in)) !== false) {
        // "# ファイル名"はワークシートの先頭行を示す。
        if (substr($line, 0, 1) != "#") {
            continue;
        }
        $worksheet_name = trim(substr($line, 1));
        $headers = explode(":", "^:" . $options["outheader"] . ":\$");
        // ワークシートを出力する。
        fputs($out, "{$worksheet_name}: \n");
        $row = 0;
        while (($record = fgetcsv($in, 65536, ",", '"')) !== FALSE) {
            // 空行はワークシートの最終行を示す。
            if ($record == array('')) {
                break;
            }
            $tag = array_shift($record);
            $record = array_slice($record, $offset);
            $num_cols = count($record);
            for ($i = 0; $i < $num_cols; $i++) {
                $record[$i] = yaml_escape_string($record[$i]);
            }
            switch ($tag) {
                case "fields":
                    $fields = $record;
                    break;
                case "types":
                    $types = $record;
                    break;
                case "params":
                    $params = $record;
                    break;
                case "titles":
                    $titles = $record;
                    break;
                case "record":
                    while (($header = array_shift($headers)) !== null) {
                        // ヘッダを出力する。
                        if ($header == "^") {
                        } else {
                            if ($header == "F") {
                            } else {
                                if ($header == "C") {
                                    $ans = "";
                                    $ans .= "  types: \n";
                                    for ($col = 0; $col < $num_cols; $col++) {
                                        $field = $fields[$col];
                                        $value = xml_escape_string($types[$col]);
                                        if ($field != "") {
                                            $ans .= "    {$field}: {$value}\n";
                                        }
                                    }
                                    fputs($out, $ans);
                                } else {
                                    if ($header == "P") {
                                        $ans = "";
                                        $ans .= "  params: \n";
                                        for ($col = 0; $col < $num_cols; $col++) {
                                            $field = $fields[$col];
                                            $value = xml_escape_string($params[$col]);
                                            if ($field != "") {
                                                $ans .= "    {$field}: {$value}\n";
                                            }
                                        }
                                        fputs($out, $ans);
                                    } else {
                                        if ($header == "T") {
                                            $ans = "";
                                            $ans .= "  titles: \n";
                                            for ($col = 0; $col < $num_cols; $col++) {
                                                $field = $fields[$col];
                                                $value = xml_escape_string($titles[$col]);
                                                if ($field != "") {
                                                    $ans .= "    {$field}: {$value}\n";
                                                }
                                            }
                                            fputs($out, $ans);
                                        } else {
                                            if ($header == "N") {
                                            } else {
                                                if ($header == "\$") {
                                                    fputs($out, "  records: \n");
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    $ans = "";
                    for ($col = 0; $col < count($record); $col++) {
                        $field = $fields[$col];
                        $value = xml_escape_string($record[$col]);
                        if ($field != "") {
                            if ($col == 0) {
                                $ans .= "    - ";
                            } else {
                                $ans .= "      ";
                            }
                            $ans .= "{$field}: '{$value}'\n";
                        }
                    }
                    fputs($out, $ans);
                    break;
                default:
                    $contents = implode(",", $record);
                    fputs(STDERR, "Error[{$tag}]: Unkown tag. {$contents}\n");
                    return;
            }
            $row += 1;
        }
    }
}
Exemple #3
0
function write_xml($in, $out_file, $options = array())
{
    if (!isset($options["offset"])) {
        $options["offset"] = "0";
    }
    $offset = $options["offset"] + 0;
    $force = $options["force"];
    if (file_exists($out_file) && !$force) {
        fputs(STDERR, "Error: already [{$out_file}] exists. use -f option to overwrite it.\n");
        return;
    }
    if (($out = fopen($out_file, "w")) === false) {
        fputs(STDERR, "Error: failed to open an output file.\n");
        return;
    }
    fputs($out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    // 第1レベルの要素名はdatasetに固定する。要素名にxmlを含めることは禁止されている。
    fputs($out, "<dataset>\n");
    while (($line = fgets($in)) !== false) {
        // "# ファイル名"はワークシートの先頭行を示す。
        if (substr($line, 0, 1) != "#") {
            continue;
        }
        $worksheet_name = trim(substr($line, 1));
        $headers = explode(":", "^:" . $options["outheader"] . ":\$");
        // ワークシートを出力する。第2レベルの要素名はワークシート名にする。
        fputs($out, "<{$worksheet_name}>\n");
        $row = 0;
        while (($record = fgetcsv($in, 65536, ",", '"')) !== FALSE) {
            // 空行はワークシートの最終行を示す。
            if ($record == array('')) {
                break;
            }
            $tag = array_shift($record);
            $record = array_slice($record, $offset);
            $num_cols = count($record);
            for ($i = 0; $i < $num_cols; $i++) {
                $record[$i] = xml_escape_string($record[$i]);
            }
            switch ($tag) {
                case "fields":
                    $fields = $record;
                    break;
                case "types":
                    $types = $record;
                    break;
                case "params":
                    $params = $record;
                    break;
                case "titles":
                    $titles = $record;
                    break;
                case "record":
                    while (($header = array_shift($headers)) !== null) {
                        // ヘッダを出力する。
                        if ($header == "^") {
                        } else {
                            if ($header == "F") {
                            } else {
                                if ($header == "C") {
                                    $ans = "";
                                    $ans .= "\t<types>\n";
                                    for ($col = 0; $col < $num_cols; $col++) {
                                        $field = $fields[$col];
                                        $value = xml_escape_string($types[$col]);
                                        $ans .= "\t\t<{$field}>{$value}</{$field}>\n";
                                    }
                                    $ans .= "\t</types>\n";
                                    fputs($out, $ans);
                                } else {
                                    if ($header == "P") {
                                        $ans = "";
                                        $ans .= "\t<params>\n";
                                        for ($col = 0; $col < $num_cols; $col++) {
                                            $field = $fields[$col];
                                            $value = xml_escape_string($params[$col]);
                                            $ans .= "\t\t<{$field}>{$value}</{$field}>\n";
                                        }
                                        $ans .= "\t</params>\n";
                                        fputs($out, $ans);
                                    } else {
                                        if ($header == "T") {
                                            $ans = "";
                                            $ans .= "\t<titles>\n";
                                            for ($col = 0; $col < $num_cols; $col++) {
                                                $field = $fields[$col];
                                                $value = xml_escape_string($titles[$col]);
                                                $ans .= "\t\t<{$field}>{$value}</{$field}>\n";
                                            }
                                            $ans .= "\t</titles>\n";
                                            fputs($out, $ans);
                                        } else {
                                            if ($header == "N") {
                                            } else {
                                                if ($header == "\$") {
                                                    fputs($out, "\t<records>\n");
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    $ans = "";
                    // 第3レベルの要素名は record に固定する。
                    $ans .= "\t\t<record>\n";
                    for ($col = 0; $col < count($record); $col++) {
                        $field = $fields[$col];
                        $value = xml_escape_string($record[$col]);
                        $ans .= "\t\t\t<{$field}>{$value}</{$field}>\n";
                    }
                    $ans .= "\t\t</record>\n";
                    fputs($out, $ans);
                    break;
                default:
                    $contents = implode(",", $record);
                    fputs(STDERR, "Error[{$tag}]: Unkown tag. {$contents}\n");
                    return;
            }
            $row += 1;
        }
        fputs($out, "\t</records>\n");
        fputs($out, "</{$worksheet_name}>\n");
    }
    fputs($out, "</dataset>\n");
    return;
}