<?php 


/** 
***************************************************
*欢迎使用免费版SiteTeam企业建站软件！
*这是一个专业为搜索引擎营销而设计的，极稳定、极易维护的建站软件！
*官方客服：help@php.net.cn
*官方下载网址：http://www.SiteTeam.cn 
***************************************************
**/




 class CKEditor { var $version = '3.3'; var $_timestamp = 'A4SD49Y'; var $basePath; var $config = array(); var $initialized = false; var $returnOutput = false; var $textareaAttributes = array( "rows" => 8, "cols" => 60 ); var $timestamp = "A4SD49Y"; var $_events = array(); var $_globalEvents = array(); function CKEditor($basePath = null) { if (!empty($basePath)) { $this->basePath = $basePath; } } function editor($name, $value = "", $config = array(), $events = array()) { $attr = ""; foreach ($this->textareaAttributes as $key => $val) { $attr.= " " . $key . '="' . str_replace('"', '&quot;', $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; } 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; } 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; } function addEventHandler($event, $javascriptCode) { if (!isset($this->_events[$event])) { $this->_events[$event] = array(); } if (!in_array($javascriptCode, $this->_events[$event])) { $this->_events[$event][] = $javascriptCode; } } function clearEventHandlers($event = null) { if (!empty($event)) { $this->_events[$event] = array(); } else { $this->_events = array(); } } function addGlobalEventHandler($event, $javascriptCode) { if (!isset($this->_globalEvents[$event])) { $this->_globalEvents[$event] = array(); } if (!in_array($javascriptCode, $this->_globalEvents[$event])) { $this->_globalEvents[$event][] = $javascriptCode; } } function clearGlobalEventHandlers($event = null) { if (!empty($event)) { $this->_globalEvents[$event] = array(); } else { $this->_globalEvents = array(); } } function script($js) { $out = "<script type=\"text/javascript\">"; $out .= "//<![CDATA[\n"; $out .= $js; $out .= "\n//]]>"; $out .= "</script>\n"; return $out; } 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; } 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; } 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 != $this->_timestamp) { $extraCode .= ($extraCode ? "\n" : "") . "CKEDITOR.timestamp = '". $this->timestamp ."';"; } if ($extraCode) { $out .= $this->script($extraCode); } $initComplete = $this->initialized = true; return $out; } 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; } 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)) { $val = str_replace(",", ".", strval($val)); } if (strpos($val, '@@') === 0) { return substr($val, 2); } else { 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) . ' }'; } } } 