Bitrix-D7 22.6
 
Загрузка...
Поиск...
Не найдено
common.php
1<?php
2namespace Bitrix\Im;
3
4class Common
5{
6 public static function getPublicDomain()
7 {
8 $schema = \Bitrix\Main\Context::getCurrent()->getRequest()->isHttps()? "https" : "http";
9
10 if (defined("SITE_SERVER_NAME") && SITE_SERVER_NAME <> '')
11 {
12 $domain = SITE_SERVER_NAME;
13 }
14 else
15 {
16 $domain = \Bitrix\Main\Config\Option::get("main", "server_name", '');
17 if (!$domain)
18 {
19 $domain = $_SERVER['SERVER_NAME'].(in_array($_SERVER['SERVER_PORT'], Array(80, 443))?'':':'.$_SERVER['SERVER_PORT']);
20 }
21 }
22
23 return $schema."://".$domain;
24 }
25
26 public static function objectEncode($params, $pureJson = false)
27 {
28 if (is_array($params))
29 {
30 array_walk_recursive($params, function(&$value, $key)
31 {
32 if ($value instanceof \Bitrix\Main\Type\DateTime)
33 {
34 $value = date('c', $value->getTimestamp());
35 }
36 else if (is_string($key) && in_array($key, ['AVATAR', 'AVATAR_HR']) && is_string($value) && $value && mb_strpos($value, 'http') !== 0)
37 {
38 $value = \Bitrix\Im\Common::getPublicDomain().$value;
39 }
40 });
41 }
42
43 return $pureJson? self::jsonEncode($params): \CUtil::PhpToJSObject($params);
44 }
45
46 public static function jsonEncode($array = [])
47 {
48 $option = null;
50 {
51 $option = JSON_UNESCAPED_UNICODE;
52 }
53
54 return \Bitrix\Main\Web\Json::encode($array, $option);
55 }
56
57 public static function getCacheUserPostfix($id)
58 {
59 return '/'.mb_substr(md5($id), 2, 2).'/'.intval($id);
60 }
61
62 public static function isChatId($id)
63 {
64 return $id && preg_match('/^(chat|sg|crm)[0-9]{1,}$/i', $id);
65 }
66
67 public static function isDialogId($id)
68 {
69 return $id && preg_match('/^([0-9]{1,}|(chat|sg|crm)[0-9]{1,})$/i', $id);
70 }
71
72 public static function getUserId($userId = null)
73 {
74 if (is_null($userId) && is_object($GLOBALS['USER']))
75 {
76 $userId = $GLOBALS['USER']->getId();
77 }
78
79 $userId = intval($userId);
80 if (!$userId)
81 {
82 return false;
83 }
84
85 return $userId;
86 }
87
88 public static function toJson($array, $camelCase = true)
89 {
90 $result = [];
91 foreach ($array as $field => $value)
92 {
93 if (is_array($value))
94 {
95 $value = self::toJson($value, $camelCase);
96 }
97 else if ($value instanceof \Bitrix\Main\Type\DateTime)
98 {
99 $value = date('c', $value->getTimestamp());
100 }
101 else if (is_string($value) && $value && is_string($field) && in_array($field, Array('AVATAR')) && mb_strpos($value, 'http') !== 0)
102 {
103 $value = \Bitrix\Im\Common::getPublicDomain().$value;
104 }
105
106 if ($camelCase)
107 {
108 $field = lcfirst(\Bitrix\Main\Text\StringHelper::snake2camel($field));
109 }
110 else
111 {
112 $field = mb_strtolower($field);
113 }
114
115 $result[$field] = $value;
116 }
117
118 return $result;
119 }
120
121 public static function getExternalAuthId($skipTypes = [])
122 {
123 return \Bitrix\Im\Model\UserTable::filterExternalUserTypes($skipTypes);
124 }
125
126 public static function getPullExtra()
127 {
128 return [
129 'revision_im_web' => \Bitrix\Im\Revision::getWeb(),
130 'revision_im_mobile' => \Bitrix\Im\Revision::getMobile(),
131 'revision_im_rest' => \Bitrix\Im\Revision::getRest(),
132 // deprecated
133 'im_revision' => \Bitrix\Im\Revision::getWeb(),
134 'im_revision_mobile' => \Bitrix\Im\Revision::getMobile(),
135 ];
136 }
137}
138
static getExternalAuthId($skipTypes=[])
Definition: common.php:121
static toJson($array, $camelCase=true)
Definition: common.php:88
static objectEncode($params, $pureJson=false)
Definition: common.php:26
static isChatId($id)
Definition: common.php:62
static getPullExtra()
Definition: common.php:126
static jsonEncode($array=[])
Definition: common.php:46
static getCacheUserPostfix($id)
Definition: common.php:57
static getUserId($userId=null)
Definition: common.php:72
static getPublicDomain()
Definition: common.php:6
static isDialogId($id)
Definition: common.php:67