]> git.mxchange.org Git - friendica.git/blob - mod/wall_upload.php
Merge remote branch 'upstream/master'
[friendica.git] / mod / wall_upload.php
1 <?php
2
3 require_once('Photo.php');
4
5 function wall_upload_post(&$a) {
6
7         if($a->argc > 1) {
8                 if(! x($_FILES,'media')) {
9                         $nick = $a->argv[1];
10                         $r = q("SELECT `user`.*, `contact`.`id` FROM `user` LEFT JOIN `contact` on `user`.`uid` = `contact`.`uid`  WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0 and `contact`.`self` = 1 LIMIT 1",
11                                 dbesc($nick)
12                         );
13
14                         if(! count($r))
15                                 return;
16                 }
17                 else {
18                         $user_info = api_get_user($a);
19                         $r = q("SELECT `user`.*, `contact`.`id` FROM `user` LEFT JOIN `contact` on `user`.`uid` = `contact`.`uid`  WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0 and `contact`.`self` = 1 LIMIT 1",
20                                 dbesc($user_info['screen_name'])
21                         );
22                 }
23         }
24         else
25                 return;
26
27
28         $can_post  = false;
29         $visitor   = 0;
30
31         $page_owner_uid   = $r[0]['uid'];
32         $default_cid      = $r[0]['id'];
33         $page_owner_nick  = $r[0]['nickname'];
34         $community_page   = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
35
36         if((local_user()) && (local_user() == $page_owner_uid))
37                 $can_post = true;
38         else {
39                 if($community_page && remote_user()) {
40                         $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
41                                 intval(remote_user()),
42                                 intval($page_owner_uid)
43                         );
44                         if(count($r)) {
45                                 $can_post = true;
46                                 $visitor = remote_user();
47                                 $default_cid = $visitor;
48                         }
49                 }
50         }
51
52         if(! $can_post) {
53                 notice( t('Permission denied.') . EOL );
54                 killme();
55         }
56
57         if(! x($_FILES,'userfile') && ! x($_FILES,'media'))
58                 killme();
59
60         if(x($_FILES,'userfile')) {
61                 $src      = $_FILES['userfile']['tmp_name'];
62                 $filename = basename($_FILES['userfile']['name']);
63                 $filesize = intval($_FILES['userfile']['size']);
64                 $filetype = $_FILES['userfile']['type'];
65         }
66         elseif(x($_FILES,'media')) {
67                 $src = $_FILES['media']['tmp_name'];
68                 $filename = basename($_FILES['media']['name']);
69                 $filesize = intval($_FILES['media']['size']);
70                 $filetype = $_FILES['media']['type'];
71         }
72         
73     if ($filetype=="") $filetype=guess_image_type($filename);
74         $maximagesize = get_config('system','maximagesize');
75
76         if(($maximagesize) && ($filesize > $maximagesize)) {
77                 echo  sprintf( t('Image exceeds size limit of %d'), $maximagesize) . EOL;
78                 @unlink($src);
79                 killme();
80         }
81
82         $r = q("select sum(octet_length(data)) as total from photo where uid = %d and scale = 0 and album != 'Contact Photos' ",
83                 intval($page_owner_uid)
84         );
85
86         $limit = service_class_fetch($page_owner_uid,'photo_upload_limit');
87
88         if(($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
89                 echo upgrade_message(true) . EOL ;
90                 @unlink($src);
91                 killme();
92         }
93
94
95         $imagedata = @file_get_contents($src);
96         $ph = new Photo($imagedata, $filetype);
97
98         if(! $ph->is_valid()) {
99                 echo ( t('Unable to process image.') . EOL);
100                 @unlink($src);
101                 killme();
102         }
103
104         @unlink($src);
105
106         $width = $ph->getWidth();
107         $height = $ph->getHeight();
108
109         $hash = photo_new_resource();
110         
111         $smallest = 0;
112
113         $defperm = '<' . $default_cid . '>';
114
115         $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 0, 0, $defperm);
116
117         if(! $r) {
118                 echo ( t('Image upload failed.') . EOL);
119                 killme();
120         }
121
122         if($width > 640 || $height > 640) {
123                 $ph->scaleImage(640);
124                 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 1, 0, $defperm);
125                 if($r) 
126                         $smallest = 1;
127         }
128
129         if($width > 320 || $height > 320) {
130                 $ph->scaleImage(320);
131                 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 2, 0, $defperm);
132                 if($r)
133                         $smallest = 2;
134         }
135
136         $basename = basename($filename);
137
138
139 /* mod Waitman Gobble NO WARRANTY */
140
141 //if we get the signal then return the image url info in BBCODE, otherwise this outputs the info and bails (for the ajax image uploader on wall post)
142         if ($_REQUEST['hush']!='yeah') {
143
144                 /*existing code*/
145                 if(local_user() && intval(get_pconfig(local_user(),'system','plaintext')))
146                         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";
147                 else
148                         echo  '<br /><br /><a href="' . $a->get_baseurl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '" ><img src="' . $a->get_baseurl() . "/photo/{$hash}-{$smallest}.".$ph->getExt()."\" alt=\"$basename\" /></a><br /><br />";
149                 /*existing code*/
150                 
151         } else {
152                 $m = '[url=' . $a->get_baseurl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . $a->get_baseurl() . "/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]";
153                 return($m);
154         }
155 /* mod Waitman Gobble NO WARRANTY */
156
157         killme();
158         // NOTREACHED
159 }