MobileProfilePlugin.php 13 KB

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