1
0

BxNexusModule.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 Nexus Nexus - Mobile Apps and Desktop apps connector
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxNexusModule extends BxDolModule
  12. {
  13. function __construct(&$aModule)
  14. {
  15. parent::__construct($aModule);
  16. }
  17. /**
  18. * Get injection code required for Nexus 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 Nexus apps
  31. * @return string with Styles and JS code
  32. */
  33. public function serviceInjectionHead()
  34. {
  35. if (!isset($_SERVER['HTTP_USER_AGENT']) || false === strpos($_SERVER['HTTP_USER_AGENT'], 'UNAMobileApp'))
  36. return '';
  37. $this->redirects();
  38. $aBubbles = array();
  39. $iBubbles = BxDolPush::getNotificationsCount(0, $aBubbles);
  40. $s = $this->_oTemplate->parseHtmlByName('styles.html', array());
  41. if ($sCustomStyles = getParam('bx_nexus_option_styles'))
  42. $s .= "<style>\n" . $sCustomStyles . "\n</style>";
  43. $oProfile = BxDolProfile::getInstance();
  44. $s .= $this->_oTemplate->parseHtmlByName('js.html', array(
  45. 'msg' => json_encode(array(
  46. 'loggedin' => isLogged(),
  47. 'user_info' => isLogged() && $oProfile ? array (
  48. 'displayName' => $oProfile->getDisplayName(),
  49. 'email' => $oProfile->getAccountObject()->getEmail(),
  50. 'avatar' => $oProfile->getAvatar(),
  51. 'id' => $oProfile->id(),
  52. 'module' => $oProfile->getModule(),
  53. ) : false,
  54. 'bubbles_num' => $iBubbles ? $iBubbles : '',
  55. 'bubbles' => $aBubbles,
  56. 'push_tags' => BxDolPush::getTags(),
  57. )),
  58. 'txt_pull_to_refresh' => bx_js_string(_t('_bx_nexus_pull_to_refresh')),
  59. 'txt_release_to_refresh' => bx_js_string(_t('_bx_nexus_release_to_refresh')),
  60. 'txt_refreshing' => bx_js_string(_t('_bx_nexus_refreshing')),
  61. 'main_menu' => getParam('bx_nexus_option_main_menu'),
  62. ));
  63. // TODO: enable it back when it will work correctly on Android
  64. // $this->_oTemplate->addJs('pulltorefresh.min.js');
  65. if (false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Desktop')) {
  66. $s .= "<script>if (window.module) module = window.module;</script>";
  67. }
  68. return $s;
  69. }
  70. public function serviceGetMenusList()
  71. {
  72. $a = BxDolMenuQuery::getMenuObjects();
  73. array_unshift($a, array('object' => 'default', 'title' => '_sys_page_type_default', 'module' => '', 'uri' => ''));
  74. $aRes = array();
  75. foreach($a as $r) {
  76. $aRes[] = array('key' => $r['object'], 'value' => $r['uri'] ? _t('_bx_nexus_option_main_menu_value', BxDolModule::getTitle($r['uri']), _t($r['title'])) : _t($r['title']));
  77. }
  78. return $aRes;
  79. }
  80. protected function redirects()
  81. {
  82. $bLoggedIn = isLogged() ? 1 : 0;
  83. $i = bx_get('i');
  84. // only several pages are available for guest, for other pages user is redirected to login page
  85. $aURIs = explode(',', getParam('bx_nexus_option_guest_pages'));
  86. array_walk($aURIs, function (&$sVal) {
  87. $sVal = trim($sVal);
  88. });
  89. $aI = array_combine($aURIs, array_fill(0, count($aURIs), 1));
  90. if (!$bLoggedIn && !preg_match('/\/oauth2\//', $_SERVER['REQUEST_URI']) && !preg_match('/searchKeyword.php$/', $_SERVER['PHP_SELF']) && !preg_match('/member.php$/', $_SERVER['PHP_SELF']) && !isset($aI[$i])) {
  91. header("Location: " . BX_DOL_URL_ROOT);
  92. exit;
  93. }
  94. if ($bLoggedIn) {
  95. if (false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Desktop')) {
  96. // desktop: for logged in members show messenger only
  97. $aI = array('messenger' => 1);
  98. if (!preg_match('/member.php$/', $_SERVER['PHP_SELF']) && !isset($aI[$i])) {
  99. header("Location: " . BX_DOL_URL_ROOT . 'page.php?i=messenger');
  100. exit;
  101. }
  102. }
  103. else {
  104. // mobile: for logged in members some pages aren't available as well
  105. $aI = array('forgot-password' => 1, 'create-account' => 1, 'login' => 1);
  106. if (isset($aI[$i])) {
  107. header("Location: " . BX_DOL_URL_ROOT);
  108. exit;
  109. }
  110. }
  111. }
  112. }
  113. }
  114. /** @} */