aboutsummaryrefslogtreecommitdiff
path: root/actions/upload.php
blob: 8dae852778a1ee89cb636ffda0176b7284761b8c (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
<?php
/**
 * Elgg multi-image uploader action
 *
 * This will upload up to 10 images at at time to an album
 */

include_once dirname(dirname(__FILE__)) . "/lib/upload.php";

// Get common variables
$access_id = (int) get_input("access_id");
$container_guid = (int) get_input('container_guid', 0);
$album = get_entity($container_guid);
if (!$album) {
	register_error(elgg_echo('tidypics:baduploadform'));
	forward($_SERVER['HTTP_REFERER']);
}

$image_lib = get_plugin_setting('image_lib', 'tidypics');
if (!$image_lib) {
	$image_lib = "GD";
}

$img_river_view = get_plugin_setting('img_river_view', 'tidypics');


// post limit exceeded
if (count($_FILES) == 0) {
	trigger_error('Tidypics warning: user exceeded post limit on image upload', E_USER_WARNING);
	register_error(elgg_echo('tidypics:exceedpostlimit'));
	forward($_SERVER['HTTP_REFERER']);
}

// test to make sure at least 1 image was selected by user
$num_images = 0;
foreach($_FILES as $key => $sent_file) {
	if (!empty($sent_file['name'])) {
		$num_images++;
	}
}
if ($num_images == 0) {
	// have user try again
	register_error(elgg_echo('tidypics:noimages'));
	forward($_SERVER['HTTP_REFERER']);
}

$uploaded_images = array();
$not_uploaded = array();
$error_msgs = array();

foreach($_FILES as $key => $sent_file) {

	// skip empty entries
	if (empty($sent_file['name'])) {
		continue;
	}

	$name = $sent_file['name'];
	$mime = $sent_file['type'];

	if ($sent_file['error']) {
		array_push($not_uploaded, $sent_file['name']);
		if ($sent_file['error'] == 1) {
			trigger_error('Tidypics warning: image exceeded server php upload limit', E_USER_WARNING);
			array_push($error_msgs, elgg_echo('tidypics:image_mem'));
		} else {
			array_push($error_msgs, elgg_echo('tidypics:unk_error'));
		}
		continue;
	}

	// must be an image
	if (!tp_upload_check_format($mime)) {
		array_push($not_uploaded, $sent_file['name']);
		array_push($error_msgs, elgg_echo('tidypics:not_image'));
		continue;
	}

	// check quota
	if (!tp_upload_check_quota($sent_file['size'], get_loggedin_userid())) {
		array_push($not_uploaded, $sent_file['name']);
		array_push($error_msgs, elgg_echo('tidypics:exceed_quota'));
		continue;
	}

	// make sure file does not exceed memory limit
	if (!tp_upload_check_max_size($sent_file['size'])) {
		array_push($not_uploaded, $sent_file['name']);
		array_push($error_msgs, elgg_echo('tidypics:image_mem'));
		continue;
	}

	// make sure the in memory image size does not exceed memory available
	$imginfo = getimagesize($sent_file['tmp_name']);
	if (!tp_upload_memory_check($image_lib, $imginfo[0] * $imginfo[1])) {
		array_push($not_uploaded, $sent_file['name']);
		array_push($error_msgs, elgg_echo('tidypics:image_pixels'));
		trigger_error('Tidypics warning: image memory size too large for resizing so rejecting', E_USER_WARNING);
		continue;
	}

	//this will save to users folder in /image/ and organize by photo album
	$file = new TidypicsImage();
	$file->container_guid = $container_guid;
	$file->setMimeType($mime);
	$file->simpletype="image";
	$file->access_id = $access_id;
	//$file->title = substr($name, 0, strrpos($name, '.'));

	$result = $file->save();

	if (!$result) {
		array_push($not_uploaded, $sent_file['name']);
		array_push($error_msgs, elgg_echo('tidypics:save_error'));
		continue;
	}

	$file->setOriginalFilename($name);
	$file->saveImageFile($sent_file['tmp_name'], $sent_file['size']);
	$file->extractExifData();
	$file->saveThumbnails($image_lib);

	//keep one file handy so we can add a notice to the river if single image option selected
	if (!$file_for_river) {
		$file_for_river = $file;
	}

	array_push($uploaded_images, $file->guid);

	// plugins can register to be told when a new image has been uploaded
	trigger_elgg_event('upload', 'tp_image', $file);

	// successful upload so check if this is a new album and throw river event/notification if so
	if ($album->new_album == TP_NEW_ALBUM) {
		$album->new_album = TP_OLD_ALBUM;

		// we throw the notification manually here so users are not told about the new album until there
		// is at least a few photos in it
		object_notifications('create', 'object', $album);

		add_to_river('river/object/album/create', 'create', $album->owner_guid, $album->guid);
	}

	if ($img_river_view == "all") {
		add_to_river('river/object/image/create', 'create', $file->getObjectOwnerGUID(), $file->getGUID());
	}
	unset($file);  // may not be needed but there seems to be a memory leak

} //end of for loop

if (count($not_uploaded) > 0) {
	if (count($uploaded_images) > 0) {
		$error = sprintf(elgg_echo("tidypics:partialuploadfailure"), count($not_uploaded), count($not_uploaded) + count($uploaded_images))  . '<br />';
	} else {
		$error = elgg_echo("tidypics:completeuploadfailure") . '<br />';
	}

	$num_failures = count($not_uploaded);
	for ($i = 0; $i < $num_failures; $i++) {
		$error .= "{$not_uploaded[$i]}: {$error_msgs[$i]} <br />";
	}
	register_error($error);

	if (count($uploaded_images) == 0) {
		//upload failed, so forward to previous page
		forward($_SERVER['HTTP_REFERER']);
	} else {
		// some images did upload so we fall through
	}
} else {
	system_message(elgg_echo('tidypics:upl_success'));
}


if (count($uploaded_images) && $img_river_view == "1") {
	add_to_river('river/object/image/create', 'create', $file_for_river->getObjectOwnerGUID(), $file_for_river->getGUID());
}

if (count($uploaded_images) > 0) {
	$album->prependImageList($uploaded_images);
}

// plugins can register to be told when a Tidypics album has had images added
trigger_elgg_event('upload', 'tp_album', $album);


//forward to multi-image edit page
forward($CONFIG->wwwroot . 'mod/tidypics/pages/edit_multiple.php?files=' . implode('-', $uploaded_images));