PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/bitrix/modules/main/classes/general/site_template.php

https://gitlab.com/neuser/bitrix-core
PHP | 413 lines | 337 code | 64 blank | 12 comment | 82 complexity | 50cfb09cb535c209530241ee28833b2d MD5 | raw file
  1. <?php
  2. /**
  3. * Bitrix Framework
  4. * @package bitrix
  5. * @subpackage main
  6. * @copyright 2001-2013 Bitrix
  7. */
  8. class CSiteTemplate
  9. {
  10. var $LAST_ERROR;
  11. public static function GetList($arOrder=array(), $arFilter=array(), $arSelect=false)
  12. {
  13. /** @global CMain $APPLICATION */
  14. global $APPLICATION;
  15. if(isset($arFilter["ID"]) && !is_array($arFilter["ID"]))
  16. $arFilter["ID"] = array($arFilter["ID"]);
  17. if(isset($arFilter["TYPE"]) && !is_array($arFilter["TYPE"]))
  18. $arFilter["TYPE"] = array($arFilter["TYPE"]);
  19. $folders = array(
  20. "/local/templates",
  21. BX_PERSONAL_ROOT."/templates",
  22. );
  23. $arRes = array();
  24. foreach($folders as $folder)
  25. {
  26. $path = $_SERVER["DOCUMENT_ROOT"].$folder;
  27. if(is_dir($path))
  28. {
  29. $handle = opendir($path);
  30. if($handle)
  31. {
  32. while(($file = readdir($handle)) !== false)
  33. {
  34. if($file == "." || $file == ".." || !is_dir($path."/".$file))
  35. continue;
  36. if($file == ".default")
  37. continue;
  38. if(isset($arRes[$file]))
  39. continue;
  40. if(isset($arFilter["ID"]) && !in_array($file, $arFilter["ID"]))
  41. continue;
  42. $arTemplate = array("DESCRIPTION" => "");
  43. $fname = $path."/".$file."/description.php";
  44. if(file_exists(($fname)))
  45. {
  46. \Bitrix\Main\Localization\Loc::loadLanguageFile($fname);
  47. include($fname);
  48. }
  49. if(!isset($arTemplate["TYPE"])) $arTemplate["TYPE"] = '';
  50. if(isset($arFilter["TYPE"]) && !in_array($arTemplate["TYPE"], $arFilter["TYPE"]))
  51. continue;
  52. $arTemplate["ID"] = $file;
  53. $arTemplate["PATH"] = $folder."/".$file;
  54. if(!isset($arTemplate["NAME"]))
  55. $arTemplate["NAME"] = $file;
  56. if($arSelect === false || in_array("SCREENSHOT", $arSelect))
  57. {
  58. if(file_exists($path."/".$file."/lang/".LANGUAGE_ID."/screen.gif"))
  59. $arTemplate["SCREENSHOT"] = $folder."/".$file."/lang/".LANGUAGE_ID."/screen.gif";
  60. elseif(file_exists($path."/".$file."/screen.gif"))
  61. $arTemplate["SCREENSHOT"] = $folder."/".$file."/screen.gif";
  62. else
  63. $arTemplate["SCREENSHOT"] = false;
  64. if(file_exists($path."/".$file."/lang/".LANGUAGE_ID."/preview.gif"))
  65. $arTemplate["PREVIEW"] = $folder."/".$file."/lang/".LANGUAGE_ID."/preview.gif";
  66. elseif(file_exists($path."/".$file."/preview.gif"))
  67. $arTemplate["PREVIEW"] = $folder."/".$file."/preview.gif";
  68. else
  69. $arTemplate["PREVIEW"] = false;
  70. }
  71. if($arSelect === false || in_array("CONTENT", $arSelect))
  72. {
  73. $arTemplate["CONTENT"] = $APPLICATION->GetFileContent($path."/".$file."/header.php")."#WORK_AREA#".$APPLICATION->GetFileContent($path."/".$file."/footer.php");
  74. }
  75. if($arSelect === false || in_array("STYLES", $arSelect))
  76. {
  77. if(file_exists($path."/".$file."/styles.css"))
  78. {
  79. $arTemplate["STYLES"] = $APPLICATION->GetFileContent($path."/".$file."/styles.css");
  80. $arTemplate["STYLES_TITLE"] = CSiteTemplate::__GetByStylesTitle($path."/".$file."/.styles.php");
  81. }
  82. if(file_exists($path."/".$file."/template_styles.css"))
  83. $arTemplate["TEMPLATE_STYLES"] = $APPLICATION->GetFileContent($path."/".$file."/template_styles.css");
  84. }
  85. $arRes[$file] = $arTemplate;
  86. }
  87. closedir($handle);
  88. }
  89. }
  90. }
  91. if(is_array($arOrder))
  92. {
  93. $columns = array();
  94. static $fields = array("ID"=>1, "NAME"=>1, "DESCRIPTION"=>1, "SORT"=>1);
  95. foreach($arOrder as $key => $val)
  96. {
  97. $key = mb_strtoupper($key);
  98. if(isset($fields[$key]))
  99. {
  100. $columns[$key] = (mb_strtoupper($val) == "DESC"? SORT_DESC : SORT_ASC);
  101. }
  102. }
  103. if(!empty($columns))
  104. {
  105. \Bitrix\Main\Type\Collection::sortByColumn($arRes, $columns);
  106. }
  107. }
  108. $db_res = new CDBResult;
  109. $db_res->InitFromArray($arRes);
  110. return $db_res;
  111. }
  112. public static function __GetByStylesTitle($file)
  113. {
  114. $io = CBXVirtualIo::GetInstance();
  115. if ($io->FileExists($file))
  116. return include($file);
  117. return false;
  118. }
  119. public static function GetByID($ID)
  120. {
  121. return CSiteTemplate::GetList(array(), array("ID"=>$ID));
  122. }
  123. public function CheckFields($arFields, $ID=false)
  124. {
  125. /** @global CMain $APPLICATION */
  126. global $APPLICATION;
  127. $this->LAST_ERROR = "";
  128. $arMsg = array();
  129. if($ID === false)
  130. {
  131. if($arFields["ID"] == '')
  132. $this->LAST_ERROR .= GetMessage("MAIN_ENTER_TEMPLATE_ID")." ";
  133. elseif(file_exists($_SERVER["DOCUMENT_ROOT"].BX_PERSONAL_ROOT."/templates/".$arFields["ID"]))
  134. $this->LAST_ERROR .= GetMessage("MAIN_TEMPLATE_ID_EX")." ";
  135. if(!isset($arFields["CONTENT"]))
  136. $this->LAST_ERROR .= GetMessage("MAIN_TEMPLATE_CONTENT_NA")." ";
  137. }
  138. if(isset($arFields["CONTENT"]) && $arFields["CONTENT"] == '')
  139. {
  140. $this->LAST_ERROR .= GetMessage("MAIN_TEMPLATE_CONTENT_NA")." ";
  141. $arMsg[] = array("id"=>"CONTENT", "text"=> GetMessage("MAIN_TEMPLATE_CONTENT_NA"));
  142. }
  143. elseif(isset($arFields["CONTENT"]) && mb_strpos($arFields["CONTENT"], "#WORK_AREA#") === false)
  144. {
  145. $this->LAST_ERROR .= GetMessage("MAIN_TEMPLATE_WORKAREA_NA")." ";
  146. $arMsg[] = array("id"=>"CONTENT", "text"=> GetMessage("MAIN_TEMPLATE_WORKAREA_NA"));
  147. }
  148. if(!empty($arMsg))
  149. {
  150. $e = new CAdminException($arMsg);
  151. $APPLICATION->ThrowException($e);
  152. }
  153. if($this->LAST_ERROR <> '')
  154. return false;
  155. return true;
  156. }
  157. public function Add($arFields)
  158. {
  159. if(!$this->CheckFields($arFields))
  160. return false;
  161. /** @global CMain $APPLICATION */
  162. global $APPLICATION;
  163. $path = BX_PERSONAL_ROOT."/templates/".$arFields["ID"];
  164. CheckDirPath($_SERVER["DOCUMENT_ROOT"].$path);
  165. if(isset($arFields["CONTENT"]))
  166. {
  167. $p = mb_strpos($arFields["CONTENT"], "#WORK_AREA#");
  168. $header = mb_substr($arFields["CONTENT"], 0, $p);
  169. $APPLICATION->SaveFileContent($_SERVER["DOCUMENT_ROOT"].$path."/header.php", $header);
  170. $footer = mb_substr($arFields["CONTENT"], $p + mb_strlen("#WORK_AREA#"));
  171. $APPLICATION->SaveFileContent($_SERVER["DOCUMENT_ROOT"].$path."/footer.php", $footer);
  172. }
  173. if(isset($arFields["STYLES"]))
  174. {
  175. $APPLICATION->SaveFileContent($_SERVER["DOCUMENT_ROOT"].$path."/styles.css", $arFields["STYLES"]);
  176. }
  177. if(isset($arFields["TEMPLATE_STYLES"]))
  178. {
  179. $APPLICATION->SaveFileContent($_SERVER["DOCUMENT_ROOT"].$path."/template_styles.css", $arFields["TEMPLATE_STYLES"]);
  180. }
  181. if(isset($arFields["NAME"]) || isset($arFields["DESCRIPTION"]) || isset($arFields["SORT"]))
  182. {
  183. self::SaveDescription($arFields, $_SERVER["DOCUMENT_ROOT"].$path."/description.php");
  184. }
  185. self::SaveStyleDescription($arFields["STYLES_DESCRIPTION"], $_SERVER["DOCUMENT_ROOT"].$path."/.styles.php");
  186. return $arFields["ID"];
  187. }
  188. public function Update($ID, $arFields)
  189. {
  190. /** @global CMain $APPLICATION */
  191. global $APPLICATION;
  192. if(!$this->CheckFields($arFields, $ID))
  193. {
  194. return false;
  195. }
  196. $path = getLocalPath("templates/".$ID, BX_PERSONAL_ROOT);
  197. if($path === false)
  198. {
  199. return false;
  200. }
  201. if(isset($arFields["CONTENT"]))
  202. {
  203. $p = mb_strpos($arFields["CONTENT"], "#WORK_AREA#");
  204. $header = mb_substr($arFields["CONTENT"], 0, $p);
  205. $APPLICATION->SaveFileContent($_SERVER["DOCUMENT_ROOT"].$path."/header.php", $header);
  206. $footer = mb_substr($arFields["CONTENT"], $p + mb_strlen("#WORK_AREA#"));
  207. $APPLICATION->SaveFileContent($_SERVER["DOCUMENT_ROOT"].$path."/footer.php", $footer);
  208. }
  209. if(isset($arFields["STYLES"]))
  210. {
  211. $APPLICATION->SaveFileContent($_SERVER["DOCUMENT_ROOT"].$path."/styles.css", $arFields["STYLES"]);
  212. }
  213. if(isset($arFields["TEMPLATE_STYLES"]))
  214. {
  215. $APPLICATION->SaveFileContent($_SERVER["DOCUMENT_ROOT"].$path."/template_styles.css", $arFields["TEMPLATE_STYLES"]);
  216. }
  217. if(isset($arFields["NAME"]) || isset($arFields["DESCRIPTION"]) || isset($arFields["SORT"]) || isset($arFields["TYPE"]))
  218. {
  219. $db_t = CSiteTemplate::GetList(array(), array("ID" => $ID), array("NAME", "DESCRIPTION", "SORT"));
  220. $ar_t = $db_t->Fetch();
  221. if(!isset($arFields["NAME"]))
  222. $arFields["NAME"] = $ar_t["NAME"];
  223. if(!isset($arFields["DESCRIPTION"]))
  224. $arFields["DESCRIPTION"] = $ar_t["DESCRIPTION"];
  225. if(!isset($arFields["SORT"]))
  226. $arFields["SORT"] = $ar_t["SORT"];
  227. if(!isset($arFields["TYPE"]))
  228. $arFields["TYPE"] = $ar_t["TYPE"];
  229. if(!isset($arFields["EDITOR_STYLES"]))
  230. $arFields["EDITOR_STYLES"] = $ar_t["EDITOR_STYLES"];
  231. self::SaveDescription($arFields, $_SERVER["DOCUMENT_ROOT"].$path."/description.php");
  232. }
  233. self::SaveStyleDescription($arFields["STYLES_DESCRIPTION"], $_SERVER["DOCUMENT_ROOT"].$path."/.styles.php");
  234. return true;
  235. }
  236. public static function Delete($ID)
  237. {
  238. if($ID == ".default")
  239. {
  240. return false;
  241. }
  242. $path = getLocalPath("templates/".$ID, BX_PERSONAL_ROOT);
  243. if($path === false)
  244. {
  245. return false;
  246. }
  247. DeleteDirFilesEx($path);
  248. return true;
  249. }
  250. public static function GetContent($ID)
  251. {
  252. if($ID == '')
  253. $arRes = array();
  254. else
  255. $arRes = CSiteTemplate::DirsRecursive($ID);
  256. $db_res = new CDBResult;
  257. $db_res->InitFromArray($arRes);
  258. return $db_res;
  259. }
  260. public static function DirsRecursive($ID, $path="", $depth=0, $maxDepth=1)
  261. {
  262. $arRes = array();
  263. $depth++;
  264. $templPath = getLocalPath("templates/".$ID, BX_PERSONAL_ROOT);
  265. if($templPath === false)
  266. {
  267. return $arRes;
  268. }
  269. GetDirList($templPath."/".$path, $arDirsTmp, $arResTmp);
  270. foreach($arResTmp as $file)
  271. {
  272. switch($file["NAME"])
  273. {
  274. case "chain_template.php":
  275. $file["DESCRIPTION"] = GetMessage("MAIN_TEMPLATE_NAV");
  276. break;
  277. case "":
  278. $file["DESCRIPTION"] = "";
  279. break;
  280. default:
  281. if(($p = mb_strpos($file["NAME"], ".menu_template.php"))!==false)
  282. $file["DESCRIPTION"] = str_replace("#MENU_TYPE#", mb_substr($file["NAME"], 0, $p), GetMessage("MAIN_TEMPLATE_MENU"));
  283. elseif(($p = mb_strpos($file["NAME"], "authorize_registration.php"))!==false)
  284. $file["DESCRIPTION"] = GetMessage("MAIN_TEMPLATE_AUTH_REG");
  285. elseif(($p = mb_strpos($file["NAME"], "forgot_password.php"))!==false)
  286. $file["DESCRIPTION"] = GetMessage("MAIN_TEMPLATE_SEND_PWD");
  287. elseif(($p = mb_strpos($file["NAME"], "change_password.php"))!==false)
  288. $file["DESCRIPTION"] = GetMessage("MAIN_TEMPLATE_CHN_PWD");
  289. elseif(($p = mb_strpos($file["NAME"], "authorize.php"))!==false)
  290. $file["DESCRIPTION"] = GetMessage("MAIN_TEMPLATE_AUTH");
  291. elseif(($p = mb_strpos($file["NAME"], "registration.php"))!==false)
  292. $file["DESCRIPTION"] = GetMessage("MAIN_TEMPLATE_REG");
  293. }
  294. $arRes[] = $file;
  295. }
  296. $nTemplateLen = mb_strlen($templPath."/");
  297. foreach($arDirsTmp as $dir)
  298. {
  299. $arDir = $dir;
  300. $arDir["DEPTH_LEVEL"] = $depth;
  301. $arRes[] = $arDir;
  302. if($depth < $maxDepth)
  303. {
  304. $dirPath = mb_substr($arDir["ABS_PATH"], $nTemplateLen);
  305. $arRes = array_merge($arRes, CSiteTemplate::DirsRecursive($ID, $dirPath, $depth, $maxDepth));
  306. }
  307. }
  308. return $arRes;
  309. }
  310. public static function SaveStyleDescription($stylesDesc = array(), $stylesPath)
  311. {
  312. /** @global CMain $APPLICATION */
  313. global $APPLICATION;
  314. if(isset($stylesDesc) && is_array($stylesDesc))
  315. {
  316. $curStylesDesc = CSiteTemplate::__GetByStylesTitle($stylesPath);
  317. if (is_array($curStylesDesc))
  318. {
  319. foreach($curStylesDesc as $code => $val)
  320. {
  321. if (!is_array($curStylesDesc[$code]))
  322. unset($curStylesDesc[$code]);
  323. }
  324. }
  325. foreach($stylesDesc as $code => $val)
  326. {
  327. if (!isset($curStylesDesc[EscapePHPString($code)]) || !is_array($curStylesDesc[EscapePHPString($code)]))
  328. {
  329. $curStylesDesc[EscapePHPString($code)] = EscapePHPString($val);
  330. }
  331. }
  332. $APPLICATION->SaveFileContent($stylesPath, '<'.'?'."\nreturn ".var_export($curStylesDesc, 1).";\n".'?'.'>');
  333. }
  334. }
  335. public static function SaveDescription($arFields = array(), $descPath)
  336. {
  337. /** @global CMain $APPLICATION */
  338. global $APPLICATION;
  339. $arDescription = array(
  340. "NAME" => $arFields['NAME'],
  341. "DESCRIPTION" => $arFields['DESCRIPTION'],
  342. "SORT" => (intval($arFields['SORT']) > 0? intval($arFields['SORT']) : ''),
  343. "TYPE" => $arFields['TYPE']
  344. );
  345. if (isset($arFields['EDITOR_STYLES']))
  346. $arDescription["EDITOR_STYLES"] = $arFields['EDITOR_STYLES'];
  347. $APPLICATION->SaveFileContent($descPath, '<'.'?'."\n\$arTemplate = ".var_export($arDescription, 1).";\n".'?'.'>');
  348. }
  349. }