getSubtype() == 'file') {
if ($filename = $file->getFilename()) {
$exploded_filename = explode('.', $filename);
$result = end($exploded_filename);
}
}
return strtolower($result);
}
/**
* Read a folder structure for a zip file
*
* @param ElggObject $folder the folder to read
* @param string $prepend current prefix
*
* @return array
*/
function file_tools_get_zip_structure($folder, $prepend) {
$entries = array();
if ($prepend) {
$prepend .= '/';
}
if (!$folder) {
$parent_guid = 0;
} else {
$parent_guid = $folder->getGUID();
}
$options = array(
"type" => "object",
"subtype" => FILE_TOOLS_SUBTYPE,
"limit" => false,
"metadata_name" => "parent_guid",
"metadata_value" => $parent_guid,
);
// voor de hoogste map de sub bestanden nog ophalen
if ($entities = elgg_get_entities_from_metadata($options)) {
foreach ($entities as $subfolder) {
$title = $prepend . $subfolder->title;
$entries[] = array('directory' => $title, 'files' => file_tools_has_files($subfolder->getGUID()));
$entries = array_merge($entries, file_tools_get_zip_structure($subfolder, $title));
}
}
return $entries;
}
/**
* Check if a folder has files
*
* @param ElggObject $folder the folder to check
*
* @return bool|array
*/
function file_tools_has_files($folder) {
$files_options = array(
"type" => "object",
"subtype" => "file",
"limit" => false,
//"container_guid" => get_loggedin_userid(),
"relationship" => FILE_TOOLS_RELATIONSHIP,
"relationship_guid" => $folder,
"inverse_relationship" => false
);
$file_guids = array();
if ($files = elgg_get_entities_from_relationship($files_options)) {
foreach ($files as $file) {
$file_guids[] = $file->getGUID();
}
return $file_guids;
}
return false;
}
/**
* Get the folders in a container
*
* @param int $container_guid the container to check
*
* @return bool|ElggObject[]
*/
function file_tools_get_folders($container_guid = 0) {
$result = false;
if (empty($container_guid)) {
$container_guid = elgg_get_page_owner_guid();
}
if (!empty($container_guid)) {
$options = array(
"type" => "object",
"subtype" => FILE_TOOLS_SUBTYPE,
"container_guid" => $container_guid,
"limit" => false
);
if ($folders = elgg_get_entities($options)) {
$parents = array();
foreach ($folders as $folder) {
$parent_guid = (int) $folder->parent_guid;
if (!empty($parent_guid)) {
if ($temp = get_entity($parent_guid)) {
if ($temp->getSubtype() != FILE_TOOLS_SUBTYPE) {
$parent_guid = 0;
}
} else {
$parent_guid = 0;
}
} else {
$parent_guid = 0;
}
if (!array_key_exists($parent_guid, $parents)) {
$parents[$parent_guid] = array();
}
$parents[$parent_guid][] = $folder;
}
$result = file_tools_sort_folders($parents, 0);
}
}
return $result;
}
/**
* Make folder select options
*
* @param array $folders folders to make the options for
* @param int $depth current depth
*
* @return string
*/
function file_tools_build_select_options($folders, $depth = 0) {
$result = array();
if (!empty($folders)) {
foreach ($folders as $index => $level) {
/**
* $level contains
* folder: the folder on this level
* children: potential children
*
*/
if ($folder = elgg_extract("folder", $level)) {
$result[$folder->getGUID()] = str_repeat("-", $depth) . $folder->title;
}
if ($childen = elgg_extract("children", $level)) {
$result += file_tools_build_select_options($childen, $depth + 1);
}
}
}
return $result;
}
/**
* Get folder selection options for widgets
*
* @param array $folder the folder to create for
* @param string $internalname the name of the input field
* @param array $selected the current selected values
*
* @return string
*/
function file_tools_build_widget_options($folder, $internalname = "", $selected = array()) {
$result = "";
if (is_array($folder) && !array_key_exists("children", $folder)) {
foreach ($folder as $folder_item) {
$result .= "
";
$result .= file_tools_build_widget_options($folder_item, $internalname, $selected);
$result .= "
";
}
} else {
$folder_item = $folder["folder"];
$result .= "";
if (in_array($folder_item->getGUID(), $selected)) {
$result .= " " . $folder_item->title;
} else {
$result .= " " . $folder_item->title;
}
if (!empty($folder["children"])) {
$result .= file_tools_build_widget_options($folder["children"], $internalname, $selected);
}
$result .= "";
}
return $result;
}
/**
* Folder folders by their order
*
* @param array $folders the folders to sort
* @param int $parent_guid the parent to sort for
*
* @return bool|array
*/
function file_tools_sort_folders($folders, $parent_guid = 0) {
$result = false;
if (array_key_exists($parent_guid, $folders)) {
$result = array();
foreach ($folders[$parent_guid] as $subfolder) {
$children = file_tools_sort_folders($folders, $subfolder->getGUID());
$order = $subfolder->order;
if (empty($order)) {
$order = 0;
}
while (array_key_exists($order, $result)) {
$order++;
}
$result[$order] = array(
"folder" => $subfolder,
"children" => $children
);
}
ksort($result);
}
return $result;
}
/**
* Get the subfolders of a folder
*
* @param ElggObject $folder the folder to get the subfolders for
* @param bool $list output a list (default: false)
*
* @return bool|array|string
*/
function file_tools_get_sub_folders($folder = false, $list = false) {
$result = false;
if (!empty($folder) && elgg_instanceof($folder, "object", FILE_TOOLS_SUBTYPE)) {
$container_guid = $folder->getContainerGUID();
$parent_guid = $folder->getGUID();
} else {
$container_guid = elgg_get_page_owner_guid();
$parent_guid = 0;
}
$options = array(
"type" => "object",
"subtype" => FILE_TOOLS_SUBTYPE,
"container_guid" => $container_guid,
"limit" => false,
"metadata_name" => "parent_guid",
"metadata_value" => $parent_guid,
"order_by_metadata" => array('name' => 'order', 'direction' => 'ASC', 'as' => 'integer'),
"full_view" => false,
"pagination" => false
);
if ($list) {
$folders = elgg_list_entities_from_metadata($options);
} else {
$folders = elgg_get_entities_from_metadata($options);
}
if ($folders) {
$result = $folders;
}
return $result;
}
/**
* Create a folder menu
*
* @param array $folders the folders to create the menu for
*
* @return bool|ElggMenuItem[]
*/
function file_tools_make_menu_items($folders) {
$result = false;
if (!empty($folders) && is_array($folders)) {
$result = array();
foreach ($folders as $index => $level) {
if ($folder = elgg_extract("folder", $level)) {
$folder_title = $folder->title;
if (empty($folder_title)) {
$folder_title = elgg_echo("untitled");
}
$folder_menu = ElggMenuItem::factory(array(
"name" => "folder_" . $folder->getGUID(),
"text" => $folder_title,
"href" => "#" . $folder->getGUID(),
"priority" => $folder->order
));
if ($children = elgg_extract("children", $level)) {
$folder_menu->setChildren(file_tools_make_menu_items($children));
}
$result[] = $folder_menu;
}
}
}
return $result;
}
/**
* Recursivly change the access of subfolders (and files)
*
* @param ElggObject $folder the folder to change the access for
* @param bool $change_files include files in this folder (default: false)
*
* @return void
*/
function file_tools_change_children_access($folder, $change_files = false) {
if (!empty($folder) && ($folder instanceof ElggObject)) {
if ($folder->getSubtype() == FILE_TOOLS_SUBTYPE) {
// get children folders
$options = array(
"type" => "object",
"subtype" => FILE_TOOLS_SUBTYPE,
"container_guid" => $folder->getContainerGUID(),
"limit" => false,
"metadata_name" => "parent_guid",
"metadata_value" => $folder->getGUID()
);
if ($children = elgg_get_entities_from_metadata($options)) {
foreach ($children as $child) {
$child->access_id = $folder->access_id;
$child->save();
file_tools_change_children_access($child, $change_files);
}
}
if ($change_files) {
// change access on files in this folder
file_tools_change_files_access($folder);
}
}
}
}
/**
* Change the access of all file in a folder
*
* @param ElggObject $folder the folder to change the file access for
*
* @return void
*/
function file_tools_change_files_access($folder) {
if (!empty($folder) && ($folder instanceof ElggObject)) {
if ($folder->getSubtype() == FILE_TOOLS_SUBTYPE) {
// change access on files in this folder
$options = array(
"type" => "object",
"subtype" => "file",
"container_guid" => $folder->getContainerGUID(),
"limit" => false,
"relationship" => FILE_TOOLS_RELATIONSHIP,
"relationship_guid" => $folder->getGUID()
);
if ($files = elgg_get_entities_from_relationship($options)) {
// need to unregister an event listener
elgg_unregister_event_handler("update", "object", "file_tools_object_handler");
foreach ($files as $file) {
$file->access_id = $folder->access_id;
$file->save();
}
}
}
}
}
/**
* Get the allowed extensions for uploading
*
* @param bool $zip return zip upload dialog format
*
* @return bool|string
*/
function file_tools_allowed_extensions($zip = false) {
$allowed_extensions_settings = elgg_get_plugin_setting('allowed_extensions', 'file_tools');
$allowed_extensions_settings = string_to_tag_array($allowed_extensions_settings);
if (!empty($allowed_extensions_settings)) {
$result = $allowed_extensions_settings;
} else {
$result = array('txt','jpg','jpeg','png','bmp','gif','pdf','doc','docx','xls','xlsx','ppt','pptx','odt','ods','odp');
}
if (!$zip) {
return $result;
}
$result = implode(";*.", $result);
return "*." . $result;
}
if (!function_exists("mime_content_type")) {
// @codingStandardsIgnoreStart
function mime_content_type($fn) {
static $mime_magic_data;
#-- fallback
$type = false;
#-- read in first 3K of given file
if (is_dir($fn)) {
return("httpd/unix-directory");
} elseif (is_resource($fn) || ($fn = @fopen($fn, "rb"))) {
$bin = fread($fn, $maxlen=3072);
fclose($fn);
} elseif (!file_exists($fn)) {
return false;
} else {
return("application/octet-stream"); // give up
}
#-- use PECL::fileinfo when available
if (function_exists("finfo_buffer")) {
if (!isset($mime_magic_data)) {
$mime_magic_data = finfo_open(MAGIC_MIME);
}
$type = finfo_buffer($bin);
return($type);
}
#-- read in magic data, when called for the very first time
if (!isset($mime_content_type)) {
if ((file_exists($fn = ini_get("mime_magic.magicfile")))
or (file_exists($fn = "/usr/share/misc/magic.mime"))
or (file_exists($fn = "/etc/mime-magic")) ) {
$mime_magic_data = array();
#-- read in file
$f = fopen($fn, "r");
$fd = fread($f, 1<<20);
fclose($f);
$fd = str_replace(" ", "\t", $fd);
#-- look at each entry
foreach (explode("\n", $fd) as $line) {
#-- skip empty lines
if (!strlen($line) or ($line[0] == "#") or ($line[0] == "\n")) {
continue;
}
#-- break into four fields at tabs
$l = preg_split("/\t+/", $line);
@list($pos, $typestr, $magic, $ct) = $l;
#print_r($l);
#-- ignore >continuing lines
if ($pos[0] == ">") {
continue;
}
#-- real mime type string?
$ct = strtok($ct, " ");
if (!strpos($ct, "/")) {
continue;
}
#-- mask given?
$mask = 0;
if (strpos($typestr, "&")) {
$typestr = strtok($typestr, "&");
$mask = strtok(" ");
if ($mask[0] == "0") {
$mask = ($mask[1] == "x") ? hexdec(substr($mask, 2)) : octdec($mask);
} else {
$mask = (int)$mask;
}
}
#-- strip prefixes
if ($magic[0] == "=") {
$magic = substr($magic, 1);
}
#-- convert type
if ($typestr == "string") {
$magic = stripcslashes($magic);
$len = strlen($magic);
if ($mask) {
continue;
}
}
#-- numeric values
else {
if ((ord($magic[0]) < 48) or (ord($magic[0]) > 57)) {
#echo "\nmagicnumspec=$line\n";
#var_dump($l);
continue; #-- skip specials like >, x, <, ^, &
}
#-- convert string representation into int
if ((strlen($magic) >= 4) && ($magic[1] == "x")) {
$magic = hexdec(substr($magic, 2));
} elseif ($magic[0]) {
$magic = octdec($magic);
} else {
$magic = (int) $magic;
if (!$magic) {
continue;
} // zero is not a good magic value anyhow
}
#-- different types
switch ($typestr) {
case "byte":
$len = 1;
break;
case "beshort":
$magic = ($magic >> 8) | (($magic & 0xFF) << 8);
case "leshort":
case "short":
$len = 2;
break;
case "belong":
$magic = (($magic >> 24) & 0xFF)
| (($magic >> 8) & 0xFF00)
| (($magic & 0xFF00) << 8)
| (($magic & 0xFF) << 24);
case "lelong":
case "long":
$len = 4;
break;
default:
//date, ldate, ledate, leldate, beldate, lebelbe...
continue;
}
}
#-- add to list
$mime_magic_data[] = array($pos, $len, $mask, $magic, trim($ct));
}
}
#print_r($mime_magic_data);
}
#-- compare against each entry from the mime magic database
foreach ($mime_magic_data as $def) {
#-- entries are organized as follows
list($pos, $len, $mask, $magic, $ct) = $def;
#-- ignored entries (we only read first 3K of file for opt. speed)
if ($pos >= $maxlen) {
continue;
}
$slice = substr($bin, $pos, $len);
#-- integer comparison value
if ($mask) {
$value = hexdec(bin2hex($slice));
if (($value & $mask) == $magic) {
$type = $ct;
break;
}
}
#-- string comparison
else {
if ($slice == $magic) {
$type = $ct;
break;
}
}
} // foreach
#-- built-in defaults
if (!$type) {
#-- some form of xml
if (strpos($bin, "<"."?xml ") !== false) {
return("text/xml");
}
#-- html
elseif ((strpos($bin, "") !== false) || (strpos($bin, "") !== false)
|| strpos($bin, "") || strpos($bin, "")
|| (strpos($bin, "