1 <?php
2
3 4 5 6 7 8 9 10
11
12 Yii::import('jquery-file-upload-widget.*');
13
14 15 16 17 18 19 20 21 22
23
24 class ImportController extends Controller {
25
26 public function filters() {
27 return array(
28 'accessControl',
29 );
30 }
31
32 public function accessRules() {
33 return array(
34 array('allow',
35 'actions' => array('upload', 'uploadPopup', 'uploadFile', 'ckeditorUpload'),
36 'expression' => 'Yii::app()->user->checkAccess("P3media.Import.*")',
37 ),
38 array('allow',
39 'actions' => array('check', 'localFile', 'scan'),
40 'expression' => 'Yii::app()->user->checkAccess("P3media.Import.scan")',
41 ),
42 array('deny',
43 'users' => array('*'),
44 ),
45 );
46 }
47
48 public $breadcrumbs = array(
49 'p3media' => array('/p3media')
50 );
51
52 public function init() {
53 parent::init();
54
55 }
56
57 public function actionUpload() {
58 $this->render('upload');
59 }
60
61 public function () {
62 $this->layout = "//layouts/popup";
63 $this->render('upload');
64 }
65
66 public function actionUploadFile() {
67 $contents = $this->uploadHandler();
68 echo $contents;
69 exit;
70
71 }
72
73 private function uploadHandler() {
74
75
76 $options = array(
77 'url' => $this->createUrl("/p3media/p3Media/update", array('path' => Yii::app()->user->id . "/")),
78 'upload_dir' => $this->module->getDataPath() . DIRECTORY_SEPARATOR,
79 'upload_url' => $this->createUrl("/p3media/p3Media/update", array('preset' => 'raw', 'path' => Yii::app()->user->id . "/")),
80 'script_url' => $this->createUrl("/p3media/import/uploadFile", array('path' => Yii::app()->user->id . "/")),
81 'field_name' => 'files',
82 'image_versions' => array(
83
84
85
86 87 88 89 90 91 92 93
94 'thumbnail' => array(
95
96 'upload_url' => $this->createUrl("/p3media/file/image", array('preset' => 'p3media-upload', 'path' => urlencode(Yii::app()->user->id . "/"))),
97 'max_width' => 80,
98 'max_height' => 80
99 )
100 )
101 );
102
103
104 $upload_handler = new UploadHandler($options);
105 header('Pragma: no-cache');
106 header('Cache-Control: private, no-cache');
107 header('Content-Disposition: inline; filename="files.json"');
108 header('X-Content-Type-Options: nosniff');
109
110 ob_start();
111 switch ($_SERVER['REQUEST_METHOD']) {
112 case 'HEAD':
113 case 'GET':
114 $upload_handler->get();
115
116 break;
117 case 'POST':
118
119 $upload = $_FILES[$options['field_name']];
120 $tmp_name = $_FILES[$options['field_name']]['tmp_name'];
121 if (is_array($tmp_name))
122 foreach ($tmp_name AS $index => $value) {
123 $model = P3Media::model()->findByAttributes(array('path' => Yii::app()->user->id . DIRECTORY_SEPARATOR . $upload['name'][$index]));
124 $model = new P3Media;
125
126 $attributes['path'] = Yii::app()->user->id . DIRECTORY_SEPARATOR . $upload['name'][$index];
127
128
129
130 $model->attributes = $attributes;
131 $model->validate(array('path'));
132
133 if ($model->hasErrors()) {
134
135 $file = new stdClass();
136 $file->error = "";
137 foreach ($model->getErrors() AS $error) {
138 $file->error .= $error[0];
139 }
140 $info[] = $file;
141 echo CJSON::encode($info);
142 exit;
143 }
144 }
145
146 $upload_handler->post();
147 $contents = ob_get_contents();
148 $result = CJSON::decode($contents);
149
150 $this->createMedia($result[0]['name'], $this->module->getDataPath() . DIRECTORY_SEPARATOR . $result[0]['name']);
151 break;
152 case 'DELETE':
153 $upload_handler->delete();
154 $contents = ob_get_contents();
155 $result = $this->deleteMedia($_GET['path']);
156 break;
157 default:
158 header('HTTP/1.0 405 Method Not Allowed');
159 $contents = ob_get_contents();
160 }
161 ob_end_clean();
162
163 return $contents;
164 }
165
166 public function actionScan() {
167 $dir = Yii::getPathOfAlias($this->module->importAlias);
168 $files = array();
169 foreach (scandir($dir, 0) AS $fileName) {
170 $filePath = $dir . DIRECTORY_SEPARATOR . $fileName;
171 if (substr($fileName, 0, 1) == ".")
172 continue;
173 if (is_dir($filePath))
174 continue;
175
176 $files[] = new CAttributeCollection(array(
177 'name' => $fileName,
178 'path' => $filePath,
179 'id' => md5($fileName),
180 ));
181 }
182 $this->render('scan', array('files' => $files));
183 }
184
185 public function actionCheck() {
186 $warnings = null;
187 $result = null;
188 $message = null;
189
190 $fileName = $_GET['fileName'];
191
192 $filePath = realpath(Yii::getPathOfAlias($this->module->importAlias) . DIRECTORY_SEPARATOR . $fileName);
193
194 if (is_file($filePath) && strstr($filePath, realpath(Yii::getPathOfAlias($this->module->importAlias)))) {
195 $md5 = md5_file($filePath);
196 $result['md5'] = $md5;
197 if (P3Media::model()->findByAttributes(array('md5' => $md5)) !== null) {
198 $message .= $warnings[] = "Identical file exists";
199 $message .= "<br/>";
200 }
201 if (P3Media::model()->findByAttributes(array('originalName' => $fileName)) !== null) {
202 $message .= $warnings[] = "File with same name exists";
203 $message .= "<br/>";
204 }
205
206 if (count($warnings) == 0)
207 $message .= "OK";
208 } else {
209 $warnings[] = "File not found";
210 }
211
212 $result['errors'] = $warnings;
213 $result['message'] = $message;
214 echo CJSON::encode($result);
215 }
216
217 public function actionLocalFile() {
218
219 if ($this->module->resolveFilePath($_GET['fileName'])) {
220
221 $fileName = $_GET['fileName'];
222 $importFilePath = $this->module->resolveFilePath($_GET['fileName']);
223
224 $dataFilePath = $this->module->getDataPath() . DIRECTORY_SEPARATOR . $fileName;
225 copy($importFilePath, $dataFilePath);
226
227 $model = $this->createMedia($fileName, $dataFilePath);
228 echo CJSON::encode($model->attributes);
229 } else {
230 throw new CHttpException(500, 'File not found');
231 }
232 }
233
234 public function actionCkeditorUpload() {
235 if ($_FILES['upload']['tmp_name']) {
236
237 $fileName = $_FILES['upload']['name'];
238 $importFilePath = $_FILES['upload']['tmp_name'];
239
240 $dataFilePath = $this->module->getDataPath() . DIRECTORY_SEPARATOR . $fileName;
241 copy($importFilePath, $dataFilePath);
242
243 $model = $this->createMedia($fileName, $dataFilePath);
244
245 echo "<script type='text/javascript'>
246 window.parent.CKEDITOR.tools.callFunction(".$_GET['CKEditorFuncNum'].", '".$model->createUrl('large')."', '');
247 </script>";
248 } else {
249 throw new CHttpException(500, 'File not found');
250 }
251 }
252
253 private function createMedia($fileName, $fullFilePath) {
254 $filePath = str_replace(Yii::getPathOfAlias($this->module->dataAlias) . DIRECTORY_SEPARATOR,"", $fullFilePath);
255
256 $md5 = md5_file($fullFilePath);
257 $getimagesize = getimagesize($fullFilePath);
258
259 $model = new P3Media;
260 $model->detachBehavior('Upload');
261
262 $model->title = substr($fileName, 0, 25) . '-' . substr(uniqid(), -6);
263 $model->originalName = $fileName;
264
265 $model->type = 1;
266 $model->path = $filePath;
267 $model->md5 = $md5;
268
269 if (function_exists("mime_content_type")){
270 $mime = mime_content_type($fullFilePath);
271 } else if (function_exists("finfo_open")) {
272 $finfo = finfo_open($fullFilePath);
273 $m = finfo_file($finfo, $filename);
274 finfo_close($finfo);
275 } else {
276 $mime = $getimagesize['mime'];
277 }
278 $model->mimeType = $mime;
279 $model->info = CJSON::encode(getimagesize($fullFilePath));
280 $model->size = filesize($fullFilePath);
281
282 if ($model->save()) {
283 return $model;
284 } else {
285 $errorMessage = "";
286 foreach ($model->errors AS $attrErrors) {
287 $errorMessage .= implode(',', $attrErrors);
288 }
289 throw new CHttpException(500, $errorMessage);
290 }
291 }
292
293 private function deleteMedia($filePath) {
294 $attributes['path'] = $filePath;
295 $model = P3Media::model()->findByAttributes($attributes);
296
297 $model->delete();
298 return true;
299 }
300
301 private function findMd5($md5) {
302 $model = P3Media::model()->findByAttributes(array('md5' => $md5));
303 if ($model === null)
304 return false;
305 else
306 return true;
307 }
308
309
310
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
336 }