Oauth_application.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Table Definition for oauth_application
  18. */
  19. defined('GNUSOCIAL') || die();
  20. class Oauth_application extends Managed_DataObject
  21. {
  22. ###START_AUTOCODE
  23. /* the code below is auto generated do not remove the above tag */
  24. public $__table = 'oauth_application'; // table name
  25. public $id; // int(4) primary_key not_null
  26. public $owner; // int(4) not_null
  27. public $consumer_key; // varchar(191) not_null not 255 because utf8mb4 takes more space
  28. public $name; // varchar(191) not_null not 255 because utf8mb4 takes more space
  29. public $description; // varchar(191) not 255 because utf8mb4 takes more space
  30. public $icon; // varchar(191) not_null not 255 because utf8mb4 takes more space
  31. public $source_url; // varchar(191) not 255 because utf8mb4 takes more space
  32. public $organization; // varchar(191) not 255 because utf8mb4 takes more space
  33. public $homepage; // varchar(191) not 255 because utf8mb4 takes more space
  34. public $callback_url; // varchar(191) not_null not 255 because utf8mb4 takes more space
  35. public $type; // tinyint(1)
  36. public $access_type; // tinyint(1)
  37. public $created; // datetime()
  38. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  39. /* the code above is auto generated do not remove the tag below */
  40. ###END_AUTOCODE
  41. // Bit flags
  42. public static $readAccess = 1;
  43. public static $writeAccess = 2;
  44. public static $browser = 1;
  45. public static $desktop = 2;
  46. public function getConsumer()
  47. {
  48. return Consumer::getKV('consumer_key', $this->consumer_key);
  49. }
  50. public static function maxDesc()
  51. {
  52. // This used to default to textlimit or allow unlimited descriptions,
  53. // but this isn't part of a notice and the field's limited to 191 chars
  54. // in the DB, so those seem silly. (utf8mb4 takes up more space, so can't use 255)
  55. //
  56. // Now just defaulting to 191 max unless a smaller application desclimit
  57. // is actually set. Setting to 0 will use the maximum.
  58. $max = 191;
  59. $desclimit = intval(common_config('application', 'desclimit'));
  60. if ($desclimit > 0 && $desclimit < $max) {
  61. return $desclimit;
  62. } else {
  63. return $max;
  64. }
  65. }
  66. public static function descriptionTooLong($desc)
  67. {
  68. $desclimit = self::maxDesc();
  69. return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
  70. }
  71. public function setAccessFlags($read, $write)
  72. {
  73. if ($read) {
  74. $this->access_type |= self::$readAccess;
  75. } else {
  76. $this->access_type &= ~self::$readAccess;
  77. }
  78. if ($write) {
  79. $this->access_type |= self::$writeAccess;
  80. } else {
  81. $this->access_type &= ~self::$writeAccess;
  82. }
  83. }
  84. public function setOriginal($filename)
  85. {
  86. $imagefile = new ImageFile(null, Avatar::path($filename));
  87. // XXX: Do we want to have a bunch of different size icons? homepage, stream, mini?
  88. // or just one and control size via CSS? --Zach
  89. $orig = clone($this);
  90. $this->icon = Avatar::url($filename);
  91. common_debug(common_log_objstring($this));
  92. return $this->update($orig);
  93. }
  94. public static function getByConsumerKey($key)
  95. {
  96. if (empty($key)) {
  97. return null;
  98. }
  99. $app = new Oauth_application();
  100. $app->consumer_key = $key;
  101. $app->limit(1);
  102. $result = $app->find(true);
  103. return empty($result) ? null : $app;
  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. public function uploadLogo()
  114. {
  115. if ($_FILES['app_icon']['error'] == UPLOAD_ERR_OK) {
  116. try {
  117. $imagefile = ImageFile::fromUpload('app_icon');
  118. } catch (Exception $e) {
  119. common_debug("damn that sucks");
  120. $this->showForm($e->getMessage());
  121. return;
  122. }
  123. $filename = Avatar::filename(
  124. $this->id,
  125. image_type_to_extension($imagefile->type),
  126. null,
  127. 'oauth-app-icon-' . common_timestamp()
  128. );
  129. $filepath = Avatar::path($filename);
  130. move_uploaded_file($imagefile->filepath, $filepath);
  131. $this->setOriginal($filename);
  132. }
  133. }
  134. public function delete($useWhere = false)
  135. {
  136. $this->deleteAppUsers();
  137. $consumer = $this->getConsumer();
  138. $consumer->delete();
  139. return parent::delete($useWhere);
  140. }
  141. private function deleteAppUsers()
  142. {
  143. $oauser = new Oauth_application_user();
  144. $oauser->application_id = $this->id;
  145. $oauser->delete();
  146. }
  147. public static function schemaDef()
  148. {
  149. return array(
  150. 'description' => 'OAuth application registration record',
  151. 'fields' => array(
  152. 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
  153. 'owner' => array('type' => 'int', 'not null' => true, 'description' => 'owner of the application'),
  154. 'consumer_key' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'application consumer key'),
  155. 'name' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'name of the application'),
  156. 'description' => array('type' => 'varchar', 'length' => 191, 'description' => 'description of the application'),
  157. 'icon' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'default' => '/theme/base/default-avatar-stream.png', 'description' => 'application icon'),
  158. 'source_url' => array('type' => 'varchar', 'length' => 191, 'description' => 'application homepage - used for source link'),
  159. 'organization' => array('type' => 'varchar', 'length' => 191, 'description' => 'name of the organization running the application'),
  160. 'homepage' => array('type' => 'varchar', 'length' => 191, 'description' => 'homepage for the organization'),
  161. 'callback_url' => array('type' => 'varchar', 'length' => 191, 'description' => 'url to redirect to after authentication'),
  162. 'type' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'type of app, 1 = browser, 2 = desktop'),
  163. 'access_type' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'default access type, bit 1 = read, bit 2 = write'),
  164. 'created' => array('type' => 'datetime', 'description' => 'date this record was created'),
  165. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  166. ),
  167. 'primary key' => array('id'),
  168. 'unique keys' => array(
  169. 'oauth_application_name_key' => array('name'), // in the long run, we should perhaps not force these unique, and use another source id
  170. ),
  171. 'foreign keys' => array(
  172. 'oauth_application_owner_fkey' => array('profile', array('owner' => 'id')), // Are remote users allowed to create oauth application records?
  173. 'oauth_application_consumer_key_fkey' => array('consumer', array('consumer_key' => 'consumer_key')),
  174. ),
  175. 'indexes' => array(
  176. 'oauth_application_owner_idx' => array('owner'),
  177. 'oauth_application_consumer_key_idx' => array('consumer_key'),
  178. ),
  179. );
  180. }
  181. }