[ Index ] |
PHP Cross Reference of phpwcms V1.5.0 _r431 (28.01.12) |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved. 4 * For licensing, see LICENSE.html or http://ckeditor.com/license 5 * 6 * Optimized for integration into phpwcms. 7 */ 8 9 /** 10 * \brief CKEditor class that can be used to create editor 11 * instances in PHP pages on server side. 12 * @see http://ckeditor.com 13 * 14 * Sample usage: 15 * @code 16 * $CKEditor = new CKEditor(); 17 * $CKEditor->editor("editor1", "<p>Initial value.</p>"); 18 * @endcode 19 */ 20 21 if (IS_PHP5) { 22 23 class CKEditor 24 { 25 /** 26 * The version of %CKEditor. 27 */ 28 const version = '3.6.2'; 29 /** 30 * A constant string unique for each release of %CKEditor. 31 */ 32 const timestamp = 'B8DJ5M3'; 33 34 /** 35 * URL to the %CKEditor installation directory (absolute or relative to document root). 36 * If not set, CKEditor will try to guess it's path. 37 * 38 * Example usage: 39 * @code 40 * $CKEditor->basePath = '/ckeditor/'; 41 * @endcode 42 */ 43 public $basePath; 44 /** 45 * An array that holds the global %CKEditor configuration. 46 * For the list of available options, see http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html 47 * 48 * Example usage: 49 * @code 50 * $CKEditor->config['height'] = 400; 51 * // Use @@ at the beggining of a string to ouput it without surrounding quotes. 52 * $CKEditor->config['width'] = '@@screen.width * 0.8'; 53 * @endcode 54 */ 55 public $config = array(); 56 /** 57 * A boolean variable indicating whether CKEditor has been initialized. 58 * Set it to true only if you have already included 59 * <script> tag loading ckeditor.js in your website. 60 */ 61 public $initialized = false; 62 /** 63 * Boolean variable indicating whether created code should be printed out or returned by a function. 64 * 65 * Example 1: get the code creating %CKEditor instance and print it on a page with the "echo" function. 66 * @code 67 * $CKEditor = new CKEditor(); 68 * $CKEditor->returnOutput = true; 69 * $code = $CKEditor->editor("editor1", "<p>Initial value.</p>"); 70 * echo "<p>Editor 1:</p>"; 71 * echo $code; 72 * @endcode 73 */ 74 public $returnOutput = false; 75 /** 76 * An array with textarea attributes. 77 * 78 * When %CKEditor is created with the editor() method, a HTML <textarea> element is created, 79 * it will be displayed to anyone with JavaScript disabled or with incompatible browser. 80 */ 81 public $textareaAttributes = array( "rows" => 8, "cols" => 60 ); 82 /** 83 * A string indicating the creation date of %CKEditor. 84 * Do not change it unless you want to force browsers to not use previously cached version of %CKEditor. 85 */ 86 public $timestamp = "B8DJ5M3"; 87 /** 88 * An array that holds event listeners. 89 */ 90 private $events = array(); 91 /** 92 * An array that holds global event listeners. 93 */ 94 private $globalEvents = array(); 95 96 /** 97 * Main Constructor. 98 * 99 * @param $basePath (string) URL to the %CKEditor installation directory (optional). 100 */ 101 function __construct($basePath = null) { 102 if (!empty($basePath)) { 103 $this->basePath = $basePath; 104 } 105 } 106 107 /** 108 * Creates a %CKEditor instance. 109 * In incompatible browsers %CKEditor will downgrade to plain HTML <textarea> element. 110 * 111 * @param $name (string) Name of the %CKEditor instance (this will be also the "name" attribute of textarea element). 112 * @param $value (string) Initial value (optional). 113 * @param $config (array) The specific configurations to apply to this editor instance (optional). 114 * @param $events (array) Event listeners for this editor instance (optional). 115 * 116 * Example usage: 117 * @code 118 * $CKEditor = new CKEditor(); 119 * $CKEditor->editor("field1", "<p>Initial value.</p>"); 120 * @endcode 121 * 122 * Advanced example: 123 * @code 124 * $CKEditor = new CKEditor(); 125 * $config = array(); 126 * $config['toolbar'] = array( 127 * array( 'Source', '-', 'Bold', 'Italic', 'Underline', 'Strike' ), 128 * array( 'Image', 'Link', 'Unlink', 'Anchor' ) 129 * ); 130 * $events['instanceReady'] = 'function (ev) { 131 * alert("Loaded: " + ev.editor.name); 132 * }'; 133 * $CKEditor->editor("field1", "<p>Initial value.</p>", $config, $events); 134 * @endcode 135 */ 136 public function editor($name, $value = "", $config = array(), $events = array()) 137 { 138 $attr = ""; 139 foreach ($this->textareaAttributes as $key => $val) { 140 $attr.= " " . $key . '="' . str_replace('"', '"', $val) . '"'; 141 } 142 $out = "<textarea name=\"" . $name . "\"" . $attr . ">" . htmlspecialchars($value) . "</textarea>\n"; 143 if (!$this->initialized) { 144 $out .= $this->init(); 145 } 146 147 $_config = $this->configSettings($config, $events); 148 149 $js = $this->returnGlobalEvents(); 150 if (!empty($_config)) 151 $js .= "CKEDITOR.replace('".$name."', ".$this->jsEncode($_config).");"; 152 else 153 $js .= "CKEDITOR.replace('".$name."');"; 154 155 $out .= $this->script($js); 156 157 if (!$this->returnOutput) { 158 print $out; 159 $out = ""; 160 } 161 162 return $out; 163 } 164 165 /** 166 * Replaces a <textarea> with a %CKEditor instance. 167 * 168 * @param $id (string) The id or name of textarea element. 169 * @param $config (array) The specific configurations to apply to this editor instance (optional). 170 * @param $events (array) Event listeners for this editor instance (optional). 171 * 172 * Example 1: adding %CKEditor to <textarea name="article"></textarea> element: 173 * @code 174 * $CKEditor = new CKEditor(); 175 * $CKEditor->replace("article"); 176 * @endcode 177 */ 178 public function replace($id, $config = array(), $events = array()) 179 { 180 $out = ""; 181 if (!$this->initialized) { 182 $out .= $this->init(); 183 } 184 185 $_config = $this->configSettings($config, $events); 186 187 $js = $this->returnGlobalEvents(); 188 if (!empty($_config)) { 189 $js .= "CKEDITOR.replace('".$id."', ".$this->jsEncode($_config).");"; 190 } 191 else { 192 $js .= "CKEDITOR.replace('".$id."');"; 193 } 194 $out .= $this->script($js); 195 196 if (!$this->returnOutput) { 197 print $out; 198 $out = ""; 199 } 200 201 return $out; 202 } 203 204 /** 205 * Replace all <textarea> elements available in the document with editor instances. 206 * 207 * @param $className (string) If set, replace all textareas with class className in the page. 208 * 209 * Example 1: replace all <textarea> elements in the page. 210 * @code 211 * $CKEditor = new CKEditor(); 212 * $CKEditor->replaceAll(); 213 * @endcode 214 * 215 * Example 2: replace all <textarea class="myClassName"> elements in the page. 216 * @code 217 * $CKEditor = new CKEditor(); 218 * $CKEditor->replaceAll( 'myClassName' ); 219 * @endcode 220 */ 221 public function replaceAll($className = null) 222 { 223 $out = ""; 224 if (!$this->initialized) { 225 $out .= $this->init(); 226 } 227 228 $_config = $this->configSettings(); 229 230 $js = $this->returnGlobalEvents(); 231 if (empty($_config)) { 232 if (empty($className)) { 233 $js .= "CKEDITOR.replaceAll();"; 234 } 235 else { 236 $js .= "CKEDITOR.replaceAll('".$className."');"; 237 } 238 } 239 else { 240 $classDetection = ""; 241 $js .= "CKEDITOR.replaceAll( function(textarea, config) {\n"; 242 if (!empty($className)) { 243 $js .= " var classRegex = new RegExp('(?:^| )' + '". $className ."' + '(?:$| )');\n"; 244 $js .= " if (!classRegex.test(textarea.className))\n"; 245 $js .= " return false;\n"; 246 } 247 $js .= " CKEDITOR.tools.extend(config, ". $this->jsEncode($_config) .", true);"; 248 $js .= "} );"; 249 250 } 251 252 $out .= $this->script($js); 253 254 if (!$this->returnOutput) { 255 print $out; 256 $out = ""; 257 } 258 259 return $out; 260 } 261 262 /** 263 * Adds event listener. 264 * Events are fired by %CKEditor in various situations. 265 * 266 * @param $event (string) Event name. 267 * @param $javascriptCode (string) Javascript anonymous function or function name. 268 * 269 * Example usage: 270 * @code 271 * $CKEditor->addEventHandler('instanceReady', 'function (ev) { 272 * alert("Loaded: " + ev.editor.name); 273 * }'); 274 * @endcode 275 */ 276 public function addEventHandler($event, $javascriptCode) 277 { 278 if (!isset($this->events[$event])) { 279 $this->events[$event] = array(); 280 } 281 // Avoid duplicates. 282 if (!in_array($javascriptCode, $this->events[$event])) { 283 $this->events[$event][] = $javascriptCode; 284 } 285 } 286 287 /** 288 * Clear registered event handlers. 289 * Note: this function will have no effect on already created editor instances. 290 * 291 * @param $event (string) Event name, if not set all event handlers will be removed (optional). 292 */ 293 public function clearEventHandlers($event = null) 294 { 295 if (!empty($event)) { 296 $this->events[$event] = array(); 297 } 298 else { 299 $this->events = array(); 300 } 301 } 302 303 /** 304 * Adds global event listener. 305 * 306 * @param $event (string) Event name. 307 * @param $javascriptCode (string) Javascript anonymous function or function name. 308 * 309 * Example usage: 310 * @code 311 * $CKEditor->addGlobalEventHandler('dialogDefinition', 'function (ev) { 312 * alert("Loading dialog: " + ev.data.name); 313 * }'); 314 * @endcode 315 */ 316 public function addGlobalEventHandler($event, $javascriptCode) 317 { 318 if (!isset($this->globalEvents[$event])) { 319 $this->globalEvents[$event] = array(); 320 } 321 // Avoid duplicates. 322 if (!in_array($javascriptCode, $this->globalEvents[$event])) { 323 $this->globalEvents[$event][] = $javascriptCode; 324 } 325 } 326 327 /** 328 * Clear registered global event handlers. 329 * Note: this function will have no effect if the event handler has been already printed/returned. 330 * 331 * @param $event (string) Event name, if not set all event handlers will be removed (optional). 332 */ 333 public function clearGlobalEventHandlers($event = null) 334 { 335 if (!empty($event)) { 336 $this->globalEvents[$event] = array(); 337 } 338 else { 339 $this->globalEvents = array(); 340 } 341 } 342 343 /** 344 * Prints javascript code. 345 * 346 * @param string $js 347 */ 348 private function script($js) 349 { 350 $out = "<script type=\"text/javascript\">"; 351 $out .= "//<![CDATA[\n"; 352 $out .= $js; 353 $out .= "\n//]]>"; 354 $out .= "</script>\n"; 355 356 return $out; 357 } 358 359 /** 360 * Returns the configuration array (global and instance specific settings are merged into one array). 361 * 362 * @param $config (array) The specific configurations to apply to editor instance. 363 * @param $events (array) Event listeners for editor instance. 364 */ 365 private function configSettings($config = array(), $events = array()) 366 { 367 $_config = $this->config; 368 $_events = $this->events; 369 370 if (is_array($config) && !empty($config)) { 371 $_config = array_merge($_config, $config); 372 } 373 374 if (is_array($events) && !empty($events)) { 375 foreach ($events as $eventName => $code) { 376 if (!isset($_events[$eventName])) { 377 $_events[$eventName] = array(); 378 } 379 if (!in_array($code, $_events[$eventName])) { 380 $_events[$eventName][] = $code; 381 } 382 } 383 } 384 385 if (!empty($_events)) { 386 foreach($_events as $eventName => $handlers) { 387 if (empty($handlers)) { 388 continue; 389 } 390 else if (count($handlers) == 1) { 391 $_config['on'][$eventName] = '@@'.$handlers[0]; 392 } 393 else { 394 $_config['on'][$eventName] = '@@function (ev){'; 395 foreach ($handlers as $handler => $code) { 396 $_config['on'][$eventName] .= '('.$code.')(ev);'; 397 } 398 $_config['on'][$eventName] .= '}'; 399 } 400 } 401 } 402 403 return $_config; 404 } 405 406 /** 407 * Return global event handlers. 408 */ 409 private function returnGlobalEvents() 410 { 411 static $returnedEvents; 412 $out = ""; 413 414 if (!isset($returnedEvents)) { 415 $returnedEvents = array(); 416 } 417 418 if (!empty($this->globalEvents)) { 419 foreach ($this->globalEvents as $eventName => $handlers) { 420 foreach ($handlers as $handler => $code) { 421 if (!isset($returnedEvents[$eventName])) { 422 $returnedEvents[$eventName] = array(); 423 } 424 // Return only new events 425 if (!in_array($code, $returnedEvents[$eventName])) { 426 $out .= ($code ? "\n" : "") . "CKEDITOR.on('". $eventName ."', $code);"; 427 $returnedEvents[$eventName][] = $code; 428 } 429 } 430 } 431 } 432 433 return $out; 434 } 435 436 /** 437 * Initializes CKEditor (executed only once). 438 */ 439 private function init() 440 { 441 static $initComplete; 442 $out = ""; 443 444 if (!empty($initComplete)) { 445 return ""; 446 } 447 448 if ($this->initialized) { 449 $initComplete = true; 450 return ""; 451 } 452 453 $args = ""; 454 $ckeditorPath = $this->ckeditorPath(); 455 456 if (!empty($this->timestamp) && $this->timestamp != "%"."TIMESTAMP%") { 457 $args = '?t=' . $this->timestamp; 458 } 459 460 // Skip relative paths... 461 if (strpos($ckeditorPath, '..') !== 0) { 462 $out .= $this->script("window.CKEDITOR_BASEPATH='". $ckeditorPath ."';"); 463 } 464 465 $out .= "<script type=\"text/javascript\" src=\"" . $ckeditorPath . 'ckeditor.js' . $args . "\"></script>\n"; 466 467 $extraCode = ""; 468 if ($this->timestamp != self::timestamp) { 469 $extraCode .= ($extraCode ? "\n" : "") . "CKEDITOR.timestamp = '". $this->timestamp ."';"; 470 } 471 if ($extraCode) { 472 $out .= $this->script($extraCode); 473 } 474 475 $initComplete = $this->initialized = true; 476 477 return $out; 478 } 479 480 /** 481 * Return path to ckeditor.js. 482 */ 483 private function ckeditorPath() 484 { 485 if (!empty($this->basePath)) { 486 return $this->basePath; 487 } 488 489 /** 490 * The absolute pathname of the currently executing script. 491 * Note: If a script is executed with the CLI, as a relative path, such as file.php or ../file.php, 492 * $_SERVER['SCRIPT_FILENAME'] will contain the relative path specified by the user. 493 */ 494 if (isset($_SERVER['SCRIPT_FILENAME'])) { 495 $realPath = dirname($_SERVER['SCRIPT_FILENAME']); 496 } 497 else { 498 /** 499 * realpath - Returns canonicalized absolute pathname 500 */ 501 $realPath = realpath( './' ) ; 502 } 503 504 /** 505 * The filename of the currently executing script, relative to the document root. 506 * For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar 507 * would be /test.php/foo.bar. 508 */ 509 $selfPath = dirname($_SERVER['PHP_SELF']); 510 $file = str_replace("\\", "/", __FILE__); 511 512 if (!$selfPath || !$realPath || !$file) { 513 return "/ckeditor/"; 514 } 515 516 $documentRoot = substr($realPath, 0, strlen($realPath) - strlen($selfPath)); 517 $fileUrl = substr($file, strlen($documentRoot)); 518 $ckeditorUrl = str_replace("ckeditor_php5.php", "", $fileUrl); 519 520 return $ckeditorUrl; 521 } 522 523 /** 524 * This little function provides a basic JSON support. 525 * 526 * @param mixed $val 527 * @return string 528 */ 529 private function jsEncode($val) 530 { 531 if (is_null($val)) { 532 return 'null'; 533 } 534 if (is_bool($val)) { 535 return $val ? 'true' : 'false'; 536 } 537 if (is_int($val)) { 538 return $val; 539 } 540 if (is_float($val)) { 541 return str_replace(',', '.', $val); 542 } 543 if (is_array($val) || is_object($val)) { 544 if (is_array($val) && (array_keys($val) === range(0,count($val)-1))) { 545 return '[' . implode(',', array_map(array($this, 'jsEncode'), $val)) . ']'; 546 } 547 $temp = array(); 548 foreach ($val as $k => $v){ 549 $temp[] = $this->jsEncode("{$k}") . ':' . $this->jsEncode($v); 550 } 551 return '{' . implode(',', $temp) . '}'; 552 } 553 // String otherwise 554 if (strpos($val, '@@') === 0) 555 return substr($val, 2); 556 if (strtoupper(substr($val, 0, 9)) == 'CKEDITOR.') 557 return $val; 558 559 return '"' . str_replace(array("\\", "/", "\n", "\t", "\r", "\x08", "\x0c", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'), $val) . '"'; 560 } 561 } 562 563 } else { 564 565 class CKEditor 566 { 567 /** 568 * The version of %CKEditor. 569 * \private 570 */ 571 var $version = '3.6.2'; 572 /** 573 * A constant string unique for each release of %CKEditor. 574 * \private 575 */ 576 var $_timestamp = 'B8DJ5M3'; 577 578 /** 579 * URL to the %CKEditor installation directory (absolute or relative to document root). 580 * If not set, CKEditor will try to guess it's path. 581 * 582 * Example usage: 583 * @code 584 * $CKEditor->basePath = '/ckeditor/'; 585 * @endcode 586 */ 587 var $basePath; 588 /** 589 * An array that holds the global %CKEditor configuration. 590 * For the list of available options, see http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html 591 * 592 * Example usage: 593 * @code 594 * $CKEditor->config['height'] = 400; 595 * // Use @@ at the beggining of a string to ouput it without surrounding quotes. 596 * $CKEditor->config['width'] = '@@screen.width * 0.8'; 597 * @endcode 598 */ 599 var $config = array(); 600 /** 601 * A boolean variable indicating whether CKEditor has been initialized. 602 * Set it to true only if you have already included 603 * <script> tag loading ckeditor.js in your website. 604 */ 605 var $initialized = false; 606 /** 607 * Boolean variable indicating whether created code should be printed out or returned by a function. 608 * 609 * Example 1: get the code creating %CKEditor instance and print it on a page with the "echo" function. 610 * @code 611 * $CKEditor = new CKEditor(); 612 * $CKEditor->returnOutput = true; 613 * $code = $CKEditor->editor("editor1", "<p>Initial value.</p>"); 614 * echo "<p>Editor 1:</p>"; 615 * echo $code; 616 * @endcode 617 */ 618 var $returnOutput = false; 619 /** 620 * An array with textarea attributes. 621 * 622 * When %CKEditor is created with the editor() method, a HTML <textarea> element is created, 623 * it will be displayed to anyone with JavaScript disabled or with incompatible browser. 624 */ 625 var $textareaAttributes = array( "rows" => 8, "cols" => 60 ); 626 /** 627 * A string indicating the creation date of %CKEditor. 628 * Do not change it unless you want to force browsers to not use previously cached version of %CKEditor. 629 */ 630 var $timestamp = "B8DJ5M3"; 631 /** 632 * An array that holds event listeners. 633 * \private 634 */ 635 var $_events = array(); 636 /** 637 * An array that holds global event listeners. 638 * \private 639 */ 640 var $_globalEvents = array(); 641 642 /** 643 * Main Constructor. 644 * 645 * @param $basePath (string) URL to the %CKEditor installation directory (optional). 646 */ 647 function CKEditor($basePath = null) { 648 if (!empty($basePath)) { 649 $this->basePath = $basePath; 650 } 651 } 652 653 /** 654 * Creates a %CKEditor instance. 655 * In incompatible browsers %CKEditor will downgrade to plain HTML <textarea> element. 656 * 657 * @param $name (string) Name of the %CKEditor instance (this will be also the "name" attribute of textarea element). 658 * @param $value (string) Initial value (optional). 659 * @param $config (array) The specific configurations to apply to this editor instance (optional). 660 * @param $events (array) Event listeners for this editor instance (optional). 661 * 662 * Example usage: 663 * @code 664 * $CKEditor = new CKEditor(); 665 * $CKEditor->editor("field1", "<p>Initial value.</p>"); 666 * @endcode 667 * 668 * Advanced example: 669 * @code 670 * $CKEditor = new CKEditor(); 671 * $config = array(); 672 * $config['toolbar'] = array( 673 * array( 'Source', '-', 'Bold', 'Italic', 'Underline', 'Strike' ), 674 * array( 'Image', 'Link', 'Unlink', 'Anchor' ) 675 * ); 676 * $events['instanceReady'] = 'function (ev) { 677 * alert("Loaded: " + ev.editor.name); 678 * }'; 679 * $CKEditor->editor("field1", "<p>Initial value.</p>", $config, $events); 680 * @endcode 681 */ 682 function editor($name, $value = "", $config = array(), $events = array()) 683 { 684 $attr = ""; 685 foreach ($this->textareaAttributes as $key => $val) { 686 $attr.= " " . $key . '="' . str_replace('"', '"', $val) . '"'; 687 } 688 $out = "<textarea name=\"" . $name . "\"" . $attr . ">" . htmlspecialchars($value) . "</textarea>\n"; 689 if (!$this->initialized) { 690 $out .= $this->init(); 691 } 692 693 $_config = $this->configSettings($config, $events); 694 695 $js = $this->returnGlobalEvents(); 696 if (!empty($_config)) 697 $js .= "CKEDITOR.replace('".$name."', ".$this->jsEncode($_config).");"; 698 else 699 $js .= "CKEDITOR.replace('".$name."');"; 700 701 $out .= $this->script($js); 702 703 if (!$this->returnOutput) { 704 print $out; 705 $out = ""; 706 } 707 708 return $out; 709 } 710 711 /** 712 * Replaces a <textarea> with a %CKEditor instance. 713 * 714 * @param $id (string) The id or name of textarea element. 715 * @param $config (array) The specific configurations to apply to this editor instance (optional). 716 * @param $events (array) Event listeners for this editor instance (optional). 717 * 718 * Example 1: adding %CKEditor to <textarea name="article"></textarea> element: 719 * @code 720 * $CKEditor = new CKEditor(); 721 * $CKEditor->replace("article"); 722 * @endcode 723 */ 724 function replace($id, $config = array(), $events = array()) 725 { 726 $out = ""; 727 if (!$this->initialized) { 728 $out .= $this->init(); 729 } 730 731 $_config = $this->configSettings($config, $events); 732 733 $js = $this->returnGlobalEvents(); 734 if (!empty($_config)) { 735 $js .= "CKEDITOR.replace('".$id."', ".$this->jsEncode($_config).");"; 736 } 737 else { 738 $js .= "CKEDITOR.replace('".$id."');"; 739 } 740 $out .= $this->script($js); 741 742 if (!$this->returnOutput) { 743 print $out; 744 $out = ""; 745 } 746 747 return $out; 748 } 749 750 /** 751 * Replace all <textarea> elements available in the document with editor instances. 752 * 753 * @param $className (string) If set, replace all textareas with class className in the page. 754 * 755 * Example 1: replace all <textarea> elements in the page. 756 * @code 757 * $CKEditor = new CKEditor(); 758 * $CKEditor->replaceAll(); 759 * @endcode 760 * 761 * Example 2: replace all <textarea class="myClassName"> elements in the page. 762 * @code 763 * $CKEditor = new CKEditor(); 764 * $CKEditor->replaceAll( 'myClassName' ); 765 * @endcode 766 */ 767 function replaceAll($className = null) 768 { 769 $out = ""; 770 if (!$this->initialized) { 771 $out .= $this->init(); 772 } 773 774 $_config = $this->configSettings(); 775 776 $js = $this->returnGlobalEvents(); 777 if (empty($_config)) { 778 if (empty($className)) { 779 $js .= "CKEDITOR.replaceAll();"; 780 } 781 else { 782 $js .= "CKEDITOR.replaceAll('".$className."');"; 783 } 784 } 785 else { 786 $classDetection = ""; 787 $js .= "CKEDITOR.replaceAll( function(textarea, config) {\n"; 788 if (!empty($className)) { 789 $js .= " var classRegex = new RegExp('(?:^| )' + '". $className ."' + '(?:$| )');\n"; 790 $js .= " if (!classRegex.test(textarea.className))\n"; 791 $js .= " return false;\n"; 792 } 793 $js .= " CKEDITOR.tools.extend(config, ". $this->jsEncode($_config) .", true);"; 794 $js .= "} );"; 795 796 } 797 798 $out .= $this->script($js); 799 800 if (!$this->returnOutput) { 801 print $out; 802 $out = ""; 803 } 804 805 return $out; 806 } 807 808 /** 809 * Adds event listener. 810 * Events are fired by %CKEditor in various situations. 811 * 812 * @param $event (string) Event name. 813 * @param $javascriptCode (string) Javascript anonymous function or function name. 814 * 815 * Example usage: 816 * @code 817 * $CKEditor->addEventHandler('instanceReady', 'function (ev) { 818 * alert("Loaded: " + ev.editor.name); 819 * }'); 820 * @endcode 821 */ 822 function addEventHandler($event, $javascriptCode) 823 { 824 if (!isset($this->_events[$event])) { 825 $this->_events[$event] = array(); 826 } 827 // Avoid duplicates. 828 if (!in_array($javascriptCode, $this->_events[$event])) { 829 $this->_events[$event][] = $javascriptCode; 830 } 831 } 832 833 /** 834 * Clear registered event handlers. 835 * Note: this function will have no effect on already created editor instances. 836 * 837 * @param $event (string) Event name, if not set all event handlers will be removed (optional). 838 */ 839 function clearEventHandlers($event = null) 840 { 841 if (!empty($event)) { 842 $this->_events[$event] = array(); 843 } 844 else { 845 $this->_events = array(); 846 } 847 } 848 849 /** 850 * Adds global event listener. 851 * 852 * @param $event (string) Event name. 853 * @param $javascriptCode (string) Javascript anonymous function or function name. 854 * 855 * Example usage: 856 * @code 857 * $CKEditor->addGlobalEventHandler('dialogDefinition', 'function (ev) { 858 * alert("Loading dialog: " + ev.data.name); 859 * }'); 860 * @endcode 861 */ 862 function addGlobalEventHandler($event, $javascriptCode) 863 { 864 if (!isset($this->_globalEvents[$event])) { 865 $this->_globalEvents[$event] = array(); 866 } 867 // Avoid duplicates. 868 if (!in_array($javascriptCode, $this->_globalEvents[$event])) { 869 $this->_globalEvents[$event][] = $javascriptCode; 870 } 871 } 872 873 /** 874 * Clear registered global event handlers. 875 * Note: this function will have no effect if the event handler has been already printed/returned. 876 * 877 * @param $event (string) Event name, if not set all event handlers will be removed (optional). 878 */ 879 function clearGlobalEventHandlers($event = null) 880 { 881 if (!empty($event)) { 882 $this->_globalEvents[$event] = array(); 883 } 884 else { 885 $this->_globalEvents = array(); 886 } 887 } 888 889 /** 890 * Prints javascript code. 891 * \private 892 * 893 * @param string $js 894 */ 895 function script($js) 896 { 897 $out = "<script type=\"text/javascript\">"; 898 $out .= "//<![CDATA[\n"; 899 $out .= $js; 900 $out .= "\n//]]>"; 901 $out .= "</script>\n"; 902 903 return $out; 904 } 905 906 /** 907 * Returns the configuration array (global and instance specific settings are merged into one array). 908 * \private 909 * 910 * @param $config (array) The specific configurations to apply to editor instance. 911 * @param $events (array) Event listeners for editor instance. 912 */ 913 function configSettings($config = array(), $events = array()) 914 { 915 $_config = $this->config; 916 $_events = $this->_events; 917 918 if (is_array($config) && !empty($config)) { 919 $_config = array_merge($_config, $config); 920 } 921 922 if (is_array($events) && !empty($events)) { 923 foreach ($events as $eventName => $code) { 924 if (!isset($_events[$eventName])) { 925 $_events[$eventName] = array(); 926 } 927 if (!in_array($code, $_events[$eventName])) { 928 $_events[$eventName][] = $code; 929 } 930 } 931 } 932 933 if (!empty($_events)) { 934 foreach($_events as $eventName => $handlers) { 935 if (empty($handlers)) { 936 continue; 937 } 938 else if (count($handlers) == 1) { 939 $_config['on'][$eventName] = '@@'.$handlers[0]; 940 } 941 else { 942 $_config['on'][$eventName] = '@@function (ev){'; 943 foreach ($handlers as $handler => $code) { 944 $_config['on'][$eventName] .= '('.$code.')(ev);'; 945 } 946 $_config['on'][$eventName] .= '}'; 947 } 948 } 949 } 950 951 return $_config; 952 } 953 954 /** 955 * Return global event handlers. 956 * \private 957 */ 958 function returnGlobalEvents() 959 { 960 static $returnedEvents; 961 $out = ""; 962 963 if (!isset($returnedEvents)) { 964 $returnedEvents = array(); 965 } 966 967 if (!empty($this->_globalEvents)) { 968 foreach ($this->_globalEvents as $eventName => $handlers) { 969 foreach ($handlers as $handler => $code) { 970 if (!isset($returnedEvents[$eventName])) { 971 $returnedEvents[$eventName] = array(); 972 } 973 // Return only new events 974 if (!in_array($code, $returnedEvents[$eventName])) { 975 $out .= ($code ? "\n" : "") . "CKEDITOR.on('". $eventName ."', $code);"; 976 $returnedEvents[$eventName][] = $code; 977 } 978 } 979 } 980 } 981 982 return $out; 983 } 984 985 /** 986 * Initializes CKEditor (executed only once). 987 * \private 988 */ 989 function init() 990 { 991 static $initComplete; 992 $out = ""; 993 994 if (!empty($initComplete)) { 995 return ""; 996 } 997 998 if ($this->initialized) { 999 $initComplete = true; 1000 return ""; 1001 } 1002 1003 $args = ""; 1004 $ckeditorPath = $this->ckeditorPath(); 1005 1006 if (!empty($this->timestamp) && $this->timestamp != "%"."TIMESTAMP%") { 1007 $args = '?t=' . $this->timestamp; 1008 } 1009 1010 // Skip relative paths... 1011 if (strpos($ckeditorPath, '..') !== 0) { 1012 $out .= $this->script("window.CKEDITOR_BASEPATH='". $ckeditorPath ."';"); 1013 } 1014 1015 $out .= "<script type=\"text/javascript\" src=\"" . $ckeditorPath . 'ckeditor.js' . $args . "\"></script>\n"; 1016 1017 $extraCode = ""; 1018 if ($this->timestamp != $this->_timestamp) { 1019 $extraCode .= ($extraCode ? "\n" : "") . "CKEDITOR.timestamp = '". $this->timestamp ."';"; 1020 } 1021 if ($extraCode) { 1022 $out .= $this->script($extraCode); 1023 } 1024 1025 $initComplete = $this->initialized = true; 1026 1027 return $out; 1028 } 1029 1030 /** 1031 * Return path to ckeditor.js. 1032 * \private 1033 */ 1034 function ckeditorPath() 1035 { 1036 if (!empty($this->basePath)) { 1037 return $this->basePath; 1038 } 1039 1040 /** 1041 * The absolute pathname of the currently executing script. 1042 * Note: If a script is executed with the CLI, as a relative path, such as file.php or ../file.php, 1043 * $_SERVER['SCRIPT_FILENAME'] will contain the relative path specified by the user. 1044 */ 1045 if (isset($_SERVER['SCRIPT_FILENAME'])) { 1046 $realPath = dirname($_SERVER['SCRIPT_FILENAME']); 1047 } 1048 else { 1049 /** 1050 * realpath - Returns canonicalized absolute pathname 1051 */ 1052 $realPath = realpath( './' ) ; 1053 } 1054 1055 /** 1056 * The filename of the currently executing script, relative to the document root. 1057 * For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar 1058 * would be /test.php/foo.bar. 1059 */ 1060 $selfPath = dirname($_SERVER['PHP_SELF']); 1061 $file = str_replace("\\", "/", __FILE__); 1062 1063 if (!$selfPath || !$realPath || !$file) { 1064 return "/ckeditor/"; 1065 } 1066 1067 $documentRoot = substr($realPath, 0, strlen($realPath) - strlen($selfPath)); 1068 $fileUrl = substr($file, strlen($documentRoot)); 1069 $ckeditorUrl = str_replace("ckeditor_php4.php", "", $fileUrl); 1070 1071 return $ckeditorUrl; 1072 } 1073 1074 /** 1075 * This little function provides a basic JSON support. 1076 * \private 1077 * 1078 * @param mixed $val 1079 * @return string 1080 */ 1081 function jsEncode($val) 1082 { 1083 if (is_null($val)) { 1084 return 'null'; 1085 } 1086 if (is_bool($val)) { 1087 return $val ? 'true' : 'false'; 1088 } 1089 if (is_int($val)) { 1090 return $val; 1091 } 1092 if (is_float($val)) { 1093 return str_replace(',', '.', $val); 1094 } 1095 if (is_array($val) || is_object($val)) { 1096 if (is_array($val) && (array_keys($val) === range(0,count($val)-1))) { 1097 return '[' . implode(',', array_map(array($this, 'jsEncode'), $val)) . ']'; 1098 } 1099 $temp = array(); 1100 foreach ($val as $k => $v){ 1101 $temp[] = $this->jsEncode("{$k}") . ':' . $this->jsEncode($v); 1102 } 1103 return '{' . implode(',', $temp) . '}'; 1104 } 1105 // String otherwise 1106 if (strpos($val, '@@') === 0) 1107 return substr($val, 2); 1108 if (strtoupper(substr($val, 0, 9)) == 'CKEDITOR.') 1109 return $val; 1110 1111 return '"' . str_replace(array("\\", "/", "\n", "\t", "\r", "\x08", "\x0c", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'), $val) . '"'; 1112 } 1113 } 1114 1115 } 1116
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Jan 29 16:31:14 2012 | Cross-referenced by PHPXref 0.7.1 |