]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Oauth_application.php
Fix for ticket #2828, part II: apostrophe in site name set in installer created a...
[quix0rs-gnu-social.git] / classes / Oauth_application.php
1 <?php
2 /**
3  * Table Definition for oauth_application
4  */
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6
7 class Oauth_application extends Memcached_DataObject
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'oauth_application';               // table name
13     public $id;                              // int(4)  primary_key not_null
14     public $owner;                           // int(4)   not_null
15     public $consumer_key;                    // varchar(255)   not_null
16     public $name;                            // varchar(255)   not_null
17     public $description;                     // varchar(255)
18     public $icon;                            // varchar(255)   not_null
19     public $source_url;                      // varchar(255)
20     public $organization;                    // varchar(255)
21     public $homepage;                        // varchar(255)
22     public $callback_url;                    // varchar(255)   not_null
23     public $type;                            // tinyint(1)
24     public $access_type;                     // tinyint(1)
25     public $created;                         // datetime   not_null
26     public $modified;                        // timestamp   not_null default_CURRENT_TIMESTAMP
27
28     /* Static get */
29     function staticGet($k,$v=NULL) {
30     return Memcached_DataObject::staticGet('Oauth_application',$k,$v);
31     }
32     /* the code above is auto generated do not remove the tag below */
33     ###END_AUTOCODE
34
35     // Bit flags
36     public static $readAccess  = 1;
37     public static $writeAccess = 2;
38
39     public static $browser = 1;
40     public static $desktop = 2;
41
42     function getConsumer()
43     {
44         return Consumer::staticGet('consumer_key', $this->consumer_key);
45     }
46
47     static function maxDesc()
48     {
49         $desclimit = common_config('application', 'desclimit');
50         // null => use global limit (distinct from 0!)
51         if (is_null($desclimit)) {
52             $desclimit = common_config('site', 'textlimit');
53         }
54         return $desclimit;
55     }
56
57     static function descriptionTooLong($desc)
58     {
59         $desclimit = self::maxDesc();
60         return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
61     }
62
63     function setAccessFlags($read, $write)
64     {
65         if ($read) {
66             $this->access_type |= self::$readAccess;
67         } else {
68             $this->access_type &= ~self::$readAccess;
69         }
70
71         if ($write) {
72             $this->access_type |= self::$writeAccess;
73         } else {
74             $this->access_type &= ~self::$writeAccess;
75         }
76     }
77
78     function setOriginal($filename)
79     {
80         $imagefile = new ImageFile($this->id, Avatar::path($filename));
81
82         // XXX: Do we want to have a bunch of different size icons? homepage, stream, mini?
83         // or just one and control size via CSS? --Zach
84
85         $orig = clone($this);
86         $this->icon = Avatar::url($filename);
87         common_debug(common_log_objstring($this));
88         return $this->update($orig);
89     }
90
91     static function getByConsumerKey($key)
92     {
93         if (empty($key)) {
94             return null;
95         }
96
97         $app = new Oauth_application();
98         $app->consumer_key = $key;
99         $app->limit(1);
100         $result = $app->find(true);
101
102         return empty($result) ? null : $app;
103     }
104
105     /**
106      * Handle an image upload
107      *
108      * Does all the magic for handling an image upload, and crops the
109      * image by default.
110      *
111      * @return void
112      */
113     function uploadLogo()
114     {
115         if ($_FILES['app_icon']['error'] ==
116             UPLOAD_ERR_OK) {
117
118             try {
119                 $imagefile = ImageFile::fromUpload('app_icon');
120             } catch (Exception $e) {
121                 common_debug("damn that sucks");
122                 $this->showForm($e->getMessage());
123                 return;
124             }
125
126             $filename = Avatar::filename($this->id,
127                                          image_type_to_extension($imagefile->type),
128                                          null,
129                                          'oauth-app-icon-'.common_timestamp());
130
131             $filepath = Avatar::path($filename);
132
133             move_uploaded_file($imagefile->filepath, $filepath);
134
135             $this->setOriginal($filename);
136         }
137     }
138
139     function delete()
140     {
141         $this->_deleteAppUsers();
142
143         $consumer = $this->getConsumer();
144         $consumer->delete();
145
146         parent::delete();
147     }
148
149     function _deleteAppUsers()
150     {
151         $oauser = new Oauth_application_user();
152         $oauser->application_id = $this->id;
153         $oauser->delete();
154     }
155 }