1 <?php
  2 
  3   4   5   6   7   8   9  10 
 11 
 12 namespace Icybee\Modules\Files;
 13 
 14 use ICanBoogie\HTTP\Request;
 15 use ICanBoogie\Uploaded;
 16 
 17  18  19  20  21 
 22 class UploadOperation extends \ICanBoogie\Operation
 23 {
 24      25  26 
 27     protected $file;
 28 
 29     protected function get_file()
 30     {
 31         return $this->file;
 32     }
 33 
 34      35  36 
 37     protected $accept;
 38 
 39      40  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  61 
 62     protected function validate(\ICanboogie\Errors $errors)
 63     {
 64         global $core;
 65 
 66         
 67         
 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 }