Ejemplo n.º 1
0
 /**
  * Handles the uploading and image post-processing if needed.
  *
  * @param array $fd
  * @return array
  */
 protected function handleUpload(array $fd)
 {
     try {
         $uploader = new Uploader('media_file', $this->_zula->getDir('uploads') . '/media/' . $fd['cid'] . '/{CATEGORY}');
         $uploader->subDirectories()->allowedMime($this->allowedMime)->maxFileSize($this->_config->get('media/max_fs'))->extractArchives();
         $file = $uploader->getFile();
         if ($file->upload() === false) {
             throw new Media_Exception(t('Please select a file to upload'));
         }
         // Upload the thumbail image if one has been provided and resize it
         $thumbnailWH = $this->_config->get('media/thumb_dimension');
         $thumbUploader = new Uploader('media_thumb', $file->dirname);
         $thumbUploader->subDirectories(false)->allowImages();
         $thumbnail = $thumbUploader->getFile();
         if ($thumbnail->upload() !== false) {
             $thumbImage = new Image($thumbnail->path);
             $thumbImage->mime = 'image/png';
             $thumbImage->thumbnail($thumbnailWH, $thumbnailWH);
             // Remove the original uploaded file
             unlink($thumbnail->path);
         }
         /**
          * Get details of all the images (could have been an archive containing
          * multiple media files
          */
         $uploadedItems = array();
         while ($details = $file->getDetails()) {
             if (isset($details['path'])) {
                 // Get the directory name where the files are stored (just the name, not path)
                 $dirname = substr($details['dirname'], strrpos($details['dirname'], DIRECTORY_SEPARATOR) + 1);
                 /**
                  * Use uploaded thumbnail, or attempt to create one from the uploaded image
                  */
                 $thumbname = $details['filename'] . '_thumb.png';
                 if (isset($thumbImage)) {
                     $thumbImage->save($details['dirname'] . '/' . $thumbname, false);
                 } else {
                     if ($details['category'] == 'image') {
                         $tmpThumb = new Image($details['path']);
                         $tmpThumb->mime = 'image/png';
                         $tmpThumb->thumbnail($thumbnailWH, $thumbnailWH)->save($details['dirname'] . '/' . $thumbname);
                     } else {
                         unset($thumbname);
                     }
                 }
                 // Generate a title from the filename automatically
                 $title = str_replace(array('-', '_', '+'), ' ', pathinfo($details['name'], PATHINFO_FILENAME));
                 $uploadedItems[] = array('title' => trim(ucfirst(strtolower($title))), 'desc' => '', 'type' => $details['category'], 'file' => $dirname . '/' . $details['basename'], 'thumbnail' => isset($thumbname) ? $dirname . '/' . $thumbname : '');
             }
         }
         if (isset($thumbImage)) {
             $thumbImage->destroy();
         }
         return $uploadedItems;
     } catch (Uploader_NotEnabled $e) {
         $msg = 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()));
     } catch (Uploader_InvalidMime $e) {
         $msg = t('Sorry, the uploaded file is of the wrong file type');
     } catch (Uploader_Exception $e) {
         $logMsg = $e->getMessage();
         $msg = t('Oops, an error occurred while uploading your files');
     } catch (Image_Exception $e) {
         $logMsg = $e->getMessage();
         $msg = t('Oops, an error occurred while processing an image');
     }
     // Cleanup and end processing, it failed.
     if (isset($file->dirname)) {
         zula_full_rmdir($file->dirname);
     }
     if (isset($logMsg)) {
         $this->_log->message($logMsg, Log::L_WARNING);
     }
     throw new Media_Exception($msg);
 }
Ejemplo n.º 2
0
 /**
  * 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();
 }