JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.3.2
[ckeditor.git] / ckeditor_php4.php
1 <?php\r
2 /*\r
3 * Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
4 * For licensing, see LICENSE.html or http://ckeditor.com/license\r
5 */\r
6 \r
7 /**\r
8  * \brief CKEditor class that can be used to create editor\r
9  * instances in PHP pages on server side.\r
10  * @see http://ckeditor.com\r
11  *\r
12  * Sample usage:\r
13  * @code\r
14  * $CKEditor = new CKEditor();\r
15  * $CKEditor->editor("editor1", "<p>Initial value.</p>");\r
16  * @endcode\r
17  */\r
18 class CKEditor\r
19 {\r
20         /**\r
21          * The version of %CKEditor.\r
22          * \private\r
23          */\r
24         var $version = '3.3.2';\r
25         /**\r
26          * A constant string unique for each release of %CKEditor.\r
27          * \private\r
28          */\r
29         var $_timestamp = 'A73H4H9';\r
30 \r
31         /**\r
32          * URL to the %CKEditor installation directory (absolute or relative to document root).\r
33          * If not set, CKEditor will try to guess it's path.\r
34          *\r
35          * Example usage:\r
36          * @code\r
37          * $CKEditor->basePath = '/ckeditor/';\r
38          * @endcode\r
39          */\r
40         var $basePath;\r
41         /**\r
42          * An array that holds the global %CKEditor configuration.\r
43          * For the list of available options, see http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html\r
44          *\r
45          * Example usage:\r
46          * @code\r
47          * $CKEditor->config['height'] = 400;\r
48          * // Use @@ at the beggining of a string to ouput it without surrounding quotes.\r
49          * $CKEditor->config['width'] = '@@screen.width * 0.8';\r
50          * @endcode\r
51          */\r
52         var $config = array();\r
53         /**\r
54          * A boolean variable indicating whether CKEditor has been initialized.\r
55          * Set it to true only if you have already included\r
56          * &lt;script&gt; tag loading ckeditor.js in your website.\r
57          */\r
58         var $initialized = false;\r
59         /**\r
60          * Boolean variable indicating whether created code should be printed out or returned by a function.\r
61          *\r
62          * Example 1: get the code creating %CKEditor instance and print it on a page with the "echo" function.\r
63          * @code\r
64          * $CKEditor = new CKEditor();\r
65          * $CKEditor->returnOutput = true;\r
66          * $code = $CKEditor->editor("editor1", "<p>Initial value.</p>");\r
67          * echo "<p>Editor 1:</p>";\r
68          * echo $code;\r
69          * @endcode\r
70          */\r
71         var $returnOutput = false;\r
72         /**\r
73          * An array with textarea attributes.\r
74          *\r
75          * When %CKEditor is created with the editor() method, a HTML &lt;textarea&gt; element is created,\r
76          * it will be displayed to anyone with JavaScript disabled or with incompatible browser.\r
77          */\r
78         var $textareaAttributes = array( "rows" => 8, "cols" => 60 );\r
79         /**\r
80          * A string indicating the creation date of %CKEditor.\r
81          * Do not change it unless you want to force browsers to not use previously cached version of %CKEditor.\r
82          */\r
83         var $timestamp = "A73H4H9";\r
84         /**\r
85          * An array that holds event listeners.\r
86          * \private\r
87          */\r
88         var $_events = array();\r
89         /**\r
90          * An array that holds global event listeners.\r
91          * \private\r
92          */\r
93         var $_globalEvents = array();\r
94 \r
95         /**\r
96          * Main Constructor.\r
97          *\r
98          *  @param $basePath (string) URL to the %CKEditor installation directory (optional).\r
99          */\r
100         function CKEditor($basePath = null) {\r
101                 if (!empty($basePath)) {\r
102                         $this->basePath = $basePath;\r
103                 }\r
104         }\r
105 \r
106         /**\r
107          * Creates a %CKEditor instance.\r
108          * In incompatible browsers %CKEditor will downgrade to plain HTML &lt;textarea&gt; element.\r
109          *\r
110          * @param $name (string) Name of the %CKEditor instance (this will be also the "name" attribute of textarea element).\r
111          * @param $value (string) Initial value (optional).\r
112          * @param $config (array) The specific configurations to apply to this editor instance (optional).\r
113          * @param $events (array) Event listeners for this editor instance (optional).\r
114          *\r
115          * Example usage:\r
116          * @code\r
117          * $CKEditor = new CKEditor();\r
118          * $CKEditor->editor("field1", "<p>Initial value.</p>");\r
119          * @endcode\r
120          *\r
121          * Advanced example:\r
122          * @code\r
123          * $CKEditor = new CKEditor();\r
124          * $config = array();\r
125          * $config['toolbar'] = array(\r
126          *     array( 'Source', '-', 'Bold', 'Italic', 'Underline', 'Strike' ),\r
127          *     array( 'Image', 'Link', 'Unlink', 'Anchor' )\r
128          * );\r
129          * $events['instanceReady'] = 'function (ev) {\r
130          *     alert("Loaded: " + ev.editor.name);\r
131          * }';\r
132          * $CKEditor->editor("field1", "<p>Initial value.</p>", $config, $events);\r
133          * @endcode\r
134          */\r
135         function editor($name, $value = "", $config = array(), $events = array())\r
136         {\r
137                 $attr = "";\r
138                 foreach ($this->textareaAttributes as $key => $val) {\r
139                         $attr.= " " . $key . '="' . str_replace('"', '&quot;', $val) . '"';\r
140                 }\r
141                 $out = "<textarea name=\"" . $name . "\"" . $attr . ">" . htmlspecialchars($value) . "</textarea>\n";\r
142                 if (!$this->initialized) {\r
143                         $out .= $this->init();\r
144                 }\r
145 \r
146                 $_config = $this->configSettings($config, $events);\r
147 \r
148                 $js = $this->returnGlobalEvents();\r
149                 if (!empty($_config))\r
150                         $js .= "CKEDITOR.replace('".$name."', ".$this->jsEncode($_config).");";\r
151                 else\r
152                         $js .= "CKEDITOR.replace('".$name."');";\r
153 \r
154                 $out .= $this->script($js);\r
155 \r
156                 if (!$this->returnOutput) {\r
157                         print $out;\r
158                         $out = "";\r
159                 }\r
160 \r
161                 return $out;\r
162         }\r
163 \r
164         /**\r
165          * Replaces a &lt;textarea&gt; with a %CKEditor instance.\r
166          *\r
167          * @param $id (string) The id or name of textarea element.\r
168          * @param $config (array) The specific configurations to apply to this editor instance (optional).\r
169          * @param $events (array) Event listeners for this editor instance (optional).\r
170          *\r
171          * Example 1: adding %CKEditor to &lt;textarea name="article"&gt;&lt;/textarea&gt; element:\r
172          * @code\r
173          * $CKEditor = new CKEditor();\r
174          * $CKEditor->replace("article");\r
175          * @endcode\r
176          */\r
177         function replace($id, $config = array(), $events = array())\r
178         {\r
179                 $out = "";\r
180                 if (!$this->initialized) {\r
181                         $out .= $this->init();\r
182                 }\r
183 \r
184                 $_config = $this->configSettings($config, $events);\r
185 \r
186                 $js = $this->returnGlobalEvents();\r
187                 if (!empty($_config)) {\r
188                         $js .= "CKEDITOR.replace('".$id."', ".$this->jsEncode($_config).");";\r
189                 }\r
190                 else {\r
191                         $js .= "CKEDITOR.replace('".$id."');";\r
192                 }\r
193                 $out .= $this->script($js);\r
194 \r
195                 if (!$this->returnOutput) {\r
196                         print $out;\r
197                         $out = "";\r
198                 }\r
199 \r
200                 return $out;\r
201         }\r
202 \r
203         /**\r
204          * Replace all &lt;textarea&gt; elements available in the document with editor instances.\r
205          *\r
206          * @param $className (string) If set, replace all textareas with class className in the page.\r
207          *\r
208          * Example 1: replace all &lt;textarea&gt; elements in the page.\r
209          * @code\r
210          * $CKEditor = new CKEditor();\r
211          * $CKEditor->replaceAll();\r
212          * @endcode\r
213          *\r
214          * Example 2: replace all &lt;textarea class="myClassName"&gt; elements in the page.\r
215          * @code\r
216          * $CKEditor = new CKEditor();\r
217          * $CKEditor->replaceAll( 'myClassName' );\r
218          * @endcode\r
219          */\r
220         function replaceAll($className = null)\r
221         {\r
222                 $out = "";\r
223                 if (!$this->initialized) {\r
224                         $out .= $this->init();\r
225                 }\r
226 \r
227                 $_config = $this->configSettings();\r
228 \r
229                 $js = $this->returnGlobalEvents();\r
230                 if (empty($_config)) {\r
231                         if (empty($className)) {\r
232                                 $js .= "CKEDITOR.replaceAll();";\r
233                         }\r
234                         else {\r
235                                 $js .= "CKEDITOR.replaceAll('".$className."');";\r
236                         }\r
237                 }\r
238                 else {\r
239                         $classDetection = "";\r
240                         $js .= "CKEDITOR.replaceAll( function(textarea, config) {\n";\r
241                         if (!empty($className)) {\r
242                                 $js .= "        var classRegex = new RegExp('(?:^| )' + '". $className ."' + '(?:$| )');\n";\r
243                                 $js .= "        if (!classRegex.test(textarea.className))\n";\r
244                                 $js .= "                return false;\n";\r
245                         }\r
246                         $js .= "        CKEDITOR.tools.extend(config, ". $this->jsEncode($_config) .", true);";\r
247                         $js .= "} );";\r
248 \r
249                 }\r
250 \r
251                 $out .= $this->script($js);\r
252 \r
253                 if (!$this->returnOutput) {\r
254                         print $out;\r
255                         $out = "";\r
256                 }\r
257 \r
258                 return $out;\r
259         }\r
260 \r
261         /**\r
262          * Adds event listener.\r
263          * Events are fired by %CKEditor in various situations.\r
264          *\r
265          * @param $event (string) Event name.\r
266          * @param $javascriptCode (string) Javascript anonymous function or function name.\r
267          *\r
268          * Example usage:\r
269          * @code\r
270          * $CKEditor->addEventHandler('instanceReady', 'function (ev) {\r
271          *     alert("Loaded: " + ev.editor.name);\r
272          * }');\r
273          * @endcode\r
274          */\r
275         function addEventHandler($event, $javascriptCode)\r
276         {\r
277                 if (!isset($this->_events[$event])) {\r
278                         $this->_events[$event] = array();\r
279                 }\r
280                 // Avoid duplicates.\r
281                 if (!in_array($javascriptCode, $this->_events[$event])) {\r
282                         $this->_events[$event][] = $javascriptCode;\r
283                 }\r
284         }\r
285 \r
286         /**\r
287          * Clear registered event handlers.\r
288          * Note: this function will have no effect on already created editor instances.\r
289          *\r
290          * @param $event (string) Event name, if not set all event handlers will be removed (optional).\r
291          */\r
292         function clearEventHandlers($event = null)\r
293         {\r
294                 if (!empty($event)) {\r
295                         $this->_events[$event] = array();\r
296                 }\r
297                 else {\r
298                         $this->_events = array();\r
299                 }\r
300         }\r
301 \r
302         /**\r
303          * Adds global event listener.\r
304          *\r
305          * @param $event (string) Event name.\r
306          * @param $javascriptCode (string) Javascript anonymous function or function name.\r
307          *\r
308          * Example usage:\r
309          * @code\r
310          * $CKEditor->addGlobalEventHandler('dialogDefinition', 'function (ev) {\r
311          *     alert("Loading dialog: " + ev.data.name);\r
312          * }');\r
313          * @endcode\r
314          */\r
315         function addGlobalEventHandler($event, $javascriptCode)\r
316         {\r
317                 if (!isset($this->_globalEvents[$event])) {\r
318                         $this->_globalEvents[$event] = array();\r
319                 }\r
320                 // Avoid duplicates.\r
321                 if (!in_array($javascriptCode, $this->_globalEvents[$event])) {\r
322                         $this->_globalEvents[$event][] = $javascriptCode;\r
323                 }\r
324         }\r
325 \r
326         /**\r
327          * Clear registered global event handlers.\r
328          * Note: this function will have no effect if the event handler has been already printed/returned.\r
329          *\r
330          * @param $event (string) Event name, if not set all event handlers will be removed (optional).\r
331          */\r
332         function clearGlobalEventHandlers($event = null)\r
333         {\r
334                 if (!empty($event)) {\r
335                         $this->_globalEvents[$event] = array();\r
336                 }\r
337                 else {\r
338                         $this->_globalEvents = array();\r
339                 }\r
340         }\r
341 \r
342         /**\r
343          * Prints javascript code.\r
344          * \private\r
345          *\r
346          * @param string $js\r
347          */\r
348         function script($js)\r
349         {\r
350                 $out = "<script type=\"text/javascript\">";\r
351                 $out .= "//<![CDATA[\n";\r
352                 $out .= $js;\r
353                 $out .= "\n//]]>";\r
354                 $out .= "</script>\n";\r
355 \r
356                 return $out;\r
357         }\r
358 \r
359         /**\r
360          * Returns the configuration array (global and instance specific settings are merged into one array).\r
361          * \private\r
362          *\r
363          * @param $config (array) The specific configurations to apply to editor instance.\r
364          * @param $events (array) Event listeners for editor instance.\r
365          */\r
366         function configSettings($config = array(), $events = array())\r
367         {\r
368                 $_config = $this->config;\r
369                 $_events = $this->_events;\r
370 \r
371                 if (is_array($config) && !empty($config)) {\r
372                         $_config = array_merge($_config, $config);\r
373                 }\r
374 \r
375                 if (is_array($events) && !empty($events)) {\r
376                         foreach ($events as $eventName => $code) {\r
377                                 if (!isset($_events[$eventName])) {\r
378                                         $_events[$eventName] = array();\r
379                                 }\r
380                                 if (!in_array($code, $_events[$eventName])) {\r
381                                         $_events[$eventName][] = $code;\r
382                                 }\r
383                         }\r
384                 }\r
385 \r
386                 if (!empty($_events)) {\r
387                         foreach($_events as $eventName => $handlers) {\r
388                                 if (empty($handlers)) {\r
389                                         continue;\r
390                                 }\r
391                                 else if (count($handlers) == 1) {\r
392                                         $_config['on'][$eventName] = '@@'.$handlers[0];\r
393                                 }\r
394                                 else {\r
395                                         $_config['on'][$eventName] = '@@function (ev){';\r
396                                         foreach ($handlers as $handler => $code) {\r
397                                                 $_config['on'][$eventName] .= '('.$code.')(ev);';\r
398                                         }\r
399                                         $_config['on'][$eventName] .= '}';\r
400                                 }\r
401                         }\r
402                 }\r
403 \r
404                 return $_config;\r
405         }\r
406 \r
407         /**\r
408          * Return global event handlers.\r
409          * \private\r
410          */\r
411         function returnGlobalEvents()\r
412         {\r
413                 static $returnedEvents;\r
414                 $out = "";\r
415 \r
416                 if (!isset($returnedEvents)) {\r
417                         $returnedEvents = array();\r
418                 }\r
419 \r
420                 if (!empty($this->_globalEvents)) {\r
421                         foreach ($this->_globalEvents as $eventName => $handlers) {\r
422                                 foreach ($handlers as $handler => $code) {\r
423                                         if (!isset($returnedEvents[$eventName])) {\r
424                                                 $returnedEvents[$eventName] = array();\r
425                                         }\r
426                                         // Return only new events\r
427                                         if (!in_array($code, $returnedEvents[$eventName])) {\r
428                                                 $out .= ($code ? "\n" : "") . "CKEDITOR.on('". $eventName ."', $code);";\r
429                                                 $returnedEvents[$eventName][] = $code;\r
430                                         }\r
431                                 }\r
432                         }\r
433                 }\r
434 \r
435                 return $out;\r
436         }\r
437 \r
438         /**\r
439          * Initializes CKEditor (executed only once).\r
440          * \private\r
441          */\r
442         function init()\r
443         {\r
444                 static $initComplete;\r
445                 $out = "";\r
446 \r
447                 if (!empty($initComplete)) {\r
448                         return "";\r
449                 }\r
450 \r
451                 if ($this->initialized) {\r
452                         $initComplete = true;\r
453                         return "";\r
454                 }\r
455 \r
456                 $args = "";\r
457                 $ckeditorPath = $this->ckeditorPath();\r
458 \r
459                 if (!empty($this->timestamp) && $this->timestamp != "%"."TIMESTAMP%") {\r
460                         $args = '?t=' . $this->timestamp;\r
461                 }\r
462 \r
463                 // Skip relative paths...\r
464                 if (strpos($ckeditorPath, '..') !== 0) {\r
465                         $out .= $this->script("window.CKEDITOR_BASEPATH='". $ckeditorPath ."';");\r
466                 }\r
467 \r
468                 $out .= "<script type=\"text/javascript\" src=\"" . $ckeditorPath . 'ckeditor.js' . $args . "\"></script>\n";\r
469 \r
470                 $extraCode = "";\r
471                 if ($this->timestamp != $this->_timestamp) {\r
472                         $extraCode .= ($extraCode ? "\n" : "") . "CKEDITOR.timestamp = '". $this->timestamp ."';";\r
473                 }\r
474                 if ($extraCode) {\r
475                         $out .= $this->script($extraCode);\r
476                 }\r
477 \r
478                 $initComplete = $this->initialized = true;\r
479 \r
480                 return $out;\r
481         }\r
482 \r
483         /**\r
484          * Return path to ckeditor.js.\r
485          * \private\r
486          */\r
487         function ckeditorPath()\r
488         {\r
489                 if (!empty($this->basePath)) {\r
490                         return $this->basePath;\r
491                 }\r
492 \r
493                 /**\r
494                  * The absolute pathname of the currently executing script.\r
495                  * Note: If a script is executed with the CLI, as a relative path, such as file.php or ../file.php,\r
496                  * $_SERVER['SCRIPT_FILENAME'] will contain the relative path specified by the user.\r
497                  */\r
498                 if (isset($_SERVER['SCRIPT_FILENAME'])) {\r
499                         $realPath = dirname($_SERVER['SCRIPT_FILENAME']);\r
500                 }\r
501                 else {\r
502                         /**\r
503                          * realpath — Returns canonicalized absolute pathname\r
504                          */\r
505                         $realPath = realpath( './' ) ;\r
506                 }\r
507 \r
508                 /**\r
509                  * The filename of the currently executing script, relative to the document root.\r
510                  * For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar\r
511                  * would be /test.php/foo.bar.\r
512                  */\r
513                 $selfPath = dirname($_SERVER['PHP_SELF']);\r
514                 $file = str_replace("\\", "/", __FILE__);\r
515 \r
516                 if (!$selfPath || !$realPath || !$file) {\r
517                         return "/ckeditor/";\r
518                 }\r
519 \r
520                 $documentRoot = substr($realPath, 0, strlen($realPath) - strlen($selfPath));\r
521                 $fileUrl = substr($file, strlen($documentRoot));\r
522                 $ckeditorUrl = str_replace("ckeditor_php5.php", "", $fileUrl);\r
523 \r
524                 return $ckeditorUrl;\r
525         }\r
526 \r
527         /**\r
528          * This little function provides a basic JSON support.\r
529          * http://php.net/manual/en/function.json-encode.php\r
530          * \private\r
531          *\r
532          * @param mixed $val\r
533          * @return string\r
534          */\r
535         function jsEncode($val)\r
536         {\r
537                 if (is_null($val)) {\r
538                         return 'null';\r
539                 }\r
540                 if ($val === false) {\r
541                         return 'false';\r
542                 }\r
543                 if ($val === true) {\r
544                         return 'true';\r
545                 }\r
546                 if (is_scalar($val))\r
547                 {\r
548                         if (is_float($val))\r
549                         {\r
550                                 // Always use "." for floats.\r
551                                 $val = str_replace(",", ".", strval($val));\r
552                         }\r
553 \r
554                         // Use @@ to not use quotes when outputting string value\r
555                         if (strpos($val, '@@') === 0) {\r
556                                 return substr($val, 2);\r
557                         }\r
558                         else {\r
559                                 // All scalars are converted to strings to avoid indeterminism.\r
560                                 // PHP's "1" and 1 are equal for all PHP operators, but\r
561                                 // JS's "1" and 1 are not. So if we pass "1" or 1 from the PHP backend,\r
562                                 // we should get the same result in the JS frontend (string).\r
563                                 // Character replacements for JSON.\r
564                                 static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'),\r
565                                 array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));\r
566 \r
567                                 $val = str_replace($jsonReplaces[0], $jsonReplaces[1], $val);\r
568 \r
569                                 return '"' . $val . '"';\r
570                         }\r
571                 }\r
572                 $isList = true;\r
573                 for ($i = 0, reset($val); $i < count($val); $i++, next($val))\r
574                 {\r
575                         if (key($val) !== $i)\r
576                         {\r
577                                 $isList = false;\r
578                                 break;\r
579                         }\r
580                 }\r
581                 $result = array();\r
582                 if ($isList)\r
583                 {\r
584                         foreach ($val as $v) $result[] = $this->jsEncode($v);\r
585                         return '[ ' . join(', ', $result) . ' ]';\r
586                 }\r
587                 else\r
588                 {\r
589                         foreach ($val as $k => $v) $result[] = $this->jsEncode($k).': '.$this->jsEncode($v);\r
590                         return '{ ' . join(', ', $result) . ' }';\r
591                 }\r
592         }\r
593 }\r