Elgg  Version 1.10
filestore.php
Go to the documentation of this file.
1 <?php
18 function get_dir_size($dir, $total_size = 0) {
19  $handle = @opendir($dir);
20  while ($file = @readdir($handle)) {
21  if (in_array($file, array('.', '..'))) {
22  continue;
23  }
24  if (is_dir($dir . $file)) {
25  $total_size = get_dir_size($dir . $file . "/", $total_size);
26  } else {
27  $total_size += filesize($dir . $file);
28  }
29  }
30  @closedir($handle);
31 
32  return($total_size);
33 }
34 
44  $files = _elgg_services()->request->files;
45  if (!$files->has($input_name)) {
46  return false;
47  }
48 
49  $file = $files->get($input_name);
50  if (elgg_extract('error', $file) !== 0) {
51  return false;
52  }
53 
54  return file_get_contents(elgg_extract('tmp_name', $file));
55 }
56 
71 function get_resized_image_from_uploaded_file($input_name, $maxwidth, $maxheight,
72 $square = false, $upscale = false) {
73  $files = _elgg_services()->request->files;
74  if (!$files->has($input_name)) {
75  return false;
76  }
77 
78  $file = $files->get($input_name);
79  if (elgg_extract('error', $file) !== 0) {
80  return false;
81  }
82 
83  return get_resized_image_from_existing_file(elgg_extract('tmp_name', $file), $maxwidth,
84  $maxheight, $square, 0, 0, 0, 0, $upscale);
85 }
86 
107 function get_resized_image_from_existing_file($input_name, $maxwidth, $maxheight, $square = false,
108 $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0, $upscale = false) {
109 
110  // Get the size information from the image
111  $imgsizearray = getimagesize($input_name);
112  if ($imgsizearray == false) {
113  return false;
114  }
115 
116  $width = $imgsizearray[0];
117  $height = $imgsizearray[1];
118 
119  $accepted_formats = array(
120  'image/jpeg' => 'jpeg',
121  'image/pjpeg' => 'jpeg',
122  'image/png' => 'png',
123  'image/x-png' => 'png',
124  'image/gif' => 'gif'
125  );
126 
127  // make sure the function is available
128  $load_function = "imagecreatefrom" . $accepted_formats[$imgsizearray['mime']];
129  if (!is_callable($load_function)) {
130  return false;
131  }
132 
133  // get the parameters for resizing the image
134  $options = array(
135  'maxwidth' => $maxwidth,
136  'maxheight' => $maxheight,
137  'square' => $square,
138  'upscale' => $upscale,
139  'x1' => $x1,
140  'y1' => $y1,
141  'x2' => $x2,
142  'y2' => $y2,
143  );
144  $params = get_image_resize_parameters($width, $height, $options);
145  if ($params == false) {
146  return false;
147  }
148 
149  // load original image
150  $original_image = call_user_func($load_function, $input_name);
151  if (!$original_image) {
152  return false;
153  }
154 
155  // allocate the new image
156  $new_image = imagecreatetruecolor($params['newwidth'], $params['newheight']);
157  if (!$new_image) {
158  return false;
159  }
160 
161  // color transparencies white (default is black)
162  imagefilledrectangle(
163  $new_image, 0, 0, $params['newwidth'], $params['newheight'],
164  imagecolorallocate($new_image, 255, 255, 255)
165  );
166 
167  $rtn_code = imagecopyresampled( $new_image,
168  $original_image,
169  0,
170  0,
171  $params['xoffset'],
172  $params['yoffset'],
173  $params['newwidth'],
174  $params['newheight'],
175  $params['selectionwidth'],
176  $params['selectionheight']);
177  if (!$rtn_code) {
178  return false;
179  }
180 
181  // grab a compressed jpeg version of the image
182  ob_start();
183  imagejpeg($new_image, null, 90);
184  $jpeg = ob_get_clean();
185 
186  imagedestroy($new_image);
187  imagedestroy($original_image);
188 
189  return $jpeg;
190 }
191 
202 function get_image_resize_parameters($width, $height, $options) {
203 
204  $defaults = array(
205  'maxwidth' => 100,
206  'maxheight' => 100,
207 
208  'square' => false,
209  'upscale' => false,
210 
211  'x1' => 0,
212  'y1' => 0,
213  'x2' => 0,
214  'y2' => 0,
215  );
216 
217  $options = array_merge($defaults, $options);
218 
219  // Avoiding extract() because it hurts static analysis
220  $maxwidth = $options['maxwidth'];
221  $maxheight = $options['maxheight'];
222  $square = $options['square'];
223  $upscale = $options['upscale'];
224  $x1 = $options['x1'];
225  $y1 = $options['y1'];
226  $x2 = $options['x2'];
227  $y2 = $options['y2'];
228 
229  // crop image first?
230  $crop = true;
231  if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 == 0) {
232  $crop = false;
233  }
234 
235  // how large a section of the image has been selected
236  if ($crop) {
237  $selection_width = $x2 - $x1;
238  $selection_height = $y2 - $y1;
239  } else {
240  // everything selected if no crop parameters
241  $selection_width = $width;
242  $selection_height = $height;
243  }
244 
245  // determine cropping offsets
246  if ($square) {
247  // asking for a square image back
248 
249  // detect case where someone is passing crop parameters that are not for a square
250  if ($crop == true && $selection_width != $selection_height) {
251  return false;
252  }
253 
254  // size of the new square image
255  $new_width = $new_height = min($maxwidth, $maxheight);
256 
257  // find largest square that fits within the selected region
258  $selection_width = $selection_height = min($selection_width, $selection_height);
259 
260  // set offsets for crop
261  if ($crop) {
262  $widthoffset = $x1;
263  $heightoffset = $y1;
264  $width = $x2 - $x1;
265  $height = $width;
266  } else {
267  // place square region in the center
268  $widthoffset = floor(($width - $selection_width) / 2);
269  $heightoffset = floor(($height - $selection_height) / 2);
270  }
271  } else {
272  // non-square new image
273  $new_width = $maxwidth;
274  $new_height = $maxheight;
275 
276  // maintain aspect ratio of original image/crop
277  if (($selection_height / (float)$new_height) > ($selection_width / (float)$new_width)) {
278  $new_width = floor($new_height * $selection_width / (float)$selection_height);
279  } else {
280  $new_height = floor($new_width * $selection_height / (float)$selection_width);
281  }
282 
283  // by default, use entire image
284  $widthoffset = 0;
285  $heightoffset = 0;
286 
287  if ($crop) {
288  $widthoffset = $x1;
289  $heightoffset = $y1;
290  }
291  }
292 
293  if (!$upscale && ($selection_height < $new_height || $selection_width < $new_width)) {
294  // we cannot upscale and selected area is too small so we decrease size of returned image
295  if ($square) {
296  $new_height = $selection_height;
297  $new_width = $selection_width;
298  } else {
299  if ($selection_height < $new_height && $selection_width < $new_width) {
300  $new_height = $selection_height;
301  $new_width = $selection_width;
302  }
303  }
304  }
305 
306  $params = array(
307  'newwidth' => $new_width,
308  'newheight' => $new_height,
309  'selectionwidth' => $selection_width,
310  'selectionheight' => $selection_height,
311  'xoffset' => $widthoffset,
312  'yoffset' => $heightoffset,
313  );
314 
315  return $params;
316 }
317 
325 function file_delete($guid) {
326  $file = get_entity($guid);
327  if (!$file || !$file->canEdit()) {
328  return false;
329  }
330 
331  $thumbnail = $file->thumbnail;
332  $smallthumb = $file->smallthumb;
333  $largethumb = $file->largethumb;
334  if ($thumbnail) {
335  $delfile = new \ElggFile();
336  $delfile->owner_guid = $file->owner_guid;
337  $delfile->setFilename($thumbnail);
338  $delfile->delete();
339  }
340  if ($smallthumb) {
341  $delfile = new \ElggFile();
342  $delfile->owner_guid = $file->owner_guid;
343  $delfile->setFilename($smallthumb);
344  $delfile->delete();
345  }
346  if ($largethumb) {
347  $delfile = new \ElggFile();
348  $delfile->owner_guid = $file->owner_guid;
349  $delfile->setFilename($largethumb);
350  $delfile->delete();
351  }
352 
353  return $file->delete();
354 }
355 
363 function delete_directory($directory) {
364  // sanity check: must be a directory
365  if (!$handle = opendir($directory)) {
366  return false;
367  }
368 
369  // loop through all files
370  while (($file = readdir($handle)) !== false) {
371  if (in_array($file, array('.', '..'))) {
372  continue;
373  }
374 
375  $path = "$directory/$file";
376  if (is_dir($path)) {
377  // recurse down through directory
378  if (!delete_directory($path)) {
379  return false;
380  }
381  } else {
382  // delete file
383  unlink($path);
384  }
385  }
386 
387  // remove empty directory
388  closedir($handle);
389  return rmdir($directory);
390 }
391 
408  $dir = new \Elgg\EntityDirLocator($entity->guid);
409  $file_path = elgg_get_config('dataroot') . $dir;
410  if (file_exists($file_path)) {
411  delete_directory($file_path);
412  }
413 }
414 
415 
418 
426 
427  return $DEFAULT_FILE_STORE;
428 }
429 
437 function set_default_filestore(\ElggFilestore $filestore) {
439 
440  $DEFAULT_FILE_STORE = $filestore;
441 
442  return true;
443 }
444 
453 function elgg_get_file_simple_type($mime_type) {
454  $params = array('mime_type' => $mime_type);
455  return elgg_trigger_plugin_hook('simple_type', 'file', $params, 'general');
456 }
457 
466  global $CONFIG;
467 
468  // Now register a default filestore
469  if (isset($CONFIG->dataroot)) {
470  set_default_filestore(new \ElggDiskFilestore($CONFIG->dataroot));
471  }
472 
473  // Fix MIME type detection for Microsoft zipped formats
474  elgg_register_plugin_hook_handler('mime_type', 'file', '_elgg_filestore_detect_mimetype');
475 
476  // Parse category of file from MIME type
477  elgg_register_plugin_hook_handler('simple_type', 'file', '_elgg_filestore_parse_simpletype');
478 
479  // Unit testing
480  elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_filestore_test');
481 }
482 
494 function _elgg_filestore_detect_mimetype($hook, $type, $mime_type, $params) {
495 
496  $original_filename = elgg_extract('original_filename', $params);
497 
498  $info = pathinfo($original_filename);
499 
500  // hack for Microsoft zipped formats
501  $office_formats = array('docx', 'xlsx', 'pptx');
502  if ($mime_type == "application/zip" && in_array($info['extension'], $office_formats)) {
503  switch ($info['extension']) {
504  case 'docx':
505  $mime_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
506  break;
507  case 'xlsx':
508  $mime_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
509  break;
510  case 'pptx':
511  $mime_type = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
512  break;
513  }
514  }
515 
516  // check for bad ppt detection
517  if ($mime_type == "application/vnd.ms-office" && $info['extension'] == "ppt") {
518  $mime_type = "application/vnd.ms-powerpoint";
519  }
520 
521  return $mime_type;
522 }
523 
535 function _elgg_filestore_parse_simpletype($hook, $type, $simple_type, $params) {
536 
537  $mime_type = elgg_extract('mime_type', $params);
538 
539  switch ($mime_type) {
540  case "application/msword":
541  case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
542  case "application/pdf":
543  return "document";
544 
545  case "application/ogg":
546  return "audio";
547  }
548 
549  if (preg_match('~^(audio|image|video)/~', $mime_type, $m)) {
550  return $m[1];
551  }
552  if (0 === strpos($mime_type, 'text/') || false !== strpos($mime_type, 'opendocument')) {
553  return "document";
554  }
555 
556  // unrecognized MIME
557  return $simple_type;
558 }
559 
570 function _elgg_filestore_test($hook, $type, $value) {
571  global $CONFIG;
572  $value[] = "{$CONFIG->path}engine/tests/ElggCoreFilestoreTest.php";
573  return $value;
574 }
575 
576 elgg_register_event_handler('init', 'system', '_elgg_filestore_init', 100);
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
$m
Definition: metadata.php:11
$DEFAULT_FILE_STORE
Variable holding the default datastore.
Definition: filestore.php:417
$input_name
Definition: item.php:14
$files
Definition: crop.php:36
$value
Definition: longtext.php:29
get_dir_size($dir, $total_size=0)
Get the size of the specified directory.
Definition: filestore.php:18
if(!$autoload_available) _elgg_services()
Definition: autoloader.php:20
_elgg_filestore_parse_simpletype($hook, $type, $simple_type, $params)
Parse a file category of file from a MIME type.
Definition: filestore.php:535
$guid
Removes an admin notice.
elgg_extract($key, array $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1349
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Register a callback as a plugin hook handler.
Definition: elgglib.php:737
if(!$owner||!($owner instanceof ElggUser)||!$owner->canEdit()) $x1
Definition: crop.php:15
get_image_resize_parameters($width, $height, $options)
Calculate the parameters for resizing an image.
Definition: filestore.php:202
get_default_filestore()
Return the default filestore.
Definition: filestore.php:424
$params
Definition: login.php:72
$options
Definition: index.php:14
get_resized_image_from_existing_file($input_name, $maxwidth, $maxheight, $square=false, $x1=0, $y1=0, $x2=0, $y2=0, $upscale=false)
Gets the jpeg contents of the resized version of an already uploaded image (Returns false if the file...
Definition: filestore.php:107
get_uploaded_file($input_name)
Get the contents of an uploaded file.
Definition: filestore.php:43
global $CONFIG
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Trigger a Plugin Hook and run all handler callbacks registered to that hook:type. ...
Definition: elgglib.php:809
elgg global
Pointer to the global context.
Definition: elgglib.js:12
$type
Definition: add.php:8
file_delete($guid)
Delete an file.
Definition: filestore.php:325
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Register a callback as an Elgg event handler.
Definition: elgglib.php:553
$y1
Definition: crop.php:16
_elgg_filestore_detect_mimetype($hook, $type, $mime_type, $params)
Fix MIME type detection for Microsoft zipped formats.
Definition: filestore.php:494
get_resized_image_from_uploaded_file($input_name, $maxwidth, $maxheight, $square=false, $upscale=false)
Gets the jpeg contents of the resized version of an uploaded image (Returns false if the uploaded fil...
Definition: filestore.php:71
delete_directory($directory)
Delete a directory and all its contents.
Definition: filestore.php:363
$x2
Definition: crop.php:17
set_default_filestore(\ElggFilestore $filestore)
Set the default filestore for the system.
Definition: filestore.php:437
$defaults
Definition: access.php:19
$entity
Definition: delete.php:10
_elgg_filestore_init()
Initialize the file library.
Definition: filestore.php:465
$path
Definition: invalid.php:17
_elgg_filestore_test($hook, $type, $value)
Unit tests for files.
Definition: filestore.php:570
_elgg_clear_entity_files($entity)
Removes all entity files.
Definition: filestore.php:407
elgg_get_file_simple_type($mime_type)
Returns the category of a file from its MIME type.
Definition: filestore.php:453
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:382
$y2
Definition: crop.php:18