/** * create a complete path to a file * * The target path can be relative to YACS, or an absolute path pointing * almost anywhere. * * @param the target path * @return TRUE on success, or FALSE on failure */ public static function make_path($path) { global $context; // sanity check if (!$path) { return TRUE; } // translate path $translated = Safe::realpath($path); // the path exists if (is_dir($translated)) { return TRUE; } // create upper level first $dir_name = dirname($path); if ($dir_name != $path && preg_match('|/|', $dir_name)) { // it is mandatory to have upper level if (!Safe::make_path($dir_name)) { return FALSE; } } // create last level directory return Safe::mkdir($translated); }
$from = $context['path_to_root'] . Files::get_path($last_parent->get_reference()) . '/' . $file->item['file_name']; $dir = $context['path_to_root'] . Files::get_path($target->get_reference()); $to = $dir . '/' . $file->item['file_name']; // check that dir exists if (!is_dir($dir)) { Safe::make_path($dir); } Safe::rename($from, $to); // move thumb if any if ($file->item['thumbnail_url']) { $from = Files::get_path($last_parent->get_reference()) . '/thumbs/' . $file->item['file_name']; // make directory thumbs $to = $dir . '/thumbs/' . $file->item['file_name']; // check that dir exist if (!is_dir($dir . '/thumbs')) { Safe::mkdir($dir . '/thumbs'); } Safe::rename($from, $to); } } } // we return some JSON $output = json_encode($output); // allow for data compression render_raw('application/json; charset=' . $context['charset']); // actual transmission except on a HEAD request if (!isset($_SERVER['REQUEST_METHOD']) || $_SERVER['REQUEST_METHOD'] != 'HEAD') { echo $output; } // the post-processing hook, then exit finalize_page(TRUE);
/** * compute a thumbnail image for a file * * This function builds a preview image where possible, and returns its URL to caller, or NULL. * That result can be saved directly as attribute ##thumbnail_url## or associated file record. * * @param string path to the file, including trailing slash (e.g., 'files/article/123/') * @param string file name (e.g., 'document.pdf') * @return string web address of the thumbnail that has been built, or NULL * * @see files/edit.php */ public static function derive_thumbnail($file_path, $file_name) { global $context; // if the file is an image, create a thumbnail for it if (($image_information = Safe::GetImageSize($file_path . $file_name)) && $image_information[2] >= 1 && $image_information[2] <= 3) { // derive a thumbnail image $thumbnail_name = 'thumbs/' . $file_name; include_once $context['path_to_root'] . 'images/image.php'; Image::shrink($context['path_to_root'] . $file_path . $file_name, $context['path_to_root'] . $file_path . $thumbnail_name, FALSE, TRUE); // remember the address of the thumbnail return $context['url_to_root'] . $file_path . $thumbnail_name; // if this is a PDF that can be converted by Image Magick, then compute a thumbnail for the file } else { if (preg_match('/\\.pdf$/i', $file_name) && class_exists('Imagick') && ($handle = new Imagick($context['path_to_root'] . $file_path . $file_name))) { // derive a thumbnail image $thumbnail_name = 'thumbs/' . $file_name . '.png'; Safe::mkdir($context['path_to_root'] . $file_path . 'thumbs'); // consider only the first page $handle->setIteratorIndex(0); $handle->setImageCompression(Imagick::COMPRESSION_LZW); $handle->setImageCompressionQuality(90); $handle->stripImage(90); $handle->thumbnailImage(100, NULL); $handle->writeImage($context['path_to_root'] . $file_path . $thumbnail_name); // remember the address of the thumbnail return $context['url_to_root'] . $file_path . $thumbnail_name; } } // no thumbnail return NULL; }