123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587 |
- <?php
- function get_dir_size($dir, $total_size = 0) {
- $handle = @opendir($dir);
- while ($file = @readdir($handle)) {
- if (in_array($file, array('.', '..'))) {
- continue;
- }
- if (is_dir($dir . $file)) {
- $total_size = get_dir_size($dir . $file . "/", $total_size);
- } else {
- $total_size += filesize($dir . $file);
- }
- }
- @closedir($handle);
- return($total_size);
- }
- function get_uploaded_file($input_name) {
- $files = _elgg_services()->request->files;
- if (!$files->has($input_name)) {
- return false;
- }
- $file = $files->get($input_name);
- if (empty($file)) {
-
- return false;
- }
- if ($file->getError() !== 0) {
- return false;
- }
- return file_get_contents($file->getPathname());
- }
- function get_resized_image_from_uploaded_file($input_name, $maxwidth, $maxheight,
- $square = false, $upscale = false) {
- $files = _elgg_services()->request->files;
- if (!$files->has($input_name)) {
- return false;
- }
-
- $file = $files->get($input_name);
- if (empty($file)) {
-
- return false;
- }
- if ($file->getError() !== 0) {
- return false;
- }
- return get_resized_image_from_existing_file($file->getPathname(), $maxwidth,
- $maxheight, $square, 0, 0, 0, 0, $upscale);
- }
- function get_resized_image_from_existing_file($input_name, $maxwidth, $maxheight, $square = false,
- $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0, $upscale = false) {
-
- $imgsizearray = getimagesize($input_name);
- if ($imgsizearray == false) {
- return false;
- }
- $width = $imgsizearray[0];
- $height = $imgsizearray[1];
- $accepted_formats = array(
- 'image/jpeg' => 'jpeg',
- 'image/pjpeg' => 'jpeg',
- 'image/png' => 'png',
- 'image/x-png' => 'png',
- 'image/gif' => 'gif'
- );
-
- $load_function = "imagecreatefrom" . $accepted_formats[$imgsizearray['mime']];
- if (!is_callable($load_function)) {
- return false;
- }
-
- $options = array(
- 'maxwidth' => $maxwidth,
- 'maxheight' => $maxheight,
- 'square' => $square,
- 'upscale' => $upscale,
- 'x1' => $x1,
- 'y1' => $y1,
- 'x2' => $x2,
- 'y2' => $y2,
- );
- $params = get_image_resize_parameters($width, $height, $options);
- if ($params == false) {
- return false;
- }
-
- $original_image = call_user_func($load_function, $input_name);
- if (!$original_image) {
- return false;
- }
-
- $new_image = imagecreatetruecolor($params['newwidth'], $params['newheight']);
- if (!$new_image) {
- return false;
- }
-
- imagefilledrectangle(
- $new_image, 0, 0, $params['newwidth'], $params['newheight'],
- imagecolorallocate($new_image, 255, 255, 255)
- );
- $rtn_code = imagecopyresampled( $new_image,
- $original_image,
- 0,
- 0,
- $params['xoffset'],
- $params['yoffset'],
- $params['newwidth'],
- $params['newheight'],
- $params['selectionwidth'],
- $params['selectionheight']);
- if (!$rtn_code) {
- return false;
- }
-
- ob_start();
- imagejpeg($new_image, null, 90);
- $jpeg = ob_get_clean();
- imagedestroy($new_image);
- imagedestroy($original_image);
- return $jpeg;
- }
- function get_image_resize_parameters($width, $height, $options) {
- $defaults = array(
- 'maxwidth' => 100,
- 'maxheight' => 100,
- 'square' => false,
- 'upscale' => false,
- 'x1' => 0,
- 'y1' => 0,
- 'x2' => 0,
- 'y2' => 0,
- );
- $options = array_merge($defaults, $options);
-
- $maxwidth = $options['maxwidth'];
- $maxheight = $options['maxheight'];
- $square = $options['square'];
- $upscale = $options['upscale'];
- $x1 = $options['x1'];
- $y1 = $options['y1'];
- $x2 = $options['x2'];
- $y2 = $options['y2'];
-
- $crop = true;
- if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 == 0) {
- $crop = false;
- }
-
- if ($crop) {
- $selection_width = $x2 - $x1;
- $selection_height = $y2 - $y1;
- } else {
-
- $selection_width = $width;
- $selection_height = $height;
- }
-
- if ($square) {
-
-
- if ($crop == true && $selection_width != $selection_height) {
- return false;
- }
-
- $new_width = $new_height = min($maxwidth, $maxheight);
-
- $selection_width = $selection_height = min($selection_width, $selection_height);
-
- if ($crop) {
- $widthoffset = $x1;
- $heightoffset = $y1;
- $width = $x2 - $x1;
- $height = $width;
- } else {
-
- $widthoffset = floor(($width - $selection_width) / 2);
- $heightoffset = floor(($height - $selection_height) / 2);
- }
- } else {
-
- $new_width = $maxwidth;
- $new_height = $maxheight;
-
- if (($selection_height / (float)$new_height) > ($selection_width / (float)$new_width)) {
- $new_width = floor($new_height * $selection_width / (float)$selection_height);
- } else {
- $new_height = floor($new_width * $selection_height / (float)$selection_width);
- }
-
- $widthoffset = 0;
- $heightoffset = 0;
- if ($crop) {
- $widthoffset = $x1;
- $heightoffset = $y1;
- }
- }
- if (!$upscale && ($selection_height < $new_height || $selection_width < $new_width)) {
-
- if ($square) {
- $new_height = $selection_height;
- $new_width = $selection_width;
- } else {
- if ($selection_height < $new_height && $selection_width < $new_width) {
- $new_height = $selection_height;
- $new_width = $selection_width;
- }
- }
- }
- $params = array(
- 'newwidth' => $new_width,
- 'newheight' => $new_height,
- 'selectionwidth' => $selection_width,
- 'selectionheight' => $selection_height,
- 'xoffset' => $widthoffset,
- 'yoffset' => $heightoffset,
- );
- return $params;
- }
- function file_delete($guid) {
- $file = get_entity($guid);
- if (!$file || !$file->canEdit()) {
- return false;
- }
- $thumbnail = $file->thumbnail;
- $smallthumb = $file->smallthumb;
- $largethumb = $file->largethumb;
- if ($thumbnail) {
- $delfile = new \ElggFile();
- $delfile->owner_guid = $file->owner_guid;
- $delfile->setFilename($thumbnail);
- $delfile->delete();
- }
- if ($smallthumb) {
- $delfile = new \ElggFile();
- $delfile->owner_guid = $file->owner_guid;
- $delfile->setFilename($smallthumb);
- $delfile->delete();
- }
- if ($largethumb) {
- $delfile = new \ElggFile();
- $delfile->owner_guid = $file->owner_guid;
- $delfile->setFilename($largethumb);
- $delfile->delete();
- }
- return $file->delete();
- }
- function delete_directory($directory) {
-
- if (!$handle = opendir($directory)) {
- return false;
- }
-
- while (($file = readdir($handle)) !== false) {
- if (in_array($file, array('.', '..'))) {
- continue;
- }
- $path = "$directory/$file";
- if (is_dir($path)) {
-
- if (!delete_directory($path)) {
- return false;
- }
- } else {
-
- unlink($path);
- }
- }
-
- closedir($handle);
- return rmdir($directory);
- }
- function _elgg_clear_entity_files($entity) {
- $dir = new \Elgg\EntityDirLocator($entity->guid);
- $file_path = elgg_get_config('dataroot') . $dir;
- if (file_exists($file_path)) {
- delete_directory($file_path);
- }
- }
- $DEFAULT_FILE_STORE = null;
- function get_default_filestore() {
- global $DEFAULT_FILE_STORE;
- return $DEFAULT_FILE_STORE;
- }
- function set_default_filestore(\ElggFilestore $filestore) {
- global $DEFAULT_FILE_STORE;
- $DEFAULT_FILE_STORE = $filestore;
- return true;
- }
- function elgg_get_file_simple_type($mime_type) {
- $params = array('mime_type' => $mime_type);
- return elgg_trigger_plugin_hook('simple_type', 'file', $params, 'general');
- }
- function _elgg_filestore_init() {
- global $CONFIG;
-
- if (isset($CONFIG->dataroot)) {
- set_default_filestore(new \ElggDiskFilestore($CONFIG->dataroot));
- }
-
- elgg_register_plugin_hook_handler('mime_type', 'file', '_elgg_filestore_detect_mimetype');
-
-
- elgg_register_plugin_hook_handler('simple_type', 'file', '_elgg_filestore_parse_simpletype');
-
- elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_filestore_test');
- }
- function _elgg_filestore_detect_mimetype($hook, $type, $mime_type, $params) {
- $original_filename = elgg_extract('original_filename', $params);
- $info = pathinfo($original_filename);
-
- $office_formats = array('docx', 'xlsx', 'pptx');
- if ($mime_type == "application/zip" && in_array($info['extension'], $office_formats)) {
- switch ($info['extension']) {
- case 'docx':
- $mime_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
- break;
- case 'xlsx':
- $mime_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
- break;
- case 'pptx':
- $mime_type = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
- break;
- }
- }
-
- if ($mime_type == "application/vnd.ms-office" && $info['extension'] == "ppt") {
- $mime_type = "application/vnd.ms-powerpoint";
- }
- return $mime_type;
- }
- function _elgg_filestore_parse_simpletype($hook, $type, $simple_type, $params) {
- $mime_type = elgg_extract('mime_type', $params);
- switch ($mime_type) {
- case "application/msword":
- case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
- case "application/pdf":
- return "document";
- case "application/ogg":
- return "audio";
- }
- if (preg_match('~^(audio|image|video)/~', $mime_type, $m)) {
- return $m[1];
- }
- if (0 === strpos($mime_type, 'text/') || false !== strpos($mime_type, 'opendocument')) {
- return "document";
- }
-
- return $simple_type;
- }
- function _elgg_filestore_test($hook, $type, $value) {
- global $CONFIG;
- $value[] = "{$CONFIG->path}engine/tests/ElggCoreFilestoreTest.php";
- return $value;
- }
- return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
- $events->registerHandler('init', 'system', '_elgg_filestore_init', 100);
- };
|