MobileProfilePlugin.php 13 KB

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