Autodoc
  • Namespace
  • Class
  • Tree

Namespaces

  • BlueTihi
    • Context
  • Brickrouge
    • Element
      • Nodes
    • Renderer
    • Widget
  • ICanBoogie
    • ActiveRecord
    • AutoConfig
    • CLDR
    • Composer
    • Core
    • Event
    • Exception
    • HTTP
      • Dispatcher
      • Request
    • I18n
      • Translator
    • Mailer
    • Modules
      • Taxonomy
        • Support
      • Thumbnailer
        • Versions
    • Object
    • Operation
      • Dispatcher
    • Prototype
    • Routes
    • Routing
      • Dispatcher
    • Session
  • Icybee
    • ActiveRecord
      • Model
    • ConfigOperation
    • Document
    • EditBlock
    • Element
      • ActionbarContextual
      • ActionbarSearch
      • ActionbarToolbar
    • FormBlock
    • Installer
    • ManageBlock
    • Modules
      • Articles
      • Cache
        • Collection
        • ManageBlock
      • Comments
        • ManageBlock
      • Contents
        • ManageBlock
      • Dashboard
      • Editor
        • Collection
      • Files
        • File
        • ManageBlock
      • Forms
        • Form
        • ManageBlock
      • I18n
      • Images
        • ManageBlock
      • Members
      • Modules
        • ManageBlock
      • Nodes
        • ManageBlock
        • Module
      • Pages
        • BreadcrumbElement
        • LanguagesElement
        • ManageBlock
        • NavigationBranchElement
        • NavigationElement
        • Page
        • PageController
      • Registry
      • Search
      • Seo
      • Sites
        • ManageBlock
      • Taxonomy
        • Terms
          • ManageBlock
        • Vocabulary
          • ManageBlock
      • Users
        • ManageBlock
        • NonceLogin
        • Roles
      • Views
        • ActiveRecordProvider
        • Collection
        • View
    • Operation
      • ActiveRecord
      • Constructor
      • Module
      • Widget
    • Rendering
  • None
  • Patron
  • PHP

Classes

  • ConfigBlock
  • ConfigOperation
  • DeleteOperation
  • DownloadOperation
  • EditBlock
  • File
  • FileUpload
  • GetOperation
  • ManageBlock
  • Model
  • Module
  • SaveOperation
  • UploadOperation
  1 <?php
  2 
  3 /*
  4  * This file is part of the Icybee package.
  5  *
  6  * (c) Olivier Laviale <olivier.laviale@gmail.com>
  7  *
  8  * For the full copyright and license information, please view the LICENSE
  9  * file that was distributed with this source code.
 10  */
 11 
 12 namespace Icybee\Modules\Files;
 13 
 14 use ICanBoogie\HTTP\Request;
 15 use ICanBoogie\Uploaded;
 16 
 17 /**
 18  * Upload a file to the repository's temporary folder.
 19  *
 20  * @property-read \ICanBoogie\HTTP\File $file The uploaded file.
 21  */
 22 class UploadOperation extends \ICanBoogie\Operation
 23 {
 24     /**
 25      * @var \ICanBoogie\HTTP\File The target file of the operation.
 26      */
 27     protected $file;
 28 
 29     protected function get_file()
 30     {
 31         return $this->file;
 32     }
 33 
 34     /**
 35      * @var array Accepted file types.
 36      */
 37     protected $accept;
 38 
 39     /**
 40      * Controls for the operation: permission(create).
 41      */
 42     protected function get_controls()
 43     {
 44         return array
 45         (
 46             self::CONTROL_PERMISSION => Module::PERMISSION_CREATE
 47         )
 48 
 49         + parent::get_controls();
 50     }
 51 
 52     public function __invoke(Request $request)
 53     {
 54         $this->module->clean_temporary_files();
 55 
 56         return parent::__invoke($request);
 57     }
 58 
 59     /**
 60      * Validates the operation if the file upload succeeded.
 61      */
 62     protected function validate(\ICanboogie\Errors $errors)
 63     {
 64         global $core;
 65 
 66         #
 67         # forces 'application/json' response type
 68         #
 69 
 70         $_SERVER['HTTP_ACCEPT'] = 'application/json';
 71 
 72         $this->file = $file = $this->request->files['path'];
 73 
 74         if (!$file)
 75         {
 76             $errors[SaveOperation::USERFILE] = $errors->format("No file was uploaded.");
 77 
 78             return false;
 79         }
 80 
 81         $error_message = $file->error_message;
 82 
 83         $max_file_size = $core->registry["{$this->module->flat_id}.max_file_size"];
 84 
 85         if ($max_file_size && $max_file_size < $file->size)
 86         {
 87             $error_message = $errors->format("Maximum file size is :size Mb", [ ':size' => round($max_file_size / 1024) ]);
 88         }
 89 
 90         if (!$file->match($this->accept))
 91         {
 92             $error_message = $errors->format("Only the following file types are accepted: %accepted.", [ '%accepted' => implode(', ', $this->accept) ]);
 93         }
 94 
 95         if ($error_message)
 96         {
 97             $errors['path'] = $error_message;
 98         }
 99 
100         return true;
101     }
102 
103     protected function process()
104     {
105         $file = $this->file;
106 
107         $pathname = \ICanBoogie\REPOSITORY . 'tmp' . DIRECTORY_SEPARATOR . uniqid(null, true) . $file->extension;
108 
109         $file->move($pathname);
110 
111         file_put_contents($pathname . '.info', json_encode($file->to_array()));
112 
113         $title = $file->unsuffixed_name;
114         $pathname = \ICanBoogie\strip_root($pathname);
115 
116         $this->response['infos'] = null;
117 
118         if (isset($_SERVER['HTTP_X_USING_FILE_API']))
119         {
120             $size = \ICanBoogie\I18n\format_size($file->size);
121 
122             $this->response['infos'] = <<<EOT
123 <ul class="details">
124     <li><span title="{$pathname}">{$title}</span></li>
125     <li>$file->type</li>
126     <li>$size</li>
127 </ul>
128 EOT;
129 
130         }
131 
132         return array_merge($file->to_array(), [
133 
134             'title' => $title,
135             'pathname' => $pathname
136 
137         ]);
138     }
139 }
Autodoc API documentation generated by ApiGen 2.8.0