autocomplete.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. // but maybe this file has been modified after that and could
  66. // respond differently
  67. return max($max, filemtime(__FILE__));
  68. }
  69. /**
  70. * An entity tag for this page
  71. *
  72. * Shows the ETag for the page, based on the notice ID and timestamps
  73. * for the notice, profile, and avatar. It's weak, since we change
  74. * the date text "one hour ago", etc.
  75. *
  76. * @return string etag
  77. */
  78. function etag()
  79. {
  80. return '"' . implode(':', array($this->arg('action'),
  81. common_user_cache_hash(),
  82. crc32($this->arg('term')), //the actual string can have funny characters in we don't want showing up in the etag
  83. $this->arg('limit'),
  84. $this->lastModified())) . '"';
  85. }
  86. protected function prepare(array $args=array())
  87. {
  88. // If we die, show short error messages.
  89. GNUsocial::setApi(true);
  90. parent::prepare($args);
  91. $this->groups=array();
  92. $this->profiles=array();
  93. $term = $this->arg('term');
  94. $limit = $this->arg('limit');
  95. if($limit > 200) $limit=200; //prevent DOS attacks
  96. if(substr($term,0,1)=='@'){
  97. //profile search
  98. $term=substr($term,1);
  99. $profile = new Profile();
  100. $profile->limit($limit);
  101. $profile->whereAdd('nickname like \'' . trim($profile->escape($term), '\'') . '%\'');
  102. $profile->whereAdd(sprintf('id in (SELECT id FROM user) OR '
  103. . 'id in (SELECT subscribed from subscription'
  104. . ' where subscriber = %d)', $this->scoped->id));
  105. if ($profile->find()) {
  106. while($profile->fetch()) {
  107. $this->profiles[]=clone($profile);
  108. }
  109. }
  110. }
  111. if(substr($term,0,1)=='!'){
  112. //group search
  113. $term=substr($term,1);
  114. $group = new User_group();
  115. $group->limit($limit);
  116. $group->whereAdd('nickname like \'' . trim($group->escape($term), '\'') . '%\'');
  117. //Can't post to groups we're not subscribed to...:
  118. $group->whereAdd(sprintf('id in (SELECT group_id FROM group_member'
  119. . ' WHERE profile_id = %d)', $this->scoped->id));
  120. if($group->find()){
  121. while($group->fetch()) {
  122. $this->groups[]=clone($group);
  123. }
  124. }
  125. }
  126. return true;
  127. }
  128. protected function handle()
  129. {
  130. parent::handle();
  131. $results = array();
  132. foreach($this->profiles as $profile){
  133. $avatarUrl = $profile->avatarUrl(AVATAR_MINI_SIZE);
  134. $acct = $profile->getAcctUri();
  135. $identifier = explode(':', $profile->getAcctUri(), 2)[1];
  136. $results[] = array(
  137. 'value' => '@'.$identifier,
  138. 'nickname' => $profile->getNickname(),
  139. 'acct_uri' => $acct,
  140. 'label'=> "${identifier} (".$profile->getFullname().")",
  141. 'avatar' => $avatarUrl,
  142. 'type' => 'user'
  143. );
  144. }
  145. foreach($this->groups as $group){
  146. $profile = $group->getProfile();
  147. // sigh.... encapsulate this upstream!
  148. if ($group->mini_logo) {
  149. $avatarUrl = $group->mini_logo;
  150. } else {
  151. $avatarUrl = User_group::defaultLogo(AVATAR_MINI_SIZE);
  152. }
  153. $acct = $profile->getAcctUri();
  154. $identifier = explode(':', $profile->getAcctUri(), 2)[1];
  155. $results[] = array(
  156. 'value' => '!'.$identifier,
  157. 'nickname' => $group->getNickname(),
  158. 'acct_uri' => $acct,
  159. 'label'=> "${identifier} (".$group->getFullname().")",
  160. 'avatar' => $avatarUrl,
  161. 'type' => 'group');
  162. }
  163. print json_encode($results);
  164. }
  165. /**
  166. * Is this action read-only?
  167. *
  168. * @param array $args other arguments
  169. *
  170. * @return boolean is read only action?
  171. */
  172. function isReadOnly($args)
  173. {
  174. return true;
  175. }
  176. }