aboutsummaryrefslogtreecommitdiff
path: root/mod/file/actions/upload.php
blob: 1f12dad9a11362773fa5aa7c6c579a24b7c16666 (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
<?php
	/**
	 * Elgg file browser uploader/edit action
	 * 
	 * @package ElggFile
	 * @author Curverider Ltd
	 * @copyright Curverider Ltd 2008-2010
	 * @link http://elgg.com/
	 */

	global $CONFIG;
	
	gatekeeper();
	
	// Get variables
	$title = get_input("title");
	$desc = get_input("description");
	$access_id = (int) get_input("access_id");
	$container_guid = (int) get_input('container_guid', 0);
	$ajax = get_input('ajax', FALSE);
	
	if ($container_guid == 0) {
		$container_guid = get_loggedin_userid();
	}
	$guid = (int) get_input('file_guid');
	$tags = get_input("tags");
	
	// check whether this is a new file or an edit
	$new_file = true;
	if ($guid > 0) {
		$new_file = false;
	}
	
	if ($new_file) {
		// must have a file if a new file upload
		if (empty($_FILES['upload']['name'])) {
			// cache information in session
			$_SESSION['uploadtitle'] = $title;
			$_SESSION['uploaddesc'] = $desc;
			$_SESSION['uploadtags'] = $tags;
			$_SESSION['uploadaccessid'] = $access_id;
			
			$error = elgg_echo('file:nofile');
			
			if ($ajax) {
				echo json_encode(array(
					'status' => 'error',
					'message' => $error
				));
				exit;
			} else {
				register_error($error);
				forward($_SERVER['HTTP_REFERER']);
			}
		}
		
		$file = new FilePluginFile();
		$file->subtype = "file";
		
		// if no title on new upload, grab filename
		if (empty($title)) {
			$title = $_FILES['upload']['name'];
		}
	
	} else {
		// load original file object
		$file = get_entity($guid);
		if (!$file) {
			register_error(elgg_echo('file:cannotload'));
			forward($_SERVER['HTTP_REFERER']);
		}
		
		// user must be able to edit file
		if (!$file->canEdit()) {
			register_error(elgg_echo('file:noaccess'));
			forward($_SERVER['HTTP_REFERER']);
		}
	}
	
	$file->title = $title;
	$file->description = $desc;
	$file->access_id = $access_id;
	$file->container_guid = $container_guid;
	
	$tags = explode(",", $tags);
	$file->tags = $tags;
	
	// we have a file upload, so process it
	if (isset($_FILES['upload']['name']) && !empty($_FILES['upload']['name'])) {
		
		$prefix = "file/";
		
		// if previous file, delete it
		if ($new_file == false) {
			$filename = $file->getFilenameOnFilestore();
			if (file_exists($filename)) {
				unlink($filename);
			}

			// use same filename on the disk - ensures thumbnails are overwritten
			$filestorename = $file->getFilename();
			$filestorename = elgg_substr($filestorename, elgg_strlen($prefix));
		} else {
			$filestorename = elgg_strtolower(time().$_FILES['upload']['name']);
		}
		
		$file->setFilename($prefix.$filestorename);
		$file->setMimeType($_FILES['upload']['type']);
		$file->originalfilename = $_FILES['upload']['name'];
		$file->simpletype = get_general_file_type($_FILES['upload']['type']);
	
		$file->open("write");
		$file->write(get_uploaded_file('upload'));
		$file->close();
		
		$guid = $file->save();
		
		// if image, we need to create thumbnails (this should be moved into a function)
		if ($guid && $file->simpletype == "image") {
			$thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),60,60, true);
			if ($thumbnail) {
				$thumb = new ElggFile();
				$thumb->setMimeType($_FILES['upload']['type']);
				
				$thumb->setFilename($prefix."thumb".$filestorename);
				$thumb->open("write");
				$thumb->write($thumbnail);
				$thumb->close();
				
				$file->thumbnail = $prefix."thumb".$filestorename;
				unset($thumbnail);
			}
			
			$thumbsmall = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),153,153, true);
			if ($thumbsmall) {
				$thumb->setFilename($prefix."smallthumb".$filestorename);
				$thumb->open("write");
				$thumb->write($thumbsmall);
				$thumb->close();
				$file->smallthumb = $prefix."smallthumb".$filestorename;
				unset($thumbsmall);
			}
			
			$thumblarge = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),600,600, false);
			if ($thumblarge) {
				$thumb->setFilename($prefix."largethumb".$filestorename);
				$thumb->open("write");
				$thumb->write($thumblarge);
				$thumb->close();
				$file->largethumb = $prefix."largethumb".$filestorename;
				unset($thumblarge);
			}
		}
	} else {
		// not saving a file but still need to save the entity to push attributes to database
		$file->save();
	}
	
	// make sure session cache is cleared
	unset($_SESSION['uploadtitle']);
	unset($_SESSION['uploaddesc']);
	unset($_SESSION['uploadtags']);
	unset($_SESSION['uploadaccessid']);
	
	// handle results differently for new files and file updates
	// ajax is only for new files from embed right now.
	if ($new_file) {
		if ($guid) {
			$message = elgg_echo("file:saved");
			if ($ajax) {
				echo json_encode(array(
					'status' => 'success',
					'message' => $message
				));
				exit;
				
			} else {
				system_message($message);
				add_to_river('river/object/file/create', 'create', get_loggedin_userid(), $file->guid);
			}
		} else {
			// failed to save file object - nothing we can do about this
			$error = elgg_echo("file:uploadfailed");
			
			if ($ajax) {
				echo json_encode(array(
					'status' => 'error',
					'message' => $error
				));
				exit;
				
			} else {
				register_error($error);
			}
		}
	
		if (!$ajax) {
			$container_user = get_entity($container_guid);
			forward($CONFIG->wwwroot . "pg/file/" . $container_user->username);
		}
	
	} else {
		if ($guid) {
			system_message(elgg_echo("file:saved"));
		} else {
			register_error(elgg_echo("file:uploadfailed"));
		}
		
		forward($file->getURL());
	}