Ticket #2872: 2872.patch

File 2872.patch, 23.3 KB (added by Wiktor Walc, 14 years ago)
  • _samples/php/advanced.php

     
     1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2<!--
     3Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
     4For licensing, see LICENSE.html or http://ckeditor.com/license
     5-->
     6<html xmlns="http://www.w3.org/1999/xhtml">
     7<head>
     8        <title>Sample - CKEditor</title>
     9        <meta content="text/html; charset=utf-8" http-equiv="content-type"/>
     10        <link href="../sample.css" rel="stylesheet" type="text/css"/>
     11</head>
     12<body>
     13        <h1>
     14                CKEditor Sample
     15        </h1>
     16        <!-- This <div> holds alert messages to be display in the sample page. -->
     17        <div id="alerts">
     18                <noscript>
     19                        <p>
     20                                <strong>CKEditor requires JavaScript to run</strong>. In a browser with no JavaScript
     21                                support, like yours, you should still see the contents (HTML data) and you should
     22                                be able to edit it normally, without a rich editor interface.
     23                        </p>
     24                </noscript>
     25        </div>
     26        <!-- This <fieldset> holds the HTML that you will usually find in your
     27             pages. -->
     28        <fieldset title="Output">
     29                <legend>Output</legend>
     30                <form action="../sample_posteddata.php" method="post">
     31                        <p>
     32                                <label>Editor 1:</label><br/>
     33                        </p>
     34<?php
     35// Include CKEditor class.
     36include("../../ckeditor.php");
     37
     38// The initial value to be displayed in the editor.
     39 $initialValue = '<p>This is some <strong>sample text</strong>.</p>';
     40
     41// Create class instance.
     42$CKEditor = new CKEditor();
     43
     44// Path to CKEditor directory, ideally instead of relative dir, use an absolute path:
     45//   $CKEditor->basePath = '/ckeditor/'
     46// If not set, CKEditor will try to detect the correct path.
     47$CKEditor->basePath = '../../';
     48
     49// Set global configuration (will be used by all instances of CKEditor).
     50$CKEditor->config['width'] = 600;
     51
     52// The initial value to be displayed in the editor.
     53$initialValue = '<p>This is some <strong>sample text</strong>. You are using <a href="http://ckeditor.com/">CKEditor</a>.</p>';
     54
     55// Create first instance.
     56echo $CKEditor->textarea("editor1", $initialValue, array("cols" => 60, "rows" => 8));
     57?>
     58                        <p>
     59                                <label>Editor 2:</label><br/>
     60                        </p>
     61<?php
     62// Configuration that will be used only by the second editor.
     63$config['toolbar'] = array(
     64        array( 'Source', '-', 'Bold', 'Italic', 'Underline', 'Strike' ),
     65        array( 'Image', 'Link', 'Unlink', 'Anchor' )
     66);
     67
     68// Events that will be handled only by the second editor.
     69$events['instanceReady'] = 'function (ev) {
     70        alert("Loaded: " + ev.editor.name);
     71}';
     72
     73// Create second instance.
     74echo $CKEditor->textarea("editor2", $initialValue, array("cols" => 60, "rows" => 8), $config, $events);
     75?>
     76                                <input type="submit" value="Submit"/>
     77                        </p>
     78                </form>
     79        </fieldset>
     80        <div id="footer">
     81                <hr/>
     82                <p>
     83                        CKEditor - The text editor for Internet - <a href="http://ckeditor.com/" shape="rect">http://ckeditor.com</a>
     84                </p>
     85                <p id="copy">
     86                        Copyright © 2003-2009, <a href="http://cksource.com/" shape="rect">CKSource</a> - Frederico Knabben. All rights reserved.
     87                </p>
     88        </div>
     89</body>
     90</html>
  • _samples/php/replace.php

     
     1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2<!--
     3Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
     4For licensing, see LICENSE.html or http://ckeditor.com/license
     5-->
     6<html xmlns="http://www.w3.org/1999/xhtml">
     7<head>
     8        <title>Sample - CKEditor</title>
     9        <meta content="text/html; charset=utf-8" http-equiv="content-type"/>
     10        <link href="../sample.css" rel="stylesheet" type="text/css"/>
     11</head>
     12<body>
     13        <h1>
     14                CKEditor Sample
     15        </h1>
     16        <!-- This <div> holds alert messages to be display in the sample page. -->
     17        <div id="alerts">
     18                <noscript>
     19                        <p>
     20                                <strong>CKEditor requires JavaScript to run</strong>. In a browser with no JavaScript
     21                                support, like yours, you should still see the contents (HTML data) and you should
     22                                be able to edit it normally, without a rich editor interface.
     23                        </p>
     24                </noscript>
     25        </div>
     26        <!-- This <fieldset> holds the HTML that you will usually find in your pages. -->
     27        <fieldset title="Output">
     28                <legend>Output</legend>
     29                <form action="../sample_posteddata.php" method="post">
     30                        <p>
     31                                <label for="editor1">
     32                                        Editor 1:</label><br/>
     33                                <textarea cols="80" id="editor1" name="editor1" rows="10">&lt;p&gt;This is some &lt;strong&gt;sample text&lt;/strong&gt;. You are using &lt;a href="http://ckeditor.com/"&gt;CKEditor&lt;/a&gt;.&lt;/p&gt;</textarea>
     34                        </p>
     35                        <p>
     36                                <input type="submit" value="Submit"/>
     37                        </p>
     38                </form>
     39        </fieldset>
     40        <div id="footer">
     41                <hr/>
     42                <p>
     43                        CKEditor - The text editor for Internet - <a href="http://ckeditor.com/" shape="rect">http://ckeditor.com</a>
     44                </p>
     45                <p id="copy">
     46                        Copyright © 2003-2009, <a href="http://cksource.com/" shape="rect">CKSource</a> - Frederico Knabben. All rights reserved.
     47                </p>
     48        </div>
     49        <?php
     50        // Include CKEditor class.
     51        include_once "../../ckeditor.php";
     52        // Create class instance.
     53        $CKEditor = new CKEditor();
     54        // Path to CKEditor directory, ideally instead of relative dir, use an absolute path:
     55        //   $CKEditor->basePath = '/ckeditor/'
     56        // If not set, CKEditor will try to detect the correct path.
     57        $CKEditor->basePath = '../../';
     58        // Replace textarea with id (or name) "editor1".
     59        echo $CKEditor->replace("editor1");
     60        ?>
     61</body>
     62</html>
  • _samples/php/replaceall.php

     
     1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2<!--
     3Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
     4For licensing, see LICENSE.html or http://ckeditor.com/license
     5-->
     6<html xmlns="http://www.w3.org/1999/xhtml">
     7<head>
     8        <title>Sample - CKEditor</title>
     9        <meta content="text/html; charset=utf-8" http-equiv="content-type"/>
     10        <link href="../sample.css" rel="stylesheet" type="text/css"/>
     11</head>
     12<body>
     13        <h1>
     14                CKEditor Sample
     15        </h1>
     16        <!-- This <div> holds alert messages to be display in the sample page. -->
     17        <div id="alerts">
     18                <noscript>
     19                        <p>
     20                                <strong>CKEditor requires JavaScript to run</strong>. In a browser with no JavaScript
     21                                support, like yours, you should still see the contents (HTML data) and you should
     22                                be able to edit it normally, without a rich editor interface.
     23                        </p>
     24                </noscript>
     25        </div>
     26        <!-- This <fieldset> holds the HTML that you will usually find in your pages. -->
     27        <fieldset title="Output">
     28                <legend>Output</legend>
     29                <form action="../sample_posteddata.php" method="post">
     30                        <p>
     31                                <label for="editor1">
     32                                        Editor 1:</label><br/>
     33                                <textarea cols="80" id="editor1" name="editor1" rows="10">&lt;p&gt;This is some &lt;strong&gt;sample text&lt;/strong&gt;. You are using &lt;a href="http://ckeditor.com/"&gt;CKEditor&lt;/a&gt;.&lt;/p&gt;</textarea>
     34                        </p>
     35                        <p>
     36                                <label for="editor2">
     37                                        Editor 2:</label><br/>
     38                                <textarea cols="80" id="editor2" name="editor2" rows="10">&lt;p&gt;This is some &lt;strong&gt;sample text&lt;/strong&gt;. You are using &lt;a href="http://ckeditor.com/"&gt;CKEditor&lt;/a&gt;.&lt;/p&gt;</textarea>
     39                        </p>
     40                        <p>
     41                                <input type="submit" value="Submit"/>
     42                        </p>
     43                </form>
     44        </fieldset>
     45        <div id="footer">
     46                <hr/>
     47                <p>
     48                        CKEditor - The text editor for Internet - <a href="http://ckeditor.com/" shape="rect">http://ckeditor.com</a>
     49                </p>
     50                <p id="copy">
     51                        Copyright © 2003-2009, <a href="http://cksource.com/" shape="rect">CKSource</a> - Frederico Knabben. All rights reserved.
     52                </p>
     53        </div>
     54        <?php
     55        // Include CKEditor class.
     56        include("../../ckeditor.php");
     57        // Create class instance.
     58        $CKEditor = new CKEditor();
     59        // Path to CKEditor directory, ideally instead of relative dir, use an absolute path:
     60        //   $CKEditor->basePath = '/ckeditor/'
     61        // If not set, CKEditor will try to detect the correct path.
     62        $CKEditor->basePath = '../../';
     63        // Replace all textareas with CKEditor.
     64        echo $CKEditor->replaceAll();
     65        ?>
     66</body>
     67</html>
  • _samples/php/standalone.php

     
     1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2<!--
     3Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
     4For licensing, see LICENSE.html or http://ckeditor.com/license
     5-->
     6<html xmlns="http://www.w3.org/1999/xhtml">
     7<head>
     8        <title>Sample - CKEditor</title>
     9        <meta content="text/html; charset=utf-8" http-equiv="content-type"/>
     10        <link href="../sample.css" rel="stylesheet" type="text/css"/>
     11</head>
     12<body>
     13        <h1>
     14                CKEditor Sample
     15        </h1>
     16        <!-- This <div> holds alert messages to be display in the sample page. -->
     17        <div id="alerts">
     18                <noscript>
     19                        <p>
     20                                <strong>CKEditor requires JavaScript to run</strong>. In a browser with no JavaScript
     21                                support, like yours, you should still see the contents (HTML data) and you should
     22                                be able to edit it normally, without a rich editor interface.
     23                        </p>
     24                </noscript>
     25        </div>
     26        <!-- This <fieldset> holds the HTML that you will usually find in your pages. -->
     27        <fieldset title="Output">
     28                <legend>Output</legend>
     29                <form action="../sample_posteddata.php" method="post">
     30                        <p>
     31                                <label for="editor1">
     32                                        Editor 1:</label><br/>
     33                        </p>
     34                        <p>
     35                        <?php
     36                                // Include CKEditor class.
     37                                include_once "../../ckeditor.php";
     38                                // The initial value to be displayed in the editor.
     39                                $initialValue = '<p>This is some <strong>sample text</strong>.</p>';
     40                                // Create class instance.
     41                                $CKEditor = new CKEditor();
     42                                // Path to CKEditor directory, ideally instead of relative dir, use an absolute path:
     43                                //   $CKEditor->basePath = '/ckeditor/'
     44                                // If not set, CKEditor will try to detect the correct path.
     45                                $CKEditor->basePath = '../../';
     46                                // Create textarea element and attach CKEditor to it.
     47                                echo $CKEditor->textarea("editor1", $initialValue, array("cols" => 60, "rows" => 8));
     48                        ?>
     49                                <input type="submit" value="Submit"/>
     50                        </p>
     51                </form>
     52        </fieldset>
     53        <div id="footer">
     54                <hr/>
     55                <p>
     56                        CKEditor - The text editor for Internet - <a href="http://ckeditor.com/" shape="rect">http://ckeditor.com</a>
     57                </p>
     58                <p id="copy">
     59                        Copyright © 2003-2009, <a href="http://cksource.com/" shape="rect">CKSource</a> - Frederico Knabben. All rights reserved.
     60                </p>
     61        </div>
     62</body>
     63</html>
  • ckeditor.php

     
     1<?php
     2/*
     3 * Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
     4 * For licensing, see LICENSE.html or http://ckeditor.com/license
     5 */
     6
     7/*! \mainpage CKEditor - PHP server side intergation
     8 * \section intro_sec CKEditor
     9 * Visit <a href="http://ckeditor.com">CKEditor web site</a> to find more information about the editor.
     10 * \section install_sec Installation
     11 * \subsection step1 Include ckeditor.php in your PHP web site.
     12 * @code
     13 * <?php
     14 * include("ckeditor/ckeditor.php");
     15 * ?>
     16 * @endcode
     17 * \subsection step2 Create CKEditor class instance and use one of available methods to insert CKEditor.
     18 * @code
     19 * <?php
     20 * $CKEditor = new CKEditor();
     21 * echo $CKEditor->textarea("field1", "<p>Initial value.</p>");
     22 * ?>
     23 * @endcode
     24 */
     25
     26if ( !function_exists('version_compare') || version_compare( phpversion(), '5', '<' ) )
     27        include_once( 'ckeditor_php4.php' ) ;
     28else
     29        include_once( 'ckeditor_php5.php' ) ;
  • ckeditor_php5.php

     
     1<?php
     2/*
     3* Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
     4* For licensing, see LICENSE.html or http://ckeditor.com/license
     5*/
     6 
     7/**
     8 * \brief CKEditor class that can be used to create editor
     9 * instances in PHP pages on server side.
     10 * @see http://ckeditor.com
     11 *
     12 * Sample usage:
     13 * @code
     14 * $CKEditor = new CKEditor();
     15 * echo $CKEditor->textarea("editor1", "<p>Initial value.</p>");
     16 * @endcode
     17 */
     18class CKEditor
     19{
     20        /**
     21         * The version of %CKEditor.
     22         */
     23        const version = '%VERSION%';
     24        /**
     25         * A constant string unique for each release of %CKEditor.
     26         */
     27        const timestamp = '%TIMESTAMP%';
     28
     29        /**
     30         * URL to the %CKEditor installation directory (absolute or relative to document root).
     31         * If not set, CKEditor will try to guess it's path.
     32         *
     33         * Example usage:
     34         * @code
     35         * $CKEditor->basePath = '/ckeditor/';
     36         * @endcode
     37         */
     38        public $basePath;
     39        /**
     40         * A string indicating the creation date of %CKEditor.
     41         * Do not change it unless you want to force browsers to not use previously cached version of %CKEditor.
     42         */
     43        public $timestamp = "%TIMESTAMP%";
     44        /**
     45         * An array that holds the global %CKEditor configuration.
     46         * For the list of available options, see http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html
     47         *
     48         * Example usage:
     49         * @code
     50         * $CKEditor->config['height'] = 400;
     51         * // Use @@ at the beggining of a string to ouput it without surrounding quotes.
     52         * $CKEditor->config['width'] = '@@screen.width * 0.8';
     53         * @endcode
     54         */
     55        public $config = array();
     56        /**
     57         * An array that holds event listeners.
     58         * Events are fired by %CKEditor in various situations.
     59         *
     60         * Example usage:
     61         * @code
     62         * $CKEditor->events['dialogDefinition'] = 'function (evt) {
     63         *     alert("Loading dialog: " + ev.data.name);
     64         * }';
     65         * @endcode
     66         */
     67        public $events = array();
     68        /**
     69         * A boolean variable indicating whether CKEditor has been initialized.
     70         * Set it to true only if you have already included
     71         * &lt;script&gt; tag loading ckeditor.js in your website.
     72         */
     73        public $initialized = false;
     74
     75        /**
     76         * Main Constructor.
     77         *
     78         *  @param $basePath (string) URL to the %CKEditor installation directory (optional).
     79         */
     80        function __construct($basePath = null) {
     81                // CKReleaser %REMOVE_START%
     82                if (self::version == '%VERSION%') {
     83                        throw new Exception("PHP SSI can be only used with compiled version of CKEditor.");
     84                }
     85                // %REMOVE_END%
     86                if (!empty($basePath)) {
     87                        $this->basePath = $basePath;
     88                }
     89        }
     90
     91        /**
     92         * Creates a &lt;textarea&gt; element with %CKEditor enabled on it.
     93         *
     94         * @param $name (string) Name of the %CKEditor instance (this will be also the "name" attribute of textarea element).
     95         * @param $value (string) Initial value (optional).
     96         * @param $attributes (array) An array with textarea attributes (optional).
     97         * @param $config (array) The specific configurations to apply to this editor instance (optional).
     98         * @param $events (array) Event listeners for this editor instance (optional).
     99         *
     100         * Example usage:
     101         * @code
     102         * $CKEditor = new CKEditor();
     103         * echo $CKEditor->textarea("field1", "<p>Initial value.</p>");
     104         * @endcode
     105         *
     106         * Advanced example:
     107         * @code
     108         * $CKEditor = new CKEditor();
     109         * $config = array();
     110         * $config['toolbar'] = array(
     111         *     array( 'Source', '-', 'Bold', 'Italic', 'Underline', 'Strike' ),
     112         *     array( 'Image', 'Link', 'Unlink', 'Anchor' )
     113         * );
     114         * $events['instanceReady'] = 'function (evt) {
     115         *     alert("Loaded: " + evt.editor.name);
     116         * }';
     117         * echo $CKEditor->textarea("field1", "<p>Initial value.</p>", array("cols" => 60, "rows" => 8), $config, $events);
     118         * @endcode
     119         */
     120        public function textarea($name, $value = "", $attributes = array(), $config = array(), $events = array())
     121        {
     122                foreach ($attributes as $key => $val) {
     123                        $attr.= " " . $key . '="' . str_replace('"', '&quot;', $val) . '"';
     124                }
     125                $out = '<textarea name="' . $name . '"' . $attr . '>' . htmlspecialchars($value) . '</textarea>';
     126                if (!$this->initialized) {
     127                        $out .= $this->init();
     128                }
     129
     130                if (!empty($events)) {
     131                        foreach($events as $event => $code) {
     132                                $config['on'][$event] = '@@'.$code;
     133                        }
     134                }
     135                if (!empty($config))
     136                        $code = "CKEDITOR.replace('".$name."', ".$this->jsEncode($config).");";
     137                else
     138                        $code = "CKEDITOR.replace('".$name."');";
     139
     140                $out .= $this->script($code);
     141
     142                return $out;
     143        }
     144
     145        /**
     146         * Replaces a &lt;textarea&gt; with a %CKEditor instance.
     147         *
     148         * @param $id (string) The id or name of textarea element.
     149         * @param $config (array) The specific configurations to apply to this editor instance (optional).
     150         * @param $events (array) Event listeners for this editor instance (optional).
     151         *
     152         * Example 1: adding %CKEditor to &lt;textarea name="article"&gt;&lt;/textarea&gt; element:
     153         * @code
     154         * $CKEditor = new CKEditor();
     155         * echo $CKEditor->replace("article");
     156         * @endcode
     157         */
     158        public function replace($id, $config = array(), $events = array())
     159        {
     160                $out = "";
     161                if (!$this->initialized) {
     162                        $out .= $this->init();
     163                }
     164                if (!empty($events)) {
     165                        foreach($events as $event => $code) {
     166                                $config['on'][$event] = '@@'.$code;
     167                        }
     168                }
     169                if (!empty($config)) {
     170                        $js = "CKEDITOR.replace('".$id."', ".$this->jsEncode($config).");";
     171                }
     172                else {
     173                        $js = "CKEDITOR.replace('".$id."');";
     174                }
     175                $out .= $this->script($js);
     176                return $out;
     177        }
     178
     179        /**
     180         * Replace all &lt;textarea&gt; elements available in the document with editor instances.
     181         *
     182         * @param $className (string) If set, replace all textareas with class className in the page.
     183         *
     184         * Example 1: replace all &lt;textarea&gt; elements in the page.
     185         * @code
     186         * $CKEditor = new CKEditor();
     187         * echo $CKEditor->replaceAll();
     188         * @endcode
     189         *
     190         * Example 2: replace all &lt;textarea class="myClassName"&gt; elements in the page.
     191         * @code
     192         * $CKEditor = new CKEditor();
     193         * echo $CKEditor->replaceAll( 'myClassName' );
     194         * @endcode
     195         */
     196        public function replaceAll($className = null)
     197        {
     198                $out = "";
     199                if (!$this->initialized) {
     200                        $out .= $this->init();
     201                }
     202                if (!empty($className)) {
     203                        $js = "CKEDITOR.replaceAll('".$className."');";
     204                }
     205                else {
     206                        $js = "CKEDITOR.replaceAll();";
     207                }
     208                $out .= $this->script($js);
     209                return $out;
     210        }
     211
     212        /**
     213         * Prints javascript code.
     214         *
     215         * @param string $js
     216         */
     217        private function script($js)
     218        {
     219                $out = "<script type=\"text/javascript\">";
     220                $out .= "//<![CDATA[\n";
     221                $out .= $js;
     222                $out .= "\n//]]>";
     223                $out .= "</script>\n";
     224
     225                return $out;
     226        }
     227
     228        private function init()
     229        {
     230                if ($this->initialized) {
     231                        return "";
     232                }
     233
     234                $this->initialized = true;
     235
     236                $args = "";
     237                $ckeditorPath = $this->ckeditorPath();
     238
     239                if (!empty($this->timestamp) && $this->timestamp != "%TIMESTAMP%") {
     240                        $args = '?t=' . $this->timestamp;
     241                }
     242
     243                $out = "";
     244                // Skip relative paths...
     245                if (strpos($ckeditorPath, '..') !== 0) {
     246                        $out .= $this->script("window.CKEDITOR_BASEPATH='". $ckeditorPath ."';");
     247                }
     248
     249                $out .= "<script type=\"text/javascript\" src=\"" . $ckeditorPath . 'ckeditor.js' . $args . "\"></script>\n";
     250
     251                $extraCode = "";
     252                if (!empty($this->timestamp) && $this->timestamp != self::timestamp) {
     253                        $extraCode .= "CKEDITOR.timestamp = '". $this->timestamp ."';";
     254                }
     255                if (!empty($this->config)) {
     256                        foreach ($this->config as $name => $code) {
     257                                $extraCode .= ($extraCode ? "\n" : "") . "CKEDITOR.config['".$name."'] = ". $this->jsEncode($code) .";";
     258                        }
     259                }
     260                if (!empty($this->events)) {
     261                        foreach ($this->events as $name => $code) {
     262                                $extraCode .= ($extraCode ? "\n" : "") . "CKEDITOR.on('". $name ."', $code);";
     263                        }
     264                }
     265
     266                if ($extraCode) {
     267                        $out .= $this->script($extraCode);
     268                }
     269
     270                return $out;
     271        }
     272
     273        private function ckeditorPath()
     274        {
     275                if (!empty($this->basePath)) {
     276                        return $this->basePath;
     277                }
     278
     279                /**
     280                 * The absolute pathname of the currently executing script.
     281                 * Note: If a script is executed with the CLI, as a relative path, such as file.php or ../file.php,
     282                 * $_SERVER['SCRIPT_FILENAME'] will contain the relative path specified by the user.
     283                 */
     284                if (isset($_SERVER['SCRIPT_FILENAME'])) {
     285                        $realPath = dirname($_SERVER['SCRIPT_FILENAME']);
     286                }
     287                else {
     288                        /**
     289                         * realpath — Returns canonicalized absolute pathname
     290                         */
     291                        $realPath = realpath( './' ) ;
     292                }
     293
     294                /**
     295                 * The filename of the currently executing script, relative to the document root.
     296                 * For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar
     297                 * would be /test.php/foo.bar.
     298                 */
     299                $selfPath = dirname($_SERVER['PHP_SELF']);
     300                $file = str_replace("\\", "/", __FILE__);
     301
     302                if (!$selfPath || !$realPath || !$file) {
     303                        return "/ckeditor/";
     304                }
     305
     306                $documentRoot = substr($realPath, 0, strlen($realPath) - strlen($selfPath));
     307                $fileUrl = substr($file, strlen($documentRoot));
     308                $ckeditorUrl = str_replace("ckeditor_php5.php", "", $fileUrl);
     309
     310                return $ckeditorUrl;
     311        }
     312
     313        /**
     314         * This little function provides a basic JSON support.
     315         * http://php.net/manual/en/function.json-encode.php
     316         *
     317         * @param mixed $val
     318         * @return string
     319         */
     320        private function jsEncode($val)
     321        {
     322                if (is_null($val)) {
     323                        return 'null';
     324                }
     325                if ($val === false) {
     326                        return 'false';
     327                }
     328                if ($val === true) {
     329                        return 'true';
     330                }
     331                if (is_scalar($val))
     332                {
     333                        if (is_float($val))
     334                        {
     335                                // Always use "." for floats.
     336                                $val = str_replace(",", ".", strval($val));
     337                        }
     338
     339                        // Use @@ to not use quotes when outputting string value
     340                        if (strpos($val, '@@') === 0) {
     341                                return substr($val, 2);
     342                        }
     343                        else {
     344                                // All scalars are converted to strings to avoid indeterminism.
     345                                // PHP's "1" and 1 are equal for all PHP operators, but
     346                                // JS's "1" and 1 are not. So if we pass "1" or 1 from the PHP backend,
     347                                // we should get the same result in the JS frontend (string).
     348                                // Character replacements for JSON.
     349                                static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'),
     350                                array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
     351
     352                                $val = str_replace($jsonReplaces[0], $jsonReplaces[1], $val);
     353
     354                                return '"' . $val . '"';
     355                        }
     356                }
     357                $isList = true;
     358                for ($i = 0, reset($val); $i < count($val); $i++, next($val))
     359                {
     360                        if (key($val) !== $i)
     361                        {
     362                                $isList = false;
     363                                break;
     364                        }
     365                }
     366                $result = array();
     367                if ($isList)
     368                {
     369                        foreach ($val as $v) $result[] = $this->jsEncode($v);
     370                        return '[ ' . join(', ', $result) . ' ]';
     371                }
     372                else
     373                {
     374                        foreach ($val as $k => $v) $result[] = $this->jsEncode($k).': '.$this->jsEncode($v);
     375                        return '{ ' . join(', ', $result) . ' }';
     376                }
     377        }
     378}
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy