|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Handles different instances of the pictures |
|
4
|
|
|
* |
|
5
|
|
|
* @package Intraface |
|
6
|
|
|
* @author Sune |
|
7
|
|
|
* @version 1.0 |
|
8
|
|
|
*/ |
|
9
|
|
|
class InstanceHandler extends Intraface_Standard |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var object |
|
13
|
|
|
*/ |
|
14
|
|
|
private $file_handler; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $instance_path; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var integer id |
|
23
|
|
|
*/ |
|
24
|
|
|
private $id; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var object db MDB2 object |
|
28
|
|
|
*/ |
|
29
|
|
|
private $db; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Constructor |
|
33
|
|
|
* |
|
34
|
|
|
* @param object $file_handler File handler object |
|
35
|
|
|
* @param integer $id Optional id |
|
36
|
|
|
* |
|
37
|
|
|
* @return void |
|
|
|
|
|
|
38
|
|
|
*/ |
|
39
|
21 |
|
function __construct($file_handler, $id = 0) |
|
40
|
|
|
{ |
|
41
|
21 |
|
$this->file_handler = $file_handler; |
|
42
|
21 |
|
$this->id = (int)$id; |
|
43
|
21 |
|
$this->instance_path = $this->file_handler->getUploadPath().'instance/'; |
|
44
|
|
|
|
|
45
|
21 |
|
$this->db = MDB2::singleton(DB_DSN); |
|
46
|
|
|
|
|
47
|
21 |
|
if ($this->file_handler->get('is_image') == 0) { |
|
48
|
|
|
$this->id = 0; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
21 |
|
if ($this->id > 0) { |
|
52
|
2 |
|
$this->load(); |
|
53
|
2 |
|
} |
|
54
|
21 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* desctructor |
|
58
|
|
|
*/ |
|
59
|
20 |
|
public function __destruct() |
|
60
|
|
|
{ |
|
61
|
20 |
|
unset($this->file_handler); |
|
62
|
20 |
|
unset($this->instance_path); |
|
63
|
20 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Factory |
|
67
|
|
|
* |
|
68
|
|
|
* @param object $file_handler File handler |
|
69
|
|
|
* @param string $type the instance type |
|
|
|
|
|
|
70
|
|
|
* @param array $param one or more of crop_width, crop_height, crop_offset_x, crop_offset_y |
|
71
|
|
|
* |
|
72
|
|
|
* @return object |
|
73
|
|
|
*/ |
|
74
|
2 |
|
function factory($file_handler, $type_name, $param = array()) |
|
75
|
|
|
{ |
|
76
|
2 |
|
if ((int)$file_handler->get('id') == 0) { |
|
77
|
|
|
throw new Exception("You need a loaded file to create an instance"); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
if ($file_handler->get('is_image') == 0) { |
|
81
|
2 |
|
throw new Exception("File must be a picture"); |
|
82
|
2 |
|
} |
|
83
|
|
|
|
|
84
|
2 |
|
$instancehandler = new InstanceHandler($file_handler); |
|
85
|
2 |
|
$type = $instancehandler->checkType($type_name); |
|
86
|
2 |
|
if ($type === false) { |
|
87
|
|
|
throw new Exception("Ugyldig type '".$type_name."' i InstanceHandler->factory"); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
$db = new DB_sql; |
|
91
|
2 |
|
$db->query("SELECT id FROM file_handler_instance WHERE intranet_id = ".$file_handler->kernel->intranet->get('id')." AND active = 1 AND file_handler_id = ".$file_handler->get('id')." AND type_key = ".$type['type_key']); |
|
92
|
2 |
|
if ($db->nextRecord()) { |
|
93
|
|
|
return new InstanceHandler($file_handler, $db->f('id')); |
|
94
|
|
|
} else { |
|
95
|
2 |
|
$file_handler->createImage(); |
|
96
|
|
|
|
|
97
|
2 |
|
if (!empty($param['crop_width']) && !empty($param['crop_height'])) { |
|
98
|
1 |
|
settype($param['crop_offset_x'], 'integer'); |
|
99
|
1 |
|
settype($param['crop_offset_y'], 'integer'); |
|
100
|
1 |
|
$file_handler->image->crop($param['crop_width'], $param['crop_height'], $param['crop_offset_x'], $param['crop_offset_y']); |
|
101
|
|
|
// first we filter only crop parameters. |
|
102
|
1 |
|
$crop_param = array_intersect_key($param, array('crop_width' =>'', 'crop_height' => '', 'crop_offset_x' => '', 'crop_offset_y' => '')); |
|
103
|
1 |
|
$crop_param_string = serialize($crop_param); |
|
104
|
1 |
|
} else { |
|
105
|
1 |
|
$crop_param_string = serialize(array()); |
|
106
|
|
|
} |
|
107
|
2 |
|
$file = $file_handler->image->resize($type['max_width'], $type['max_height'], $type['resize_type']); |
|
108
|
|
|
|
|
109
|
2 |
|
if (!is_file($file)) { |
|
110
|
|
|
throw new Exception("Filen blev ikke opretett i InstanceHandler->factory"); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
2 |
|
$file_size = filesize($file); |
|
114
|
2 |
|
$imagesize = getimagesize($file); |
|
115
|
2 |
|
$width = $imagesize[0]; // imagesx($file); |
|
|
|
|
|
|
116
|
2 |
|
$height = $imagesize[1]; // imagesy($file); |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
2 |
|
$db->query("INSERT INTO file_handler_instance SET |
|
119
|
2 |
|
intranet_id = ".$file_handler->kernel->intranet->get('id').", |
|
120
|
2 |
|
file_handler_id = ".$file_handler->get('id').", |
|
121
|
|
|
date_created = NOW(), |
|
122
|
|
|
date_changed = NOW(), |
|
123
|
2 |
|
type_key = ".$type['type_key'].", |
|
124
|
2 |
|
file_size = ".(int)$file_size.", |
|
125
|
2 |
|
width = ".(int)$width.", |
|
126
|
2 |
|
height = ".(int)$height.", " . |
|
127
|
2 |
|
"crop_parameter = \"".safeToDb($crop_param_string)."\""); |
|
128
|
|
|
|
|
129
|
2 |
|
$id = $db->insertedId(); |
|
130
|
|
|
|
|
131
|
2 |
|
$mime_type = $file_handler->get('file_type'); |
|
132
|
2 |
|
$server_file_name = $id.'.'.$mime_type['extension']; |
|
133
|
|
|
|
|
134
|
2 |
|
if (!is_dir($instancehandler->instance_path)) { |
|
135
|
2 |
|
if (!mkdir($instancehandler->instance_path, 0755)) { |
|
136
|
|
|
$this->delete(); |
|
137
|
|
|
throw new Exception("Kunne ikke oprette mappe i InstanceHandler->factory"); |
|
138
|
|
|
} |
|
139
|
2 |
|
} |
|
140
|
|
|
|
|
141
|
2 |
|
if (!rename($file, $instancehandler->instance_path.$server_file_name)) { |
|
142
|
|
|
throw new Exception("Det var ikke muligt at flytte fil i InstanceHandler->factory"); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
2 |
|
if (!chmod($instancehandler->instance_path.$server_file_name, 0644)) { |
|
146
|
|
|
// please do not stop executing here |
|
147
|
|
|
throw new Exception("Unable to chmod file '".$instancehandler->instance_path.$server_file_name."'"); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
2 |
|
$db->query("UPDATE file_handler_instance SET server_file_name = \"".$server_file_name."\", active = 1 WHERE intranet_id = ".$file_handler->kernel->intranet->get('id')." AND id = ".$id); |
|
151
|
|
|
|
|
152
|
2 |
|
return new InstanceHandler($file_handler, $id); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Henter en instance af et billede. |
|
159
|
|
|
* |
|
160
|
|
|
* @return boolean true or false |
|
161
|
|
|
*/ |
|
162
|
2 |
|
private function load() |
|
163
|
|
|
{ |
|
164
|
|
|
|
|
165
|
2 |
|
$db = new DB_sql; |
|
166
|
2 |
|
$db->query("SELECT * FROM file_handler_instance WHERE intranet_id = ".$this->file_handler->kernel->intranet->get('id')." AND active = 1 AND id = ".$this->id); |
|
167
|
2 |
View Code Duplication |
if (!$db->nextRecord()) { |
|
168
|
|
|
$this->id = 0; |
|
169
|
|
|
$this->value['id'] = 0; |
|
170
|
1 |
|
return false; |
|
171
|
|
|
} |
|
172
|
2 |
|
$type = $this->checkType((int)$db->f('type_key'), 'type_key'); |
|
173
|
2 |
|
if ($type === false) { |
|
174
|
|
|
$this->id = 0; |
|
175
|
|
|
$this->value['id'] = 0; |
|
176
|
|
|
return false; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
2 |
|
$this->value['instance_properties'] = $type; |
|
180
|
2 |
|
$this->value['type'] = $type['name']; |
|
181
|
|
|
|
|
182
|
2 |
|
$this->id = $db->f('id'); |
|
183
|
2 |
|
$this->value['id'] = $db->f('id'); |
|
184
|
2 |
|
$this->value['date_created'] = $db->f('date_created'); |
|
185
|
2 |
|
$this->value['date_changed'] = $db->f('date_changed'); |
|
186
|
|
|
|
|
187
|
|
|
//$this->value['predefined_size'] = $db->f('predefined_size'); |
|
|
|
|
|
|
188
|
2 |
|
$this->value['server_file_name'] = $db->f('server_file_name'); |
|
189
|
2 |
|
$this->value['file_size'] = $db->f('file_size'); |
|
190
|
2 |
|
$this->value['file_path'] = $this->instance_path . $db->f('server_file_name'); |
|
191
|
|
|
|
|
192
|
2 |
|
$this->value['last_modified'] = filemtime($this->get('file_path')); |
|
193
|
|
|
// $this->value['file_uri'] = FILE_VIEWER.'?id='.$this->get('id').'&type='.$this->get('type').'&name=/'.urlencode($this->file_handler->get('file_name')); |
|
|
|
|
|
|
194
|
2 |
|
$this->value['file_uri_parameters'] = '?/'.$this->file_handler->kernel->intranet->get('public_key').'/'.$this->file_handler->get('access_key').'/'.$this->get('type').'/'.urlencode($this->file_handler->get('file_name')); |
|
195
|
2 |
|
$this->value['file_uri'] = FILE_VIEWER.$this->value['file_uri_parameters']; |
|
196
|
|
|
|
|
197
|
2 |
View Code Duplication |
if ($db->f('width') == 0) { |
|
198
|
|
|
$imagesize = getimagesize($this->get('file_path')); |
|
199
|
|
|
$this->value['width'] = $imagesize[0]; // imagesx($this->get('file_uri')); |
|
|
|
|
|
|
200
|
|
|
$db2 = new DB_sql; |
|
201
|
|
|
$db2->query("UPDATE file_handler_instance SET width = ".$this->value['width']." WHERE intranet_id = ".$this->file_handler->kernel->intranet->get('id')." AND id = ".$this->id); |
|
202
|
|
|
} else { |
|
203
|
2 |
|
$this->value['width'] = $db->f('width'); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
2 |
View Code Duplication |
if ($db->f('height') == 0) { |
|
207
|
|
|
$imagesize = getimagesize($this->get('file_path')); |
|
208
|
|
|
$this->value['height'] = $imagesize[1]; //imagesy($this->get('file_uri')); |
|
|
|
|
|
|
209
|
|
|
$db2 = new DB_sql; |
|
210
|
|
|
$db2->query("UPDATE file_handler_instance SET height = ".$this->value['height']." WHERE intranet_id = ".$this->file_handler->kernel->intranet->get('id')." AND id = ".$this->id); |
|
211
|
|
|
} else { |
|
212
|
2 |
|
$this->value['height'] = $db->f('height'); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
2 |
|
return true; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Returns an array with instance types included information about the instance |
|
220
|
|
|
* |
|
221
|
|
|
* @return array |
|
222
|
|
|
*/ |
|
223
|
18 |
|
function getList($show = 'visible') |
|
224
|
|
|
{ |
|
225
|
18 |
|
if (!in_array($show, array('visible', 'include_hidden'))) { |
|
226
|
|
|
throw new Exception('First parameter to InstanceManager->getList should either be visibe or include_hidden'); |
|
227
|
|
|
exit; |
|
|
|
|
|
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
18 |
|
$db = new DB_Sql; |
|
|
|
|
|
|
231
|
18 |
|
require_once('Intraface/modules/filemanager/InstanceManager.php'); |
|
232
|
18 |
|
$instancemanager = new InstanceManager($this->file_handler->kernel); |
|
233
|
18 |
|
$types = $instancemanager->getList($show); |
|
234
|
18 |
|
$i = 0; |
|
|
|
|
|
|
235
|
|
|
// if filehander has an id we supply the file information to the array. |
|
236
|
18 |
|
if ($this->file_handler->get('id') != 0) { |
|
237
|
18 |
|
$result = $this->db->query("SELECT id, width, height, type_key, file_size FROM file_handler_instance WHERE intranet_id = ".$this->file_handler->kernel->intranet->get('id')." AND file_handler_id = ".$this->file_handler->get('id')." AND active = 1 ORDER BY type_key"); |
|
238
|
18 |
|
if (PEAR::isError($result)) { |
|
239
|
|
|
throw new Exception("Error in query: ".$result->getUserInfo()); |
|
240
|
|
|
exit; |
|
|
|
|
|
|
241
|
|
|
} |
|
242
|
18 |
|
$file_instances = $result->fetchAll(MDB2_FETCHMODE_ASSOC); |
|
243
|
|
|
|
|
244
|
18 |
|
$this->file_handler->createImage(); |
|
245
|
|
|
|
|
246
|
18 |
|
for ($i = 0, $max = count($types); $i < $max; $i++) { |
|
247
|
18 |
|
$types[$i]['width'] = ''; |
|
248
|
18 |
|
$types[$i]['height'] = ''; |
|
249
|
18 |
|
$types[$i]['file_size'] = '-'; |
|
250
|
18 |
|
$types[$i]['file_uri'] = FILE_VIEWER.'?/'.$this->file_handler->kernel->intranet->get('public_key').'/'.$this->file_handler->get('access_key').'/'.$types[$i]['name'].'/'.urlencode($this->file_handler->get('file_name')); |
|
251
|
|
|
|
|
252
|
18 |
|
$match_file_instance_key = false; |
|
253
|
18 |
|
foreach ($file_instances as $file_instance_key => $file_instance) { |
|
254
|
|
|
if ($file_instance['type_key'] == $types[$i]['type_key']) { |
|
255
|
|
|
$match_file_instance_key = $file_instance_key; |
|
256
|
|
|
break; |
|
257
|
|
|
} |
|
258
|
18 |
|
} |
|
259
|
|
|
|
|
260
|
18 |
|
if ($match_file_instance_key !== false) { |
|
261
|
|
|
$types[$i]['width'] = $file_instances[$match_file_instance_key]['width']; |
|
262
|
|
|
$types[$i]['height'] = $file_instances[$match_file_instance_key]['height']; |
|
263
|
|
|
$types[$i]['file_size'] = $file_instances[$match_file_instance_key]['file_size']; |
|
264
|
18 |
|
} elseif (isset($types[$i]['max_width']) && isset($types[$i]['max_height']) && isset($types[$i]['resize_type']) && $types[$i]['resize_type'] == 'strict') { |
|
265
|
18 |
|
$types[$i]['width'] = $types[$i]['max_width']; |
|
266
|
18 |
|
$types[$i]['height'] = $types[$i]['max_height']; |
|
267
|
18 |
|
} elseif (isset($types[$i]['max_width']) && isset($types[$i]['max_height'])) { |
|
268
|
18 |
|
$tmp_size = $this->file_handler->image->getRelativeSize($types[$i]['max_width'], $types[$i]['max_height']); |
|
269
|
18 |
|
$types[$i]['width'] = $tmp_size['width']; |
|
270
|
18 |
|
$types[$i]['height'] = $tmp_size['height']; |
|
271
|
18 |
|
} |
|
272
|
18 |
|
} |
|
273
|
18 |
|
} |
|
274
|
|
|
|
|
275
|
18 |
|
return $types; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* check the instance type |
|
280
|
|
|
* |
|
281
|
|
|
* @param string $type name of type |
|
282
|
|
|
* @param string $compare either 'name' or 'type_key' |
|
283
|
|
|
* |
|
284
|
|
|
* @return mixed array with the type or false on failure; |
|
285
|
|
|
*/ |
|
286
|
20 |
|
public function checkType($type, $compare = 'name') |
|
287
|
|
|
{ |
|
288
|
20 |
|
if (!in_array($compare, array('name', 'type_key'))) { |
|
289
|
|
|
throw new Exception('Second parameter to InstanceHander->checkType should be either name or type_key'); |
|
290
|
|
|
return false; |
|
|
|
|
|
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
2 |
|
require_once 'Intraface/modules/filemanager/InstanceManager.php'; |
|
294
|
2 |
|
$instancemanager = new InstanceManager($this->file_handler->kernel); |
|
295
|
2 |
|
$instance_types = $instancemanager->getList('include_hidden'); |
|
296
|
|
|
|
|
297
|
2 |
|
for ($i = 0, $max = count($instance_types); $i < $max; $i++) { |
|
298
|
2 |
|
if (isset($instance_types[$i][$compare]) && $instance_types[$i][$compare] == $type) { |
|
299
|
2 |
|
return $instance_types[$i]; |
|
300
|
|
|
exit; |
|
|
|
|
|
|
301
|
|
|
} |
|
302
|
2 |
|
} |
|
303
|
|
|
return false; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* Deletes an instance |
|
308
|
|
|
* |
|
309
|
|
|
* @return boolean |
|
310
|
|
|
*/ |
|
311
|
|
View Code Duplication |
function delete() |
|
|
|
|
|
|
312
|
|
|
{ |
|
313
|
|
|
if ($this->id == 0) { |
|
314
|
|
|
return false; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
$db = new DB_Sql; |
|
318
|
|
|
|
|
319
|
|
|
if (file_exists($this->get('file_path'))) { |
|
320
|
|
|
if (!rename($this->get('file_path'), $this->instance_path.'_deleted_'.$this->get('server_file_name'))) { |
|
321
|
|
|
throw new Exception("Kunne ikke omd�be filen i InstanceHandler->delete()"); |
|
322
|
|
|
} |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
$db->query("UPDATE file_handler_instance SET active = 0 WHERE intranet_id = ".$this->file_handler->kernel->intranet->get('id')." AND id = ".$this->id); |
|
326
|
|
|
return true; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
/** |
|
330
|
|
|
* Deletes all instances for a file |
|
331
|
|
|
* |
|
332
|
|
|
* @param boolean |
|
333
|
|
|
*/ |
|
334
|
|
|
function deleteAll() |
|
335
|
|
|
{ |
|
336
|
|
|
if ($this->file_handler->get('id') == 0) { |
|
337
|
|
|
throw new Exception('An file_handler_id is needed to delete instances in InstanceHandler->deleteAll()'); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
$db = new DB_sql; |
|
341
|
|
|
$db->query("SELECT id FROM file_handler_instance WHERE intranet_id = ".$this->file_handler->kernel->intranet->get('id')." AND file_handler_id = ".$this->file_handler->get('id')." AND active = 1"); |
|
342
|
|
|
while ($db->nextRecord()) { |
|
343
|
|
|
$instance = new InstanceHandler($this->file_handler, $db->f('id')); |
|
344
|
|
|
$instance->delete(); |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
return true; |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
/** |
|
351
|
|
|
* deletes all instances of a type |
|
352
|
|
|
* |
|
353
|
|
|
* @param string $instance instance representation either name or type_key depending on next parameter |
|
354
|
|
|
* @param string $compare either 'name' or 'type_key' |
|
355
|
|
|
* |
|
356
|
|
|
*/ |
|
357
|
|
|
public function deleteInstanceType($instance, $compare = 'name') |
|
|
|
|
|
|
358
|
|
|
{ |
|
359
|
|
|
throw new Exception('so fare not used!'); |
|
360
|
|
|
exit; |
|
|
|
|
|
|
361
|
|
|
$type = $this->checkType($instance, $compare); |
|
|
|
|
|
|
362
|
|
|
$db = new DB_sql; |
|
363
|
|
|
$db->query("SELECT id FROM file_handler_instance WHERE intranet_id = ".$this->file_handler->kernel->intranet->get('id')." AND type_key = ".intval($type['type_key'])." AND active = 1"); |
|
364
|
|
|
while ($db->nextRecord()) { |
|
365
|
|
|
$instance = new InstanceHandler($this->file_handler, $db->f('id')); |
|
366
|
|
|
$instance->delete(); |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
return true; |
|
370
|
|
|
} |
|
371
|
|
|
} |
|
372
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.