1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
update_b24.php
См. документацию.
1<?php
2
4{
5 private $arFileVersions = null;
6 private $fileGenerateDate = 0;
7
8 private $serverUniqID = "";
9 private $isProcessingMain = false;
10
11 const versionsFileNameConst = "/bitrix/modules/versions.php";
12 private $versionsFileName = "";
13
14 const versionsDatabaseFolderConst = "/bitrix/updates/bitrix24_clients/";
15 private $versionsDatabaseFolder = "";
16
17 private $updatersDir = "";
18
19 public function __construct($isProcessingMain = false)
20 {
21 $this->serverUniqID = self::GetServerUniqID();
22 $this->isProcessingMain = $isProcessingMain;
23
24 $this->versionsFileName = $_SERVER["DOCUMENT_ROOT"].self::versionsFileNameConst;
25 $this->versionsDatabaseFolder = $_SERVER["DOCUMENT_ROOT"].self::versionsDatabaseFolderConst;
26 $this->updatersDir = $_SERVER["DOCUMENT_ROOT"].US_SAVE_UPDATERS_DIR."/";
27 }
28
29 private static function GetOption($name, $def = "")
30 {
31 $cache = \Bitrix\Main\Application::getInstance()->getManagedCache();
32
33 $cacheFlags = \Bitrix\Main\Config\Configuration::getValue("cache_flags");
34 if (isset($cacheFlags["config_options"]))
35 $cacheTtl = $cacheFlags["config_options"];
36 else
37 $cacheTtl = 0;
38
39 if ($cache->read($cacheTtl, "b_option:main", "b_option"))
40 {
41 $options = $cache->get("b_option:main");
42 return $options["-"][$name];
43 }
44
45 $con = \Bitrix\Main\Application::getConnection();
46 $sqlHelper = $con->getSqlHelper();
47
48 $query = "
49 SELECT VALUE
50 FROM b_option
51 WHERE MODULE_ID = 'main'
52 AND NAME = '{$sqlHelper->forSql($name)}'
53 ";
54
55 $res = $con->query($query);
56 if ($ar = $res->fetch())
57 {
58 return $ar["VALUE"];
59 }
60
61 return $def;
62 }
63
64 private static function SetOption($name, $value = "")
65 {
66 $con = \Bitrix\Main\Application::getConnection();
67 $sqlHelper = $con->getSqlHelper();
68
69 $updateFields = [
70 "VALUE" => $value,
71 ];
72
73 $insertFields = [
74 "MODULE_ID" => "main",
75 "NAME" => $name,
76 "VALUE" => $value,
77 ];
78
79 $keyFields = ["MODULE_ID", "NAME"];
80
81 $sql = $sqlHelper->prepareMerge("b_option", $keyFields, $insertFields, $updateFields);
82
83 $con->queryExecute(current($sql));
84
85 $cache = \Bitrix\Main\Application::getInstance()->getManagedCache();
86 $cache->clean("b_option:main", "b_option");
87
88// $path = \Bitrix\Main\Loader::getLocal("modules/main/option_triggers.php");
89// if ($path !== false)
90// include($path);
91 }
92
93 private static function GetServerUniqID()
94 {
95 $uniq = self::GetOption("server_uniq_id", "");
96 if ($uniq == '')
97 {
98 $uniq = md5(uniqid(rand(), true));
99 self::SetOption("server_uniq_id", $uniq);
100 }
101 return $uniq;
102 }
103
104 public function Lock()
105 {
106 global $DB;
107
108 $uniq = $this->serverUniqID;
109
110 if ($DB->type == "MYSQL")
111 {
112 $dbLock = $DB->Query("SELECT GET_LOCK('".$uniq."_DBUpdater', 0) as L");
113 $arLock = $dbLock->Fetch();
114 if ($arLock["L"] == "1")
115 return true;
116 else
117 return false;
118 }
119 elseif ($DB->type == "ORACLE")
120 {
121 $dbLock = $DB->Query("
122 declare
123 my_lock_id number;
124 my_result number;
125 lock_failed exception;
126 pragma exception_init(lock_failed, -54);
127 begin
128 my_lock_id:=dbms_utility.get_hash_value(to_char('".$uniq."_DBUpdater'), 0, 1024);
129 my_result:=dbms_lock.request(my_lock_id, dbms_lock.x_mode, 0, true);
130 -- Return value:
131 -- 0 - success
132 -- 1 - timeout
133 -- 2 - deadlock
134 -- 3 - parameter error
135 -- 4 - already own lock specified by 'id' or 'lockhandle'
136 -- 5 - illegal lockhandle
137 if(my_result<>0 and my_result<>4)then
138 raise lock_failed;
139 end if;
140 end;
141 ", true);
142 return ($dbLock !== false);
143 }
144 else
145 {
146 $i = 60;
147 $DB->Query("DELETE FROM b_option WHERE MODULE_ID = 'main' AND NAME = '".$uniq."_DBUpdater' AND SITE_ID IS NULL AND DATEDIFF(SECOND, CONVERT(DATETIME, DESCRIPTION), GETDATE()) > ".$i);
148 $DB->Query("SET LOCK_TIMEOUT 1");
149 $dbLock = $DB->Query("INSERT INTO b_option(MODULE_ID, NAME, SITE_ID, VALUE, DESCRIPTION) VALUES ('main', '".$uniq."_DBUpdater', NULL, NULL, CONVERT(VARCHAR(128), GETDATE()))", true);
150 $DB->Query("SET LOCK_TIMEOUT -1");
151 return ($dbLock !== false);
152 }
153 }
154
155 public function UnLock()
156 {
157 global $DB;
158
159 $uniq = $this->serverUniqID;
160
161 if ($DB->type == "MYSQL")
162 {
163 $dbLock = $DB->Query("SELECT RELEASE_LOCK('".$uniq."_DBUpdater') as L");
164 $arLock = $dbLock->Fetch();
165 if($arLock["L"] == "0")
166 return false;
167 else
168 return true;
169 }
170 elseif ($DB->type == "ORACLE")
171 {
172 return true;
173 }
174 else
175 {
176 $DB->Query("DELETE FROM b_option WHERE MODULE_ID = 'main' AND NAME = '".$uniq."_DBUpdater' AND SITE_ID IS NULL");
177 return true;
178 }
179 }
180
181 private static function GetDatabaseVersions()
182 {
183 global $DB;
184
185 $arDBVersions = array();
186
187 $dbResult = $DB->Query("SELECT VALUE FROM b_option WHERE MODULE_ID='main' AND NAME='BITRIX24_VERSIONS' AND SITE_ID IS NULL", true);
188 if ($arResult = $dbResult->Fetch())
189 {
190 $val = $arResult["VALUE"];
191 if ($val <> '')
192 {
193 $arDBVersions = unserialize($val, ['allowed_classes' => false]);
194 if (!is_array($arDBVersions))
195 $arDBVersions = array();
196 }
197 }
198
199 return $arDBVersions;
200 }
201
202 private static function SetDatabaseVersions($arDBVersions)
203 {
204 global $DB;
205
206 if(is_array($arDBVersions))
207 {
208 self::SetOption("BITRIX24_VERSIONS", serialize($arDBVersions));
209 $DB->Query("INSERT INTO b_sm_version_history(DATE_INSERT, VERSIONS) VALUES(NOW(), '".$DB->ForSql(serialize($arDBVersions))."')", true);
210 }
211 }
212
213 private function InitializeFileData()
214 {
215 $generationDate = 0;
216 $arVersions = array();
217
218 if (file_exists($this->versionsFileName))
219 {
220 include($this->versionsFileName);
221 if (!is_array($arVersions))
222 $arVersions = array();
223 }
224
225 if (empty($arVersions))
226 {
227 include_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/classes/general/update_client.php");
228
229 $errorMessage = "";
230 $arVersions = CUpdateClient::GetCurrentModules($errorMessage, false);
231 $generationDate = time();
232 if ($errorMessage == '')
233 {
234 $f = fopen($this->versionsFileName, "w");
235 fwrite($f, "<"."?\n");
236 fwrite($f, "\$generationDate = ".$generationDate.";\n");
237 fwrite($f, "\$arVersions = array(\n");
238 foreach ($arVersions as $moduleId => $version)
239 fwrite($f, "\t\"".htmlspecialchars($moduleId)."\" => \"".htmlspecialchars($version)."\",\n");
240 fwrite($f, ");\n");
241 fwrite($f, "?".">");
242 fclose($f);
243 }
244 }
245
246 $this->fileGenerateDate = $generationDate;
247 $this->arFileVersions = $arVersions;
248 }
249
250 private function GetFileVersions()
251 {
252 if (is_null($this->arFileVersions))
253 $this->InitializeFileData();
254
255 return $this->arFileVersions;
256 }
257
258 private function GetFileGenerateDate()
259 {
260 if ($this->fileGenerateDate <= 0)
261 $this->InitializeFileData();
262
263 return $this->fileGenerateDate;
264 }
265
266 private function GetDatabaseGenerationDate()
267 {
268 return self::GetOption("BITRIX24_GENERATION_DATE_".($this->isProcessingMain ? "M" : "N"), 0);
269 }
270
271 public function PreCheckUpdates()
272 {
273 $generationDate = $this->GetFileGenerateDate();
274 $dbGenerationDate = $this->GetDatabaseGenerationDate();
275
276 if ($dbGenerationDate >= $generationDate)
277 return false;
278
279 return true;
280 }
281
282 private function Collect4PreCheckUpdates($generationDate)
283 {
284 self::SetOption("BITRIX24_GENERATION_DATE_".($this->isProcessingMain ? "M" : "N"), $generationDate);
285 }
286
287 public function CheckUpdates()
288 {
289 $arDBVersions = self::GetDatabaseVersions();
290 if (empty($arDBVersions))
291 {
292 $this->CollectDatabaseVersions("ALL");
293 $arDBVersions = self::GetDatabaseVersions();
294 }
295
296 $arFileVersions = $this->GetFileVersions();
297
298 $arResult = array();
299 foreach ($arFileVersions as $moduleId => $version)
300 {
301 if (($this->isProcessingMain && ($moduleId !== "main"))
302 || (!$this->isProcessingMain && ($moduleId === "main")))
303 continue;
304
305 if (CUpdateClient::CompareVersions($version, $arDBVersions[$moduleId]) > 0)
306 $arResult[$moduleId] = $arDBVersions[$moduleId];
307 }
308
309 // Das ist strashnyy kostyl' for new Options
310 global $DB;
311
312 if ($this->isProcessingMain && !empty($arResult))
313 {
314 if(!$DB->TableExists("b_option_site"))
315 {
316 $DB->Query("
317 CREATE TABLE b_option_site
318 (
319 MODULE_ID VARCHAR(50) not null,
320 NAME VARCHAR(50) not null,
321 SITE_ID CHAR(2) not null,
322 VALUE TEXT,
323 PRIMARY KEY(MODULE_ID, NAME, SITE_ID),
324 INDEX ix_option_site_module_site(MODULE_ID, SITE_ID)
325 )
326 ", true);
327 }
328 if(!$DB->Query("SELECT UNIQUE_ID FROM b_module_to_module WHERE 1=0", true))
329 {
330 $DB->Query("ALTER TABLE b_module_to_module ADD UNIQUE_ID VARCHAR(32) NOT NULL", true);
331 }
332 }
333 // End of strashnyy kostyl'
334
335 return $arResult;
336 }
337
338 public function UpdateFromVersion($moduleId, $dbVersion)
339 {
340 if ($moduleId == '')
341 {
342 return;
343 }
344
345 $errorMessage = "";
346
347 if (file_exists($this->updatersDir.$moduleId) && is_dir($this->updatersDir.$moduleId))
348 {
349 $arUpdaters = array();
350
351 if ($handle = @opendir($this->updatersDir.$moduleId))
352 {
353 while (false !== ($dir = readdir($handle)))
354 {
355 if ($dir == "." || $dir == "..")
356 {
357 continue;
358 }
359
360 if (str_starts_with($dir, "updater"))
361 {
362 if (is_file($this->updatersDir.$moduleId."/".$dir))
363 {
364 $num = substr($dir, 7, strlen($dir) - 11);
365 if (substr($dir, strlen($dir) - 9) == "_post.php")
366 {
367 $num = substr($dir, 7, strlen($dir) - 16);
368 }
369
370 $arUpdaters[] = array("/".$dir, trim($num));
371 }
372 elseif (file_exists($this->updatersDir.$moduleId."/".$dir."/index.php"))
373 {
374 $num = substr($dir, 7);
375 if (substr($dir, strlen($dir) - 5) == "_post")
376 {
377 $num = substr($dir, 7, strlen($dir) - 12);
378 }
379
380 $arUpdaters[] = array("/".$dir."/index.php", trim($num));
381 }
382 }
383 }
384 closedir($handle);
385 }
386
387 $ni1 = count($arUpdaters);
388 for ($i1 = 0; $i1 < $ni1 - 1; $i1++)
389 {
390 for ($j1 = $i1 + 1; $j1 < $ni1; $j1++)
391 {
392 if (CUpdateClient::CompareVersions($arUpdaters[$i1][1], $arUpdaters[$j1][1]) > 0)
393 {
394 $tmp1 = $arUpdaters[$i1];
395 $arUpdaters[$i1] = $arUpdaters[$j1];
396 $arUpdaters[$j1] = $tmp1;
397 }
398 }
399 }
400
401 for ($i1 = 0; $i1 < $ni1; $i1++)
402 {
403 if (CUpdateClient::CompareVersions($arUpdaters[$i1][1], $dbVersion) <= 0)
404 {
405 continue;
406 }
407
408 $errorMessageTmp = "";
409
410 syslog(LOG_INFO, $_SERVER["HTTP_HOST"]."\tstart\t".$moduleId.$arUpdaters[$i1][0]);
411
412 CUpdateClient::RunUpdaterScript($this->updatersDir.$moduleId.$arUpdaters[$i1][0], $errorMessageTmp, "", $moduleId);
413
414 syslog(LOG_INFO, $_SERVER["HTTP_HOST"]."\tend\t".$moduleId.$arUpdaters[$i1][0]."\t".$errorMessageTmp);
415
416 if ($errorMessageTmp <> '')
417 {
418 $errorMessage .= str_replace("#MODULE#", $moduleId, str_replace("#VER#", $arUpdaters[$i1][1], GetMessage("SUPP_UK_UPDN_ERR"))).": ".$errorMessageTmp.".";
419 }
420
421 $this->CollectDatabaseVersions("MODULE", $moduleId, $arUpdaters[$i1][1]);
422 }
423 }
424
425 if ($errorMessage <> '')
426 {
427 $message = "Database update error (".$moduleId."-".$dbVersion."): ".$errorMessage;
428 CUpdateClient::AddMessage2Log($message, "DUE");
429 syslog(LOG_INFO, $_SERVER["HTTP_HOST"] . "\terror\t" . $message);
430 }
431 else
432 {
433 $message = "Database updated successfully (".$moduleId."-".$dbVersion.")";
434 CUpdateClient::AddMessage2Log($message, "DUS");
435 syslog(LOG_INFO, $_SERVER["HTTP_HOST"] . "\tok\t" . $message);
436 }
437 }
438
439 public function CollectDatabaseVersions($type, $moduleId = null, $version = null)
440 {
441 $arDBVersions = self::GetDatabaseVersions();
442 $arFileVersions = $this->GetFileVersions();
443 switch ($type)
444 {
445 case "MODULE":
446 if (isset($moduleId) && isset($version))
447 {
448 //Only one selected module
449 $arDBVersions[$moduleId] = $version;
450 self::SetDatabaseVersions($arDBVersions);
451 }
452 break;
453
454 case "HIT":
455 if ($this->isProcessingMain)
456 {
457 //Main only
458 $arDBVersions["main"] = $arFileVersions["main"];
459 self::SetDatabaseVersions($arDBVersions);
460 }
461 else
462 {
463 //All except main
464 $arFileVersions["main"] = $arDBVersions["main"];
465 self::SetDatabaseVersions($arFileVersions);
466 }
467 $this->Collect4PreCheckUpdates($this->GetFileGenerateDate());
468 break;
469
470 default:
471 //All
472 self::SetDatabaseVersions($arFileVersions);
473 $this->Collect4PreCheckUpdates($this->GetFileGenerateDate());
474 break;
475 }
476 }
477}
$con
Определения admin_tab.php:7
$type
Определения options.php:106
$arResult
Определения generate_coupon.php:16
static getInstance()
Определения application.php:98
static getValue($name)
Определения configuration.php:24
Определения update_b24.php:4
UnLock()
Определения update_b24.php:155
PreCheckUpdates()
Определения update_b24.php:271
__construct($isProcessingMain=false)
Определения update_b24.php:19
CheckUpdates()
Определения update_b24.php:287
Lock()
Определения update_b24.php:104
const versionsDatabaseFolderConst
Определения update_b24.php:14
UpdateFromVersion($moduleId, $dbVersion)
Определения update_b24.php:338
const versionsFileNameConst
Определения update_b24.php:11
CollectDatabaseVersions($type, $moduleId=null, $version=null)
Определения update_b24.php:439
$options
Определения commerceml2.php:49
$f
Определения component_props.php:52
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$handle
Определения include.php:55
$query
Определения get_search.php:11
$moduleId
while($arParentIBlockProperty=$dbParentIBlockProperty->Fetch()) $errorMessage
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
global $DB
Определения cron_frame.php:29
GetMessage($name, $aReplace=null)
Определения tools.php:3397
$name
Определения menu_edit.php:35
$value
Определения Param.php:39
$message
Определения payment.php:8
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$ar
Определения options.php:199
$dir
Определения quickway.php:303
$i
Определения factura.php:643
</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
$val
Определения options.php:1793
foreach($arTemplatesList as $templ) if(mb_strpos($templ["NAME"] $def
Определения template_copy.php:264
$dbResult
Определения updtr957.php:3