Ejemplo n.º 1
0
 public static function get_mediatype($mime, $filename = '')
 {
     $mediatype = static::mimetype2mediatype($mime);
     if ($mediatype === 'other' && !empty($filename)) {
         $ext = \pdyn\filesystem\FilesystemUtils::get_ext($filename);
         $mimefromext = \pdyn\filesystem\Mimetype::ext2mime($ext);
         $mediatype = static::mimetype2mediatype($mimefromext);
     }
     return $mediatype;
 }
Ejemplo n.º 2
0
 /**
  * Download the file at $url and store locally.
  *
  * @param string $url The URL to fetch.
  * @param string $local_path The path to store the file, should be a full path, and include the desired filename
  * @param boolean $append_ext_based_on_mime If true, append an appropriate extension to $local_path.
  * @return array Array of The $local_path (with appended extension, if $append_ext_based_on_mime is true), and the response object
  */
 public function download($url, $local_path, $append_ext_based_on_mime = false)
 {
     $fp = fopen($local_path, 'w+');
     $additional_opts = [CURLOPT_FILE => &$fp, CURLOPT_TIMEOUT => 1800, CURLOPT_CONNECTTIMEOUT => 15];
     $response = $this->request($url, 'get', array(), $additional_opts);
     fclose($fp);
     $responseerrors = $response->errors();
     if (!empty($responseerrors)) {
         if (!empty($this->logger)) {
             $this->logger->debug('httpclient download failed: ' . $response->get_debug_string());
         }
         return [false, $response];
     }
     if ($append_ext_based_on_mime === true) {
         $mime = $response->mime_type();
         $ext = \pdyn\filesystem\Mimetype::mime2ext($mime);
         if (empty($ext) || $ext === 'unknown') {
             $ext = \pdyn\filesystem\FilesystemUtils::get_ext($url);
             $mime = \pdyn\filesystem\Mimetype::ext2mime($ext);
         }
         rename($local_path, $local_path . '.' . $ext);
         $local_path = $local_path . '.' . $ext;
     }
     return [$local_path, $response];
 }
Ejemplo n.º 3
0
 /**
  * Analyze the file for MIME type.
  *
  * @return string The mime type.
  */
 public function get_analyzed_mimetype()
 {
     return \pdyn\filesystem\FilesystemUtils::get_mime_type($this->stored_filename);
 }
Ejemplo n.º 4
0
 /**
  * Serve a file.
  *
  * Includes support for range requests/partial content.
  *
  * @param $file The absolute filename
  * @param $mime The mimetype of the file.
  * @param $filename The filename to serve the file as.
  */
 public static function serve_file($file, $mime = '', $filename = '', $forcedownload = false)
 {
     if (!file_exists($file)) {
         throw new Exception('File not found', Exception::ERR_RESOURCE_NOT_FOUND);
     }
     if (empty($mime)) {
         $mime = \pdyn\filesystem\FilesystemUtils::get_mime_type($file);
     }
     if (empty($filename)) {
         $filename = basename($file);
     }
     static::caching_headers($file, filemtime($file), true);
     clearstatcache();
     $fp = fopen($file, 'r');
     $size = filesize($file);
     $length = $size;
     $start = 0;
     $end = $size - 1;
     header('Accept-Ranges: 0-' . $length);
     if (isset($_SERVER['HTTP_RANGE'])) {
         $c_start = $start;
         $c_end = $end;
         list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
         if (strpos($range, ',') !== false) {
             header('HTTP/1.1 416 Requested Range Not Satisfiable');
             header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size);
             exit;
         }
         if ($range == '-') {
             $c_start = $size - mb_substr($range, 1);
         } else {
             $range = explode('-', $range);
             $c_start = $range[0];
             $c_end = isset($range[1]) && is_numeric($range[1]) ? $range[1] : $size;
         }
         $c_end = $c_end > $end ? $end : $c_end;
         if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
             header('HTTP/1.1 416 Requested Range Not Satisfiable');
             header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size);
             exit;
         }
         $start = $c_start;
         $end = $c_end;
         $length = $end - $start + 1;
         fseek($fp, $start);
         header('HTTP/1.1 206 Partial Content');
     }
     header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size);
     header('Content-Length: ' . $length);
     header('Content-Type: ' . $mime);
     if ($forcedownload === true) {
         header('Content-disposition: attachment; filename="' . $filename . '"');
     }
     $buffer = 1024 * 8;
     while (!feof($fp) && ($p = ftell($fp)) <= $end) {
         if ($p + $buffer > $end) {
             $buffer = $end - $p + 1;
         }
         set_time_limit(0);
         echo fread($fp, $buffer);
         flush();
     }
     fclose($fp);
 }
Ejemplo n.º 5
0
Archivo: Image.php Proyecto: pdyn/image
 /**
  * Load an image file into the object.
  *
  * @param string $filename The filename of the image.
  * @param bool $loadexif Whether to load exif information (can cause memory problems with large files).
  */
 protected function load_from_file($filename, $loadexif = true)
 {
     if (!file_exists($filename)) {
         throw new \Exception('File "' . $filename . '" not found', static::ERR_FILE_NOT_FOUND);
     }
     $this->filename = $filename;
     // Create image resource.
     $this->mime = \pdyn\filesystem\FilesystemUtils::get_mime_type($this->filename);
     switch ($this->mime) {
         case 'image/jpeg':
             $this->res = @imagecreatefromjpeg($this->filename);
             break;
         case 'image/png':
             $this->res = @imagecreatefrompng($this->filename);
             break;
         case 'image/gif':
             $this->res = @imagecreatefromgif($this->filename);
             break;
         default:
             throw new \Exception('Filetype not supported', static::ERR_FILETYPE_NOT_SUPPORTED);
     }
     if (empty($this->res)) {
         throw new \Exception('Error opening file.', static::ERR_BAD_INPUT);
     }
     if ($loadexif === true && $this->mime === 'image/jpeg') {
         $this->exif = $this->get_exif_pel();
     }
 }
Ejemplo n.º 6
0
 /**
  * Assign values to class properties.
  *
  * @param array $info An array of data to assign.
  * @return bool Success/Failure.
  */
 protected function populate(array $info = array())
 {
     if (empty($info)) {
         $this->post_populate();
         return false;
     }
     // Load datatype information into static var cache.
     $obj_datatypes = static::get_obj_datatypes($this->DB);
     $info = array_intersect_key($info, $obj_datatypes);
     foreach ($info as $key => $val) {
         switch ($obj_datatypes[$key]) {
             case 'email':
                 if (\pdyn\datatype\Validator::email($val)) {
                     $this->{$key} = (string) $val;
                 }
                 break;
             case 'timestamp':
                 if ((int) $val > 0) {
                     $this->{$key} = (int) $val;
                 }
                 break;
             case 'str':
             case 'text':
             case 'longtext':
                 $this->{$key} = (string) $val;
                 break;
             case 'filename':
                 $val = trim($val);
                 if (!empty($val)) {
                     $this->{$key} = \pdyn\filesystem\FilesystemUtils::sanitize_filename($val);
                 }
                 break;
             case 'int':
             case 'bigint':
                 $this->{$key} = (int) $val;
                 break;
             case 'float':
                 $this->{$key} = (double) $val;
                 break;
             case 'id':
                 $this->{$key} = (int) $val;
                 break;
             case 'bool':
                 $this->{$key} = (bool) $val;
                 break;
             case 'user_id':
                 if (\pdyn\datatype\Id::validate($val) === true) {
                     $this->{$key} = (int) $val;
                 }
                 break;
             case 'url':
                 if (\pdyn\datatype\Url::validate($val) === true) {
                     $this->{$key} = (string) $val;
                 }
                 break;
             case 'mime':
                 if (\pdyn\datatype\Validator::mime($val) === true) {
                     $this->{$key} = (string) $val;
                 }
                 break;
             default:
                 if (is_array($obj_datatypes[$key])) {
                     if (in_array($val, $obj_datatypes[$key], true)) {
                         $this->{$key} = $val;
                     }
                 } else {
                     $this->{$key} = $val;
                 }
         }
     }
     $this->post_populate();
     return true;
 }