PageRenderTime 24ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/file/start.php

https://github.com/bpmn/bpmn
PHP | 334 lines | 191 code | 55 blank | 88 comment | 48 complexity | ca83776b3d1a03ae152cf85d9e4b73dc MD5 | raw file
  1. <?php
  2. /**
  3. * Elgg file browser
  4. *
  5. * @package ElggFile
  6. */
  7. /**
  8. * Override the ElggFile so that
  9. */
  10. class FilePluginFile extends ElggFile {
  11. protected function initialise_attributes() {
  12. parent::initialise_attributes();
  13. $this->attributes['subtype'] = "file";
  14. }
  15. public function __construct($guid = null) {
  16. parent::__construct($guid);
  17. }
  18. }
  19. /**
  20. * File plugin initialisation functions.
  21. */
  22. function file_init() {
  23. global $CONFIG;
  24. // Set up menu (tools dropdown or other uses as defined by theme)
  25. if (isloggedin()) {
  26. //add_menu(elgg_echo('file'), $CONFIG->wwwroot . "pg/file/owner/" . get_loggedin_user()->username);
  27. } else {
  28. //add_menu(elgg_echo('file'), $CONFIG->wwwroot . "pg/file/all/" );
  29. }
  30. // Extend CSS
  31. elgg_extend_view('css', 'file/css');
  32. // Extend hover-over and profile menu
  33. //elgg_extend_view('profile/menu/links', 'file/menu');
  34. // Register a page handler, so we can have nice URLs
  35. register_page_handler('file', 'file_page_handler');
  36. // Add a new file widget
  37. add_widget_type('filerepo', elgg_echo("file"), elgg_echo("file:widget:description"));
  38. // Register a URL handler for files
  39. register_entity_url_handler('file_url', 'object', 'file');
  40. // Register granular notification for this object type
  41. if (is_callable('register_notification_object')) {
  42. register_notification_object('object', 'file', elgg_echo('file:newupload'));
  43. }
  44. // Listen to notification events and supply a more useful message
  45. register_plugin_hook('notify:entity:message', 'object', 'file_notify_message');
  46. // add the group files tool option
  47. add_group_tool_option('file', elgg_echo('groups:enablefiles'), true);
  48. // extend group main page
  49. elgg_extend_view('groups/left_column', 'file/groupprofile_files');
  50. elgg_extend_view('teams/left_column', 'file/groupprofile_files');
  51. // STd file for openlab plugin
  52. elgg_extend_view('openlabs/left_column', 'file/groupprofile_files');
  53. // Register entity type
  54. register_entity_type('object', 'file');
  55. }
  56. /**
  57. * Sets up submenus for the file system. Triggered on pagesetup.
  58. *
  59. */
  60. function file_submenus() {
  61. global $CONFIG;
  62. $page_owner = page_owner_entity();
  63. // Group submenu option
  64. if ($page_owner instanceof ElggGroup && get_context() == "groups") {
  65. if ($page_owner->file_enable != "no") {
  66. add_submenu_item(sprintf(elgg_echo("file:group"), $page_owner->name), $CONFIG->wwwroot . "pg/file/owner/" . $page_owner->username);
  67. }
  68. }
  69. if ($page_owner instanceof ElggGroup && get_context() == "teams") {
  70. if ($page_owner->file_enable != "no") {
  71. add_submenu_item(sprintf(elgg_echo("file:team"), $page_owner->name), $CONFIG->wwwroot . "pg/teamsfile/owner/" . $page_owner->username);
  72. }
  73. }
  74. // STD add file menus
  75. if ($page_owner instanceof ElggGroup && get_context() == "openlabs") {
  76. if ($page_owner->file_enable != "no") {
  77. add_submenu_item(sprintf(elgg_echo("file:openlabs"), $page_owner->name), $CONFIG->wwwroot . "pg/openlabfile/owner/" . $page_owner->username);
  78. }
  79. }
  80. // General submenu options
  81. //if ((get_context() == "file") || (get_context() == 'openlab' ) || (get_context() =='teams' ) ) {
  82. if ((get_context() == "teams_file") || (get_context() == 'openlabs_file' ) ) {
  83. /**
  84. * STD no more menus Your files + Your friends files
  85. if ((page_owner() == $_SESSION['guid'] || !page_owner()) && isloggedin()) {
  86. add_submenu_item(sprintf(elgg_echo("file:yours"), $page_owner->name), $CONFIG->wwwroot . "pg/file/owner/" . $page_owner->username);
  87. add_submenu_item(sprintf(elgg_echo('file:yours:friends'), $page_owner->name), $CONFIG->wwwroot . "pg/file/friends/" . $page_owner->username);
  88. } else if (page_owner()) {
  89. add_submenu_item(sprintf(elgg_echo("file:user"), $page_owner->name), $CONFIG->wwwroot . "pg/file/owner/" . $page_owner->username);
  90. if ($page_owner instanceof ElggUser) // This one's for users, not groups
  91. add_submenu_item(sprintf(elgg_echo('file:friends'), $page_owner->name), $CONFIG->wwwroot . "pg/file/friends/" . $page_owner->username);
  92. } */
  93. /* add_submenu_item(elgg_echo('file:all'), $CONFIG->wwwroot . "pg/file/all/"); */
  94. if (can_write_to_container($_SESSION['guid'], page_owner()) && isloggedin())
  95. add_submenu_item(elgg_echo('file:upload'), $CONFIG->wwwroot . "pg/file/new/" . $page_owner->username);
  96. }
  97. }
  98. /**
  99. * File page handler
  100. *
  101. * @param array $page Array of page elements, forwarded by the page handling mechanism
  102. */
  103. function file_page_handler($page) {
  104. global $CONFIG;
  105. // group usernames
  106. if (substr_count($page[0], 'group:')) {
  107. preg_match('/group\:([0-9]+)/i', $page[0], $matches);
  108. $guid = $matches[1];
  109. if ($entity = get_entity($guid)) {
  110. file_url_forwarder($page);
  111. }
  112. }
  113. // user usernames
  114. $user = get_user_by_username($page[0]);
  115. if ($user) {
  116. file_url_forwarder($page);
  117. }
  118. switch ($page[0]) {
  119. case "read":
  120. set_input('guid', $page[1]);
  121. require(dirname(dirname(dirname(__FILE__))) . "/entities/index.php");
  122. break;
  123. case "owner":
  124. set_input('username', $page[1]);
  125. require($CONFIG->pluginspath . "file/index.php");
  126. break;
  127. case "friends":
  128. set_input('username', $page[1]);
  129. require($CONFIG->pluginspath . "file/friends.php");
  130. break;
  131. case "all":
  132. require($CONFIG->pluginspath . "file/world.php");
  133. break;
  134. case "new":
  135. set_input('username', $page[1]);
  136. require($CONFIG->pluginspath . "file/upload.php");
  137. break;
  138. case "edit":
  139. set_input('file_guid', $page[1]);
  140. require($CONFIG->pluginspath . "file/edit.php");
  141. break;
  142. default:
  143. return false;
  144. }
  145. return true;
  146. }
  147. /**
  148. * Forward to the new style of URLs
  149. *
  150. * @param string $page
  151. */
  152. function file_url_forwarder($page) {
  153. global $CONFIG;
  154. if (!isset($page[1])) {
  155. $page[1] = 'owner';
  156. }
  157. switch ($page[1]) {
  158. case "read":
  159. $url = "{$CONFIG->wwwroot}pg/file/read/{$page[2]}/{$page[3]}";
  160. break;
  161. case "owner":
  162. $url = "{$CONFIG->wwwroot}pg/file/owner/{$page[0]}/";
  163. break;
  164. case "friends":
  165. $url = "{$CONFIG->wwwroot}pg/file/friends/{$page[0]}/";
  166. break;
  167. case "new":
  168. $url = "{$CONFIG->wwwroot}pg/file/new/{$page[0]}/";
  169. break;
  170. }
  171. register_error(elgg_echo("changebookmark"));
  172. forward($url);
  173. }
  174. /**
  175. * Returns a more meaningful message
  176. *
  177. * @param unknown_type $hook
  178. * @param unknown_type $entity_type
  179. * @param unknown_type $returnvalue
  180. * @param unknown_type $params
  181. */
  182. function file_notify_message($hook, $entity_type, $returnvalue, $params) {
  183. $entity = $params['entity'];
  184. $to_entity = $params['to_entity'];
  185. $method = $params['method'];
  186. if (($entity instanceof ElggEntity) && ($entity->getSubtype() == 'file')) {
  187. $descr = $entity->description;
  188. $title = $entity->title;
  189. global $CONFIG;
  190. $url = $CONFIG->wwwroot . "pg/view/" . $entity->guid;
  191. if ($method == 'sms') {
  192. $owner = $entity->getOwnerEntity();
  193. return $owner->name . ' ' . elgg_echo("file:via") . ': ' . $url . ' (' . $title . ')';
  194. }
  195. if ($method == 'email') {
  196. $owner = $entity->getOwnerEntity();
  197. return $owner->name . ' ' . elgg_echo("file:via") . ': ' . $entity->title . "\n\n" . $descr . "\n\n" . $entity->getURL();
  198. }
  199. if ($method == 'web') {
  200. $owner = $entity->getOwnerEntity();
  201. return $owner->name . ' ' . elgg_echo("file:via") . ': ' . $entity->title . "\n\n" . $descr . "\n\n" . $entity->getURL();
  202. }
  203. }
  204. return null;
  205. }
  206. /**
  207. * Returns an overall file type from the mimetype
  208. *
  209. * @param string $mimetype The MIME type
  210. * @return string The overall type
  211. */
  212. function get_general_file_type($mimetype) {
  213. switch ($mimetype) {
  214. case "application/msword":
  215. return "document";
  216. break;
  217. case "application/pdf":
  218. return "document";
  219. break;
  220. }
  221. if (substr_count($mimetype, 'text/'))
  222. return "document";
  223. if (substr_count($mimetype, 'audio/'))
  224. return "audio";
  225. if (substr_count($mimetype, 'image/'))
  226. return "image";
  227. if (substr_count($mimetype, 'video/'))
  228. return "video";
  229. if (substr_count($mimetype, 'opendocument'))
  230. return "document";
  231. return "general";
  232. }
  233. /**
  234. * Returns a list of filetypes to search specifically on
  235. *
  236. * @param int|array $owner_guid The GUID(s) of the owner(s) of the files
  237. * @param true|false $friends Whether we're looking at the owner or the owner's friends
  238. * @return string The typecloud
  239. */
  240. function get_filetype_cloud($owner_guid = "", $friends = false) {
  241. if ($friends) {
  242. if ($friendslist = get_user_friends($user_guid, "", 999999, 0)) {
  243. $friendguids = array();
  244. foreach ($friendslist as $friend) {
  245. $friendguids[] = $friend->getGUID();
  246. }
  247. }
  248. $friendofguid = $owner_guid;
  249. $owner_guid = $friendguids;
  250. } else {
  251. $friendofguid = false;
  252. }
  253. elgg_register_tag_metadata_name('simpletype');
  254. $types = get_tags(0, 10, 'simpletype', 'object', 'file', $owner_guid);
  255. return elgg_view('file/typecloud', array('owner_guid' => $owner_guid, 'friend_guid' => $friendofguid, 'types' => $types));
  256. }
  257. /**
  258. * Populates the ->getUrl() method for file objects
  259. *
  260. * @param ElggEntity $entity File entity
  261. * @return string File URL
  262. */
  263. function file_url($entity) {
  264. global $CONFIG;
  265. $title = $entity->title;
  266. $title = elgg_get_friendly_title($title);
  267. return $CONFIG->url . "pg/file/read/" . $entity->getGUID() . "/" . $title;
  268. }
  269. // Make sure test_init is called on initialisation
  270. register_elgg_event_handler('init', 'system', 'file_init');
  271. register_elgg_event_handler('pagesetup', 'system', 'file_submenus');
  272. // Register actions
  273. register_action("file/upload", false, $CONFIG->pluginspath . "file/actions/upload.php");
  274. register_action("file/save", false, $CONFIG->pluginspath . "file/actions/save.php");
  275. register_action("file/delete", false, $CONFIG->pluginspath . "file/actions/delete.php");
  276. // temporary - see #2010
  277. register_action("file/download", false, $CONFIG->pluginspath . "file/actions/download.php");
  278. ?>