]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Avatar.php
I missed a preg_replace with /e
[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(255)
19     public $url;                             // varchar(255)  unique_key
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' => 255, 'description' => 'local filename, if local'),
36                 'url' => array('type' => 'varchar', 'length' => 255, 'description' => 'avatar location'),
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_url_key' => array('url'),
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()
55     {
56         $filename = $this->filename;
57         if (parent::delete() && file_exists(Avatar::path($filename))) {
58             @unlink(Avatar::path($filename));
59         }
60     }
61
62     /*
63      * Deletes all avatars (but may spare the original) from a profile.
64      * 
65      * @param   Profile $target     The profile we're deleting avatars of.
66      * @param   boolean $original   Whether original should be removed or not.
67      */
68     public static function deleteFromProfile(Profile $target, $original=true) {
69         try {
70             $avatars = self::getProfileAvatars($target);
71             foreach ($avatars as $avatar) {
72                 if ($avatar->original && !$original) {
73                     continue;
74                 }
75                 $avatar->delete();
76             }
77         } catch (NoResultException $e) {
78             // There are no avatars to delete, a sort of success.
79         }
80
81         return true;
82     }
83
84     public static function getUploaded(Profile $target)
85     {
86         $avatar = new Avatar();
87         $avatar->profile_id = $target->id;
88         $avatar->original = true;
89         if (!$avatar->find(true)) {
90             throw new NoResultException($avatar);
91         }
92         if (!file_exists(Avatar::path($avatar->filename))) {
93             // The delete call may be odd for, say, unmounted filesystems
94             // that cause a file to currently not exist, but actually it does...
95             $avatar->delete();
96             throw new FileNotFoundException(Avatar::path($avatar->filename));
97         }
98         return $avatar;
99     }
100
101     public static function getProfileAvatars(Profile $target) {
102         $avatar = new Avatar();
103         $avatar->profile_id = $target->id;
104         if (!$avatar->find()) {
105             throw new NoResultException($avatar);
106         }
107         return $avatar->fetchAll();
108     }
109
110     /**
111      * Where should the avatar go for this user?
112      */
113     static function filename($id, $extension, $size=null, $extra=null)
114     {
115         if ($size) {
116             return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
117         } else {
118             return $id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
119         }
120     }
121
122     static function path($filename)
123     {
124         $dir = common_config('avatar', 'dir');
125
126         if ($dir[strlen($dir)-1] != '/') {
127             $dir .= '/';
128         }
129
130         return $dir . $filename;
131     }
132
133     static function url($filename)
134     {
135         $path = common_config('avatar', 'path');
136
137         if ($path[strlen($path)-1] != '/') {
138             $path .= '/';
139         }
140
141         if ($path[0] != '/') {
142             $path = '/'.$path;
143         }
144
145         $server = common_config('avatar', 'server');
146
147         if (empty($server)) {
148             $server = common_config('site', 'server');
149         }
150
151         $ssl = common_config('avatar', 'ssl');
152
153         if (is_null($ssl)) { // null -> guess
154             if (common_config('site', 'ssl') == 'always' &&
155                 !common_config('avatar', 'server')) {
156                 $ssl = true;
157             } else {
158                 $ssl = false;
159             }
160         }
161
162         $protocol = ($ssl) ? 'https' : 'http';
163
164         return $protocol.'://'.$server.$path.$filename;
165     }
166
167     function displayUrl()
168     {
169         $server = common_config('avatar', 'server');
170         if ($server && !empty($this->filename)) {
171             return Avatar::url($this->filename);
172         } else {
173             return $this->url;
174         }
175     }
176
177     static function defaultImage($size)
178     {
179         static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
180                                   AVATAR_STREAM_SIZE => 'stream',
181                                   AVATAR_MINI_SIZE => 'mini');
182         return Theme::path('default-avatar-'.$sizenames[$size].'.png');
183     }
184
185     static function newSize(Profile $target, $size) {
186         $size = floor($size);
187         if ($size < 1 || $size > common_config('avatar', 'maxsize')) {
188             // TRANS: An error message when avatar size is unreasonable
189             throw new Exception(_m('Avatar size too large'));
190         }
191
192         $original = Avatar::getUploaded($target);
193
194         $imagefile = new ImageFile($target->id, Avatar::path($original->filename));
195         $filename = $imagefile->resize($size);
196
197         $scaled = clone($original);
198         $scaled->original = false;
199         $scaled->width = $size;
200         $scaled->height = $size;
201         $scaled->url = Avatar::url($filename);
202         $scaled->filename = $filename;
203         $scaled->created = common_sql_now();
204
205         if (!$scaled->insert()) {
206             // TRANS: An error message when unable to insert avatar data into the db
207             throw new Exception(_m('Could not insert new avatar data to database'));
208         }
209
210         // Return the new avatar object
211         return $scaled;
212     }
213 }