/** * Constructor * Check if the given file is loaded and attempt to create resource * * @param string $file * @return object */ public function __construct($file) { if (!extension_loaded('gd')) { throw new Image_NoGd('PHP Extension "gd" is not loaded'); } else { if (!is_file($file)) { throw new Image_FileNoExist($file . ' does not exist or is not a readable file'); } } // Check if the image would consume too much memory. $imageInfo = getimagesize($file); if (!isset($imageInfo['channels'])) { $imageInfo['channels'] = 1; } if (isset($imageInfo['bits'])) { $memoryNeeded = round(($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $imageInfo['channels'] / 8 + pow(2, 16)) * 1.65); $memoryLimit = zula_byte_value(ini_get('memory_limit')); if (memory_get_usage() + $memoryNeeded > $memoryLimit) { throw new Image_LoadFailed('image would consume more memory than current limit'); } } /** * Create correct resource from the mime file and store other * details about the image that we may need. */ $mime = zula_get_file_mime($file); switch ($mime) { case 'image/jpeg': case 'image/jpg': $this->resource = @imagecreatefromjpeg($file); break; case 'image/png': $this->resource = @imagecreatefrompng($file); break; case 'image/gif': $this->resource = @imagecreatefromgif($file); } if (!is_resource($this->resource)) { throw new Image_LoadFailed('image is not a valid file or has invalid mime type "' . $mime . '"'); } $this->details = array_merge(pathinfo($file), array('path' => $file, 'mime' => $mime, 'originalWidth' => imagesx($this->resource), 'originalHeight' => imagesy($this->resource))); $this->details['width'] = $this->details['originalWidth']; $this->details['height'] = $this->details['originalHeight']; if (!is_resource($this->resource)) { throw new Image_LoadFailed('failed to create image resource'); } }
/** * Main method for uploading/moving the file. Various exceptions are * thrown in this method to indicate the errors that could occur * (such as the main PHP error codes). * * bool false is returned if there was no file uploaded, * * @param string $filename * @return bool */ public function upload($filename = null) { switch ($this->error) { case UPLOAD_ERR_INI_SIZE: throw new Uploader_MaxFileSize(zula_byte_value(ini_get('upload_max_filesize'))); case UPLOAD_ERR_FORM_SIZE: throw new Uploader_MaxFileSize(abs($this->_input->post('MAX_FILE_SIZE'))); case UPLOAD_ERR_PARTIAL: throw new Uploader_PartialUpload(sprintf($this->errorMsg['partial'], $this->name)); case UPLOAD_ERR_NO_FILE: return false; case UPLOAD_ERR_NO_TMP_DIR: throw new Uploader_NoTmpDir(sprintf($this->errorMsg['no_tmp_dir'], $this->name)); case UPLOAD_ERR_CANT_WRITE: throw new Uploader_NoWrite(sprintf($this->errorMsg['cant_write'], $this->name)); case UPLOAD_ERR_EXTENSION: throw new Uploader_FileBlocked(sprintf($this->errorMsg['extension'], $this->name)); } if (!is_uploaded_file($this->tmpName)) { $this->_log->message('file was not uploaded, possible malicious attack', Log::L_WARNING); throw new Uploader_Exception('requested file was not actually uploaded, possible malicious attack'); } /** * Find out the mime type + category of the file, then run some checks * to ensure filesize/mime type etc are correct. */ $this->fileDetails[0]['mime'] = zula_get_file_mime($this->tmpName); $this->fileDetails[0]['category'] = substr($this->mime, 0, strpos($this->mime, '/')); if ($this->mime == false) { throw new Uploader_Exception('unable to find mime type for uploaded file'); } else { if ($this->checkFileSize($this->size) === false) { throw new Uploader_MaxFileSize($this->uploader->maxFileSize); } else { if ($this->checkMime($this->mime) === false) { throw new Uploader_InvalidMime(sprintf($this->errorMsg['mime'], $this->name, $this->mime)); } else { if ($this->checkExtension($this->name) === false) { throw new Uploader_InvalidExtension(sprintf($this->errorMsg['file_ext'], $this->name)); } } } } // All is ok, attempt to move/extract the uploaded file if ($this->uploader->extractArchives && $this->mime == 'application/zip') { $uploaded = $this->handleZip($filename); unlink($this->tmpName); if ($uploaded !== true) { throw new Uploader_Exception('failed to extract files from archive'); } } else { if ($this->handleNormal($filename) === false) { throw new Uploader_Exception('failed to move file "' . $this->name . '"'); } } return true; }
/** * Set maximum file size for uploaded files. Also * accepts PHP short-hand byte value, ie '9m' or * '5k', or '4g' * * @param int $fileSize * @return bool|object */ public function maxFileSize($fileSize) { $this->config['maxFileSize'] = zula_byte_value($fileSize); return $this; }
/** * Updates settings for the media module * * @return string */ public function settingsSection() { $this->setTitle(t('Media Settings')); $this->setOutputType(self::_OT_CONFIG); if (!$this->_acl->check('media_manage_settings')) { throw new Module_NoPermission(); } // Prepare the form of settings $mediaConf = $this->_config->get('media'); $form = new View_form('config/settings.html', 'media'); $form->addElement('media/per_page', $mediaConf['per_page'], t('Per page'), new Validator_Int())->addElement('media/use_lightbox', $mediaConf['use_lightbox'], t('Use lightbox'), new Validator_Bool())->addElement('media/max_fs', $mediaConf['max_fs'], t('Maximum file size'), new Validator_Int())->addElement('media/thumb_dimension', $mediaConf['thumb_dimension'], t('Thumbnail width/height'), new Validator_Between(20, 200))->addElement('media/max_image_width', $mediaConf['max_image_width'], t('Maximum image width'), new Validator_Between(200, 90000))->addElement('media/wm_position', $mediaConf['wm_position'], t('Watermark position'), new Validator_InArray(array('t', 'tr', 'r', 'br', 'b', 'bl', 'l', 'tl')), false); if ($form->hasInput() && $form->isValid()) { $purgeTmpImages = false; foreach ($form->getValues('media') as $key => $val) { if ($key == 'max_image_width' && $mediaConf['max_image_width'] != $val || $key == 'wm_position' && $mediaConf['wm_position'] != $val) { $purgeTmpImages = true; } else { if ($key == 'max_fs') { $val = zula_byte_value($val . $this->_input->post('media/max_fs_unit')); } } $this->_config_sql->update('media/' . $key, $val); } // Upload the watermark if ($this->_input->has('post', 'media_wm_delete')) { unlink($this->_zula->getDir('uploads') . '/media/wm.png'); unlink($this->_zula->getDir('uploads') . '/media/wm_thumb.png'); $purgeTmpImages = true; } try { $uploader = new Uploader('media_wm', $this->_zula->getDir('uploads') . '/media'); $uploader->subDirectories(false)->allowImages(); $file = $uploader->getFile(); if ($file->upload() !== false) { $image = new Image($file->path); $image->mime = 'image/png'; $image->save($file->dirname . '/wm.png', false); $image->thumbnail(80, 80)->save($file->dirname . '/wm_thumb.png'); $purgeTmpImages = true; } } catch (Uploader_NotEnabled $e) { $this->_event->error(t('Sorry, it appears file uploads are disabled within your PHP configuration')); } catch (Uploader_MaxFileSize $e) { $msg = sprintf(t('Selected file exceeds the maximum allowed file size of %s'), zula_human_readable($e->getMessage())); $this->_event->error($msg); } catch (Uploader_InvalidMime $e) { $this->_event->error(t('Sorry, the uploaded file is of the wrong file type')); } catch (Uploader_Exception $e) { $this->_log->message($e->getMessage(), Log::L_WARNING); $this->_event->error(t('Oops, an error occurred while uploading your files')); } catch (Image_Exception $e) { $this->_log->message($e->getMessage(), Log::L_WARNING); $this->_event->error(t('Oops, an error occurred while processing an image')); } // Purge tmp images if needed and redirect if ($purgeTmpImages) { $files = (array) glob($this->_zula->getDir('tmp') . '/media/max*-*'); foreach (array_filter($files) as $tmpFile) { unlink($tmpFile); } } $this->_event->success(t('Updated media settings')); return zula_redirect($this->_router->makeUrl('media', 'config', 'settings')); } if (is_file($this->_zula->getDir('uploads') . '/media/wm_thumb.png')) { $wmThumbPath = $this->_zula->getDir('uploads', true) . '/media/wm_thumb.png'; } else { $wmThumbPath = null; } $form->assign(array('WM_THUMB_PATH' => $wmThumbPath)); return $form->getOutput(); }