Example #1
0
function importFile($file)
{
    $name = decodeName($file);
    if (!$_REQUEST['xpf']) {
        // Treat the contents of the file as literal data.
        if (!($source = file_get_contents($file))) {
            return false;
        }
        return importPage($name, $source, array('stamp' => filemtime($file)));
    }
    // Treat the file as a page written in XPF:
    // Build a version history.
    if (!($f = fopen($file, "r"))) {
        return false;
    }
    $history = array();
    $head = array();
    while ($line = fgets($f, 16384)) {
        if ($line == "\n") {
            if (!$head['stamp'] && !$head['size']) {
                pieError("FormatError", array('page' => htmlspecialchars($name)));
            }
            $head['offset'] = ftell($f);
            if (fseek($f, $head['size'], SEEK_CUR) == -1) {
                pieError("FormatError", array('page' => htmlspecialchars($name)));
            }
            $history[$head['stamp']] = $head;
            $head = array();
        } elseif (preg_match('/^(\\w+)=(.+)$/', $line, $match)) {
            $head[$match[1]] = $match[2];
        } else {
            pieError("FormatError", array('page' => htmlspecialchars($name)));
        }
    }
    // Process all versions in order from oldest to latest.
    ksort($history);
    foreach ($history as $stamp => $head) {
        if ($head['type'] == 'alias') {
            importPage($name, '', $head);
            continue;
        }
        if (fseek($f, $head['offset'], CUR_SET) == -1) {
            pieError("FormatError", array('page' => htmlspecialchars($name)));
        }
        if (!($source = fread($f, $head['size']))) {
            pieError("FormatError", array('page' => htmlspecialchars($name)));
        }
        unset($head['offset']);
        if (!importPage($name, $source, $head)) {
            return false;
        }
    }
    fclose($f);
    return true;
}
Example #2
0
function checkFiles($list)
{
    $f = new File();
    $num = 0;
    foreach ($list as $file) {
        $name = decodeName($file);
        $name = preg_replace('/\\s+\\d+$/', "", $name);
        if (!$f->isValidName($name)) {
            pieError("InvalidName", array('file' => htmlspecialchars($name)));
        }
        if ($f->exists($name)) {
            pieError("FileExists", array('file' => htmlspecialchars($name)));
        }
        $num++;
    }
    return $num;
}