Source for file ckeditor_php5.php
Documentation is available at ckeditor_php5.php
* Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.html or http://ckeditor.com/license
* \brief CKEditor class that can be used to create editor
* instances in PHP pages on server side.
* @see http://ckeditor.com
* $CKEditor = new CKEditor();
* $CKEditor->editor("editor1", "<p>Initial value.</p>");
* The version of %CKEditor.
* A constant string unique for each release of %CKEditor.
const timestamp =
'B8DJ5M3';
* URL to the %CKEditor installation directory (absolute or relative to document root).
* If not set, CKEditor will try to guess it's path.
* $CKEditor->basePath = '/ckeditor/';
* An array that holds the global %CKEditor configuration.
* For the list of available options, see http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html
* $CKEditor->config['height'] = 400;
* // Use @@ at the beggining of a string to ouput it without surrounding quotes.
* $CKEditor->config['width'] = '@@screen.width * 0.8';
* A boolean variable indicating whether CKEditor has been initialized.
* Set it to true only if you have already included
* <script> tag loading ckeditor.js in your website.
* Boolean variable indicating whether created code should be printed out or returned by a function.
* Example 1: get the code creating %CKEditor instance and print it on a page with the "echo" function.
* $CKEditor = new CKEditor();
* $CKEditor->returnOutput = true;
* $code = $CKEditor->editor("editor1", "<p>Initial value.</p>");
* echo "<p>Editor 1:</p>";
* An array with textarea attributes.
* When %CKEditor is created with the editor() method, a HTML <textarea> element is created,
* it will be displayed to anyone with JavaScript disabled or with incompatible browser.
* A string indicating the creation date of %CKEditor.
* Do not change it unless you want to force browsers to not use previously cached version of %CKEditor.
* An array that holds event listeners.
private $events =
array();
* An array that holds global event listeners.
private $globalEvents =
array();
* @param $basePath (string) URL to the %CKEditor installation directory (optional).
function __construct($basePath =
null) {
* Creates a %CKEditor instance.
* In incompatible browsers %CKEditor will downgrade to plain HTML <textarea> element.
* @param $name (string) Name of the %CKEditor instance (this will be also the "name" attribute of textarea element).
* @param $value (string) Initial value (optional).
* @param $config (array) The specific configurations to apply to this editor instance (optional).
* @param $events (array) Event listeners for this editor instance (optional).
* $CKEditor = new CKEditor();
* $CKEditor->editor("field1", "<p>Initial value.</p>");
* $CKEditor = new CKEditor();
* $config['toolbar'] = array(
* array( 'Source', '-', 'Bold', 'Italic', 'Underline', 'Strike' ),
* array( 'Image', 'Link', 'Unlink', 'Anchor' )
* $events['instanceReady'] = 'function (ev) {
* alert("Loaded: " + ev.editor.name);
* $CKEditor->editor("field1", "<p>Initial value.</p>", $config, $events);
public function editor($name, $value =
"", $config =
array(), $events =
array())
$attr.=
" " .
$key .
'="' .
str_replace('"', '"', $val) .
'"';
$out =
"<textarea name=\"" .
$name .
"\"" .
$attr .
">" .
htmlspecialchars($value) .
"</textarea>\n";
$js .=
"CKEDITOR.replace('".
$name.
"', ".
$this->jsEncode($_config).
");";
$js .=
"CKEDITOR.replace('".
$name.
"');";
* Replaces a <textarea> with a %CKEditor instance.
* @param $id (string) The id or name of textarea element.
* @param $config (array) The specific configurations to apply to this editor instance (optional).
* @param $events (array) Event listeners for this editor instance (optional).
* Example 1: adding %CKEditor to <textarea name="article"></textarea> element:
* $CKEditor = new CKEditor();
* $CKEditor->replace("article");
public function replace($id, $config =
array(), $events =
array())
$js .=
"CKEDITOR.replace('".
$id.
"', ".
$this->jsEncode($_config).
");";
$js .=
"CKEDITOR.replace('".
$id.
"');";
* Replace all <textarea> elements available in the document with editor instances.
* @param $className (string) If set, replace all textareas with class className in the page.
* Example 1: replace all <textarea> elements in the page.
* $CKEditor = new CKEditor();
* $CKEditor->replaceAll();
* Example 2: replace all <textarea class="myClassName"> elements in the page.
* $CKEditor = new CKEditor();
* $CKEditor->replaceAll( 'myClassName' );
$js .=
"CKEDITOR.replaceAll();";
$js .=
"CKEDITOR.replaceAll('".
$className.
"');";
$js .=
"CKEDITOR.replaceAll( function(textarea, config) {\n";
if (!empty($className)) {
$js .=
" var classRegex = new RegExp('(?:^| )' + '".
$className .
"' + '(?:$| )');\n";
$js .=
" if (!classRegex.test(textarea.className))\n";
$js .=
" return false;\n";
$js .=
" CKEDITOR.tools.extend(config, ".
$this->jsEncode($_config) .
", true);";
* Events are fired by %CKEditor in various situations.
* @param $event (string) Event name.
* @param $javascriptCode (string) Javascript anonymous function or function name.
* $CKEditor->addEventHandler('instanceReady', 'function (ev) {
* alert("Loaded: " + ev.editor.name);
if (!isset
($this->events[$event])) {
$this->events[$event] =
array();
if (!in_array($javascriptCode, $this->events[$event])) {
$this->events[$event][] =
$javascriptCode;
* Clear registered event handlers.
* Note: this function will have no effect on already created editor instances.
* @param $event (string) Event name, if not set all event handlers will be removed (optional).
$this->events[$event] =
array();
* Adds global event listener.
* @param $event (string) Event name.
* @param $javascriptCode (string) Javascript anonymous function or function name.
* $CKEditor->addGlobalEventHandler('dialogDefinition', 'function (ev) {
* alert("Loading dialog: " + ev.data.name);
if (!isset
($this->globalEvents[$event])) {
$this->globalEvents[$event] =
array();
if (!in_array($javascriptCode, $this->globalEvents[$event])) {
$this->globalEvents[$event][] =
$javascriptCode;
* Clear registered global event handlers.
* Note: this function will have no effect if the event handler has been already printed/returned.
* @param $event (string) Event name, if not set all event handlers will be removed (optional).
$this->globalEvents[$event] =
array();
$this->globalEvents =
array();
* Prints javascript code.
$out =
"<script type=\"text/javascript\">";
* Returns the configuration array (global and instance specific settings are merged into one array).
* @param $config (array) The specific configurations to apply to editor instance.
* @param $events (array) Event listeners for editor instance.
$_events =
$this->events;
if (is_array($config) &&
!empty($config)) {
if (is_array($events) &&
!empty($events)) {
foreach ($events as $eventName =>
$code) {
if (!isset
($_events[$eventName])) {
$_events[$eventName] =
array();
if (!in_array($code, $_events[$eventName])) {
$_events[$eventName][] =
$code;
foreach($_events as $eventName =>
$handlers) {
else if (count($handlers) ==
1) {
$_config['on'][$eventName] =
'@@'.
$handlers[0];
$_config['on'][$eventName] =
'@@function (ev){';
foreach ($handlers as $handler =>
$code) {
$_config['on'][$eventName] .=
'('.
$code.
')(ev);';
$_config['on'][$eventName] .=
'}';
* Return global event handlers.
if (!isset
($returnedEvents)) {
$returnedEvents =
array();
if (!empty($this->globalEvents)) {
foreach ($this->globalEvents as $eventName =>
$handlers) {
foreach ($handlers as $handler =>
$code) {
if (!isset
($returnedEvents[$eventName])) {
$returnedEvents[$eventName] =
array();
// Return only new events
if (!in_array($code, $returnedEvents[$eventName])) {
$out .=
($code ?
"\n" :
"") .
"CKEDITOR.on('".
$eventName .
"', $code);";
$returnedEvents[$eventName][] =
$code;
* Initializes CKEditor (executed only once).
if (!empty($initComplete)) {
// Skip relative paths...
if (strpos($ckeditorPath, '..') !==
0) {
$out .=
$this->script("window.CKEDITOR_BASEPATH='".
$ckeditorPath .
"';");
$out .=
"<script type=\"text/javascript\" src=\"" .
$ckeditorPath .
'ckeditor.js' .
$args .
"\"></script>\n";
$extraCode .=
($extraCode ?
"\n" :
"") .
"CKEDITOR.timestamp = '".
$this->timestamp .
"';";
$out .=
$this->script($extraCode);
* Return path to ckeditor.js.
* The absolute pathname of the currently executing script.
* Note: If a script is executed with the CLI, as a relative path, such as file.php or ../file.php,
* $_SERVER['SCRIPT_FILENAME'] will contain the relative path specified by the user.
if (isset
($_SERVER['SCRIPT_FILENAME'])) {
$realPath =
dirname($_SERVER['SCRIPT_FILENAME']);
* realpath - Returns canonicalized absolute pathname
* The filename of the currently executing script, relative to the document root.
* For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar
* would be /test.php/foo.bar.
$selfPath =
dirname($_SERVER['PHP_SELF']);
if (!$selfPath ||
!$realPath ||
!$file) {
$ckeditorUrl =
str_replace("ckeditor_php5.php", "", $fileUrl);
* This little function provides a basic JSON support.
return $val ?
'true' :
'false';
foreach ($val as $k =>
$v){
return '{' .
implode(',', $temp) .
'}';
return '"' .
str_replace(array("\\", "/", "\n", "\t", "\r", "\x08", "\x0c", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'), $val) .
'"';