1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
mobile.php
См. документацию.
1<?php
2
3namespace Bitrix\MobileApp;
4
5use Bitrix\Main\Application;
6use Bitrix\Main\Context;
7use Bitrix\Main\IO\File;
8use Bitrix\Main\Text\Encoding;
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(mb_strtoupper($deviceDetectSource), "IPHONE") > 0 || mb_strrpos(mb_strtoupper($deviceDetectSource), "IPAD") > 0)
81 {
82 self::$platform = "ios";
83 }
84 elseif (mb_strrpos(mb_strtoupper($deviceDetectSource), "ANDROID") > 0 || mb_strrpos(mb_strtoupper($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
244 \CJSCore::Init("db");
245 $jsVarsFormat = <<<JSCODE
246 <script>
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,
261 ), false, true);
262
263 if (self::$platform == "android")
264 {
269 $androidJS = <<<JSCODE
270 <script>
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 src='$pgJsFile'></script>", false, true);
282 }
283
284
285 $APPLICATION->AddHeadString("<script src=\"" . \CUtil::GetAdditionalFileURL("/bitrix/js/mobileapp/bitrix_mobile.js") . "\"></script>", false, true);
286 $APPLICATION->AddHeadString("<script src=\"" . \CUtil::GetAdditionalFileURL("/bitrix/js/mobileapp/mobile_lib.js") . "\"></script>", false, true);
287
288
289 if (self::$platform == "android")
290 {
291 $APPLICATION->AddHeadString("<script>app.bindloadPageBlank();</script>", false, false);
292 }
293
294 if (!array_key_exists("doNotUseViewPort", $_REQUEST))
295 {
296 $APPLICATION->AddHeadString(Mobile::getInstance()->getViewPort());
297 }
298
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
596 public static function PrepareStrToJson($s = '')
597 {
598 return $s;
599 }
600
609 public static function ConvertFromUtf($s = '')
610 {
611 return $s;
612 }
613
614 public static function onMobileInit()
615 {
616 if (!defined("MOBILE_INIT_EVENT_SKIP"))
617 {
618 $db_events = getModuleEvents("mobileapp", "OnMobileInit");
619 while ($arEvent = $db_events->Fetch())
620 {
621 ExecuteModuleEventEx($arEvent);
622 }
623 }
624 }
625
631 public static function getApiVersion()
632 {
633 return self::$apiVersion;
634 }
635
640 public static function getPgVersion()
641 {
642 return self::$pgVersion;
643 }
644
648 public static function getSystemVersion()
649 {
650 return (float) self::$systemVersion;
651 }
652
656 public function isWebRtcSupported()
657 {
658 return $this->isWebRtcSupported;
659 }
660
666 public function getViewPortPortrait()
667 {
668 return $this->getViewPort($this->deviceWidth);
669 }
670
676 public function getViewPortLandscape()
677 {
678 return $this->getViewPort($this->deviceHeight);
679 }
680
686 public function getDeviceHeight()
687 {
688 return $this->deviceHeight;
689 }
690
696 public function setDeviceHeight($deviceHeight)
697 {
698 $this->deviceHeight = $deviceHeight;
699 }
700
706 public function getLargeScreenSupport()
707 {
708 return $this->largeScreenSupport;
709 }
710
716 public function setLargeScreenSupport($largeScreenSupport)
717 {
718 $this->largeScreenSupport = $largeScreenSupport;
719 }
720
726 public function getScale()
727 {
728 return $this->scale;
729 }
730
736
737 public function setScale($scale)
738 {
739 $this->scale = $scale;
740 }
741
746 public function isLarge()
747 {
748 return ($this->getScreenCategory() == "LARGE" || $this->getScreenCategory() == "XLARGE");
749 }
750
751 private function __clone()
752 {
753 //you can't clone it
754 }
755}
756
757?>
global $APPLICATION
Определения include.php:80
static load($extNames)
Определения extension.php:16
getMinScale()
Определения mobile.php:529
setWebRtcSupport($isWebRtcSupported)
Определения mobile.php:144
static ConvertFromUtf($s='')
Определения mobile.php:609
setDeviceWidth($deviceWidth)
Определения mobile.php:394
static $supportedCordovaVersion
Определения mobile.php:15
static $isDev
Определения mobile.php:16
setMaxScale($maxScale)
Определения mobile.php:519
isLarge()
Определения mobile.php:746
static getPlatform()
Определения mobile.php:565
getIPadViewPort($width=320)
Определения mobile.php:406
getScale()
Определения mobile.php:726
getUserScalable()
Определения mobile.php:574
getDeviceHeight()
Определения mobile.php:686
setPixelRatio($pixelRatio)
Определения mobile.php:181
setMinScale($minScale)
Определения mobile.php:539
getDeviceWidth()
Определения mobile.php:384
getTargetDpi()
Определения mobile.php:455
getViewPort($width="")
Определения mobile.php:319
getViewPortLandscape()
Определения mobile.php:676
setIniScale($iniScale)
Определения mobile.php:499
getIniScale()
Определения mobile.php:489
setDeviceHeight($deviceHeight)
Определения mobile.php:696
setBXScriptSupported($isBXScriptSupported)
Определения mobile.php:152
static getApiVersion()
Определения mobile.php:631
static initScripts()
Определения mobile.php:239
getPixelRatio()
Определения mobile.php:171
setUserScalable($userScalable)
Определения mobile.php:583
static getPgVersion()
Определения mobile.php:640
static $pgVersion
Определения mobile.php:14
getViewPortPortrait()
Определения mobile.php:666
static getSystemVersion()
Определения mobile.php:648
static $platform
Определения mobile.php:12
_Init()
Определения mobile.php:207
isWebRtcSupported()
Определения mobile.php:656
static $systemVersion
Определения mobile.php:19
getDevice()
Определения mobile.php:161
getMaxScale()
Определения mobile.php:509
static $isAlreadyInit
Определения mobile.php:18
setScreenCategory($screenCategory)
Определения mobile.php:437
static isAppBackground()
Определения mobile.php:190
static $apiVersion
Определения mobile.php:13
getBXScriptSupported()
Определения mobile.php:307
static getInstance()
Определения mobile.php:226
getLargeScreenSupport()
Определения mobile.php:706
static Init()
Определения mobile.php:199
setScale($scale)
Определения mobile.php:737
static $instance
Определения mobile.php:17
setWidth($width)
Определения mobile.php:556
getWidth()
Определения mobile.php:548
setLargeScreenSupport($largeScreenSupport)
Определения mobile.php:716
static PrepareStrToJson($s='')
Определения mobile.php:596
getScreenCategory()
Определения mobile.php:427
getLargeScreenViewPort()
Определения mobile.php:445
static onMobileInit()
Определения mobile.php:614
static Init($arExt=array(), $bReturn=false)
Определения jscore.php:66
$content
Определения commerceml.php:144
bx popup label bx width30 PAGE_NEW_MENU_NAME text width
Определения file_new.php:677
bx_acc_lim_group_list limitGroupList[] multiple<?=$group[ 'ID']?> ID selected margin top
Определения file_new.php:657
$_REQUEST["admin_mnu_menu_id"]
Определения get_menu.php:8
global $USER
Определения csv_new_run.php:40
const SITE_DIR(!defined('LANG'))
Определения include.php:72
ExecuteModuleEventEx($arEvent, $arParams=[])
Определения tools.php:5214
AddEventHandler($FROM_MODULE_ID, $MESSAGE_ID, $CALLBACK, $SORT=100, $FULL_PATH=false)
Определения tools.php:5165
$GLOBALS['____1690880296']
Определения license.php:1
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$width
Определения html.php:68