1
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.'); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This class is what the upload field accually returns |
5
|
|
|
* and has all the nesessary info and manipulation abilities to save / delete / validate itself |
6
|
|
|
* |
7
|
|
|
* @package Jam |
8
|
|
|
* @author Ivan Kerin |
9
|
|
|
* @copyright (c) 2011-2012 Despark Ltd. |
10
|
|
|
* @license http://creativecommons.org/licenses/by-sa/3.0/legalcode |
11
|
|
|
*/ |
12
|
|
|
class Kohana_Upload_File { |
13
|
|
|
|
14
|
|
|
protected $_source; |
15
|
|
|
|
16
|
|
|
protected $_server; |
17
|
|
|
|
18
|
|
|
protected $_path; |
19
|
|
|
|
20
|
|
|
protected $_temp; |
21
|
|
|
|
22
|
|
|
protected $_filename; |
23
|
|
|
|
24
|
|
|
protected $_transformations = array(); |
25
|
|
|
|
26
|
|
|
protected $_thumbnails = array(); |
27
|
|
|
|
28
|
|
|
protected $_extracted_from_source = FALSE; |
29
|
|
|
|
30
|
|
|
|
31
|
27 |
|
public function __construct($server, $path, $filename = NULL) |
32
|
|
|
{ |
33
|
27 |
|
$this->server($server); |
34
|
27 |
|
$this->_path = $path; |
35
|
|
|
|
36
|
27 |
|
if ($filename !== NULL) |
37
|
|
|
{ |
38
|
1 |
|
$this->_filename = $filename; |
39
|
|
|
} |
40
|
27 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get / Set the path for the image on the server |
44
|
|
|
* @param string $path |
45
|
|
|
* @return string|Upload_File |
46
|
|
|
*/ |
47
|
20 |
|
public function path($path = NULL) |
48
|
|
|
{ |
49
|
20 |
|
if ($path !== NULL) |
50
|
|
|
{ |
51
|
17 |
|
$this->_path = $path; |
52
|
|
|
|
53
|
17 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
4 |
|
return $this->_path; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function move_to_server($new_server) |
60
|
|
|
{ |
61
|
1 |
|
$file = Upload_Util::combine($this->temp()->directory_path(), $this->filename()); |
62
|
1 |
|
$old_file = $this->full_path(); |
63
|
|
|
|
64
|
1 |
|
if ( ! $this->server()->is_file($old_file)) |
65
|
|
|
throw new Kohana_Exception('File '.$old_file.' does not exist'); |
66
|
|
|
|
67
|
1 |
|
$this->server()->download_move($old_file, $file); |
68
|
|
|
|
69
|
1 |
|
foreach ($this->thumbnails() as $thumbnail => $thumbnail_params) |
|
|
|
|
70
|
|
|
{ |
71
|
1 |
|
$thumbnail_file = Upload_Util::combine($this->temp()->directory_path($thumbnail), $this->filename()); |
72
|
1 |
|
$old_thumbnail_file = $this->full_path($thumbnail); |
73
|
|
|
|
74
|
1 |
|
if ( ! $this->server()->is_file($old_thumbnail_file)) |
75
|
|
|
throw new Kohana_Exception('File '.$old_thumbnail_file.' does not exist'); |
76
|
|
|
|
77
|
1 |
|
$this->server()->download_move($old_thumbnail_file, $thumbnail_file); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
$this->server($new_server); |
81
|
1 |
|
$this->server()->upload_move($this->full_path(), $file); |
82
|
|
|
|
83
|
1 |
|
foreach ($this->thumbnails() as $thumbnail => $thumbnail_params) |
|
|
|
|
84
|
|
|
{ |
85
|
1 |
|
$thumbnail_file = Upload_Util::combine($this->temp()->directory_path($thumbnail), $this->filename()); |
86
|
1 |
|
$this->server()->upload_move($this->full_path($thumbnail), $thumbnail_file); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
$this->temp()->clear(); |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get / Set the source. Automatically set the source_type |
94
|
|
|
* |
95
|
|
|
* @param mixed $source |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
22 |
|
public function source($source = NULL) |
99
|
|
|
{ |
100
|
22 |
|
if ($source !== NULL) |
101
|
|
|
{ |
102
|
21 |
|
$this->_source = Upload_Source::factory($source); |
103
|
|
|
|
104
|
21 |
|
if ($this->_source->type() === Upload_Source::TYPE_TEMP) |
105
|
|
|
{ |
106
|
5 |
|
$this->temp()->directory(dirname($source)); |
107
|
5 |
|
$this->filename(basename($source)); |
108
|
|
|
} |
109
|
|
|
|
110
|
21 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
18 |
|
return $this->_source; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get / Set transformations |
118
|
|
|
* @param array $transformations |
119
|
|
|
* @return array|Upload_File |
120
|
|
|
*/ |
121
|
1 |
|
public function transformations(array $transformations = NULL) |
122
|
|
|
{ |
123
|
1 |
|
if ($transformations !== NULL) |
124
|
|
|
{ |
125
|
1 |
|
$this->_transformations = $transformations; |
126
|
|
|
|
127
|
1 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
return $this->_transformations; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get / Set thumbnails |
136
|
|
|
* @param array $thumbnails |
137
|
|
|
* @return array|Upload_File |
138
|
|
|
*/ |
139
|
4 |
|
public function thumbnails(array $thumbnails = NULL) |
140
|
|
|
{ |
141
|
4 |
|
if ($thumbnails !== NULL) |
142
|
|
|
{ |
143
|
3 |
|
$this->_thumbnails = $thumbnails; |
144
|
|
|
|
145
|
3 |
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
4 |
|
return $this->_thumbnails; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the Upload_Temp object. Create it if it's not already created |
153
|
|
|
* @return Upload_Temp |
154
|
|
|
*/ |
155
|
22 |
|
public function temp() |
156
|
|
|
{ |
157
|
22 |
|
if ( ! $this->_temp) |
158
|
|
|
{ |
159
|
22 |
|
$this->_temp = Upload_Temp::factory(); |
160
|
|
|
} |
161
|
|
|
|
162
|
22 |
|
return $this->_temp; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get the upload server |
167
|
|
|
* @return Upload_Server |
168
|
|
|
*/ |
169
|
27 |
|
public function server($server = NULL) |
170
|
|
|
{ |
171
|
27 |
|
if ($server !== NULL) |
172
|
|
|
{ |
173
|
27 |
|
$this->_server = $server; |
174
|
27 |
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
4 |
|
return Upload_Server::instance($this->_server); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get / Set the current filename |
182
|
|
|
* @param string $filename |
183
|
|
|
* @return string|Upload_File |
184
|
|
|
*/ |
185
|
26 |
|
public function filename($filename = NULL) |
186
|
|
|
{ |
187
|
26 |
|
if ($filename !== NULL) |
188
|
|
|
{ |
189
|
23 |
|
if (is_array($filename)) |
190
|
|
|
{ |
191
|
1 |
|
if (isset($filename['name'])) |
192
|
|
|
{ |
193
|
1 |
|
$this->_filename = $filename['name']; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
else |
197
|
|
|
{ |
198
|
23 |
|
$this->_filename = $filename; |
199
|
|
|
} |
200
|
|
|
|
201
|
23 |
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
25 |
|
return $this->_filename; |
205
|
|
|
} |
206
|
|
|
|
207
|
1 |
|
public function transform() |
208
|
|
|
{ |
209
|
1 |
|
if (($this->_transformations OR $this->_thumbnails) AND @ getimagesize($this->file())) |
210
|
|
|
{ |
211
|
1 |
|
if ($this->_transformations) |
|
|
|
|
212
|
|
|
{ |
213
|
1 |
|
Upload_Util::transform_image($this->file(), $this->file(), $this->transformations()); |
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
1 |
|
$this->generate_thumbnails(); |
217
|
|
|
} |
218
|
1 |
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Save the current source to the temp folder |
222
|
|
|
*/ |
223
|
13 |
|
public function save_to_temp() |
224
|
|
|
{ |
225
|
13 |
|
if ( ! $this->source()) |
226
|
|
|
throw new Kohana_Exception("Cannot move file to temp directory, source does not exist, path :path, filename :filename", array(':path' => $this->path(), ':filename' => $this->filename())); |
227
|
|
|
|
228
|
13 |
|
if ( ! $this->source()->copied()) |
229
|
|
|
{ |
230
|
13 |
|
$this->source()->copy_to($this->temp()->directory_path()); |
231
|
13 |
|
$this->filename($this->source()->filename()); |
232
|
|
|
} |
233
|
|
|
|
234
|
13 |
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Generate the thumbnails if they are not generated |
239
|
|
|
* |
240
|
|
|
* @return Upload_File $this |
241
|
|
|
*/ |
242
|
3 |
|
public function generate_thumbnails() |
243
|
|
|
{ |
244
|
3 |
|
foreach ($this->thumbnails() as $thumbnail => $thumbnail_params) |
|
|
|
|
245
|
|
|
{ |
246
|
2 |
|
if ( ! is_file($this->file($thumbnail))) |
247
|
|
|
{ |
248
|
2 |
|
Upload_Util::transform_image($this->file(), $this->file($thumbnail), $thumbnail_params['transformations']); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
3 |
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Save the file by moving it from temporary to the upload server |
257
|
|
|
* Generate the thumbnails if nesessary |
258
|
|
|
* @return Upload_File $this |
259
|
|
|
*/ |
260
|
1 |
|
public function save() |
261
|
|
|
{ |
262
|
1 |
|
if ($this->_thumbnails AND @ getimagesize($this->file())) |
263
|
|
|
{ |
264
|
1 |
|
$this->generate_thumbnails(); |
265
|
|
|
} |
266
|
|
|
|
267
|
1 |
|
$this->server()->upload_move($this->full_path(), $this->file()); |
268
|
|
|
|
269
|
1 |
|
foreach ($this->thumbnails() as $thumbnail => $thumbnail_params) |
|
|
|
|
270
|
|
|
{ |
271
|
1 |
|
$this->server()->upload_move($this->full_path($thumbnail), $this->file($thumbnail)); |
272
|
|
|
} |
273
|
|
|
|
274
|
1 |
|
$this->server()->unlink(dirname($this->file())); |
275
|
|
|
|
276
|
1 |
|
$this->_source = NULL; |
277
|
|
|
|
278
|
1 |
|
return $this; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Clear temporary files |
283
|
|
|
* @return Upload_File $this |
284
|
|
|
*/ |
285
|
16 |
|
public function clear() |
286
|
|
|
{ |
287
|
16 |
|
$this->temp()->clear(); |
288
|
|
|
|
289
|
16 |
|
return $this; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Delete the current file on the server and clear temporary files |
294
|
|
|
* @return Upload_File $this |
295
|
|
|
*/ |
296
|
1 |
|
public function delete() |
297
|
|
|
{ |
298
|
1 |
|
$this->server()->unlink($this->full_path()); |
299
|
|
|
|
300
|
1 |
|
foreach ($this->thumbnails() as $thumbnail => $transformations) |
|
|
|
|
301
|
|
|
{ |
302
|
1 |
|
$this->server()->unlink($this->full_path($thumbnail)); |
303
|
|
|
} |
304
|
|
|
|
305
|
1 |
|
$this->clear(); |
306
|
|
|
|
307
|
1 |
|
return $this; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Get the current filename (temp or server) |
312
|
|
|
* @param string $thumbnail |
313
|
|
|
* @return string |
314
|
|
|
*/ |
315
|
19 |
|
public function file($thumbnail = NULL) |
316
|
|
|
{ |
317
|
19 |
|
return $this->location('realpath', $thumbnail); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Get the current url (temp or server) |
322
|
|
|
* @param string $thumbnail |
323
|
|
|
* @param mixed $protocol |
|
|
|
|
324
|
|
|
* @return string |
325
|
|
|
*/ |
326
|
1 |
|
public function url($thumbnail = NULL) |
327
|
|
|
{ |
328
|
1 |
|
return $this->location('url', $thumbnail); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Get the full path with the filename |
333
|
|
|
* @param string $thumbnail |
334
|
|
|
* @return string |
335
|
|
|
*/ |
336
|
3 |
|
public function full_path($thumbnail = NULL) |
337
|
|
|
{ |
338
|
3 |
|
return Upload_Util::combine($this->path(), $thumbnail, $this->filename()); |
339
|
|
|
} |
340
|
|
|
|
341
|
1 |
|
public function temp_source() |
342
|
|
|
{ |
343
|
1 |
|
if ( ! $this->_source OR ! $this->filename()) |
344
|
1 |
|
return NULL; |
345
|
|
|
|
346
|
1 |
|
return $this->temp()->directory().'/'.$this->filename(); |
347
|
|
|
} |
348
|
|
|
|
349
|
19 |
|
protected function location($method, $thumbnail = NULL) |
350
|
|
|
{ |
351
|
19 |
|
if ( ! $this->filename()) |
352
|
|
|
return NULL; |
353
|
|
|
|
354
|
|
|
try |
355
|
|
|
{ |
356
|
19 |
|
if ($this->_source) |
357
|
|
|
{ |
358
|
17 |
|
return $this->temp()->$method(Upload_Util::combine($this->temp()->directory(), $thumbnail, $this->filename())); |
359
|
|
|
} |
360
|
|
|
else |
361
|
|
|
{ |
362
|
3 |
|
return $this->server()->$method($this->full_path($thumbnail)); |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
catch (Flex\Storage\Exception_Notsupported $exception) |
366
|
|
|
{ |
367
|
|
|
return NULL; |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Check if its empty (no filename or source) |
373
|
|
|
* @return boolean |
374
|
|
|
*/ |
375
|
14 |
|
public function is_empty() |
376
|
|
|
{ |
377
|
14 |
|
return ! $this->filename(); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
public function __toString() |
381
|
|
|
{ |
382
|
|
|
return (string) $this->filename(); |
383
|
|
|
} |
384
|
|
|
} |
385
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.