PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/subscribe/classes/general/template.php

https://gitlab.com/alexprowars/bitrix
PHP | 286 lines | 249 code | 29 blank | 8 comment | 54 complexity | 2da7d68f34dd9571f925d6de338b4c4d MD5 | raw file
  1. <?
  2. IncludeModuleLangFile(__FILE__);
  3. class CPostingTemplate
  4. {
  5. var $LAST_ERROR = "";
  6. //Get list
  7. public static function GetList()
  8. {
  9. $io = CBXVirtualIo::GetInstance();
  10. $arTemplates = array();
  11. $dir = mb_substr(getLocalPath("php_interface/subscribe/templates", BX_PERSONAL_ROOT), 1); //cut leading slash
  12. $abs_dir = $_SERVER["DOCUMENT_ROOT"]."/".$dir;
  13. if ($io->DirectoryExists($abs_dir))
  14. {
  15. $d = $io->GetDirectory($abs_dir);
  16. foreach ($d->GetChildren() as $dir_entry)
  17. {
  18. if ($dir_entry->IsDirectory())
  19. {
  20. $arTemplates[] = $dir."/".$dir_entry->GetName();
  21. }
  22. }
  23. }
  24. return $arTemplates;
  25. }
  26. public static function GetByID($path="")
  27. {
  28. global $MESS;
  29. if(!CPostingTemplate::IsExists($path))
  30. return false;
  31. $arTemplate = array();
  32. $strFileName= $_SERVER["DOCUMENT_ROOT"]."/".$path."/lang/".LANGUAGE_ID."/description.php";
  33. if(file_exists($strFileName))
  34. include($strFileName);
  35. $strFileName= $_SERVER["DOCUMENT_ROOT"]."/".$path."/description.php";
  36. if(file_exists($strFileName))
  37. include($strFileName);
  38. $arTemplate["PATH"] = $path;
  39. return $arTemplate;
  40. }
  41. public static function IsExists($path="")
  42. {
  43. $io = CBXVirtualIo::GetInstance();
  44. $dir = mb_substr(getLocalPath("php_interface/subscribe/templates", BX_PERSONAL_ROOT), 1);
  45. if (mb_strpos($path, $dir."/") === 0)
  46. {
  47. $template = mb_substr($path, mb_strlen($dir) + 1);
  48. if(
  49. mb_strpos($template, "\0") !== false
  50. || mb_strpos($template, "\\") !== false
  51. || mb_strpos($template, "/") !== false
  52. || mb_strpos($template, "..") !== false
  53. )
  54. {
  55. return false;
  56. }
  57. return $io->DirectoryExists($_SERVER["DOCUMENT_ROOT"]."/".$path);
  58. }
  59. return false;
  60. }
  61. public static function Execute()
  62. {
  63. $rubrics = CRubric::GetList(array(), array("ACTIVE"=>"Y", "AUTO"=>"Y"));
  64. $current_time = time();
  65. $time_of_exec = false;
  66. $result = "";
  67. while(($arRubric=$rubrics->Fetch()) && $time_of_exec===false)
  68. {
  69. if ($arRubric["LAST_EXECUTED"] == '')
  70. continue;
  71. $last_executed = MakeTimeStamp(ConvertDateTime($arRubric["LAST_EXECUTED"], "DD.MM.YYYY HH:MI:SS"), "DD.MM.YYYY HH:MI:SS");
  72. if ($last_executed <= 0)
  73. continue;
  74. //parse schedule
  75. $arDoM = CPostingTemplate::ParseDaysOfMonth($arRubric["DAYS_OF_MONTH"]);
  76. $arDoW = CPostingTemplate::ParseDaysOfWeek($arRubric["DAYS_OF_WEEK"]);
  77. $arToD = CPostingTemplate::ParseTimesOfDay($arRubric["TIMES_OF_DAY"]);
  78. if($arToD)
  79. sort($arToD, SORT_NUMERIC);
  80. //sdate = truncate(last_execute)
  81. $arSDate = localtime($last_executed);
  82. $sdate = mktime(0, 0, 0, $arSDate[4]+1, $arSDate[3], $arSDate[5]+1900);
  83. while($sdate < $current_time && $time_of_exec===false)
  84. {
  85. $arSDate = localtime($sdate);
  86. if($arSDate[6]==0) $arSDate[6]=7;
  87. //determine if date is good for execution
  88. if($arDoM)
  89. {
  90. $flag = array_search($arSDate[3], $arDoM);
  91. if($arDoW)
  92. $flag = array_search($arSDate[6], $arDoW);
  93. }
  94. elseif($arDoW)
  95. $flag = array_search($arSDate[6], $arDoW);
  96. else
  97. $flag=false;
  98. if($flag!==false && $arToD)
  99. foreach($arToD as $intToD)
  100. {
  101. if($sdate+$intToD > $last_executed && $sdate+$intToD <= $current_time)
  102. {
  103. $time_of_exec = $sdate+$intToD;
  104. break;
  105. }
  106. }
  107. $sdate = mktime(0, 0, 0, date("m",$sdate), date("d",$sdate)+1, date("Y",$sdate));//next day
  108. }
  109. if($time_of_exec!==false)
  110. {
  111. $arRubric["START_TIME"] = ConvertTimeStamp($last_executed, "FULL");
  112. $arRubric["END_TIME"] = ConvertTimeStamp($time_of_exec, "FULL");
  113. $arRubric["SITE_ID"] = $arRubric["LID"];
  114. CPostingTemplate::AddPosting($arRubric);
  115. }
  116. $result = "CPostingTemplate::Execute();";
  117. }
  118. return $result;
  119. }
  120. public static function AddPosting($arRubric)
  121. {
  122. global $DB, $USER, $MESS;
  123. if(!is_object($USER)) $USER = new CUser;
  124. //Include language file for template.php
  125. $rsSite = CSite::GetByID($arRubric["SITE_ID"]);
  126. $arSite = $rsSite->Fetch();
  127. $rsLang = CLanguage::GetByID($arSite["LANGUAGE_ID"]);
  128. $arLang = $rsLang->Fetch();
  129. $strBody="";
  130. $arFields=false;
  131. if(CPostingTemplate::IsExists($arRubric["TEMPLATE"]))
  132. {
  133. $strFileName= $_SERVER["DOCUMENT_ROOT"]."/".$arRubric["TEMPLATE"]."/lang/".$arSite["LANGUAGE_ID"]."/template.php";
  134. if(file_exists($strFileName))
  135. include($strFileName);
  136. //Execute template
  137. $strFileName= $_SERVER["DOCUMENT_ROOT"]."/".$arRubric["TEMPLATE"]."/template.php";
  138. if(file_exists($strFileName))
  139. {
  140. ob_start();
  141. $arFields = @include($strFileName);
  142. $strBody = ob_get_contents();
  143. ob_end_clean();
  144. }
  145. }
  146. $ID = false;
  147. //If there was an array returned then add posting
  148. if(is_array($arFields))
  149. {
  150. $arFields["BODY"] = $strBody;
  151. $cPosting=new CPosting;
  152. $arFields["AUTO_SEND_TIME"]=$arRubric["END_TIME"];
  153. $arFields["RUB_ID"]=array($arRubric["ID"]);
  154. $arFields["MSG_CHARSET"] = $arLang["CHARSET"];
  155. $ID = $cPosting->Add($arFields);
  156. if($ID)
  157. {
  158. if(array_key_exists("FILES", $arFields))
  159. {
  160. foreach($arFields["FILES"] as $arFile)
  161. $cPosting->SaveFile($ID, $arFile);
  162. }
  163. if(!array_key_exists("DO_NOT_SEND", $arFields) || $arFields["DO_NOT_SEND"]!="Y")
  164. {
  165. $cPosting->ChangeStatus($ID, "P");
  166. if(COption::GetOptionString("subscribe", "subscribe_auto_method")!=="cron")
  167. CAgent::AddAgent("CPosting::AutoSend(".$ID.",true,\"".$arRubric["LID"]."\");", "subscribe", "N", 0, $arRubric["END_TIME"], "Y", $arRubric["END_TIME"]);
  168. }
  169. }
  170. }
  171. //Update last execution time mark
  172. $strSql = "UPDATE b_list_rubric SET LAST_EXECUTED=".$DB->CharToDateFunction($arRubric["END_TIME"])." WHERE ID=".intval($arRubric["ID"]);
  173. $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
  174. return $ID;
  175. }
  176. public static function ParseDaysOfMonth($strDaysOfMonth)
  177. {
  178. $arResult=array();
  179. if($strDaysOfMonth <> '')
  180. {
  181. $arDoM = explode(",", $strDaysOfMonth);
  182. $arFound = array();
  183. foreach($arDoM as $strDoM)
  184. {
  185. if(preg_match("/^(\d{1,2})$/", trim($strDoM), $arFound))
  186. {
  187. if(intval($arFound[1]) < 1 || intval($arFound[1]) > 31)
  188. return false;
  189. else
  190. $arResult[]=intval($arFound[1]);
  191. }
  192. elseif(preg_match("/^(\d{1,2})-(\d{1,2})$/", trim($strDoM), $arFound))
  193. {
  194. if(intval($arFound[1]) < 1 || intval($arFound[1]) > 31 || intval($arFound[2]) < 1 || intval($arFound[2]) > 31 || intval($arFound[1]) >= intval($arFound[2]))
  195. return false;
  196. else
  197. for($i=intval($arFound[1]);$i<=intval($arFound[2]);$i++)
  198. $arResult[]=intval($i);
  199. }
  200. else
  201. return false;
  202. }
  203. }
  204. else
  205. return false;
  206. return $arResult;
  207. }
  208. public static function ParseDaysOfWeek($strDaysOfWeek)
  209. {
  210. if($strDaysOfWeek == '')
  211. return false;
  212. $arResult = array();
  213. $arDoW = explode(",", $strDaysOfWeek);
  214. foreach($arDoW as $strDoW)
  215. {
  216. $arFound = array();
  217. if(
  218. preg_match("/^(\d)$/", trim($strDoW), $arFound)
  219. && $arFound[1] >= 1
  220. && $arFound[1] <= 7
  221. )
  222. {
  223. $arResult[]=intval($arFound[1]);
  224. }
  225. else
  226. {
  227. return false;
  228. }
  229. }
  230. return $arResult;
  231. }
  232. public static function ParseTimesOfDay($strTimesOfDay)
  233. {
  234. if($strTimesOfDay == '')
  235. return false;
  236. $arResult = array();
  237. $arToD = explode(",", $strTimesOfDay);
  238. foreach($arToD as $strToD)
  239. {
  240. $arFound = array();
  241. if(
  242. preg_match("/^(\d{1,2}):(\d{1,2})$/", trim($strToD), $arFound)
  243. && $arFound[1] <= 23
  244. && $arFound[2] <= 59
  245. )
  246. {
  247. $arResult[]=intval($arFound[1])*3600+intval($arFound[2])*60;
  248. }
  249. else
  250. {
  251. return false;
  252. }
  253. }
  254. return $arResult;
  255. }
  256. }
  257. ?>