autocomplete.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * List profiles and groups for autocompletion
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Plugin
  23. * @package StatusNet
  24. * @author Craig Andrews <candrews@integralblue.com>
  25. * @author Mikael Nordfeldth <mmn@hethane.se>
  26. * @copyright 2008-2009 StatusNet, Inc.
  27. * @copyright 2009-2013 Free Software Foundation, Inc http://www.fsf.org
  28. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  29. * @link http://status.net/
  30. */
  31. if (!defined('GNUSOCIAL')) { exit(1); }
  32. /**
  33. * List users for autocompletion
  34. *
  35. * This is the form for adding a new g
  36. *
  37. * @category Plugin
  38. * @package StatusNet
  39. * @author Craig Andrews <candrews@integralblue.com>
  40. * @author Mikael Nordfeldth <mmn@hethane.se>
  41. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  42. * @link http://status.net/
  43. */
  44. class AutocompleteAction extends Action
  45. {
  46. protected $needLogin = true;
  47. private $result;
  48. /**
  49. * Last-modified date for page
  50. *
  51. * When was the content of this page last modified? Based on notice,
  52. * profile, avatar.
  53. *
  54. * @return int last-modified date as unix timestamp
  55. */
  56. function lastModified()
  57. {
  58. $max=0;
  59. foreach($this->profiles as $profile){
  60. $max = max($max, strtotime($profile->modified));
  61. }
  62. foreach($this->groups as $group){
  63. $max = max($max,strtotime($group->modified));
  64. }
  65. return $max;
  66. }
  67. /**
  68. * An entity tag for this page
  69. *
  70. * Shows the ETag for the page, based on the notice ID and timestamps
  71. * for the notice, profile, and avatar. It's weak, since we change
  72. * the date text "one hour ago", etc.
  73. *
  74. * @return string etag
  75. */
  76. function etag()
  77. {
  78. return '"' . implode(':', array($this->arg('action'),
  79. common_user_cache_hash(),
  80. crc32($this->arg('term')), //the actual string can have funny characters in we don't want showing up in the etag
  81. $this->arg('limit'),
  82. $this->lastModified())) . '"';
  83. }
  84. protected function prepare(array $args=array())
  85. {
  86. // If we die, show short error messages.
  87. GNUsocial::setApi(true);
  88. parent::prepare($args);
  89. $this->groups=array();
  90. $this->profiles=array();
  91. $term = $this->arg('term');
  92. $limit = $this->arg('limit');
  93. if($limit > 200) $limit=200; //prevent DOS attacks
  94. if(substr($term,0,1)=='@'){
  95. //profile search
  96. $term=substr($term,1);
  97. $profile = new Profile();
  98. $profile->limit($limit);
  99. $profile->whereAdd('nickname like \'' . trim($profile->escape($term), '\'') . '%\'');
  100. $profile->whereAdd(sprintf('id in (SELECT id FROM user) OR '
  101. . 'id in (SELECT subscribed from subscription'
  102. . ' where subscriber = %d)', $this->scoped->id));
  103. if ($profile->find()) {
  104. while($profile->fetch()) {
  105. $this->profiles[]=clone($profile);
  106. }
  107. }
  108. }
  109. if(substr($term,0,1)=='!'){
  110. //group search
  111. $term=substr($term,1);
  112. $group = new User_group();
  113. $group->limit($limit);
  114. $group->whereAdd('nickname like \'' . trim($group->escape($term), '\'') . '%\'');
  115. //Can't post to groups we're not subscribed to...:
  116. $group->whereAdd(sprintf('id in (SELECT group_id FROM group_member'
  117. . ' WHERE profile_id = %d)', $this->scoped->id));
  118. if($group->find()){
  119. while($group->fetch()) {
  120. $this->groups[]=clone($group);
  121. }
  122. }
  123. }
  124. return true;
  125. }
  126. protected function handle()
  127. {
  128. parent::handle();
  129. $results = array();
  130. foreach($this->profiles as $profile){
  131. $avatarUrl = $profile->avatarUrl(AVATAR_MINI_SIZE);
  132. $acct = $profile->getAcctUri();
  133. $identifier = explode(':', $profile->getAcctUri(), 2)[1];
  134. $results[] = array(
  135. 'value' => '@'.$identifier,
  136. 'nickname' => $profile->getNickname(),
  137. 'acct_uri' => $acct,
  138. 'label'=> "${identifier} (".$profile->getFullname().")",
  139. 'avatar' => $avatarUrl,
  140. 'type' => 'user'
  141. );
  142. }
  143. foreach($this->groups as $group){
  144. $profile = $group->getProfile();
  145. // sigh.... encapsulate this upstream!
  146. if ($group->mini_logo) {
  147. $avatarUrl = $group->mini_logo;
  148. } else {
  149. $avatarUrl = User_group::defaultLogo(AVATAR_MINI_SIZE);
  150. }
  151. $acct = $profile->getAcctUri();
  152. $identifier = explode(':', $profile->getAcctUri(), 2)[1];
  153. $results[] = array(
  154. 'value' => '!'.$group->getNickname(),
  155. 'nickname' => $group->getNickname(),
  156. 'acct_uri' => $acct,
  157. 'label'=> "${identifier} (".$group->getFullname().")",
  158. 'avatar' => $avatarUrl,
  159. 'type' => 'group');
  160. }
  161. print json_encode($results);
  162. }
  163. /**
  164. * Is this action read-only?
  165. *
  166. * @param array $args other arguments
  167. *
  168. * @return boolean is read only action?
  169. */
  170. function isReadOnly($args)
  171. {
  172. return true;
  173. }
  174. }