| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
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 6.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 /** 37 * Guess the absolute server path to the document root 38 * Usually it should return $_SERVER['DOCUMENT_ROOT'] 39 * 40 * @todo Improve me!! 41 * Returns absolute path or false on failure 42 * 43 * @return string|boolean 44 */ 45 function ckeditor_get_document_root_full_path() { 46 $found=0; 47 $index_dir = realpath(dirname($_SERVER['SCRIPT_FILENAME'])); // {/dir1/dir2/home/drupal}/index.php 48 if (getcwd() == $index_dir) { 49 $found++; 50 } 51 $drupal_dir = base_path(); 52 $index_dir = str_replace('\\', "/", $index_dir); 53 $drupal_dir = str_replace('\\', "/", $drupal_dir); 54 $document_root_dir = $index_dir .'/'. str_repeat('../', substr_count($drupal_dir, '/')-1); 55 $document_root_dir = realpath($document_root_dir); 56 $document_root_dir = rtrim($document_root_dir, '/\\'); 57 if ($document_root_dir == $_SERVER['DOCUMENT_ROOT']) { 58 $found++; 59 } 60 $document_root_dir = str_replace('\\', '/', $document_root_dir); 61 62 if ($document_root_dir != '') { 63 $found++; 64 } 65 if (file_exists($document_root_dir)) { 66 $found++; 67 } 68 if (file_exists($document_root_dir . base_path() .'includes/bootstrap.inc')) { 69 $found++; 70 } 71 72 if ($found >= 3) { 73 return $document_root_dir; 74 } 75 else{ 76 return FALSE; 77 } 78 } 79 80 /** 81 * Emulates the asp Server.mapPath function. 82 * Given an url path return the physical directory that it corresponds to. 83 * 84 * Returns absolute path or false on failure 85 * 86 * @param string $path 87 * @return @return string|boolean 88 */ 89 function ckeditor_resolve_url($path) { 90 if (function_exists('apache_lookup_uri')) { 91 $info = @apache_lookup_uri($path); 92 if (!$info) { 93 return FALSE; 94 } 95 return $info->filename . $info->path_info ; 96 } 97 98 $document_root = ckeditor_get_document_root_full_path(); 99 if ($document_root !== FALSE) { 100 return $document_root . $path; 101 } 102 103 return FALSE; 104 } 105 106 function ckeditor_load_skin_options() { 107 $arr = array(); 108 $editor_local_path = ckeditor_path(TRUE); 109 $skin_dir = $editor_local_path .'/skins'; 110 if (is_dir($skin_dir)) { 111 $dh = @opendir($skin_dir); 112 if (FALSE !== $dh) { 113 while (($file = readdir($dh)) !== FALSE ) { 114 if (in_array($file, array(".", "..", "CVS", ".svn"))) { 115 continue; 116 } 117 if (is_dir($skin_dir . DIRECTORY_SEPARATOR . $file)) { 118 $arr[$file] = drupal_ucfirst($file); 119 } 120 } 121 closedir( $dh ); 122 } 123 } 124 125 //oops, we have no information about skins, let's use only default 126 if (empty($arr)) { 127 $arr = array( 128 'kama' => 'Kama', 129 ); 130 } 131 asort($arr); 132 133 return $arr; 134 } 135 136 function ckeditor_load_lang_options() { 137 $arr = array(); 138 $editor_local_path = ckeditor_path(TRUE); 139 $lang_file = $editor_local_path . '/lang/_languages.js'; 140 if (file_exists($lang_file)) { 141 $f = fopen($lang_file, 'r'); 142 $file = fread($f, filesize($lang_file)); 143 $tmp = explode('{', $file); 144 if (isset($tmp[2])) { 145 $tmp = explode('}', $tmp[2]); 146 } 147 $langs = explode(',', $tmp[0]); 148 foreach ($langs AS $lang) { 149 preg_match("/'?(\w+-?\w+)'?:'([\w\s\(\)]+)'/i", $lang, $matches); 150 if (isset($matches[1]) && isset($matches[2])) 151 $arr[$matches[1]] = $matches[2]; 152 } 153 } 154 155 //oops, we have no information about languages, let's use those available in CKEditor 2.4.3 156 if (empty($arr)) { 157 $arr = array( 158 'af' => 'Afrikaans', 159 'ar' => 'Arabic', 160 'bg' => 'Bulgarian', 161 'bn' => 'Bengali/Bangla', 162 'bs' => 'Bosnian', 163 'ca' => 'Catalan', 164 'cs' => 'Czech', 165 'da' => 'Danish', 166 'de' => 'German', 167 'el' => 'Greek', 168 'en' => 'English', 169 'en-au' => 'English (Australia)', 170 'en-ca' => 'English (Canadian)', 171 'en-uk' => 'English (United Kingdom)', 172 'eo' => 'Esperanto', 173 'es' => 'Spanish', 174 'et' => 'Estonian', 175 'eu' => 'Basque', 176 'fa' => 'Persian', 177 'fi' => 'Finnish', 178 'fo' => 'Faroese', 179 'fr' => 'French', 180 'gl' => 'Galician', 181 'he' => 'Hebrew', 182 'hi' => 'Hindi', 183 'hr' => 'Croatian', 184 'hu' => 'Hungarian', 185 'it' => 'Italian', 186 'ja' => 'Japanese', 187 'km' => 'Khmer', 188 'ko' => 'Korean', 189 'lt' => 'Lithuanian', 190 'lv' => 'Latvian', 191 'mn' => 'Mongolian', 192 'ms' => 'Malay', 193 'nb' => 'Norwegian Bokmal', 194 'nl' => 'Dutch', 195 'no' => 'Norwegian', 196 'pl' => 'Polish', 197 'pt' => 'Portuguese (Portugal)', 198 'pt-br' => 'Portuguese (Brazil)', 199 'ro' => 'Romanian', 200 'ru' => 'Russian', 201 'sk' => 'Slovak', 202 'sl' => 'Slovenian', 203 'sr' => 'Serbian (Cyrillic)', 204 'sr-latn' => 'Serbian (Latin)', 205 'sv' => 'Swedish', 206 'th' => 'Thai', 207 'tr' => 'Turkish', 208 'uk' => 'Ukrainian', 209 'vi' => 'Vietnamese', 210 'zh' => 'Chinese Traditional', 211 'zh-cn' => 'Chinese Simplified', 212 ); 213 } 214 215 asort($arr); 216 217 return $arr; 218 } 219 220 /** 221 * List of CKEditor plugins 222 * 223 * @return array 224 */ 225 function ckeditor_load_plugins($render = FALSE) { 226 $arr = array(); 227 $base_path = '%base_path%'; 228 $editor_path = '%editor_path%'; 229 $ckeditor_path = '%ckeditor_path%'; 230 $plugin_dir = '%plugin_dir%'; 231 $plugin_dir_additional = '%plugin_dir_extra%'; 232 $pattern = '#\.addButton\([\s]*[\'"](.*?)[\'"][\s]*\,[\s]*\{[\s]*(.*?)[\s]*\}#s'; 233 234 /* 235 * External plugins 236 */ 237 if (module_exists('ckeditor_swf') && file_exists(drupal_get_path('module', 'ckeditor_swf') . '/plugins/swf/plugin.js')) { 238 $arr['ckeditor_swf'] = array( 239 'name' => 'swf', 240 'desc' => t('Support for the CKEditor SWF module'), 241 'path' => $base_path . drupal_get_path('module', 'ckeditor_swf') . '/plugins/swf/', 242 'buttons' => FALSE, 243 'default' => 't' 244 ); 245 } 246 247 if (module_exists('ckeditor_link') && file_exists(drupal_get_path('module', 'ckeditor_link') . '/plugins/link/plugin.js')) { 248 $arr['ckeditor_link'] = array( 249 'name' => 'drupal_path', 250 'desc' => t('Support for the CKEditor Link module'), 251 'path' => $base_path . drupal_get_path('module', 'ckeditor_link') . '/plugins/link/', 252 'buttons' => FALSE, 253 'default' => 't' 254 ); 255 } 256 257 if (module_exists('linkit') && file_exists(drupal_get_path('module', 'linkit') . '/editors/ckeditor/plugin.js')) { 258 $arr['linkit'] = array( 259 'name' => 'Linkit', 260 'desc' => t('Support for the Linkit module <em>(buttons: Linkit)</em>'), 261 'path' => $base_path . drupal_get_path('module', 'linkit') . '/editors/ckeditor/', 262 'buttons' => array( 263 'Linkit' => array( 264 'title' => 'Linkit', 265 'icon' => $base_path . drupal_get_path('module', 'linkit') . '/editors/ckeditor/linkit.png' 266 ) 267 ), 268 'default' => 't' 269 ); 270 } 271 272 /* 273 * CKEditor build-in plugins 274 */ 275 $_editor_path = ckeditor_path(TRUE) . '/'; 276 if (file_exists($_editor_path . 'plugins/tableresize/plugin.js')) { 277 $arr['tableresize'] = array( 278 'name' => 'tableresize', 279 'desc' => t('Table Resize plugin'), 280 'path' => $base_path . $editor_path . 'plugins/tableresize/', 281 'buttons' => FALSE, 282 'default' => 't' 283 ); 284 } 285 286 if (file_exists($_editor_path . 'plugins/autogrow/plugin.js')) { 287 $arr['autogrow'] = array( 288 'name' => 'autogrow', 289 'desc' => t('Auto Grow plugin'), 290 'path' => $base_path . $editor_path . 'plugins/autogrow/', 291 'buttons' => FALSE, 292 'default' => 'f' 293 ); 294 } 295 296 if (file_exists($_editor_path . 'plugins/stylesheetparser/plugin.js')) { 297 $arr['stylesheetparser'] = array( 298 'name' => 'stylesheetparser', 299 'desc' => t('Stylesheet Parser plugin'), 300 'path' => $base_path . $editor_path . 'plugins/stylesheetparser/', 301 'buttons' => FALSE, 302 'default' => 'f' 303 ); 304 } 305 306 /* 307 * CKEditor module plugins 308 */ 309 $_plugin_dir = drupal_get_path('module', 'ckeditor') . '/plugins/'; 310 if (is_dir($_plugin_dir) && $handle = opendir($_plugin_dir)) { 311 while (false !== ($file = readdir($handle))) { 312 if (is_dir($_plugin_dir . $file) && file_exists($_plugin_dir . $file . '/plugin.js')) { 313 $source = file_get_contents($_plugin_dir . $file . '/plugin.js'); 314 $buttons = array(); 315 if (preg_match_all($pattern, $source, $matches)) { 316 foreach ($matches[1] as $i => $button_name) { 317 if (preg_match('#(icon)[\s]*\:[\s]*([^\,\n]*)#', $matches[2][$i], $matches2)) { 318 $buttons[$button_name] = array(); 319 $buttons[$button_name]['label'] = $button_name; 320 $matches2[2] = str_replace(array('this.path', '+', '\'', '"'), array('', '', '', ''), $matches2[2]); 321 $buttons[$button_name]['icon'] = trim($matches2[2]); 322 } 323 } 324 } 325 if (preg_match('#@file ([^\n\r]*)#', $source, $matches)) { 326 $arr[$file] = array( 327 'name' => $file, 328 'desc' => t($matches[1]), 329 'path' => $base_path . $plugin_dir . $file . '/', 330 'buttons' => (count($buttons) > 0) ? $buttons : FALSE, 331 'default' => 'f' 332 ); 333 } 334 else { 335 $arr[$file] = array( 336 'name' => $file, 337 'desc' => t('Plugin file: ' . $file), 338 'path' => $base_path . $plugin_dir . $file . '/', 339 'buttons' => (count($buttons) > 0) ? $buttons : FALSE, 340 'default' => 'f' 341 ); 342 } 343 } 344 } 345 346 closedir($handle); 347 } 348 349 /* 350 * CKEditor module plugins - additional directory 351 */ 352 $_plugin_dir_additional = ckeditor_plugins_path(TRUE) . '/'; 353 if ($_plugin_dir != $_plugin_dir_additional && is_dir($_plugin_dir_additional) && $handle = opendir($_plugin_dir_additional)) { 354 while (false !== ($file = readdir($handle))) { 355 if (is_dir($_plugin_dir_additional . $file) && file_exists($_plugin_dir_additional . $file . '/plugin.js')) { 356 $source = file_get_contents($_plugin_dir_additional . $file . '/plugin.js'); 357 $buttons = array(); 358 if (preg_match_all($pattern, $source, $matches)) { 359 foreach ($matches[1] as $i => $button_name) { 360 if (preg_match('#(icon)[\s]*\:[\s]*([^\,\n]*)#', $matches[2][$i], $matches2)) { 361 $buttons[$button_name] = array(); 362 $buttons[$button_name]['label'] = $button_name; 363 $matches2[2] = str_replace(array('this.path', '+', '\'', '"'), array('', '', '', ''), $matches2[2]); 364 $buttons[$button_name]['icon'] = trim($matches2[2]); 365 } 366 } 367 } 368 if (preg_match('#@file ([^\n\r]*)#', $source, $matches)) { 369 $arr[$file] = array( 370 'name' => $file, 371 'desc' => t($matches[1]), 372 'path' => $base_path . $plugin_dir_additional . $file . '/', 373 'buttons' => (count($buttons) > 0) ? $buttons : FALSE, 374 'default' => 'f' 375 ); 376 } 377 else { 378 $arr[$file] = array( 379 'name' => $file, 380 'desc' => t('Plugin file: ' . $file), 381 'path' => $base_path . $plugin_dir_additional . $file . '/', 382 'buttons' => (count($buttons) > 0) ? $buttons : FALSE, 383 'default' => 'f' 384 ); 385 } 386 } 387 } 388 389 closedir($handle); 390 } 391 392 /* 393 * CKEditor plugins registered by hook 394 */ 395 $plugins = module_invoke_all('ckeditor_plugin'); 396 397 foreach ($plugins as $i => $plugin) { 398 if (file_exists($plugin['path'] . 'plugin.js')) { 399 $source = file_get_contents($plugin['path'] . 'plugin.js'); 400 $plugins[$i]['path'] = $base_path . $plugin['path']; 401 if (!isset($plugin['buttons']) || count($plugin['buttons']) == 0) { 402 $buttons = array(); 403 if (preg_match_all($pattern, $source, $matches)) { 404 foreach ($matches[1] as $j => $button_name) { 405 if (preg_match('#(icon)[\s]*\:[\s]*([^\,\n]*)#', $matches[2][$j], $matches2)) { 406 $buttons[$button_name] = array(); 407 $buttons[$button_name]['label'] = $button_name; 408 $matches2[2] = str_replace(array('this.path', '+', '\'', '"'), array('', '', '', ''), $matches2[2]); 409 $buttons[$button_name]['icon'] = trim($matches2[2]); 410 } 411 } 412 } 413 $plugins[$i]['buttons'] = (count($buttons) > 0) ? $buttons : FALSE; 414 } 415 } 416 else { 417 unset($plugins[$i]); 418 } 419 } 420 $arr = array_merge($arr, $plugins); 421 422 if (isset($arr['linktomenu']) && module_exists('linktocontent') == FALSE) { 423 unset($arr['linktomenu']); 424 } 425 426 if (isset($arr['linktonode']) && module_exists('linktocontent') == FALSE) { 427 unset($arr['linktonode']); 428 } 429 430 if (isset($arr['imce']) && module_exists('imce') == FALSE) { 431 unset($arr['imce']); 432 } 433 434 //remove page break button if there is no module to do this 435 if (isset($arr['drupalbreaks']['buttons']['DrupalPageBreak']) && !module_exists('paging') && !module_exists('pagebreak')) { 436 unset($arr['drupalbreaks']['buttons']['DrupalPageBreak']); 437 } 438 439 if (isset($arr['drupalbreaks'])) { 440 $arr['drupalbreaks']['default'] = 't'; 441 } 442 443 ksort($arr); 444 445 if ($render === TRUE) { 446 $arr = ckeditor_plugins_render($arr); 447 } 448 449 return $arr; 450 } 451 452 /** 453 * Render CKEditor plugins path 454 */ 455 function ckeditor_plugins_render($plugins) { 456 $render = array(); 457 $render["%base_path%"] = base_path(); 458 $render["%editor_path%"] = ckeditor_path(TRUE) . '/'; 459 $render["%ckeditor_path%"] = drupal_get_path('module', 'ckeditor') . '/'; 460 $render["%plugin_dir%"] = $render["%ckeditor_path%"] . 'plugins/'; 461 $render["%plugin_dir_extra%"] = ckeditor_plugins_path(TRUE) . '/'; 462 463 foreach ((array) $plugins as $i => $plugin) { 464 $plugins[$i]['path'] = str_replace(array_keys($render), array_values($render), $plugin['path']); 465 } 466 467 return $plugins; 468 } 469 470 471 function ckeditor_user_get_setting($user, $profile, $setting) { 472 $default = array( 473 'default' => 't', 474 'show_toggle' => 't', 475 'popup' => 'f', 476 'skin' => 'kama', 477 'toolbar' => 'default', 478 'expand' => 't', 479 'width' => '100%', 480 'lang' => 'en', 481 'auto_lang' => 't', 482 ); 483 484 if ($profile->settings['allow_user_conf']) { 485 $status = isset($user->{'ckeditor_'. $setting}) ? $user->{'ckeditor_'. $setting} : (isset($profile->settings[$setting]) ? $profile->settings[$setting] : $default[$setting]); 486 } 487 else { 488 $status = isset($profile->settings[$setting]) ? $profile->settings[$setting] : $default[$setting]; 489 } 490 491 return $status; 492 } 493 494 function ckeditor_user_get_profile($user, $element_id = 'edit-body', $url = NULL) { 495 $rids = array(); 496 if (is_null($url)) { 497 $url = $_GET['q']; 498 } 499 // Since ckeditor_profile_load() makes a db hit, only call it when we're pretty sure 500 // we're gonna render ckeditor. 501 $sorted_roles = ckeditor_sorted_roles(); 502 foreach (array_keys($sorted_roles) as $rid) { 503 if (isset($user->roles[$rid])) { 504 $rids[] = $rid; 505 } 506 } 507 508 if ($user->uid == 1 && !sizeof($rids)) { 509 $r = db_fetch_object(db_query_range("SELECT r.rid FROM {ckeditor_role} r ORDER BY r.rid DESC", 1)); 510 $rids[] = $r->rid; 511 } 512 513 $profile_names = array(); 514 if (sizeof($rids)) { 515 $result = db_query("SELECT r.rid, s.name FROM {ckeditor_settings} s INNER JOIN {ckeditor_role} r ON r.name = s.name WHERE r.rid IN (". implode(",", $rids) .")"); 516 while (($row = db_fetch_array($result))) { 517 if (!isset($profile_names[$row['rid']])) { 518 $profile_names[$row['rid']] = array(); 519 } 520 array_push($profile_names[$row['rid']], $row['name']); 521 } 522 } 523 524 foreach ($rids as $rid) { 525 if (!empty($profile_names[$rid])) { 526 foreach ($profile_names[$rid] as $profile_name) { 527 $profile = ckeditor_profile_load($profile_name); 528 529 $conf = $profile->settings; 530 $enabled = ckeditor_is_enabled(empty($conf['excl_mode']) ? '0' : $conf['excl_mode'], empty($conf['excl_regex']) ? '' : $conf['excl_regex'], $element_id, $url); 531 532 if ($enabled) { 533 return $profile; 534 } 535 } 536 } 537 } 538 539 return FALSE; 540 } 541 542 /** 543 * sort roles according to precedence settings. previously sorted roles are followed by latest added roles. 544 */ 545 function ckeditor_sorted_roles($clear = FALSE) { 546 static $order; 547 if (isset($order) && $clear !== TRUE) { 548 return $order; 549 } 550 $order = array(); 551 $roles = user_roles(0, 'access ckeditor'); 552 553 $result = db_query("SELECT settings FROM {ckeditor_settings} WHERE name='CKEditor Global Profile'"); 554 $data = db_fetch_object($result); 555 if (!empty($data->settings)) { 556 $settings = unserialize($data->settings); 557 if (isset($settings['rank']) && !empty($settings['rank'])) 558 foreach ($settings['rank'] as $rid) { 559 if (isset($roles[$rid])) { 560 $order[$rid] = $roles[$rid]; 561 unset($roles[$rid]); 562 } 563 } 564 } 565 krsort($roles);//sort the remaining unsorted roles by id, descending. 566 $order += $roles; 567 return $order; 568 } 569 570 /** 571 * @param int $excl_mode 1/include, exclude otherwise 572 * @param string $excl_regex paths (drupal paths with ids attached) 573 * @param string $element_id current ID 574 * @param string $get_q current path 575 * 576 * @return boolean 577 * returns true if CKEditor is enabled 578 */ 579 function ckeditor_is_enabled($excl_mode, $excl_regex, $element_id, $get_q) { 580 global $theme; 581 582 $front = variable_get('site_frontpage', 'node'); 583 $excl_regex = str_replace('<front>', $front, $excl_regex); 584 $nodetype = ckeditor_get_nodetype($get_q); 585 $element_id = str_replace('.', '\.', $element_id); 586 587 $match = !empty($excl_regex) && preg_match($excl_regex, $theme . ':' . $nodetype .'@'. $get_q .'.'. $element_id); 588 589 return ($excl_mode == '0' xor $match); 590 } 591 592 function _ckeditor_script_path() { 593 $jspath = FALSE; 594 $module_path=drupal_get_path('module', 'ckeditor'); 595 596 if (file_exists($module_path . '/ckeditor/ckeditor.js')) { 597 $jspath='%m/ckeditor'; 598 } 599 elseif (file_exists($module_path . '/ckeditor/ckeditor/ckeditor.js')) { 600 $jspath='%m/ckeditor/ckeditor'; 601 } 602 elseif (file_exists('sites/all/libraries/ckeditor/ckeditor.js')) { 603 $jspath='%b/sites/all/libraries/ckeditor'; 604 } 605 return $jspath; 606 } 607 608 /** 609 * Determines whether the CKEditor sources are present 610 * 611 * It checks if ckeditor.js is present. 612 * 613 * This function is used by ckeditor_requirements() 614 * 615 * @return boolean True if CKEditor is installed 616 */ 617 function _ckeditor_requirements_isinstalled() { 618 $editor_path = ckeditor_path(TRUE); 619 $jspath = $editor_path .'/ckeditor.js'; 620 $jsp=file_exists($jspath); 621 if (!$jsp && ($editor_path = _ckeditor_script_path())) { 622 $result = db_query("SELECT name, settings FROM {ckeditor_settings} WHERE name = 'CKEditor Global Profile'"); 623 if ($rs=db_fetch_array($result)) { 624 $rs['settings']=unserialize($rs['settings']); 625 $rs['settings']['ckeditor_path']=$editor_path; 626 $rs['settings']=serialize($rs['settings']); 627 db_query("UPDATE {ckeditor_settings} SET settings='%s' WHERE name = 'CKEditor Global Profile'", $rs['settings']); 628 $jsp=TRUE; 629 ckeditor_path(TRUE, TRUE); 630 } 631 } 632 return $jsp; 633 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |