1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
database.php
См. документацию.
1<?php
2
4{
6 const CONNECTION_NAME = 'user_session';
8 protected static $connection = null;
9
10 public static function Init($bDoConnect = false)
11 {
12 if (is_object(self::$connection))
13 {
14 return true;
15 }
16
17 $pool = \Bitrix\Main\Application::getInstance()->getConnectionPool();
18 $isConnectionExists = $pool->getConnection(static::CONNECTION_NAME) !== null;
19 if (!$isConnectionExists)
20 {
21 $pool->cloneConnection(
22 $pool::DEFAULT_CONNECTION_NAME,
23 static::CONNECTION_NAME
24 );
25 }
26
27 if ($bDoConnect)
28 {
29 self::$connection = $pool->getConnection(static::CONNECTION_NAME);
30 }
31 else
32 {
33 return true;
34 }
35
36 //In case of error just skip it over
37 if (!is_object(self::$connection))
38 {
39 return false;
40 }
41
42 if (
43 defined("BX_SECURITY_SQL_LOG_BIN")
44 && (
45 BX_SECURITY_SQL_LOG_BIN === false
46 || BX_SECURITY_SQL_LOG_BIN === "N"
47 )
48 )
49 {
50 CSecurityDB::Query("SET sql_log_bin = 0", "Module: security; Class: CSecurityDB; Function: Init; File: ".__FILE__."; Line: ".__LINE__);
51 }
52
53 $rs = CSecurityDB::Query("SHOW TABLES LIKE 'b_sec_session'", "Module: security; Class: CSecurityDB; Function: Init; File: ".__FILE__."; Line: ".__LINE__);
54 if (!is_object($rs))
55 {
56 return false;
57 }
58
60 if ($ar)
61 {
62 return true;
63 }
64
65 if (defined("MYSQL_TABLE_TYPE") && MYSQL_TABLE_TYPE <> '')
66 {
67 $rs = CSecurityDB::Query("SET storage_engine = '".MYSQL_TABLE_TYPE."'", "Module: security; Class: CSecurityDB; Function: Init; File: ".__FILE__."; Line: ".__LINE__);
68 if (!is_object($rs))
69 {
70 return false;
71 }
72 }
73
74 $rs = CSecurityDB::Query("CREATE TABLE b_sec_session
75 (
76 SESSION_ID VARCHAR(250) NOT NULL,
77 TIMESTAMP_X TIMESTAMP NOT NULL,
78 SESSION_DATA LONGTEXT,
79 PRIMARY KEY(SESSION_ID),
80 KEY ix_b_sec_session_time (TIMESTAMP_X)
81 )
82 ", "Module: security; Class: CSecurityDB; Function: Init; File: ".__FILE__."; Line: ".__LINE__);
83
84 return is_object($rs);
85 }
86
87 public static function Disconnect()
88 {
89 if (is_object(self::$connection))
90 {
91 self::$connection->disconnect();
92 self::$connection = null;
93 }
94 }
95
96 public static function CurrentTimeFunction()
97 {
98 return "now()";
99 }
100
101 public static function SecondsAgo($sec)
102 {
103 return "DATE_ADD(now(), INTERVAL - ".intval($sec)." SECOND)";
104 }
105
106 public static function Query($strSql, $error_position)
107 {
108 if (!is_object(self::$connection))
109 {
110 CSecurityDB::Init(true);
111 }
112
113 if (is_object(self::$connection))
114 {
115 $strSql = preg_replace("/^\\s*SELECT\\s+(?!GET_LOCK|RELEASE_LOCK)/i", "SELECT SQL_NO_CACHE ", $strSql);
116 try
117 {
118 $result = self::$connection->query($strSql);
119 return $result;
120 }
121 catch (\Bitrix\Main\Db\SqlQueryException $e)
122 {
123 AddMessage2Log($error_position." MySql Query Error: ".$strSql." [".$e."]", "security");
124 }
125 }
126
127 return false;
128 }
129
130 public static function QueryBind($strSql, $arBinds, $error_position)
131 {
132 foreach ($arBinds as $key => $value)
133 $strSql = str_replace(":".$key, "'".$value."'", $strSql);
134 return CSecurityDB::Query($strSql, $error_position);
135 }
136
141 public static function Fetch($result)
142 {
143 if ($result)
144 return $result->fetch();
145 else
146 return false;
147 }
148
149 public static function Lock($id, $timeout = 60)
150 {
151 static $lock_id = "";
152
153 if ($id === false)
154 {
155 if ($lock_id)
156 {
157 $rsLock = CSecurityDB::Query("DO RELEASE_LOCK('".$lock_id."')", "Module: security; Class: CSecurityDB; Function: Lock; File: ".__FILE__."; Line: ".__LINE__);
158 }
159 else
160 {
161 $rsLock = false;
162 }
163 }
164 else
165 {
166 $rsLock = CSecurityDB::Query("SELECT GET_LOCK('".md5($id)."', ".intval($timeout).") as L", "Module: security; Class: CSecurityDB; Function: Lock; File: ".__FILE__."; Line: ".__LINE__);
167 if ($rsLock)
168 {
169 $arLock = CSecurityDB::Fetch($rsLock);
170 if ($arLock["L"] == "0")
171 return false;
172 else
173 $lock_id = md5($id);
174 }
175 }
176 return is_object($rsLock);
177 }
178
179 public static function LockTable($table_name, $lock_id)
180 {
181 $rsLock = CSecurityDB::Query("SELECT GET_LOCK('".md5($lock_id)."', 0) as L", "Module: security; Class: CSecurityDB; Function: LockTable; File: ".__FILE__."; Line: ".__LINE__);
182 if ($rsLock)
183 {
184 $arLock = CSecurityDB::Fetch($rsLock);
185 if ($arLock["L"] == "0")
186 return false;
187 else
188 return array("lock_id" => $lock_id);
189 }
190 else
191 {
192 return false;
193 }
194 }
195
196 public static function UnlockTable($table_lock)
197 {
198 if (is_array($table_lock))
199 {
200 CSecurityDB::Query("SELECT RELEASE_LOCK('".$table_lock["lock_id"]."')", "Module: security; Class: CSecurityDB; Function: UnlockTable; File: ".__FILE__."; Line: ".__LINE__);
201 }
202 }
203}
static getInstance()
Определения application.php:98
Определения database.php:4
static UnlockTable($table_lock)
Определения database.php:196
static SecondsAgo($sec)
Определения database.php:101
static $connection
Определения database.php:8
static Query($strSql, $error_position)
Определения database.php:106
static CurrentTimeFunction()
Определения database.php:96
const CONNECTION_NAME
Определения database.php:6
static QueryBind($strSql, $arBinds, $error_position)
Определения database.php:130
static Fetch($result)
Определения database.php:141
static Disconnect()
Определения database.php:87
static Init($bDoConnect=false)
Определения database.php:10
static Lock($id, $timeout=60)
Определения database.php:149
static LockTable($table_name, $lock_id)
Определения database.php:179
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
AddMessage2Log($text, $module='', $traceDepth=6, $showArgs=false)
Определения tools.php:3941
$ar
Определения options.php:199
if(empty($signedUserToken)) $key
Определения quickway.php:257
$rs
Определения action.php:82