userautocomplete.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. * Action for showing Twitter-like JSON search results
  18. *
  19. * @category Search
  20. * @package GNUsocial
  21. * @author Zach Copley <zach@status.net>
  22. * @copyright 2011 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. class UserautocompleteAction extends Action
  27. {
  28. public $query;
  29. /**
  30. * Initialization.
  31. *
  32. * @param array $args Web and URL arguments
  33. *
  34. * @return boolean true if nothing goes wrong
  35. * @throws ClientException
  36. */
  37. public function prepare(array $args = [])
  38. {
  39. parent::prepare($args);
  40. $this->query = $this->trimmed('term');
  41. return true;
  42. }
  43. /**
  44. * Handle a request
  45. *
  46. * @return void
  47. */
  48. public function handle()
  49. {
  50. parent::handle();
  51. $this->showResults();
  52. }
  53. /**
  54. * Search for users matching the query and spit the results out
  55. * as a quick-n-dirty JSON document
  56. *
  57. * @return void
  58. * @throws ServerException
  59. */
  60. public function showResults()
  61. {
  62. $people = array();
  63. $profile = new Profile();
  64. $search_engine = $profile->getSearchEngine('profile');
  65. $search_engine->set_sort_mode('nickname_desc');
  66. $search_engine->limit(0, 10);
  67. $search_engine->query(strtolower($this->query . '*'));
  68. $cnt = $profile->find();
  69. if ($cnt > 0) {
  70. $sql = 'SELECT profile.* FROM profile, user WHERE profile.id = user.id '
  71. . ' AND LEFT(LOWER(profile.nickname), '
  72. . strlen($this->query)
  73. . ') = \'%s\' '
  74. . ' LIMIT 10';
  75. $profile->query(sprintf($sql, $this->query));
  76. }
  77. while ($profile->fetch()) {
  78. $people[] = $profile->nickname;
  79. }
  80. header('Content-Type: application/json; charset=utf-8');
  81. print json_encode($people);
  82. }
  83. /**
  84. * Do we need to write to the database?
  85. *
  86. * @param $args
  87. * @return boolean true
  88. */
  89. public function isReadOnly($args)
  90. {
  91. return true;
  92. }
  93. }