PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/freetrix/modules/fileman/classes/general/snippets.php

https://github.com/ivanbogomoloff/open_bx
PHP | 433 lines | 360 code | 64 blank | 9 comment | 93 complexity | abd57b1a78371515934400dcb1e1dc09 MD5 | raw file
  1. <?
  2. class CSnippets
  3. {
  4. public static function LoadList($Params)
  5. {
  6. $template = CFileMan::SecurePathVar($Params["template"]);
  7. if ($template == '')
  8. $template = '.default';
  9. $arSNIPPETS = false;
  10. $CACHE_SNIPPETS = Array();
  11. if ($Params['bClearCache'])
  12. CSnippets::ClearCache();
  13. if (!$arSNIPPETS || !is_array($arSNIPPETS))
  14. {
  15. $arSNIPPETS = Array();
  16. $arTemplateKeys = Array(); //Array contain keys of snippets for each template for correct writing .content.php
  17. CSnippets::HandleForTemplate('.default', $arSNIPPETS, $arTemplateKeys);
  18. if ($template != '.default')
  19. CSnippets::HandleForTemplate($template, $arSNIPPETS, $arTemplateKeys);
  20. $CACHE_SNIPPETS[$template] = $arSNIPPETS;
  21. }
  22. if ($Params['returnArray'])
  23. {
  24. return $arSNIPPETS;
  25. }
  26. else
  27. {
  28. ?><script>window.arSnippets = <?= CUtil::PhpToJSObject($arSNIPPETS)?>; </script><?
  29. }
  30. }
  31. function HandleForTemplate($template, &$arSNIPPETS, &$arTemplateKeys)
  32. {
  33. $arTemplateKeys[$template] = Array();
  34. CSnippets::ReadDir($arSNIPPETS, $arTemplateKeys[$template], "", $template);
  35. if (count($arSNIPPETS) > 0)
  36. CSnippets::UpdateContentInfo($arSNIPPETS, $arTemplateKeys[$template], $template);
  37. }
  38. function ReadDir(&$arSNIPPETS, &$arKeys, $path, $template, $level = 0, $parent = "")
  39. {
  40. $basePath = $_SERVER["DOCUMENT_ROOT"].FX_PERSONAL_ROOT."/templates/".$template."/snippets";
  41. if (!file_exists($basePath))
  42. return;
  43. $imagesPath = $basePath."/images";
  44. CSnippets::WriteHtaccess($imagesPath);
  45. $bpPath = $basePath.($path == "" ? "" : "/").$path;
  46. $handle = @opendir($bpPath);
  47. while(false !== ($file = @readdir($handle)))
  48. {
  49. if($file == "." || $file == ".." || $file == ".htaccess" || $file == ".content.php" || ($level == 0 && $file == "images"))
  50. continue;
  51. $fullPath = $bpPath."/".$file;
  52. if (is_dir($fullPath))
  53. {
  54. $new_path = "".$path.($path == "" ? "" : "/").$file;
  55. CSnippets::ReadDir($arSNIPPETS, $arKeys, $new_path, $template, $level + 1, $file);
  56. }
  57. elseif(is_file($fullPath))
  58. {
  59. $name = $file;
  60. $pos = strrpos($name, ".");
  61. $f_name = ($pos !== FALSE) ? substr($name, 0, $pos) : $name;
  62. $f_ext = ($pos !== FALSE) ? substr($name, $pos + 1) : '';
  63. // Rename file *.* => *.snp
  64. if ($f_ext != 'snp')
  65. {
  66. $name = $f_name.".snp";
  67. if (!file_exists($bpPath."/".$name))
  68. {
  69. rename($fullPath, $bpPath."/".$name);
  70. }
  71. else
  72. {
  73. for ($n = 1; $n < 256; $n++)
  74. {
  75. $test_f_name = $f_name."(".$n.")";
  76. $name = $test_f_name.".snp";
  77. if (!file_exists($bpPath."/".$name))
  78. {
  79. rename($fullPath, $bpPath."/".$name);
  80. break;
  81. }
  82. }
  83. }
  84. $f_ext = 'snp';
  85. }
  86. $imgPath = $imagesPath."/".$path;
  87. //Check thumbnail
  88. if(file_exists($imgPath."/".$f_name.".gif"))
  89. $thumb = $f_name.".gif";
  90. elseif(file_exists($imgPath."/".$f_name.".jpg"))
  91. $thumb = $f_name.".jpg";
  92. elseif(file_exists($imgPath."/".$f_name.".jpeg"))
  93. $thumb = $f_name.".jpeg";
  94. elseif(file_exists($imgPath."/".$f_name.".png"))
  95. $thumb = $f_name.".png";
  96. elseif(file_exists($imgPath."/".$f_name.".bmp"))
  97. $thumb = $f_name.".bmp";
  98. else
  99. $thumb = "";
  100. $key = $path.($path != '' ? '/' : '').$name;
  101. $arSNIPPETS[$key] = Array(
  102. 'name' => $name,
  103. 'path' => $path,
  104. 'title' => $name,
  105. 'thumb' => $thumb,
  106. 'code' => CSnippets::GetCode($bpPath."/".$name),
  107. 'description' => "",
  108. 'template' => $template,
  109. 'level' => $level,
  110. 'parent' => $parent
  111. );
  112. $arKeys[$key] = Array(
  113. 'name' => $name,
  114. 'path' => $path,
  115. 'title' => $name,
  116. 'description' => ""
  117. );
  118. }
  119. }
  120. }
  121. function UpdateContentInfo(&$ar, &$arKeys, $template)
  122. {
  123. $path = $_SERVER["DOCUMENT_ROOT"].FX_PERSONAL_ROOT."/templates/".$template."/snippets";
  124. if (file_exists($path."/.content.php"))
  125. {
  126. @include($path."/.content.php");
  127. $arK = array_keys($SNIPPETS);
  128. for ($i=0, $len = count($arK); $i<$len;$i++)
  129. {
  130. $name = $arK[$i];
  131. $pos = strrpos($name,".");
  132. $f_name = ($pos !== FALSE) ? substr($name,0,$pos) : $name;
  133. if ($ar[$f_name.".snp"])
  134. {
  135. $ar[$f_name.".snp"]['title'] = stripslashes($SNIPPETS[$name]['title']);
  136. $ar[$f_name.".snp"]['description'] = stripslashes($SNIPPETS[$name]['description']);
  137. }
  138. }
  139. }
  140. }
  141. function WriteHtaccess($path)
  142. {
  143. if(file_exists($path) && !file_exists($path."/.htaccess"))
  144. $GLOBALS['APPLICATION']->SaveFileContent($path."/.htaccess", "Allow from All");
  145. }
  146. function ClearCache()
  147. {
  148. return true;
  149. }
  150. function GetCode($path)
  151. {
  152. return file_exists($path) ? $GLOBALS['APPLICATION']->GetFileContent($path) : '';
  153. }
  154. function Edit($Params)
  155. {
  156. global $APPLICATION;
  157. $name = CFileMan::SecurePathVar($Params['name']);
  158. $title = $Params['title'];
  159. $description = $Params['description'];
  160. $path = CFileMan::SecurePathVar($Params['path']);
  161. $template = CFileMan::SecurePathVar($Params['template']);
  162. $site = $Params['site'];
  163. $code = $Params['code'];
  164. $contPath = $_SERVER["DOCUMENT_ROOT"].FX_PERSONAL_ROOT."/templates/".$template."/snippets";
  165. $thumb = $Params['thumb'] === false ? false : CFileMan::SecurePathVar($Params['thumb']);
  166. if (!file_exists($_SERVER["DOCUMENT_ROOT"].FX_PERSONAL_ROOT."/templates/".$template))
  167. {
  168. ?><script>alert('Error: Incorrect template Id: <?= CUtil::JSEscape($template)?>');</script><?
  169. return;
  170. }
  171. if ($Params['bNew'])
  172. {
  173. $location = CUtil::addslashes(CFileMan::SecurePathVar($Params["location"]));
  174. $newGroup = CUtil::addslashes(CFileMan::SecurePathVar($Params["newGroup"]));
  175. $path = trim(($location ? $location.'/' : '').($newGroup ? $newGroup.'/' : ''), ' /');
  176. if ($name == '')
  177. {
  178. $name = CSnippets::GetDefaultFileName($_SERVER["DOCUMENT_ROOT"].FX_PERSONAL_ROOT."/templates/".$template."/snippets/".$path);
  179. ?><script>window.__bx_res_sn_filename = "<?= CUtil::JSEscape($name);?>";</script><?
  180. }
  181. $name = $name.'.snp';
  182. }
  183. $key = $path.($path != '' ? '/' : '').$name;
  184. // 1. Save new snippet with new content
  185. if ($code)
  186. $APPLICATION->SaveFileContent($contPath.'/'.$key, $code);
  187. // 2. Rewrite title & description in .content.php
  188. if ($title || $description)
  189. {
  190. if (file_exists($contPath."/.content.php"))
  191. @include($contPath."/.content.php");
  192. else
  193. $SNIPPETS = array();
  194. if ($title)
  195. $SNIPPETS[$key]['title'] = $title;
  196. if ($description)
  197. $SNIPPETS[$key]['description'] = $description;
  198. $contentSrc = '<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();?>'.chr(10);
  199. $contentSrc .= '<?'.chr(10).'$SNIPPETS = Array();'.chr(10);
  200. foreach ($SNIPPETS as $k=>$_arSn)
  201. {
  202. if (CSnippets::CheckFile(array('site' => $Params["site"], 'template' => $Params['template'], 'path' => $k)))
  203. $contentSrc .= '$SNIPPETS[\''.CUtil::addslashes($k).'\'] = Array("title"=>\''.Cutil::addslashes($_arSn['title']).'\', "description"=>\''.Cutil::addslashes($_arSn['description']).'\');'.chr(10);
  204. }
  205. $contentSrc .= '?>';
  206. $APPLICATION->SaveFileContent($contPath."/.content.php", $contentSrc);
  207. }
  208. CSnippets::ClearCache();
  209. // 3. Handle thumbnail
  210. if ($thumb !== false)
  211. {
  212. if (substr($thumb,0,1) == '/')
  213. $thumb = substr($thumb,1);
  214. $pos = strrpos($name,".");
  215. if ($pos === FALSE)
  216. return true;
  217. //delete existent thumbnail
  218. $f_name = substr($name, 0, $pos);
  219. $img_path1 = FX_PERSONAL_ROOT.'/templates/'.$template.'/snippets/images/'.$path.($path == '' ? '' : '/').$f_name;
  220. $DOC_ROOT = CSite::GetSiteDocRoot($site);
  221. $arExt = array("gif", "jpg", "jpeg", "png", "bmp");
  222. for ($i = 0, $c = count($arExt); $i < $c; $i++)
  223. {
  224. $p_ = $img_path1.".".$arExt[$i];
  225. if(file_exists($DOC_ROOT.$p_))
  226. CFileman::DeleteFile(Array($site, $p_));
  227. }
  228. if (empty($thumb) || strrpos($thumb, '.') === FALSE)
  229. return true;
  230. // Copy Thumbnail
  231. $path_from_1 = $DOC_ROOT."/".$thumb;
  232. $path_from = '/'.$thumb;
  233. if (file_exists($path_from_1))
  234. {
  235. $pos = strrpos($thumb,".");
  236. $f_ext = ($pos !== FALSE) ? strtolower(substr($thumb, $pos + 1)) : '';
  237. if (in_array($f_ext, $arExt))
  238. {
  239. $path_to = $img_path1.'.'.$f_ext;
  240. $strWarning_tmp = CFileMan::CopyEx(Array($site, $path_from), Array($site, $path_to));
  241. }
  242. }
  243. }
  244. }
  245. function Delete($Params)
  246. {
  247. global $APPLICATION;
  248. $snPath = FX_PERSONAL_ROOT."/templates/".CFileMan::SecurePathVar($Params['template'])."/snippets";
  249. $contPath = $_SERVER["DOCUMENT_ROOT"].$snPath;
  250. $path = CFileMan::SecurePathVar($Params["path"]);
  251. $key = $Params["path"].($Params["path"] == '' ? '' : '/').CFileMan::SecurePathVar($Params["name"]);
  252. //Delete snippet file
  253. CFileman::DeleteFile(Array($Params["site"], $snPath.'/'.$key));
  254. //Delete thumbnail
  255. if ($Params["thumb"] != '')
  256. CFileman::DeleteFile(Array($Params["site"], $snPath.'/images/'.$path.($path == '' ? '' : '/').CFileMan::SecurePathVar($Params["thumb"])));
  257. if (file_exists($contPath."/.content.php"))
  258. {
  259. @include($contPath."/.content.php");
  260. $contentSrc = '<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();?>'.chr(10);
  261. $contentSrc .= '<?'.chr(10).'$SNIPPETS = Array();'.chr(10);
  262. foreach ($SNIPPETS as $k=>$_arSn)
  263. {
  264. if ($k != $key && CSnippets::CheckFile(array('site' => $Params["site"], 'template' => $Params['template'], 'path' => $k)))
  265. $contentSrc .= '$SNIPPETS[\''.CUtil::JSEscape($k).'\'] = Array("title"=>\''.CUtil::JSEscape($_arSn['title']).'\', "description"=>\''.CUtil::JSEscape($_arSn['description']).'\');'.chr(10);
  266. }
  267. $contentSrc .= '?>';
  268. $APPLICATION->SaveFileContent($contPath."/.content.php", $contentSrc);
  269. }
  270. CSnippets::ClearCache();
  271. ?>
  272. <script>
  273. window.operation_success = true;
  274. </script>
  275. <?
  276. }
  277. function CheckFile($Params)
  278. {
  279. $contPath = $_SERVER["DOCUMENT_ROOT"].FX_PERSONAL_ROOT."/templates/".$Params['template']."/snippets";
  280. return file_exists(CFileMan::SecurePathVar($contPath.'/'.$Params['path']));
  281. }
  282. public static function GetGroups($Params)
  283. {
  284. $template = CFileMan::SecurePathVar($Params['template']);
  285. $arSnGroups = Array();
  286. CSnippets::InspectDir($arSnGroups, "", $template);
  287. CSnippets::DisplayJSGroups($template, $arSnGroups);
  288. }
  289. public static function GetGroupList($Params)
  290. {
  291. $template = CFileMan::SecurePathVar($Params['template']);
  292. $arGroups = false;
  293. $CACHE_SNIPPETS = Array();
  294. if ($Params['bClearCache'])
  295. CSnippets::ClearCache();
  296. if (!$arGroups || !is_array($arGroups))
  297. {
  298. $arGroups = Array();
  299. CSnippets::InspectDir($arGroups, "", $template);
  300. $CACHE_SNIPPETS[$template] = $arGroups;
  301. }
  302. return $arGroups;
  303. }
  304. public static function SaveGroupList($Params)
  305. {
  306. }
  307. function InspectDir(&$arSnGroups, $path, $template, $level = 0, $parent = '')
  308. {
  309. $basePath = $_SERVER["DOCUMENT_ROOT"].FX_PERSONAL_ROOT."/templates/".$template."/snippets";
  310. if (!file_exists($basePath))
  311. return;
  312. $bpPath = $basePath.($path == "" ? "" : "/").$path;
  313. $handle = @opendir($bpPath);
  314. if (!$level)
  315. $level = 0;
  316. if (!$parent)
  317. $parent = "";
  318. while(false !== ($file = @readdir($handle)))
  319. {
  320. if($file == "." || $file == ".." || $file == ".htaccess" || $file == ".content.php" || ($level == 0 && $file == "images"))
  321. continue;
  322. $fullPath = $bpPath."/".$file;
  323. if (!is_dir($fullPath))
  324. continue;
  325. $arSnGroups[] = Array
  326. (
  327. 'path' => $path,
  328. 'name' => $file,
  329. 'level' => $level,
  330. 'default_name' => CSnippets::GetDefaultFileName($fullPath)
  331. );
  332. $new_path = "".$path.($path == "" ? "" : "/").$file;
  333. CSnippets::InspectDir($arSnGroups, $new_path, $template, $level + 1,$parent);
  334. }
  335. }
  336. function GetDefaultFileName($path)
  337. {
  338. for ($i=1; $i <= 9999; $i++)
  339. {
  340. $name = 'snippet'.str_pad($i, 4, "0", STR_PAD_LEFT);
  341. if (!file_exists($path.'/'.$name.'.snp'))
  342. break;
  343. }
  344. return $name;
  345. }
  346. function DisplayJSGroups($template, $ar = array())
  347. {
  348. $template = CUtil::JSEscape(htmlspecialcharsex($template));
  349. ?><script>
  350. window.arSnGroups['<?= $template?>'] = {};
  351. window.rootDefaultName['<?= $template?>'] = '<?= CSnippets::GetDefaultFileName($_SERVER["DOCUMENT_ROOT"].FX_PERSONAL_ROOT."/templates/".$template."/snippets")?>';
  352. <?
  353. for($i=0,$len = count($ar); $i < $len; $i++)
  354. {
  355. $key = CUtil::JSEscape($ar[$i]['path'].($ar[$i]['path'] != '' ? '/' : '').$ar[$i]['name']);
  356. ?>
  357. window.arSnGroups['<?=$template?>']['<?= $key?>'] =
  358. {
  359. name: '<?=CUtil::JSEscape($ar[$i]['name'])?>',
  360. path: '<?=CUtil::JSEscape($ar[$i]['path'])?>',
  361. level: '<?=CUtil::JSEscape($ar[$i]['level'])?>',
  362. default_name: '<?=CUtil::JSEscape($ar[$i]['default_name'])?>'
  363. };
  364. <?
  365. }
  366. ?></script><?
  367. }
  368. }
  369. ?>