]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Autocomplete/actions/autocomplete.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / Autocomplete / actions / autocomplete.php
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
32 if (!defined('GNUSOCIAL')) { 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
50     private $result;
51
52     /**
53      * Last-modified date for page
54      *
55      * When was the content of this page last modified? Based on notice,
56      * profile, avatar.
57      *
58      * @return int last-modified date as unix timestamp
59      */
60     function lastModified()
61     {
62         $max=0;
63         foreach($this->profiles as $profile){
64             $max = max($max, strtotime($profile->modified));
65         }
66         foreach($this->groups as $group){
67             $max = max($max,strtotime($group->modified));
68         }
69         return $max;
70     }
71
72     /**
73      * An entity tag for this page
74      *
75      * Shows the ETag for the page, based on the notice ID and timestamps
76      * for the notice, profile, and avatar. It's weak, since we change
77      * the date text "one hour ago", etc.
78      *
79      * @return string etag
80      */
81     function etag()
82     {
83         return '"' . implode(':', array($this->arg('action'),
84             common_user_cache_hash(),
85             crc32($this->arg('term')), //the actual string can have funny characters in we don't want showing up in the etag
86             $this->arg('limit'),
87             $this->lastModified())) . '"';
88     }
89
90     protected function prepare(array $args=array())
91     {
92         // If we die, show short error messages.
93         GNUsocial::setApi(true);
94
95         parent::prepare($args);
96
97         $this->groups=array();
98         $this->profiles=array();
99         $term = $this->arg('term');
100         $limit = $this->arg('limit');
101         if($limit > 200) $limit=200; //prevent DOS attacks
102         if(substr($term,0,1)=='@'){
103             //profile search
104             $term=substr($term,1);
105             $profile = new Profile();
106             $profile->limit($limit);
107             $profile->whereAdd('nickname like \'' . trim($profile->escape($term), '\'') . '%\'');
108             $profile->whereAdd(sprintf('id in (SELECT id FROM user) OR '
109                                . 'id in (SELECT subscribed from subscription'
110                                . ' where subscriber = %d)', $this->scoped->id));
111             if ($profile->find()) {
112                 while($profile->fetch()) {
113                     $this->profiles[]=clone($profile);
114                 }
115             }
116         }
117         if(substr($term,0,1)=='!'){
118             //group search
119             $term=substr($term,1);
120             $group = new User_group();
121             $group->limit($limit);
122             $group->whereAdd('nickname like \'' . trim($group->escape($term), '\'') . '%\'');
123             //Can't post to groups we're not subscribed to...:
124             $group->whereAdd(sprintf('id in (SELECT group_id FROM group_member'
125                              . ' WHERE profile_id = %d)', $this->scoped->id));
126             if($group->find()){
127                 while($group->fetch()) {
128                     $this->groups[]=clone($group);
129                 }
130             }
131         }
132         return true;
133     }
134
135     protected function handle()
136     {
137         parent::handle();
138
139         $results = array();
140         foreach($this->profiles as $profile){
141             $avatarUrl = $profile->avatarUrl(AVATAR_MINI_SIZE);
142             $acct = $profile->getAcctUri();
143             $identifier = explode(':', $profile->getAcctUri(), 2)[1];
144             $results[] = array(
145                 'value' => '@'.$identifier,
146                 'nickname' => $profile->getNickname(),
147                 'acct_uri' => $acct,
148                 'label'=> "${identifier} (".$profile->getFullname().")",
149                 'avatar' => $avatarUrl,
150                 'type' => 'user'
151             );
152         }
153         foreach($this->groups as $group){
154             $profile = $group->getProfile();
155             // sigh.... encapsulate this upstream!
156             if ($group->mini_logo) {
157                 $avatarUrl = $group->mini_logo;
158             } else {
159                 $avatarUrl = User_group::defaultLogo(AVATAR_MINI_SIZE);
160             }
161             $acct = $profile->getAcctUri();
162             $identifier = explode(':', $profile->getAcctUri(), 2)[1];
163             $results[] = array(
164                 'value' => '!'.$group->getNickname(),
165                 'nickname' => $group->getNickname(),
166                 'acct_uri' => $acct,
167                 'label'=> "${identifier} (".$group->getFullname().")",
168                 'avatar' => $avatarUrl,
169                 'type' => 'group');
170         }
171         print json_encode($results);
172     }
173
174     /**
175      * Is this action read-only?
176      *
177      * @param array $args other arguments
178      *
179      * @return boolean is read only action?
180      */
181     function isReadOnly(array $args=array())
182     {
183         return true;
184     }
185 }