PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/third_party/fckeditor/fckeditor_php5.php

https://github.com/viglesiasce/testlink
PHP | 257 lines | 125 code | 24 blank | 108 comment | 26 complexity | 604eae15656111368bb6c025c47ea6f1 MD5 | raw file
  1. <?php
  2. /*
  3. * FCKeditor - The text editor for Internet - http://www.fckeditor.net
  4. * Copyright (C) 2003-2009 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. * Check if browser is compatible with FCKeditor.
  29. * Return true if is compatible.
  30. *
  31. * @return boolean
  32. */
  33. function FCKeditor_IsCompatibleBrowser()
  34. {
  35. if ( isset( $_SERVER ) ) {
  36. $sAgent = $_SERVER['HTTP_USER_AGENT'] ;
  37. }
  38. else {
  39. global $HTTP_SERVER_VARS ;
  40. if ( isset( $HTTP_SERVER_VARS ) ) {
  41. $sAgent = $HTTP_SERVER_VARS['HTTP_USER_AGENT'] ;
  42. }
  43. else {
  44. global $HTTP_USER_AGENT ;
  45. $sAgent = $HTTP_USER_AGENT ;
  46. }
  47. }
  48. if ( strpos($sAgent, 'MSIE') !== false && strpos($sAgent, 'mac') === false && strpos($sAgent, 'Opera') === false )
  49. {
  50. $iVersion = (float)substr($sAgent, strpos($sAgent, 'MSIE') + 5, 3) ;
  51. return ($iVersion >= 5.5) ;
  52. }
  53. else if ( strpos($sAgent, 'Gecko/') !== false )
  54. {
  55. $iVersion = (int)substr($sAgent, strpos($sAgent, 'Gecko/') + 6, 8) ;
  56. return ($iVersion >= 20030210) ;
  57. }
  58. else if ( strpos($sAgent, 'Opera/') !== false )
  59. {
  60. $fVersion = (float)substr($sAgent, strpos($sAgent, 'Opera/') + 6, 4) ;
  61. return ($fVersion >= 9.5) ;
  62. }
  63. else if ( preg_match( "|AppleWebKit/(\d+)|i", $sAgent, $matches ) )
  64. {
  65. $iVersion = $matches[1] ;
  66. return ( $matches[1] >= 522 ) ;
  67. }
  68. else
  69. return false ;
  70. }
  71. class FCKeditor
  72. {
  73. /**
  74. * Name of the FCKeditor instance.
  75. *
  76. * @access protected
  77. * @var string
  78. */
  79. public $InstanceName ;
  80. /**
  81. * Path to FCKeditor relative to the document root.
  82. *
  83. * @var string
  84. */
  85. public $BasePath ;
  86. /**
  87. * Width of the FCKeditor.
  88. * Examples: 100%, 600
  89. *
  90. * @var mixed
  91. */
  92. public $Width ;
  93. /**
  94. * Height of the FCKeditor.
  95. * Examples: 400, 50%
  96. *
  97. * @var mixed
  98. */
  99. public $Height ;
  100. /**
  101. * Name of the toolbar to load.
  102. *
  103. * @var string
  104. */
  105. public $ToolbarSet ;
  106. /**
  107. * Initial value.
  108. *
  109. * @var string
  110. */
  111. public $Value ;
  112. /**
  113. * This is where additional configuration can be passed.
  114. * Example:
  115. * $oFCKeditor->Config['EnterMode'] = 'br';
  116. *
  117. * @var array
  118. */
  119. public $Config ;
  120. /**
  121. * Main Constructor.
  122. * Refer to the _samples/php directory for examples.
  123. *
  124. * @param string $instanceName
  125. */
  126. public function __construct( $instanceName )
  127. {
  128. $this->InstanceName = $instanceName ;
  129. $this->BasePath = '/fckeditor/' ;
  130. $this->Width = '100%' ;
  131. $this->Height = '200' ;
  132. $this->ToolbarSet = 'Default' ;
  133. $this->Value = '' ;
  134. $this->Config = array() ;
  135. }
  136. /**
  137. * Display FCKeditor.
  138. *
  139. */
  140. public function Create()
  141. {
  142. echo $this->CreateHtml() ;
  143. }
  144. /**
  145. * Return the HTML code required to run FCKeditor.
  146. *
  147. * @return string
  148. */
  149. public function CreateHtml()
  150. {
  151. $HtmlValue = htmlspecialchars( $this->Value ) ;
  152. $Html = '' ;
  153. if ( $this->IsCompatible() )
  154. {
  155. if ( isset( $_GET['fcksource'] ) && $_GET['fcksource'] == "true" )
  156. $File = 'fckeditor.original.html' ;
  157. else
  158. $File = 'fckeditor.html' ;
  159. $Link = "{$this->BasePath}editor/{$File}?InstanceName={$this->InstanceName}" ;
  160. if ( $this->ToolbarSet != '' )
  161. $Link .= "&amp;Toolbar={$this->ToolbarSet}" ;
  162. // Render the linked hidden field.
  163. $Html .= "<input type=\"hidden\" id=\"{$this->InstanceName}\" name=\"{$this->InstanceName}\" value=\"{$HtmlValue}\" style=\"display:none\" />" ;
  164. // Render the configurations hidden field.
  165. $Html .= "<input type=\"hidden\" id=\"{$this->InstanceName}___Config\" value=\"" . $this->GetConfigFieldString() . "\" style=\"display:none\" />" ;
  166. // Render the editor IFRAME.
  167. $Html .= "<iframe id=\"{$this->InstanceName}___Frame\" src=\"{$Link}\" width=\"{$this->Width}\" height=\"{$this->Height}\" frameborder=\"0\" scrolling=\"no\"></iframe>" ;
  168. }
  169. else
  170. {
  171. if ( strpos( $this->Width, '%' ) === false )
  172. $WidthCSS = $this->Width . 'px' ;
  173. else
  174. $WidthCSS = $this->Width ;
  175. if ( strpos( $this->Height, '%' ) === false )
  176. $HeightCSS = $this->Height . 'px' ;
  177. else
  178. $HeightCSS = $this->Height ;
  179. $Html .= "<textarea name=\"{$this->InstanceName}\" rows=\"4\" cols=\"40\" style=\"width: {$WidthCSS}; height: {$HeightCSS}\">{$HtmlValue}</textarea>" ;
  180. }
  181. return $Html ;
  182. }
  183. /**
  184. * Returns true if browser is compatible with FCKeditor.
  185. *
  186. * @return boolean
  187. */
  188. public function IsCompatible()
  189. {
  190. return FCKeditor_IsCompatibleBrowser() ;
  191. }
  192. /**
  193. * Get settings from Config array as a single string.
  194. *
  195. * @access protected
  196. * @return string
  197. */
  198. public function GetConfigFieldString()
  199. {
  200. $sParams = '' ;
  201. $bFirst = true ;
  202. foreach ( $this->Config as $sKey => $sValue )
  203. {
  204. if ( $bFirst == false )
  205. $sParams .= '&amp;' ;
  206. else
  207. $bFirst = false ;
  208. if ( $sValue === true )
  209. $sParams .= $this->EncodeConfig( $sKey ) . '=true' ;
  210. else if ( $sValue === false )
  211. $sParams .= $this->EncodeConfig( $sKey ) . '=false' ;
  212. else
  213. $sParams .= $this->EncodeConfig( $sKey ) . '=' . $this->EncodeConfig( $sValue ) ;
  214. }
  215. return $sParams ;
  216. }
  217. /**
  218. * Encode characters that may break the configuration string
  219. * generated by GetConfigFieldString().
  220. *
  221. * @access protected
  222. * @param string $valueToEncode
  223. * @return string
  224. */
  225. public function EncodeConfig( $valueToEncode )
  226. {
  227. $chars = array(
  228. '&' => '%26',
  229. '=' => '%3D',
  230. '"' => '%22' ) ;
  231. return strtr( $valueToEncode, $chars ) ;
  232. }
  233. }