MobileProfilePlugin.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. define('PAGE_TYPE_PREFS_MOBILEPROFILE',
  33. 'application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html;q=0.9');
  34. require_once INSTALLDIR.'/plugins/Mobile/WAP20Plugin.php';
  35. /**
  36. * Superclass for plugin to output XHTML Mobile Profile
  37. *
  38. * @category Plugin
  39. * @package StatusNet
  40. * @author Sarven Capadisli <csarven@status.net>
  41. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  42. * @link http://status.net/
  43. */
  44. class MobileProfilePlugin extends WAP20Plugin
  45. {
  46. public $DTD = null;
  47. public $serveMobile = false;
  48. public $reallyMobile = false;
  49. public $mobileFeatures = array();
  50. function __construct($DTD='http://www.wapforum.org/DTD/xhtml-mobile10.dtd')
  51. {
  52. $this->DTD = $DTD;
  53. parent::__construct();
  54. }
  55. function onStartShowHTML($action)
  56. {
  57. // XXX: This should probably graduate to WAP20Plugin
  58. // If they are on the mobile site, serve them MP
  59. if ((common_config('site', 'mobileserver').'/'.
  60. common_config('site', 'path').'/' ==
  61. $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'])) {
  62. $this->serveMobile = true;
  63. } else if (isset($_COOKIE['MobileOverride'])) {
  64. // Cookie override is controlled by link at bottom.
  65. $this->serveMobile = (bool)$_COOKIE['MobileOverride'];
  66. } elseif (array_key_exists('HTTP_USER_AGENT', $_SERVER)) {
  67. // If they like the WAP 2.0 mimetype, serve them MP
  68. // @fixme $type is undefined, making this if case useless and spewing errors.
  69. // What's the intent?
  70. //if (strstr('application/vnd.wap.xhtml+xml', $type) !== false) {
  71. // $this->serveMobile = true;
  72. //} else {
  73. // If they are a mobile device that supports WAP 2.0,
  74. // serve them MP
  75. // XXX: Browser sniffing sucks
  76. // I really don't like going through this every page,
  77. // perhaps use $_SESSION or cookies
  78. // May be better to group the devices in terms of
  79. // low,mid,high-end
  80. // Or, detect the mobile devices based on their support for
  81. // MP 1.0, 1.1, or 1.2 may be ideal. Possible?
  82. $this->mobiledevices = array(
  83. 'alcatel',
  84. 'android',
  85. 'audiovox',
  86. 'au-mic,',
  87. 'avantgo',
  88. 'blackberry',
  89. 'blazer',
  90. 'cldc-',
  91. 'danger',
  92. 'epoc',
  93. 'ericsson',
  94. 'ericy',
  95. 'iphone',
  96. 'ipaq',
  97. 'ipod',
  98. 'j2me',
  99. 'lg',
  100. 'maemo',
  101. 'midp-',
  102. 'mobile',
  103. 'mot',
  104. 'netfront',
  105. 'nitro',
  106. 'nokia',
  107. 'opera mini',
  108. 'palm',
  109. 'palmsource',
  110. 'panasonic',
  111. 'philips',
  112. 'pocketpc',
  113. 'portalmmm',
  114. 'rover',
  115. 'samsung',
  116. 'sanyo',
  117. 'series60',
  118. 'sharp',
  119. 'sie-',
  120. 'smartphone',
  121. 'sony',
  122. 'symbian',
  123. 'up.browser',
  124. 'up.link',
  125. 'up.link',
  126. 'vodafone',
  127. 'wap1',
  128. 'wap2',
  129. 'webos',
  130. 'windows ce'
  131. );
  132. $blacklist = array(
  133. 'ipad', // Larger screen handles the full theme fairly well.
  134. );
  135. $httpuseragent = strtolower($_SERVER['HTTP_USER_AGENT']);
  136. foreach ($blacklist as $md) {
  137. if (strstr($httpuseragent, $md) !== false) {
  138. $this->serveMobile = false;
  139. return true;
  140. }
  141. }
  142. foreach ($this->mobiledevices as $md) {
  143. if (strstr($httpuseragent, $md) !== false) {
  144. $this->setMobileFeatures($httpuseragent);
  145. $this->serveMobile = true;
  146. $this->reallyMobile = true;
  147. break;
  148. }
  149. }
  150. //}
  151. // If they are okay with MP, and the site has a mobile server,
  152. // redirect there
  153. if ($this->serveMobile &&
  154. common_config('site', 'mobileserver') !== false &&
  155. (common_config('site', 'mobileserver') !=
  156. common_config('site', 'server'))) {
  157. // FIXME: Redirect to equivalent page on mobile site instead
  158. common_redirect($this->_common_path(''), 302);
  159. }
  160. }
  161. if (!$this->serveMobile) {
  162. return true;
  163. }
  164. // @fixme $type is undefined, making this if case useless and spewing errors.
  165. // What's the intent?
  166. //if (!$type) {
  167. $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
  168. $_SERVER['HTTP_ACCEPT'] : null;
  169. $cp = common_accept_to_prefs($httpaccept);
  170. $sp = common_accept_to_prefs(PAGE_TYPE_PREFS_MOBILEPROFILE);
  171. $type = common_negotiate_type($cp, $sp);
  172. if (!$type) {
  173. // TRANS: Client exception thrown when requesting a not supported media type.
  174. throw new ClientException(_m('This page is not available in a '.
  175. 'media type you accept.'), 406);
  176. }
  177. //}
  178. header('Content-Type: '.$type);
  179. if ($this->reallyMobile) {
  180. $action->extraHeaders();
  181. if (preg_match("/.*\/.*xml/", $type)) {
  182. // Required for XML documents
  183. $action->startXML();
  184. }
  185. $action->xw->writeDTD('html',
  186. '-//WAPFORUM//DTD XHTML Mobile 1.0//EN',
  187. $this->DTD);
  188. $language = $action->getLanguage();
  189. $action->elementStart('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
  190. 'xml:lang' => $language));
  191. return false;
  192. } else {
  193. return true;
  194. }
  195. }
  196. function setMobileFeatures($useragent)
  197. {
  198. $mobiledeviceInputFileType = array(
  199. 'nokia'
  200. );
  201. $this->mobileFeatures['inputfiletype'] = false;
  202. foreach ($mobiledeviceInputFileType as $md) {
  203. if (strstr($useragent, $md) !== false) {
  204. $this->mobileFeatures['inputfiletype'] = true;
  205. break;
  206. }
  207. }
  208. }
  209. public function onStartShowStylesheets(Action $action)
  210. {
  211. if (!$this->serveMobile) {
  212. return true;
  213. }
  214. $action->primaryCssLink();
  215. $action->cssLink($this->path('mp-screen.css'),null,'screen');
  216. if (file_exists(Theme::file('css/mp-screen.css'))) {
  217. $action->cssLink('css/mp-screen.css', null, 'screen');
  218. }
  219. $action->cssLink($this->path('mp-handheld.css'),null,'handheld');
  220. if (file_exists(Theme::file('css/mp-handheld.css'))) {
  221. $action->cssLink('css/mp-handheld.css', null, 'handheld');
  222. }
  223. // Allow other plugins to load their styles.
  224. Event::handle('EndShowStylesheets', array($action));
  225. return false;
  226. }
  227. function onStartShowUAStyles($action) {
  228. if (!$this->serveMobile) {
  229. return true;
  230. }
  231. return false;
  232. }
  233. function onStartShowHeader($action)
  234. {
  235. if (!$this->serveMobile) {
  236. return true;
  237. }
  238. $action->elementStart('div', array('id' => 'header'));
  239. $this->_showLogo($action);
  240. $action->showPrimaryNav();
  241. $action->elementEnd('div');
  242. return false;
  243. }
  244. function _showLogo($action)
  245. {
  246. $action->elementStart('address');
  247. if (common_config('singleuser', 'enabled')) {
  248. $user = User::singleUser();
  249. $url = common_local_url('showstream', array('nickname' => $user->getNickname()));
  250. } else {
  251. $url = common_local_url('public');
  252. }
  253. $action->elementStart('a', array('class' => 'h-card home bookmark',
  254. 'href' => $url));
  255. if (common_config('site', 'mobilelogo') ||
  256. file_exists(Theme::file('logo.png')) ||
  257. file_exists(Theme::file('mobilelogo.png'))) {
  258. $action->element('img', array('class' => 'u-photo',
  259. 'src' => (common_config('site', 'mobilelogo')) ? common_config('site', 'mobilelogo') :
  260. ((file_exists(Theme::file('mobilelogo.png'))) ? (Theme::path('mobilelogo.png')) : Theme::path('logo.png')),
  261. 'alt' => common_config('site', 'name')));
  262. }
  263. $action->elementEnd('a');
  264. $action->elementEnd('address');
  265. }
  266. function onStartShowAside($action)
  267. {
  268. if ($this->serveMobile) {
  269. return false;
  270. }
  271. }
  272. function onStartShowLocalNavBlock($action)
  273. {
  274. if ($this->serveMobile) {
  275. // @todo FIXME: "Show Navigation" / "Hide Navigation" needs i18n
  276. $action->element('a', array('href' => '#', 'id' => 'navtoggle'), 'Show Navigation');
  277. return true;
  278. }
  279. }
  280. function onEndShowScripts($action)
  281. {
  282. // @todo FIXME: "Show Navigation" / "Hide Navigation" needs i18n
  283. $action->inlineScript('
  284. $(function() {
  285. $("#mobile-toggle-disable").click(function() {
  286. $.cookie("MobileOverride", "0", {path: "/"});
  287. window.location.reload();
  288. return false;
  289. });
  290. $("#mobile-toggle-enable").click(function() {
  291. $.cookie("MobileOverride", "1", {path: "/"});
  292. window.location.reload();
  293. return false;
  294. });
  295. $("#navtoggle").click(function () {
  296. $("#site_nav_local_views").fadeToggle();
  297. var text = $("#navtoggle").text();
  298. $("#navtoggle").text(
  299. text == "Show Navigation" ? "Hide Navigation" : "Show Navigation");
  300. });
  301. });'
  302. );
  303. if ($this->serveMobile) {
  304. $action->inlineScript('
  305. $(function() {
  306. $(".checkbox-wrapper").unbind("click");
  307. });'
  308. );
  309. }
  310. }
  311. function onEndShowInsideFooter($action)
  312. {
  313. if ($this->serveMobile) {
  314. // TRANS: Link to switch site layout from mobile to desktop mode. Appears at very bottom of page.
  315. $linkText = _m('Switch to desktop site layout.');
  316. $key = 'mobile-toggle-disable';
  317. } else {
  318. // TRANS: Link to switch site layout from desktop to mobile mode. Appears at very bottom of page.
  319. $linkText = _m('Switch to mobile site layout.');
  320. $key = 'mobile-toggle-enable';
  321. }
  322. $action->elementStart('p');
  323. $action->element('a', array('href' => '#', 'id' => $key), $linkText);
  324. $action->elementEnd('p');
  325. return true;
  326. }
  327. function _common_path($relative, $ssl=false)
  328. {
  329. $pathpart = (common_config('site', 'path')) ? common_config('site', 'path')."/" : '';
  330. if (($ssl && (common_config('site', 'ssl') === 'sometimes'))
  331. || common_config('site', 'ssl') === 'always') {
  332. $proto = 'https';
  333. if (is_string(common_config('site', 'sslserver')) &&
  334. mb_strlen(common_config('site', 'sslserver')) > 0) {
  335. $serverpart = common_config('site', 'sslserver');
  336. } else {
  337. $serverpart = common_config('site', 'mobileserver');
  338. }
  339. } else {
  340. $proto = 'http';
  341. $serverpart = common_config('site', 'mobileserver');
  342. }
  343. return $proto.'://'.$serverpart.'/'.$pathpart.$relative;
  344. }
  345. function onPluginVersion(&$versions)
  346. {
  347. $versions[] = array('name' => 'MobileProfile',
  348. 'version' => GNUSOCIAL_VERSION,
  349. 'author' => 'Sarven Capadisli',
  350. 'homepage' => 'http://status.net/wiki/Plugin:MobileProfile',
  351. 'rawdescription' =>
  352. // TRANS: Plugin description.
  353. _m('XHTML MobileProfile output for supporting user agents.'));
  354. return true;
  355. }
  356. }