BxMobileAppsModule.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup MobileApps Mobile Apps
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxMobileAppsModule extends BxDolModule
  12. {
  13. function __construct(&$aModule)
  14. {
  15. parent::__construct($aModule);
  16. }
  17. /**
  18. * Get injection code required for mobile apps
  19. * @return string
  20. */
  21. public function serviceInjectionHeadBegin()
  22. {
  23. $s = '';
  24. if (false !== strpos($_SERVER['HTTP_USER_AGENT'], 'UNAMobileApp/Desktop')) {
  25. $s .= "<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>";
  26. }
  27. return $s;
  28. }
  29. /**
  30. * Get injection code required for mobile apps
  31. * @return string with Styles and JS code
  32. */
  33. public function serviceInjectionHead()
  34. {
  35. if (false === strpos($_SERVER['HTTP_USER_AGENT'], 'UNAMobileApp'))
  36. return '';
  37. $this->redirects();
  38. $oMenu = BxDolMenu::getObjectInstance('sys_account_notifications'); // sys_toolbar_member
  39. $a = $oMenu->getMenuItems();
  40. $iBubbles = 0;
  41. $aBubbles = array();
  42. foreach ($a as $r) {
  43. if (!$r['bx_if:addon']['condition'])
  44. continue;
  45. $aBubbles[$r['name']] = $r['bx_if:addon']['content']['addon'];
  46. $iBubbles += $r['bx_if:addon']['content']['addon'];
  47. }
  48. $s = $this->_oTemplate->parseHtmlByName('mobile_apps_styles.html', array());
  49. $s .= $this->_oTemplate->parseHtmlByName('mobile_apps_js.html', array(
  50. 'msg' => json_encode(array(
  51. 'loggedin' => isLogged(),
  52. 'bubbles_num' => $iBubbles ? $iBubbles : '',
  53. 'bubbles' => $aBubbles,
  54. 'push_tags' => array('user' => bx_get_logged_profile_id())
  55. )),
  56. 'txt_pull_to_refresh' => bx_js_string(_t('_bx_mobileapps_pull_to_refresh')),
  57. 'txt_release_to_refresh' => bx_js_string(_t('_bx_mobileapps_release_to_refresh')),
  58. 'txt_refreshing' => bx_js_string(_t('_bx_mobileapps_refreshing')),
  59. ));
  60. $this->_oTemplate->addJs('pulltorefresh.min.js');
  61. if (false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Desktop')) {
  62. $s .= "<script>if (window.module) module = window.module;</script>";
  63. }
  64. return $s;
  65. }
  66. protected function redirects()
  67. {
  68. $bLoggedIn = isLogged() ? 1 : 0;
  69. $i = bx_get('i');
  70. // only several pages are available for guest, for other pages user is redirected to login page
  71. $aI = array('forgot-password' => 1, 'create-account' => 1, 'terms' => 1, 'privacy' => 1, 'contact' => 1, 'about' => 1, 'home' => 1);
  72. if (!$bLoggedIn && !preg_match('/searchKeyword.php$/', $_SERVER['PHP_SELF']) && !preg_match('/member.php$/', $_SERVER['PHP_SELF']) && !isset($aI[$i])) {
  73. header("Location: " . BX_DOL_URL_ROOT);
  74. exit;
  75. }
  76. if ($bLoggedIn) {
  77. if (false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Desktop')) {
  78. // desktop: for logged in members show messenger only
  79. $aI = array('messenger' => 1);
  80. if (!preg_match('/member.php$/', $_SERVER['PHP_SELF']) && !isset($aI[$i])) {
  81. header("Location: " . BX_DOL_URL_ROOT . 'page.php?i=messenger');
  82. exit;
  83. }
  84. }
  85. else {
  86. // mobile: for logged in members some pages aren't available as well
  87. $aI = array('forgot-password' => 1, 'create-account' => 1, 'login' => 1);
  88. if (isset($aI[$i])) {
  89. header("Location: " . BX_DOL_URL_ROOT);
  90. exit;
  91. }
  92. }
  93. }
  94. }
  95. }
  96. /** @} */