aboutsummaryrefslogtreecommitdiffstats
path: root/mod/wall_upload.php
blob: a3dcf9a0bba4135e0792849b480316738db571e5 (plain) (blame)
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
<?php

require_once('Photo.php');




function wall_upload_post(&$a) {

	$using_api = ((x($_FILES,'media')) ? true : false); 

	if($using_api) {
		require_once('include/api.php');
		$user_info = api_get_user($a);
		$nick = $user_info['screen_name'];
	}
	else {
		if(argc() > 1)
			$nick = argv(1);
	}

	$channel = null;

	if($nick) {		
		$r = q("SELECT channel.* from channel where channel_address = '%s' limit 1",
			dbesc($nick)
		);
		if($r)
			$channel = $r[0];
	}

	if(! $channel) {
		if($using_api)
			return;
		else {
			notice( t('Channel not found.') . EOL);
			killme();
		}
	}

	$can_post  = false;
	$visitor   = 0;


	$page_owner_uid   = $r[0]['channel_id'];

	$observer = $a->get_observer();

	if(! perm_is_allowed($page_owner_uid,$observer['xchan_hash'],'post_photos')) {
		if($using_api)
			return;
		else {
			notice( t('Permission denied.') . EOL);
			killme();
		}
	}

	if(x($_FILES,'userfile')) {
		$src      = $_FILES['userfile']['tmp_name'];
		$filename = basename($_FILES['userfile']['name']);
		$filesize = intval($_FILES['userfile']['size']);
		$filetype = $_FILES['userfile']['type'];
	}
	elseif(x($_FILES,'media')) {
		$src = $_FILES['media']['tmp_name'];
		$filename = basename($_FILES['media']['name']);
		$filesize = intval($_FILES['media']['size']);
		$filetype = $_FILES['media']['type'];
	}
	else {
		if($using_api)
			return;
		else {
			notice( t('Empty upload.') . EOL);
			killme();
		}
	}

	
    if($filetype == "") 
		$filetype=guess_image_type($filename);
	$maximagesize = get_config('system','maximagesize');

	if(($maximagesize) && ($filesize > $maximagesize)) {
		@unlink($src);
		if($using_api)
			return;
		else {
			echo  sprintf( t('Image exceeds size limit of %d'), $maximagesize) . EOL;
			killme();
		}
	}


	$limit = service_class_fetch($page_owner_uid,'photo_upload_limit');
	if($limit !== false) {
		$r = q("select sum(size) as total from photo where uid = %d and scale = 0 ",
			intval($page_owner_uid)
		);
		if(($r) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
			@unlink($src);
			if($using_api)
				return;
			else {
				echo upgrade_message(true) . EOL ;
				killme();
			}
		}
	}

	$imagedata = @file_get_contents($src);
	$ph = new Photo($imagedata, $filetype);

	if(! $ph->is_valid()) {
		@unlink($src);
		if($using_api)
			return;
		else {
			echo ( t('Unable to process image.') . EOL);
			killme();
		}
	}

	$ph->orient($src);
	@unlink($src);

	$max_length = get_config('system','max_image_length');
	if(! $max_length)
		$max_length = MAX_IMAGE_LENGTH;
	if($max_length > 0)
		$ph->scaleImage($max_length);

	$width = $ph->getWidth();
	$height = $ph->getHeight();

	$hash = photo_new_resource();
	
	$smallest = 0;

	$defperm = '<' . $channel['channel_hash'] . '>';
	$aid = $channel['channel_account_id'];
	$visitor = ((remote_user()) ? remote_user() : '');

	$r = $ph->store($aid, $page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 0, 0, $defperm);

	if(! $r) {
		if($using_api)
			return;
		else {
			echo ( t('Image upload failed.') . EOL);
			killme();
		}
	}

	if($width > 640 || $height > 640) {
		$ph->scaleImage(640);
		$r = $ph->store($aid, $page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 1, 0, $defperm);
		if($r) 
			$smallest = 1;
	}

	if($width > 320 || $height > 320) {
		$ph->scaleImage(320);
		$r = $ph->store($aid, $page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 2, 0, $defperm);
		if($r)
			$smallest = 2;
	}

	$basename = basename($filename);

	if($_REQUEST['silent']) {
		$m = '[url=' . $a->get_baseurl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . $a->get_baseurl() . "/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]";
		return($m);
	}
	else {
		echo  "\n\n" . '[url=' . $a->get_baseurl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . $a->get_baseurl() . "/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]\n\n";
	}

	killme();
	// NOTREACHED
}