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