aboutsummaryrefslogtreecommitdiff
path: root/mod/file/start.php
blob: 31362308c89af6dd8915a8a18e1dec029a833be1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
	/**
	 * Elgg file browser
	 *
	 * @package ElggFile
	 */

	/**
	 * Override the ElggFile so that
	 */
	class FilePluginFile extends ElggFile {
		protected function initialise_attributes() {
			parent::initialise_attributes();

			$this->attributes['subtype'] = "file";
		}

		public function __construct($guid = null) {
			parent::__construct($guid);
		}
	}


	/**
	 * File plugin initialisation functions.
	 */
	function file_init() {
		global $CONFIG;

		// Set up menu (tools dropdown)
		$item = new ElggMenuItem('file', elgg_echo('file'), 'pg/file');
		elgg_register_menu_item('site', $item);

		// Extend CSS
		elgg_extend_view('css/screen', 'file/css');

		// extend group main page
		elgg_extend_view('groups/tool_latest','file/groupprofile_files');

		// Register a page handler, so we can have nice URLs
		register_page_handler('file','file_page_handler');

		// Add a new file widget
		elgg_register_widget_type('filerepo',elgg_echo("file"),elgg_echo("file:widget:description"));

		// Register a URL handler for files
		register_entity_url_handler('file_url','object','file');

		// Register granular notification for this object type
		if (is_callable('register_notification_object')) {
			register_notification_object('object', 'file', elgg_echo('file:newupload'));
		}

		// Listen to notification events and supply a more useful message
		elgg_register_plugin_hook_handler('notify:entity:message', 'object', 'file_notify_message');

		// add the group files tool option
		add_group_tool_option('file',elgg_echo('groups:enablefiles'),true);

		// Register entity type
		register_entity_type('object','file');

		elgg_register_plugin_hook_handler('register', 'menu:user_ownerblock', 'file_user_ownerblock_menu');

		// embed support
		elgg_register_plugin_hook_handler('embed_get_sections', 'all', 'file_embed_get_sections');
		elgg_register_plugin_hook_handler('embed_get_items', 'file', 'file_embed_get_items');
		elgg_register_plugin_hook_handler('embed_get_upload_sections', 'all', 'file_embed_get_upload_sections');

	}

	/**
	 * Sets up submenus for the file system.  Triggered on pagesetup.
	 *
	 */
	function file_submenus() {

		global $CONFIG;

		$page_owner = elgg_get_page_owner();

		// Group submenu option
			if ($page_owner instanceof ElggGroup && elgg_get_context() == "groups") {
				if($page_owner->file_enable != "no"){
					add_submenu_item(elgg_echo("file:group",array($page_owner->name)), $CONFIG->wwwroot . "pg/file/" . $page_owner->username);
				}
			}
	}

	/**
	 * File page handler
	 *
	 * @param array $page Array of page elements, forwarded by the page handling mechanism
	 */
	function file_page_handler($page) {

		global $CONFIG;

		// The username should be the file we're getting
		if (isset($page[0])) {
			set_input('username',$page[0]);
		}

		if (isset($page[1])) {
			switch($page[1]) {
				case "read":
					set_input('guid',$page[2]);
					include(dirname(dirname(dirname(__FILE__))) . "/pages/entities/index.php");
				break;
				case "friends":
					include($CONFIG->pluginspath . "file/friends.php");
				  break;
				case "world":
					include($CONFIG->pluginspath . "file/world.php");
				  break;
				case "new":
					include($CONFIG->pluginspath . "file/upload.php");
				  break;
			}
		} else {
			// Include the standard profile index
			include($CONFIG->pluginspath . "file/index.php");
		}

	}

	/**
		 * Returns a more meaningful message
		 *
		 * @param unknown_type $hook
		 * @param unknown_type $entity_type
		 * @param unknown_type $returnvalue
		 * @param unknown_type $params
	*/
		function file_notify_message($hook, $entity_type, $returnvalue, $params) {
			$entity = $params['entity'];
			$to_entity = $params['to_entity'];
			$method = $params['method'];
			if (($entity instanceof ElggEntity) && ($entity->getSubtype() == 'file'))
			{
				$descr = $entity->description;
				$title = $entity->title;
				global $CONFIG;
				$url = elgg_get_site_url() . "pg/view/" . $entity->guid;
				if ($method == 'sms') {
					$owner = $entity->getOwnerEntity();
					return $owner->name . ' ' . elgg_echo("file:via") . ': ' . $url . ' (' . $title . ')';
				}
				if ($method == 'email') {
					$owner = $entity->getOwnerEntity();
					return $owner->name . ' ' . elgg_echo("file:via") . ': ' . $entity->title . "\n\n" . $descr . "\n\n" . $entity->getURL();
				}
				if ($method == 'web') {
					$owner = $entity->getOwnerEntity();
					return $owner->name . ' ' . elgg_echo("file:via") . ': ' . $entity->title . "\n\n" . $descr . "\n\n" . $entity->getURL();
				}
			}
			return null;
		}

/**
 * Add a menu item to the user ownerblock
 */
function file_user_ownerblock_menu($hook, $type, $return, $params) {
	$item = new ElggMenuItem('file', elgg_echo('file'), "pg/file/owner/{$params['user']->username}");
	elgg_register_menu_item('user_ownerblock', $item);
}

	/**
	 * Returns an overall file type from the mimetype
	 *
	 * @param string $mimetype The MIME type
	 * @return string The overall type
	 */
	function get_general_file_type($mimetype) {

		switch($mimetype) {
			case "application/msword":
				return "document";
				break;
			case "application/pdf":
				return "document";
				break;
		}

		if (substr_count($mimetype,'text/'))
			return "document";

		if (substr_count($mimetype,'audio/'))
			return "audio";

		if (substr_count($mimetype,'image/'))
			return "image";

		if (substr_count($mimetype,'video/'))
			return "video";

		if (substr_count($mimetype,'opendocument'))
			return "document";

		return "general";
	}

	/**
	 * Returns a list of filetypes to search specifically on
	 *
	 * @param int|array $owner_guid The GUID(s) of the owner(s) of the files
	 * @param true|false $friends Whether we're looking at the owner or the owner's friends
	 * @return string The typecloud
	 */
	function get_filetype_cloud($owner_guid = "", $friends = false) {

		if ($friends) {
			if ($friendslist = get_user_friends($user_guid, "", 999999, 0)) {
				$friendguids = array();
				foreach($friendslist as $friend) {
					$friendguids[] = $friend->getGUID();
				}
			}
			$friendofguid = $owner_guid;
			$owner_guid = $friendguids;
		} else {
			$friendofguid = false;
		}

		elgg_register_tag_metadata_name('simpletype');
		$options = array(
			'type' => 'object',
			'subtype' => 'file',
			'owner_guid' => $owner_guid,
			'threshold' => 0,
			'limit' => 10,
			'tag_names' => array('simpletype')
		);
		$types = elgg_get_tags($options);

		return elgg_view('file/typecloud',array('owner_guid' => $owner_guid, 'friend_guid' => $friendofguid, 'types' => $types));
	}

	/**
	 * Register file as an embed type.
	 *
	 * @param unknown_type $hook
	 * @param unknown_type $type
	 * @param unknown_type $value
	 * @param unknown_type $params
	 */
	function file_embed_get_sections($hook, $type, $value, $params) {
		$value['file'] = array(
			'name' => elgg_echo('file'),
			'layout' => 'list',
			'icon_size' => 'small',
		);

		return $value;
	}

	/**
	 * Return a list of files for embedding
	 *
	 * @param unknown_type $hook
	 * @param unknown_type $type
	 * @param unknown_type $value
	 * @param unknown_type $params
	 */
	function file_embed_get_items($hook, $type, $value, $params) {
		$options = array(
			'owner_guid' => get_loggedin_userid(),
			'type_subtype_pair' => array('object' => 'file'),
			'count' => TRUE
		);

		if ($count = elgg_get_entities($options)) {
			$value['count'] += $count;

			unset($options['count']);
			$options['offset'] = $params['offset'];
			$options['limit'] = $params['limit'];

			$items = elgg_get_entities($options);

			$value['items'] = array_merge($items, $value['items']);
		}

		return $value;
	}

	/**
	 * Register file as an embed type.
	 *
	 * @param unknown_type $hook
	 * @param unknown_type $type
	 * @param unknown_type $value
	 * @param unknown_type $params
	 */
	function file_embed_get_upload_sections($hook, $type, $value, $params) {
		$value['file'] = array(
			'name' => elgg_echo('file'),
			'view' => 'file/embed_upload'
		);

		return $value;
	}


	/**
	 * Populates the ->getUrl() method for file objects
	 *
	 * @param ElggEntity $entity File entity
	 * @return string File URL
	 */
		function file_url($entity) {
			$title = $entity->title;
			$title = elgg_get_friendly_title($title);
			return "pg/file/" . $entity->getOwnerEntity()->username . "/read/" . $entity->getGUID() . "/" . $title;
		}

	// Make sure test_init is called on initialisation
	elgg_register_event_handler('init','system','file_init');
	elgg_register_event_handler('pagesetup','system','file_submenus');

	// Register actions
	elgg_register_action("file/upload", $CONFIG->pluginspath . "file/actions/upload.php");
	elgg_register_action("file/save", $CONFIG->pluginspath . "file/actions/save.php");
	elgg_register_action("file/delete", $CONFIG->pluginspath. "file/actions/delete.php");

	// temporary - see #2010
	elgg_register_action("file/download", $CONFIG->pluginspath. "file/actions/download.php");

?>