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 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
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 5 Constructor (by Marcus Bointon <coolbru@users.sourceforge.net>)
39	function __construct( $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 ( $this->IsCompatible() )
63		{
64			if ( isset( $_GET['fcksource'] ) && $_GET['fcksource'] == "true" )
65				$File = 'fckeditor.original.html' ;
66			else
67				$File = 'fckeditor.html' ;
68
69			$Link = "{$this->BasePath}editor/{$File}?InstanceName={$this->InstanceName}" ;
70
71			if ( $this->ToolbarSet != '' )
72				$Link .= "&amp;Toolbar={$this->ToolbarSet}" ;
73
74			// Render the linked hidden field.
75			$Html .= "<input type=\"hidden\" id=\"{$this->InstanceName}\" name=\"{$this->InstanceName}\" value=\"{$HtmlValue}\" style=\"display:none\" />" ;
76
77			// Render the configurations hidden field.
78			$Html .= "<input type=\"hidden\" id=\"{$this->InstanceName}___Config\" value=\"" . $this->GetConfigFieldString() . "\" style=\"display:none\" />" ;
79
80			// Render the editor IFRAME.
81			$Html .= "<iframe id=\"{$this->InstanceName}___Frame\" src=\"{$Link}\" width=\"{$this->Width}\" height=\"{$this->Height}\" frameborder=\"0\" scrolling=\"no\"></iframe>" ;
82		}
83		else
84		{
85			if ( strpos( $this->Width, '%' ) === false )
86				$WidthCSS = $this->Width . 'px' ;
87			else
88				$WidthCSS = $this->Width ;
89
90			if ( strpos( $this->Height, '%' ) === false )
91				$HeightCSS = $this->Height . 'px' ;
92			else
93				$HeightCSS = $this->Height ;
94
95			$Html .= "<textarea name=\"{$this->InstanceName}\" rows=\"4\" cols=\"40\" style=\"width: {$WidthCSS}; height: {$HeightCSS}\">{$HtmlValue}</textarea>" ;
96		}
97
98		$Html .= '</div>' ;
99
100		return $Html ;
101	}
102
103	function IsCompatible()
104	{
105		global $HTTP_USER_AGENT ;
106
107		if ( isset( $HTTP_USER_AGENT ) )
108			$sAgent = $HTTP_USER_AGENT ;
109		else
110			$sAgent = $_SERVER['HTTP_USER_AGENT'] ;
111
112		if ( strpos($sAgent, 'MSIE') !== false && strpos($sAgent, 'mac') === false && strpos($sAgent, 'Opera') === false )
113		{
114			$iVersion = (float)substr($sAgent, strpos($sAgent, 'MSIE') + 5, 3) ;
115			return ($iVersion >= 5.5) ;
116		}
117		else if ( strpos($sAgent, 'Gecko/') !== false )
118		{
119			$iVersion = (int)substr($sAgent, strpos($sAgent, 'Gecko/') + 6, 8) ;
120			return ($iVersion >= 20030210) ;
121		}
122		else if ( strpos($sAgent, 'Opera/') !== false )
123		{
124			$fVersion = (float)substr($sAgent, strpos($sAgent, 'Opera/') + 6, 4) ;
125			return ($fVersion >= 9.5) ;
126		}
127		else if ( preg_match( "|AppleWebKit/(\d+)|i", $sAgent, $matches ) )
128		{
129			$iVersion = $matches[1] ;
130			return ( $matches[1] >= 522 ) ;
131		}
132		else
133			return false ;
134	}
135
136	function GetConfigFieldString()
137	{
138		$sParams = '' ;
139		$bFirst = true ;
140
141		foreach ( $this->Config as $sKey => $sValue )
142		{
143			if ( $bFirst == false )
144				$sParams .= '&amp;' ;
145			else
146				$bFirst = false ;
147
148			if ( $sValue === true )
149				$sParams .= $this->EncodeConfig( $sKey ) . '=true' ;
150			else if ( $sValue === false )
151				$sParams .= $this->EncodeConfig( $sKey ) . '=false' ;
152			else
153				$sParams .= $this->EncodeConfig( $sKey ) . '=' . $this->EncodeConfig( $sValue ) ;
154		}
155
156		return $sParams ;
157	}
158
159	function EncodeConfig( $valueToEncode )
160	{
161		$chars = array(
162			'&' => '%26',
163			'=' => '%3D',
164			'"' => '%22' ) ;
165
166		return strtr( $valueToEncode,  $chars ) ;
167	}
168}
169
170?>