Project

General

Profile

1
<?php
2
/*
3
 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
4
 * Copyright (C) 2003-2010 Frederico Caldeira Knabben
5
 *
6
 * == BEGIN LICENSE ==
7
 *
8
 * Licensed under the terms of any of the following licenses at your
9
 * choice:
10
 *
11
 *  - GNU General Public License Version 2 or later (the "GPL")
12
 *    http://www.gnu.org/licenses/gpl.html
13
 *
14
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
15
 *    http://www.gnu.org/licenses/lgpl.html
16
 *
17
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
18
 *    http://www.mozilla.org/MPL/MPL-1.1.html
19
 *
20
 * == END LICENSE ==
21
 *
22
 * This is the integration file for PHP 5.
23
 *
24
 * It defines the FCKeditor class that can be used to create editor
25
 * instances in PHP pages on server side.
26
 */
27

    
28

    
29
/**
30
 * Check if browser is compatible with FCKeditor.
31
 * Return true if is compatible.
32
 *
33
 * @return boolean
34
 */
35
function FCKeditor_IsCompatibleBrowser()
36
{
37
    $sAgent = 'sorry';
38
    if ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
39
        $sAgent = $_SERVER['HTTP_USER_AGENT'] ;
40
    } else {
41
        if( isset( $GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT'] )) {
42
            $sAgent = $GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT'] ;
43
        } else {
44
            if( isset( $GLOBALS['HTTP_USER_AGENT'] )) {
45
                $sAgent = $GLOBALS['HTTP_USER_AGENT'];
46
            }
47
        }
48
    }
49

    
50
    // check for client agent
51
    $bRetval = false;
52
    if(preg_match('/ gecko\/([0-9.]+)/si', $sAgent, $aMatches)) {
53
        // [Gecko] Firefox, SeaMonkey and most Gecko based browsers
54
        if(strpos($aMatches[1], '.')) {
55
            // Versions from Gecko 17.0 up
56
            $bRetval = version_compare($aMatches[1], '4.0', '>=');
57
        } else {
58
            // versions before Gecko 17.0
59
            if((int)$aMatches[1] >= 20030210) { $bRetval = true; }
60
        }
61
    } elseif(preg_match('/ applewebkit\/([0-9.]+)/si', $sAgent, $aMatches)) {
62
        // [AppleWebKit] Crome, Safari
63
        $bRetval = version_compare($aMatches[1], '522', '>=');
64
    } elseif(preg_match('/^opera\/([0-9.]+)/si', $sAgent, $aMatches)) {
65
        // [Opera] Opera
66
        $bRetval = version_compare($aMatches[1], '9.5', '>=');
67
    } elseif(preg_match('/^mozilla\/[\d.]+\s\(.*?msie[^\)]*\(.*?\ msie\ ([0-9.]+);/si', $sAgent, $aMatches)) {
68
        // [MSIE] Internetexplorer compatibility mode
69
        $bRetval = version_compare($aMatches[1], '5.5', '>=');
70
    } elseif(preg_match('/^mozilla\/[\d.]+\s\(.*?\ msie\ ([0-9.]+);/si', $sAgent, $aMatches)) {
71
        // [MSIE] Internetexplorer
72
        $bRetval = version_compare($aMatches[1], '5.5', '>=');
73
    } else {
74
        // undefined client agent
75
        $bRetval = false;
76
    }
77
    return $bRetval;
78
}
79

    
80
class FCKeditor
81
{
82
	/**
83
	 * Name of the FCKeditor instance.
84
	 *
85
	 * @access protected
86
	 * @var string
87
	 */
88
	public $InstanceName ;
89
	/**
90
	 * Path to FCKeditor relative to the document root.
91
	 *
92
	 * @var string
93
	 */
94
	public $BasePath ;
95
	/**
96
	 * Width of the FCKeditor.
97
	 * Examples: 100%, 600
98
	 *
99
	 * @var mixed
100
	 */
101
	public $Width ;
102
	/**
103
	 * Height of the FCKeditor.
104
	 * Examples: 400, 50%
105
	 *
106
	 * @var mixed
107
	 */
108
	public $Height ;
109
	/**
110
	 * Name of the toolbar to load.
111
	 *
112
	 * @var string
113
	 */
114
	public $ToolbarSet ;
115
	/**
116
	 * Initial value.
117
	 *
118
	 * @var string
119
	 */
120
	public $Value ;
121
	/**
122
	 * This is where additional configuration can be passed.
123
	 * Example:
124
	 * $oFCKeditor->Config['EnterMode'] = 'br';
125
	 *
126
	 * @var array
127
	 */
128
	public $Config ;
129

    
130
	/**
131
	 * Main Constructor.
132
	 * Refer to the _samples/php directory for examples.
133
	 *
134
	 * @param string $instanceName
135
	 */
136
	public function __construct( $instanceName )
137
 	{
138
		$this->InstanceName	= $instanceName ;
139
		$this->BasePath		= '/fckeditor/' ;
140
		$this->Width		= '100%' ;
141
		$this->Height		= '200' ;
142
		$this->ToolbarSet	= 'Default' ;
143
		$this->Value		= '' ;
144

    
145
		$this->Config		= array() ;
146
	}
147

    
148
	/**
149
	 * Display FCKeditor.
150
	 *
151
	 */
152
	public function Create()
153
	{
154
		echo $this->CreateHtml() ;
155
	}
156

    
157
	/**
158
	 * Return the HTML code required to run FCKeditor.
159
	 *
160
	 * @return string
161
	 */
162
	public function CreateHtml()
163
	{
164
		$HtmlValue = htmlspecialchars( $this->Value ) ;
165

    
166
		$Html = '' ;
167

    
168
		if ( $this->IsCompatible() )
169
		{
170
			if ( isset( $_GET['fcksource'] ) && $_GET['fcksource'] == "true" )
171
				$File = 'fckeditor.original.html' ;
172
			else
173
				$File = 'fckeditor.html' ;
174

    
175
			$Link = "{$this->BasePath}editor/{$File}?InstanceName={$this->InstanceName}" ;
176

    
177
			if ( $this->ToolbarSet != '' )
178
				$Link .= "&amp;Toolbar={$this->ToolbarSet}" ;
179

    
180
			// Render the linked hidden field.
181
			$Html .= "<input type=\"hidden\" id=\"{$this->InstanceName}\" name=\"{$this->InstanceName}\" value=\"{$HtmlValue}\" style=\"display:none\" />" ;
182

    
183
			// Render the configurations hidden field.
184
			$Html .= "<input type=\"hidden\" id=\"{$this->InstanceName}___Config\" value=\"" . $this->GetConfigFieldString() . "\" style=\"display:none\" />" ;
185

    
186
			// Render the editor IFRAME.
187
			$Html .= "<iframe id=\"{$this->InstanceName}___Frame\" src=\"{$Link}\" width=\"{$this->Width}\" height=\"{$this->Height}\" frameborder=\"0\" scrolling=\"no\"></iframe>" ;
188
		}
189
		else
190
		{
191
			if ( strpos( $this->Width, '%' ) === false )
192
				$WidthCSS = $this->Width . 'px' ;
193
			else
194
				$WidthCSS = $this->Width ;
195

    
196
			if ( strpos( $this->Height, '%' ) === false )
197
				$HeightCSS = $this->Height . 'px' ;
198
			else
199
				$HeightCSS = $this->Height ;
200

    
201
			$Html .= "<textarea name=\"{$this->InstanceName}\" rows=\"4\" cols=\"40\" style=\"width: {$WidthCSS}; height: {$HeightCSS}\">{$HtmlValue}</textarea>" ;
202
		}
203

    
204
		return $Html ;
205
	}
206

    
207
	/**
208
	 * Returns true if browser is compatible with FCKeditor.
209
	 *
210
	 * @return boolean
211
	 */
212
	public function IsCompatible()
213
	{
214
		return FCKeditor_IsCompatibleBrowser() ;
215
	}
216

    
217
	/**
218
	 * Get settings from Config array as a single string.
219
	 *
220
	 * @access protected
221
	 * @return string
222
	 */
223
	public function GetConfigFieldString()
224
	{
225
		$sParams = '' ;
226
		$bFirst = true ;
227

    
228
		foreach ( $this->Config as $sKey => $sValue )
229
		{
230
			if ( $bFirst == false )
231
				$sParams .= '&amp;' ;
232
			else
233
				$bFirst = false ;
234

    
235
			if ( $sValue === true )
236
				$sParams .= $this->EncodeConfig( $sKey ) . '=true' ;
237
			else if ( $sValue === false )
238
				$sParams .= $this->EncodeConfig( $sKey ) . '=false' ;
239
			else
240
				$sParams .= $this->EncodeConfig( $sKey ) . '=' . $this->EncodeConfig( $sValue ) ;
241
		}
242

    
243
		return $sParams ;
244
	}
245

    
246
	/**
247
	 * Encode characters that may break the configuration string
248
	 * generated by GetConfigFieldString().
249
	 *
250
	 * @access protected
251
	 * @param string $valueToEncode
252
	 * @return string
253
	 */
254
	public function EncodeConfig( $valueToEncode )
255
	{
256
		$chars = array(
257
			'&' => '%26',
258
			'=' => '%3D',
259
			'"' => '%22' ) ;
260

    
261
		return strtr( $valueToEncode,  $chars ) ;
262
	}
263
}
(9-9/14)