pageowner.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. /**
  3. * Elgg page owner library
  4. * Contains functions for managing page ownership and context
  5. *
  6. * @package Elgg.Core
  7. * @subpackage PageOwner
  8. */
  9. /**
  10. * Gets the guid of the entity that owns the current page.
  11. *
  12. * @param int $guid Optional parameter used by elgg_set_page_owner_guid().
  13. *
  14. * @return int The current page owner guid (0 if none).
  15. * @since 1.8.0
  16. */
  17. function elgg_get_page_owner_guid($guid = 0) {
  18. static $page_owner_guid;
  19. if ($guid === false || $guid === null) {
  20. $page_owner_guid = 0;
  21. return $page_owner_guid;
  22. }
  23. if ($guid) {
  24. $page_owner_guid = (int)$guid;
  25. }
  26. if (isset($page_owner_guid)) {
  27. return $page_owner_guid;
  28. }
  29. // return guid of page owner entity
  30. $guid = (int)elgg_trigger_plugin_hook('page_owner', 'system', null, 0);
  31. if ($guid) {
  32. $page_owner_guid = $guid;
  33. }
  34. return $guid;
  35. }
  36. /**
  37. * Gets the owner entity for the current page.
  38. *
  39. * @note Access is disabled when getting the page owner entity.
  40. *
  41. * @return \ElggUser|\ElggGroup|false The current page owner or false if none.
  42. *
  43. * @since 1.8.0
  44. */
  45. function elgg_get_page_owner_entity() {
  46. $guid = elgg_get_page_owner_guid();
  47. if ($guid > 0) {
  48. $ia = elgg_set_ignore_access(true);
  49. $owner = get_entity($guid);
  50. elgg_set_ignore_access($ia);
  51. return $owner;
  52. }
  53. return false;
  54. }
  55. /**
  56. * Set the guid of the entity that owns this page
  57. *
  58. * @param int $guid The guid of the page owner
  59. * @return void
  60. * @since 1.8.0
  61. */
  62. function elgg_set_page_owner_guid($guid) {
  63. elgg_get_page_owner_guid($guid);
  64. }
  65. /**
  66. * Sets the page owner based on request
  67. *
  68. * Tries to figure out the page owner by looking at the URL or a request
  69. * parameter. The request parameters used are 'username' and 'owner_guid'.
  70. * Otherwise, this function attempts to figure out the owner if the url
  71. * fits the patterns of:
  72. * <identifier>/owner/<username>
  73. * <identifier>/friends/<username>
  74. * <identifier>/view/<entity guid>
  75. * <identifier>/add/<container guid>
  76. * <identifier>/edit/<entity guid>
  77. * <identifier>/group/<group guid>
  78. *
  79. * @note Access is disabled while finding the page owner for the group gatekeeper functions.
  80. *
  81. *
  82. * @param string $hook 'page_owner'
  83. * @param string $entity_type 'system'
  84. * @param int $returnvalue Previous function's return value
  85. * @param array $params no parameters
  86. *
  87. * @return int GUID
  88. * @access private
  89. */
  90. function default_page_owner_handler($hook, $entity_type, $returnvalue, $params) {
  91. if ($returnvalue) {
  92. return $returnvalue;
  93. }
  94. $ia = elgg_set_ignore_access(true);
  95. $username = get_input("username");
  96. if ($username) {
  97. // @todo using a username of group:<guid> is deprecated
  98. if (substr_count($username, 'group:')) {
  99. preg_match('/group\:([0-9]+)/i', $username, $matches);
  100. $guid = $matches[1];
  101. if ($entity = get_entity($guid)) {
  102. elgg_set_ignore_access($ia);
  103. return $entity->getGUID();
  104. }
  105. }
  106. if ($user = get_user_by_username($username)) {
  107. elgg_set_ignore_access($ia);
  108. return $user->getGUID();
  109. }
  110. }
  111. $owner = get_input("owner_guid");
  112. if ($owner) {
  113. if ($user = get_entity($owner)) {
  114. elgg_set_ignore_access($ia);
  115. return $user->getGUID();
  116. }
  117. }
  118. // ignore root and query
  119. $uri = current_page_url();
  120. $path = str_replace(elgg_get_site_url(), '', $uri);
  121. $path = trim($path, "/");
  122. if (strpos($path, "?")) {
  123. $path = substr($path, 0, strpos($path, "?"));
  124. }
  125. // @todo feels hacky
  126. $segments = explode('/', $path);
  127. if (isset($segments[1]) && isset($segments[2])) {
  128. switch ($segments[1]) {
  129. case 'owner':
  130. case 'friends':
  131. $user = get_user_by_username($segments[2]);
  132. if ($user) {
  133. elgg_set_ignore_access($ia);
  134. return $user->getGUID();
  135. }
  136. break;
  137. case 'view':
  138. case 'edit':
  139. $entity = get_entity($segments[2]);
  140. if ($entity) {
  141. elgg_set_ignore_access($ia);
  142. return $entity->getContainerGUID();
  143. }
  144. break;
  145. case 'add':
  146. case 'group':
  147. $entity = get_entity($segments[2]);
  148. if ($entity) {
  149. elgg_set_ignore_access($ia);
  150. return $entity->getGUID();
  151. }
  152. break;
  153. }
  154. }
  155. elgg_set_ignore_access($ia);
  156. }
  157. /**
  158. * Sets the page context
  159. *
  160. * Views can modify their output based on the local context. You may want to
  161. * display a list of blogs on a blog page or in a small widget. The rendered
  162. * output could be different for those two contexts ('blog' vs 'widget').
  163. *
  164. * Pages that pass through the page handling system set the context to the
  165. * first string after the root url. Example: http://example.org/elgg/bookmarks/
  166. * results in the initial context being set to 'bookmarks'.
  167. *
  168. * The context is a stack so that for a widget on a profile, the context stack
  169. * may contain first 'profile' and then 'widget'.
  170. *
  171. * If no context was been set, the default context returned is 'main'.
  172. *
  173. * @warning The context is not available until the page_handler runs (after
  174. * the 'init, system' event processing has completed).
  175. *
  176. * @param string $context The context of the page
  177. * @return bool
  178. * @since 1.8.0
  179. */
  180. function elgg_set_context($context) {
  181. return _elgg_services()->context->set($context);
  182. }
  183. /**
  184. * Get the current context.
  185. *
  186. * Since context is a stack, this is equivalent to a peek.
  187. *
  188. * @return string|null
  189. * @since 1.8.0
  190. */
  191. function elgg_get_context() {
  192. return _elgg_services()->context->peek();
  193. }
  194. /**
  195. * Push a context onto the top of the stack
  196. *
  197. * @param string $context The context string to add to the context stack
  198. * @return void
  199. * @since 1.8.0
  200. */
  201. function elgg_push_context($context) {
  202. _elgg_services()->context->push($context);
  203. }
  204. /**
  205. * Removes and returns the top context string from the stack
  206. *
  207. * @return string|null
  208. * @since 1.8.0
  209. */
  210. function elgg_pop_context() {
  211. return _elgg_services()->context->pop();
  212. }
  213. /**
  214. * Check if this context exists anywhere in the stack
  215. *
  216. * This is useful for situations with more than one element in the stack. For
  217. * example, a widget has a context of 'widget'. If a widget view needs to render
  218. * itself differently based on being on the dashboard or profile pages, it
  219. * can check the stack.
  220. *
  221. * @param string $context The context string to check for
  222. * @return bool
  223. * @since 1.8.0
  224. */
  225. function elgg_in_context($context) {
  226. return _elgg_services()->context->contains($context);
  227. }
  228. /**
  229. * Get the entire context stack (e.g. for backing it up)
  230. *
  231. * @return string[]
  232. * @since 1.11
  233. */
  234. function elgg_get_context_stack() {
  235. return _elgg_services()->context->toArray();
  236. }
  237. /**
  238. * Set the entire context stack
  239. *
  240. * @param string[] $stack All contexts to be placed on the stack
  241. * @return void
  242. * @since 1.11
  243. */
  244. function elgg_set_context_stack(array $stack) {
  245. _elgg_services()->context->fromArray($stack);
  246. }
  247. /**
  248. * Initializes the page owner functions
  249. *
  250. * @note This is on the 'boot, system' event so that the context is set up quickly.
  251. *
  252. * @return void
  253. * @access private
  254. */
  255. function page_owner_boot() {
  256. elgg_register_plugin_hook_handler('page_owner', 'system', 'default_page_owner_handler');
  257. // Bootstrap the context stack by setting its first entry to the handler.
  258. // This is the first segment of the URL and the handler is set by the rewrite rules.
  259. // @todo this does not work for actions
  260. $request = _elgg_services()->request;
  261. // don't do this for *_handler.php, etc.
  262. if (basename($request->server->get('SCRIPT_FILENAME')) === 'index.php') {
  263. $context = $request->getFirstUrlSegment();
  264. if (!$context) {
  265. $context = 'main';
  266. }
  267. elgg_set_context($context);
  268. }
  269. }
  270. return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
  271. $events->registerHandler('boot', 'system', 'page_owner_boot');
  272. };