]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Avatar.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / classes / Avatar.php
1 <?php
2 /**
3  * Table Definition for avatar
4  */
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6
7 class Avatar extends Managed_DataObject
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'avatar';                          // table name
13     public $profile_id;                      // int(4)  primary_key not_null
14     public $original;                        // tinyint(1)
15     public $width;                           // int(4)  primary_key not_null
16     public $height;                          // int(4)  primary_key not_null
17     public $mediatype;                       // varchar(32)   not_null
18     public $filename;                        // varchar(191)   not 255 because utf8mb4 takes more space
19     public $url;                             // varchar(191)  unique_key   not 255 because utf8mb4 takes more space
20     public $created;                         // datetime()   not_null
21     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
22
23     /* the code above is auto generated do not remove the tag below */
24     ###END_AUTOCODE
25         
26     public static function schemaDef()
27     {
28         return array(
29             'fields' => array(
30                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
31                 'original' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'uploaded by user or generated?'),
32                 'width' => array('type' => 'int', 'not null' => true, 'description' => 'image width'),
33                 'height' => array('type' => 'int', 'not null' => true, 'description' => 'image height'),
34                 'mediatype' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'file type'),
35                 'filename' => array('type' => 'varchar', 'length' => 191, 'description' => 'local filename, if local'),
36                 'url' => array('type' => 'text', 'description' => 'avatar location, not indexed - do not use in WHERE statement'),
37                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
38                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
39             ),
40             'primary key' => array('profile_id', 'width', 'height'),
41             'unique keys' => array(
42 //                'avatar_filename_key' => array('filename'),
43             ),
44             'foreign keys' => array(
45                 'avatar_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
46             ),
47             'indexes' => array(
48                 'avatar_profile_id_idx' => array('profile_id'),
49             ),
50         );
51     }
52
53     // We clean up the file, too
54     function delete($useWhere=false)
55     {
56         $filename = $this->filename;
57         if (file_exists(Avatar::path($filename))) {
58             @unlink(Avatar::path($filename));
59         }
60
61         return parent::delete($useWhere);
62     }
63
64     /*
65      * Deletes all avatars (but may spare the original) from a profile.
66      * 
67      * @param   Profile $target     The profile we're deleting avatars of.
68      * @param   boolean $original   Whether original should be removed or not.
69      */
70     public static function deleteFromProfile(Profile $target, $original=true) {
71         try {
72             $avatars = self::getProfileAvatars($target);
73             foreach ($avatars as $avatar) {
74                 if ($avatar->original && !$original) {
75                     continue;
76                 }
77                 $avatar->delete();
78             }
79         } catch (NoAvatarException $e) {
80             // There are no avatars to delete, a sort of success.
81         }
82
83         return true;
84     }
85
86     static protected $_avatars = array();
87
88     /*
89      * Get an avatar by profile. Currently can't call newSize with $height
90      */
91     public static function byProfile(Profile $target, $width=null, $height=null)
92     {
93         $width  = intval($width);
94         $height = !is_null($height) ? intval($height) : null;
95         if (is_null($height)) {
96             $height = $width;
97         }
98
99         $size = "{$width}x{$height}";
100         if (!isset(self::$_avatars[$target->id])) {
101             self::$_avatars[$target->id] = array();
102         } elseif (isset(self::$_avatars[$target->id][$size])){
103             return self::$_avatars[$target->id][$size];
104         }
105
106         $avatar = null;
107         if (Event::handle('StartProfileGetAvatar', array($target, $width, &$avatar))) {
108             $avatar = self::pkeyGet(
109                 array(
110                     'profile_id' => $target->id,
111                     'width'      => $width,
112                     'height'     => $height,
113                 )
114             );
115             Event::handle('EndProfileGetAvatar', array($target, $width, &$avatar));
116         }
117
118         if (is_null($avatar)) {
119             // Obviously we can't find an avatar, so let's resize the original!
120             $avatar = Avatar::newSize($target, $width);
121         } elseif (!($avatar instanceof Avatar)) {
122             throw new NoAvatarException($target, $avatar);
123         }
124
125         self::$_avatars[$target->id]["{$avatar->width}x{$avatar->height}"] = $avatar;
126         return $avatar;
127     }
128
129     public static function getUploaded(Profile $target)
130     {
131         $avatar = new Avatar();
132         $avatar->profile_id = $target->id;
133         $avatar->original = true;
134         if (!$avatar->find(true)) {
135             throw new NoAvatarException($target, $avatar);
136         }
137         if (!file_exists(Avatar::path($avatar->filename))) {
138             // The delete call may be odd for, say, unmounted filesystems
139             // that cause a file to currently not exist, but actually it does...
140             $avatar->delete();
141             throw new NoAvatarException($target, $avatar);
142         }
143         return $avatar;
144     }
145
146     public static function getProfileAvatars(Profile $target) {
147         $avatar = new Avatar();
148         $avatar->profile_id = $target->id;
149         if (!$avatar->find()) {
150             throw new NoAvatarException($target, $avatar);
151         }
152         return $avatar->fetchAll();
153     }
154
155     /**
156      * Where should the avatar go for this user?
157      */
158     static function filename($id, $extension, $size=null, $extra=null)
159     {
160         if ($size) {
161             return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
162         } else {
163             return $id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
164         }
165     }
166
167     static function path($filename)
168     {
169         $dir = common_config('avatar', 'dir');
170
171         if ($dir[strlen($dir)-1] != '/') {
172             $dir .= '/';
173         }
174
175         return $dir . $filename;
176     }
177
178     static function url($filename)
179     {
180         $path = common_config('avatar', 'path');
181
182         if ($path[strlen($path)-1] != '/') {
183             $path .= '/';
184         }
185
186         if ($path[0] != '/') {
187             $path = '/'.$path;
188         }
189
190         $server = common_config('avatar', 'server');
191
192         if (empty($server)) {
193             $server = common_config('site', 'server');
194         }
195
196         $ssl = common_config('avatar', 'ssl');
197
198         if (is_null($ssl)) { // null -> guess
199             if (common_config('site', 'ssl') == 'always' &&
200                 !common_config('avatar', 'server')) {
201                 $ssl = true;
202             } else {
203                 $ssl = false;
204             }
205         }
206
207         $protocol = ($ssl) ? 'https' : 'http';
208
209         return $protocol.'://'.$server.$path.$filename;
210     }
211
212     function displayUrl()
213     {
214         $server = common_config('avatar', 'server');
215         if ($server && !empty($this->filename)) {
216             return Avatar::url($this->filename);
217         } else {
218             return $this->url;
219         }
220     }
221
222     static function urlByProfile(Profile $target, $width=null, $height=null) {
223         try {
224             return self::byProfile($target,  $width, $height)->displayUrl();
225         } catch (Exception $e) {
226             return self::defaultImage($width);
227         }
228     }
229
230     static function defaultImage($size)
231     {
232         static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
233                                   AVATAR_STREAM_SIZE => 'stream',
234                                   AVATAR_MINI_SIZE => 'mini');
235         return Theme::path('default-avatar-'.$sizenames[$size].'.png');
236     }
237
238     static function newSize(Profile $target, $width) {
239         $width = intval($width);
240         if ($width < 1 || $width > common_config('avatar', 'maxsize')) {
241             // TRANS: An error message when avatar size is unreasonable
242             throw new Exception(_m('Avatar size too large'));
243         }
244         // So far we only have square avatars and I don't have time to
245         // rewrite support for non-square ones right now ;)
246         $height = $width;
247
248         $original = Avatar::getUploaded($target);
249
250         $imagefile = new ImageFile(null, Avatar::path($original->filename));
251         $filename = Avatar::filename($target->getID(), image_type_to_extension($imagefile->preferredType()),
252                                      $width, common_timestamp());
253         $imagefile->resizeTo(Avatar::path($filename), array('width'=>$width, 'height'=>$height));
254
255         $scaled = clone($original);
256         $scaled->original = false;
257         $scaled->width = $width;
258         $scaled->height = $height;
259         $scaled->url = Avatar::url($filename);
260         $scaled->filename = $filename;
261         $scaled->created = common_sql_now();
262
263         if (!$scaled->insert()) {
264             // TRANS: An error message when unable to insert avatar data into the db
265             throw new Exception(_m('Could not insert new avatar data to database'));
266         }
267
268         // Return the new avatar object
269         return $scaled;
270     }
271 }