User_urlshortener_prefs.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. * @copyright 2010 StatusNet, Inc.
  18. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  19. */
  20. defined('GNUSOCIAL') || die();
  21. class User_urlshortener_prefs extends Managed_DataObject
  22. {
  23. ###START_AUTOCODE
  24. /* the code below is auto generated do not remove the above tag */
  25. public $__table = 'user_urlshortener_prefs'; // table name
  26. public $user_id; // int(4) primary_key not_null
  27. public $urlshorteningservice; // varchar(50) default_ur1.ca
  28. public $maxurllength; // int(4) not_null
  29. public $maxnoticelength; // int(4) not_null
  30. public $created; // datetime()
  31. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  32. /* the code above is auto generated do not remove the tag below */
  33. ###END_AUTOCODE
  34. public static function schemaDef()
  35. {
  36. return array(
  37. 'fields' => array(
  38. 'user_id' => array('type' => 'int', 'not null' => true, 'description' => 'user'),
  39. 'urlshorteningservice' => array('type' => 'varchar', 'length' => 50, 'default' => 'internal', 'description' => 'service to use for auto-shortening URLs'),
  40. 'maxurllength' => array('type' => 'int', 'not null' => true, 'description' => 'urls greater than this length will be shortened, 0 = always, null = never'),
  41. 'maxnoticelength' => array('type' => 'int', 'not null' => true, 'description' => 'notices with content greater than this value will have all urls shortened, 0 = always, -1 = only if notice text is longer than max allowed'),
  42. 'created' => array('type' => 'datetime', 'description' => 'date this record was created'),
  43. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  44. ),
  45. 'primary key' => array('user_id'),
  46. 'foreign keys' => array(
  47. 'user_urlshortener_prefs_user_id_fkey' => array('user', array('user_id' => 'id')),
  48. ),
  49. );
  50. }
  51. public static function maxUrlLength($user)
  52. {
  53. $def = common_config('url', 'maxurllength');
  54. $prefs = self::getPrefs($user);
  55. if (empty($prefs)) {
  56. return $def;
  57. } else {
  58. return $prefs->maxurllength;
  59. }
  60. }
  61. public static function maxNoticeLength($user)
  62. {
  63. $def = common_config('url', 'maxnoticelength');
  64. if ($def == -1) {
  65. /*
  66. * maxContent==0 means infinite length,
  67. * but maxNoticeLength==0 means "always shorten"
  68. * so if maxContent==0 we must set this to -1
  69. */
  70. $def = Notice::maxContent() ?: -1;
  71. }
  72. $prefs = self::getPrefs($user);
  73. if (empty($prefs)) {
  74. return $def;
  75. } else {
  76. return $prefs->maxnoticelength;
  77. }
  78. }
  79. public static function urlShorteningService($user)
  80. {
  81. $def = common_config('url', 'shortener');
  82. $prefs = self::getPrefs($user);
  83. if (empty($prefs)) {
  84. if (!empty($user)) {
  85. return $user->urlshorteningservice;
  86. } else {
  87. return $def;
  88. }
  89. } else {
  90. return $prefs->urlshorteningservice;
  91. }
  92. }
  93. public static function getPrefs($user)
  94. {
  95. if (empty($user)) {
  96. return null;
  97. }
  98. $prefs = User_urlshortener_prefs::getKV('user_id', $user->id);
  99. return $prefs;
  100. }
  101. }