Index: _samples/php/advanced.php =================================================================== --- _samples/php/advanced.php (revision 0) +++ _samples/php/advanced.php (revision 0) @@ -0,0 +1,90 @@ + + + +
+Initial value.
"); + * ?> + * @endcode + */ + +if ( !function_exists('version_compare') || version_compare( phpversion(), '5', '<' ) ) + include_once( 'ckeditor_php4.php' ) ; +else + include_once( 'ckeditor_php5.php' ) ; Index: ckeditor_php5.php =================================================================== --- ckeditor_php5.php (revision 0) +++ ckeditor_php5.php (revision 0) @@ -0,0 +1,378 @@ +textarea("editor1", "Initial value.
"); + * @endcode + */ +class CKEditor +{ + /** + * The version of %CKEditor. + */ + const version = '%VERSION%'; + /** + * A constant string unique for each release of %CKEditor. + */ + const timestamp = '%TIMESTAMP%'; + + /** + * URL to the %CKEditor installation directory (absolute or relative to document root). + * If not set, CKEditor will try to guess it's path. + * + * Example usage: + * @code + * $CKEditor->basePath = '/ckeditor/'; + * @endcode + */ + public $basePath; + /** + * 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. + */ + public $timestamp = "%TIMESTAMP%"; + /** + * 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 + * + * Example usage: + * @code + * $CKEditor->config['height'] = 400; + * // Use @@ at the beggining of a string to ouput it without surrounding quotes. + * $CKEditor->config['width'] = '@@screen.width * 0.8'; + * @endcode + */ + public $config = array(); + /** + * An array that holds event listeners. + * Events are fired by %CKEditor in various situations. + * + * Example usage: + * @code + * $CKEditor->events['dialogDefinition'] = 'function (evt) { + * alert("Loading dialog: " + ev.data.name); + * }'; + * @endcode + */ + public $events = array(); + /** + * 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. + */ + public $initialized = false; + + /** + * Main Constructor. + * + * @param $basePath (string) URL to the %CKEditor installation directory (optional). + */ + function __construct($basePath = null) { + // CKReleaser %REMOVE_START% + if (self::version == '%VERSION%') { + throw new Exception("PHP SSI can be only used with compiled version of CKEditor."); + } + // %REMOVE_END% + if (!empty($basePath)) { + $this->basePath = $basePath; + } + } + + /** + * Creates a <textarea> element with %CKEditor enabled on it. + * + * @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 $attributes (array) An array with textarea attributes (optional). + * @param $config (array) The specific configurations to apply to this editor instance (optional). + * @param $events (array) Event listeners for this editor instance (optional). + * + * Example usage: + * @code + * $CKEditor = new CKEditor(); + * echo $CKEditor->textarea("field1", "Initial value.
"); + * @endcode + * + * Advanced example: + * @code + * $CKEditor = new CKEditor(); + * $config = array(); + * $config['toolbar'] = array( + * array( 'Source', '-', 'Bold', 'Italic', 'Underline', 'Strike' ), + * array( 'Image', 'Link', 'Unlink', 'Anchor' ) + * ); + * $events['instanceReady'] = 'function (evt) { + * alert("Loaded: " + evt.editor.name); + * }'; + * echo $CKEditor->textarea("field1", "Initial value.
", array("cols" => 60, "rows" => 8), $config, $events); + * @endcode + */ + public function textarea($name, $value = "", $attributes = array(), $config = array(), $events = array()) + { + foreach ($attributes as $key => $val) { + $attr.= " " . $key . '="' . str_replace('"', '"', $val) . '"'; + } + $out = ''; + if (!$this->initialized) { + $out .= $this->init(); + } + + if (!empty($events)) { + foreach($events as $event => $code) { + $config['on'][$event] = '@@'.$code; + } + } + if (!empty($config)) + $code = "CKEDITOR.replace('".$name."', ".$this->jsEncode($config).");"; + else + $code = "CKEDITOR.replace('".$name."');"; + + $out .= $this->script($code); + + return $out; + } + + /** + * 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: + * @code + * $CKEditor = new CKEditor(); + * echo $CKEditor->replace("article"); + * @endcode + */ + public function replace($id, $config = array(), $events = array()) + { + $out = ""; + if (!$this->initialized) { + $out .= $this->init(); + } + if (!empty($events)) { + foreach($events as $event => $code) { + $config['on'][$event] = '@@'.$code; + } + } + if (!empty($config)) { + $js = "CKEDITOR.replace('".$id."', ".$this->jsEncode($config).");"; + } + else { + $js = "CKEDITOR.replace('".$id."');"; + } + $out .= $this->script($js); + return $out; + } + + /** + * 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. + * @code + * $CKEditor = new CKEditor(); + * echo $CKEditor->replaceAll(); + * @endcode + * + * Example 2: replace all <textarea class="myClassName"> elements in the page. + * @code + * $CKEditor = new CKEditor(); + * echo $CKEditor->replaceAll( 'myClassName' ); + * @endcode + */ + public function replaceAll($className = null) + { + $out = ""; + if (!$this->initialized) { + $out .= $this->init(); + } + if (!empty($className)) { + $js = "CKEDITOR.replaceAll('".$className."');"; + } + else { + $js = "CKEDITOR.replaceAll();"; + } + $out .= $this->script($js); + return $out; + } + + /** + * Prints javascript code. + * + * @param string $js + */ + private function script($js) + { + $out = "\n"; + + return $out; + } + + private function init() + { + if ($this->initialized) { + return ""; + } + + $this->initialized = true; + + $args = ""; + $ckeditorPath = $this->ckeditorPath(); + + if (!empty($this->timestamp) && $this->timestamp != "%TIMESTAMP%") { + $args = '?t=' . $this->timestamp; + } + + $out = ""; + // Skip relative paths... + if (strpos($ckeditorPath, '..') !== 0) { + $out .= $this->script("window.CKEDITOR_BASEPATH='". $ckeditorPath ."';"); + } + + $out .= "\n"; + + $extraCode = ""; + if (!empty($this->timestamp) && $this->timestamp != self::timestamp) { + $extraCode .= "CKEDITOR.timestamp = '". $this->timestamp ."';"; + } + if (!empty($this->config)) { + foreach ($this->config as $name => $code) { + $extraCode .= ($extraCode ? "\n" : "") . "CKEDITOR.config['".$name."'] = ". $this->jsEncode($code) .";"; + } + } + if (!empty($this->events)) { + foreach ($this->events as $name => $code) { + $extraCode .= ($extraCode ? "\n" : "") . "CKEDITOR.on('". $name ."', $code);"; + } + } + + if ($extraCode) { + $out .= $this->script($extraCode); + } + + return $out; + } + + private function ckeditorPath() + { + if (!empty($this->basePath)) { + return $this->basePath; + } + + /** + * 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']); + } + else { + /** + * realpath — Returns canonicalized absolute pathname + */ + $realPath = realpath( './' ) ; + } + + /** + * 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']); + $file = str_replace("\\", "/", __FILE__); + + if (!$selfPath || !$realPath || !$file) { + return "/ckeditor/"; + } + + $documentRoot = substr($realPath, 0, strlen($realPath) - strlen($selfPath)); + $fileUrl = substr($file, strlen($documentRoot)); + $ckeditorUrl = str_replace("ckeditor_php5.php", "", $fileUrl); + + return $ckeditorUrl; + } + + /** + * This little function provides a basic JSON support. + * http://php.net/manual/en/function.json-encode.php + * + * @param mixed $val + * @return string + */ + private function jsEncode($val) + { + if (is_null($val)) { + return 'null'; + } + if ($val === false) { + return 'false'; + } + if ($val === true) { + return 'true'; + } + if (is_scalar($val)) + { + if (is_float($val)) + { + // Always use "." for floats. + $val = str_replace(",", ".", strval($val)); + } + + // Use @@ to not use quotes when outputting string value + if (strpos($val, '@@') === 0) { + return substr($val, 2); + } + else { + // All scalars are converted to strings to avoid indeterminism. + // PHP's "1" and 1 are equal for all PHP operators, but + // JS's "1" and 1 are not. So if we pass "1" or 1 from the PHP backend, + // we should get the same result in the JS frontend (string). + // Character replacements for JSON. + static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), + array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"')); + + $val = str_replace($jsonReplaces[0], $jsonReplaces[1], $val); + + return '"' . $val . '"'; + } + } + $isList = true; + for ($i = 0, reset($val); $i < count($val); $i++, next($val)) + { + if (key($val) !== $i) + { + $isList = false; + break; + } + } + $result = array(); + if ($isList) + { + foreach ($val as $v) $result[] = $this->jsEncode($v); + return '[ ' . join(', ', $result) . ' ]'; + } + else + { + foreach ($val as $k => $v) $result[] = $this->jsEncode($k).': '.$this->jsEncode($v); + return '{ ' . join(', ', $result) . ' }'; + } + } +}