Пример #1
0
 private static function _writeLog($type, $module, $message)
 {
     $logFile = ROOT . Media::getFilesPath() . Config::get(sprintf('log.%s_file', $type));
     $logDir = implode('/', explode('/', $logFile, -1));
     if (!file_exists($logDir)) {
         $filesPath = ROOT . Media::getFilesPath();
         if (is_writable($filesPath)) {
             mkdir($logDir);
         } else {
             die('Unable to create log directory because your files directory isn\'t writable.');
         }
     }
     if (!is_writable($logDir)) {
         die('Unable to write log file because your log directory isn\'t writable.');
     }
     if (!file_exists($logFile)) {
         $header = "<?php if(!defined('ROOT')) exit; ?>\n";
         if (!($handle = fopen($logFile, 'a'))) {
             die('Cant create log file: ' . $logFile);
         }
         if (fwrite($handle, $header) === false) {
             die('Cant write to log file: ' . $logFile);
         }
         fclose($handle);
     }
     $log = sprintf("[%s] %s: %s\n", date('Y-m-d H:i:s'), $module, $message);
     if (!($handle = fopen($logFile, 'a'))) {
         die('Cant open log file for writing: ' . $logFile);
     }
     if (fwrite($handle, $log) === false) {
         die('Cant write to log file: ' . $logFile);
     }
     fclose($handle);
 }
Пример #2
0
 /**
  * Gets an image by id sized and rotated based on the passed params. If the file
  * doesn't exist, it is created using the imager class and stored in cache dir. Every 
  * call after that will load the file directly.
  *
  * When sizing an image with the same width and height, it will be scaled down to the closest
  * width/height and then cropped.
  *
  * To size an image to a specific width, set the width value needed and the height value to 0. 
  * This ensures the specified width is met, and the height is adjusted proportionally. The same
  * can be done by setting the height needed and the width to 0.
  *
  * @param $id int The id of image to get
  * @param $rotation int The degrees to rotate the image, can be null
  * @param $widthOrPercent int When by itself, represents percent to increase/decrease size.
  *      When used with the height param, represents the image width.
  * @param $height int The height to size the image.
  *
  * @return string The relative path from ROOT to the file, if it exists. Boolean false otherwise.
  */
 public function render($id, $rotation = null, $widthOrPercent = null, $height = null)
 {
     try {
         if ($image = Media::m('file')->find($id)) {
             $ext = pathinfo($image->name, PATHINFO_EXTENSION);
             $cachedFilename = str_replace('.' . $ext, sprintf('_%d.%s', $id . $rotation . $widthOrPercent . $height, $ext), $image->name);
             $cachedFullPath = ROOT . Media::getFilesPath() . Media::getCachePath() . $cachedFilename;
             if (!file_exists($cachedFullPath)) {
                 $origFullPath = ROOT . Media::getFilesPath() . $image->path . $image->name;
                 Media_Imager::open($origFullPath);
                 if (!is_null($widthOrPercent) && is_null($height)) {
                     Media_Imager::percent($widthOrPercent);
                 } elseif (!is_null($widthOrPercent) && !is_null($height)) {
                     $adaptive = $widthOrPercent == 0 || $height == 0 ? false : true;
                     Media_Imager::resize($widthOrPercent, $height, $adaptive);
                 }
                 if (!is_null($rotation) && $rotation > 0) {
                     Media_Imager::rotate($rotation);
                 }
                 Media_Imager::save($cachedFullPath);
             }
             return Media::getFilesPath() . Media::getCachePath() . $cachedFilename;
         }
     } catch (Exception $e) {
         Log::error('media', $e->getMessage());
     }
     return false;
 }
Пример #3
0
 /**
  * Returns the relative path from ROOT to the file.
  */
 public function getPath($id)
 {
     if ($file = Media::m('file')->find($id)) {
         $path = Media::getFilesPath() . $file->path . $file->name;
         if (file_exists($path)) {
             return $path;
         }
     }
     return false;
 }
Пример #4
0
 /**
  * Forces a download of the file based on the given id. If the file doesn't
  * exist, 404 is shown.
  */
 public static function download($id)
 {
     $file = Media::m('file')->find($id);
     if ($file) {
         $filePath = Media::getFilesPath() . $file->path . $file->name;
         header('Content-Type: application/octet-stream');
         header('Content-Disposition: attachment; filename=' . $file->name);
         header('Content-Transfer-Encoding: binary');
         header('Expires: 0');
         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
         header('Pragma: public');
         header('Content-Length: ' . $file->size);
         ob_clean();
         flush();
         readfile($file_path);
         exit;
     }
     return ERROR_404;
 }
Пример #5
0
 /**
  * Gets a new, unused filename for the given filename and returns it with
  * the full path to the new filename.
  *
  * @param string $filename The original filename, no paths.
  *
  * @return An array, first element is the new filename, the second is the full path.
  */
 private static function _getNewNameAndPath($filename)
 {
     if (!($filesPath = Media::getFilesPath())) {
         self::$_error = 'Files directory doesn\'t exist or isn\'t writable.';
         return false;
     }
     if (!($mediaPath = Media::getMediaPath())) {
         self::$_error = 'Media directory doesn\'t exist or isn\'t writable.';
         return false;
     }
     $uploadPath = $filesPath . $mediaPath;
     $newFilename = self::_getAvailFilename($filename, $uploadPath);
     $fullPath = ROOT . $uploadPath . $newFilename;
     return array($newFilename, $fullPath);
 }