start.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * Elgg profile plugin
  4. *
  5. * @package ElggProfile
  6. */
  7. elgg_register_event_handler('init', 'system', 'profile_init', 1);
  8. // Metadata on users needs to be independent
  9. // outside of init so it happens earlier in boot. See #3316
  10. register_metadata_as_independent('user');
  11. /**
  12. * Profile init function
  13. */
  14. function profile_init() {
  15. // Register a URL handler for users
  16. elgg_register_plugin_hook_handler('entity:url', 'user', 'profile_set_url');
  17. elgg_register_plugin_hook_handler('entity:icon:url', 'user', 'profile_set_icon_url');
  18. elgg_unregister_plugin_hook_handler('entity:icon:url', 'user', 'user_avatar_hook');
  19. elgg_register_simplecache_view('icon/user/default/tiny');
  20. elgg_register_simplecache_view('icon/user/default/topbar');
  21. elgg_register_simplecache_view('icon/user/default/small');
  22. elgg_register_simplecache_view('icon/user/default/medium');
  23. elgg_register_simplecache_view('icon/user/default/large');
  24. elgg_register_simplecache_view('icon/user/default/master');
  25. elgg_register_page_handler('profile', 'profile_page_handler');
  26. elgg_extend_view('css/elgg', 'profile/css');
  27. elgg_extend_view('js/elgg', 'profile/js');
  28. // allow ECML in parts of the profile
  29. elgg_register_plugin_hook_handler('get_views', 'ecml', 'profile_ecml_views_hook');
  30. // allow admins to set default widgets for users on profiles
  31. elgg_register_plugin_hook_handler('get_list', 'default_widgets', 'profile_default_widgets_hook');
  32. elgg_register_event_handler('pagesetup', 'system', 'profile_pagesetup', 50);
  33. }
  34. /**
  35. * Profile page handler
  36. *
  37. * @param array $page Array of URL segments passed by the page handling mechanism
  38. * @return bool
  39. */
  40. function profile_page_handler($page) {
  41. if (isset($page[0])) {
  42. $username = $page[0];
  43. $user = get_user_by_username($username);
  44. elgg_set_page_owner_guid($user->guid);
  45. } elseif (elgg_is_logged_in()) {
  46. forward(elgg_get_logged_in_user_entity()->getURL());
  47. }
  48. // short circuit if invalid or banned username
  49. if (!$user || ($user->isBanned() && !elgg_is_admin_logged_in())) {
  50. register_error(elgg_echo('profile:notfound'));
  51. forward();
  52. }
  53. $action = NULL;
  54. if (isset($page[1])) {
  55. $action = $page[1];
  56. }
  57. if ($action == 'edit') {
  58. // use the core profile edit page
  59. $base_dir = elgg_get_root_path();
  60. require "{$base_dir}pages/profile/edit.php";
  61. return true;
  62. }
  63. $content = elgg_view('profile/layout', array('entity' => $user));
  64. $body = elgg_view_layout('one_column', array(
  65. 'content' => $content
  66. ));
  67. echo elgg_view_page($user->name, $body);
  68. return true;
  69. }
  70. /**
  71. * Profile URL generator for $user->getUrl();
  72. *
  73. * @param string $hook
  74. * @param string $type
  75. * @param string $url
  76. * @param array $params
  77. * @return string
  78. */
  79. function profile_set_url($hook, $type, $url, $params) {
  80. $user = $params['entity'];
  81. return "profile/" . $user->username;
  82. }
  83. /**
  84. * Use a URL for avatars that avoids loading Elgg engine for better performance
  85. *
  86. * @param string $hook
  87. * @param string $type
  88. * @param string $url
  89. * @param array $params
  90. * @return string
  91. */
  92. function profile_set_icon_url($hook, $type, $url, $params) {
  93. // if someone already set this, quit
  94. if ($url) {
  95. return;
  96. }
  97. $user = $params['entity'];
  98. $size = $params['size'];
  99. $user_guid = $user->getGUID();
  100. $icon_time = $user->icontime;
  101. if (!$icon_time) {
  102. return "_graphics/icons/user/default{$size}.gif";
  103. }
  104. $filehandler = new ElggFile();
  105. $filehandler->owner_guid = $user_guid;
  106. $filehandler->setFilename("profile/{$user_guid}{$size}.jpg");
  107. try {
  108. if ($filehandler->exists()) {
  109. $join_date = $user->getTimeCreated();
  110. return "mod/profile/icondirect.php?lastcache=$icon_time&joindate=$join_date&guid=$user_guid&size=$size";
  111. }
  112. } catch (InvalidParameterException $e) {
  113. elgg_log("Unable to get profile icon for user with GUID $user_guid", 'ERROR');
  114. return "_graphics/icons/default/$size.png";
  115. }
  116. }
  117. /**
  118. * Parse ECML on parts of the profile
  119. *
  120. * @param string $hook
  121. * @param string $entity_type
  122. * @param array $return_value
  123. * @return array
  124. */
  125. function profile_ecml_views_hook($hook, $entity_type, $return_value) {
  126. $return_value['profile/profile_content'] = elgg_echo('profile');
  127. return $return_value;
  128. }
  129. /**
  130. * Register profile widgets with default widgets
  131. *
  132. * @param string $hook
  133. * @param string $type
  134. * @param array $return
  135. * @return array
  136. */
  137. function profile_default_widgets_hook($hook, $type, $return) {
  138. $return[] = array(
  139. 'name' => elgg_echo('profile'),
  140. 'widget_context' => 'profile',
  141. 'widget_columns' => 3,
  142. 'event' => 'create',
  143. 'entity_type' => 'user',
  144. 'entity_subtype' => ELGG_ENTITIES_ANY_VALUE,
  145. );
  146. return $return;
  147. }
  148. /**
  149. * Sets up user-related menu items
  150. *
  151. * @return void
  152. * @access private
  153. */
  154. function profile_pagesetup() {
  155. $viewer = elgg_get_logged_in_user_entity();
  156. if (!$viewer) {
  157. return;
  158. }
  159. elgg_register_menu_item('topbar', array(
  160. 'name' => 'profile',
  161. 'href' => $viewer->getURL(),
  162. 'text' => elgg_view('output/img', array(
  163. 'src' => $viewer->getIconURL('topbar'),
  164. 'alt' => $viewer->name,
  165. 'title' => elgg_echo('profile'),
  166. 'class' => 'elgg-border-plain elgg-transition',
  167. )),
  168. 'priority' => 100,
  169. 'link_class' => 'elgg-topbar-avatar',
  170. 'item_class' => 'elgg-avatar elgg-avatar-topbar',
  171. ));
  172. }