123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556 |
- <?php
- class CKEditor
- {
-
- const version = '3.6.2';
-
- const timestamp = 'B8DJ5M3';
-
- public $basePath;
-
- public $config = array();
-
- public $initialized = false;
-
- public $returnOutput = false;
-
- public $textareaAttributes = array( "rows" => 8, "cols" => 60 );
-
- public $timestamp = "B8DJ5M3";
-
- private $events = array();
-
- private $globalEvents = array();
-
- function __construct($basePath = null) {
- if (!empty($basePath)) {
- $this->basePath = $basePath;
- }
- }
-
- public function editor($name, $value = "", $config = array(), $events = array())
- {
- $attr = "";
- foreach ($this->textareaAttributes as $key => $val) {
- $attr.= " " . $key . '="' . str_replace('"', '"', $val) . '"';
- }
- $out = "<textarea name=\"" . $name . "\"" . $attr . ">" . htmlspecialchars($value) . "</textarea>\n";
- if (!$this->initialized) {
- $out .= $this->init();
- }
- $_config = $this->configSettings($config, $events);
- $js = $this->returnGlobalEvents();
- if (!empty($_config))
- $js .= "CKEDITOR.replace('".$name."', ".$this->jsEncode($_config).");";
- else
- $js .= "CKEDITOR.replace('".$name."');";
- $out .= $this->script($js);
- if (!$this->returnOutput) {
- print $out;
- $out = "";
- }
- return $out;
- }
-
- public function replace($id, $config = array(), $events = array())
- {
- $out = "";
- if (!$this->initialized) {
- $out .= $this->init();
- }
- $_config = $this->configSettings($config, $events);
- $js = $this->returnGlobalEvents();
- if (!empty($_config)) {
- $js .= "CKEDITOR.replace('".$id."', ".$this->jsEncode($_config).");";
- }
- else {
- $js .= "CKEDITOR.replace('".$id."');";
- }
- $out .= $this->script($js);
- if (!$this->returnOutput) {
- print $out;
- $out = "";
- }
- return $out;
- }
-
- public function replaceAll($className = null)
- {
- $out = "";
- if (!$this->initialized) {
- $out .= $this->init();
- }
- $_config = $this->configSettings();
- $js = $this->returnGlobalEvents();
- if (empty($_config)) {
- if (empty($className)) {
- $js .= "CKEDITOR.replaceAll();";
- }
- else {
- $js .= "CKEDITOR.replaceAll('".$className."');";
- }
- }
- else {
- $classDetection = "";
- $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);";
- $js .= "} );";
- }
- $out .= $this->script($js);
- if (!$this->returnOutput) {
- print $out;
- $out = "";
- }
- return $out;
- }
-
- public function addEventHandler($event, $javascriptCode)
- {
- if (!isset($this->events[$event])) {
- $this->events[$event] = array();
- }
-
- if (!in_array($javascriptCode, $this->events[$event])) {
- $this->events[$event][] = $javascriptCode;
- }
- }
-
- public function clearEventHandlers($event = null)
- {
- if (!empty($event)) {
- $this->events[$event] = array();
- }
- else {
- $this->events = array();
- }
- }
-
- public function addGlobalEventHandler($event, $javascriptCode)
- {
- if (!isset($this->globalEvents[$event])) {
- $this->globalEvents[$event] = array();
- }
-
- if (!in_array($javascriptCode, $this->globalEvents[$event])) {
- $this->globalEvents[$event][] = $javascriptCode;
- }
- }
-
- public function clearGlobalEventHandlers($event = null)
- {
- if (!empty($event)) {
- $this->globalEvents[$event] = array();
- }
- else {
- $this->globalEvents = array();
- }
- }
-
- private function script($js)
- {
- $out = "<script type=\"text/javascript\">";
- $out .= "//<![CDATA[\n";
- $out .= $js;
- $out .= "\n//]]>";
- $out .= "</script>\n";
- return $out;
- }
-
- private function configSettings($config = array(), $events = array())
- {
- $_config = $this->config;
- $_events = $this->events;
- if (is_array($config) && !empty($config)) {
- $_config = array_merge($_config, $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;
- }
- }
- }
- if (!empty($_events)) {
- foreach($_events as $eventName => $handlers) {
- if (empty($handlers)) {
- continue;
- }
- else if (count($handlers) == 1) {
- $_config['on'][$eventName] = '@@'.$handlers[0];
- }
- else {
- $_config['on'][$eventName] = '@@function (ev){';
- foreach ($handlers as $handler => $code) {
- $_config['on'][$eventName] .= '('.$code.')(ev);';
- }
- $_config['on'][$eventName] .= '}';
- }
- }
- }
- return $_config;
- }
-
- private function returnGlobalEvents()
- {
- static $returnedEvents;
- $out = "";
- 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();
- }
-
- if (!in_array($code, $returnedEvents[$eventName])) {
- $out .= ($code ? "\n" : "") . "CKEDITOR.on('". $eventName ."', $code);";
- $returnedEvents[$eventName][] = $code;
- }
- }
- }
- }
- return $out;
- }
-
- private function init()
- {
- static $initComplete;
- $out = "";
- if (!empty($initComplete)) {
- return "";
- }
- if ($this->initialized) {
- $initComplete = true;
- return "";
- }
- $args = "";
- $ckeditorPath = $this->ckeditorPath();
- if (!empty($this->timestamp) && $this->timestamp != "%"."TIMESTAMP%") {
- $args = '?t=' . $this->timestamp;
- }
-
- if (strpos($ckeditorPath, '..') !== 0) {
- $out .= $this->script("window.CKEDITOR_BASEPATH='". $ckeditorPath ."';");
- }
- $out .= "<script type=\"text/javascript\" src=\"" . $ckeditorPath . 'ckeditor.js' . $args . "\"></script>\n";
- $extraCode = "";
- if ($this->timestamp != self::timestamp) {
- $extraCode .= ($extraCode ? "\n" : "") . "CKEDITOR.timestamp = '". $this->timestamp ."';";
- }
- if ($extraCode) {
- $out .= $this->script($extraCode);
- }
- $initComplete = $this->initialized = true;
- return $out;
- }
-
- private function ckeditorPath()
- {
- if (!empty($this->basePath)) {
- return $this->basePath;
- }
-
- if (isset($_SERVER['SCRIPT_FILENAME'])) {
- $realPath = dirname($_SERVER['SCRIPT_FILENAME']);
- }
- else {
-
- $realPath = realpath( './' ) ;
- }
-
- $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;
- }
-
- private function jsEncode($val)
- {
- if (is_null($val)) {
- return 'null';
- }
- if (is_bool($val)) {
- return $val ? 'true' : 'false';
- }
- if (is_int($val)) {
- return $val;
- }
- if (is_float($val)) {
- return str_replace(',', '.', $val);
- }
- if (is_array($val) || is_object($val)) {
- if (is_array($val) && (array_keys($val) === range(0,count($val)-1))) {
- return '[' . implode(',', array_map(array($this, 'jsEncode'), $val)) . ']';
- }
- $temp = array();
- foreach ($val as $k => $v){
- $temp[] = $this->jsEncode("{$k}") . ':' . $this->jsEncode($v);
- }
- return '{' . implode(',', $temp) . '}';
- }
-
- if (strpos($val, '@@') === 0)
- return substr($val, 2);
- if (strtoupper(substr($val, 0, 9)) == 'CKEDITOR.')
- return $val;
- return '"' . str_replace(array("\\", "/", "\n", "\t", "\r", "\x08", "\x0c", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'), $val) . '"';
- }
- }
|