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

mod/file/start.php (6 issues)

1
<?php
2
/**
3
 * Elgg file plugin
4
 *
5
 * @package ElggFile
6
 */
7
8
/**
9
 * File plugin initialization functions
10
 *
11
 * @return void
12
 */
13
function file_init() {
14
15
	// register a library of helper functions
16 31
	elgg_register_library('elgg:file', __DIR__ . '/lib/file.php');
17
18
	// Site navigation
19 31
	$item = new ElggMenuItem('file', elgg_echo('file'), 'file/all');
20 31
	elgg_register_menu_item('site', $item);
21
22
	// Extend CSS
23 31
	elgg_extend_view('elgg.css', 'file/file.css');
24
25
	// add enclosure to rss item
26 31
	elgg_extend_view('extensions/item', 'file/enclosure');
27
28
	// extend group main page
29 31
	elgg_extend_view('groups/tool_latest', 'file/group_module');
30
31
	// Register a page handler, so we can have nice URLs
32 31
	elgg_register_page_handler('file', 'file_page_handler');
33
34
	// Register URL handlers for files
35 31
	elgg_register_plugin_hook_handler('entity:url', 'object', 'file_set_url');
36 31
	elgg_register_plugin_hook_handler('entity:icon:url', 'object', 'file_set_icon_url');
37
38
	// Register for notifications
39 31
	elgg_register_notification_event('object', 'file', ['create']);
40 31
	elgg_register_plugin_hook_handler('prepare', 'notification:create:object:file', 'file_prepare_notification');
41
42
	// add the group files tool option
43 31
	add_group_tool_option('file', elgg_echo('groups:enablefiles'), true);
44
45
	// add a file link to owner blocks
46 31
	elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'file_owner_block_menu');
47
48
	// cleanup thumbnails on delete. high priority because we want to try to make sure the
49
	// deletion will actually occur before we go through with this.
50 31
	elgg_register_event_handler('delete', 'object', 'file_handle_object_delete', 999);
51
52
	// embed support
53 31
	$item = ElggMenuItem::factory([
54 31
		'name' => 'file',
55 31
		'text' => elgg_echo('file'),
56 31
		'priority' => 10,
57
		'data' => [
58
			'options' => [
59
				'type' => 'object',
60
				'subtype' => 'file',
61
			],
62
		],
63
	]);
64 31
	elgg_register_menu_item('embed', $item);
65
66 31
	$item = ElggMenuItem::factory([
67 31
		'name' => 'file_upload',
68 31
		'text' => elgg_echo('file:upload'),
69 31
		'priority' => 100,
70
		'data' => [
71
			'view' => 'embed/file_upload/content',
72
		],
73
	]);
74
75 31
	elgg_register_menu_item('embed', $item);
76
77 31
	elgg_extend_view('theme_sandbox/icons', 'file/theme_sandbox/icons/files');
78
79
	// allow to be liked
80 31
	elgg_register_plugin_hook_handler('likes:is_likable', 'object:file', 'Elgg\Values::getTrue');
81
82 31
	elgg_register_plugin_hook_handler('entity:icon:sizes', 'object', 'file_set_custom_icon_sizes');
83 31
	elgg_register_plugin_hook_handler('entity:icon:file', 'object', 'file_set_icon_file');
84
85 31
	elgg_register_plugin_hook_handler('seeds', 'database', 'file_register_db_seeds');
86 31
}
87
88
/**
89
 * Dispatches file pages.
90
 * URLs take the form of
91
 *  All files:       file/all
92
 *  User's files:    file/owner/<username>
93
 *  Friends' files:  file/friends/<username>
94
 *  View file:       file/view/<guid>/<title>
95
 *  New file:        file/add/<guid>
96
 *  Edit file:       file/edit/<guid>
97
 *  Group files:     file/group/<guid>/all
98
 *
99
 * Title is ignored
100
 *
101
 * @param array $page URL segments
102
 *
103
 * @return bool
104
 */
105
function file_page_handler($page) {
106
107
	if (!isset($page[0])) {
108
		$page[0] = 'all';
109
	}
110
111
	$page_type = $page[0];
112
	switch ($page_type) {
113
		case 'owner':
114
			file_register_toggle();
115
			echo elgg_view_resource('file/owner');
116
			break;
117
		case 'friends':
118
			file_register_toggle();
119
			echo elgg_view_resource('file/friends');
120
			break;
121
		case 'view':
122
			echo elgg_view_resource('file/view', [
123
				'guid' => $page[1],
124
			]);
125
			break;
126
		case 'add':
127
			echo elgg_view_resource('file/upload');
128
			break;
129
		case 'edit':
130
			echo elgg_view_resource('file/edit', [
131
				'guid' => $page[1],
132
			]);
133
			break;
134
		case 'group':
135
			file_register_toggle();
136
			echo elgg_view_resource('file/owner');
137
			break;
138
		case 'all':
139
			file_register_toggle();
140
			echo elgg_view_resource('file/all');
141
			break;
142
		default:
143
			return false;
144
	}
145
	return true;
146
}
147
148
/**
149
 * Adds a toggle to filter menu for switching between list and gallery views
150
 *
151
 * @return void
152
 */
153
function file_register_toggle() {
154
155
	if (get_input('list_type', 'list') == 'list') {
156
		$list_type = 'gallery';
157
		$icon = elgg_view_icon('grid');
158
	} else {
159
		$list_type = 'list';
160
		$icon = elgg_view_icon('list');
161
	}
162
163
	$url = elgg_http_add_url_query_elements(current_page_url(), ['list_type' => $list_type]);
164
	
165
	elgg_register_menu_item('filter:file', [
166
		'name' => 'file_list',
167
		'text' => $icon,
168
		'href' => $url,
169
		'title' => elgg_echo("file:list:$list_type"),
170
		'priority' => 1000,
171
	]);
172
}
173
174
/**
175
 * Prepare a notification message about a new file
176
 *
177
 * @param string                          $hook         Hook name
178
 * @param string                          $type         Hook type
179
 * @param Elgg\Notifications\Notification $notification The notification to prepare
180
 * @param array                           $params       Hook parameters
181
 * @return Elgg\Notifications\Notification
182
 */
183
function file_prepare_notification($hook, $type, $notification, $params) {
184
	$entity = $params['event']->getObject();
185
	$owner = $params['event']->getActor();
186
	$recipient = $params['recipient'];
187
	$language = $params['language'];
188
	$method = $params['method'];
189
190
	$descr = $entity->description;
191
	$title = $entity->getDisplayName();
192
193
	$notification->subject = elgg_echo('file:notify:subject', [$title], $language);
194
	$notification->body = elgg_echo('file:notify:body', [
195
		$owner->getDisplayName(),
196
		$title,
197
		$descr,
198
		$entity->getURL()
199
	], $language);
200
	$notification->summary = elgg_echo('file:notify:summary', [$title], $language);
201
	$notification->url = $entity->getURL();
202
	return $notification;
203
}
204
205
/**
206
 * Add a menu item to the user ownerblock
207
 *
208
 * @param string         $hook   'register'
209
 * @param string         $type   'menu:owner_block'
210
 * @param ElggMenuItem[] $return current return value
211
 * @param array          $params supplied params
212
 *
213
 * @return ElggMenuItem[]
214
 */
215
function file_owner_block_menu($hook, $type, $return, $params) {
216
217
	$entity = elgg_extract('entity', $params);
218
	if ($entity instanceof ElggUser) {
219
		$url = "file/owner/{$entity->username}";
220
		$item = new ElggMenuItem('file', elgg_echo('file'), $url);
221
		$return[] = $item;
222
	} elseif ($entity instanceof ElggGroup) {
223
		if ($entity->isToolEnabled('file')) {
224
			$url = "file/group/{$entity->guid}/all";
225
			$item = new ElggMenuItem('file', elgg_echo('file:group'), $url);
226
			$return[] = $item;
227
		}
228
	}
229
	
230
	return $return;
231
}
232
233
/**
234
 * Populates the ->getUrl() method for file objects
235
 *
236
 * @param string $hook   'entity:url'
237
 * @param string $type   'object'
238
 * @param string $url    current return value
239
 * @param array  $params supplied params
240
 *
241
 * @return void|string
242
 */
243
function file_set_url($hook, $type, $url, $params) {
244
	
245 3
	$entity = elgg_extract('entity', $params);
246 3
	if (!$entity instanceof ElggFile) {
247 3
		return;
248
	}
249
	
250
	$title = elgg_get_friendly_title($entity->getDisplayName());
251
	return "file/view/{$entity->getGUID()}/{$title}";
252
}
253
254
/**
255
 * Override the default entity icon for files
256
 *
257
 * Plugins can override or extend the icons using the plugin hook: 'file:icon:url', 'override'
258
 *
259
 * @param string $hook   'entity:icon:url'
260
 * @param string $type   'object'
261
 * @param string $url    current return value
262
 * @param array  $params supplied params
263
 *
264
 * @return void|string
265
 */
266
function file_set_icon_url($hook, $type, $url, $params) {
267
	
268 1
	$file = elgg_extract('entity', $params);
269 1
	if (!$file instanceof ElggFile) {
270 1
		return;
271
	}
272
	
273
	$size = elgg_extract('size', $params, 'large');
274
	
275
	// thumbnails get first priority
276
	if ($file->hasIcon($size)) {
277
		return $file->getIcon($size)->getInlineURL(true);
278
	}
279
280
	$mapping = [
281
		'application/excel' => 'excel',
282
		'application/msword' => 'word',
283
		'application/ogg' => 'music',
284
		'application/pdf' => 'pdf',
285
		'application/powerpoint' => 'ppt',
286
		'application/vnd.ms-excel' => 'excel',
287
		'application/vnd.ms-powerpoint' => 'ppt',
288
		'application/vnd.oasis.opendocument.text' => 'openoffice',
289
		'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'word',
290
		'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'excel',
291
		'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'ppt',
292
		'application/x-gzip' => 'archive',
293
		'application/x-rar-compressed' => 'archive',
294
		'application/x-stuffit' => 'archive',
295
		'application/zip' => 'archive',
296
		'text/directory' => 'vcard',
297
		'text/v-card' => 'vcard',
298
		'application' => 'application',
299
		'audio' => 'music',
300
		'text' => 'text',
301
		'video' => 'video',
302
	];
303
304
	$mime = $file->getMimeType();
305
	if ($mime) {
306
		$base_type = substr($mime, 0, strpos($mime, '/'));
307
	} else {
308
		$mime = 'none';
309
		$base_type = 'none';
310
	}
311
312
	if (isset($mapping[$mime])) {
313
		$type = $mapping[$mime];
314
	} elseif (isset($mapping[$base_type])) {
315
		$type = $mapping[$base_type];
316
	} else {
317
		$type = 'general';
318
	}
319
320
	if ($size == 'large') {
321
		$ext = '_lrg';
322
	} else {
323
		$ext = '';
324
	}
325
326
	$url = elgg_get_simplecache_url("file/icons/{$type}{$ext}.gif");
327
	$url = elgg_trigger_plugin_hook('file:icon:url', 'override', $params, $url);
328
	
329
	return $url;
330
}
331
332
/**
333
 * Handle an object being deleted
334
 *
335
 * @param string     $event Event name
336
 * @param string     $type  Event type
337
 * @param ElggObject $file  The object deleted
338
 * @return void
339
 */
340
function file_handle_object_delete($event, $type, ElggObject $file) {
2 ignored issues
show
The parameter $type 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

340
function file_handle_object_delete($event, /** @scrutinizer ignore-unused */ $type, ElggObject $file) {

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...
The parameter $event 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

340
function file_handle_object_delete(/** @scrutinizer ignore-unused */ $event, $type, ElggObject $file) {

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...
341 172
	if (!$file instanceof ElggFile) {
342 171
		return;
343
	}
344 1
	if (!$file->guid) {
345
		// this is an ElggFile used as temporary API
346
		return;
347
	}
348
349 1
	$file->deleteIcon();
350 1
}
351
352
/**
353
 * Set custom icon sizes for file objects
354
 *
355
 * @param string $hook   "entity:icon:url"
356
 * @param string $type   "object"
357
 * @param array  $return Sizes
358
 * @param array  $params Hook params
359
 * @return array
360
 */
361
function file_set_custom_icon_sizes($hook, $type, $return, $params) {
2 ignored issues
show
The parameter $hook 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

361
function file_set_custom_icon_sizes(/** @scrutinizer ignore-unused */ $hook, $type, $return, $params) {

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...
The parameter $type 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

361
function file_set_custom_icon_sizes($hook, /** @scrutinizer ignore-unused */ $type, $return, $params) {

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...
362
363 6
	$entity_subtype = elgg_extract('entity_subtype', $params);
364 6
	if ($entity_subtype !== 'file') {
365 5
		return;
366
	}
367
368 1
	$return['small'] = [
369
		'w' => 60,
370
		'h' => 60,
371
		'square' => true,
372
		'upscale' => true,
373
	];
374 1
	$return['medium'] = [
375
		'w' => 153,
376
		'h' => 153,
377
		'square' => true,
378
		'upscale' => true,
379
	];
380 1
	$return['large'] = [
381
		'w' => 600,
382
		'h' => 600,
383
		'upscale' => false,
384
	];
385
	
386 1
	return $return;
387
}
388
389
/**
390
 * Set custom file thumbnail location
391
 *
392
 * @param string    $hook   "entity:icon:file"
393
 * @param string    $type   "object"
394
 * @param \ElggIcon $icon   Icon file
395
 * @param array     $params Hook params
396
 * @return \ElggIcon
397
 */
398
function file_set_icon_file($hook, $type, $icon, $params) {
2 ignored issues
show
The parameter $hook 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

398
function file_set_icon_file(/** @scrutinizer ignore-unused */ $hook, $type, $icon, $params) {

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...
The parameter $type 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

398
function file_set_icon_file($hook, /** @scrutinizer ignore-unused */ $type, $icon, $params) {

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...
399
400 6
	$entity = elgg_extract('entity', $params);
401 6
	$size = elgg_extract('size', $params, 'large');
402
403 6
	if (!($entity instanceof \ElggFile)) {
404 5
		return;
405
	}
406
	
407 1
	switch ($size) {
408
		case 'small' :
409 1
			$filename_prefix = 'thumb';
410 1
			$metadata_name = 'thumbnail';
411 1
			break;
412
413
		case 'medium' :
414 1
			$filename_prefix = 'smallthumb';
415 1
			$metadata_name = 'smallthumb';
416 1
			break;
417
418
		default :
419 1
			$filename_prefix = "{$size}thumb";
420 1
			$metadata_name = $filename_prefix;
421 1
			break;
422
	}
423
424 1
	$icon->owner_guid = $entity->owner_guid;
425 1
	if (isset($entity->$metadata_name)) {
426
		$icon->setFilename($entity->$metadata_name);
427
	} else {
428 1
		$filename = pathinfo($entity->getFilenameOnFilestore(), PATHINFO_FILENAME);
429 1
		$filename = "file/{$filename_prefix}{$filename}.jpg";
430 1
		$icon->setFilename($filename);
431
	}
432
	
433 1
	return $icon;
434
}
435
436
/**
437
 * Register database seed
438
 *
439
 * @elgg_plugin_hook seeds database
440
 *
441
 * @param \Elgg\Hook $hook Hook
442
 * @return array
443
 */
444
function file_register_db_seeds(\Elgg\Hook $hook) {
445
446
	$seeds = $hook->getValue();
447
448
	$seeds[] = \Elgg\File\Seeder::class;
449
450
	return $seeds;
451
}
452
453
return function() {
454 18
	elgg_register_event_handler('init', 'system', 'file_init');
455
};
456