]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Autocomplete/actions/autocomplete.php
Merge commit 'refs/merge-requests/30' of https://gitorious.org/social/mainline into...
[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') && !defined('STATUSNET')) {
33     exit(1);
34 }
35
36 /**
37  * List users for autocompletion
38  *
39  * This is the form for adding a new g
40  *
41  * @category Plugin
42  * @package  StatusNet
43  * @author   Craig Andrews <candrews@integralblue.com>
44  * @author   Mikael Nordfeldth <mmn@hethane.se>
45  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46  * @link     http://status.net/
47  */
48 class AutocompleteAction extends Action
49 {
50     protected $needLogin = true;
51
52     private $result;
53
54     /**
55      * Last-modified date for page
56      *
57      * When was the content of this page last modified? Based on notice,
58      * profile, avatar.
59      *
60      * @return int last-modified date as unix timestamp
61      */
62     function lastModified()
63     {
64         $max=0;
65         foreach($this->profiles as $profile){
66             $max = max($max, strtotime($profile->modified));
67         }
68         foreach($this->groups as $group){
69             $max = max($max,strtotime($group->modified));
70         }
71         return $max;
72     }
73
74     /**
75      * An entity tag for this page
76      *
77      * Shows the ETag for the page, based on the notice ID and timestamps
78      * for the notice, profile, and avatar. It's weak, since we change
79      * the date text "one hour ago", etc.
80      *
81      * @return string etag
82      */
83     function etag()
84     {
85         return '"' . implode(':', array($this->arg('action'),
86             common_user_cache_hash(),
87             crc32($this->arg('term')), //the actual string can have funny characters in we don't want showing up in the etag
88             $this->arg('limit'),
89             $this->lastModified())) . '"';
90     }
91
92     protected function prepare(array $args=array())
93     {
94         // If we die, show short error messages.
95         StatusNet::setApi(true);
96
97         parent::prepare($args);
98
99         $this->groups=array();
100         $this->profiles=array();
101         $term = $this->arg('term');
102         $limit = $this->arg('limit');
103         if($limit > 200) $limit=200; //prevent DOS attacks
104         if(substr($term,0,1)=='@'){
105             //profile search
106             $term=substr($term,1);
107             $profile = new Profile();
108             $profile->limit($limit);
109             $profile->whereAdd('nickname like \'' . trim($profile->escape($term), '\'') . '%\'');
110             $profile->whereAdd(sprintf('id in (SELECT id FROM user) OR '
111                                . 'id in (SELECT subscribed from subscription'
112                                . ' where subscriber = %d)', $this->scoped->id));
113             if ($profile->find()) {
114                 while($profile->fetch()) {
115                     $this->profiles[]=clone($profile);
116                 }
117             }
118         }
119         if(substr($term,0,1)=='!'){
120             //group search
121             $term=substr($term,1);
122             $group = new User_group();
123             $group->limit($limit);
124             $group->whereAdd('nickname like \'' . trim($group->escape($term), '\'') . '%\'');
125             //Can't post to groups we're not subscribed to...:
126             $group->whereAdd(sprintf('id in (SELECT group_id FROM group_member'
127                              . ' WHERE profile_id = %d)', $this->scoped->id));
128             if($group->find()){
129                 while($group->fetch()) {
130                     $this->groups[]=clone($group);
131                 }
132             }
133         }
134         return true;
135     }
136
137     protected function handle()
138     {
139         parent::handle();
140
141         $results = array();
142         foreach($this->profiles as $profile){
143             $avatarUrl = $profile->avatarUrl(AVATAR_MINI_SIZE);
144             $results[] = array(
145                 'value' => '@'.$profile->nickname,
146                 'nickname' => $profile->nickname,
147                 'label'=> $profile->getFancyName(),
148                 'avatar' => $avatarUrl,
149                 'type' => 'user'
150             );
151         }
152         foreach($this->groups as $group){
153             // sigh.... encapsulate this upstream!
154             if ($group->mini_logo) {
155                 $avatarUrl = $group->mini_logo;
156             } else {
157                 $avatarUrl = User_group::defaultLogo(AVATAR_MINI_SIZE);
158             }
159             $results[] = array(
160                 'value' => '!'.$group->nickname,
161                 'nickname' => $group->nickname,
162                 'label'=> $group->getFancyName(),
163                 'avatar' => $avatarUrl,
164                 'type' => 'group');
165         }
166         print json_encode($results);
167     }
168
169     /**
170      * Is this action read-only?
171      *
172      * @param array $args other arguments
173      *
174      * @return boolean is read only action?
175      */
176     function isReadOnly(array $args=array())
177     {
178         return true;
179     }
180 }