PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/main/public/template_copy.php

https://gitlab.com/alexprowars/bitrix
PHP | 341 lines | 285 code | 36 blank | 20 comment | 97 complexity | ebe96c46b3f5726d28a1f87a02846072 MD5 | raw file
  1. <?
  2. /**
  3. * Bitrix Framework
  4. * @package bitrix
  5. * @subpackage main
  6. * @copyright 2001-2013 Bitrix
  7. */
  8. /**
  9. * Bitrix vars
  10. * @global CUser $USER
  11. * @global CMain $APPLICATION
  12. */
  13. require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_before.php");
  14. require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_js.php");
  15. $io = CBXVirtualIo::GetInstance();
  16. $src_path = $io->CombinePath("/", $_GET["src_path"]);
  17. $src_line = intval($_GET["src_line"]);
  18. $template_site_template = $_GET["template_site_template"];
  19. if(!$USER->CanDoOperation('edit_php') && !$USER->CanDoFileOperation('fm_lpa', array($_GET["src_site"], $src_path)))
  20. die(GetMessage("ACCESS_DENIED"));
  21. IncludeModuleLangFile(__FILE__);
  22. CUtil::JSPostUnescape();
  23. $obJSPopup = new CJSPopup('',
  24. array(
  25. 'TITLE' => GetMessage("template_copy_title"),
  26. 'ARGS' => 'component_name='.urlencode($_GET["component_name"]).
  27. '&amp;component_template='.urlencode($_GET["component_template"]).
  28. '&amp;template_id='.urlencode($_GET["template_id"]).
  29. '&amp;lang='.urlencode(LANGUAGE_ID).
  30. '&amp;template_site_template='.urlencode($template_site_template).
  31. '&amp;src_path='.urlencode($_GET["src_path"]).
  32. '&amp;src_line='.intval($_GET["src_line"]).
  33. '&amp;src_site='.intval($_GET["src_site"]).
  34. '&amp;edit_file='.urlencode($_GET["edit_file"]).
  35. '&amp;back_path='.urlencode($_GET["back_path"]).
  36. '&amp;action=save'
  37. )
  38. );
  39. $strWarning = "";
  40. $arTemplate = false;
  41. $aComponent = false;
  42. $filesrc = "";
  43. $abs_path = "";
  44. // try to read parameters from script file
  45. /* Try to open script containing the component call */
  46. if(!$src_path || $src_line <= 0)
  47. {
  48. $strWarning .= GetMessage("comp_prop_err_param")."<br>";
  49. }
  50. else
  51. {
  52. $abs_path = $io->RelativeToAbsolutePath($src_path);
  53. $f = $io->GetFile($abs_path);
  54. $filesrc = $f->GetContents();
  55. if(!$filesrc || $filesrc == "")
  56. $strWarning .= GetMessage("comp_prop_err_open")."<br>";
  57. }
  58. $arComponent = false;
  59. if($strWarning == "")
  60. {
  61. /* parse source file for PHP code */
  62. $arComponents = PHPParser::ParseScript($filesrc);
  63. /* identify the component by line number */
  64. for ($i = 0, $cnt = count($arComponents); $i < $cnt; $i++)
  65. {
  66. $nLineFrom = substr_count(mb_substr($filesrc, 0, $arComponents[$i]["START"]), "\n") + 1;
  67. $nLineTo = substr_count(mb_substr($filesrc, 0, $arComponents[$i]["END"]), "\n") + 1;
  68. if ($nLineFrom <= $src_line && $nLineTo >= $src_line)
  69. {
  70. if ($arComponents[$i]["DATA"]["COMPONENT_NAME"] == $_GET["component_name"])
  71. {
  72. $arComponent = $arComponents[$i];
  73. break;
  74. }
  75. }
  76. if ($nLineTo > $src_line)
  77. break;
  78. }
  79. }
  80. if ($arComponent === false)
  81. $strWarning .= GetMessage("comp_prop_err_comp")."<br>";
  82. $arComponentDescription = array();
  83. $arTemplatesList = array();
  84. $templateSiteTemplate = "";
  85. if($strWarning == "")
  86. {
  87. $arComponentDescription = CComponentUtil::GetComponentDescr($_GET["component_name"]);
  88. $arComponentParameters = CComponentUtil::GetComponentProps($_GET["component_name"], $arComponent["DATA"]["PARAMS"]);
  89. $arTemplateParameters = CComponentUtil::GetTemplateProps($_GET["component_name"], $_GET["component_template"], $_GET["template_id"], $arComponent["DATA"]["PARAMS"]);
  90. $arParameterGroups = array();
  91. if (isset($arComponentParameters["GROUPS"]) && is_array($arComponentParameters["GROUPS"]))
  92. $arParameterGroups = $arParameterGroups + $arComponentParameters["GROUPS"];
  93. if (isset($arTemplateParameters) && is_array($arTemplateParameters))
  94. $arParameterGroups = $arParameterGroups + array("TEMPLATE" => array("NAME" => GetMessage("comp_templ_template")));
  95. $arParameters = array();
  96. if (isset($arComponentParameters["PARAMETERS"]) && is_array($arComponentParameters["PARAMETERS"]))
  97. $arParameters = $arParameters + $arComponentParameters["PARAMETERS"];
  98. if (isset($arTemplateParameters) && is_array($arTemplateParameters))
  99. $arParameters = $arParameters + $arTemplateParameters;
  100. $arTemplatesList = CComponentUtil::GetTemplatesList($_GET["component_name"], $_GET["template_id"]);
  101. for ($i = 0, $cnt = count($arTemplatesList); $i < $cnt; $i++)
  102. {
  103. if($arComponent["DATA"]["TEMPLATE_NAME"]<>"" && $arTemplatesList[$i]["NAME"] == $arComponent["DATA"]["TEMPLATE_NAME"]
  104. || $arComponent["DATA"]["TEMPLATE_NAME"]=="" && $arTemplatesList[$i]["NAME"] == ".default")
  105. {
  106. $templateSiteTemplate = $arTemplatesList[$i]["TEMPLATE"];
  107. break;
  108. }
  109. }
  110. /* save parameters to file */
  111. if($_SERVER["REQUEST_METHOD"] == "POST" && $_REQUEST["action"] == "save" && $arComponent !== false && $arComponentDescription !== false && check_bitrix_sessid())
  112. {
  113. //check template name
  114. $sTemplateName = trim($_POST["TEMPLATE_NAME"]);
  115. if($sTemplateName == '' || !CBitrixComponentTemplate::CheckName($sTemplateName))
  116. $sTemplateName = '.default';
  117. if ($_POST["SITE_TEMPLATE"] != $_GET["template_id"] && $_POST["SITE_TEMPLATE"] != ".default")
  118. $_POST["USE_TEMPLATE"] = "N";
  119. if (CComponentUtil::CopyTemplate($arComponent["DATA"]["COMPONENT_NAME"], $arComponent["DATA"]["TEMPLATE_NAME"], (($templateSiteTemplate <> '') ? $templateSiteTemplate : false), $_POST["SITE_TEMPLATE"], $sTemplateName, false))
  120. {
  121. if ($_POST["USE_TEMPLATE"] == "Y")
  122. {
  123. $code = ($arComponent["DATA"]["VARIABLE"]?$arComponent["DATA"]["VARIABLE"]."=":"").
  124. "\$APPLICATION->IncludeComponent(\"".$arComponent["DATA"]["COMPONENT_NAME"]."\", ".
  125. "\"".$sTemplateName."\", ".
  126. "Array(\n\t".PHPParser::ReturnPHPStr2($arComponent["DATA"]["PARAMS"], $arParameters)."\n\t)".
  127. ",\n\t".($arComponent["DATA"]["PARENT_COMP"] <> ''? $arComponent["DATA"]["PARENT_COMP"] : "false").
  128. (!empty($arComponent["DATA"]["FUNCTION_PARAMS"])? ",\n\t"."array(\n\t".PHPParser::ReturnPHPStr2($arComponent["DATA"]["FUNCTION_PARAMS"])."\n\t)" : "").
  129. "\n);";
  130. $filesrc_for_save = mb_substr($filesrc, 0, $arComponent["START"]).$code.mb_substr($filesrc, $arComponent["END"]);
  131. if(!$APPLICATION->SaveFileContent($abs_path, $filesrc_for_save))
  132. $strWarning .= GetMessage("comp_prop_err_save")."<br>";
  133. }
  134. if($strWarning == "")
  135. {
  136. $strJSText = 'window.location = window.location.href;';
  137. if ($_POST["EDIT_TEMPLATE"] == "Y")
  138. {
  139. $component = new CBitrixComponent();
  140. if ($component->InitComponent($arComponent["DATA"]["COMPONENT_NAME"], $_POST["TEMPLATE_NAME"]))
  141. {
  142. if ($component->InitComponentTemplate($_REQUEST["edit_file"], $_POST["SITE_TEMPLATE"]))
  143. {
  144. $template = & $component->GetTemplate();
  145. if (!is_null($template))
  146. {
  147. $strJSText = $APPLICATION->GetPopupLink(
  148. array(
  149. 'URL' => '/bitrix/admin/public_file_edit_src.php?lang='.LANGUAGE_ID.'&site='.SITE_ID.'&back_url='.urlencode($_REQUEST["back_path"]).'&path='.urlencode($template->GetFile()),
  150. "PARAMS" => Array("width" => 770, "height" => 570,"resize" => true,"dialog_type" => 'EDITOR', "min_width" => 700, "min_height" => 400),
  151. )
  152. );
  153. }
  154. }
  155. }
  156. }
  157. ?>
  158. <script>
  159. <?=$obJSPopup->jsPopup?>.Close();
  160. //setTimeout(BX.showWait(), 30);
  161. <?=$strJSText?>
  162. </script>
  163. <?
  164. die();
  165. }
  166. }
  167. else
  168. {
  169. if ($ex = $APPLICATION->GetException())
  170. $strWarning .= $ex->GetString()."<br>";
  171. else
  172. $strWarning .= GetMessage("comp_templ_error_copy")."<br>";
  173. }
  174. }
  175. }
  176. $componentPath = CComponentEngine::MakeComponentPath($_GET["component_name"]);
  177. $arComponentDescription["ICON"] = ltrim($arComponentDescription["ICON"], "/");
  178. $localPath = getLocalPath("components".$componentPath);
  179. if($localPath !== false && $arComponentDescription["ICON"] <> "" && $io->FileExists($io->RelativeToAbsolutePath($localPath."/".$arComponentDescription["ICON"])))
  180. $sIcon = $localPath."/".$arComponentDescription["ICON"];
  181. else
  182. $sIcon = "/bitrix/images/fileman/htmledit2/component.gif";
  183. $sCurrentTemplateName = ($arComponent["DATA"]["TEMPLATE_NAME"] <> ""? htmlspecialcharsbx($arComponent["DATA"]["TEMPLATE_NAME"]) : ".default");
  184. $obJSPopup->ShowTitlebar();
  185. $obJSPopup->StartDescription($sIcon);
  186. ?>
  187. <?if($arComponentDescription["NAME"] <> ""):?>
  188. <p title="<?echo GetMessage("comp_prop_name")?>"><b><?echo htmlspecialcharsbx($arComponentDescription["NAME"])?></b></p>
  189. <?endif;?>
  190. <?if($arComponentDescription["DESCRIPTION"] <> ""):?>
  191. <p title="<?echo GetMessage("comp_prop_desc")?>"><?echo htmlspecialcharsbx($arComponentDescription["DESCRIPTION"])?></p>
  192. <?endif;?>
  193. <p class="note" title="<?echo GetMessage("comp_prop_path")?>"><a href="/bitrix/admin/fileman_admin.php?lang=<?echo LANGUAGE_ID?>&amp;path=<?echo urlencode($localPath)?>"><?echo htmlspecialcharsbx($_GET["component_name"])?></a></p>
  194. <?
  195. if($_GET['system_template'] == 'Y')
  196. ShowNote(GetMessage("copy_comp_sys_templ"));
  197. if($strWarning <> "")
  198. {
  199. //ShowError($strWarning);
  200. $obJSPopup->ShowValidationError($strWarning);
  201. echo '<script>jsPopup.AdjustShadow()</script>';
  202. }
  203. ?>
  204. <?
  205. $obJSPopup->StartContent();
  206. ?>
  207. <input type="hidden" name="action" value="save" />
  208. <script>
  209. window.CheckSiteTemplate = function(el)
  210. {
  211. var bList = (el.id == 'SITE_TEMPLATE_sel');
  212. if(el.form.USE_TEMPLATE)
  213. {
  214. el.form.USE_TEMPLATE.disabled = bList;
  215. el.form.USE_TEMPLATE.checked = !bList;
  216. }
  217. el.form.SITE_TEMPLATE[el.form.SITE_TEMPLATE.length-1].disabled = !bList;
  218. }
  219. </script>
  220. <table cellspacing="0" class="bx-width100">
  221. <tr>
  222. <td class="bx-popup-label bx-width50"><?= GetMessage("comp_templ_cur_template") ?>:</td>
  223. <td><b><?=$sCurrentTemplateName?></b><?if($templateSiteTemplate==""):?> (<?echo GetMessage("comp_templ_system")?>)<?endif?></td>
  224. </tr>
  225. <?
  226. $arSiteTemplates = array(".default"=>GetMessage("comp_templ_def_templ"));
  227. $db_site_templates = CSiteTemplate::GetList(array("sort"=>"asc", "name"=>"asc"), array(), array());
  228. while($ar_site_templates = $db_site_templates->Fetch())
  229. $arSiteTemplates[$ar_site_templates['ID']] = $ar_site_templates['NAME'];
  230. if($templateSiteTemplate<>""):
  231. $sSiteTemplate = $arSiteTemplates[$templateSiteTemplate];
  232. ?>
  233. <tr>
  234. <td class="bx-popup-label bx-width50"><?= GetMessage("comp_templ_cur_site_template")?>:</td>
  235. <td><b><?= htmlspecialcharsbx($templateSiteTemplate)?></b><?if($sSiteTemplate <> "") echo " (".htmlspecialcharsbx($sSiteTemplate).")"?></td>
  236. </tr>
  237. <?
  238. endif;
  239. ?>
  240. <tr>
  241. <td class="bx-popup-label bx-width50"><?= GetMessage("comp_templ_new_tpl") ?>:</td>
  242. <td>
  243. <?
  244. $sParentComp = mb_strtolower($arComponent["DATA"]["PARENT_COMP"]);
  245. $bParentComp = ($sParentComp <> "" && $sParentComp !== "false" && $sParentComp !== "null");
  246. if(!$bParentComp):
  247. //find next template name
  248. $def = ($arComponent["DATA"]["TEMPLATE_NAME"] <> '' && $arComponent["DATA"]["TEMPLATE_NAME"]<>".default"? rtrim($arComponent["DATA"]["TEMPLATE_NAME"], "0..9") : "template");
  249. if($def == '')
  250. $def = "template";
  251. $max = 0;
  252. foreach($arTemplatesList as $templ)
  253. if(mb_strpos($templ["NAME"], $def) === 0 && ($v = intval(mb_substr($templ["NAME"], mb_strlen($def))))>$max)
  254. $max = $v;
  255. ?>
  256. <input type="text" name="TEMPLATE_NAME" value="<?echo ($_REQUEST["TEMPLATE_NAME"] <> ''? htmlspecialcharsbx($_REQUEST["TEMPLATE_NAME"]) : htmlspecialcharsbx($def).($max+1)); ?>">
  257. <?else:?>
  258. <?echo $sCurrentTemplateName?>
  259. <input type="hidden" name="TEMPLATE_NAME" value="<?echo $sCurrentTemplateName?>">
  260. <?endif;?>
  261. </td>
  262. </tr>
  263. <tr>
  264. <td class="bx-popup-label bx-width50" valign="top"><?= GetMessage("comp_templ_new_template") ?>:</td>
  265. <td>
  266. <input type="radio" name="SITE_TEMPLATE" value=".default" id="SITE_TEMPLATE_def"<?if($_REQUEST["SITE_TEMPLATE"] == "" || $_REQUEST["SITE_TEMPLATE"] == ".default") echo " checked"?> onclick="CheckSiteTemplate(this)"><label for="SITE_TEMPLATE_def"><?echo GetMessage("template_copy_def")?> / .default (<?echo GetMessage("comp_templ_def_templ")?>)</label><br>
  267. <?if($_GET["template_id"] <> "" && $_GET["template_id"] <> ".default"):?>
  268. <input type="radio" name="SITE_TEMPLATE" value="<?echo htmlspecialcharsbx($_GET["template_id"])?>" id="SITE_TEMPLATE_cur"<?if($_REQUEST["SITE_TEMPLATE"] == $_GET["template_id"]) echo " checked"?> onclick="CheckSiteTemplate(this)"><label for="SITE_TEMPLATE_cur"><?echo GetMessage("template_copy_cur")?> / <?echo htmlspecialcharsbx($_GET["template_id"])?><?if($arSiteTemplates[$_GET["template_id"]] <> '') echo " (".$arSiteTemplates[$_GET["template_id"]].")"?></label><br>
  269. <?endif?>
  270. <?
  271. $bList = ($_REQUEST["SITE_TEMPLATE"] <> "" && $_REQUEST["SITE_TEMPLATE"] <> $_GET["template_id"] && $_REQUEST["SITE_TEMPLATE"] <> ".default")
  272. ?>
  273. <input type="radio" name="SITE_TEMPLATE" value="" id="SITE_TEMPLATE_sel"<?if($bList) echo " checked"?> onclick="CheckSiteTemplate(this)"><label for="SITE_TEMPLATE_sel"><?echo GetMessage("template_copy_sel")?></label>
  274. <select name="SITE_TEMPLATE"<?if(!$bList) echo " disabled"?>>
  275. <?
  276. foreach($arSiteTemplates as $templ_id=>$templ_name):
  277. if($templ_id == ".default" || $templ_id == $_GET["template_id"])
  278. continue;
  279. ?>
  280. <option value="<?= htmlspecialcharsbx($templ_id) ?>"<?if (($_REQUEST["SITE_TEMPLATE"] <> '' && $_REQUEST["SITE_TEMPLATE"] == $templ_id) || ($_REQUEST["SITE_TEMPLATE"] == '' && $templ_id == $template_site_template)) echo " selected";?>><?= htmlspecialcharsbx($templ_id." (".$templ_name.")") ?></option>
  281. <?endforeach;?>
  282. </select>
  283. </td>
  284. </tr>
  285. <?if(!$bParentComp):?>
  286. <tr>
  287. <td class="bx-popup-label bx-width50"><?= GetMessage("comp_templ_use") ?>:</td>
  288. <td>
  289. <input type="checkbox" name="USE_TEMPLATE" value="Y"<?if (!($_REQUEST["action"] == "save" && $_REQUEST["USE_TEMPLATE"] <> "Y")) echo " checked";?><?if($bList) echo " disabled"?>>
  290. </td>
  291. </tr>
  292. <?endif?>
  293. <?if($USER->CanDoOperation('edit_php')):?>
  294. <tr>
  295. <td class="bx-popup-label bx-width50"><?= GetMessage("comp_templ_edit") ?>:</td>
  296. <td>
  297. <input type="checkbox" name="EDIT_TEMPLATE" value="Y"<?if (!($_REQUEST["action"] == "save" && $_REQUEST["EDIT_TEMPLATE"] <> "Y")) echo " checked";?>>
  298. </td>
  299. </tr>
  300. <?endif?>
  301. </table>
  302. <?
  303. $obJSPopup->ShowStandardButtons();
  304. require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/epilog_admin_js.php");
  305. ?>