ckeditor.lib.inc 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  1. <?php
  2. /**
  3. * CKEditor - The text editor for the Internet - http://ckeditor.com
  4. * Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.
  5. *
  6. * == BEGIN LICENSE ==
  7. *
  8. * Licensed under the terms of any of the following licenses of your
  9. * choice:
  10. *
  11. * - GNU General Public License Version 2 or later (the "GPL")
  12. * http://www.gnu.org/licenses/gpl.html
  13. *
  14. * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
  15. * http://www.gnu.org/licenses/lgpl.html
  16. *
  17. * - Mozilla Public License Version 1.1 or later (the "MPL")
  18. * http://www.mozilla.org/MPL/MPL-1.1.html
  19. *
  20. * == END LICENSE ==
  21. *
  22. * @file
  23. * CKEditor Module for Drupal 7.x
  24. *
  25. * This module allows Drupal to replace textarea fields with CKEditor.
  26. *
  27. * CKEditor is an online rich text editor that can be embedded inside web pages.
  28. * It is a WYSIWYG (What You See Is What You Get) editor which means that the
  29. * text edited in it looks as similar as possible to the results end users will
  30. * see after the document gets published. It brings to the Web popular editing
  31. * features found in desktop word processors such as Microsoft Word and
  32. * OpenOffice.org Writer. CKEditor is truly lightweight and does not require any
  33. * kind of installation on the client computer.
  34. */
  35. /**
  36. * Guess the absolute server path to the document root
  37. * Usually it should return $_SERVER['DOCUMENT_ROOT']
  38. *
  39. * @todo Improve me!!
  40. * Returns absolute path or false on failure
  41. *
  42. * @return string|boolean
  43. */
  44. function ckeditor_get_document_root_full_path() {
  45. $found = 0;
  46. $index_dir = realpath(dirname($_SERVER['SCRIPT_FILENAME'])); // {/dir1/dir2/home/drupal}/index.php
  47. if (getcwd() == $index_dir) {
  48. $found++;
  49. }
  50. $drupal_dir = base_path();
  51. $index_dir = str_replace('\\', "/", $index_dir);
  52. $drupal_dir = str_replace('\\', "/", $drupal_dir);
  53. $document_root_dir = $index_dir . '/' . str_repeat('../', substr_count($drupal_dir, '/') - 1);
  54. $document_root_dir = realpath($document_root_dir);
  55. $document_root_dir = rtrim($document_root_dir, '/\\');
  56. if ($document_root_dir == $_SERVER['DOCUMENT_ROOT']) {
  57. $found++;
  58. }
  59. $document_root_dir = str_replace('\\', '/', $document_root_dir);
  60. if ($document_root_dir != '') {
  61. $found++;
  62. }
  63. if (file_exists($document_root_dir)) {
  64. $found++;
  65. }
  66. if (file_exists($document_root_dir . base_path() . 'includes/bootstrap.inc')) {
  67. $found++;
  68. }
  69. if ($found >= 3) {
  70. return $document_root_dir;
  71. }
  72. else {
  73. return FALSE;
  74. }
  75. }
  76. /**
  77. * Emulates the asp Server.mapPath function.
  78. * Given an url path return the physical directory that it corresponds to.
  79. *
  80. * Returns absolute path or false on failure
  81. *
  82. * @param string $path
  83. * @return string|boolean
  84. */
  85. function ckeditor_resolve_url($path) {
  86. if (function_exists('apache_lookup_uri')) {
  87. $info = @apache_lookup_uri($path);
  88. if (!$info) {
  89. return FALSE;
  90. }
  91. return $info->filename . $info->path_info;
  92. }
  93. $document_root = ckeditor_get_document_root_full_path();
  94. if ($document_root !== FALSE) {
  95. return $document_root . $path;
  96. }
  97. return FALSE;
  98. }
  99. /**
  100. * List of configured CKEditor toolbars
  101. *
  102. * @return array
  103. */
  104. function ckeditor_load_toolbar_options() {
  105. $arr = array('Basic' => 'Basic', 'Full' => 'Full');
  106. $module_drupal_path = drupal_get_path('module', 'ckeditor');
  107. $editor_local_path = ckeditor_path('local');
  108. $ckconfig_js = $editor_local_path . '/config.js';
  109. $ckeditor_config_js = $module_drupal_path . '/ckeditor.config.js';
  110. if (file_exists($ckconfig_js) && is_readable($ckconfig_js)) {
  111. $fp = @fopen($ckconfig_js, "r");
  112. if ($fp) {
  113. while (!feof($fp)) {
  114. $line = fgets($fp, 1024);
  115. $matches = array();
  116. if (preg_match('/config.toolbar_([a-z0-9_]+)/i', $line, $matches)) {
  117. $arr[$matches[1]] = drupal_ucfirst($matches[1]);
  118. }
  119. }
  120. fclose($fp);
  121. }
  122. }
  123. if (file_exists($ckeditor_config_js) && is_readable($ckeditor_config_js)) {
  124. $fp = @fopen($ckeditor_config_js, "r");
  125. if ($fp) {
  126. while (!feof($fp)) {
  127. $line = fgets($fp, 1024);
  128. $matches = array();
  129. if (preg_match('/config.toolbar_([a-z0-9_]+)/i', $line, $matches)) {
  130. $arr[$matches[1]] = drupal_ucfirst($matches[1]);
  131. }
  132. }
  133. fclose($fp);
  134. }
  135. }
  136. //oops, we have no information about toolbars, let's use hardcoded array
  137. if (empty($arr)) {
  138. $arr = array(
  139. 'Basic' => 'Basic',
  140. 'Default' => 'Default',
  141. );
  142. }
  143. asort($arr);
  144. return $arr;
  145. }
  146. /**
  147. * List of installed CKEditor skins
  148. *
  149. * @return array
  150. */
  151. function ckeditor_load_skin_options() {
  152. $arr = array();
  153. $editor_local_path = ckeditor_path('local');
  154. $skin_dir = $editor_local_path . '/skins';
  155. if (is_dir($skin_dir)) {
  156. $dh = @opendir($skin_dir);
  157. if (FALSE !== $dh) {
  158. while (($file = readdir($dh)) !== FALSE) {
  159. if (in_array($file, array(".", "..", "CVS", ".svn"))) {
  160. continue;
  161. }
  162. if (is_dir($skin_dir . DIRECTORY_SEPARATOR . $file)) {
  163. $arr[$file] = drupal_ucfirst($file);
  164. }
  165. }
  166. closedir($dh);
  167. }
  168. }
  169. //oops, we have no information about skins, let's use only default
  170. if (empty($arr)) {
  171. $arr = array(
  172. 'kama' => 'Kama',
  173. );
  174. }
  175. asort($arr);
  176. return $arr;
  177. }
  178. /**
  179. * Return default skin for CKEditor
  180. *
  181. * @return string
  182. */
  183. function ckeditor_default_skin() {
  184. $skin_options = ckeditor_load_skin_options();
  185. if (array_key_exists('moono', $skin_options)) {
  186. return 'moono';
  187. }
  188. if (array_key_exists('kama', $skin_options)) {
  189. return 'kama';
  190. }
  191. //if any default theme not exists select first from the list
  192. return key(reset($skin_options));
  193. }
  194. /**
  195. * List of installed CKEditor languages
  196. *
  197. * @return array
  198. */
  199. function ckeditor_load_lang_options() {
  200. $arr = array();
  201. $editor_local_path = ckeditor_path('local');
  202. $lang_file = $editor_local_path . '/lang/_languages.js';
  203. if (file_exists($lang_file)) {
  204. $f = fopen($lang_file, 'r');
  205. $file = fread($f, filesize($lang_file));
  206. $tmp = explode('{', $file);
  207. if (isset($tmp[2])) {
  208. $tmp = explode('}', $tmp[2]);
  209. }
  210. $langs = explode(',', $tmp[0]);
  211. foreach ($langs AS $key => $lang) {
  212. preg_match("/'?(\w+-?\w+)'?:'([\w\s\(\)]+)'/i", $lang, $matches);
  213. if (isset($matches[1]) && isset($matches[2]))
  214. $arr[$matches[1]] = $matches[2];
  215. }
  216. }
  217. //oops, we have no information about languages, let's use those available in CKEditor 2.4.3
  218. if (empty($arr)) {
  219. $arr = array(
  220. 'af' => 'Afrikaans',
  221. 'ar' => 'Arabic',
  222. 'bg' => 'Bulgarian',
  223. 'bn' => 'Bengali/Bangla',
  224. 'bs' => 'Bosnian',
  225. 'ca' => 'Catalan',
  226. 'cs' => 'Czech',
  227. 'da' => 'Danish',
  228. 'de' => 'German',
  229. 'el' => 'Greek',
  230. 'en' => 'English',
  231. 'en-au' => 'English (Australia)',
  232. 'en-ca' => 'English (Canadian)',
  233. 'en-uk' => 'English (United Kingdom)',
  234. 'eo' => 'Esperanto',
  235. 'es' => 'Spanish',
  236. 'et' => 'Estonian',
  237. 'eu' => 'Basque',
  238. 'fa' => 'Persian',
  239. 'fi' => 'Finnish',
  240. 'fo' => 'Faroese',
  241. 'fr' => 'French',
  242. 'gl' => 'Galician',
  243. 'he' => 'Hebrew',
  244. 'hi' => 'Hindi',
  245. 'hr' => 'Croatian',
  246. 'hu' => 'Hungarian',
  247. 'it' => 'Italian',
  248. 'ja' => 'Japanese',
  249. 'km' => 'Khmer',
  250. 'ko' => 'Korean',
  251. 'lt' => 'Lithuanian',
  252. 'lv' => 'Latvian',
  253. 'mn' => 'Mongolian',
  254. 'ms' => 'Malay',
  255. 'nb' => 'Norwegian Bokmal',
  256. 'nl' => 'Dutch',
  257. 'no' => 'Norwegian',
  258. 'pl' => 'Polish',
  259. 'pt' => 'Portuguese (Portugal)',
  260. 'pt-br' => 'Portuguese (Brazil)',
  261. 'ro' => 'Romanian',
  262. 'ru' => 'Russian',
  263. 'sk' => 'Slovak',
  264. 'sl' => 'Slovenian',
  265. 'sr' => 'Serbian (Cyrillic)',
  266. 'sr-latn' => 'Serbian (Latin)',
  267. 'sv' => 'Swedish',
  268. 'th' => 'Thai',
  269. 'tr' => 'Turkish',
  270. 'uk' => 'Ukrainian',
  271. 'vi' => 'Vietnamese',
  272. 'zh' => 'Chinese Traditional',
  273. 'zh-cn' => 'Chinese Simplified',
  274. );
  275. }
  276. asort($arr);
  277. return $arr;
  278. }
  279. /**
  280. * Get the language locale code supported by Scayt for the specified language
  281. */
  282. function ckeditor_scayt_langcode($lang) {
  283. $scayt_langs = array(
  284. 'en' => 'en_US',
  285. 'es' => 'es_ES',
  286. 'fr' => 'fr_FR',
  287. 'de' => 'de_DE',
  288. 'it' => 'it_IT',
  289. 'el' => 'el_EL',
  290. 'pt' => 'pt_PT',
  291. 'da' => 'da_DA',
  292. 'sv' => 'sv_SE',
  293. 'nl' => 'nl_NL',
  294. 'nb' => 'no_NO',
  295. 'fi' => 'fi_FI',
  296. );
  297. if (array_key_exists($lang, $scayt_langs)) {
  298. return $scayt_langs[$lang];
  299. }
  300. $default = language_default();
  301. $default = $default->language;
  302. if (array_key_exists($default, $scayt_langs)) {
  303. return $scayt_langs[$default];
  304. }
  305. return 'en_US';
  306. }
  307. /**
  308. * List of CKEditor plugins;
  309. *
  310. * @return array
  311. */
  312. function ckeditor_load_plugins($render = FALSE) {
  313. $arr = array();
  314. $base_path = '%base_path%';
  315. $editor_path = '%editor_path%';
  316. $ckeditor_path = '%module_path%';
  317. $plugin_dir = '%plugin_dir%';
  318. $plugin_dir_additional = '%plugin_dir_extra%';
  319. $pattern = '#\.addButton\([\s]*[\'"](.*?)[\'"][\s]*\,[\s]*\{[\s]*(.*?)[\s]*\}#s';
  320. /*
  321. * External plugins
  322. */
  323. if (module_exists('ckeditor_swf') && file_exists(drupal_get_path('module', 'ckeditor_swf') . '/plugins/swf/plugin.js')) {
  324. $arr['ckeditor_swf'] = array(
  325. 'name' => 'swf',
  326. 'desc' => t('Support for the CKEditor SWF module'),
  327. 'path' => $base_path . drupal_get_path('module', 'ckeditor_swf') . '/plugins/swf/',
  328. 'buttons' => FALSE,
  329. 'default' => 't'
  330. );
  331. }
  332. if (module_exists('ckeditor_link') && file_exists(drupal_get_path('module', 'ckeditor_link') . '/plugins/link/plugin.js')) {
  333. $arr['ckeditor_link'] = array(
  334. 'name' => 'drupal_path',
  335. 'desc' => t('Support for the CKEditor Link module'),
  336. 'path' => $base_path . drupal_get_path('module', 'ckeditor_link') . '/plugins/link/',
  337. 'buttons' => FALSE,
  338. 'default' => 't'
  339. );
  340. }
  341. if (module_exists('linkit') && file_exists(drupal_get_path('module', 'linkit') . '/editors/ckeditor/plugin.js')) {
  342. $arr['linkit'] = array(
  343. 'name' => 'Linkit',
  344. 'desc' => t('Support for the Linkit module <em>(buttons: Linkit)</em>'),
  345. 'path' => $base_path . drupal_get_path('module', 'linkit') . '/editors/ckeditor/',
  346. 'buttons' => array(
  347. 'Linkit' => array(
  348. 'title' => 'Linkit',
  349. 'icon' => $base_path . drupal_get_path('module', 'linkit') . '/editors/ckeditor/linkit.png'
  350. )
  351. ),
  352. 'default' => 't'
  353. );
  354. }
  355. /*
  356. * CKEditor build-in plugins
  357. */
  358. $_editor_path = ckeditor_path('local') . '/';
  359. if (file_exists($_editor_path . 'plugins/tableresize/plugin.js')) {
  360. $arr['tableresize'] = array(
  361. 'name' => 'tableresize',
  362. 'desc' => t('Table Resize plugin'),
  363. 'path' => $editor_path . 'plugins/tableresize/',
  364. 'buttons' => FALSE,
  365. 'default' => 't'
  366. );
  367. }
  368. if (file_exists($_editor_path . 'plugins/autogrow/plugin.js')) {
  369. $arr['autogrow'] = array(
  370. 'name' => 'autogrow',
  371. 'desc' => t('Auto Grow plugin'),
  372. 'path' => $editor_path . 'plugins/autogrow/',
  373. 'buttons' => FALSE,
  374. 'default' => 'f'
  375. );
  376. }
  377. if (file_exists($_editor_path . 'plugins/stylesheetparser/plugin.js')) {
  378. $arr['stylesheetparser'] = array(
  379. 'name' => 'stylesheetparser',
  380. 'desc' => t('Stylesheet Parser plugin'),
  381. 'path' => $editor_path . 'plugins/stylesheetparser/',
  382. 'buttons' => FALSE,
  383. 'default' => 'f'
  384. );
  385. }
  386. /*
  387. * CKEditor module plugins
  388. */
  389. $_plugin_dir = ckeditor_module_path('local') . '/plugins/';
  390. if ($handle = opendir($_plugin_dir)) {
  391. while (false !== ($file = readdir($handle))) {
  392. if (is_dir($_plugin_dir . $file) && file_exists($_plugin_dir . $file . '/plugin.js')) {
  393. $source = file_get_contents($_plugin_dir . $file . '/plugin.js');
  394. $buttons = array();
  395. if (preg_match_all($pattern, $source, $matches)) {
  396. foreach ($matches[1] as $i => $button_name) {
  397. if (preg_match('#(icon)[\s]*\:[\s]*([^\,\n]*)#', $matches[2][$i], $matches2)) {
  398. $buttons[$button_name] = array();
  399. $buttons[$button_name]['label'] = $button_name;
  400. $matches2[2] = str_replace(array('this.path', '+', '\'', '"'), array('', '', '', ''), $matches2[2]);
  401. $buttons[$button_name]['icon'] = trim($matches2[2]);
  402. }
  403. }
  404. }
  405. if (preg_match('#@file ([^\n\r]*)#', $source, $matches)) {
  406. $arr[$file] = array(
  407. 'name' => $file,
  408. 'desc' => t($matches[1]),
  409. 'path' => $plugin_dir . $file . '/',
  410. 'buttons' => (count($buttons) > 0) ? $buttons : FALSE,
  411. 'default' => 'f'
  412. );
  413. }
  414. else {
  415. $arr[$file] = array(
  416. 'name' => $file,
  417. 'desc' => t('Plugin file: ' . $file),
  418. 'path' => $plugin_dir . $file . '/',
  419. 'buttons' => (count($buttons) > 0) ? $buttons : FALSE,
  420. 'default' => 'f'
  421. );
  422. }
  423. }
  424. }
  425. closedir($handle);
  426. }
  427. /*
  428. * CKEditor module plugins - additional directory
  429. */
  430. $_plugin_dir_additional = ckeditor_plugins_path('local') . '/';
  431. if ($_plugin_dir != $_plugin_dir_additional && is_dir($_plugin_dir_additional) && $handle = opendir($_plugin_dir_additional)) {
  432. while (false !== ($file = readdir($handle))) {
  433. if (is_dir($_plugin_dir_additional . $file) && file_exists($_plugin_dir_additional . $file . '/plugin.js')) {
  434. $source = file_get_contents($_plugin_dir_additional . $file . '/plugin.js');
  435. $buttons = array();
  436. if (preg_match_all($pattern, $source, $matches)) {
  437. foreach ($matches[1] as $i => $button_name) {
  438. if (preg_match('#(icon)[\s]*\:[\s]*([^\,\n]*)#', $matches[2][$i], $matches2)) {
  439. $buttons[$button_name] = array();
  440. $buttons[$button_name]['label'] = $button_name;
  441. $matches2[2] = str_replace(array('this.path', '+', '\'', '"'), array('', '', '', ''), $matches2[2]);
  442. $buttons[$button_name]['icon'] = trim($matches2[2]);
  443. }
  444. }
  445. }
  446. if (preg_match('#@file ([^\n\r]*)#', $source, $matches)) {
  447. $arr[$file] = array(
  448. 'name' => $file,
  449. 'desc' => t($matches[1]),
  450. 'path' => $plugin_dir_additional . $file . '/',
  451. 'buttons' => (count($buttons) > 0) ? $buttons : FALSE,
  452. 'default' => 'f'
  453. );
  454. }
  455. else {
  456. $arr[$file] = array(
  457. 'name' => $file,
  458. 'desc' => t('Plugin file: ' . $file),
  459. 'path' => $plugin_dir_additional . $file . '/',
  460. 'buttons' => (count($buttons) > 0) ? $buttons : FALSE,
  461. 'default' => 'f'
  462. );
  463. }
  464. }
  465. }
  466. closedir($handle);
  467. }
  468. /*
  469. * CKEditor plugins registered by hook
  470. */
  471. $plugins = module_invoke_all('ckeditor_plugin');
  472. foreach ($plugins as $i => $plugin) {
  473. if (file_exists($plugin['path'] . 'plugin.js')) {
  474. $source = file_get_contents($plugin['path'] . 'plugin.js');
  475. $plugins[$i]['path'] = $base_path . $plugin['path'];
  476. if (!isset($plugin['buttons']) || count($plugin['buttons']) == 0) {
  477. $buttons = array();
  478. if (preg_match_all($pattern, $source, $matches)) {
  479. foreach ($matches[1] as $j => $button_name) {
  480. if (preg_match('#(icon)[\s]*\:[\s]*([^\,\n]*)#', $matches[2][$j], $matches2)) {
  481. $buttons[$button_name] = array();
  482. $buttons[$button_name]['label'] = $button_name;
  483. $matches2[2] = str_replace(array('this.path', '+', '\'', '"'), array('', '', '', ''), $matches2[2]);
  484. $buttons[$button_name]['icon'] = trim($matches2[2]);
  485. }
  486. }
  487. }
  488. $plugins[$i]['buttons'] = (count($buttons) > 0) ? $buttons : FALSE;
  489. }
  490. }
  491. else {
  492. unset($plugins[$i]);
  493. }
  494. }
  495. $arr = array_merge($arr, $plugins);
  496. if (isset($arr['media']) && module_exists('media') == FALSE) {
  497. unset($arr['media']);
  498. }
  499. if (isset($arr['imce']) && module_exists('imce') == FALSE) {
  500. unset($arr['imce']);
  501. }
  502. //remove page break button if there is no module to do this
  503. if (isset($arr['drupalbreaks']['buttons']['DrupalPageBreak']) && !module_exists('paging') && !module_exists('pagebreak')) {
  504. unset($arr['drupalbreaks']['buttons']['DrupalPageBreak']);
  505. }
  506. if (isset($arr['drupalbreaks'])) {
  507. $arr['drupalbreaks']['default'] = 't';
  508. }
  509. ksort($arr);
  510. if ($render === TRUE) {
  511. $arr = ckeditor_plugins_render($arr);
  512. }
  513. return $arr;
  514. }
  515. /**
  516. * Render CKEditor plugins path
  517. */
  518. function ckeditor_plugins_render($plugins) {
  519. $render = array();
  520. $render["%base_path%"] = ckeditor_base_path('relative') . '/';
  521. $render["%editor_path%"] = ckeditor_path('relative') . '/';
  522. $render["%module_path%"] = ckeditor_module_path('relative') . '/';
  523. $render["%plugin_dir%"] = $render["%module_path%"] . 'plugins/';
  524. $render["%plugin_dir_extra%"] = ckeditor_plugins_path('relative') . '/';
  525. foreach ((array) $plugins as $i => $plugin) {
  526. $plugins[$i]['path'] = str_replace(array_keys($render), array_values($render), $plugin['path']);
  527. }
  528. return $plugins;
  529. }
  530. /**
  531. * Get default ckeditor settings
  532. *
  533. * @return array
  534. */
  535. function ckeditor_user_get_setting_default() {
  536. $default = array(
  537. 'default' => 't',
  538. 'show_toggle' => 't',
  539. 'width' => '100%',
  540. 'lang' => 'en',
  541. 'auto_lang' => 't',
  542. );
  543. return $default;
  544. }
  545. /**
  546. * Return CKEditor settings
  547. *
  548. * @param object $user
  549. * @param object $profile
  550. * @param string $setting
  551. * @return array
  552. */
  553. function ckeditor_user_get_setting($user, $profile, $setting) {
  554. $default = ckeditor_user_get_setting_default();
  555. if (user_access('customize ckeditor')) {
  556. $status = isset($user->data['ckeditor_' . $setting]) ? $user->data['ckeditor_' . $setting] : (isset($profile->settings[$setting]) ? $profile->settings[$setting] : $default[$setting]);
  557. }
  558. else {
  559. $status = isset($profile->settings[$setting]) ? $profile->settings[$setting] : $default[$setting];
  560. }
  561. return $status;
  562. }
  563. /**
  564. * Return CKEditor profile by input format
  565. *
  566. * @param string $input_format
  567. * @return object|boolean
  568. */
  569. function ckeditor_get_profile($input_format) {
  570. $select = db_select('ckeditor_settings', 's');
  571. $select->join('ckeditor_input_format', 'f', 'f.name = s.name');
  572. $result = $select->fields('s', array("name"))->condition('f.format', $input_format)->condition('f.name', 'CKEditor Global Profile', '<>')->range(0, 1)->execute()->fetchAssoc();
  573. if ($result && $profile = ckeditor_profile_load($result['name'])) {
  574. return $profile;
  575. }
  576. return FALSE;
  577. }
  578. /**
  579. * Return CKEditor profile list
  580. */
  581. function ckeditor_profile_input_formats() {
  582. $select = db_select('ckeditor_settings', 's');
  583. $select->join('ckeditor_input_format', 'f', 'f.name = s.name');
  584. $result = $select->fields('s', array("name"))->fields('f', array("format"))->execute();
  585. $list = array();
  586. while ($row = $result->fetchAssoc()) {
  587. if (!isset($row['name'])) {
  588. $list[$row['name']] = array();
  589. }
  590. $list[$row['name']][] = $row['format'];
  591. }
  592. return $list;
  593. }
  594. /**
  595. * Search and return CKEditor plugin path
  596. *
  597. * @return string
  598. */
  599. function _ckeditor_script_path() {
  600. $jspath = FALSE;
  601. $module_path = drupal_get_path('module', 'ckeditor');
  602. if (file_exists($module_path . '/ckeditor/ckeditor.js')) {
  603. $jspath = '%m/ckeditor';
  604. }
  605. elseif (file_exists($module_path . '/ckeditor/ckeditor/ckeditor.js')) {
  606. $jspath = '%m/ckeditor/ckeditor';
  607. }
  608. elseif (file_exists('sites/all/libraries/ckeditor/ckeditor.js')) {
  609. $jspath = '%l/ckeditor';
  610. }
  611. return $jspath;
  612. }
  613. /**
  614. * Determines whether the CKEditor sources are present
  615. *
  616. * It checks if ckeditor.js is present.
  617. *
  618. * This function is used by ckeditor_requirements()
  619. *
  620. * @return boolean True if CKEditor is installed
  621. */
  622. function _ckeditor_requirements_isinstalled() {
  623. $editor_path = ckeditor_path('local');
  624. $jspath = $editor_path . '/ckeditor.js';
  625. $jsp = file_exists($jspath);
  626. if (!$jsp && ($editor_path = _ckeditor_script_path())) {
  627. $result = db_select('ckeditor_settings', 's')->fields('s')->condition('name', 'CKEditor Global Profile')->execute()->fetchAssoc();
  628. if ($result) {
  629. $result['settings'] = unserialize($result['settings']);
  630. $result['settings']['ckeditor_path'] = $editor_path;
  631. $result['settings'] = serialize($result['settings']);
  632. db_update('ckeditor_settings')->fields(array("settings" => $result['settings']))->condition('name', 'CKEditor Global Profile')->execute();
  633. $jsp = TRUE;
  634. ckeditor_path('local', TRUE);
  635. }
  636. }
  637. return $jsp;
  638. }
  639. /**
  640. * Compile settings of all profiles at returns is as array
  641. *
  642. * @param string $input_format
  643. * @param boolean $clear
  644. * @return array
  645. */
  646. function ckeditor_profiles_compile($input_format = FALSE, $clear = FALSE) {
  647. static $compiled = FALSE;
  648. static $_ckeditor_compiled = array();
  649. if ($clear !== FALSE && $compiled !== FALSE) {
  650. $compiled = FALSE;
  651. }
  652. if ($compiled === TRUE) {
  653. return ( $input_format === FALSE ) ? $_ckeditor_compiled : ( isset($_ckeditor_compiled[$input_format]) ? $_ckeditor_compiled[$input_format] : array() );
  654. }
  655. $global_profile = ckeditor_profile_load('CKEditor Global Profile');
  656. $profiles_list = ckeditor_profile_input_formats();
  657. foreach ($profiles_list AS $_profile => $_inputs) {
  658. $profile = ckeditor_profile_load($_profile);
  659. $setting = ckeditor_profile_settings_compile($global_profile, $profile);
  660. foreach ($_inputs AS $_input) {
  661. $_ckeditor_compiled[$_input] = $setting;
  662. }
  663. }
  664. $compiled = TRUE;
  665. return ( $input_format === FALSE ) ? $_ckeditor_compiled : $_ckeditor_compiled[$input_format];
  666. }
  667. /**
  668. * Compile settings of profile
  669. *
  670. * @param object $global_profile
  671. * @param object $profile
  672. * @return array
  673. */
  674. function ckeditor_profile_settings_compile($global_profile, $profile) {
  675. global $user, $language, $theme;
  676. $current_theme = variable_get('theme_default', $theme);
  677. $settings = array();
  678. $conf = array();
  679. $conf = $profile->settings;
  680. $profile_name = $profile->name;
  681. if (user_access('customize ckeditor')) {
  682. foreach (array('default', 'show_toggle', 'width', 'lang', 'auto_lang') as $setting) {
  683. $conf[$setting] = ckeditor_user_get_setting($user, $profile, $setting);
  684. }
  685. }
  686. if (!isset($conf['ss'])) {
  687. $conf['ss'] = 2;
  688. }
  689. $themepath = drupal_get_path('theme', $current_theme) . '/';
  690. $host = base_path();
  691. // setting some variables
  692. $base_path = ckeditor_base_path('relative');
  693. $module_drupal_path = ckeditor_module_path('relative');
  694. $module_drupal_local_path = ckeditor_module_path('local');
  695. $editor_path = ckeditor_path('relative');
  696. $editor_local_path = ckeditor_path('local');
  697. // get the default drupal files path
  698. $files_path = $host . variable_get('file_private_path', conf_path() . '/files');
  699. $toolbar = $conf['toolbar'];
  700. if (!empty($conf['theme_config_js']) && $conf['theme_config_js'] == 't' && file_exists($themepath . 'ckeditor.config.js')) {
  701. $ckeditor_config_path = $host . $themepath . 'ckeditor.config.js?' . @filemtime($themepath . 'ckeditor.config.js');
  702. }
  703. else {
  704. $ckeditor_config_path = $module_drupal_path . "/ckeditor.config.js?" . @filemtime($module_drupal_path . "/ckeditor.config.js");
  705. }
  706. $settings['customConfig'] = $ckeditor_config_path;
  707. $settings['defaultLanguage'] = $conf['lang'];
  708. $settings['toolbar'] = $toolbar;
  709. $settings['enterMode'] = constant("CKEDITOR_ENTERMODE_" . strtoupper($conf['enter_mode']));
  710. $settings['shiftEnterMode'] = constant("CKEDITOR_ENTERMODE_" . strtoupper($conf['shift_enter_mode']));
  711. $settings['toolbarStartupExpanded'] = ( $conf['expand'] == 't' );
  712. $settings['width'] = $conf['width'];
  713. //check if skin exists, if not select default one
  714. if (isset($global_profile->settings['skin']) && file_exists($editor_local_path . '/skins/' . $global_profile->settings['skin'])) {
  715. $settings['skin'] = $global_profile->settings['skin'];
  716. }
  717. else {
  718. $settings['skin'] = ckeditor_default_skin();
  719. }
  720. $settings['format_tags'] = $conf['font_format'];
  721. $settings['show_toggle'] = $conf['show_toggle'];
  722. $settings['ss'] = $conf['ss'];
  723. if (isset($conf['language_direction'])) {
  724. switch ($conf['language_direction']) {
  725. case 'default':
  726. if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
  727. $settings['contentsLangDirection'] = 'rtl';
  728. }
  729. break;
  730. case 'ltr':
  731. $settings['contentsLangDirection'] = 'ltr';
  732. break;
  733. case 'rtl':
  734. $settings['contentsLangDirection'] = 'rtl';
  735. break;
  736. }
  737. }
  738. if (isset($conf['loadPlugins'])) {
  739. $settings['loadPlugins'] = ckeditor_plugins_render($conf['loadPlugins']);
  740. if (array_key_exists('media', $settings['loadPlugins']) && module_exists('media')) {
  741. module_load_include('inc', 'media', 'includes/media.browser');
  742. $javascript = media_browser_js();
  743. foreach ($javascript as $key => $definitions) {
  744. foreach ($definitions as $definition) {
  745. $function = 'drupal_add_' . $key;
  746. call_user_func_array($function, $definition);
  747. }
  748. }
  749. drupal_add_js(drupal_get_path('module', 'ckeditor') . '/plugins/media/library.js', array('scope' => 'footer', 'weight' => -20));
  750. }
  751. }
  752. else {
  753. $settings['loadPlugins'] = array();
  754. }
  755. //add support for divarea plugin from CKE4
  756. if (((isset($conf['use_divarea']) && $conf['use_divarea'] == 't') || !isset($conf['use_divarea'])) && file_exists($editor_local_path . '/plugins/divarea/plugin.js')) {
  757. $settings['loadPlugins']['divarea'] = array('name' => 'divarea', 'path' => $editor_path . '/plugins/divarea/', 'buttons' => FALSE, 'default' => 't');
  758. }
  759. if (isset($conf['html_entities']) && $conf['html_entities'] == 'f') {
  760. $settings['entities'] = FALSE;
  761. $settings['entities_greek'] = FALSE;
  762. $settings['entities_latin'] = FALSE;
  763. }
  764. if (isset($conf['scayt_autoStartup']) && $conf['scayt_autoStartup'] == 't') {
  765. $settings['scayt_autoStartup'] = TRUE;
  766. }
  767. else {
  768. $settings['scayt_autoStartup'] = FALSE;
  769. }
  770. if ($conf['auto_lang'] == "f") {
  771. $settings['language'] = $conf['lang'];
  772. //[#1473010]
  773. $settings['scayt_sLang'] = ckeditor_scayt_langcode($conf['lang']);
  774. }
  775. if (isset($conf['forcePasteAsPlainText']) && $conf['forcePasteAsPlainText'] == 't') {
  776. $settings['forcePasteAsPlainText'] = TRUE;
  777. }
  778. if (isset($conf['custom_formatting']) && $conf['custom_formatting'] == 't') {
  779. foreach ($conf['formatting']['custom_formatting_options'] as $k => $v) {
  780. if ($v === 0) {
  781. $conf['formatting']['custom_formatting_options'][$k] = FALSE;
  782. }
  783. else {
  784. $conf['formatting']['custom_formatting_options'][$k] = TRUE;
  785. }
  786. }
  787. $settings['output_pre_indent'] = $conf['formatting']['custom_formatting_options']['pre_indent'];
  788. unset($conf['formatting']['custom_formatting_options']['pre_indent']);
  789. $settings['custom_formatting'] = $conf['formatting']['custom_formatting_options'];
  790. }
  791. // add code for filebrowser for users that have access
  792. $filebrowser = !empty($conf['filebrowser']) ? $conf['filebrowser'] : 'none';
  793. $filebrowser_image = !empty($conf['filebrowser_image']) ? $conf['filebrowser_image'] : $filebrowser;
  794. $filebrowser_flash = !empty($conf['filebrowser_flash']) ? $conf['filebrowser_flash'] : $filebrowser;
  795. if ($filebrowser == 'imce' && !module_exists('imce')) {
  796. $filebrowser = 'none';
  797. }
  798. if ($filebrowser == 'elfinder' && !module_exists('elfinder')) {
  799. $filebrowser = 'none';
  800. }
  801. /* MODULES NOT PORTED TO D7
  802. if ($filebrowser == 'tinybrowser' && !module_exists('tinybrowser')) {
  803. $filebrowser = 'none';
  804. }
  805. if ($filebrowser == 'ib' && !module_exists('imagebrowser')) {
  806. $filebrowser = 'none';
  807. }
  808. if ($filebrowser == 'webfm' && !module_exists('webfm_popup')) {
  809. $filebrowser = 'none';
  810. }
  811. */
  812. if ($filebrowser_image != $filebrowser) {
  813. if ($filebrowser_image == 'imce' && !module_exists('imce')) {
  814. $filebrowser_image = $filebrowser;
  815. }
  816. if ($filebrowser_image == 'elfinder' && !module_exists('elfinder')) {
  817. $filebrowser_image = $filebrowser;
  818. }
  819. /* MODULES NOT PORTED TO D7
  820. if ($filebrowser_image == 'tinybrowser' && !module_exists('tinybrowser')) {
  821. $filebrowser_image = $filebrowser;
  822. }
  823. if ($filebrowser_image == 'ib' && !module_exists('imagebrowser')) {
  824. $filebrowser_image = $filebrowser;
  825. }
  826. if ($filebrowser_image == 'webfm' && !module_exists('webfm_popup')) {
  827. $filebrowser_image = $filebrowser;
  828. }
  829. */
  830. }
  831. if ($filebrowser_flash != $filebrowser) {
  832. if ($filebrowser_flash == 'imce' && !module_exists('imce')) {
  833. $filebrowser_flash = $filebrowser;
  834. }
  835. if ($filebrowser_image == 'elfinder' && !module_exists('elfinder')) {
  836. $filebrowser_flash = $filebrowser;
  837. }
  838. /* MODULES NOT PORTED TO D7
  839. if ($filebrowser_image == 'tinybrowser' && !module_exists('tinybrowser')) {
  840. $filebrowser_flash = $filebrowser;
  841. }
  842. if ($filebrowser_flash == 'ib' && !module_exists('imagebrowser')) {
  843. $filebrowser_flash = $filebrowser;
  844. }
  845. if ($filebrowser_flash == 'webfm' && !module_exists('webfm_popup')) {
  846. $filebrowser_flash = $filebrowser;
  847. }
  848. */
  849. }
  850. if ($filebrowser == 'ckfinder' || $filebrowser_image == 'ckfinder' || $filebrowser_flash == 'ckfinder') {
  851. if (user_access('allow CKFinder file uploads')) {
  852. if (!empty($profile->settings['UserFilesPath'])) {
  853. $_SESSION['ckeditor'][$profile_name]['UserFilesPath'] = strtr($profile->settings['UserFilesPath'], array("%f" => variable_get('file_public_path', conf_path() . '/files'), "%u" => $user->uid, "%b" => $host, "%n" => $user->name));
  854. }
  855. else {
  856. $_SESSION['ckeditor'][$profile_name]['UserFilesPath'] = strtr('%b%f/', array("%f" => variable_get('file_public_path', conf_path() . '/files'), "%u" => $user->uid, "%b" => $host, "%n" => $user->name));
  857. }
  858. if (!empty($profile->settings['UserFilesAbsolutePath'])) {
  859. $_SESSION['ckeditor'][$profile_name]['UserFilesAbsolutePath'] = strtr($profile->settings['UserFilesAbsolutePath'], array("%f" => variable_get('file_public_path', conf_path() . '/files'), "%u" => $user->uid, "%b" => base_path(), "%d" => ckeditor_get_document_root_full_path(), "%n" => $user->name));
  860. }
  861. else {
  862. $_SESSION['ckeditor'][$profile_name]['UserFilesAbsolutePath'] = strtr('%d%b%f/', array("%f" => variable_get('file_public_path', conf_path() . '/files'), "%u" => $user->uid, "%b" => base_path(), "%d" => ckeditor_get_document_root_full_path(), "%n" => $user->name));
  863. }
  864. if (variable_get('file_default_scheme', '') == 'private') {
  865. $private_dir = isset($global_profile->settings['private_dir']) ? trim($global_profile->settings['private_dir'], '\/') : '';
  866. if (!empty($private_dir)) {
  867. $private_dir = strtr($private_dir, array('%u' => $user->uid, '%n' => $user->name));
  868. $_SESSION['ckeditor'][$profile_name]['UserFilesPath'] = url('system/files') . '/' . $private_dir . '/';
  869. $_SESSION['ckeditor'][$profile_name]['UserFilesAbsolutePath'] = variable_get('file_private_path', '') . DIRECTORY_SEPARATOR . $private_dir . DIRECTORY_SEPARATOR;
  870. }
  871. else {
  872. $_SESSION['ckeditor'][$profile_name]['UserFilesPath'] = url('system/files') . '/';
  873. $_SESSION['ckeditor'][$profile_name]['UserFilesAbsolutePath'] = variable_get('file_private_path', '') . DIRECTORY_SEPARATOR;
  874. }
  875. }
  876. }
  877. }
  878. /* MODULES NOT PORTED TO D7
  879. if (in_array('tinybrowser', array($filebrowser, $filebrowser_image, $filebrowser_flash))) {
  880. $popup_win_size = variable_get('tinybrowser_popup_window_size', '770x480');
  881. if (!preg_match('#\d+x\d+#is', $popup_win_size)) {
  882. $popup_win_size = '770x480';
  883. }
  884. $popup_win_size = trim($popup_win_size);
  885. $popup_win_size = strtolower($popup_win_size);
  886. $win_size = split('x', $popup_win_size);
  887. }
  888. */
  889. switch ($filebrowser) {
  890. case 'ckfinder':
  891. if (user_access('allow CKFinder file uploads')) {
  892. $ckfinder_full_path = ckfinder_path('relative');
  893. $settings['filebrowserBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html?id=' . $profile_name;
  894. $settings['filebrowserImageBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html?Type=Images&id=' . $profile_name;
  895. $settings['filebrowserFlashBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html?Type=Flash&id=' . $profile_name;
  896. $settings['filebrowserUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Files&id=' . $profile_name;
  897. $settings['filebrowserImageUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Images&id=' . $profile_name;
  898. $settings['filebrowserFlashUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Flash&id=' . $profile_name;
  899. }
  900. break;
  901. case 'imce':
  902. $settings['filebrowserBrowseUrl'] = url('imce', array('query' => array('app' => 'ckeditor|sendto@ckeditor_imceSendTo|')));
  903. break;
  904. case 'elfinder':
  905. $settings['filebrowserBrowseUrl'] = $host . "index.php?q=elfinder&app=ckeditor";
  906. break;
  907. /* MODULES NOT PORTED TO D7
  908. case 'webfm':
  909. if (user_access('access webfm')) {
  910. $settings['filebrowserBrowseUrl'] = $host . "index.php?q=webfm_popup";
  911. }
  912. break;
  913. case 'ib':
  914. if (user_access('browse own images')) {
  915. $settings['filebrowserBrowseUrl'] = $host . "index.php?q=imagebrowser/view/browser&app=ckeditor";
  916. $settings['filebrowserWindowWidth'] = 700;
  917. $settings['filebrowserWindowHeight'] = 520;
  918. }
  919. break;
  920. case 'tinybrowser':
  921. $settings['filebrowserBrowseUrl'] = $host . drupal_get_path('module', 'tinybrowser') . "/tinybrowser/tinybrowser.php?type=file";
  922. $settings['filebrowserWindowWidth'] = (int) $win_size[0] + 15;
  923. $settings['filebrowserWindowHeight'] = (int) $win_size[1] + 15;
  924. break;
  925. */
  926. }
  927. if ($filebrowser_image != $filebrowser) {
  928. switch ($filebrowser_image) {
  929. case 'ckfinder':
  930. if (user_access('allow CKFinder file uploads')) {
  931. $ckfinder_full_path = ckfinder_path('relative');
  932. $settings['filebrowserImageBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html?Type=Images&id=' . $profile_name;
  933. $settings['filebrowserImageUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Images&id=' . $profile_name;
  934. }
  935. break;
  936. case 'imce':
  937. $settings['filebrowserImageBrowseUrl'] = url('imce', array('query' => array('app' => 'ckeditor|sendto@ckeditor_imceSendTo|')));
  938. break;
  939. case 'elfinder':
  940. $settings['filebrowserImageBrowseUrl'] = $host . "index.php?q=elfinder&app=ckeditor";
  941. break;
  942. /* MODULES NOT PORTED TO D7
  943. case 'webfm':
  944. if (user_access('access webfm')) {
  945. $settings['filebrowserImageBrowseUrl'] = $host . "index.php?q=webfm_popup";
  946. }
  947. break;
  948. case 'ib':
  949. if (user_access('browse own images')) {
  950. $settings['filebrowserImageBrowseUrl'] = $host . "index.php?q=imagebrowser/view/browser&app=ckeditor";
  951. $settings['filebrowserImageWindowWidth'] = 680;
  952. $settings['filebrowserImageWindowHeight'] = 439;
  953. }
  954. break;
  955. case 'tinybrowser':
  956. $settings['filebrowserImageBrowseUrl'] = $host . drupal_get_path('module', 'tinybrowser') . "/tinybrowser/tinybrowser.php?type=image";
  957. $settings['filebrowserImageWindowWidth'] = (int) $win_size[0] + 15;
  958. $settings['filebrowserImageWindowHeight'] = (int) $win_size[1] + 15;
  959. break;
  960. */
  961. }
  962. }
  963. if ($filebrowser_flash != $filebrowser) {
  964. switch ($filebrowser_flash) {
  965. case 'ckfinder':
  966. if (user_access('allow CKFinder file uploads')) {
  967. $ckfinder_full_path = ckfinder_path('relative');
  968. $settings['filebrowserFlashBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html?Type=Images&id=' . $profile_name;
  969. $settings['filebrowserFlashUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Images&id=' . $profile_name;
  970. }
  971. break;
  972. case 'imce':
  973. $settings['filebrowserFlashBrowseUrl'] = url('imce', array('query' => array('app' => 'ckeditor|sendto@ckeditor_imceSendTo|')));
  974. break;
  975. case 'elfinder':
  976. $settings['filebrowserFlashBrowseUrl'] = $host . "index.php?q=elfinder&app=ckeditor";
  977. break;
  978. /* MODULES NOT PORTED TO D7
  979. case 'webfm':
  980. if (user_access('access webfm')) {
  981. $settings['filebrowserFlashBrowseUrl'] = $host . "index.php?q=webfm_popup";
  982. }
  983. break;
  984. case 'ib':
  985. if (user_access('browse own images')) {
  986. $settings['filebrowserFlashBrowseUrl'] = $host . "index.php?q=imagebrowser/view/browser&app=ckeditor";
  987. $settings['filebrowserFlashWindowWidth'] = 680;
  988. $settings['filebrowserFlashWindowHeight'] = 439;
  989. }
  990. break;
  991. case 'tinybrowser':
  992. $settings['filebrowserFlashBrowseUrl'] = $host . drupal_get_path('module', 'tinybrowser') . "/tinybrowser/tinybrowser.php?type=media";
  993. $settings['filebrowserFlashWindowWidth'] = (int) $win_size[0] + 15;
  994. $settings['filebrowserFlashWindowHeight'] = (int) $win_size[1] + 15;
  995. break;
  996. */
  997. }
  998. }
  999. if (!empty($conf['js_conf'])) {
  1000. preg_match_all('#config\.(\w+)[\s]*=[\s]*(.+?);[\s]*(?=config\.|$)#is', preg_replace("/[\n\r]+/", "", $conf['js_conf']), $matches);
  1001. foreach ($matches[2] as $i => $match) {
  1002. if (!empty($match)) {
  1003. $value = trim($match, " ;\n\r\t\0\x0B");
  1004. if (strcasecmp($value, 'true') == 0) {
  1005. $value = TRUE;
  1006. }
  1007. if (strcasecmp($value, 'false') == 0) {
  1008. $value = FALSE;
  1009. }
  1010. $settings["js_conf"][$matches[1][$i]] = $value;
  1011. }
  1012. }
  1013. }
  1014. $settings['stylesCombo_stylesSet'] = "drupal:" . $module_drupal_path . '/ckeditor.styles.js';
  1015. if (!empty($conf['css_style'])) {
  1016. if ($conf['css_style'] == 'theme' && file_exists($themepath . 'ckeditor.styles.js')) {
  1017. $settings['stylesCombo_stylesSet'] = "drupal:" . $host . $themepath . 'ckeditor.styles.js';
  1018. }
  1019. elseif (!empty($conf['css_style']) && $conf['css_style'] == 'self') {
  1020. $conf['styles_path'] = str_replace("%h%t", "%t", $conf['styles_path']);
  1021. $settings['stylesCombo_stylesSet'] = "drupal:" . str_replace(array('%h', '%t', '%m'), array($host, $host . $themepath, $module_drupal_path), $conf['styles_path']);
  1022. }
  1023. }
  1024. // add custom stylesheet if configured
  1025. // lets hope it exists but we'll leave that to the site admin
  1026. $query_string = '?' . substr(variable_get('css_js_query_string', '0'), 0, 1);
  1027. $css_files = array();
  1028. switch ($conf['css_mode']) {
  1029. case 'theme':
  1030. global $language, $base_theme_info;
  1031. $themes = list_themes();
  1032. $theme_info = $themes[$current_theme];
  1033. if (!empty($theme_info->stylesheets)) {
  1034. $editorcss = "\"";
  1035. foreach ($base_theme_info as $base) { // Grab stylesheets from base theme
  1036. if (!empty($base->stylesheets)) { // may be empty when the base theme reference in the info file is invalid
  1037. foreach ($base->stylesheets as $type => $stylesheets) {
  1038. if ($type != "print") {
  1039. foreach ($stylesheets as $name => $path) {
  1040. if (file_exists($path)) {
  1041. $css_files[$name] = $host . $path . $query_string;
  1042. // Grab rtl stylesheets ( will get rtl css files when thay are named with suffix "-rtl.css" (ex: fusion baased themes) )
  1043. if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL && substr($path, 0, -8) != "-rtl.css") {
  1044. $rtl_path = substr($path, 0, -4) . "-rtl.css";
  1045. if (file_exists($rtl_path)) {
  1046. $css_files[$name . "-rtl"] = $host . $rtl_path . $query_string;
  1047. }
  1048. }
  1049. }
  1050. }
  1051. }
  1052. }
  1053. }
  1054. }
  1055. if (!empty($theme_info->stylesheets)) { // Grab stylesheets from current theme
  1056. foreach ($theme_info->stylesheets as $type => $stylesheets) {
  1057. if ($type != "print") {
  1058. foreach ($stylesheets as $name => $path) {
  1059. // Checks if less module exists...
  1060. if (strstr($path, '.less') && module_exists('less')) {
  1061. $path = 'sites/default/files/less/' . $path; // append the less file path
  1062. $path = str_replace('.less', '', $path); // remove the .less
  1063. }
  1064. if (file_exists($path)) {
  1065. $css_files[$name] = $host . $path . $query_string;
  1066. // Grab rtl stylesheets ( will get rtl css files when thay are named with suffix "-rtl.css" (ex: fusion baased themes) )
  1067. if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL && substr($path, 0, -8) != "-rtl.css") {
  1068. $rtl_path = substr($path, 0, -4) . "-rtl.css";
  1069. if (file_exists($rtl_path)) {
  1070. $css_files[$name . "-rtl"] = $host . $rtl_path . $query_string;
  1071. }
  1072. }
  1073. }
  1074. elseif (!empty($css_files[$name])) {
  1075. unset($css_files[$name]);
  1076. }
  1077. }
  1078. }
  1079. }
  1080. }
  1081. // Grab stylesheets local.css and local-rtl.css if they exist (fusion based themes)
  1082. if (file_exists($themepath . 'css/local.css')) {
  1083. $css_files[] = $host . $themepath . 'css/local.css' . $query_string;
  1084. }
  1085. if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL && file_exists($themepath . 'css/local-rtl.css')) {
  1086. $css_files[] = $host . $themepath . 'css/local-rtl.css' . $query_string;
  1087. }
  1088. // Grab stylesheets from color module
  1089. $color_paths = variable_get('color_' . $current_theme . '_stylesheets', array());
  1090. if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
  1091. if (!empty($color_paths[1])) {
  1092. $css_files[] = $host . $color_paths[1] . $query_string;
  1093. }
  1094. }
  1095. elseif (!empty($color_paths[0])) {
  1096. $css_files[] = $host . $color_paths[0] . $query_string;
  1097. }
  1098. }
  1099. else {
  1100. if (!file_exists($themepath . 'ckeditor.css') && file_exists($themepath . 'style.css')) {
  1101. $css_files[] = $host . $themepath . 'style.css' . $query_string;
  1102. }
  1103. }
  1104. if (file_exists($module_drupal_local_path . '/ckeditor.css')) {
  1105. $css_files[] = $module_drupal_path . '/ckeditor.css' . $query_string;
  1106. }
  1107. if (file_exists($themepath . 'ckeditor.css')) {
  1108. $css_files[] = $host . $themepath . 'ckeditor.css' . $query_string;
  1109. }
  1110. break;
  1111. case 'self':
  1112. if (file_exists($module_drupal_local_path . '/ckeditor.css')) {
  1113. $css_files[] = $module_drupal_path . '/ckeditor.css' . $query_string;
  1114. if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
  1115. if (file_exists($module_drupal_local_path . '/ckeditor-rtl.css')) {
  1116. $css_files[] = $module_drupal_path . '/ckeditor-rtl.css' . $query_string;
  1117. }
  1118. }
  1119. }
  1120. foreach (explode(',', $conf['css_path']) as $css_path) {
  1121. $css_path = trim(str_replace("%h%t", "%t", $css_path));
  1122. $css_files[] = str_replace(array('%h', '%t'), array($host, $host . $themepath), $css_path) . $query_string;
  1123. }
  1124. break;
  1125. case 'none':
  1126. if (file_exists($module_drupal_local_path . '/ckeditor.css')) {
  1127. $css_files[] = $module_drupal_path . '/ckeditor.css' . $query_string;
  1128. if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
  1129. if (file_exists($module_drupal_local_path . '/ckeditor-rtl.css')) {
  1130. $css_files[] = $module_drupal_path . '/ckeditor-rtl.css' . $query_string;
  1131. }
  1132. }
  1133. }
  1134. if (file_exists($editor_local_path . '/contents.css')) {
  1135. $css_files[] = $editor_path . '/contents.css' . $query_string;
  1136. }
  1137. break;
  1138. }
  1139. if (isset($conf['ckeditor_load_method']) && $conf['ckeditor_load_method'] == 'ckeditor_source.js') {
  1140. foreach ($css_files as $k => $v) {
  1141. $css_files[$k] = $v . '&t=' . time();
  1142. }
  1143. }
  1144. $settings['contentsCss'] = array_values($css_files);
  1145. if (!empty($conf['uicolor']) && $conf['uicolor'] == "custom" && !empty($conf['uicolor_user'])) {
  1146. $settings['uiColor'] = $conf['uicolor_user'];
  1147. }
  1148. if (!empty($conf['uicolor']) && strpos($conf['uicolor'], "color_") === 0) {
  1149. if (function_exists('color_get_palette')) {
  1150. $palette = @color_get_palette($current_theme, FALSE); //[#652274]
  1151. $color = str_replace("color_", "", $conf['uicolor']);
  1152. if (!empty($palette[$color])) {
  1153. $settings['uiColor'] = $palette[$color];
  1154. }
  1155. }
  1156. }
  1157. return $settings;
  1158. }
  1159. /**
  1160. * Load CKEditor for field ID
  1161. *
  1162. * @param object $field
  1163. * @param string $format
  1164. *
  1165. * @return object
  1166. *
  1167. */
  1168. function ckeditor_load_by_field($field, $format, $show_toggle = TRUE, $add_fields_to_toggle = FALSE) {
  1169. global $theme;
  1170. static $processed_ids = array();
  1171. static $is_running = FALSE;
  1172. $use_ckeditor = FALSE;
  1173. $format_arr = FALSE;
  1174. $suffix = '';
  1175. if (is_array($format)) {
  1176. $format_arr = $format;
  1177. $format = isset($format_arr['#value']) ? $format_arr['#value'] : $format_arr['#default_value'];
  1178. }
  1179. if (!isset($field['#id'])) {
  1180. return $field;
  1181. }
  1182. if (isset($processed_ids[$field['#id']])) {
  1183. return $field;
  1184. }
  1185. if (key_exists('#wysiwyg', $field) && !$field['#wysiwyg']) {
  1186. return $field;
  1187. }
  1188. if (isset($field['#access']) && !$field['#access']) {
  1189. return $field;
  1190. }
  1191. if ($field['#id'] == "edit-log") {
  1192. return $field;
  1193. }
  1194. if (isset($field['#attributes']['disabled']) && $field['#attributes']['disabled'] == 'disabled') {
  1195. return $field;
  1196. }
  1197. drupal_add_js(array('ckeditor' => array('textarea_default_format' => array($field['#id'] => $format))), 'setting');
  1198. if (!isset($processed_ids[$field['#id']])) {
  1199. $processed_ids[$field['#id']] = array();
  1200. }
  1201. $global_profile = ckeditor_profile_load('CKEditor Global Profile');
  1202. $profile = ckeditor_get_profile($format);
  1203. $host = base_path();
  1204. if ($profile === FALSE) {
  1205. $ckeditor_in_default_format = FALSE;
  1206. foreach ((array) $format_arr['#options'] as $key => $val) {
  1207. if ($key == $format)
  1208. continue;
  1209. if ($profile = ckeditor_get_profile($key)) {
  1210. $use_ckeditor = $key;
  1211. break;
  1212. }
  1213. }
  1214. if ($use_ckeditor === FALSE) {
  1215. return $field;
  1216. }
  1217. }
  1218. else {
  1219. $ckeditor_in_default_format = TRUE;
  1220. }
  1221. $settings = FALSE;
  1222. if ($settings = ckeditor_profiles_compile($format)) {
  1223. $ckeditor_on = ($profile->settings['default'] == 't') ? TRUE : FALSE;
  1224. }
  1225. elseif ($settings = ckeditor_profiles_compile($use_ckeditor)) {
  1226. $ckeditor_on = FALSE;
  1227. }
  1228. else {
  1229. return $field;
  1230. }
  1231. if ($settings) {
  1232. $textarea_id = $field['#id'];
  1233. $class[] = 'ckeditor-mod';
  1234. $_ckeditor_ids[] = $textarea_id;
  1235. //settings are saved as strings, not booleans
  1236. if ($settings['show_toggle'] == 't' && $show_toggle) {
  1237. if ($add_fields_to_toggle !== FALSE) {
  1238. if (is_array($add_fields_to_toggle)) {
  1239. $toggle_fields = "['" . $textarea_id . "','" . implode("','", $add_fields_to_toggle) . "']";
  1240. }
  1241. else {
  1242. $toggle_fields = "['" . $textarea_id . "','" . $add_fields_to_toggle . "']";
  1243. }
  1244. }
  1245. else {
  1246. $toggle_fields = "['{$textarea_id}']";
  1247. }
  1248. $wysiwyg_link = '';
  1249. $wysiwyg_link .= "<a class=\"ckeditor_links\" style=\"display:none\" href=\"javascript:void(0);\" onclick=\"javascript:Drupal.ckeditorToggle({$toggle_fields},'" . str_replace("'", "\\'", t('Switch to plain text editor')) . "','" . str_replace("'", "\\'", t('Switch to rich text editor')) . "');\" id=\"switch_{$textarea_id}\">";
  1250. $wysiwyg_link .= $ckeditor_on ? t('Switch to plain text editor') : t('Switch to rich text editor');
  1251. $wysiwyg_link .= '</a>';
  1252. // Make sure to append to #suffix so it isn't completely overwritten
  1253. $suffix .= $wysiwyg_link;
  1254. }
  1255. $editor_local_path = ckeditor_path('local');
  1256. $editor_url_path = ckeditor_path('url');
  1257. // get the default drupal files path
  1258. $files_path = $host . variable_get('file_private_path', conf_path() . '/files');
  1259. if (!$is_running) {
  1260. if (!$ckeditor_in_default_format) {
  1261. $load_method = 'ckeditor_basic.js';
  1262. $load_time_out = 0;
  1263. }
  1264. elseif (isset($profile->settings['ckeditor_load_method'])) {
  1265. $load_method = $profile->settings['ckeditor_load_method'];
  1266. $load_time_out = $profile->settings['ckeditor_load_time_out'];
  1267. }
  1268. drupal_add_js('window.CKEDITOR_BASEPATH = "' . ckeditor_path('relative') . '/"', array('type' => 'inline', 'weight' => -100));
  1269. drupal_add_js(ckeditor_module_path('url') . '/includes/ckeditor.utils.js', array('type' => 'file', 'scope' => 'footer'));
  1270. $preprocess = FALSE;
  1271. if (isset($global_profile->settings['ckeditor_aggregate']) && $global_profile->settings['ckeditor_aggregate'] == 't') {
  1272. $preprocess = TRUE;
  1273. }
  1274. if (isset($load_method) && file_exists($editor_local_path . '/' . $load_method)) {
  1275. drupal_add_js($editor_url_path . '/' . $load_method, array('type' => 'file', 'scope' => 'footer', 'preprocess' => $preprocess));
  1276. if ($load_method == 'ckeditor_basic.js') {
  1277. drupal_add_js('CKEDITOR.loadFullCoreTimeout = ' . $load_time_out . ';', array('type' => 'inline', 'scope' => 'footer'));
  1278. drupal_add_js(array('ckeditor' => array('load_timeout' => TRUE)), 'setting');
  1279. }
  1280. }
  1281. else {
  1282. drupal_add_js($editor_url_path . '/ckeditor.js', array('type' => 'file', 'scope' => 'footer', 'preprocess' => $preprocess));
  1283. }
  1284. drupal_add_js(array('ckeditor' => array('module_path' => ckeditor_module_path('relative'), 'editor_path' => ckeditor_path('relative') . '/')), 'setting');
  1285. if (module_exists('paging')) {
  1286. drupal_add_js(array('ckeditor' => array('pagebreak' => TRUE)), 'setting');
  1287. }
  1288. if (module_exists('linktocontent_node')) {
  1289. drupal_add_js(array('ckeditor' => array('linktocontent_node' => TRUE)), 'setting');
  1290. }
  1291. if (module_exists('linktocontent_menu')) {
  1292. drupal_add_js(array('ckeditor' => array('linktocontent_menu' => TRUE)), 'setting');
  1293. }
  1294. if (module_exists('pagebreak')) {
  1295. drupal_add_js(array('ckeditor' => array('pagebreak' => TRUE)), 'setting');
  1296. }
  1297. drupal_add_js(array('ckeditor' => array('ajaxToken' => drupal_get_token('ckeditorAjaxCall'), 'xss_url' => url('ckeditor/xss'))), 'setting');
  1298. $is_running = TRUE;
  1299. }
  1300. drupal_add_js(array('ckeditor' => array('theme' => $theme)), 'setting');
  1301. if (!empty($settings)) {
  1302. drupal_add_js(array('ckeditor' => array('elements' => array($textarea_id => $format))), 'setting');
  1303. }
  1304. if (!empty($ckeditor_on)) {
  1305. drupal_add_js(array('ckeditor' => array('autostart' => array($textarea_id => $ckeditor_on))), 'setting');
  1306. }
  1307. //[#1473010]
  1308. if (isset($settings['scayt_sLang'])) {
  1309. drupal_add_js(array('ckeditor' => array('scayt_language' => $settings['scayt_sLang'])), 'setting');
  1310. }
  1311. elseif (!empty($field["#language"]) && $field["#language"] != LANGUAGE_NONE) {
  1312. drupal_add_js(array('ckeditor' => array('scayt_language' => ckeditor_scayt_langcode($field["#language"]))), 'setting');
  1313. }
  1314. // Remember extra information and reuse it during "Preview"
  1315. $processed_ids[$field['#id']]['suffix'] = $suffix;
  1316. $processed_ids[$field['#id']]['class'] = $class;
  1317. if (empty($field['#suffix'])) {
  1318. $field['#suffix'] = $suffix;
  1319. }
  1320. else {
  1321. $field['#suffix'] .= $suffix;
  1322. }
  1323. if (empty($field['#attributes']['class'])) {
  1324. $field['#attributes']['class'] = $class;
  1325. }
  1326. else {
  1327. $field['#attributes']['class'] = array_merge($field['#attributes']['class'], $class);
  1328. }
  1329. }
  1330. return $field;
  1331. }
  1332. /**
  1333. * Return all modules that provide security filters.
  1334. */
  1335. function ckeditor_security_filters() {
  1336. $security_filters = array();
  1337. $security_filters['modules'] = array(
  1338. 'htmLawed' => array(
  1339. 'title' => 'htmLawed',
  1340. 'project_page' => 'http://drupal.org/project/htmLawed',
  1341. 'weight' => 0,
  1342. 'installed' => FALSE,
  1343. 'filters' => array()
  1344. ),
  1345. 'htmltidy' => array(
  1346. 'title' => 'Htmltidy',
  1347. 'project_page' => 'http://drupal.org/project/htmltidy',
  1348. 'weight' => 0,
  1349. 'installed' => FALSE,
  1350. 'filters' => array()
  1351. ),
  1352. 'htmlpurifier' => array(
  1353. 'title' => 'HTML Purifier',
  1354. 'project_page' => 'http://drupal.org/project/htmlpurifier',
  1355. 'weight' => 0,
  1356. 'installed' => FALSE,
  1357. 'filters' => array()
  1358. ),
  1359. 'wysiwyg_filter' => array(
  1360. 'title' => 'WYSIWYG Filter',
  1361. 'project_page' => 'http://drupal.org/project/wysiwyg_filter',
  1362. 'weight' => 0,
  1363. 'installed' => FALSE,
  1364. 'filters' => array()
  1365. )
  1366. );
  1367. $security_filters['filters'] = array();
  1368. foreach ($security_filters['modules'] as $module_name => $module_conf) {
  1369. if (module_exists($module_name)) {
  1370. $security_filters['modules'][$module_name]['installed'] = TRUE;
  1371. $module_filters = module_invoke($module_name, 'filter_info');
  1372. foreach ($module_filters as $module_filter_name => $module_filter_conf) {
  1373. $security_filters['modules'][$module_name]['filters'][$module_filter_name] = $module_filter_conf;
  1374. $security_filters['filters'][$module_filter_name] = TRUE;
  1375. }
  1376. }
  1377. }
  1378. //add filters from Drupal core
  1379. $security_filters['modules']['__drupal'] = array(
  1380. 'title' => 'Drupal core',
  1381. 'project_page' => FALSE,
  1382. 'weight' => -1,
  1383. 'installed' => TRUE,
  1384. 'filters' => array(
  1385. 'filter_html' => array(
  1386. 'title' => 'Limit allowed HTML tags',
  1387. 'description' => 'Removes the attributes that the built-in "Limit allowed HTML tags"-filter does not allow inside HTML elements/tags'
  1388. )
  1389. )
  1390. );
  1391. $security_filters['filters']['filter_html'] = TRUE;
  1392. //load security filters added by API
  1393. $external_module_filters = module_invoke_all('ckeditor_security_filter');
  1394. if (count($external_module_filters) > 0) {
  1395. $security_filters['modules']['__external'] = array(
  1396. 'title' => 'External filters',
  1397. 'project_page' => FALSE,
  1398. 'weight' => 1,
  1399. 'installed' => TRUE,
  1400. 'filters' => array()
  1401. );
  1402. foreach ($external_module_filters as $module_filter_name => $module_filter_conf) {
  1403. $security_filters['modules']['__external']['filters'][$module_filter_name] = $module_filter_conf;
  1404. $security_filters['filters'][$module_filter_name] = TRUE;
  1405. }
  1406. }
  1407. return $security_filters;
  1408. }