]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Profile.php
Don't allow user to send a new message or nudge right after subscribing to another...
[quix0rs-gnu-social.git] / classes / Profile.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 /**
23  * Table Definition for profile
24  */
25 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
26
27 class Profile extends Memcached_DataObject 
28 {
29     ###START_AUTOCODE
30     /* the code below is auto generated do not remove the above tag */
31
32     public $__table = 'profile';                         // table name
33     public $id;                              // int(4)  primary_key not_null
34     public $nickname;                        // varchar(64)  multiple_key not_null
35     public $fullname;                        // varchar(255)  multiple_key
36     public $profileurl;                      // varchar(255)  
37     public $homepage;                        // varchar(255)  multiple_key
38     public $bio;                             // varchar(140)  multiple_key
39     public $location;                        // varchar(255)  multiple_key
40     public $created;                         // datetime()   not_null
41     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
42
43     /* Static get */
44     function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Profile',$k,$v); }
45
46     /* the code above is auto generated do not remove the tag below */
47     ###END_AUTOCODE
48
49     function getSearchEngine() {
50         require_once INSTALLDIR.'/classes/SearchEngines.php';
51         static $search_engine;
52         if (!isset($search_engine)) {
53                 if (common_config('sphinx', 'enabled')) {
54                     $search_engine = new SphinxSearch($this);
55                 } elseif ('mysql' === common_config('db', 'type')) {
56                     $search_engine = new MySQLSearch($this);
57                 } else {
58                     $search_engine = new PGSearch($this);
59                 }
60         }
61         return $search_engine;
62     }
63
64         function getAvatar($width, $height=NULL) {
65                 if (is_null($height)) {
66                         $height = $width;
67                 }
68                 return Avatar::pkeyGet(array('profile_id' => $this->id,
69                                                                          'width' => $width,
70                                                                          'height' => $height));
71         }
72
73         function getOriginalAvatar() {
74                 $avatar = DB_DataObject::factory('avatar');
75                 $avatar->profile_id = $this->id;
76                 $avatar->original = true;
77                 if ($avatar->find(true)) {
78                         return $avatar;
79                 } else {
80                         return NULL;
81                 }
82         }
83
84         function setOriginal($source) {
85
86                 $info = @getimagesize($source);
87
88                 if (!$info) {
89                         return NULL;
90                 }
91
92                 $filename = common_avatar_filename($this->id,
93                                                                                    image_type_to_extension($info[2]),
94                                                                                    NULL, common_timestamp());
95                 $filepath = common_avatar_path($filename);
96
97                 copy($source, $filepath);
98
99                 $avatar = new Avatar();
100
101                 $avatar->profile_id = $this->id;
102                 $avatar->width = $info[0];
103                 $avatar->height = $info[1];
104                 $avatar->mediatype = image_type_to_mime_type($info[2]);
105                 $avatar->filename = $filename;
106                 $avatar->original = true;
107                 $avatar->url = common_avatar_url($filename);
108                 $avatar->created = DB_DataObject_Cast::dateTime(); # current time
109
110                 # XXX: start a transaction here
111
112                 if (!$this->delete_avatars()) {
113                         @unlink($filepath);
114                         return NULL;
115                 }
116
117                 if (!$avatar->insert()) {
118                         @unlink($filepath);
119                         return NULL;
120                 }
121
122                 foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
123                         # We don't do a scaled one if original is our scaled size
124                         if (!($avatar->width == $size && $avatar->height == $size)) {
125                                 $s = $avatar->scale($size);
126                                 if (!$s) {
127                                         return NULL;
128                                 }
129                         }
130                 }
131
132                 return $avatar;
133         }
134
135         function delete_avatars() {
136                 $avatar = new Avatar();
137                 $avatar->profile_id = $this->id;
138                 $avatar->find();
139                 while ($avatar->fetch()) {
140                         $avatar->delete();
141                 }
142                 return true;
143         }
144
145         function getBestName() {
146                 return ($this->fullname) ? $this->fullname : $this->nickname;
147         }
148
149     # Get latest notice on or before date; default now
150         function getCurrentNotice($dt=NULL) {
151                 $notice = new Notice();
152                 $notice->profile_id = $this->id;
153                 if ($dt) {
154                         $notice->whereAdd('created < "' . $dt . '"');
155                 }
156                 $notice->orderBy('created DESC, notice.id DESC');
157                 $notice->limit(1);
158                 if ($notice->find(true)) {
159                         return $notice;
160                 }
161                 return NULL;
162         }
163 }