1<?php
2/*
3 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
4 * Copyright (C) 2003-2007 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 4.
23 *
24 * It defines the FCKeditor class that can be used to create editor
25 * instances in PHP pages on server side.
26 */
27
28class FCKeditor
29{
30	var $InstanceName ;
31	var $BasePath ;
32	var $Width ;
33	var $Height ;
34	var $ToolbarSet ;
35	var $Value ;
36	var $Config ;
37
38	// PHP 4 Constructor
39	function FCKeditor( $instanceName )
40	{
41		$this->InstanceName	= $instanceName ;
42		$this->BasePath		= '/fckeditor/' ;
43		$this->Width		= '100%' ;
44		$this->Height		= '200' ;
45		$this->ToolbarSet	= 'Default' ;
46		$this->Value		= '' ;
47
48		$this->Config		= array() ;
49	}
50
51	function Create()
52	{
53		echo $this->CreateHtml() ;
54	}
55
56	function CreateHtml()
57	{
58		$HtmlValue = htmlspecialchars( $this->Value ) ;
59
60		$Html = '<div>' ;
61
62		if ( !isset( $_GET ) ) {
63			global $HTTP_GET_VARS ;
64		    $_GET = $HTTP_GET_VARS ;
65		}
66
67		if ( $this->IsCompatible() )
68		{
69			if ( isset( $_GET['fcksource'] ) && $_GET['fcksource'] == "true" )
70				$File = 'fckeditor.original.html' ;
71			else
72				$File = 'fckeditor.html' ;
73
74			$Link = "{$this->BasePath}editor/{$File}?InstanceName={$this->InstanceName}" ;
75
76			if ( $this->ToolbarSet != '' )
77				$Link .= "&amp;Toolbar={$this->ToolbarSet}" ;
78
79			// Render the linked hidden field.
80			$Html .= "<input type=\"hidden\" id=\"{$this->InstanceName}\" name=\"{$this->InstanceName}\" value=\"{$HtmlValue}\" style=\"display:none\" />" ;
81
82			// Render the configurations hidden field.
83			$Html .= "<input type=\"hidden\" id=\"{$this->InstanceName}___Config\" value=\"" . $this->GetConfigFieldString() . "\" style=\"display:none\" />" ;
84
85			// Render the editor IFRAME.
86			$Html .= "<iframe id=\"{$this->InstanceName}___Frame\" src=\"{$Link}\" width=\"{$this->Width}\" height=\"{$this->Height}\" frameborder=\"0\" scrolling=\"no\"></iframe>" ;
87		}
88		else
89		{
90			if ( strpos( $this->Width, '%' ) === false )
91				$WidthCSS = $this->Width . 'px' ;
92			else
93				$WidthCSS = $this->Width ;
94
95			if ( strpos( $this->Height, '%' ) === false )
96				$HeightCSS = $this->Height . 'px' ;
97			else
98				$HeightCSS = $this->Height ;
99
100			$Html .= "<textarea name=\"{$this->InstanceName}\" rows=\"4\" cols=\"40\" style=\"width: {$WidthCSS}; height: {$HeightCSS}\">{$HtmlValue}</textarea>" ;
101		}
102
103		$Html .= '</div>' ;
104
105		return $Html ;
106	}
107
108	function IsCompatible()
109	{
110		global $HTTP_USER_AGENT ;
111
112		if ( !isset( $_SERVER ) ) {
113			global $HTTP_SERVER_VARS ;
114		    $_SERVER = $HTTP_SERVER_VARS ;
115		}
116
117		if ( isset( $HTTP_USER_AGENT ) )
118			$sAgent = $HTTP_USER_AGENT ;
119		else
120			$sAgent = $_SERVER['HTTP_USER_AGENT'] ;
121
122		if ( strpos($sAgent, 'MSIE') !== false && strpos($sAgent, 'mac') === false && strpos($sAgent, 'Opera') === false )
123		{
124			$iVersion = (float)substr($sAgent, strpos($sAgent, 'MSIE') + 5, 3) ;
125			return ($iVersion >= 5.5) ;
126		}
127		else if ( strpos($sAgent, 'Gecko/') !== false )
128		{
129			$iVersion = (int)substr($sAgent, strpos($sAgent, 'Gecko/') + 6, 8) ;
130			return ($iVersion >= 20030210) ;
131		}
132		else if ( strpos($sAgent, 'Opera/') !== false )
133		{
134			$fVersion = (float)substr($sAgent, strpos($sAgent, 'Opera/') + 6, 4) ;
135			return ($fVersion >= 9.5) ;
136		}
137		else if ( preg_match( "|AppleWebKit/(\d+)|i", $sAgent, $matches ) )
138		{
139			$iVersion = $matches[1] ;
140			return ( $matches[1] >= 522 ) ;
141		}
142		else
143			return false ;
144	}
145
146	function GetConfigFieldString()
147	{
148		$sParams = '' ;
149		$bFirst = true ;
150
151		foreach ( $this->Config as $sKey => $sValue )
152		{
153			if ( $bFirst == false )
154				$sParams .= '&amp;' ;
155			else
156				$bFirst = false ;
157
158			if ( $sValue === true )
159				$sParams .= $this->EncodeConfig( $sKey ) . '=true' ;
160			else if ( $sValue === false )
161				$sParams .= $this->EncodeConfig( $sKey ) . '=false' ;
162			else
163				$sParams .= $this->EncodeConfig( $sKey ) . '=' . $this->EncodeConfig( $sValue ) ;
164		}
165
166		return $sParams ;
167	}
168
169	function EncodeConfig( $valueToEncode )
170	{
171		$chars = array(
172			'&' => '%26',
173			'=' => '%3D',
174			'"' => '%22' ) ;
175
176		return strtr( $valueToEncode,  $chars ) ;
177	}
178}
179
180?>
181