1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
template.php
См. документацию.
1<?php
3
5{
6 public $LAST_ERROR = '';
7
8 //Get list
9 public static function GetList()
10 {
12 $arTemplates = [];
13
14 $dir = mb_substr(getLocalPath('php_interface/subscribe/templates', BX_PERSONAL_ROOT), 1); //cut leading slash
15 $abs_dir = $_SERVER['DOCUMENT_ROOT'] . '/' . $dir;
16 if ($dir && $io->DirectoryExists($abs_dir))
17 {
18 $d = $io->GetDirectory($abs_dir);
19 foreach ($d->GetChildren() as $dir_entry)
20 {
21 if ($dir_entry->IsDirectory())
22 {
23 $arTemplates[] = $dir . '/' . $dir_entry->GetName();
24 }
25 }
26 }
27
28 return $arTemplates;
29 }
30
31 public static function GetByID($path='')
32 {
34 {
35 return false;
36 }
37
38 $arTemplate = [];
39
41
42 $strFileName = $_SERVER['DOCUMENT_ROOT'] . '/' . $path . '/description.php';
43 if (file_exists($strFileName))
44 {
45 include $strFileName;
46 }
47
48 $arTemplate['PATH'] = $path;
49 return $arTemplate;
50 }
51
52 public static function IsExists($path='')
53 {
55
56 $dir = mb_substr(getLocalPath('php_interface/subscribe/templates', BX_PERSONAL_ROOT), 1);
57 if (mb_strpos($path, $dir . '/') === 0)
58 {
59 $template = mb_substr($path, mb_strlen($dir) + 1);
60 if (
61 mb_strpos($template, "\0") !== false
62 || mb_strpos($template, '\\') !== false
63 || mb_strpos($template, '/') !== false
64 || mb_strpos($template, '..') !== false
65 )
66 {
67 return false;
68 }
69
70 return $io->DirectoryExists($_SERVER['DOCUMENT_ROOT'] . '/' . $path);
71 }
72 return false;
73 }
74
75 public static function Execute()
76 {
77 $rubrics = CRubric::GetList([], ['ACTIVE' => 'Y', 'AUTO' => 'Y']);
78 $current_time = time();
79 $time_of_exec = false;
80 $result = '';
81 while (($arRubric = $rubrics->Fetch()) && $time_of_exec === false)
82 {
83 if ($arRubric['LAST_EXECUTED'] == '')
84 {
85 continue;
86 }
87
88 $last_executed = MakeTimeStamp(ConvertDateTime($arRubric['LAST_EXECUTED'], 'DD.MM.YYYY HH:MI:SS'), 'DD.MM.YYYY HH:MI:SS');
89
90 if ($last_executed <= 0)
91 {
92 continue;
93 }
94
95 //parse schedule
96 $arDoM = CPostingTemplate::ParseDaysOfMonth($arRubric['DAYS_OF_MONTH']);
97 $arDoW = CPostingTemplate::ParseDaysOfWeek($arRubric['DAYS_OF_WEEK']);
98 $arToD = CPostingTemplate::ParseTimesOfDay($arRubric['TIMES_OF_DAY']);
99 if ($arToD)
100 {
101 sort($arToD, SORT_NUMERIC);
102 }
103 //sdate = truncate(last_execute)
104 $arSDate = localtime($last_executed);
105 $sdate = mktime(0, 0, 0, $arSDate[4] + 1, $arSDate[3], $arSDate[5] + 1900);
106 while ($sdate < $current_time && $time_of_exec === false)
107 {
108 $arSDate = localtime($sdate);
109 if ($arSDate[6] == 0)
110 {
111 $arSDate[6] = 7;
112 }
113 //determine if date is good for execution
114 if ($arDoM)
115 {
116 $flag = array_search($arSDate[3], $arDoM);
117 if ($arDoW)
118 {
119 $flag = array_search($arSDate[6], $arDoW);
120 }
121 }
122 elseif ($arDoW)
123 {
124 $flag = array_search($arSDate[6], $arDoW);
125 }
126 else
127 {
128 $flag = false;
129 }
130
131 if ($flag !== false && $arToD)
132 {
133 foreach ($arToD as $intToD)
134 {
135 if ($sdate + $intToD > $last_executed && $sdate + $intToD <= $current_time)
136 {
137 $time_of_exec = $sdate + $intToD;
138 break;
139 }
140 }
141 }
142 $sdate = mktime(0, 0, 0, date('m',$sdate), date('d',$sdate) + 1, date('Y',$sdate));//next day
143 }
144 if ($time_of_exec !== false)
145 {
146 $arRubric['START_TIME'] = ConvertTimeStamp($last_executed, 'FULL');
147 $arRubric['END_TIME'] = ConvertTimeStamp($time_of_exec, 'FULL');
148 $arRubric['SITE_ID'] = $arRubric['LID'];
150 }
151 $result = 'CPostingTemplate::Execute();';
152 }
153 return $result;
154 }
155
156 public static function AddPosting($arRubric)
157 {
158 global $DB, $USER;
159 if (!is_object($USER))
160 {
161 $USER = new CUser;
162 }
163 //Include language file for template.php
164 $rsSite = CSite::GetByID($arRubric['SITE_ID']);
165 $arSite = $rsSite->Fetch();
166
167 $strBody = '';
168 $arFields = false;
169 if (CPostingTemplate::IsExists($arRubric['TEMPLATE']))
170 {
171 //Execute template
172 $strFileName = $_SERVER['DOCUMENT_ROOT'] . '/' . $arRubric['TEMPLATE'] . '/template.php';
173 if (file_exists($strFileName))
174 {
175 \Bitrix\Main\Localization\Loc::loadLanguageFile($strFileName, $arSite['LANGUAGE_ID']);
176 ob_start();
177 $arFields = @include $strFileName;
178 $strBody = ob_get_contents();
179 ob_end_clean();
180 }
181 }
182 $ID = false;
183 //If there was an array returned then add posting
184 if (is_array($arFields))
185 {
186 $rsLang = CLanguage::GetByID($arSite['LANGUAGE_ID']);
187 $arLang = $rsLang->Fetch();
188
189 $arFields['BODY'] = $strBody;
190 $cPosting = new CPosting;
191 $arFields['AUTO_SEND_TIME'] = $arRubric['END_TIME'];
192 $arFields['RUB_ID'] = [$arRubric['ID']];
193 $arFields['MSG_CHARSET'] = $arLang['CHARSET'];
194 $ID = $cPosting->Add($arFields);
195 if ($ID)
196 {
197 if (array_key_exists('FILES', $arFields))
198 {
199 foreach ($arFields['FILES'] as $arFile)
200 {
201 $cPosting->SaveFile($ID, $arFile);
202 }
203 }
204 if (!array_key_exists('DO_NOT_SEND', $arFields) || $arFields['DO_NOT_SEND'] != 'Y')
205 {
206 $cPosting->ChangeStatus($ID, 'P');
207 if (COption::GetOptionString('subscribe', 'subscribe_auto_method') !== 'cron')
208 {
209 CAgent::AddAgent('CPosting::AutoSend(' . $ID . ',true,"' . $arRubric['LID'] . '");', 'subscribe', 'N', 0, $arRubric['END_TIME'], 'Y', $arRubric['END_TIME']);
210 }
211 }
212 }
213 }
214 //Update last execution time mark
215 $strSql = 'UPDATE b_list_rubric SET LAST_EXECUTED=' . $DB->CharToDateFunction($arRubric['END_TIME']) . ' WHERE ID=' . intval($arRubric['ID']);
216 $DB->Query($strSql);
217 return $ID;
218 }
219
220 public static function ParseDaysOfMonth($strDaysOfMonth)
221 {
222 $arResult = [];
223 if ($strDaysOfMonth <> '')
224 {
225 $arDoM = explode(',', $strDaysOfMonth);
226 $arFound = [];
227 foreach ($arDoM as $strDoM)
228 {
229 if (preg_match('/^(\d{1,2})$/', trim($strDoM), $arFound))
230 {
231 if (intval($arFound[1]) < 1 || intval($arFound[1]) > 31)
232 {
233 return false;
234 }
235 else
236 {
237 $arResult[] = intval($arFound[1]);
238 }
239 }
240 elseif (preg_match('/^(\d{1,2})-(\d{1,2})$/', trim($strDoM), $arFound))
241 {
242 if (intval($arFound[1]) < 1 || intval($arFound[1]) > 31 || intval($arFound[2]) < 1 || intval($arFound[2]) > 31 || intval($arFound[1]) >= intval($arFound[2]))
243 {
244 return false;
245 }
246 else
247 {
248 for ($i = intval($arFound[1]);$i <= intval($arFound[2]);$i++)
249 {
250 $arResult[] = intval($i);
251 }
252 }
253 }
254 else
255 {
256 return false;
257 }
258 }
259 }
260 else
261 {
262 return false;
263 }
264 return $arResult;
265 }
266
267 public static function ParseDaysOfWeek($strDaysOfWeek)
268 {
269 if ($strDaysOfWeek == '')
270 {
271 return false;
272 }
273
274 $arResult = [];
275
276 $arDoW = explode(',', $strDaysOfWeek);
277 foreach ($arDoW as $strDoW)
278 {
279 $arFound = [];
280 if (
281 preg_match('/^(\d)$/', trim($strDoW), $arFound)
282 && $arFound[1] >= 1
283 && $arFound[1] <= 7
284 )
285 {
286 $arResult[] = intval($arFound[1]);
287 }
288 else
289 {
290 return false;
291 }
292 }
293
294 return $arResult;
295 }
296
297 public static function ParseTimesOfDay($strTimesOfDay)
298 {
299 if ($strTimesOfDay == '')
300 {
301 return false;
302 }
303
304 $arResult = [];
305
306 $arToD = explode(',', $strTimesOfDay);
307 foreach ($arToD as $strToD)
308 {
309 $arFound = [];
310 if (
311 preg_match('/^(\d{1,2}):(\d{1,2})$/', trim($strToD), $arFound)
312 && $arFound[1] <= 23
313 && $arFound[2] <= 59
314 )
315 {
316 $arResult[] = intval($arFound[1]) * 3600 + intval($arFound[2]) * 60;
317 }
318 else
319 {
320 return false;
321 }
322 }
323
324 return $arResult;
325 }
326}
$path
Определения access_edit.php:21
$arResult
Определения generate_coupon.php:16
static loadLanguageFile($file, $language=null, $normalize=true)
Определения loc.php:225
static loadMessages($file)
Определения loc.php:65
static GetByID($ID)
Определения language.php:99
static GetInstance()
Определения virtual_io.php:60
Определения posting.php:8
Определения template.php:5
static IsExists($path='')
Определения template.php:52
static GetList()
Определения template.php:9
static GetByID($path='')
Определения template.php:31
static ParseDaysOfWeek($strDaysOfWeek)
Определения template.php:267
$LAST_ERROR
Определения template.php:6
static AddPosting($arRubric)
Определения template.php:156
static Execute()
Определения template.php:75
static ParseTimesOfDay($strTimesOfDay)
Определения template.php:297
static ParseDaysOfMonth($strDaysOfMonth)
Определения template.php:220
static GetList($aSort=[], $aFilter=[])
Определения rubric.php:9
$arTemplate
Определения component_props.php:26
$arFields
Определения dblapprove.php:5
$template
Определения file_edit.php:49
$result
Определения get_property_values.php:14
if($ajaxMode) $ID
Определения get_user.php:27
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
global $DB
Определения cron_frame.php:29
global $USER
Определения csv_new_run.php:40
$io
Определения csv_new_run.php:98
ConvertDateTime($datetime, $to_format=false, $from_site=false, $bSearchInSitesOnly=false)
Определения tools.php:724
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
getLocalPath($path, $baseFolder="/bitrix")
Определения tools.php:5092
MakeTimeStamp($datetime, $format=false)
Определения tools.php:538
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$dir
Определения quickway.php:303
$i
Определения factura.php:643