BxNexusModule.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 (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. ) : false,
  52. 'bubbles_num' => $iBubbles ? $iBubbles : '',
  53. 'bubbles' => $aBubbles,
  54. 'push_tags' => BxDolPush::getTags(),
  55. )),
  56. 'txt_pull_to_refresh' => bx_js_string(_t('_bx_nexus_pull_to_refresh')),
  57. 'txt_release_to_refresh' => bx_js_string(_t('_bx_nexus_release_to_refresh')),
  58. 'txt_refreshing' => bx_js_string(_t('_bx_nexus_refreshing')),
  59. ));
  60. // TODO: enable it back when it will work correctly on Android
  61. // $this->_oTemplate->addJs('pulltorefresh.min.js');
  62. if (false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Desktop')) {
  63. $s .= "<script>if (window.module) module = window.module;</script>";
  64. }
  65. return $s;
  66. }
  67. protected function redirects()
  68. {
  69. $bLoggedIn = isLogged() ? 1 : 0;
  70. $i = bx_get('i');
  71. // only several pages are available for guest, for other pages user is redirected to login page
  72. $aI = array('forgot-password' => 1, 'create-account' => 1, 'terms' => 1, 'privacy' => 1, 'contact' => 1, 'about' => 1, 'home' => 1);
  73. 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])) {
  74. header("Location: " . BX_DOL_URL_ROOT);
  75. exit;
  76. }
  77. if ($bLoggedIn) {
  78. if (false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Desktop')) {
  79. // desktop: for logged in members show messenger only
  80. $aI = array('messenger' => 1);
  81. if (!preg_match('/member.php$/', $_SERVER['PHP_SELF']) && !isset($aI[$i])) {
  82. header("Location: " . BX_DOL_URL_ROOT . 'page.php?i=messenger');
  83. exit;
  84. }
  85. }
  86. else {
  87. // mobile: for logged in members some pages aren't available as well
  88. $aI = array('forgot-password' => 1, 'create-account' => 1, 'login' => 1);
  89. if (isset($aI[$i])) {
  90. header("Location: " . BX_DOL_URL_ROOT);
  91. exit;
  92. }
  93. }
  94. }
  95. }
  96. }
  97. /** @} */