]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Autocomplete/actions/autocomplete.php
Autocomplete migrated to jquery-ui autocomplete
[quix0rs-gnu-social.git] / plugins / Autocomplete / actions / autocomplete.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * List users 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('STATUSNET') && !defined('LACONICA')) {
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->users as $user){
66             $max = max($max,strtotime($user->modified),strtotime($user->getProfile()->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($args)
93     {
94         // If we die, show short error messages.
95         StatusNet::setApi(true);
96
97         parent::prepare($args);
98
99         $cur = common_current_user();
100         if (!$cur) {
101             // TRANS: Client exception in autocomplete plugin.
102             throw new ClientException(_m('Access forbidden.'), true);
103         }
104
105         $this->groups=array();
106         $this->users=array();
107         $term = $this->arg('term');
108         $limit = $this->arg('limit');
109         if($limit > 200) $limit=200; //prevent DOS attacks
110         if(substr($term,0,1)=='@'){
111             //user search
112             $term=substr($term,1);
113             $user = new User();
114             $user->limit($limit);
115             $user->whereAdd('nickname like \'' . trim($user->escape($term), '\'') . '%\'');
116             if($user->find()){
117                 while($user->fetch()) {
118                     $this->users[]=clone($user);
119                 }
120             }
121         }
122         if(substr($term,0,1)=='!'){
123             //group search
124             $term=substr($term,1);
125             $group = new User_group();
126             $group->limit($limit);
127             $group->whereAdd('nickname like \'' . trim($group->escape($term), '\'') . '%\'');
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->users as $user){
143             $profile = $user->getProfile();
144             $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
145             // sigh.... encapsulate this upstream!
146             if ($avatar) {
147                 $avatar = $avatar->displayUrl();
148             } else {
149                 $avatar = Avatar::defaultImage(AVATAR_MINI_SIZE);
150             }
151             $results[] = array(
152                 'value' => '@'.$profile->nickname,
153                 'nickname' => $profile->nickname,
154                 'label'=> $profile->getFancyName(),
155                 'avatar' => $avatar,
156                 'type' => 'user'
157             );
158         }
159         foreach($this->groups as $group){
160             // sigh.... encapsulate this upstream!
161             if ($group->mini_logo) {
162                 $avatar = $group->mini_logo;
163             } else {
164                 $avatar = User_group::defaultLogo(AVATAR_MINI_SIZE);
165             }
166             $results[] = array(
167                 'value' => '!'.$group->nickname,
168                 'nickname' => $group->nickname,
169                 'label'=> $group->getFancyName(),
170                 'avatar' => $avatar,
171                 'type' => 'group');
172         }
173         print json_encode($results);
174     }
175
176     /**
177      * Is this action read-only?
178      *
179      * @param array $args other arguments
180      *
181      * @return boolean is read only action?
182      */
183     function isReadOnly($args)
184     {
185         return true;
186     }
187 }