function importFile($file, $meta) { $user = new User(); $name = decodeName($file); $name = preg_replace('/\\s+\\d+$/', "", $name); if ($_REQUEST['group']) { $name = $_REQUEST['group'] . $GLOBALS['pie']['group_delimiter'] . $name; } if ($meta['user']) { // Old style author. $meta['author'] = $meta['user']; unset($meta['user']); } if (!$meta['stamp']) { $meta['stamp'] = filemtime($file); } if (!$meta['type']) { // Determine file type. if (preg_match('/\\.([0-9A-Za-z]{1,5})$/', $name, $match)) { // File type can be determined by file name suffix. $map = new MapFile(); if ($type = $map->read($GLOBALS['pie']['library_path'] . "/share/suffix.map", strtolower($match[1]))) { $meta['type'] = $type; } } elseif (function_exists("mime_content_type")) { $meta['type'] = mime_content_type($file); } } // User mapping. if (!$meta['author'] && $_REQUEST['author']) { $meta['author'] = $_REQUEST['author']; } if (!$user->exists($meta['author']) && $_REQUEST['author']) { $meta['author'] = $_REQUEST['author']; } $temp = pieTempName("_import"); if (!copy($file, $temp)) { return false; } $f = new File(); $f->name = $name; $f->meta = $meta; return $f->write($temp); }
function write($file, $key, $value) { $tempfile = pieTempName(basename($file)); if (file_exists($tempfile)) { // The file is locked (by another process?). // Wait up to 10 seconds for it to be released. if (($l = time() - filemtime($tempfile)) < 10) { sleep($l); } if (!unlink($tempfile)) { return false; } } if (!($out = fopen($tempfile, 'w'))) { return false; } if (!empty($value)) { fwrite($out, "{$key}:\t{$value}\n"); } if (file_exists($file)) { // Copy data from original file. if (!($in = fopen($file, 'r'))) { fclose($out); unlink($tempfile); return false; } $key .= ':'; $l = strlen($key); while ($line = fgets($in, BLOCKSIZE)) { if (substr($line, 0, $l) != $key) { fwrite($out, $line); } } fclose($in); unlink($file); } fclose($out); return rename($tempfile, $file); }
function write($replace) { // Prepare data. $this->source = pieCleanString($this->source); $this->source = preg_replace('/\\x0a{3,}/s', "\n\n", $this->source); $this->source = rtrim($this->source) . "\n\n"; $this->meta['size'] = strlen($this->source); if (!$this->meta['stamp']) { $this->meta['stamp'] = time(); } if (!$this->meta['type']) { $this->meta['type'] = 'full'; } // Write source and meta data to database. if (!($out = pieTempFile($this->name))) { return false; } fwrite($out, pieImplodePage($this->meta, $this->source)); // Append any previous versions if ($this->exists($this->name)) { if (!($in = fopen($this->_filePath($this->name), 'r'))) { fclose($out); unlink(pieTempName($this->name)); return false; } while ($info = $this->_nextVersion($in)) { if ($replace) { $replace = false; continue; } // Remove redundant data before updating. $source = $info['source']; unset($info['source']); unset($info['pages']); unset($info['files']); fwrite($out, pieImplodePage($info, $source)); } fclose($in); } // Activate new file fclose($out); touch(pieTempName($this->name), $this->meta['stamp']); return rename(pieTempName($this->name), $this->_filePath($this->name)); }
include_once "{$lib}/share/stdio.php"; include_once "{$lib}/share/storage.php"; include_once "{$lib}/share/string.php"; include_once "{$lib}/compiler/html.php"; pieLoadLocale(); pieRequireUser(); pieHead("edit"); $_REQUEST['page'] = pieBeautifyName(pieGetOption(@$_REQUEST['page'])); $_REQUEST['section'] = intval(@$_REQUEST['section']); $_REQUEST['stamp'] = intval(@$_REQUEST['stamp']); if ($_REQUEST['stamp'] < 1) { $_REQUEST['stamp'] = 0; } $page = new Page(); $page->name = $_REQUEST['page']; $preview = pieTempName("_preview"); // Check validity. if (!$page->isValidName($page->name)) { pieError("PageNameInvalid"); } if (!$page->lock($GLOBALS['pie']['user'])) { pieError("PageLockError"); } // Prepare editing environment. if ($GLOBALS['pie']['edit_timeout']) { pieExpireDirectory($GLOBALS['pie']['run_path'] . "/temp", $GLOBALS['pie']['edit_timeout']); } $_REQUEST['cols'] = 80; $_REQUEST['rows'] = 20; $_REQUEST['author'] = $GLOBALS['pie']['user']; $pref = new UserPref();
function pieTempFile($name) { return fopen(pieTempName($name), "w"); }
} // Prepare meta data of the file: $file->name = $name; $file->meta = array('stamp' => time(), 'author' => $GLOBALS['pie']['user']); // .. file size if ($_FILES['upload']['size']) { $file->meta['size'] = $_FILES['upload']['size']; } else { $file->meta['size'] = filesize(pieTempName("_upload")); } // .. file type if (preg_match('/^[a-z]+\\/[a-z]+[\\w\\-\\+\\.]*\\w+$/', $_FILES['upload']['type'])) { $file->meta['type'] = $_FILES['upload']['type']; } elseif (preg_match('/\\.([0-9A-Za-z]{1,5})$/', $name, $match)) { // File type is determined by file name suffix. $map = new MapFile(); if ($type = $map->read("{$lib}/share/suffix.map", strtolower($match[1]))) { $file->meta['type'] = $type; } } elseif (function_exists("mime_content_type")) { $file->meta['type'] = mime_content_type(pieTempName("_upload")); } if (@$_REQUEST['comment']) { $file->meta['comment'] = pieGetOption($_REQUEST['comment']); } if (!$file->write(pieTempName("_upload"))) { pieError("FileWriteError"); } pieLog("edit"); pieNotice("UploadComplete"); pieTail();
function replace($file) { if (($meta = $this->_current($file)) === false) { return false; } $path = $this->_mediaPath("{$file} " . $meta['stamp']); if (file_exists($path)) { unlink($path); } if (!($out = pieTempFile($file))) { return false; } if (!($in = fopen($this->_filePath($file), 'r'))) { return false; } $this->_nextVersion($in); while (!feof($in)) { $data = fread($in, BLOCKSIZE); fwrite($out, $data); } fclose($in); fclose($out); return rename(pieTempName($file), $this->_filePath($file)); }