start.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. /**
  3. * Elgg file plugin
  4. *
  5. * @package ElggFile
  6. */
  7. elgg_register_event_handler('init', 'system', 'file_init');
  8. /**
  9. * File plugin initialization functions.
  10. */
  11. function file_init() {
  12. // register a library of helper functions
  13. elgg_register_library('elgg:file', elgg_get_plugins_path() . 'file/lib/file.php');
  14. // Site navigation
  15. $item = new ElggMenuItem('file', elgg_echo('file'), 'file/all');
  16. elgg_register_menu_item('site', $item);
  17. // Extend CSS
  18. elgg_extend_view('css/elgg', 'file/css');
  19. // add enclosure to rss item
  20. elgg_extend_view('extensions/item', 'file/enclosure');
  21. // extend group main page
  22. elgg_extend_view('groups/tool_latest', 'file/group_module');
  23. // Register a page handler, so we can have nice URLs
  24. elgg_register_page_handler('file', 'file_page_handler');
  25. // Add a new file widget
  26. elgg_register_widget_type('filerepo', elgg_echo("file"), elgg_echo("file:widget:description"));
  27. // Register URL handlers for files
  28. elgg_register_plugin_hook_handler('entity:url', 'object', 'file_set_url');
  29. elgg_register_plugin_hook_handler('entity:icon:url', 'object', 'file_set_icon_url');
  30. // Register for notifications
  31. elgg_register_notification_event('object', 'file', array('create'));
  32. elgg_register_plugin_hook_handler('prepare', 'notification:create:object:file', 'file_prepare_notification');
  33. // add the group files tool option
  34. add_group_tool_option('file', elgg_echo('groups:enablefiles'), true);
  35. // Register entity type for search
  36. elgg_register_entity_type('object', 'file');
  37. // add a file link to owner blocks
  38. elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'file_owner_block_menu');
  39. // Register actions
  40. $action_path = elgg_get_plugins_path() . 'file/actions/file';
  41. elgg_register_action("file/upload", "$action_path/upload.php");
  42. elgg_register_action("file/delete", "$action_path/delete.php");
  43. // temporary - see #2010
  44. elgg_register_action("file/download", "$action_path/download.php");
  45. // embed support
  46. $item = ElggMenuItem::factory(array(
  47. 'name' => 'file',
  48. 'text' => elgg_echo('file'),
  49. 'priority' => 10,
  50. 'data' => array(
  51. 'options' => array(
  52. 'type' => 'object',
  53. 'subtype' => 'file',
  54. ),
  55. ),
  56. ));
  57. elgg_register_menu_item('embed', $item);
  58. $item = ElggMenuItem::factory(array(
  59. 'name' => 'file_upload',
  60. 'text' => elgg_echo('file:upload'),
  61. 'priority' => 100,
  62. 'data' => array(
  63. 'view' => 'embed/file_upload/content',
  64. ),
  65. ));
  66. elgg_register_menu_item('embed', $item);
  67. }
  68. /**
  69. * Dispatches file pages.
  70. * URLs take the form of
  71. * All files: file/all
  72. * User's files: file/owner/<username>
  73. * Friends' files: file/friends/<username>
  74. * View file: file/view/<guid>/<title>
  75. * New file: file/add/<guid>
  76. * Edit file: file/edit/<guid>
  77. * Group files: file/group/<guid>/all
  78. * Download: file/download/<guid>
  79. *
  80. * Title is ignored
  81. *
  82. * @param array $page
  83. * @return bool
  84. */
  85. function file_page_handler($page) {
  86. if (!isset($page[0])) {
  87. $page[0] = 'all';
  88. }
  89. $file_dir = elgg_get_plugins_path() . 'file/pages/file';
  90. $page_type = $page[0];
  91. switch ($page_type) {
  92. case 'owner':
  93. file_register_toggle();
  94. include "$file_dir/owner.php";
  95. break;
  96. case 'friends':
  97. file_register_toggle();
  98. include "$file_dir/friends.php";
  99. break;
  100. case 'view':
  101. set_input('guid', $page[1]);
  102. include "$file_dir/view.php";
  103. break;
  104. case 'add':
  105. include "$file_dir/upload.php";
  106. break;
  107. case 'edit':
  108. set_input('guid', $page[1]);
  109. include "$file_dir/edit.php";
  110. break;
  111. case 'search':
  112. file_register_toggle();
  113. include "$file_dir/search.php";
  114. break;
  115. case 'group':
  116. file_register_toggle();
  117. include "$file_dir/owner.php";
  118. break;
  119. case 'all':
  120. file_register_toggle();
  121. include "$file_dir/world.php";
  122. break;
  123. case 'download':
  124. set_input('guid', $page[1]);
  125. include "$file_dir/download.php";
  126. break;
  127. case 'play':
  128. set_input('guid', $page[1]);
  129. include "$file_dir/play.php";
  130. break;
  131. default:
  132. return false;
  133. }
  134. return true;
  135. }
  136. /**
  137. * Adds a toggle to extra menu for switching between list and gallery views
  138. */
  139. function file_register_toggle() {
  140. $url = elgg_http_remove_url_query_element(current_page_url(), 'list_type');
  141. if (get_input('list_type', 'list') == 'list') {
  142. $list_type = "gallery";
  143. $icon = elgg_view_icon('grid');
  144. } else {
  145. $list_type = "list";
  146. $icon = elgg_view_icon('list');
  147. }
  148. if (substr_count($url, '?')) {
  149. $url .= "&list_type=" . $list_type;
  150. } else {
  151. $url .= "?list_type=" . $list_type;
  152. }
  153. elgg_register_menu_item('extras', array(
  154. 'name' => 'file_list',
  155. 'text' => $icon,
  156. 'href' => $url,
  157. 'title' => elgg_echo("file:list:$list_type"),
  158. 'priority' => 1000,
  159. ));
  160. }
  161. /**
  162. * Prepare a notification message about a new file
  163. *
  164. * @param string $hook Hook name
  165. * @param string $type Hook type
  166. * @param Elgg\Notifications\Notification $notification The notification to prepare
  167. * @param array $params Hook parameters
  168. * @return Elgg\Notifications\Notification
  169. */
  170. function file_prepare_notification($hook, $type, $notification, $params) {
  171. $entity = $params['event']->getObject();
  172. $owner = $params['event']->getActor();
  173. $recipient = $params['recipient'];
  174. $language = $params['language'];
  175. $method = $params['method'];
  176. $descr = $entity->description;
  177. $title = $entity->title;
  178. $notification->subject = elgg_echo('file:notify:subject', array($entity->title), $language);
  179. $notification->body = elgg_echo('file:notify:body', array(
  180. $owner->name,
  181. $title,
  182. $descr,
  183. $entity->getURL()
  184. ), $language);
  185. $notification->summary = elgg_echo('file:notify:summary', array($entity->title), $language);
  186. return $notification;
  187. }
  188. /**
  189. * Add a menu item to the user ownerblock
  190. */
  191. function file_owner_block_menu($hook, $type, $return, $params) {
  192. if (elgg_instanceof($params['entity'], 'user')) {
  193. $url = "file/owner/{$params['entity']->username}";
  194. $item = new ElggMenuItem('file', elgg_echo('file'), $url);
  195. $return[] = $item;
  196. } else {
  197. if ($params['entity']->file_enable != "no") {
  198. $url = "file/group/{$params['entity']->guid}/all";
  199. $item = new ElggMenuItem('file', elgg_echo('file:group'), $url);
  200. $return[] = $item;
  201. }
  202. }
  203. return $return;
  204. }
  205. /**
  206. * Registers page menu items for file type filtering and returns a view
  207. *
  208. * @param int $container_guid The GUID of the container of the files
  209. * @param bool $friends Whether we're looking at the container or the container's friends
  210. *
  211. * @return string The typecloud
  212. */
  213. function file_get_type_cloud($container_guid = "", $friends = false) {
  214. $container_guids = $container_guid;
  215. $container = get_entity($container_guid);
  216. if ($friends && $container) {
  217. // tags interface does not support pulling tags on friends' content so
  218. // we need to grab all friends
  219. $friend_entities = $container->getFriends(array('limit' => 0));
  220. if ($friend_entities) {
  221. $friend_guids = array();
  222. foreach ($friend_entities as $friend) {
  223. $friend_guids[] = $friend->getGUID();
  224. }
  225. }
  226. $container_guids = $friend_guids;
  227. }
  228. elgg_register_tag_metadata_name('simpletype');
  229. $options = array(
  230. 'type' => 'object',
  231. 'subtype' => 'file',
  232. 'container_guids' => $container_guids,
  233. 'threshold' => 0,
  234. 'limit' => 10,
  235. 'tag_names' => array('simpletype')
  236. );
  237. $types = elgg_get_tags($options);
  238. if ($types) {
  239. $all = new stdClass;
  240. $all->tag = 'all';
  241. elgg_register_menu_item('page', array(
  242. 'name' => 'file:all',
  243. 'text' => elgg_echo('all'),
  244. 'href' => file_type_cloud_get_url($all, $friends),
  245. ));
  246. foreach ($types as $type) {
  247. elgg_register_menu_item('page', array(
  248. 'name' => "file:$type->tag",
  249. 'text' => elgg_echo("file:type:$type->tag"),
  250. 'href' => file_type_cloud_get_url($type, $friends),
  251. ));
  252. }
  253. }
  254. // returning the view is needed for BC
  255. $params = array(
  256. 'friends' => $friends,
  257. 'types' => $types,
  258. );
  259. return elgg_view('file/typecloud', $params);
  260. }
  261. function file_type_cloud_get_url($type, $friends) {
  262. $url = elgg_get_site_url() . 'file/search?subtype=file';
  263. if ($type->tag != "all") {
  264. $url .= "&md_type=simpletype&tag=" . urlencode($type->tag);
  265. }
  266. if ($friends) {
  267. $url .= "&friends=$friends";
  268. }
  269. if ($type->tag == "image") {
  270. $url .= "&list_type=gallery";
  271. }
  272. if (elgg_get_page_owner_guid()) {
  273. $url .= "&page_owner=" . elgg_get_page_owner_guid();
  274. }
  275. return $url;
  276. }
  277. function get_filetype_cloud($owner_guid = "", $friends = false) {
  278. elgg_deprecated_notice('Use file_get_type_cloud instead of get_filetype_cloud', 1.8);
  279. return file_get_type_cloud($owner_guid, $friends);
  280. }
  281. /**
  282. * Populates the ->getUrl() method for file objects
  283. *
  284. * @param string $hook
  285. * @param string $type
  286. * @param string $url
  287. * @param array $params
  288. * @return string File URL
  289. */
  290. function file_set_url($hook, $type, $url, $params) {
  291. $entity = $params['entity'];
  292. if (elgg_instanceof($entity, 'object', 'file')) {
  293. $title = elgg_get_friendly_title($entity->title);
  294. return "file/view/" . $entity->getGUID() . "/" . $title;
  295. }
  296. }
  297. /**
  298. * Override the default entity icon for files
  299. *
  300. * Plugins can override or extend the icons using the plugin hook: 'file:icon:url', 'override'
  301. *
  302. * @param string $hook
  303. * @param string $type
  304. * @param string $url
  305. * @param array $params
  306. * @return string Relative URL
  307. */
  308. function file_set_icon_url($hook, $type, $url, $params) {
  309. $file = $params['entity'];
  310. $size = $params['size'];
  311. if (elgg_instanceof($file, 'object', 'file')) {
  312. // thumbnails get first priority
  313. if ($file->thumbnail) {
  314. $ts = (int)$file->icontime;
  315. return "mod/file/thumbnail.php?file_guid=$file->guid&size=$size&icontime=$ts";
  316. }
  317. $mapping = array(
  318. 'application/excel' => 'excel',
  319. 'application/msword' => 'word',
  320. 'application/ogg' => 'music',
  321. 'application/pdf' => 'pdf',
  322. 'application/powerpoint' => 'ppt',
  323. 'application/vnd.ms-excel' => 'excel',
  324. 'application/vnd.ms-powerpoint' => 'ppt',
  325. 'application/vnd.oasis.opendocument.text' => 'openoffice',
  326. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'word',
  327. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'excel',
  328. 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'ppt',
  329. 'application/x-gzip' => 'archive',
  330. 'application/x-rar-compressed' => 'archive',
  331. 'application/x-stuffit' => 'archive',
  332. 'application/zip' => 'archive',
  333. 'text/directory' => 'vcard',
  334. 'text/v-card' => 'vcard',
  335. 'application' => 'application',
  336. 'audio' => 'music',
  337. 'text' => 'text',
  338. 'video' => 'video',
  339. );
  340. $mime = $file->mimetype;
  341. if ($mime) {
  342. $base_type = $file->simpletype;
  343. } else {
  344. $mime = 'none';
  345. $base_type = 'none';
  346. }
  347. if (isset($mapping[$mime])) {
  348. $type = $mapping[$mime];
  349. } elseif (isset($mapping[$base_type])) {
  350. $type = $mapping[$base_type];
  351. } else {
  352. $type = 'general';
  353. }
  354. if ($size == 'large') {
  355. $ext = '_lrg';
  356. } else {
  357. $ext = '';
  358. }
  359. $url = "mod/file/graphics/icons/{$type}{$ext}.gif";
  360. $url = elgg_trigger_plugin_hook('file:icon:url', 'override', $params, $url);
  361. return $url;
  362. }
  363. }