]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/AnonymousFave/AnonymousFavePlugin.php
Initial plugin for allowing anonymous favoriting
[quix0rs-gnu-social.git] / plugins / AnonymousFave / AnonymousFavePlugin.php
1 <?php
2
3 /**
4  * StatusNet - the distributed open-source microblogging tool
5  * Copyright (C) 2010, StatusNet, Inc.
6  *
7  * A plugin to allow anonymous users to favorite notices
8  *
9  * PHP version 5
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Affero General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU Affero General Public License for more details.
20  *
21  * You should have received a copy of the GNU Affero General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  *
24  * @category  Plugin
25  * @package   StatusNet
26  * @author    Zach Copley <zach@status.net>
27  * @copyright 2010 StatusNet, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
29  * @link      http://status.net/
30  */
31
32 if (!defined('STATUSNET')) {
33     // This check helps protect against security problems;
34     // your code file can't be executed directly from the web.
35     exit(1);
36 }
37
38 define('ANONYMOUS_FAVE_PLUGIN_VERSION', '0.1');
39
40 /**
41  * Anonymous Fave plugin to allow anonymous (not logged in) users
42  * to favorite notices
43  *
44  * @category  Plugin
45  * @package   StatusNet
46  * @author    Zach Copley <zach@status.net>
47  * @copyright 2010 StatusNet, Inc.
48  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
49  * @link      http://status.net/
50  */
51 class AnonymousFavePlugin extends Plugin {
52
53     function onArgsInitialize() {
54         // We always want a session because we're tracking anon users
55         common_ensure_session();
56     }
57
58     function onEndShowHTML($action)
59     {
60         if (!common_logged_in()) {
61             // Set a place to return to when submitting forms
62             common_set_returnto($action->selfUrl());
63         }
64     }
65
66     function onEndShowScripts($action)
67     {
68         // Setup ajax calls for favoriting. Usually this is only done when
69         // a user is logged in.
70         $action->inlineScript('SN.U.NoticeFavor();');
71     }
72
73     function onAutoload($cls)
74     {
75         $dir = dirname(__FILE__);
76
77         switch ($cls) {
78             case 'AnonFavorAction':
79                 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
80                 return false;
81             case 'AnonDisFavorAction':
82                 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
83                 return false;
84             case 'AnonFavorForm':
85                 include_once $dir . '/anonfavorform.php';
86                 return false;
87             case 'AnonDisFavorForm':
88                 include_once $dir . '/anondisfavorform.php';
89                 return false;
90             default:
91                 return true;
92         }
93     }
94
95     function onStartInitializeRouter($m) {
96
97         $m->connect('main/anonfavor', array('action' => 'AnonFavor'));
98         $m->connect('main/anondisfavor', array('action' => 'AnonDisFavor'));
99
100         return true;
101     }
102
103     function onStartShowNoticeOptions($item) {
104
105         if (!common_logged_in()) {
106             $item->out->elementStart('div', 'notice-options');
107             $item->showFaveForm();
108             $item->out->elementEnd('div');
109         }
110
111         return true;
112     }
113
114     function onStartShowFaveForm($item) {
115
116         if (!common_logged_in()) {
117
118             $profile = $this->getAnonProfile();
119             if (!empty($profile)) {
120                 if ($profile->hasFave($item->notice)) {
121                     $disfavor = new AnonDisFavorForm($item->out, $item->notice);
122                     $disfavor->show();
123                 } else {
124                     $favor = new AnonFavorForm($item->out, $item->notice);
125                     $favor->show();
126                 }
127             }
128         }
129
130         return true;
131     }
132
133     function createAnonProfile() {
134
135         // Get the anon user's IP, and turn it into a nickname
136         list($proxy, $ip) = common_client_ip();
137         // IP + time + random number should avoid collisions
138         $nickname = 'anonymous-' . $ip . '-' . time() . '-' . common_good_rand(5);
139
140         $profile = new Profile();
141         $profile->nickname = $nickname;
142         $id = $profile->insert();
143
144         if (!empty($id)) {
145             common_log(
146                     LOG_INFO,
147                     "AnonymousFavePlugin - created profile for anonymous user from IP: "
148                     . $ip
149                     . ', nickname = '
150                     . $nickname
151             );
152         }
153
154         return $profile;
155     }
156
157     function getAnonProfile() {
158
159         $anon = $_SESSION['anon_nickname'];
160
161         $profile = null;
162
163         if (!empty($anon)) {
164             $profile = Profile::staticGet('nickname', $anon);
165         } else {
166             $profile = $this->createAnonProfile();
167             $_SESSION['anon_nickname'] = $profile->nickname;
168         }
169
170         if (!empty($profile)) {
171             return $profile;
172         }
173     }
174
175     /**
176      * Provide plugin version information.
177      *
178      * This data is used when showing the version page.
179      *
180      * @param array &$versions array of version data arrays; see EVENTS.txt
181      *
182      * @return boolean hook value
183      */
184     function onPluginVersion(&$versions)
185     {
186         $url = 'http://status.net/wiki/Plugin:AnonymousFave';
187
188         $versions[] = array('name' => 'AnonymousFave',
189             'version' => ANONYMOUS_FAVE_PLUGIN_VERSION,
190             'author' => 'Zach Copley',
191             'homepage' => $url,
192             'rawdescription' =>
193             _m('Allow anonymous users to favorite notices.'));
194
195         return true;
196     }
197
198 }