autocomplete.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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') && !defined('STATUSNET')) {
  32. exit(1);
  33. }
  34. /**
  35. * List users for autocompletion
  36. *
  37. * This is the form for adding a new g
  38. *
  39. * @category Plugin
  40. * @package StatusNet
  41. * @author Craig Andrews <candrews@integralblue.com>
  42. * @author Mikael Nordfeldth <mmn@hethane.se>
  43. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  44. * @link http://status.net/
  45. */
  46. class AutocompleteAction extends Action
  47. {
  48. protected $needLogin = true;
  49. private $result;
  50. /**
  51. * Last-modified date for page
  52. *
  53. * When was the content of this page last modified? Based on notice,
  54. * profile, avatar.
  55. *
  56. * @return int last-modified date as unix timestamp
  57. */
  58. function lastModified()
  59. {
  60. $max=0;
  61. foreach($this->profiles as $profile){
  62. $max = max($max, strtotime($profile->modified));
  63. }
  64. foreach($this->groups as $group){
  65. $max = max($max,strtotime($group->modified));
  66. }
  67. return $max;
  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. StatusNet::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. $results[] = array(
  135. 'value' => '@'.$profile->nickname,
  136. 'nickname' => $profile->nickname,
  137. 'label'=> $profile->getFancyName(),
  138. 'avatar' => $avatarUrl,
  139. 'type' => 'user'
  140. );
  141. }
  142. foreach($this->groups as $group){
  143. // sigh.... encapsulate this upstream!
  144. if ($group->mini_logo) {
  145. $avatarUrl = $group->mini_logo;
  146. } else {
  147. $avatarUrl = User_group::defaultLogo(AVATAR_MINI_SIZE);
  148. }
  149. $results[] = array(
  150. 'value' => '!'.$group->nickname,
  151. 'nickname' => $group->nickname,
  152. 'label'=> $group->getFancyName(),
  153. 'avatar' => $avatarUrl,
  154. 'type' => 'group');
  155. }
  156. print json_encode($results);
  157. }
  158. /**
  159. * Is this action read-only?
  160. *
  161. * @param array $args other arguments
  162. *
  163. * @return boolean is read only action?
  164. */
  165. function isReadOnly($args)
  166. {
  167. return true;
  168. }
  169. }