![]() |
Xataface CKeditor Module 0.3
CKeditor Widget for Xataface
|
00001 <?php 00002 /* 00003 * Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. 00004 * For licensing, see LICENSE.html or http://ckeditor.com/license 00005 */ 00006 00018 class CKEditor 00019 { 00024 var $version = '3.4.1'; 00029 var $_timestamp = 'A8LE4JO'; 00030 00040 var $basePath; 00052 var $config = array(); 00058 var $initialized = false; 00071 var $returnOutput = false; 00078 var $textareaAttributes = array( "rows" => 8, "cols" => 60 ); 00083 var $timestamp = "A8LE4JO"; 00088 var $_events = array(); 00093 var $_globalEvents = array(); 00094 00100 function CKEditor($basePath = null) { 00101 if (!empty($basePath)) { 00102 $this->basePath = $basePath; 00103 } 00104 } 00105 00135 function editor($name, $value = "", $config = array(), $events = array()) 00136 { 00137 $attr = ""; 00138 foreach ($this->textareaAttributes as $key => $val) { 00139 $attr.= " " . $key . '="' . str_replace('"', '"', $val) . '"'; 00140 } 00141 $out = "<textarea name=\"" . $name . "\"" . $attr . ">" . htmlspecialchars($value) . "</textarea>\n"; 00142 if (!$this->initialized) { 00143 $out .= $this->init(); 00144 } 00145 00146 $_config = $this->configSettings($config, $events); 00147 00148 $js = $this->returnGlobalEvents(); 00149 if (!empty($_config)) 00150 $js .= "CKEDITOR.replace('".$name."', ".$this->jsEncode($_config).");"; 00151 else 00152 $js .= "CKEDITOR.replace('".$name."');"; 00153 00154 $out .= $this->script($js); 00155 00156 if (!$this->returnOutput) { 00157 print $out; 00158 $out = ""; 00159 } 00160 00161 return $out; 00162 } 00163 00177 function replace($id, $config = array(), $events = array()) 00178 { 00179 $out = ""; 00180 if (!$this->initialized) { 00181 $out .= $this->init(); 00182 } 00183 00184 $_config = $this->configSettings($config, $events); 00185 00186 $js = $this->returnGlobalEvents(); 00187 if (!empty($_config)) { 00188 $js .= "CKEDITOR.replace('".$id."', ".$this->jsEncode($_config).");"; 00189 } 00190 else { 00191 $js .= "CKEDITOR.replace('".$id."');"; 00192 } 00193 $out .= $this->script($js); 00194 00195 if (!$this->returnOutput) { 00196 print $out; 00197 $out = ""; 00198 } 00199 00200 return $out; 00201 } 00202 00220 function replaceAll($className = null) 00221 { 00222 $out = ""; 00223 if (!$this->initialized) { 00224 $out .= $this->init(); 00225 } 00226 00227 $_config = $this->configSettings(); 00228 00229 $js = $this->returnGlobalEvents(); 00230 if (empty($_config)) { 00231 if (empty($className)) { 00232 $js .= "CKEDITOR.replaceAll();"; 00233 } 00234 else { 00235 $js .= "CKEDITOR.replaceAll('".$className."');"; 00236 } 00237 } 00238 else { 00239 $classDetection = ""; 00240 $js .= "CKEDITOR.replaceAll( function(textarea, config) {\n"; 00241 if (!empty($className)) { 00242 $js .= " var classRegex = new RegExp('(?:^| )' + '". $className ."' + '(?:$| )');\n"; 00243 $js .= " if (!classRegex.test(textarea.className))\n"; 00244 $js .= " return false;\n"; 00245 } 00246 $js .= " CKEDITOR.tools.extend(config, ". $this->jsEncode($_config) .", true);"; 00247 $js .= "} );"; 00248 00249 } 00250 00251 $out .= $this->script($js); 00252 00253 if (!$this->returnOutput) { 00254 print $out; 00255 $out = ""; 00256 } 00257 00258 return $out; 00259 } 00260 00275 function addEventHandler($event, $javascriptCode) 00276 { 00277 if (!isset($this->_events[$event])) { 00278 $this->_events[$event] = array(); 00279 } 00280 // Avoid duplicates. 00281 if (!in_array($javascriptCode, $this->_events[$event])) { 00282 $this->_events[$event][] = $javascriptCode; 00283 } 00284 } 00285 00292 function clearEventHandlers($event = null) 00293 { 00294 if (!empty($event)) { 00295 $this->_events[$event] = array(); 00296 } 00297 else { 00298 $this->_events = array(); 00299 } 00300 } 00301 00315 function addGlobalEventHandler($event, $javascriptCode) 00316 { 00317 if (!isset($this->_globalEvents[$event])) { 00318 $this->_globalEvents[$event] = array(); 00319 } 00320 // Avoid duplicates. 00321 if (!in_array($javascriptCode, $this->_globalEvents[$event])) { 00322 $this->_globalEvents[$event][] = $javascriptCode; 00323 } 00324 } 00325 00332 function clearGlobalEventHandlers($event = null) 00333 { 00334 if (!empty($event)) { 00335 $this->_globalEvents[$event] = array(); 00336 } 00337 else { 00338 $this->_globalEvents = array(); 00339 } 00340 } 00341 00348 function script($js) 00349 { 00350 $out = "<script type=\"text/javascript\">"; 00351 $out .= "//<![CDATA[\n"; 00352 $out .= $js; 00353 $out .= "\n//]]>"; 00354 $out .= "</script>\n"; 00355 00356 return $out; 00357 } 00358 00366 function configSettings($config = array(), $events = array()) 00367 { 00368 $_config = $this->config; 00369 $_events = $this->_events; 00370 00371 if (is_array($config) && !empty($config)) { 00372 $_config = array_merge($_config, $config); 00373 } 00374 00375 if (is_array($events) && !empty($events)) { 00376 foreach ($events as $eventName => $code) { 00377 if (!isset($_events[$eventName])) { 00378 $_events[$eventName] = array(); 00379 } 00380 if (!in_array($code, $_events[$eventName])) { 00381 $_events[$eventName][] = $code; 00382 } 00383 } 00384 } 00385 00386 if (!empty($_events)) { 00387 foreach($_events as $eventName => $handlers) { 00388 if (empty($handlers)) { 00389 continue; 00390 } 00391 else if (count($handlers) == 1) { 00392 $_config['on'][$eventName] = '@@'.$handlers[0]; 00393 } 00394 else { 00395 $_config['on'][$eventName] = '@@function (ev){'; 00396 foreach ($handlers as $handler => $code) { 00397 $_config['on'][$eventName] .= '('.$code.')(ev);'; 00398 } 00399 $_config['on'][$eventName] .= '}'; 00400 } 00401 } 00402 } 00403 00404 return $_config; 00405 } 00406 00411 function returnGlobalEvents() 00412 { 00413 static $returnedEvents; 00414 $out = ""; 00415 00416 if (!isset($returnedEvents)) { 00417 $returnedEvents = array(); 00418 } 00419 00420 if (!empty($this->_globalEvents)) { 00421 foreach ($this->_globalEvents as $eventName => $handlers) { 00422 foreach ($handlers as $handler => $code) { 00423 if (!isset($returnedEvents[$eventName])) { 00424 $returnedEvents[$eventName] = array(); 00425 } 00426 // Return only new events 00427 if (!in_array($code, $returnedEvents[$eventName])) { 00428 $out .= ($code ? "\n" : "") . "CKEDITOR.on('". $eventName ."', $code);"; 00429 $returnedEvents[$eventName][] = $code; 00430 } 00431 } 00432 } 00433 } 00434 00435 return $out; 00436 } 00437 00442 function init() 00443 { 00444 static $initComplete; 00445 $out = ""; 00446 00447 if (!empty($initComplete)) { 00448 return ""; 00449 } 00450 00451 if ($this->initialized) { 00452 $initComplete = true; 00453 return ""; 00454 } 00455 00456 $args = ""; 00457 $ckeditorPath = $this->ckeditorPath(); 00458 00459 if (!empty($this->timestamp) && $this->timestamp != "%"."TIMESTAMP%") { 00460 $args = '?t=' . $this->timestamp; 00461 } 00462 00463 // Skip relative paths... 00464 if (strpos($ckeditorPath, '..') !== 0) { 00465 $out .= $this->script("window.CKEDITOR_BASEPATH='". $ckeditorPath ."';"); 00466 } 00467 00468 $out .= "<script type=\"text/javascript\" src=\"" . $ckeditorPath . 'ckeditor.js' . $args . "\"></script>\n"; 00469 00470 $extraCode = ""; 00471 if ($this->timestamp != $this->_timestamp) { 00472 $extraCode .= ($extraCode ? "\n" : "") . "CKEDITOR.timestamp = '". $this->timestamp ."';"; 00473 } 00474 if ($extraCode) { 00475 $out .= $this->script($extraCode); 00476 } 00477 00478 $initComplete = $this->initialized = true; 00479 00480 return $out; 00481 } 00482 00487 function ckeditorPath() 00488 { 00489 if (!empty($this->basePath)) { 00490 return $this->basePath; 00491 } 00492 00498 if (isset($_SERVER['SCRIPT_FILENAME'])) { 00499 $realPath = dirname($_SERVER['SCRIPT_FILENAME']); 00500 } 00501 else { 00505 $realPath = realpath( './' ) ; 00506 } 00507 00513 $selfPath = dirname($_SERVER['PHP_SELF']); 00514 $file = str_replace("\\", "/", __FILE__); 00515 00516 if (!$selfPath || !$realPath || !$file) { 00517 return "/ckeditor/"; 00518 } 00519 00520 $documentRoot = substr($realPath, 0, strlen($realPath) - strlen($selfPath)); 00521 $fileUrl = substr($file, strlen($documentRoot)); 00522 $ckeditorUrl = str_replace("ckeditor_php5.php", "", $fileUrl); 00523 00524 return $ckeditorUrl; 00525 } 00526 00535 function jsEncode($val) 00536 { 00537 if (is_null($val)) { 00538 return 'null'; 00539 } 00540 if ($val === false) { 00541 return 'false'; 00542 } 00543 if ($val === true) { 00544 return 'true'; 00545 } 00546 if (is_scalar($val)) 00547 { 00548 if (is_float($val)) 00549 { 00550 // Always use "." for floats. 00551 $val = str_replace(",", ".", strval($val)); 00552 } 00553 00554 // Use @@ to not use quotes when outputting string value 00555 if (strpos($val, '@@') === 0) { 00556 return substr($val, 2); 00557 } 00558 else { 00559 // All scalars are converted to strings to avoid indeterminism. 00560 // PHP's "1" and 1 are equal for all PHP operators, but 00561 // JS's "1" and 1 are not. So if we pass "1" or 1 from the PHP backend, 00562 // we should get the same result in the JS frontend (string). 00563 // Character replacements for JSON. 00564 static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), 00565 array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"')); 00566 00567 $val = str_replace($jsonReplaces[0], $jsonReplaces[1], $val); 00568 00569 return '"' . $val . '"'; 00570 } 00571 } 00572 $isList = true; 00573 for ($i = 0, reset($val); $i < count($val); $i++, next($val)) 00574 { 00575 if (key($val) !== $i) 00576 { 00577 $isList = false; 00578 break; 00579 } 00580 } 00581 $result = array(); 00582 if ($isList) 00583 { 00584 foreach ($val as $v) $result[] = $this->jsEncode($v); 00585 return '[ ' . join(', ', $result) . ' ]'; 00586 } 00587 else 00588 { 00589 foreach ($val as $k => $v) $result[] = $this->jsEncode($k).': '.$this->jsEncode($v); 00590 return '{ ' . join(', ', $result) . ' }'; 00591 } 00592 } 00593 }