3 * StatusNet, the distributed open-source microblogging tool
5 * XHTML Mobile Profile plugin that uses WAP 2.0 Plugin
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.
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.
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/>.
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/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
34 define('PAGE_TYPE_PREFS_MOBILEPROFILE',
35 'application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html;q=0.9');
37 require_once INSTALLDIR.'/plugins/Mobile/WAP20Plugin.php';
40 * Superclass for plugin to output XHTML Mobile Profile
44 * @author Sarven Capadisli <csarven@status.net>
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/
48 class MobileProfilePlugin extends WAP20Plugin
51 public $serveMobile = false;
52 public $reallyMobile = false;
53 public $mobileFeatures = array();
55 function __construct($DTD='http://www.wapforum.org/DTD/xhtml-mobile10.dtd')
59 parent::__construct();
62 function onStartShowHTML($action)
64 // XXX: This should probably graduate to WAP20Plugin
66 // If they are on the mobile site, serve them MP
67 if ((common_config('site', 'mobileserver').'/'.
68 common_config('site', 'path').'/' ==
69 $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'])) {
71 $this->serveMobile = true;
72 } else if (isset($_COOKIE['MobileOverride'])) {
73 // Cookie override is controlled by link at bottom.
74 $this->serveMobile = (bool)$_COOKIE['MobileOverride'];
76 // If they like the WAP 2.0 mimetype, serve them MP
77 // @fixme $type is undefined, making this if case useless and spewing errors.
79 //if (strstr('application/vnd.wap.xhtml+xml', $type) !== false) {
80 // $this->serveMobile = true;
82 // If they are a mobile device that supports WAP 2.0,
85 // XXX: Browser sniffing sucks
87 // I really don't like going through this every page,
88 // perhaps use $_SESSION or cookies
90 // May be better to group the devices in terms of
93 // Or, detect the mobile devices based on their support for
94 // MP 1.0, 1.1, or 1.2 may be ideal. Possible?
96 $this->mobiledevices = array(
147 'ipad', // Larger screen handles the full theme fairly well.
150 $httpuseragent = strtolower($_SERVER['HTTP_USER_AGENT']);
152 foreach ($blacklist as $md) {
153 if (strstr($httpuseragent, $md) !== false) {
154 $this->serveMobile = false;
159 foreach ($this->mobiledevices as $md) {
160 if (strstr($httpuseragent, $md) !== false) {
161 $this->setMobileFeatures($httpuseragent);
163 $this->serveMobile = true;
164 $this->reallyMobile = true;
170 // If they are okay with MP, and the site has a mobile server,
172 if ($this->serveMobile &&
173 common_config('site', 'mobileserver') !== false &&
174 (common_config('site', 'mobileserver') !=
175 common_config('site', 'server'))) {
177 // FIXME: Redirect to equivalent page on mobile site instead
178 common_redirect($this->_common_path(''), 302);
182 if (!$this->serveMobile) {
186 // @fixme $type is undefined, making this if case useless and spewing errors.
187 // What's the intent?
189 $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
190 $_SERVER['HTTP_ACCEPT'] : null;
192 $cp = common_accept_to_prefs($httpaccept);
193 $sp = common_accept_to_prefs(PAGE_TYPE_PREFS_MOBILEPROFILE);
195 $type = common_negotiate_type($cp, $sp);
198 // TRANS: Client exception thrown when requesting a not supported media type.
199 throw new ClientException(_m('This page is not available in a '.
200 'media type you accept.'), 406);
204 header('Content-Type: '.$type);
206 if ($this->reallyMobile) {
208 $action->extraHeaders();
209 if (preg_match("/.*\/.*xml/", $type)) {
210 // Required for XML documents
211 $action->xw->startDocument('1.0', 'UTF-8');
213 $action->xw->writeDTD('html',
214 '-//WAPFORUM//DTD XHTML Mobile 1.0//EN',
217 $language = $action->getLanguage();
219 $action->elementStart('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
220 'xml:lang' => $language));
230 function setMobileFeatures($useragent)
232 $mobiledeviceInputFileType = array(
236 $this->mobileFeatures['inputfiletype'] = false;
238 foreach ($mobiledeviceInputFileType as $md) {
239 if (strstr($useragent, $md) !== false) {
240 $this->mobileFeatures['inputfiletype'] = true;
246 function onStartShowStatusNetStyles($action)
248 if (!$this->serveMobile) {
252 $action->primaryCssLink();
254 $action->cssLink($this->path('mp-screen.css'),null,'screen');
255 if (file_exists(Theme::file('css/mp-screen.css'))) {
256 $action->cssLink('css/mp-screen.css', null, 'screen');
259 $action->cssLink($this->path('mp-handheld.css'),null,'handheld');
260 if (file_exists(Theme::file('css/mp-handheld.css'))) {
261 $action->cssLink('css/mp-handheld.css', null, 'handheld');
264 // Allow other plugins to load their styles.
265 Event::handle('EndShowStatusNetStyles', array($action));
266 Event::handle('EndShowLaconicaStyles', array($action));
271 function onStartShowUAStyles($action) {
272 if (!$this->serveMobile) {
279 function onStartShowHeader($action)
281 if (!$this->serveMobile) {
285 $action->elementStart('div', array('id' => 'header'));
286 $this->_showLogo($action);
287 $action->showPrimaryNav();
288 $action->elementEnd('div');
293 function _showLogo($action)
295 $action->elementStart('address', 'vcard');
296 if (common_config('singleuser', 'enabled')) {
297 $user = User::singleUser();
298 $url = common_local_url('showstream', array('nickname' => $user->nickname));
300 $url = common_local_url('public');
303 $action->elementStart('a', array('class' => 'url home bookmark',
306 if (common_config('site', 'mobilelogo') ||
307 file_exists(Theme::file('logo.png')) ||
308 file_exists(Theme::file('mobilelogo.png'))) {
310 $action->element('img', array('class' => 'photo',
311 'src' => (common_config('site', 'mobilelogo')) ? common_config('site', 'mobilelogo') :
312 ((file_exists(Theme::file('mobilelogo.png'))) ? (Theme::path('mobilelogo.png')) : Theme::path('logo.png')),
313 'alt' => common_config('site', 'name')));
315 $action->element('span', array('class' => 'fn org'), common_config('site', 'name'));
316 $action->elementEnd('a');
317 $action->elementEnd('address');
320 function onStartShowAside($action)
322 if ($this->serveMobile) {
327 function onStartShowLocalNavBlock($action)
329 if ($this->serveMobile) {
330 // @todo FIXME: "Show Navigation" / "Hide Navigation" needs i18n
331 $action->element('a', array('href' => '#', 'id' => 'navtoggle'), 'Show Navigation');
336 function onEndShowScripts($action)
338 // @todo FIXME: "Show Navigation" / "Hide Navigation" needs i18n
339 $action->inlineScript('
341 $("#mobile-toggle-disable").click(function() {
342 $.cookie("MobileOverride", "0", {path: "/"});
343 window.location.reload();
346 $("#mobile-toggle-enable").click(function() {
347 $.cookie("MobileOverride", "1", {path: "/"});
348 window.location.reload();
351 $("#navtoggle").click(function () {
352 $("#site_nav_local_views").fadeToggle();
353 var text = $("#navtoggle").text();
354 $("#navtoggle").text(
355 text == "Show Navigation" ? "Hide Navigation" : "Show Navigation");
360 if ($this->serveMobile) {
361 $action->inlineScript('
363 $(".checkbox-wrapper").unbind("click");
372 function onEndShowInsideFooter($action)
374 if ($this->serveMobile) {
375 // TRANS: Link to switch site layout from mobile to desktop mode. Appears at very bottom of page.
376 $linkText = _m('Switch to desktop site layout.');
377 $key = 'mobile-toggle-disable';
379 // TRANS: Link to switch site layout from desktop to mobile mode. Appears at very bottom of page.
380 $linkText = _m('Switch to mobile site layout.');
381 $key = 'mobile-toggle-enable';
383 $action->elementStart('p');
384 $action->element('a', array('href' => '#', 'id' => $key), $linkText);
385 $action->elementEnd('p');
389 function _common_path($relative, $ssl=false)
391 $pathpart = (common_config('site', 'path')) ? common_config('site', 'path')."/" : '';
393 if (($ssl && (common_config('site', 'ssl') === 'sometimes'))
394 || common_config('site', 'ssl') === 'always') {
396 if (is_string(common_config('site', 'sslserver')) &&
397 mb_strlen(common_config('site', 'sslserver')) > 0) {
398 $serverpart = common_config('site', 'sslserver');
400 $serverpart = common_config('site', 'mobileserver');
404 $serverpart = common_config('site', 'mobileserver');
407 return $proto.'://'.$serverpart.'/'.$pathpart.$relative;
410 function onPluginVersion(&$versions)
412 $versions[] = array('name' => 'MobileProfile',
413 'version' => STATUSNET_VERSION,
414 'author' => 'Sarven Capadisli',
415 'homepage' => 'http://status.net/wiki/Plugin:MobileProfile',
417 // TRANS: Plugin description.
418 _m('XHTML MobileProfile output for supporting user agents.'));