]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/MobileProfile/MobileProfilePlugin.php
1164974b97cc2c67dd61e1f2886a3c4c6218247b
[quix0rs-gnu-social.git] / plugins / MobileProfile / MobileProfilePlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * XHTML Mobile Profile plugin that uses WAP 2.0 Plugin
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    Sarven Capadisli <csarven@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 define('PAGE_TYPE_PREFS',
35        'application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html;q=0.9');
36
37 require_once INSTALLDIR.'/plugins/Mobile/WAP20Plugin.php';
38
39
40 /**
41  * Superclass for plugin to output XHTML Mobile Profile
42  *
43  * @category Plugin
44  * @package  StatusNet
45  * @author   Sarven Capadisli <csarven@status.net>
46  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://status.net/
48  */
49
50 class MobileProfilePlugin extends WAP20Plugin
51 {
52     public $DTD             = null;
53     public $serveMobile     = false;
54
55     function __construct($DTD='http://www.wapforum.org/DTD/xhtml-mobile10.dtd')
56     {
57         $this->DTD       = $DTD;
58
59         parent::__construct();
60     }
61
62
63     function onStartShowHTML($action)
64     {
65         if (!$type) {
66             $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
67               $_SERVER['HTTP_ACCEPT'] : null;
68
69             $cp = common_accept_to_prefs($httpaccept);
70             $sp = common_accept_to_prefs(PAGE_TYPE_PREFS);
71
72             $type = common_negotiate_type($cp, $sp);
73
74             if (!$type) {
75                 throw new ClientException(_('This page is not available in a '.
76                                             'media type you accept'), 406);
77             }
78         }
79
80         // XXX: This should probably graduate to WAP20Plugin
81
82         // If they are on the mobile site, serve them MP
83         if ((common_config('site', 'mobileserver').'/'.
84              common_config('site', 'path').'/' == 
85             $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'])) {
86
87             $this->serveMobile = true;
88         }
89         else {
90             // If they like the WAP 2.0 mimetype, serve them MP
91             if (strstr('application/vnd.wap.xhtml+xml', $type) !== false) {
92                 $this->serveMobile = true;
93             }
94             else {
95                 // If they are a mobile device that supports WAP 2.0, 
96                 // serve them MP
97
98                 // XXX: Browser sniffing sucks
99
100                 // I really don't like going through this every page, 
101                 // find a better way
102
103                 // May be better to categorize the devices in terms of 
104                 // low,mid,high-end
105
106                 // Or, detect the mobile devices based on their support for 
107                 // MP 1.0, 1.1, or 1.2 may be ideal. Possible?
108
109                 $this->mobiledevices = 
110                     array('alcatel', 'android', 'audiovox', 'au-mic,', 
111                           'avantgo', 'blackberry', 'blazer', 'cldc-', 'danger', 
112                           'epoc', 'ericsson', 'ericy', 'ipone', 'ipaq', 'j2me', 
113                           'lg', 'midp-', 'mobile', 'mot', 'netfront', 'nitro', 
114                           'nokia', 'opera mini', 'palm', 'palmsource', 
115                           'panasonic', 'philips', 'pocketpc', 'portalmmm', 
116                           'rover', 'samsung', 'sanyo', 'series60', 'sharp', 
117                           'sie-', 'smartphone', 'sony', 'symbian', 
118                           'up.browser', 'up.link', 'up.link', 'vodafone', 
119                           'wap1', 'wap2', 'windows ce');
120
121                 $httpuseragent = strtolower($_SERVER['HTTP_USER_AGENT']);
122
123                 foreach($this->mobiledevices as $md) {
124                     if (strstr($httpuseragent, $md) !== false) {
125                         $this->serveMobile = true;
126                         break;
127                     }
128                 }
129             }
130
131             // If they are okay with MP, and the site has a mobile server, 
132             // redirect there
133             if ($this->serveMobile && 
134                 common_config('site', 'mobileserver') !== false &&
135                 (common_config('site', 'mobileserver') != 
136                     common_config('site', 'server'))) {
137
138                 // FIXME: Redirect to equivalent page on mobile site instead
139                 header("Location: ".$this->_common_path(''));
140                 exit();
141             }
142         }
143
144         if (!$this->serveMobile) {
145             return true;
146         }
147
148         header('Content-Type: '.$type);
149
150         $action->extraHeaders();
151
152         $action->startXML('html',
153                         '-//WAPFORUM//DTD XHTML Mobile 1.0//EN',
154                         $this->DTD);
155
156         $language = $action->getLanguage();
157
158         $action->elementStart('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
159                                             'xml:lang' => $language));
160
161         return false;
162     }
163
164
165     function onStartShowHeadElements($action)
166     {
167         if (!$action->serveMobile) {
168             return true;
169         }
170
171         $action->showTitle();
172         $action->showShortcutIcon();
173         $action->showStylesheets();
174         $action->showFeeds();
175         $action->showDescription();
176         $action->extraHead();
177     }
178
179
180     function onStartShowStatusNetStyles($action)
181     {
182         if (!$this->serveMobile) {
183             return true;
184         }
185
186         if (file_exists(theme_file('css/mp-screen.css'))) {
187             $action->cssLink('css/mp-screen.css', null, 'screen');
188         }
189         else {
190             $action->element('link', array('rel' => 'stylesheet',
191                                          'type' => 'text/css',
192                                          'href' => common_path('plugins/MobileProfile/mp-screen.css') . '?version=' . STATUSNET_VERSION,
193                                          'media' => 'screen'));
194         }
195
196         if (file_exists(theme_file('css/mp-handheld.css'))) {
197             $action->cssLink('css/mp-handheld.css', null, 'handheld');
198         }
199         else {
200             $action->element('link', array('rel' => 'stylesheet',
201                                          'type' => 'text/css',
202                                          'href' => common_path('plugins/MobileProfile/mp-handheld.css') . '?version=' . STATUSNET_VERSION,
203                                          'media' => 'handheld'));
204         }
205
206         return false;
207     }
208
209
210     function onStartShowHeader($action)
211     {
212         if (!$this->serveMobile) {
213             return true;
214         }
215
216         $action->elementStart('div', array('id' => 'header'));
217         $this->_showLogo($action);
218         $this->_showPrimaryNav($action);
219         if (common_logged_in()) {
220             $action->showNoticeForm();
221         }
222         $action->elementEnd('div');
223
224         return false;
225     }
226
227
228     function _showLogo($action)
229     {
230         $action->elementStart('address', 'vcard');
231         $action->elementStart('a', array('class' => 'url home bookmark',
232                                        'href' => common_local_url('public')));
233         if (common_config('site', 'mobilelogo') || 
234             file_exists(theme_file('logo.png')) || 
235             file_exists(theme_file('mobilelogo.gif'))) {
236
237             $action->element('img', array('class' => 'photo',
238                 'src' => (common_config('site', 'mobilelogo')) ? common_config('site', 'mobilelogo') : 
239                             ((file_exists(theme_file('mobilelogo.png'))) ? (theme_path('mobilelogo.png')) : theme_path('logo.png')),
240                 'alt' => common_config('site', 'name')));
241         }
242         $action->element('span', array('class' => 'fn org'), common_config('site', 'name'));
243         $action->elementEnd('a');
244         $action->elementEnd('address');
245     }
246
247
248     function _showPrimaryNav($action) {
249         $user = common_current_user();
250         $connect = '';
251         if (common_config('xmpp', 'enabled')) {
252             $connect = 'imsettings';
253         } else if (common_config('sms', 'enabled')) {
254             $connect = 'smssettings';
255         } else if (common_config('twitter', 'enabled')) {
256             $connect = 'twittersettings';
257         }
258
259         $action->elementStart('ul', array('id' => 'site_nav_global_primary'));
260         if ($user) {
261             $action->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
262                             _('Home'));
263             $action->menuItem(common_local_url('profilesettings'),
264                             _('Account'));
265             if ($connect) {
266                 $action->menuItem(common_local_url($connect),
267                                 _('Connect'));
268             }
269             if (common_config('invite', 'enabled')) {
270                 $action->menuItem(common_local_url('invite'),
271                                 _('Invite'));
272             }
273             $action->menuItem(common_local_url('logout'),
274                             _('Logout'));
275         }
276         else {
277             if (!common_config('site', 'closed')) {
278                 $action->menuItem(common_local_url('register'),
279                                 _('Register'));
280             }
281             $action->menuItem(common_local_url('login'),
282                             _('Login'));
283         }
284         $action->menuItem(common_local_url('doc', array('title' => 'help')),
285                         _('Help'));
286         if ($user || !common_config('site', 'private')) {
287             $action->menuItem(common_local_url('peoplesearch'),
288                             _('Search'));
289         }
290         $action->elementEnd('ul');
291     }
292
293
294     function onStartShowNoticeFormData($form)
295     {
296         if (!$this->serveMobile) {
297             return true;
298         }
299
300         $form->out->element('textarea', array('id' => 'notice_data-text',
301                                               'cols' => 35,
302                                               'rows' => 4,
303                                               'name' => 'status_textarea'),
304                             ($form->content) ? $form->content : '');
305
306         $contentLimit = Notice::maxContent();
307
308         $form->out->element('script', array('type' => 'text/javascript'),
309                             'maxLength = ' . $contentLimit . ';');
310
311         if ($contentLimit > 0) {
312             $form->out->element('div', array('id' => 'notice_text-count'),
313                                 $contentLimit);
314         }
315
316         if (common_config('attachments', 'uploads')) {
317             $form->out->element('label', array('for' => 'notice_data-attach'),_('Attach'));
318             $form->out->element('input', array('id' => 'notice_data-attach',
319                                                'type' => 'file',
320                                                'name' => 'attach',
321                                                'title' => _('Attach a file')));
322             $form->out->hidden('MAX_FILE_SIZE', common_config('attachments', 'file_quota'));
323         }
324         if ($form->action) {
325             $form->out->hidden('notice_return-to', $form->action, 'returnto');
326         }
327         $form->out->hidden('notice_in-reply-to', $form->inreplyto, 'inreplyto');
328
329         return false;
330     }
331
332
333     function onStartShowAside($action)
334     {
335         if ($this->serveMobile) {
336             return false;
337         }
338     }
339
340
341     function onStartShowScripts($action)
342     {
343
344     }
345
346
347     function _common_path($relative, $ssl=false)
348     {
349         $pathpart = (common_config('site', 'path')) ? common_config('site', 'path')."/" : '';
350
351         if (($ssl && (common_config('site', 'ssl') === 'sometimes'))
352             || common_config('site', 'ssl') === 'always') {
353             $proto = 'https';
354             if (is_string(common_config('site', 'sslserver')) &&
355                 mb_strlen(common_config('site', 'sslserver')) > 0) {
356                 $serverpart = common_config('site', 'sslserver');
357             } else {
358                 $serverpart = common_config('site', 'mobileserver');
359             }
360         } else {
361             $proto = 'http';
362             $serverpart = common_config('site', 'mobileserver');
363         }
364
365         return $proto.'://'.$serverpart.'/'.$pathpart.$relative;
366     }
367 }
368
369
370 ?>