]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Oauth_application.php
Always naming it 'plugin' is not good, it can easily confuse. So better name it
[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 Managed_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(191)   not_null   not 255 because utf8mb4 takes more space
16     public $name;                            // varchar(191)   not_null   not 255 because utf8mb4 takes more space
17     public $description;                     // varchar(191)              not 255 because utf8mb4 takes more space
18     public $icon;                            // varchar(191)              not_null   not 255 because utf8mb4 takes more space
19     public $source_url;                      // varchar(191)              not 255 because utf8mb4 takes more space
20     public $organization;                    // varchar(191)              not 255 because utf8mb4 takes more space
21     public $homepage;                        // varchar(191)              not 255 because utf8mb4 takes more space
22     public $callback_url;                    // varchar(191)   not_null   not 255 because utf8mb4 takes more space
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     /* the code above is auto generated do not remove the tag below */
29     ###END_AUTOCODE
30
31     // Bit flags
32     public static $readAccess  = 1;
33     public static $writeAccess = 2;
34
35     public static $browser = 1;
36     public static $desktop = 2;
37
38     function getConsumer()
39     {
40         return Consumer::getKV('consumer_key', $this->consumer_key);
41     }
42
43     static function maxDesc()
44     {
45         // This used to default to textlimit or allow unlimited descriptions,
46         // but this isn't part of a notice and the field's limited to 191 chars
47         // in the DB, so those seem silly. (utf8mb4 takes up more space, so can't use 255)
48         //
49         // Now just defaulting to 191 max unless a smaller application desclimit
50         // is actually set. Setting to 0 will use the maximum.
51         $max = 191;
52         $desclimit = intval(common_config('application', 'desclimit'));
53         if ($desclimit > 0 && $desclimit < $max) {
54             return $desclimit;
55         } else {
56             return $max;
57         }
58     }
59
60     static function descriptionTooLong($desc)
61     {
62         $desclimit = self::maxDesc();
63         return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
64     }
65
66     function setAccessFlags($read, $write)
67     {
68         if ($read) {
69             $this->access_type |= self::$readAccess;
70         } else {
71             $this->access_type &= ~self::$readAccess;
72         }
73
74         if ($write) {
75             $this->access_type |= self::$writeAccess;
76         } else {
77             $this->access_type &= ~self::$writeAccess;
78         }
79     }
80
81     function setOriginal($filename)
82     {
83         $imagefile = new ImageFile(null, Avatar::path($filename));
84
85         // XXX: Do we want to have a bunch of different size icons? homepage, stream, mini?
86         // or just one and control size via CSS? --Zach
87
88         $orig = clone($this);
89         $this->icon = Avatar::url($filename);
90         common_debug(common_log_objstring($this));
91         return $this->update($orig);
92     }
93
94     static function getByConsumerKey($key)
95     {
96         if (empty($key)) {
97             return null;
98         }
99
100         $app = new Oauth_application();
101         $app->consumer_key = $key;
102         $app->limit(1);
103         $result = $app->find(true);
104
105         return empty($result) ? null : $app;
106     }
107
108     /**
109      * Handle an image upload
110      *
111      * Does all the magic for handling an image upload, and crops the
112      * image by default.
113      *
114      * @return void
115      */
116     function uploadLogo()
117     {
118         if ($_FILES['app_icon']['error'] ==
119             UPLOAD_ERR_OK) {
120
121             try {
122                 $imagefile = ImageFile::fromUpload('app_icon');
123             } catch (Exception $e) {
124                 common_debug("damn that sucks");
125                 $this->showForm($e->getMessage());
126                 return;
127             }
128
129             $filename = Avatar::filename($this->id,
130                                          image_type_to_extension($imagefile->type),
131                                          null,
132                                          'oauth-app-icon-'.common_timestamp());
133
134             $filepath = Avatar::path($filename);
135
136             move_uploaded_file($imagefile->filepath, $filepath);
137
138             $this->setOriginal($filename);
139         }
140     }
141
142     function delete($useWhere=false)
143     {
144         $this->_deleteAppUsers();
145
146         $consumer = $this->getConsumer();
147         $consumer->delete();
148
149         return parent::delete($useWhere);
150     }
151
152     function _deleteAppUsers()
153     {
154         $oauser = new Oauth_application_user();
155         $oauser->application_id = $this->id;
156         $oauser->delete();
157     }
158
159     public static function schemaDef()
160     {
161         return array(
162             'description' => 'OAuth application registration record',
163             'fields' => array(
164                 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
165                 'owner' => array('type' => 'int', 'not null' => true, 'description' => 'owner of the application'),
166                 'consumer_key' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'application consumer key'),
167                 'name' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'name of the application'),
168                 'description' => array('type' => 'varchar', 'length' => 191, 'description' => 'description of the application'),
169                 'icon' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'application icon'),
170                 'source_url' => array('type' => 'varchar', 'length' => 191, 'description' => 'application homepage - used for source link'),
171                 'organization' => array('type' => 'varchar', 'length' => 191, 'description' => 'name of the organization running the application'),
172                 'homepage' => array('type' => 'varchar', 'length' => 191, 'description' => 'homepage for the organization'),
173                 'callback_url' => array('type' => 'varchar', 'length' => 191, 'description' => 'url to redirect to after authentication'),
174                 'type' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'type of app, 1 = browser, 2 = desktop'),
175                 'access_type' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'default access type, bit 1 = read, bit 2 = write'),
176                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
177                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
178             ),
179             'primary key' => array('id'),
180             'unique keys' => array(
181                 'oauth_application_name_key' => array('name'), // in the long run, we should perhaps not force these unique, and use another source id
182             ),
183             'foreign keys' => array(
184                 'oauth_application_owner_fkey' => array('profile', array('owner' => 'id')), // Are remote users allowed to create oauth application records?
185                 'oauth_application_consumer_key_fkey' => array('consumer', array('consumer_key' => 'consumer_key')),
186             ),
187         );
188     }
189 }