Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/lib/pageowner.php (1 issue)

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

265
return function(\Elgg\EventsService $events, /** @scrutinizer ignore-unused */ \Elgg\HooksRegistrationService $hooks) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
266 18
	$events->registerHandler('init', 'system', '_elgg_pageowner_init');
267
};
268