]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/MobileProfile/MobileProfilePlugin.php
Comment out unreachable code spewing notices due to use of undefined variables in...
[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_MOBILEPROFILE',
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     public $mobileFeatures = array();
55
56     function __construct($DTD='http://www.wapforum.org/DTD/xhtml-mobile10.dtd')
57     {
58         $this->DTD = $DTD;
59
60         parent::__construct();
61     }
62
63
64     function onStartShowHTML($action)
65     {
66         // XXX: This should probably graduate to WAP20Plugin
67
68         // If they are on the mobile site, serve them MP
69         if ((common_config('site', 'mobileserver').'/'.
70              common_config('site', 'path').'/' == 
71             $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'])) {
72
73             $this->serveMobile = true;
74         } else {
75             // If they like the WAP 2.0 mimetype, serve them MP
76             // @fixme $type is undefined, making this if case useless and spewing errors.
77             // What's the intent?
78             //if (strstr('application/vnd.wap.xhtml+xml', $type) !== false) {
79             //    $this->serveMobile = true;
80             //} else {
81                 // If they are a mobile device that supports WAP 2.0, 
82                 // serve them MP
83
84                 // XXX: Browser sniffing sucks
85
86                 // I really don't like going through this every page, 
87                 // perhaps use $_SESSION or cookies
88
89                 // May be better to group the devices in terms of 
90                 // low,mid,high-end
91
92                 // Or, detect the mobile devices based on their support for 
93                 // MP 1.0, 1.1, or 1.2 may be ideal. Possible?
94
95                 $this->mobiledevices = array(
96                     'alcatel',
97                     'android',
98                     'audiovox',
99                     'au-mic,',
100                     'avantgo',
101                     'blackberry',
102                     'blazer',
103                     'cldc-',
104                     'danger',
105                     'epoc',
106                     'ericsson',
107                     'ericy',
108                     'iphone',
109                     'ipaq',
110                     'ipod',
111                     'j2me',
112                     'lg',
113                     'midp-',
114                     'mobile',
115                     'mot',
116                     'netfront',
117                     'nitro',
118                     'nokia',
119                     'opera mini',
120                     'palm',
121                     'palmsource',
122                     'panasonic',
123                     'philips',
124                     'pocketpc',
125                     'portalmmm',
126                     'rover',
127                     'samsung',
128                     'sanyo',
129                     'series60',
130                     'sharp',
131                     'sie-',
132                     'smartphone',
133                     'sony',
134                     'symbian',
135                     'up.browser',
136                     'up.link',
137                     'up.link',
138                     'vodafone',
139                     'wap1',
140                     'wap2',
141                     'windows ce'
142                 );
143
144                 $httpuseragent = strtolower($_SERVER['HTTP_USER_AGENT']);
145
146                 foreach ($this->mobiledevices as $md) {
147                     if (strstr($httpuseragent, $md) !== false) {
148                         $this->setMobileFeatures($httpuseragent);
149
150                         $this->serveMobile = true;
151                         break;
152                     }
153                 }
154             //}
155
156             // If they are okay with MP, and the site has a mobile server, 
157             // redirect there
158             if ($this->serveMobile && 
159                 common_config('site', 'mobileserver') !== false &&
160                 (common_config('site', 'mobileserver') != 
161                     common_config('site', 'server'))) {
162
163                 // FIXME: Redirect to equivalent page on mobile site instead
164                 common_redirect($this->_common_path(''), 302);
165             }
166         }
167
168         if (!$this->serveMobile) {
169             return true;
170         }
171
172         // @fixme $type is undefined, making this if case useless and spewing errors.
173         // What's the intent?
174         //if (!$type) {
175             $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
176               $_SERVER['HTTP_ACCEPT'] : null;
177
178             $cp = common_accept_to_prefs($httpaccept);
179             $sp = common_accept_to_prefs(PAGE_TYPE_PREFS_MOBILEPROFILE);
180
181             $type = common_negotiate_type($cp, $sp);
182
183             if (!$type) {
184                 throw new ClientException(_('This page is not available in a '.
185                                             'media type you accept'), 406);
186             }
187         //}
188
189         header('Content-Type: '.$type);
190
191         $action->extraHeaders();
192         if (preg_match("/.*\/.*xml/", $type)) {
193             // Required for XML documents
194             $action->xw->startDocument('1.0', 'UTF-8');
195         }
196         $action->xw->writeDTD('html',
197                         '-//WAPFORUM//DTD XHTML Mobile 1.0//EN',
198                         $this->DTD);
199
200         $language = $action->getLanguage();
201
202         $action->elementStart('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
203                                             'xml:lang' => $language));
204
205         return false;
206     }
207
208
209     function setMobileFeatures($useragent)
210     {
211         $mobiledeviceInputFileType = array(
212             'nokia'
213         );
214
215         $this->mobileFeatures['inputfiletype'] = false;
216
217         foreach ($mobiledeviceInputFileType as $md) {
218             if (strstr($useragent, $md) !== false) {
219                 $this->mobileFeatures['inputfiletype'] = true;
220                 break;
221             }
222         }
223     }
224
225
226     function onStartShowHeadElements($action)
227     {
228         // @fixme nothing appears to set a serveMobile on any action,
229         // so this is useless and spews errors. Is this supposed to be
230         // checking $this?
231         //if (!$action->serveMobile) {
232         //    return true;
233         //}
234
235         $action->showTitle();
236         $action->showShortcutIcon();
237         $action->showStylesheets();
238         $action->showFeeds();
239         $action->showDescription();
240         $action->extraHead();
241     }
242
243
244     function onStartShowStatusNetStyles($action)
245     {
246         if (!$this->serveMobile) {
247             return true;
248         }
249
250         $action->cssLink('css/display.css');
251
252         if (file_exists(Theme::file('css/mp-screen.css'))) {
253             $action->cssLink('css/mp-screen.css', null, 'screen');
254         } else {
255             $action->cssLink('plugins/MobileProfile/mp-screen.css',null,'screen');
256         }
257
258         if (file_exists(Theme::file('css/mp-handheld.css'))) {
259             $action->cssLink('css/mp-handheld.css', null, 'handheld');
260         } else {
261             $action->cssLink('plugins/MobileProfile/mp-handheld.css',null,'handheld');
262         }
263
264         return false;
265     }
266
267
268     function onStartShowUAStyles($action) {
269         if (!$this->serveMobile) {
270             return true;
271         }
272
273         return false;
274     }
275
276     function onStartShowHeader($action)
277     {
278         if (!$this->serveMobile) {
279             return true;
280         }
281
282         $action->elementStart('div', array('id' => 'header'));
283         $this->_showLogo($action);
284         $this->_showPrimaryNav($action);
285         if (common_logged_in()) {
286             $action->showNoticeForm();
287         }
288         $action->elementEnd('div');
289
290         return false;
291     }
292
293
294     function _showLogo($action)
295     {
296         $action->elementStart('address', 'vcard');
297         $action->elementStart('a', array('class' => 'url home bookmark',
298                                        'href' => common_local_url('public')));
299         if (common_config('site', 'mobilelogo') || 
300             file_exists(Theme::file('logo.png')) || 
301             file_exists(Theme::file('mobilelogo.png'))) {
302
303             $action->element('img', array('class' => 'photo',
304                 'src' => (common_config('site', 'mobilelogo')) ? common_config('site', 'mobilelogo') : 
305                             ((file_exists(Theme::file('mobilelogo.png'))) ? (Theme::path('mobilelogo.png')) : Theme::path('logo.png')),
306                 'alt' => common_config('site', 'name')));
307         }
308         $action->element('span', array('class' => 'fn org'), common_config('site', 'name'));
309         $action->elementEnd('a');
310         $action->elementEnd('address');
311     }
312
313
314     function _showPrimaryNav($action)
315     {
316         $user    = common_current_user();
317         $action->elementStart('ul', array('id' => 'site_nav_global_primary'));
318         if ($user) {
319             $action->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
320                             _('Home'));
321             $action->menuItem(common_local_url('profilesettings'),
322                             _('Account'));
323             $action->menuItem(common_local_url('oauthconnectionssettings'),
324                                 _('Connect'));
325             if ($user->hasRight(Right::CONFIGURESITE)) {
326                 $action->menuItem(common_local_url('siteadminpanel'),
327                                 _('Admin'), _('Change site configuration'), false, 'nav_admin');
328             }
329             if (common_config('invite', 'enabled')) {
330                 $action->menuItem(common_local_url('invite'),
331                                 _('Invite'));
332             }
333             $action->menuItem(common_local_url('logout'),
334                             _('Logout'));
335         } else {
336             if (!common_config('site', 'closed')) {
337                 $action->menuItem(common_local_url('register'),
338                                 _('Register'));
339             }
340             $action->menuItem(common_local_url('login'),
341                             _('Login'));
342         }
343         if ($user || !common_config('site', 'private')) {
344             $action->menuItem(common_local_url('peoplesearch'),
345                             _('Search'));
346         }
347         $action->elementEnd('ul');
348     }
349
350
351     function onStartShowNoticeFormData($form)
352     {
353         if (!$this->serveMobile) {
354             return true;
355         }
356
357         $form->out->element('textarea', array('id' => 'notice_data-text',
358                                               'cols' => 15,
359                                               'rows' => 4,
360                                               'name' => 'status_textarea'),
361                             ($form->content) ? $form->content : '');
362
363         $contentLimit = Notice::maxContent();
364
365         if ($contentLimit > 0) {
366             $form->out->element('div', array('id' => 'notice_text-count'),
367                                 $contentLimit);
368         }
369
370         if (common_config('attachments', 'uploads')) {
371             if ($this->mobileFeatures['inputfiletype']) {
372                 $form->out->element('label', array('for' => 'notice_data-attach'), _('Attach'));
373                 $form->out->element('input', array('id' => 'notice_data-attach',
374                                                    'type' => 'file',
375                                                    'name' => 'attach',
376                                                    'title' => _('Attach a file')));
377                 $form->out->hidden('MAX_FILE_SIZE', common_config('attachments', 'file_quota'));
378             }
379         }
380         if ($form->action) {
381             $form->out->hidden('notice_return-to', $form->action, 'returnto');
382         }
383         $form->out->hidden('notice_in-reply-to', $form->inreplyto, 'inreplyto');
384
385         return false;
386     }
387
388
389     function onStartShowAside($action)
390     {
391         if ($this->serveMobile) {
392             return false;
393         }
394     }
395
396
397     function onStartShowScripts($action)
398     {
399
400     }
401
402
403     function _common_path($relative, $ssl=false)
404     {
405         $pathpart = (common_config('site', 'path')) ? common_config('site', 'path')."/" : '';
406
407         if (($ssl && (common_config('site', 'ssl') === 'sometimes'))
408             || common_config('site', 'ssl') === 'always') {
409             $proto = 'https';
410             if (is_string(common_config('site', 'sslserver')) &&
411                 mb_strlen(common_config('site', 'sslserver')) > 0) {
412                 $serverpart = common_config('site', 'sslserver');
413             } else {
414                 $serverpart = common_config('site', 'mobileserver');
415             }
416         } else {
417             $proto      = 'http';
418             $serverpart = common_config('site', 'mobileserver');
419         }
420
421         return $proto.'://'.$serverpart.'/'.$pathpart.$relative;
422     }
423
424     function onPluginVersion(&$versions)
425     {
426         $versions[] = array('name' => 'MobileProfile',
427                             'version' => STATUSNET_VERSION,
428                             'author' => 'Sarven Capadisli',
429                             'homepage' => 'http://status.net/wiki/Plugin:MobileProfile',
430                             'rawdescription' =>
431                             _m('XHTML MobileProfile output for supporting user agents.'));
432         return true;
433     }
434 }