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('GNUSOCIAL')) { exit(1); }
32 define('PAGE_TYPE_PREFS_MOBILEPROFILE',
33 'application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html;q=0.9');
35 require_once INSTALLDIR.'/plugins/Mobile/WAP20Plugin.php';
38 * Superclass for plugin to output XHTML Mobile Profile
42 * @author Sarven Capadisli <csarven@status.net>
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 * @link http://status.net/
46 class MobileProfilePlugin extends WAP20Plugin
49 public $serveMobile = false;
50 public $reallyMobile = false;
51 public $mobileFeatures = array();
53 function __construct($DTD='http://www.wapforum.org/DTD/xhtml-mobile10.dtd')
57 parent::__construct();
60 public function onStartShowHTML(Action $action)
62 // TODO: A lot of this should probably graduate to WAP20Plugin
64 $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : null;
66 $cp = common_accept_to_prefs($httpaccept);
67 $sp = common_accept_to_prefs(PAGE_TYPE_PREFS_MOBILEPROFILE);
69 $type = common_negotiate_type($cp, $sp);
72 // TRANS: Client exception thrown when requesting a not supported media type.
73 throw new ClientException(_m('This page is not available in a '.
74 'media type you accept.'), 406);
77 // If they are on the mobile site, serve them MP
78 if ((common_config('site', 'mobileserver').'/'.common_config('site', 'path').'/'
79 == $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'])) {
80 $this->serveMobile = true;
81 } elseif (isset($_COOKIE['MobileOverride'])) {
82 // Cookie override is controlled by link at bottom.
83 $this->serveMobile = (bool)$_COOKIE['MobileOverride'];
84 } elseif (strstr('application/vnd.wap.xhtml+xml', $type) !== false) {
85 // If they like the WAP 2.0 mimetype, serve them MP
86 $this->serveMobile = true;
87 } elseif (array_key_exists('HTTP_USER_AGENT', $_SERVER)) {
88 // If they are a mobile device that supports WAP 2.0,
91 // XXX: Browser sniffing sucks
93 // I really don't like going through this every page,
94 // perhaps use $_SESSION or cookies
96 // May be better to group the devices in terms of
99 // Or, detect the mobile devices based on their support for
100 // MP 1.0, 1.1, or 1.2 may be ideal. Possible?
102 $this->mobiledevices = array(
154 'ipad', // Larger screen handles the full theme fairly well.
157 $httpuseragent = strtolower($_SERVER['HTTP_USER_AGENT']);
159 foreach ($blacklist as $md) {
160 if (strstr($httpuseragent, $md) !== false) {
161 $this->serveMobile = false;
166 foreach ($this->mobiledevices as $md) {
167 if (strstr($httpuseragent, $md) !== false) {
168 $this->setMobileFeatures($httpuseragent);
170 $this->serveMobile = true;
171 $this->reallyMobile = true;
177 if (!$this->serveMobile) {
181 // If they are okay with MP, and the site has a mobile server,
183 if (common_config('site', 'mobileserver') !== false &&
184 common_config('site', 'mobileserver') != common_config('site', 'server')) {
186 // FIXME: Redirect to equivalent page on mobile site instead
187 common_redirect($this->_common_path(''), 302);
190 header('Content-Type: '.$type);
192 if ($this->reallyMobile) {
193 $action->setDTD('html', '-//WAPFORUM//DTD XHTML Mobile 1.0//EN', $this->DTD);
200 function setMobileFeatures($useragent)
202 $mobiledeviceInputFileType = array(
206 $this->mobileFeatures['inputfiletype'] = false;
208 foreach ($mobiledeviceInputFileType as $md) {
209 if (strstr($useragent, $md) !== false) {
210 $this->mobileFeatures['inputfiletype'] = true;
216 public function onStartShowStylesheets(Action $action)
218 if (!$this->serveMobile) {
222 $action->primaryCssLink();
224 $action->cssLink($this->path('mp-screen.css'),null,'screen');
225 if (file_exists(Theme::file('css/mp-screen.css'))) {
226 $action->cssLink('css/mp-screen.css', null, 'screen');
229 $action->cssLink($this->path('mp-handheld.css'),null,'handheld');
230 if (file_exists(Theme::file('css/mp-handheld.css'))) {
231 $action->cssLink('css/mp-handheld.css', null, 'handheld');
234 // Allow other plugins to load their styles.
235 Event::handle('EndShowStylesheets', array($action));
240 public function onStartShowUAStyles(Action $action) {
241 if (!$this->serveMobile) {
248 public function onStartShowHeader(Action $action)
250 if (!$this->serveMobile) {
254 $action->elementStart('div', array('id' => 'header'));
255 $this->_showLogo($action);
256 $action->showPrimaryNav();
257 $action->elementEnd('div');
262 protected function _showLogo(Action $action)
264 $action->elementStart('address');
265 if (common_config('singleuser', 'enabled')) {
266 $user = User::singleUser();
267 $url = common_local_url('showstream', array('nickname' => $user->getNickname()));
269 $url = common_local_url('public');
272 $action->elementStart('a', array('class' => 'h-card home bookmark',
275 if (common_config('site', 'mobilelogo') ||
276 file_exists(Theme::file('logo.png')) ||
277 file_exists(Theme::file('mobilelogo.png'))) {
279 $action->element('img', array('class' => 'u-photo',
280 'src' => (common_config('site', 'mobilelogo')) ? common_config('site', 'mobilelogo') :
281 ((file_exists(Theme::file('mobilelogo.png'))) ? (Theme::path('mobilelogo.png')) : Theme::path('logo.png')),
282 'alt' => common_config('site', 'name')));
284 $action->elementEnd('a');
285 $action->elementEnd('address');
288 public function onStartShowAside(Action $action)
290 if ($this->serveMobile) {
295 public function onStartShowLocalNavBlock(Action $action)
297 if ($this->serveMobile) {
298 // @todo FIXME: "Show Navigation" / "Hide Navigation" needs i18n
299 $action->element('a', array('href' => '#', 'id' => 'navtoggle'), 'Show Navigation');
303 public function onEndShowScripts(Action $action)
305 // @todo FIXME: "Show Navigation" / "Hide Navigation" needs i18n
306 $action->inlineScript('
308 $("#mobile-toggle-disable").click(function() {
309 $.cookie("MobileOverride", "0", {path: "/"});
310 window.location.reload();
313 $("#mobile-toggle-enable").click(function() {
314 $.cookie("MobileOverride", "1", {path: "/"});
315 window.location.reload();
318 $("#navtoggle").click(function () {
319 $("#site_nav_local_views").fadeToggle();
320 var text = $("#navtoggle").text();
321 $("#navtoggle").text(
322 text == "Show Navigation" ? "Hide Navigation" : "Show Navigation");
327 if ($this->serveMobile) {
328 $action->inlineScript('$(function() { $(".checkbox-wrapper").unbind("click"); });');
333 public function onEndShowInsideFooter(Action $action)
335 if ($this->serveMobile) {
336 // TRANS: Link to switch site layout from mobile to desktop mode. Appears at very bottom of page.
337 $linkText = _m('Switch to desktop site layout.');
338 $key = 'mobile-toggle-disable';
340 // TRANS: Link to switch site layout from desktop to mobile mode. Appears at very bottom of page.
341 $linkText = _m('Switch to mobile site layout.');
342 $key = 'mobile-toggle-enable';
344 $action->elementStart('p');
345 $action->element('a', array('href' => '#', 'id' => $key), $linkText);
346 $action->elementEnd('p');
350 function _common_path($relative, $ssl=false)
352 $pathpart = (common_config('site', 'path')) ? common_config('site', 'path')."/" : '';
354 if (($ssl && (common_config('site', 'ssl') === 'sometimes'))
355 || common_config('site', 'ssl') === 'always') {
357 if (is_string(common_config('site', 'sslserver')) &&
358 mb_strlen(common_config('site', 'sslserver')) > 0) {
359 $serverpart = common_config('site', 'sslserver');
361 $serverpart = common_config('site', 'mobileserver');
365 $serverpart = common_config('site', 'mobileserver');
368 return $proto.'://'.$serverpart.'/'.$pathpart.$relative;
371 function onPluginVersion(array &$versions)
373 $versions[] = array('name' => 'MobileProfile',
374 'version' => GNUSOCIAL_VERSION,
375 'author' => 'Sarven Capadisli',
376 'homepage' => 'http://status.net/wiki/Plugin:MobileProfile',
378 // TRANS: Plugin description.
379 _m('XHTML MobileProfile output for supporting user agents.'));