Elgg  Version 1.9
filestore.php
Go to the documentation of this file.
1 <?php
18 function get_dir_size($dir, $totalsize = 0) {
19  $handle = @opendir($dir);
20  while ($file = @readdir($handle)) {
21  if (eregi("^\.{1,2}$", $file)) {
22  continue;
23  }
24  if (is_dir($dir . $file)) {
25  $totalsize = get_dir_size($dir . $file . "/", $totalsize);
26  } else {
27  $totalsize += filesize($dir . $file);
28  }
29  }
30  @closedir($handle);
31 
32  return($totalsize);
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  if ($file = get_entity($guid)) {
327  if ($file->canEdit()) {
328  $thumbnail = $file->thumbnail;
329  $smallthumb = $file->smallthumb;
330  $largethumb = $file->largethumb;
331  if ($thumbnail) {
332  $delfile = new ElggFile();
333  $delfile->owner_guid = $file->owner_guid;
334  $delfile->setFilename($thumbnail);
335  $delfile->delete();
336  }
337  if ($smallthumb) {
338  $delfile = new ElggFile();
339  $delfile->owner_guid = $file->owner_guid;
340  $delfile->setFilename($smallthumb);
341  $delfile->delete();
342  }
343  if ($largethumb) {
344  $delfile = new ElggFile();
345  $delfile->owner_guid = $file->owner_guid;
346  $delfile->setFilename($largethumb);
347  $delfile->delete();
348  }
349 
350  return $file->delete();
351  }
352  }
353 
354  return false;
355 }
356 
364 function file_get_general_file_type($mimetype) {
365  switch($mimetype) {
366 
367  case "application/msword":
368  return "document";
369  break;
370  case "application/pdf":
371  return "document";
372  break;
373  }
374 
375  if (substr_count($mimetype, 'text/')) {
376  return "document";
377  }
378 
379  if (substr_count($mimetype, 'audio/')) {
380  return "audio";
381  }
382 
383  if (substr_count($mimetype, 'image/')) {
384  return "image";
385  }
386 
387  if (substr_count($mimetype, 'video/')) {
388  return "video";
389  }
390 
391  if (substr_count($mimetype, 'opendocument')) {
392  return "document";
393  }
394 
395  return "general";
396 }
397 
405 function delete_directory($directory) {
406  // sanity check: must be a directory
407  if (!$handle = opendir($directory)) {
408  return false;
409  }
410 
411  // loop through all files
412  while (($file = readdir($handle)) !== false) {
413  if (in_array($file, array('.', '..'))) {
414  continue;
415  }
416 
417  $path = "$directory/$file";
418  if (is_dir($path)) {
419  // recurse down through directory
420  if (!delete_directory($path)) {
421  return false;
422  }
423  } else {
424  // delete file
425  unlink($path);
426  }
427  }
428 
429  // remove empty directory
430  closedir($handle);
431  return rmdir($directory);
432 }
433 
449  global $CONFIG;
450 
451  $dir = new Elgg_EntityDirLocator($user->guid);
452  $file_path = $CONFIG->dataroot . $dir;
453  if (file_exists($file_path)) {
454  delete_directory($file_path);
455  }
456 }
457 
458 
461 
469 
470  return $DEFAULT_FILE_STORE;
471 }
472 
480 function set_default_filestore(ElggFilestore $filestore) {
482 
483  $DEFAULT_FILE_STORE = $filestore;
484 
485  return true;
486 }
487 
496  global $CONFIG;
497 
498  // Now register a default filestore
499  if (isset($CONFIG->dataroot)) {
500  set_default_filestore(new ElggDiskFilestore($CONFIG->dataroot));
501  }
502 
503  // Unit testing
504  elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_filestore_test');
505 }
506 
517 function _elgg_filestore_test($hook, $type, $value) {
518  global $CONFIG;
519  $value[] = "{$CONFIG->path}engine/tests/ElggCoreFilestoreTest.php";
520  return $value;
521 }
522 
523 elgg_register_event_handler('init', 'system', '_elgg_filestore_init', 100);
get_dir_size($dir, $totalsize=0)
Get the size of the specified directory.
Definition: filestore.php:18
$DEFAULT_FILE_STORE
Variable holding the default datastore.
Definition: filestore.php:460
clear_user_files($user)
Removes all user files.
Definition: filestore.php:448
$input_name
Definition: item.php:14
file_get_general_file_type($mimetype)
Returns an overall file type from the mimetype.
Definition: filestore.php:364
$files
Definition: crop.php:36
$value
Definition: longtext.php:29
$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:1464
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Register a callback as a plugin hook handler.
Definition: elgglib.php:853
set_default_filestore(ElggFilestore $filestore)
Set the default filestore for the system.
Definition: filestore.php:480
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:467
$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
_elgg_services()
Definition: autoloader.php:14
global $CONFIG
$user
Definition: ban.php:13
elgg global
Pointer to the global context.
Definition: elgglib.js:12
$type
Definition: add.php:8
file_delete($guid)
Delete an ElggFile 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:669
$y1
Definition: crop.php:16
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:405
$x2
Definition: crop.php:17
$defaults
Definition: access.php:19
_elgg_filestore_init()
Initialize the file library.
Definition: filestore.php:495
$path
Definition: invalid.php:17
_elgg_filestore_test($hook, $type, $value)
Unit tests for files.
Definition: filestore.php:517
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:604
$y2
Definition: crop.php:18