1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
short_uri.php
См. документацию.
1<?php
2
4
6{
7 private static $httpStatusCodes = array(
8 301 => "301 Moved Permanently",
9 302 => "302 Found",
10 /*303 => "303 See Other",
11 307 => "307 Temporary Redirect"*/
12 );
13
14 protected static $arErrors = array();
15
16 public static function GetErrors()
17 {
18 return self::$arErrors;
19 }
20
21 protected static function AddError($error)
22 {
23 self::$arErrors[] = $error;
24 }
25
26 protected static function ClearErrors()
27 {
28 self::$arErrors = array();
29 }
30
31 public static function Update($id, $arFields)
32 {
33 global $DB;
34
36
37 $id = intval($id);
38 if ($id <= 0)
39 {
40 self::AddError(GetMessage("MN_SU_NO_ID"));
41 return false;
42 }
43
44 if (!self::ParseFields($arFields, $id))
45 return false;
46
47 $strUpdate = $DB->PrepareUpdate("b_short_uri", $arFields);
48
49 $strSql =
50 "UPDATE b_short_uri SET ".
51 " ".$strUpdate.", ".
52 " MODIFIED = ".$DB->CurrentTimeFunction()." ".
53 "WHERE ID = ".$id;
54 $DB->Query($strSql);
55
56 return $id;
57 }
58
59 public static function GetShortUri($uri)
60 {
61 $uriCrc32 = self::Crc32($uri);
62
63 $dbResult = CBXShortUri::GetList(array(), array("URI_CRC" => $uriCrc32));
64 while ($arResult = $dbResult->Fetch())
65 {
66 if ($arResult["URI"] == $uri)
67 return "/".$arResult["SHORT_URI"];
68 }
69
71 "URI" => $uri,
72 "SHORT_URI" => self::GenerateShortUri(),
73 "STATUS" => 301,
74 );
75
77
78 if ($id)
79 return "/".$arFields["SHORT_URI"];
80
81 return "";
82 }
83
84 public static function GetUri($shortUri)
85 {
86 $shortUri = trim($shortUri);
87
88 $ar = @parse_url($shortUri);
89 if (isset($ar["path"]))
90 $shortUri = $ar["path"];
91
92 $shortUri = trim($shortUri, "/");
93
94 $uriCrc32 = self::Crc32($shortUri);
95
96 $dbResult = CBXShortUri::GetList(array(), array("SHORT_URI_CRC" => $uriCrc32));
97 while ($arResult = $dbResult->Fetch())
98 {
99 if ($arResult["SHORT_URI"] == $shortUri)
100 return array("URI" => $arResult["URI"], "STATUS" => $arResult["STATUS"], "ID" => $arResult["ID"]);
101 }
102
103 return null;
104 }
105
106 public static function SetLastUsed($id)
107 {
108 global $DB;
109
110 $strSql =
111 "UPDATE b_short_uri SET ".
112 " NUMBER_USED = NUMBER_USED + 1, ".
113 " LAST_USED = ".$DB->CurrentTimeFunction()." ".
114 "WHERE ID = ".intval($id);
115 $DB->Query($strSql);
116 }
117
118 public static function Delete($id)
119 {
120 global $DB, $APPLICATION;
121
123
124 $id = intval($id);
125 if ($id <= 0)
126 {
127 self::AddError(GetMessage("MN_SU_NO_ID"));
128 return false;
129 }
130
131 foreach(GetModuleEvents("main", "OnBeforeShortUriDelete", true) as $arEvent)
132 {
133 if(ExecuteModuleEventEx($arEvent, array($id)) === false)
134 {
135 if(($ex = $APPLICATION->GetException()))
136 $err = $ex->GetString();
137 else
138 $err = GetMessage("MN_SU_DELETE_ERROR");
139 self::AddError($err);
140 return false;
141 }
142 }
143
144 $fl = $DB->Query("DELETE FROM b_short_uri WHERE ID = ".$id, true);
145
146 if (!$fl)
147 {
148 self::AddError(GetMessage("MN_SU_DELETE_ERROR"));
149 return false;
150 }
151
152 return true;
153 }
154
155 public static function Crc32($str)
156 {
157 $c = crc32($str);
158 if ($c > 0x7FFFFFFF)
159 $c = -(0xFFFFFFFF - $c + 1);
160 return $c;
161 }
162
163 protected static function ParseFields(&$arFields, $id = 0)
164 {
165 $id = intval($id);
166 $updateMode = ($id > 0 ? true : false);
167 $addMode = !$updateMode;
168
169 if (is_set($arFields, "URI") || $addMode)
170 {
171 $arFields["URI"] = trim($arFields["URI"]);
172 if ($arFields["URI"] == '')
173 {
174 self::AddError(GetMessage("MN_SU_NO_URI"));
175 return false;
176 }
177
178 $arFields["URI_CRC"] = self::Crc32($arFields["URI"]);
179 }
180
181 if (is_set($arFields, "SHORT_URI") || $addMode)
182 {
183 $arFields["SHORT_URI"] = trim($arFields["SHORT_URI"]);
184 if ($arFields["SHORT_URI"] == '')
185 {
186 self::AddError(GetMessage("MN_SU_NO_SHORT_URI"));
187 return false;
188 }
189
190 $ar = @parse_url($arFields["SHORT_URI"]);
191 if (isset($ar["path"]))
192 $arFields["SHORT_URI"] = $ar["path"];
193
194 //$arFields["SHORT_URI"] = @parse_url($arFields["SHORT_URI"], PHP_URL_PATH);
195 $arFields["SHORT_URI"] = trim($arFields["SHORT_URI"], "/");
196 if ($arFields["SHORT_URI"] == '')
197 {
198 self::AddError(GetMessage("MN_SU_WRONG_SHORT_URI"));
199 return false;
200 }
201
202 $arFields["SHORT_URI_CRC"] = self::Crc32($arFields["SHORT_URI"]);
203 }
204
205 if (is_set($arFields, "STATUS") || $addMode)
206 {
207 $arFields["STATUS"] = intval($arFields["STATUS"]);
208 if ($arFields["STATUS"] <= 0)
209 {
210 self::AddError(GetMessage("MN_SU_NO_STATUS"));
211 return false;
212 }
213 elseif (!array_key_exists($arFields["STATUS"], self::$httpStatusCodes))
214 {
215 self::AddError(GetMessage("MN_SU_WRONG_STATUS"));
216 return false;
217 }
218 }
219
220 if (is_set($arFields, "NUMBER_USED") || $addMode)
221 {
222 $arFields["NUMBER_USED"] = intval($arFields["NUMBER_USED"] ?? 0);
223 if ($arFields["NUMBER_USED"] <= 0)
224 $arFields["NUMBER_USED"] = 0;
225 }
226
227 return true;
228 }
229
230 public static function GetHttpStatusCodeText($code)
231 {
232 $code = intval($code);
233
234 if (array_key_exists($code, self::$httpStatusCodes))
235 return self::$httpStatusCodes[$code];
236
237 return "";
238 }
239
240 public static function SelectBox($fieldName, $value, $defaultValue = "", $field = "class=\"typeselect\"")
241 {
242 $s = '<select name="'.$fieldName.'" '.$field.'>'."\n";
243 $s1 = "";
244 $found = false;
245 foreach (self::$httpStatusCodes as $code => $codeText)
246 {
247 $found = ($code == $value);
248 $m = GetMessage("MN_SU_HTTP_STATUS_".$code);
249 $s1 .= '<option value="'.$code.'"'.($found ? ' selected':'').'>'.(empty($m) ? htmlspecialcharsex($codeText) : htmlspecialcharsex($m)).'</option>'."\n";
250 }
251 if ($defaultValue <> '')
252 $s .= "<option value='' ".($found ? "" : "selected").">".htmlspecialcharsex($defaultValue)."</option>";
253 return $s.$s1.'</select>';
254 }
255
256 public static function GenerateShortUri()
257 {
258 do
259 {
260 $uri = "~".randString(5);
261 $bNew = true;
262 $uriCrc32 = self::Crc32($uri);
263
264 $dbResult = CBXShortUri::GetList(array(), array("SHORT_URI_CRC" => $uriCrc32));
265 while ($arResult = $dbResult->Fetch())
266 {
267 if ($arResult["SHORT_URI"] == $uri)
268 {
269 $bNew = false;
270 break;
271 }
272 }
273 }
274 while (!$bNew);
275
276 return $uri;
277 }
278
279 public static function CheckUri()
280 {
281 if ($arUri = static::GetUri(Bitrix\Main\Context::getCurrent()->getRequest()->getDecodedUri()))
282 {
283 static::SetLastUsed($arUri["ID"]);
284 if (CModule::IncludeModule("statistic"))
285 {
286 CStatEvent::AddCurrent("short_uri_redirect", "", "", "", "", $arUri["URI"], "N", SITE_ID);
287 }
288 LocalRedirect($arUri["URI"], true, static::GetHttpStatusCodeText($arUri["STATUS"]));
289 return true;
290 }
291 return false;
292 }
293
294 public static function Add($arFields)
295 {
296 global $DB;
297
299
300 if (!self::ParseFields($arFields))
301 return false;
302
303 $arInsert = $DB->PrepareInsert("b_short_uri", $arFields);
304
305 $strSql =
306 "INSERT INTO b_short_uri (".$arInsert[0].", MODIFIED) ".
307 "VALUES(".$arInsert[1].", ".$DB->CurrentTimeFunction().")";
308 $DB->Query($strSql);
309
310 $taskId = intval($DB->LastID());
311
312 $arFields["ID"] = $taskId;
313
314 foreach (GetModuleEvents("main", "OnAfterShortUriAdd", true) as $arEvent)
316
317 return $taskId;
318 }
319
320 public static function GetList($arOrder = array("ID" => "DESC"), $arFilter = array(), $arNavStartParams = false)
321 {
322 global $DB;
323
325
326 $arWherePart = array();
327 if (is_array($arFilter))
328 {
329 foreach ($arFilter as $key => $val)
330 {
331 $key = mb_strtoupper($key);
332 switch($key)
333 {
334 case "ID":
335 $arWherePart[] = "U.ID=".intval($val);
336 break;
337 case "URI":
338 $q = GetFilterQuery("U.URI", $val);
339 if (!empty($q) && ($q != "0"))
340 $arWherePart[] = $q;
341 break;
342 case "URI_EXACT":
343 $arWherePart[] = "U.URI='".$DB->ForSQL($val)."'";
344 break;
345 case "URI_CRC":
346 $arWherePart[] = "U.URI_CRC=".intval($val);
347 break;
348 case "SHORT_URI":
349 $arWherePart[] = "U.SHORT_URI='".$DB->ForSQL($val)."'";
350 break;
351 case "SHORT_URI_CRC":
352 $arWherePart[] = "U.SHORT_URI_CRC=".intval($val);
353 break;
354 case "STATUS":
355 $arWherePart[] = "U.STATUS=".intval($val);
356 break;
357 case "MODIFIED_1":
358 $arWherePart[] = "U.MODIFIED >= FROM_UNIXTIME('".MkDateTime(FmtDate($val, "D.M.Y"), "d.m.Y")."')";
359 break;
360 case "MODIFIED_2":
361 $arWherePart[] = "U.MODIFIED <= FROM_UNIXTIME('".MkDateTime(FmtDate($val, "D.M.Y")." 23:59:59", "d.m.Y")."')";
362 break;
363 case "LAST_USED_1":
364 $arWherePart[] = "U.LAST_USED >= FROM_UNIXTIME('".MkDateTime(FmtDate($val, "D.M.Y"), "d.m.Y")."')";
365 break;
366 case "LAST_USED_2":
367 $arWherePart[] = "U.LAST_USED <= FROM_UNIXTIME('".MkDateTime(FmtDate($val, "D.M.Y")." 23:59:59", "d.m.Y")."')";
368 break;
369 case "NUMBER_USED":
370 $arWherePart[] = "U.NUMBER_USED=".intval($val);
371 break;
372 }
373 }
374 }
375
376 $strWherePart = "";
377 if (!empty($arWherePart))
378 {
379 foreach ($arWherePart as $val)
380 {
381 if ($strWherePart !== "")
382 $strWherePart .= " AND ";
383 $strWherePart .= "(".$val.")";
384 }
385 }
386 if ($strWherePart !== "")
387 $strWherePart = "WHERE ".$strWherePart;
388
389 $arOrderByPart = array();
390 if (is_array($arOrder))
391 {
392 foreach ($arOrder as $key => $val)
393 {
394 $key = mb_strtoupper($key);
395 if (!in_array($key, array("ID", "URI", "URI_CRC", "SHORT_URI", "SHORT_URI_CRC", "STATUS", "MODIFIED", "LAST_USED", "NUMBER_USED")))
396 continue;
397 $val = mb_strtoupper($val);
398 if (!in_array($val, array("ASC", "DESC")))
399 $val = "ASC";
400 if ($key == "MODIFIED")
401 $key = "MODIFIED1";
402 if ($key == "LAST_USED")
403 $key = "LAST_USED1";
404 $arOrderByPart[] = $key." ".$val;
405 }
406 }
407
408 $strOrderByPart = "";
409 if (!empty($arOrderByPart))
410 {
411 foreach ($arOrderByPart as $val)
412 {
413 if ($strOrderByPart !== "")
414 $strOrderByPart .= ", ";
415 $strOrderByPart .= $val;
416 }
417 }
418 if ($strOrderByPart !== "")
419 $strOrderByPart = "ORDER BY ".$strOrderByPart;
420
421 $strSql = "FROM b_short_uri U ".$strWherePart;
422
423 if ($arNavStartParams)
424 {
425 $dbResultCount = $DB->Query("SELECT COUNT(U.ID) as C ".$strSql);
426 $arResultCount = $dbResultCount->Fetch();
427 $strSql = "SELECT ID, URI, URI_CRC, SHORT_URI, SHORT_URI_CRC, STATUS, ".$DB->DateToCharFunction("MODIFIED")." MODIFIED, MODIFIED MODIFIED1, ".$DB->DateToCharFunction("LAST_USED")." LAST_USED, LAST_USED LAST_USED1, NUMBER_USED ".$strSql.$strOrderByPart;
428 $dbResult = new CDBResult();
429 $dbResult->NavQuery($strSql, $arResultCount["C"], $arNavStartParams);
430 }
431 else
432 {
433 $strSql = "SELECT ID, URI, URI_CRC, SHORT_URI, SHORT_URI_CRC, STATUS, ".$DB->DateToCharFunction("MODIFIED")." MODIFIED, MODIFIED MODIFIED1, ".$DB->DateToCharFunction("LAST_USED")." LAST_USED, LAST_USED LAST_USED1, NUMBER_USED ".$strSql.$strOrderByPart;
434 $dbResult = $DB->Query($strSql);
435 }
436
437 return $dbResult;
438 }
439}
440
441/*
442 * create table b_short_uri
443 * (
444 * ID int(18) not null auto_increment,
445 * URI varchar(250) not null,
446 * URI_CRC int(18) not null,
447 * SHORT_URI varbinary(250) not null,
448 * SHORT_URI_CRC int(18) not null,
449 * STATUS int(18) not null default 301,
450 * MODIFIED timestamp not null,
451 * LAST_USED timestamp null,
452 * NUMBER_USED int(18) not null default 0,
453 * primary key (ID),
454 * index ux_b_short_uri_1 (SHORT_URI_CRC),
455 * index ux_b_short_uri_2 (URI_CRC)
456 * )
457 * */
if($_SERVER $defaultValue['REQUEST_METHOD']==="GET" &&!empty($RestoreDefaults) && $bizprocPerms==="W" &&check_bitrix_sessid())
Определения options.php:32
global $APPLICATION
Определения include.php:80
$arResult
Определения generate_coupon.php:16
static AddCurrent($event1, $event2="", $event3="", $money="", $currency="", $goto="", $chargeback="N", $site_id=false)
Определения statevent.php:38
Определения short_uri.php:6
static GetHttpStatusCodeText($code)
Определения short_uri.php:230
static $arErrors
Определения short_uri.php:14
static GetList($arOrder=array("ID"=> "DESC"), $arFilter=array(), $arNavStartParams=false)
Определения short_uri.php:320
static ClearErrors()
Определения short_uri.php:26
static Delete($id)
Определения short_uri.php:118
static AddError($error)
Определения short_uri.php:21
static Add($arFields)
Определения short_uri.php:294
static GetShortUri($uri)
Определения short_uri.php:59
static GetUri($shortUri)
Определения short_uri.php:84
static GetErrors()
Определения short_uri.php:16
static SelectBox($fieldName, $value, $defaultValue="", $field="class=\"typeselect\"")
Определения short_uri.php:240
static CheckUri()
Определения short_uri.php:279
static Crc32($str)
Определения short_uri.php:155
static SetLastUsed($id)
Определения short_uri.php:106
static ParseFields(&$arFields, $id=0)
Определения short_uri.php:163
static Update($id, $arFields)
Определения short_uri.php:31
static GenerateShortUri()
Определения short_uri.php:256
$str
Определения commerceml2.php:63
$arFields
Определения dblapprove.php:5
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
GetFilterQuery($field, $val, $procent="Y", $ex_sep=array(), $clob="N", $div_fields="Y", $clob_upper="N")
Определения filter_tools.php:383
global $DB
Определения cron_frame.php:29
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/urlrewrite.php")) $uri
Определения urlrewrite.php:61
ExecuteModuleEventEx($arEvent, $arParams=[])
Определения tools.php:5214
FmtDate($str_date, $format=false, $site=false, $bSearchInSitesOnly=false)
Определения tools.php:745
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
Определения tools.php:5177
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
is_set($a, $k=false)
Определения tools.php:2133
GetMessage($name, $aReplace=null)
Определения tools.php:3397
LocalRedirect($url, $skip_security_check=false, $status="302 Found")
Определения tools.php:4005
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$ar
Определения options.php:199
if(empty($signedUserToken)) $key
Определения quickway.php:257
$val
Определения options.php:1793
const SITE_ID
Определения sonet_set_content_view.php:12
$error
Определения subscription_card_product.php:20
$dbResult
Определения updtr957.php:3
$arFilter
Определения user_search.php:106