Oauth_application.php 8.0 KB

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