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')) {
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(
148 'ipad', // Larger screen handles the full theme fairly well.
151 $httpuseragent = strtolower($_SERVER['HTTP_USER_AGENT']);
153 foreach ($blacklist as $md) {
154 if (strstr($httpuseragent, $md) !== false) {
155 $this->serveMobile = false;
160 foreach ($this->mobiledevices as $md) {
161 if (strstr($httpuseragent, $md) !== false) {
162 $this->setMobileFeatures($httpuseragent);
164 $this->serveMobile = true;
165 $this->reallyMobile = true;
171 // If they are okay with MP, and the site has a mobile server,
173 if ($this->serveMobile &&
174 common_config('site', 'mobileserver') !== false &&
175 (common_config('site', 'mobileserver') !=
176 common_config('site', 'server'))) {
178 // FIXME: Redirect to equivalent page on mobile site instead
179 common_redirect($this->_common_path(''), 302);
183 if (!$this->serveMobile) {
187 // @fixme $type is undefined, making this if case useless and spewing errors.
188 // What's the intent?
190 $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
191 $_SERVER['HTTP_ACCEPT'] : null;
193 $cp = common_accept_to_prefs($httpaccept);
194 $sp = common_accept_to_prefs(PAGE_TYPE_PREFS_MOBILEPROFILE);
196 $type = common_negotiate_type($cp, $sp);
199 // TRANS: Client exception thrown when requesting a not supported media type.
200 throw new ClientException(_m('This page is not available in a '.
201 'media type you accept.'), 406);
205 header('Content-Type: '.$type);
207 if ($this->reallyMobile) {
209 $action->extraHeaders();
210 if (preg_match("/.*\/.*xml/", $type)) {
211 // Required for XML documents
214 $action->xw->writeDTD('html',
215 '-//WAPFORUM//DTD XHTML Mobile 1.0//EN',
218 $language = $action->getLanguage();
220 $action->elementStart('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
221 'xml:lang' => $language));
231 function setMobileFeatures($useragent)
233 $mobiledeviceInputFileType = array(
237 $this->mobileFeatures['inputfiletype'] = false;
239 foreach ($mobiledeviceInputFileType as $md) {
240 if (strstr($useragent, $md) !== false) {
241 $this->mobileFeatures['inputfiletype'] = true;
247 public function onStartShowStylesheets(Action $action)
249 if (!$this->serveMobile) {
253 $action->primaryCssLink();
255 $action->cssLink($this->path('mp-screen.css'),null,'screen');
256 if (file_exists(Theme::file('css/mp-screen.css'))) {
257 $action->cssLink('css/mp-screen.css', null, 'screen');
260 $action->cssLink($this->path('mp-handheld.css'),null,'handheld');
261 if (file_exists(Theme::file('css/mp-handheld.css'))) {
262 $action->cssLink('css/mp-handheld.css', null, 'handheld');
265 // Allow other plugins to load their styles.
266 Event::handle('EndShowStylesheets', 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');
296 if (common_config('singleuser', 'enabled')) {
297 $user = User::singleUser();
298 $url = common_local_url('showstream', array('nickname' => $user->getNickname()));
300 $url = common_local_url('public');
303 $action->elementStart('a', array('class' => 'h-card 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' => 'u-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->elementEnd('a');
316 $action->elementEnd('address');
319 function onStartShowAside($action)
321 if ($this->serveMobile) {
326 function onStartShowLocalNavBlock($action)
328 if ($this->serveMobile) {
329 // @todo FIXME: "Show Navigation" / "Hide Navigation" needs i18n
330 $action->element('a', array('href' => '#', 'id' => 'navtoggle'), 'Show Navigation');
335 function onEndShowScripts($action)
337 // @todo FIXME: "Show Navigation" / "Hide Navigation" needs i18n
338 $action->inlineScript('
340 $("#mobile-toggle-disable").click(function() {
341 $.cookie("MobileOverride", "0", {path: "/"});
342 window.location.reload();
345 $("#mobile-toggle-enable").click(function() {
346 $.cookie("MobileOverride", "1", {path: "/"});
347 window.location.reload();
350 $("#navtoggle").click(function () {
351 $("#site_nav_local_views").fadeToggle();
352 var text = $("#navtoggle").text();
353 $("#navtoggle").text(
354 text == "Show Navigation" ? "Hide Navigation" : "Show Navigation");
359 if ($this->serveMobile) {
360 $action->inlineScript('
362 $(".checkbox-wrapper").unbind("click");
371 function onEndShowInsideFooter($action)
373 if ($this->serveMobile) {
374 // TRANS: Link to switch site layout from mobile to desktop mode. Appears at very bottom of page.
375 $linkText = _m('Switch to desktop site layout.');
376 $key = 'mobile-toggle-disable';
378 // TRANS: Link to switch site layout from desktop to mobile mode. Appears at very bottom of page.
379 $linkText = _m('Switch to mobile site layout.');
380 $key = 'mobile-toggle-enable';
382 $action->elementStart('p');
383 $action->element('a', array('href' => '#', 'id' => $key), $linkText);
384 $action->elementEnd('p');
388 function _common_path($relative, $ssl=false)
390 $pathpart = (common_config('site', 'path')) ? common_config('site', 'path')."/" : '';
392 if (($ssl && (common_config('site', 'ssl') === 'sometimes'))
393 || common_config('site', 'ssl') === 'always') {
395 if (is_string(common_config('site', 'sslserver')) &&
396 mb_strlen(common_config('site', 'sslserver')) > 0) {
397 $serverpart = common_config('site', 'sslserver');
399 $serverpart = common_config('site', 'mobileserver');
403 $serverpart = common_config('site', 'mobileserver');
406 return $proto.'://'.$serverpart.'/'.$pathpart.$relative;
409 function onPluginVersion(&$versions)
411 $versions[] = array('name' => 'MobileProfile',
412 'version' => GNUSOCIAL_VERSION,
413 'author' => 'Sarven Capadisli',
414 'homepage' => 'http://status.net/wiki/Plugin:MobileProfile',
416 // TRANS: Plugin description.
417 _m('XHTML MobileProfile output for supporting user agents.'));