PageRenderTime 88ms CodeModel.GetById 22ms RepoModel.GetById 2ms app.codeStats 0ms

/engine/lib/pageowner.php

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