PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/bitrix/modules/fileman/classes/general/light_editor.php

https://gitlab.com/neuser/bitrix-core
PHP | 359 lines | 307 code | 42 blank | 10 comment | 52 complexity | 8ecd54bf8b9311eaedba38b7ed5aaa23 MD5 | raw file
  1. <?
  2. IncludeModuleLangFile(__FILE__);
  3. class CLightHTMLEditor // LHE
  4. {
  5. function Init(&$arParams)
  6. {
  7. global $USER, $APPLICATION;
  8. $basePath = '/bitrix/js/fileman/light_editor/';
  9. $this->Id = (isset($arParams['id']) && $arParams['id'] <> '') ? $arParams['id'] : 'bxlhe'.mb_substr(uniqid(mt_rand(), true), 0, 4);
  10. $this->Id = preg_replace("/[^a-zA-Z0-9_:\.]/is", "", $this->Id);
  11. $this->cssPath = $basePath."light_editor.css";
  12. $APPLICATION->SetAdditionalCSS($this->cssPath);
  13. $this->arJSPath = array(
  14. $basePath.'le_dialogs.js',
  15. $basePath.'le_controls.js',
  16. $basePath.'le_toolbarbuttons.js',
  17. $basePath.'le_core.js'
  18. );
  19. $this->bBBCode = $arParams['BBCode'] === true;
  20. $this->bRecreate = $arParams['bRecreate'] === true;
  21. $arJS = Array();
  22. $arCSS = Array();
  23. foreach(GetModuleEvents("fileman", "OnBeforeLightEditorScriptsGet", true) as $arEvent)
  24. {
  25. $tmp = ExecuteModuleEventEx($arEvent, array($this->Id, $arParams));
  26. if (!is_array($tmp))
  27. continue;
  28. if (is_array($tmp['JS']))
  29. {
  30. for($i = 0, $c = count($tmp['JS']); $i < $c; $i++)
  31. {
  32. if(file_exists($_SERVER['DOCUMENT_ROOT'].$tmp['JS'][$i]))
  33. $this->arJSPath[] = $tmp['JS'][$i];
  34. }
  35. }
  36. }
  37. foreach($this->arJSPath as $path)
  38. {
  39. $APPLICATION->AddHeadScript($path);
  40. }
  41. //Messages
  42. $langPath = $_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/fileman/lang/'.LANGUAGE_ID.'/classes/general/light_editor_js.php';
  43. if(!file_exists($langPath))
  44. $langPath = $_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/fileman/lang/en/classes/general/light_editor_js.php';
  45. $this->mess = __IncludeLang($langPath, true, true);
  46. $this->messOld = array();
  47. if (!empty($this->mess))
  48. {
  49. $this->messOld = array('Image' => $this->mess['Image'], 'Video' => $this->mess['Video'],'ImageSizing' => $this->mess['ImageSizing']);
  50. $jsMsg = "<script bxrunfirst>LHE_MESS = window.LHE_MESS = ".CUtil::PhpToJSObject($this->messOld)."; (window.BX||top.BX).message(".CUtil::PhpToJSObject($this->mess, false).");</script>";
  51. $APPLICATION->AddLangJS($jsMsg);
  52. }
  53. $this->bAutorized = is_object($USER) && $USER->IsAuthorized();
  54. $this->bUseFileDialogs = $arParams['bUseFileDialogs'] !== false && $this->bAutorized;
  55. $this->bUseMedialib = $arParams['bUseMedialib'] !== false && COption::GetOptionString('fileman', "use_medialib", "Y") == "Y" && CMedialib::CanDoOperation('medialib_view_collection', 0);
  56. $this->bResizable = $arParams['bResizable'] === true;
  57. $this->bManualResize = $this->bResizable && $arParams['bManualResize'] !== false;
  58. $this->bAutoResize = $arParams['bAutoResize'] !== false;
  59. $this->bInitByJS = $arParams['bInitByJS'] === true;
  60. $this->bSaveOnBlur = $arParams['bSaveOnBlur'] !== false;
  61. $this->content = $arParams['content'];
  62. $this->inputName = isset($arParams['inputName']) ? $arParams['inputName'] : 'lha_content';
  63. $this->inputId = isset($arParams['inputId']) ? $arParams['inputId'] : 'lha_content_id';
  64. $this->videoSettings = is_array($arParams['videoSettings']) ? $arParams['videoSettings'] : array(
  65. 'maxWidth' => 640,
  66. 'maxHeight' => 480,
  67. 'WMode' => 'transparent',
  68. 'windowless' => true,
  69. 'bufferLength' => 20,
  70. 'skin' => '/bitrix/components/bitrix/player/mediaplayer/skins/bitrix.swf',
  71. 'logo' => ''
  72. );
  73. if (!is_array($arParams['arFonts']) || count($arParams['arFonts']) <= 0)
  74. $arParams['arFonts'] = array('Arial', 'Verdana', 'Times New Roman', 'Courier', 'Tahoma', 'Georgia', 'Optima', 'Impact', 'Geneva', 'Helvetica');
  75. if (!is_array($arParams['arFontSizes']) || count($arParams['arFontSizes']) <= 0)
  76. $arParams['arFontSizes'] = array('1' => 'xx-small', '2' => 'x-small', '3' => 'small', '4' => 'medium', '5' => 'large', '6' => 'x-large', '7' => 'xx-large');
  77. // Tables
  78. //$this->arJSPath[] = $this->GetActualPath($basePath.'le_table.js');
  79. $this->jsObjName = (isset($arParams['jsObjName']) && $arParams['jsObjName'] <> '') ? $arParams['jsObjName'] : 'LightHTMLEditor'.$this->Id;
  80. $this->jsObjName = preg_replace("/[^a-zA-Z0-9_:\.]/is", "", $this->jsObjName);
  81. if ($this->bResizable)
  82. {
  83. // Get height user settings
  84. $userOpt = CUserOptions::GetOption(
  85. 'fileman',
  86. 'LHESize_'.$this->Id,
  87. array('height' => $arParams['height'])
  88. );
  89. $arParams['height'] = intval($userOpt['height']) > 0 ? $userOpt['height'] : $arParams['height'];
  90. }
  91. $this->JSConfig = array(
  92. 'id' => $this->Id,
  93. 'content' => $this->content,
  94. 'bBBCode' => $this->bBBCode,
  95. 'bUseFileDialogs' => $this->bUseFileDialogs,
  96. 'bUseMedialib' => $this->bUseMedialib,
  97. 'arSmiles' => $arParams['arSmiles'],
  98. 'arFonts' => $arParams['arFonts'],
  99. 'arFontSizes' => $arParams['arFontSizes'],
  100. 'inputName' => $this->inputName,
  101. 'inputId' => $this->inputId,
  102. 'videoSettings' => $this->videoSettings,
  103. 'bSaveOnBlur' => $this->bSaveOnBlur,
  104. 'bResizable' => $this->bResizable,
  105. 'autoResizeSaveSize' => $arParams['autoResizeSaveSize'] !== false,
  106. 'bManualResize' => $this->bManualResize,
  107. 'bAutoResize' => $this->bAutoResize,
  108. 'bReplaceTabToNbsp' => true,
  109. 'bSetDefaultCodeView' => isset($arParams['bSetDefaultCodeView']) && $arParams['bSetDefaultCodeView'],
  110. 'bBBParseImageSize' => isset($arParams['bBBParseImageSize']) && $arParams['bBBParseImageSize'],
  111. 'smileCountInToolbar' => intval($arParams['smileCountInToolbar']),
  112. 'bQuoteFromSelection' => isset($arParams['bQuoteFromSelection']) && $arParams['bQuoteFromSelection'],
  113. 'bConvertContentFromBBCodes' => isset($arParams['bConvertContentFromBBCodes']) && $arParams['bConvertContentFromBBCodes'],
  114. 'oneGif' => '/bitrix/images/1.gif',
  115. 'imagePath' => '/bitrix/images/fileman/light_htmledit/'
  116. );
  117. // Set editor from visual mode to textarea for mobile devices
  118. if (!isset($this->JSConfig['bSetDefaultCodeView']) && CLightHTMLEditor::IsMobileDevice())
  119. $this->JSConfig['bSetDefaultCodeView'] = true;
  120. if (isset($arParams['width']) && intval($arParams['width']) > 0)
  121. $this->JSConfig['width'] = $arParams['width'];
  122. if (isset($arParams['height']) && intval($arParams['height']) > 0)
  123. $this->JSConfig['height'] = $arParams['height'];
  124. if (isset($arParams['toolbarConfig']))
  125. $this->JSConfig['toolbarConfig'] = $arParams['toolbarConfig'];
  126. if (isset($arParams['documentCSS']))
  127. $this->JSConfig['documentCSS'] = $arParams['documentCSS'];
  128. if (isset($arParams['fontFamily']))
  129. $this->JSConfig['fontFamily'] = $arParams['fontFamily'];
  130. if (isset($arParams['fontSize']))
  131. $this->JSConfig['fontSize'] = $arParams['fontSize'];
  132. if (isset($arParams['lineHeight']))
  133. $this->JSConfig['lineHeight'] = $arParams['lineHeight'];
  134. if (isset($arParams['bHandleOnPaste']))
  135. $this->JSConfig['bHandleOnPaste'] = $arParams['bHandleOnPaste'];
  136. if (isset($arParams['autoResizeOffset']))
  137. $this->JSConfig['autoResizeOffset'] = $arParams['autoResizeOffset'];
  138. if (isset($arParams['autoResizeMaxHeight']))
  139. $this->JSConfig['autoResizeMaxHeight'] = $arParams['autoResizeMaxHeight'];
  140. if (isset($arParams['controlButtonsHeight']))
  141. $this->JSConfig['controlButtonsHeight'] = $arParams['controlButtonsHeight'];
  142. if ($this->bBBCode)
  143. {
  144. $this->JSConfig['bParceBBImageSize'] = true;
  145. }
  146. if (isset($arParams['ctrlEnterHandler']))
  147. $this->JSConfig['ctrlEnterHandler'] = $arParams['ctrlEnterHandler'];
  148. }
  149. function GetActualPath($path)
  150. {
  151. return $path.'?'.@filemtime($_SERVER['DOCUMENT_ROOT'].$path);
  152. }
  153. function Show($arParams)
  154. {
  155. CUtil::InitJSCore(array('window', 'ajax'));
  156. $this->Init($arParams);
  157. $this->BuildSceleton();
  158. $this->InitScripts();
  159. if ($this->bUseFileDialogs)
  160. $this->InitFileDialogs();
  161. if ($this->bUseMedialib)
  162. $this->InitMedialibDialogs();
  163. }
  164. function BuildSceleton()
  165. {
  166. $width = isset($this->JSConfig['width']) && intval($this->JSConfig['width']) > 0 ? $this->JSConfig['width'] : "100%";
  167. $height = isset($this->JSConfig['height']) && intval($this->JSConfig['height']) > 0 ? $this->JSConfig['height'] : "100%";
  168. $widthUnit = mb_strpos($width, "%") === false ? "px" : "%";
  169. $heightUnit = mb_strpos($height, "%") === false ? "px" : "%";
  170. $width = intval($width);
  171. $height = intval($height);
  172. $editorCellHeight = ($heightUnit == "px" && $height > 50 ? "height:".($height - 27 - ($this->bResizable ? 3 : 0))."px" : "");
  173. ?>
  174. <?/* <img src="/bitrix/images/1.gif" width="300" height="1" id="bxlhe_ww_<?=$this->Id?>" />*/?>
  175. <div class="bxlhe-frame" id="bxlhe_frame_<?=$this->Id?>" style="width:<?=$width.$widthUnit?>; height:<?=$height.$heightUnit?>;"><table class="bxlhe-frame-table" cellspacing="0" style="height:<?=$height.$heightUnit?>; width: 100%;">
  176. <tr class="bxlhe-editor-toolbar-row"><td class="bxlhe-editor-buttons" style="height:27px;"><div class="lhe-stat-toolbar-cont lhe-stat-toolbar-cont-preload"></div></td></tr>
  177. <tr><td class="bxlhe-editor-cell" style="<?=$editorCellHeight?>"></td></tr>
  178. <?if ($this->bResizable):?>
  179. <tr><td class="lhe-resize-row" style="height: 3px;"><img id="bxlhe_resize_<?=$this->Id?>" src="/bitrix/images/1.gif"/></td></tr>
  180. <?endif;?>
  181. </table></div>
  182. <?
  183. }
  184. function InitScripts()
  185. {
  186. ob_start();
  187. foreach(GetModuleEvents("fileman", "OnIncludeLightEditorScript", true) as $arEvent)
  188. ExecuteModuleEventEx($arEvent, array($this->Id));
  189. $scripts = trim(ob_get_contents());
  190. ob_end_clean();
  191. $scripts = str_replace("<script>", "", $scripts);
  192. $scripts = str_replace("</script>", "", $scripts);
  193. $loadScript = "";
  194. foreach ($this->arJSPath as $path)
  195. {
  196. if ($loadScript != "")
  197. {
  198. $loadScript .= ", ";
  199. }
  200. $loadScript .= "\"".$this->GetActualPath($path)."\"";
  201. }
  202. ?>
  203. <script>
  204. function LoadLHE_<?=$this->Id?>()
  205. {
  206. function _lheScriptloaded()
  207. {
  208. if (!window.JCLightHTMLEditor)
  209. return setTimeout(_lheScriptloaded, 10);
  210. <?if (!empty($scripts)):?>
  211. // User's customization scripts here
  212. try{<?= $scripts?>}
  213. catch(e){alert('Errors in customization scripts! ' + e);}
  214. <?endif;?>
  215. if (
  216. <?= ($this->bRecreate ? 'true' : 'false')?> ||
  217. JCLightHTMLEditor.items['<?= $this->Id?>'] == undefined ||
  218. !document.body.contains(JCLightHTMLEditor.items['<?= $this->Id?>'].pFrame)
  219. )
  220. {
  221. top.<?=$this->jsObjName?> = window.<?=$this->jsObjName?> = new window.JCLightHTMLEditor(<?=CUtil::PhpToJSObject($this->JSConfig)?>);
  222. BX.onCustomEvent(window, 'LHE_ConstructorInited', [window.<?=$this->jsObjName?>]);
  223. }
  224. }
  225. if (!window.JCLightHTMLEditor)
  226. {
  227. BX.loadCSS("<?=$this->GetActualPath($this->cssPath)?>");
  228. <?if (!empty($this->mess)):?>
  229. LHE_MESS = window.LHE_MESS = "<?=CUtil::PhpToJSObject($this->messOld)?>"; (window.BX||top.BX).message(<?=CUtil::PhpToJSObject($this->mess, false)?>);
  230. <?endif?>
  231. BX.loadScript([<?=$loadScript?>], _lheScriptloaded);
  232. }
  233. else
  234. {
  235. _lheScriptloaded();
  236. }
  237. }
  238. <?if(!$this->bInitByJS):?>
  239. BX.ready(function(){LoadLHE_<?=$this->Id?>();});
  240. <?endif;?>
  241. </script><?
  242. }
  243. function InitFileDialogs()
  244. {
  245. // Link
  246. CAdminFileDialog::ShowScript(Array(
  247. "event" => "LHED_Link_FDOpen",
  248. "arResultDest" => Array("ELEMENT_ID" => "lhed_link_href"),
  249. "arPath" => Array("SITE" => SITE_ID),
  250. "select" => 'F',
  251. "operation" => 'O',
  252. "showUploadTab" => true,
  253. "showAddToMenuTab" => false,
  254. "fileFilter" => 'php, html',
  255. "allowAllFiles" => true,
  256. "SaveConfig" => true
  257. ));
  258. // Image
  259. CAdminFileDialog::ShowScript(Array
  260. (
  261. "event" => "LHED_Img_FDOpen",
  262. "arResultDest" => Array("FUNCTION_NAME" => "LHED_Img_SetUrl"),
  263. "arPath" => Array("SITE" => SITE_ID),
  264. "select" => 'F',
  265. "operation" => 'O',
  266. "showUploadTab" => true,
  267. "showAddToMenuTab" => false,
  268. "fileFilter" => 'image',
  269. "allowAllFiles" => true,
  270. "SaveConfig" => true
  271. ));
  272. // video path
  273. CAdminFileDialog::ShowScript(Array
  274. (
  275. "event" => "LHED_VideoPath_FDOpen",
  276. "arResultDest" => Array("FUNCTION_NAME" => "LHED_Video_SetPath"),
  277. "arPath" => Array("SITE" => SITE_ID),
  278. "select" => 'F',
  279. "operation" => 'O',
  280. "showUploadTab" => true,
  281. "showAddToMenuTab" => false,
  282. "fileFilter" => 'wmv,wma,flv,vp6,mp3,mp4,aac,jpg,jpeg,gif,png',
  283. "allowAllFiles" => true,
  284. "SaveConfig" => true
  285. ));
  286. // video preview
  287. CAdminFileDialog::ShowScript(Array
  288. (
  289. "event" => "LHED_VideoPreview_FDOpen",
  290. "arResultDest" => Array("ELEMENT_ID" => "lhed_video_prev_path"),
  291. "arPath" => Array("SITE" => SITE_ID),
  292. "select" => 'F',
  293. "operation" => 'O',
  294. "showUploadTab" => true,
  295. "showAddToMenuTab" => false,
  296. "fileFilter" => 'image',
  297. "allowAllFiles" => true,
  298. "SaveConfig" => true
  299. ));
  300. }
  301. function InitMedialibDialogs()
  302. {
  303. CMedialib::ShowDialogScript(array(
  304. "event" => "LHED_Img_MLOpen",
  305. "arResultDest" => Array("FUNCTION_NAME" => "LHED_Img_SetUrl")
  306. ));
  307. CMedialib::ShowDialogScript(array(
  308. "event" => "LHED_Video_MLOpen",
  309. "arResultDest" => Array("FUNCTION_NAME" => "LHED_Video_SetPath")
  310. ));
  311. }
  312. public static function IsMobileDevice()
  313. {
  314. return preg_match('/ipad|iphone|android|mobile|touch/i',$_SERVER['HTTP_USER_AGENT']);
  315. }
  316. }
  317. ?>