PEEL Shopping
Open source ecommerce : PEEL Shopping
ckeditor_php5.php
Go to the documentation of this file.
1 <?php
2 /*
3 * Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.
4 * For licensing, see LICENSE.html or http://ckeditor.com/license
5 */
6 
18 class CKEditor
19 {
23  const version = '3.6.5';
27  const timestamp = 'C9A85WF';
28 
38  public $basePath;
50  public $config = array();
56  public $initialized = false;
69  public $returnOutput = false;
76  public $textareaAttributes = array( "rows" => 8, "cols" => 60 );
81  public $timestamp = "C9A85WF";
85  private $events = array();
89  private $globalEvents = array();
90 
96  function __construct($basePath = null) {
97  if (!empty($basePath)) {
98  $this->basePath = $basePath;
99  }
100  }
101 
131  public function editor($name, $value = "", $config = array(), $events = array())
132  {
133  $attr = "";
134  foreach ($this->textareaAttributes as $key => $val) {
135  $attr.= " " . $key . '="' . str_replace('"', '&quot;', $val) . '"';
136  }
137  $out = "<textarea name=\"" . $name . "\"" . $attr . ">" . htmlspecialchars($value) . "</textarea>\n";
138  if (!$this->initialized) {
139  $out .= $this->init();
140  }
141 
142  $_config = $this->configSettings($config, $events);
143 
144  $js = $this->returnGlobalEvents();
145  if (!empty($_config))
146  $js .= "CKEDITOR.replace('".$name."', ".$this->jsEncode($_config).");";
147  else
148  $js .= "CKEDITOR.replace('".$name."');";
149 
150  $out .= $this->script($js);
151 
152  if (!$this->returnOutput) {
153  print $out;
154  $out = "";
155  }
156 
157  return $out;
158  }
159 
173  public function replace($id, $config = array(), $events = array())
174  {
175  $out = "";
176  if (!$this->initialized) {
177  $out .= $this->init();
178  }
179 
180  $_config = $this->configSettings($config, $events);
181 
182  $js = $this->returnGlobalEvents();
183  if (!empty($_config)) {
184  $js .= "CKEDITOR.replace('".$id."', ".$this->jsEncode($_config).");";
185  }
186  else {
187  $js .= "CKEDITOR.replace('".$id."');";
188  }
189  $out .= $this->script($js);
190 
191  if (!$this->returnOutput) {
192  print $out;
193  $out = "";
194  }
195 
196  return $out;
197  }
198 
216  public function replaceAll($className = null)
217  {
218  $out = "";
219  if (!$this->initialized) {
220  $out .= $this->init();
221  }
222 
223  $_config = $this->configSettings();
224 
225  $js = $this->returnGlobalEvents();
226  if (empty($_config)) {
227  if (empty($className)) {
228  $js .= "CKEDITOR.replaceAll();";
229  }
230  else {
231  $js .= "CKEDITOR.replaceAll('".$className."');";
232  }
233  }
234  else {
235  $classDetection = "";
236  $js .= "CKEDITOR.replaceAll( function(textarea, config) {\n";
237  if (!empty($className)) {
238  $js .= " var classRegex = new RegExp('(?:^| )' + '". $className ."' + '(?:$| )');\n";
239  $js .= " if (!classRegex.test(textarea.className))\n";
240  $js .= " return false;\n";
241  }
242  $js .= " CKEDITOR.tools.extend(config, ". $this->jsEncode($_config) .", true);";
243  $js .= "} );";
244 
245  }
246 
247  $out .= $this->script($js);
248 
249  if (!$this->returnOutput) {
250  print $out;
251  $out = "";
252  }
253 
254  return $out;
255  }
256 
271  public function addEventHandler($event, $javascriptCode)
272  {
273  if (!isset($this->events[$event])) {
274  $this->events[$event] = array();
275  }
276  // Avoid duplicates.
277  if (!in_array($javascriptCode, $this->events[$event])) {
278  $this->events[$event][] = $javascriptCode;
279  }
280  }
281 
288  public function clearEventHandlers($event = null)
289  {
290  if (!empty($event)) {
291  $this->events[$event] = array();
292  }
293  else {
294  $this->events = array();
295  }
296  }
297 
311  public function addGlobalEventHandler($event, $javascriptCode)
312  {
313  if (!isset($this->globalEvents[$event])) {
314  $this->globalEvents[$event] = array();
315  }
316  // Avoid duplicates.
317  if (!in_array($javascriptCode, $this->globalEvents[$event])) {
318  $this->globalEvents[$event][] = $javascriptCode;
319  }
320  }
321 
328  public function clearGlobalEventHandlers($event = null)
329  {
330  if (!empty($event)) {
331  $this->globalEvents[$event] = array();
332  }
333  else {
334  $this->globalEvents = array();
335  }
336  }
337 
343  private function script($js)
344  {
345  $out = "<script type=\"text/javascript\">";
346  $out .= "//<![CDATA[\n";
347  $out .= $js;
348  $out .= "\n//]]>";
349  $out .= "</script>\n";
350 
351  return $out;
352  }
353 
360  private function configSettings($config = array(), $events = array())
361  {
362  $_config = $this->config;
363  $_events = $this->events;
364 
365  if (is_array($config) && !empty($config)) {
366  $_config = array_merge($_config, $config);
367  }
368 
369  if (is_array($events) && !empty($events)) {
370  foreach ($events as $eventName => $code) {
371  if (!isset($_events[$eventName])) {
372  $_events[$eventName] = array();
373  }
374  if (!in_array($code, $_events[$eventName])) {
375  $_events[$eventName][] = $code;
376  }
377  }
378  }
379 
380  if (!empty($_events)) {
381  foreach($_events as $eventName => $handlers) {
382  if (empty($handlers)) {
383  continue;
384  }
385  else if (count($handlers) == 1) {
386  $_config['on'][$eventName] = '@@'.$handlers[0];
387  }
388  else {
389  $_config['on'][$eventName] = '@@function (ev){';
390  foreach ($handlers as $handler => $code) {
391  $_config['on'][$eventName] .= '('.$code.')(ev);';
392  }
393  $_config['on'][$eventName] .= '}';
394  }
395  }
396  }
397 
398  return $_config;
399  }
400 
404  private function returnGlobalEvents()
405  {
406  static $returnedEvents;
407  $out = "";
408 
409  if (!isset($returnedEvents)) {
410  $returnedEvents = array();
411  }
412 
413  if (!empty($this->globalEvents)) {
414  foreach ($this->globalEvents as $eventName => $handlers) {
415  foreach ($handlers as $handler => $code) {
416  if (!isset($returnedEvents[$eventName])) {
417  $returnedEvents[$eventName] = array();
418  }
419  // Return only new events
420  if (!in_array($code, $returnedEvents[$eventName])) {
421  $out .= ($code ? "\n" : "") . "CKEDITOR.on('". $eventName ."', $code);";
422  $returnedEvents[$eventName][] = $code;
423  }
424  }
425  }
426  }
427 
428  return $out;
429  }
430 
434  private function init()
435  {
436  static $initComplete;
437  $out = "";
438 
439  if (!empty($initComplete)) {
440  return "";
441  }
442 
443  if ($this->initialized) {
444  $initComplete = true;
445  return "";
446  }
447 
448  $args = "";
449  $ckeditorPath = $this->ckeditorPath();
450 
451  if (!empty($this->timestamp) && $this->timestamp != "%"."TIMESTAMP%") {
452  $args = '?t=' . $this->timestamp;
453  }
454 
455  // Skip relative paths...
456  if (strpos($ckeditorPath, '..') !== 0) {
457  $out .= $this->script("window.CKEDITOR_BASEPATH='". $ckeditorPath ."';");
458  }
459 
460  $out .= "<script type=\"text/javascript\" src=\"" . $ckeditorPath . 'ckeditor.js' . $args . "\"></script>\n";
461 
462  $extraCode = "";
463  if ($this->timestamp != self::timestamp) {
464  $extraCode .= ($extraCode ? "\n" : "") . "CKEDITOR.timestamp = '". $this->timestamp ."';";
465  }
466  if ($extraCode) {
467  $out .= $this->script($extraCode);
468  }
469 
470  $initComplete = $this->initialized = true;
471 
472  return $out;
473  }
474 
478  private function ckeditorPath()
479  {
480  if (!empty($this->basePath)) {
481  return $this->basePath;
482  }
483 
489  if (isset($_SERVER['SCRIPT_FILENAME'])) {
490  $realPath = dirname($_SERVER['SCRIPT_FILENAME']);
491  }
492  else {
496  $realPath = realpath( './' ) ;
497  }
498 
504  $selfPath = dirname($_SERVER['PHP_SELF']);
505  $file = str_replace("\\", "/", __FILE__);
506 
507  if (!$selfPath || !$realPath || !$file) {
508  return "/ckeditor/";
509  }
510 
511  $documentRoot = substr($realPath, 0, strlen($realPath) - strlen($selfPath));
512  $fileUrl = substr($file, strlen($documentRoot));
513  $ckeditorUrl = str_replace("ckeditor_php5.php", "", $fileUrl);
514 
515  return $ckeditorUrl;
516  }
517 
524  private function jsEncode($val)
525  {
526  if (is_null($val)) {
527  return 'null';
528  }
529  if (is_bool($val)) {
530  return $val ? 'true' : 'false';
531  }
532  if (is_int($val)) {
533  return $val;
534  }
535  if (is_float($val)) {
536  return str_replace(',', '.', $val);
537  }
538  if (is_array($val) || is_object($val)) {
539  if (is_array($val) && (array_keys($val) === range(0,count($val)-1))) {
540  return '[' . implode(',', array_map(array($this, 'jsEncode'), $val)) . ']';
541  }
542  $temp = array();
543  foreach ($val as $k => $v){
544  $temp[] = $this->jsEncode("{$k}") . ':' . $this->jsEncode($v);
545  }
546  return '{' . implode(',', $temp) . '}';
547  }
548  // String otherwise
549  if (strpos($val, '@@') === 0)
550  return substr($val, 2);
551  if (strtoupper(substr($val, 0, 9)) == 'CKEDITOR.')
552  return $val;
553 
554  return '"' . str_replace(array("\\", "/", "\n", "\t", "\r", "\x08", "\x0c", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'), $val) . '"';
555  }
556 }
const version
The version of CKEditor.
clearGlobalEventHandlers($event=null)
Clear registered global event handlers.
$textareaAttributes
An array with textarea attributes.
$returnOutput
Boolean variable indicating whether created code should be printed out or returned by a function...
__construct($basePath=null)
Main Constructor.
CKEditor class that can be used to create editor instances in PHP pages on server side...
const timestamp
A constant string unique for each release of CKEditor.
replaceAll($className=null)
Replace all