3 * Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
4 * For licensing, see LICENSE.html or http://ckeditor.com/license
\r
8 * \brief CKEditor class that can be used to create editor
\r
9 * instances in PHP pages on server side.
\r
10 * @see http://ckeditor.com
\r
14 * $CKEditor = new CKEditor();
\r
15 * $CKEditor->editor("editor1", "<p>Initial value.</p>");
\r
21 * The version of %CKEditor.
\r
24 var $version = '3.4.3';
\r
26 * A constant string unique for each release of %CKEditor.
\r
29 var $_timestamp = 'ABH04T8';
\r
32 * URL to the %CKEditor installation directory (absolute or relative to document root).
\r
33 * If not set, CKEditor will try to guess it's path.
\r
37 * $CKEditor->basePath = '/ckeditor/';
\r
42 * An array that holds the global %CKEditor configuration.
\r
43 * For the list of available options, see http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html
\r
47 * $CKEditor->config['height'] = 400;
\r
48 * // Use @@ at the beggining of a string to ouput it without surrounding quotes.
\r
49 * $CKEditor->config['width'] = '@@screen.width * 0.8';
\r
52 var $config = array();
\r
54 * A boolean variable indicating whether CKEditor has been initialized.
\r
55 * Set it to true only if you have already included
\r
56 * <script> tag loading ckeditor.js in your website.
\r
58 var $initialized = false;
\r
60 * Boolean variable indicating whether created code should be printed out or returned by a function.
\r
62 * Example 1: get the code creating %CKEditor instance and print it on a page with the "echo" function.
\r
64 * $CKEditor = new CKEditor();
\r
65 * $CKEditor->returnOutput = true;
\r
66 * $code = $CKEditor->editor("editor1", "<p>Initial value.</p>");
\r
67 * echo "<p>Editor 1:</p>";
\r
71 var $returnOutput = false;
\r
73 * An array with textarea attributes.
\r
75 * When %CKEditor is created with the editor() method, a HTML <textarea> element is created,
\r
76 * it will be displayed to anyone with JavaScript disabled or with incompatible browser.
\r
78 var $textareaAttributes = array( "rows" => 8, "cols" => 60 );
\r
80 * A string indicating the creation date of %CKEditor.
\r
81 * Do not change it unless you want to force browsers to not use previously cached version of %CKEditor.
\r
83 var $timestamp = "ABH04T8";
\r
85 * An array that holds event listeners.
\r
88 var $_events = array();
\r
90 * An array that holds global event listeners.
\r
93 var $_globalEvents = array();
\r
98 * @param $basePath (string) URL to the %CKEditor installation directory (optional).
\r
100 function CKEditor($basePath = null) {
\r
101 if (!empty($basePath)) {
\r
102 $this->basePath = $basePath;
\r
107 * Creates a %CKEditor instance.
\r
108 * In incompatible browsers %CKEditor will downgrade to plain HTML <textarea> element.
\r
110 * @param $name (string) Name of the %CKEditor instance (this will be also the "name" attribute of textarea element).
\r
111 * @param $value (string) Initial value (optional).
\r
112 * @param $config (array) The specific configurations to apply to this editor instance (optional).
\r
113 * @param $events (array) Event listeners for this editor instance (optional).
\r
117 * $CKEditor = new CKEditor();
\r
118 * $CKEditor->editor("field1", "<p>Initial value.</p>");
\r
121 * Advanced example:
\r
123 * $CKEditor = new CKEditor();
\r
124 * $config = array();
\r
125 * $config['toolbar'] = array(
\r
126 * array( 'Source', '-', 'Bold', 'Italic', 'Underline', 'Strike' ),
\r
127 * array( 'Image', 'Link', 'Unlink', 'Anchor' )
\r
129 * $events['instanceReady'] = 'function (ev) {
\r
130 * alert("Loaded: " + ev.editor.name);
\r
132 * $CKEditor->editor("field1", "<p>Initial value.</p>", $config, $events);
\r
135 function editor($name, $value = "", $config = array(), $events = array())
\r
138 foreach ($this->textareaAttributes as $key => $val) {
\r
139 $attr.= " " . $key . '="' . str_replace('"', '"', $val) . '"';
\r
141 $out = "<textarea name=\"" . $name . "\"" . $attr . ">" . htmlspecialchars($value) . "</textarea>\n";
\r
142 if (!$this->initialized) {
\r
143 $out .= $this->init();
\r
146 $_config = $this->configSettings($config, $events);
\r
148 $js = $this->returnGlobalEvents();
\r
149 if (!empty($_config))
\r
150 $js .= "CKEDITOR.replace('".$name."', ".$this->jsEncode($_config).");";
\r
152 $js .= "CKEDITOR.replace('".$name."');";
\r
154 $out .= $this->script($js);
\r
156 if (!$this->returnOutput) {
\r
165 * Replaces a <textarea> with a %CKEditor instance.
\r
167 * @param $id (string) The id or name of textarea element.
\r
168 * @param $config (array) The specific configurations to apply to this editor instance (optional).
\r
169 * @param $events (array) Event listeners for this editor instance (optional).
\r
171 * Example 1: adding %CKEditor to <textarea name="article"></textarea> element:
\r
173 * $CKEditor = new CKEditor();
\r
174 * $CKEditor->replace("article");
\r
177 function replace($id, $config = array(), $events = array())
\r
180 if (!$this->initialized) {
\r
181 $out .= $this->init();
\r
184 $_config = $this->configSettings($config, $events);
\r
186 $js = $this->returnGlobalEvents();
\r
187 if (!empty($_config)) {
\r
188 $js .= "CKEDITOR.replace('".$id."', ".$this->jsEncode($_config).");";
\r
191 $js .= "CKEDITOR.replace('".$id."');";
\r
193 $out .= $this->script($js);
\r
195 if (!$this->returnOutput) {
\r
204 * Replace all <textarea> elements available in the document with editor instances.
\r
206 * @param $className (string) If set, replace all textareas with class className in the page.
\r
208 * Example 1: replace all <textarea> elements in the page.
\r
210 * $CKEditor = new CKEditor();
\r
211 * $CKEditor->replaceAll();
\r
214 * Example 2: replace all <textarea class="myClassName"> elements in the page.
\r
216 * $CKEditor = new CKEditor();
\r
217 * $CKEditor->replaceAll( 'myClassName' );
\r
220 function replaceAll($className = null)
\r
223 if (!$this->initialized) {
\r
224 $out .= $this->init();
\r
227 $_config = $this->configSettings();
\r
229 $js = $this->returnGlobalEvents();
\r
230 if (empty($_config)) {
\r
231 if (empty($className)) {
\r
232 $js .= "CKEDITOR.replaceAll();";
\r
235 $js .= "CKEDITOR.replaceAll('".$className."');";
\r
239 $classDetection = "";
\r
240 $js .= "CKEDITOR.replaceAll( function(textarea, config) {\n";
\r
241 if (!empty($className)) {
\r
242 $js .= " var classRegex = new RegExp('(?:^| )' + '". $className ."' + '(?:$| )');\n";
\r
243 $js .= " if (!classRegex.test(textarea.className))\n";
\r
244 $js .= " return false;\n";
\r
246 $js .= " CKEDITOR.tools.extend(config, ". $this->jsEncode($_config) .", true);";
\r
251 $out .= $this->script($js);
\r
253 if (!$this->returnOutput) {
\r
262 * Adds event listener.
\r
263 * Events are fired by %CKEditor in various situations.
\r
265 * @param $event (string) Event name.
\r
266 * @param $javascriptCode (string) Javascript anonymous function or function name.
\r
270 * $CKEditor->addEventHandler('instanceReady', 'function (ev) {
\r
271 * alert("Loaded: " + ev.editor.name);
\r
275 function addEventHandler($event, $javascriptCode)
\r
277 if (!isset($this->_events[$event])) {
\r
278 $this->_events[$event] = array();
\r
280 // Avoid duplicates.
\r
281 if (!in_array($javascriptCode, $this->_events[$event])) {
\r
282 $this->_events[$event][] = $javascriptCode;
\r
287 * Clear registered event handlers.
\r
288 * Note: this function will have no effect on already created editor instances.
\r
290 * @param $event (string) Event name, if not set all event handlers will be removed (optional).
\r
292 function clearEventHandlers($event = null)
\r
294 if (!empty($event)) {
\r
295 $this->_events[$event] = array();
\r
298 $this->_events = array();
\r
303 * Adds global event listener.
\r
305 * @param $event (string) Event name.
\r
306 * @param $javascriptCode (string) Javascript anonymous function or function name.
\r
310 * $CKEditor->addGlobalEventHandler('dialogDefinition', 'function (ev) {
\r
311 * alert("Loading dialog: " + ev.data.name);
\r
315 function addGlobalEventHandler($event, $javascriptCode)
\r
317 if (!isset($this->_globalEvents[$event])) {
\r
318 $this->_globalEvents[$event] = array();
\r
320 // Avoid duplicates.
\r
321 if (!in_array($javascriptCode, $this->_globalEvents[$event])) {
\r
322 $this->_globalEvents[$event][] = $javascriptCode;
\r
327 * Clear registered global event handlers.
\r
328 * Note: this function will have no effect if the event handler has been already printed/returned.
\r
330 * @param $event (string) Event name, if not set all event handlers will be removed (optional).
\r
332 function clearGlobalEventHandlers($event = null)
\r
334 if (!empty($event)) {
\r
335 $this->_globalEvents[$event] = array();
\r
338 $this->_globalEvents = array();
\r
343 * Prints javascript code.
\r
346 * @param string $js
\r
348 function script($js)
\r
350 $out = "<script type=\"text/javascript\">";
\r
351 $out .= "//<![CDATA[\n";
\r
354 $out .= "</script>\n";
\r
360 * Returns the configuration array (global and instance specific settings are merged into one array).
\r
363 * @param $config (array) The specific configurations to apply to editor instance.
\r
364 * @param $events (array) Event listeners for editor instance.
\r
366 function configSettings($config = array(), $events = array())
\r
368 $_config = $this->config;
\r
369 $_events = $this->_events;
\r
371 if (is_array($config) && !empty($config)) {
\r
372 $_config = array_merge($_config, $config);
\r
375 if (is_array($events) && !empty($events)) {
\r
376 foreach ($events as $eventName => $code) {
\r
377 if (!isset($_events[$eventName])) {
\r
378 $_events[$eventName] = array();
\r
380 if (!in_array($code, $_events[$eventName])) {
\r
381 $_events[$eventName][] = $code;
\r
386 if (!empty($_events)) {
\r
387 foreach($_events as $eventName => $handlers) {
\r
388 if (empty($handlers)) {
\r
391 else if (count($handlers) == 1) {
\r
392 $_config['on'][$eventName] = '@@'.$handlers[0];
\r
395 $_config['on'][$eventName] = '@@function (ev){';
\r
396 foreach ($handlers as $handler => $code) {
\r
397 $_config['on'][$eventName] .= '('.$code.')(ev);';
\r
399 $_config['on'][$eventName] .= '}';
\r
408 * Return global event handlers.
\r
411 function returnGlobalEvents()
\r
413 static $returnedEvents;
\r
416 if (!isset($returnedEvents)) {
\r
417 $returnedEvents = array();
\r
420 if (!empty($this->_globalEvents)) {
\r
421 foreach ($this->_globalEvents as $eventName => $handlers) {
\r
422 foreach ($handlers as $handler => $code) {
\r
423 if (!isset($returnedEvents[$eventName])) {
\r
424 $returnedEvents[$eventName] = array();
\r
426 // Return only new events
\r
427 if (!in_array($code, $returnedEvents[$eventName])) {
\r
428 $out .= ($code ? "\n" : "") . "CKEDITOR.on('". $eventName ."', $code);";
\r
429 $returnedEvents[$eventName][] = $code;
\r
439 * Initializes CKEditor (executed only once).
\r
444 static $initComplete;
\r
447 if (!empty($initComplete)) {
\r
451 if ($this->initialized) {
\r
452 $initComplete = true;
\r
457 $ckeditorPath = $this->ckeditorPath();
\r
459 if (!empty($this->timestamp) && $this->timestamp != "%"."TIMESTAMP%") {
\r
460 $args = '?t=' . $this->timestamp;
\r
463 // Skip relative paths...
\r
464 if (strpos($ckeditorPath, '..') !== 0) {
\r
465 $out .= $this->script("window.CKEDITOR_BASEPATH='". $ckeditorPath ."';");
\r
468 $out .= "<script type=\"text/javascript\" src=\"" . $ckeditorPath . 'ckeditor.js' . $args . "\"></script>\n";
\r
471 if ($this->timestamp != $this->_timestamp) {
\r
472 $extraCode .= ($extraCode ? "\n" : "") . "CKEDITOR.timestamp = '". $this->timestamp ."';";
\r
475 $out .= $this->script($extraCode);
\r
478 $initComplete = $this->initialized = true;
\r
484 * Return path to ckeditor.js.
\r
487 function ckeditorPath()
\r
489 if (!empty($this->basePath)) {
\r
490 return $this->basePath;
\r
494 * The absolute pathname of the currently executing script.
\r
495 * Note: If a script is executed with the CLI, as a relative path, such as file.php or ../file.php,
\r
496 * $_SERVER['SCRIPT_FILENAME'] will contain the relative path specified by the user.
\r
498 if (isset($_SERVER['SCRIPT_FILENAME'])) {
\r
499 $realPath = dirname($_SERVER['SCRIPT_FILENAME']);
\r
503 * realpath — Returns canonicalized absolute pathname
\r
505 $realPath = realpath( './' ) ;
\r
509 * The filename of the currently executing script, relative to the document root.
\r
510 * For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar
\r
511 * would be /test.php/foo.bar.
\r
513 $selfPath = dirname($_SERVER['PHP_SELF']);
\r
514 $file = str_replace("\\", "/", __FILE__);
\r
516 if (!$selfPath || !$realPath || !$file) {
\r
517 return "/ckeditor/";
\r
520 $documentRoot = substr($realPath, 0, strlen($realPath) - strlen($selfPath));
\r
521 $fileUrl = substr($file, strlen($documentRoot));
\r
522 $ckeditorUrl = str_replace("ckeditor_php5.php", "", $fileUrl);
\r
524 return $ckeditorUrl;
\r
528 * This little function provides a basic JSON support.
\r
529 * http://php.net/manual/en/function.json-encode.php
\r
532 * @param mixed $val
\r
535 function jsEncode($val)
\r
537 if (is_null($val)) {
\r
540 if ($val === false) {
\r
543 if ($val === true) {
\r
546 if (is_scalar($val))
\r
548 if (is_float($val))
\r
550 // Always use "." for floats.
\r
551 $val = str_replace(",", ".", strval($val));
\r
554 // Use @@ to not use quotes when outputting string value
\r
555 if (strpos($val, '@@') === 0) {
\r
556 return substr($val, 2);
\r
559 // All scalars are converted to strings to avoid indeterminism.
\r
560 // PHP's "1" and 1 are equal for all PHP operators, but
\r
561 // JS's "1" and 1 are not. So if we pass "1" or 1 from the PHP backend,
\r
562 // we should get the same result in the JS frontend (string).
\r
563 // Character replacements for JSON.
\r
564 static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'),
\r
565 array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
\r
567 $val = str_replace($jsonReplaces[0], $jsonReplaces[1], $val);
\r
569 return '"' . $val . '"';
\r
573 for ($i = 0, reset($val); $i < count($val); $i++, next($val))
\r
575 if (key($val) !== $i)
\r
584 foreach ($val as $v) $result[] = $this->jsEncode($v);
\r
585 return '[ ' . join(', ', $result) . ' ]';
\r
589 foreach ($val as $k => $v) $result[] = $this->jsEncode($k).': '.$this->jsEncode($v);
\r
590 return '{ ' . join(', ', $result) . ' }';
\r