Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mobile.php
1<?php
2
3namespace Bitrix\MobileApp;
4
9
10class Mobile
11{
12 public static $platform = "ios";
13 public static $apiVersion = 1;
14 public static $pgVersion = "2.0.0";
15 public static $supportedCordovaVersion = "3.6.3";
16 public static $isDev = false;
17 protected static $instance;
18 protected static $isAlreadyInit = false;
19 protected static $systemVersion = 9;
20 private $pixelRatio = 1.0;
21 private $minScale = false;
22 private $iniScale = false;
23 private $maxScale = false;
24 private $scale = 1.2;
25 private $width = false;
26 private $userScalable = "no";
27 private $deviceWidth = 320;
28 private $deviceHeight = 480;
29 private $screenCategory = "NORMAL";
30 private $device = "";
31 private $largeScreenSupport = true;
32 private $isWebRtcSupported = false;
33 private $isBXScriptSupported = false;
34
35 private function __construct()
36 {
37 global $APPLICATION;
38
39 $this->setDeviceWidth($_COOKIE["MOBILE_RESOLUTION_WIDTH"] ?? null);
40 $this->setDeviceHeight($_COOKIE["MOBILE_RESOLUTION_HEIGHT"] ?? null);
41 $this->setPixelratio($_COOKIE["MOBILE_SCALE"] ?? null);
42 $this->screenCategory = $_COOKIE["MOBILE_SCREEN_CATEGORY"] ?? null;
43
44 if (!empty($_COOKIE["PG_VERSION"]))
45 {
46 self::$pgVersion = $_COOKIE["PG_VERSION"];
47 }
48
49 self::$isDev = (isset($_COOKIE["MOBILE_DEV"]) && $_COOKIE["MOBILE_DEV"] == "Y");
50 $this->device = $_COOKIE["MOBILE_DEVICE"] ?? null;
51 if (!empty($_COOKIE["IS_WEBRTC_SUPPORTED"]) && $_COOKIE["IS_WEBRTC_SUPPORTED"] == "Y")
52 {
53 $this->setWebRtcSupport(true);
54 }
55 if (!empty($_COOKIE["IS_BXSCRIPT_SUPPORTED"]) && $_COOKIE["IS_BXSCRIPT_SUPPORTED"] == "Y")
56 {
57 $this->setBXScriptSupported(true);
58 }
59
60 if ($this->getDevice() == "iPad")
61 {
62 $this->setScreenCategory("LARGE");
63 if (intval($this->getPixelRatio()) == 2) //retina hack
64 {
65 $this->setDeviceWidth($_COOKIE["MOBILE_RESOLUTION_WIDTH"] / 2);
66 $this->setDeviceHeight($_COOKIE["MOBILE_RESOLUTION_HEIGHT"] / 2);
67 }
68 }
69
70 //detecting OS
71 if (array_key_exists("MOBILE_DEVICE", $_COOKIE))
72 {
73 $deviceDetectSource = $_COOKIE["MOBILE_DEVICE"];
74 }
75 else
76 {
77 $deviceDetectSource = mb_strtolower(Context::getCurrent()->getServer()->get("HTTP_USER_AGENT"));
78 }
79
80 if (mb_strrpos(ToUpper($deviceDetectSource), "IPHONE") > 0 || mb_strrpos(ToUpper($deviceDetectSource), "IPAD") > 0)
81 {
82 self::$platform = "ios";
83 }
84 elseif (mb_strrpos(ToUpper($deviceDetectSource), "ANDROID") > 0 || mb_strrpos(ToUpper($deviceDetectSource), "ANDROID") === 0)
85 {
86 self::$platform = "android";
87 }
88
89 $userAgent = \Bitrix\Main\Context::getCurrent()->getServer()->get("HTTP_USER_AGENT");
90 if (!empty($_COOKIE["MOBILE_SYSTEM_VERSION"]))
91 {
92 self::$systemVersion = $_COOKIE["MOBILE_SYSTEM_VERSION"];
93 }
94 else
95 {
96 //iOS
97 preg_match("/iOS\s(\d+\.\d+)/i",$userAgent, $pregMatch);
98 if(count($pregMatch) == 2)
99 self::$systemVersion = floatval($pregMatch[1]);
100 }
101
102 if (array_key_exists("emulate_platform", $_REQUEST))
103 {
104 self::$platform = $_REQUEST["emulate_platform"];
105 }
106
107 if ($mobileTZ = $APPLICATION->get_cookie("TZ", "MOBILE")) {
108 $tz = $APPLICATION->get_cookie("TZ");
109 if ($tz != $mobileTZ) {
110 $cookie = new \Bitrix\Main\Web\Cookie("TZ", $mobileTZ, time() + 60 * 60 * 24 * 30 * 12);
111 \Bitrix\Main\Context::getCurrent()->getResponse()->addCookie($cookie);
112 }
113
114 }
115
116 if (array_key_exists("MOBILE_API_VERSION", $_COOKIE))
117 {
118 self::$apiVersion = $_COOKIE["MOBILE_API_VERSION"];
119 }
120 elseif ($APPLICATION->get_cookie("MOBILE_APP_VERSION"))
121 {
122 self::$apiVersion = $APPLICATION->get_cookie("MOBILE_APP_VERSION");
123 }
124 elseif (array_key_exists("api_version", $_REQUEST))
125 {
126 self::$apiVersion = $_REQUEST["api_version"];
127 }
128 else
129 {
130 preg_match("/(?<=BitrixMobile\/Version=).*\d/i",$userAgent, $pregMatch);
131
132 if(count($pregMatch) == 1)
133 {
134 self::$apiVersion = $pregMatch[0];
135 }
136 }
137
138 self::$apiVersion = intval(self::$apiVersion);
139 }
140
144 public function setWebRtcSupport($isWebRtcSupported)
145 {
146 $this->isWebRtcSupported = $isWebRtcSupported;
147 }
148
152 public function setBXScriptSupported($isBXScriptSupported)
153 {
154 $this->isBXScriptSupported = $isBXScriptSupported;
155 }
156
161 public function getDevice()
162 {
163 return $this->device;
164 }
165
171 public function getPixelRatio()
172 {
173 return $this->pixelRatio;
174 }
175
181 public function setPixelRatio($pixelRatio)
182 {
183 $this->pixelRatio = $pixelRatio;
184 }
185
190 public static function isAppBackground()
191 {
192 $isBackground = Context::getCurrent()->getServer()->get("HTTP_BX_MOBILE_BACKGROUND");
193 return ($isBackground === "true");
194 }
195
199 public static function Init()
200 {
201 self::getInstance()->_Init();
202 }
203
207 protected function _Init()
208 {
209 if (self::$isAlreadyInit)
210 {
211 return;
212 }
213
214 header("BX-Cordova-Version: " . self::$supportedCordovaVersion);
215 $GLOBALS["BITRIX_PLATFORM"] = self::$platform;
216 $GLOBALS["BITRIX_API_VERSION"] = self::$apiVersion;
217
218 AddEventHandler("main", "OnBeforeEndBufferContent", Array(__CLASS__, "initScripts"));
219 AddEventHandler("main", "OnEpilog", Array($this, "onMobileInit"));
220 self::$isAlreadyInit = true;
221 }
222
226 public static function getInstance()
227 {
228 if (is_null(self::$instance))
229 {
230 self::$instance = new Mobile();
231 }
232
233 return self::$instance;
234 }
235
239 public static function initScripts()
240 {
241 global $APPLICATION, $USER;
242
243 \CJSCore::Init();
244 \CJSCore::Init("db");
245 $jsVarsFormat = <<<JSCODE
246 <script type="text/javascript">
247 (window.BX||top.BX).message({ 'USER_ID': '%s'});
248 var appVersion = "%s";
249 var platform = "%s";
250 var mobileSiteDir = "%s";
251 </script>
252
253JSCODE;
254
255 $APPLICATION->AddHeadString(
256 sprintf($jsVarsFormat,
257 $USER->getId(),
258 self::$apiVersion,
259 self::$platform,
260 SITE_DIR
261 ), false, true);
262
263 if (self::$platform == "android")
264 {
269 $androidJS = <<<JSCODE
270 <script type="text/javascript">
271 console.log("bxdata://success");
272 </script>
273JSCODE;
274
275 $APPLICATION->AddHeadString($androidJS, false, true);
276 }
277 $userAgent = \Bitrix\Main\Context::getCurrent()->getServer()->get("HTTP_USER_AGENT");
278 if(mb_strpos($userAgent, "WKWebView/BitrixMobile") === false)
279 {
280 $pgJsFile = "/bitrix/js/mobileapp/__deviceload__/cordova.js?mod=1";
281 $APPLICATION->AddHeadString("<script type='text/javascript' src='$pgJsFile'></script>", false, true);
282 }
283
284
285 $APPLICATION->AddHeadString("<script type=\"text/javascript\" src=\"" . \CUtil::GetAdditionalFileURL("/bitrix/js/mobileapp/bitrix_mobile.js") . "\"></script>", false, true);
286 $APPLICATION->AddHeadString("<script type=\"text/javascript\" src=\"" . \CUtil::GetAdditionalFileURL("/bitrix/js/mobileapp/mobile_lib.js") . "\"></script>", false, true);
287
288
289 if (self::$platform == "android")
290 {
291 $APPLICATION->AddHeadString("<script type=\"text/javascript\">app.bindloadPageBlank();</script>", false, false);
292 }
293
294 if (!array_key_exists("doNotUseViewPort", $_REQUEST))
295 {
296 $APPLICATION->AddHeadString(Mobile::getInstance()->getViewPort());
297 }
298
299 \Bitrix\Main\UI\Extension::load([
300 'mobile.ajax'
301 ]);
302 }
303
307 public function getBXScriptSupported()
308 {
309 return $this->isBXScriptSupported;
310 }
311
319 public function getViewPort($width = "")
320 {
321
322 if ($width == "")
323 {
324 $width = $this->getDevicewidth();
325 }
326
327 if ($this->largeScreenSupport == true)
328 {
329 //we need to change densitydpi for large screens
330 if ($this->getDevice() == "iPad")
331 {
332 //ipad large screen setting
333 return $this->getIPadViewPort();
334
335 }
336 elseif (($this->getScreenCategory() == "LARGE" || $this->getScreenCategory() == "XLARGE"))
337 {
338 //android large screen setting
339 return $this->getLargeScreenViewPort();
340 }
341 }
342
343 $viewPortMeta = "<meta id=\"bx_mobile_viewport\" name=\"viewport\" content=\"#content_value#\">";
344 if ($this->getIniscale())
345 {
346 $contentAttributes[] = "initial-scale=" . $this->getIniscale();
347 }
348 if ($this->getMaxscale())
349 {
350 $contentAttributes[] = "maximum-scale=" . $this->getMaxscale();
351 }
352 if ($this->getMinscale())
353 {
354 $contentAttributes[] = "minimum-scale=" . $this->getMinscale();
355 }
356
357
358 if ($this->getWidth())
359 {
360 $contentAttributes[] = "width=" . $this->getWidth();
361 }
362 elseif ($this->getIniscale())
363 {
364 $contentAttributes[] = "width=" . ($width / $this->getIniscale());
365 }
366
367 if (!$this->getWidth())
368 {
369 $contentAttributes[] = "width=device-width";
370 }
371
372
373 $contentAttributes[] = "user-scalable=" . $this->getUserScalable();
374 $contentAttributes[] = "viewport-fit=cover";
375
376 return str_replace("#content_value#", implode(", ", $contentAttributes), $viewPortMeta);
377 }
378
384 public function getDeviceWidth()
385 {
386 return $this->deviceWidth;
387 }
388
394 public function setDeviceWidth($deviceWidth)
395 {
396 $this->deviceWidth = $deviceWidth;
397 }
398
406 public function getIPadViewPort($width = 320)
407 {
408 if ($width == false)
409 {
410 $width = $this->getDevicewidth();
411 }
412 $viewPortMeta = "<meta id=\"bx_mobile_viewport\" name=\"viewport\" content=\"#content_value#\">";
413 $contentAttributes = Array(
414 "width=" . ($width / $this->scale),
415 "user-scalable=no"
416 );
417 $content = implode(", ", $contentAttributes);
418
419 return str_replace("#content_value#", $content, $viewPortMeta);
420 }
421
427 public function getScreenCategory()
428 {
429 return $this->screenCategory;
430 }
431
437 public function setScreenCategory($screenCategory)
438 {
439 $this->screenCategory = $screenCategory;
440 }
441
445 public function getLargeScreenViewPort()
446 {
447 return "<meta id=\"bx_mobile_viewport\" name=\"viewport\" content=\"user-scalable=no width=device-width target-densitydpi=" . $this->getTargetDpi() . "\">";
448 }
449
455 public function getTargetDpi()
456 {
457 $targetDpi = "medium-dpi";
458 if ($this->getDevice() == "iPad")
459 {
460 return $targetDpi;
461 }
462 switch ($this->getScreenCategory())
463 {
464 case 'NORMAL':
465 $targetDpi = "medium-dpi";
466 break;
467 case 'LARGE':
468 $targetDpi = "low-dpi";
469 break;
470 case 'XLARGE':
471 $targetDpi = "low-dpi";
472 break;
473 case 'SMALL':
474 $targetDpi = "medium-dpi";
475 break;
476 default:
477 $targetDpi = "medium-dpi";
478 break;
479 }
480
481 return $targetDpi;
482 }
483
489 public function getIniScale()
490 {
491 return $this->iniScale;
492 }
493
499 public function setIniScale($iniScale)
500 {
501 $this->iniScale = $iniScale;
502 }
503
509 public function getMaxScale()
510 {
511 return $this->maxScale;
512 }
513
519 public function setMaxScale($maxScale)
520 {
521 $this->maxScale = $maxScale;
522 }
523
529 public function getMinScale()
530 {
531 return $this->minScale;
532 }
533
539 public function setMinScale($minScale)
540 {
541 $this->minScale = $minScale;
542 }
543
548 public function getWidth()
549 {
550 return $this->width;
551 }
552
556 public function setWidth($width)
557 {
558 $this->width = $width;
559 }
560
565 public static function getPlatform()
566 {
567 return self::$platform;
568 }
569
574 public function getUserScalable()
575 {
576 return $this->userScalable;
577 }
578
583 public function setUserScalable($userScalable)
584 {
585 $this->userScalable = ($userScalable === false ? "no" : "yes");
586 }
587
595 public static function PrepareStrToJson($s = '')
596 {
597 return (Application::isUtfMode() ? $s : Encoding::convertEncoding($s, SITE_CHARSET, 'UTF-8'));
598 }
599
607 public static function ConvertFromUtf($s = '')
608 {
609 return (defined("BX_UTF") ? $s : Encoding::convertEncoding($s, 'UTF-8', SITE_CHARSET));
610 }
611
612 public static function onMobileInit()
613 {
614 if (!defined("MOBILE_INIT_EVENT_SKIP"))
615 {
616 $db_events = getModuleEvents("mobileapp", "OnMobileInit");
617 while ($arEvent = $db_events->Fetch())
618 {
619 ExecuteModuleEventEx($arEvent);
620 }
621 }
622 }
623
629 public static function getApiVersion()
630 {
631 return self::$apiVersion;
632 }
633
638 public static function getPgVersion()
639 {
640 return self::$pgVersion;
641 }
642
646 public static function getSystemVersion()
647 {
648 return (float) self::$systemVersion;
649 }
650
654 public function isWebRtcSupported()
655 {
656 return $this->isWebRtcSupported;
657 }
658
664 public function getViewPortPortrait()
665 {
666 return $this->getViewPort($this->deviceWidth);
667 }
668
674 public function getViewPortLandscape()
675 {
676 return $this->getViewPort($this->deviceHeight);
677 }
678
684 public function getDeviceHeight()
685 {
686 return $this->deviceHeight;
687 }
688
694 public function setDeviceHeight($deviceHeight)
695 {
696 $this->deviceHeight = $deviceHeight;
697 }
698
704 public function getLargeScreenSupport()
705 {
706 return $this->largeScreenSupport;
707 }
708
714 public function setLargeScreenSupport($largeScreenSupport)
715 {
716 $this->largeScreenSupport = $largeScreenSupport;
717 }
718
724 public function getScale()
725 {
726 return $this->scale;
727 }
728
735 public function setScale($scale)
736 {
737 $this->scale = $scale;
738 }
739
744 public function isLarge()
745 {
746 return ($this->getScreenCategory() == "LARGE" || $this->getScreenCategory() == "XLARGE");
747 }
748
749 private function __clone()
750 {
751 //you can't clone it
752 }
753}
754
755?>
static getCurrent()
Definition context.php:241
setWebRtcSupport($isWebRtcSupported)
Definition mobile.php:144
static ConvertFromUtf($s='')
Definition mobile.php:607
setDeviceWidth($deviceWidth)
Definition mobile.php:394
static $supportedCordovaVersion
Definition mobile.php:15
setMaxScale($maxScale)
Definition mobile.php:519
getIPadViewPort($width=320)
Definition mobile.php:406
setPixelRatio($pixelRatio)
Definition mobile.php:181
setMinScale($minScale)
Definition mobile.php:539
getViewPort($width="")
Definition mobile.php:319
setIniScale($iniScale)
Definition mobile.php:499
setDeviceHeight($deviceHeight)
Definition mobile.php:694
setBXScriptSupported($isBXScriptSupported)
Definition mobile.php:152
setUserScalable($userScalable)
Definition mobile.php:583
static getSystemVersion()
Definition mobile.php:646
setScreenCategory($screenCategory)
Definition mobile.php:437
setLargeScreenSupport($largeScreenSupport)
Definition mobile.php:714
static PrepareStrToJson($s='')
Definition mobile.php:595
$GLOBALS['____1444769544']
Definition license.php:1