fckeditor.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. // Code about adaptation of the editor and its plugins has been added by the Dokeos team, February - July 2009.
  28. // For modifying configuration options see myconfig.php and myconfig.js.
  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. if ( isset( $_SERVER ) ) {
  38. $sAgent = $_SERVER['HTTP_USER_AGENT'] ;
  39. }
  40. else {
  41. global $HTTP_SERVER_VARS ;
  42. if ( isset( $HTTP_SERVER_VARS ) ) {
  43. $sAgent = $HTTP_SERVER_VARS['HTTP_USER_AGENT'] ;
  44. }
  45. else {
  46. global $HTTP_USER_AGENT ;
  47. $sAgent = $HTTP_USER_AGENT ;
  48. }
  49. }
  50. if ( strpos($sAgent, 'MSIE') !== false && strpos($sAgent, 'mac') === false && strpos($sAgent, 'Opera') === false )
  51. {
  52. $iVersion = (float)substr($sAgent, strpos($sAgent, 'MSIE') + 5, 3) ;
  53. return ($iVersion >= 5.5) ;
  54. }
  55. else if ( strpos($sAgent, 'Gecko/') !== false )
  56. {
  57. $iVersion = (int)substr($sAgent, strpos($sAgent, 'Gecko/') + 6, 8) ;
  58. return ($iVersion >= 20030210) ;
  59. }
  60. else if ( strpos($sAgent, 'Opera/') !== false )
  61. {
  62. $fVersion = (float)substr($sAgent, strpos($sAgent, 'Opera/') + 6, 4) ;
  63. return ($fVersion >= 9.5) ;
  64. }
  65. else if ( preg_match( "|AppleWebKit/(\d+)|i", $sAgent, $matches ) )
  66. {
  67. $iVersion = $matches[1] ;
  68. return ( $matches[1] >= 522 ) ;
  69. }
  70. else
  71. return false ;
  72. }
  73. class FCKeditor
  74. {
  75. /**
  76. * Name of the FCKeditor instance.
  77. *
  78. * @access protected
  79. * @var string
  80. */
  81. public $InstanceName ;
  82. /**
  83. * Path to FCKeditor relative to the document root.
  84. *
  85. * @var string
  86. */
  87. public $BasePath ;
  88. /**
  89. * Width of the FCKeditor.
  90. * Examples: 100%, 600
  91. *
  92. * @var mixed
  93. */
  94. public $Width ;
  95. /**
  96. * Height of the FCKeditor.
  97. * Examples: 400, 50%
  98. *
  99. * @var mixed
  100. */
  101. public $Height ;
  102. /**
  103. * Name of the toolbar to load.
  104. *
  105. * @var string
  106. */
  107. public $ToolbarSet ;
  108. /**
  109. * Initial value.
  110. *
  111. * @var string
  112. */
  113. public $Value ;
  114. /**
  115. * This is where additional configuration can be passed.
  116. * Example:
  117. * $oFCKeditor->Config['EnterMode'] = 'br';
  118. *
  119. * @var array
  120. */
  121. public $Config ;
  122. /**
  123. * Main Constructor.
  124. * Refer to the _samples/php directory for examples.
  125. *
  126. * @param string $instanceName
  127. */
  128. public function __construct( $instanceName )
  129. {
  130. $this->InstanceName = $instanceName ;
  131. $this->BasePath = '/fckeditor/' ;
  132. $this->Width = '100%' ;
  133. $this->Height = '200' ;
  134. $this->ToolbarSet = 'Default' ;
  135. $this->Value = '' ;
  136. $this->Config = array() ;
  137. }
  138. /**
  139. * Display FCKeditor.
  140. *
  141. */
  142. public function Create()
  143. {
  144. echo $this->CreateHtml() ;
  145. }
  146. /**
  147. * Return the HTML code required to run FCKeditor.
  148. *
  149. * @return string
  150. */
  151. public function CreateHtml()
  152. {
  153. // Adaptation for the Dokeos LMS ---------------------------------------------------------
  154. $this->BasePath = api_get_path(REL_PATH).'main/inc/lib/fckeditor/';
  155. $config = $this->get_custom_configuration();
  156. $this->read_configuration($config);
  157. $config = $this->get_default_configuration();
  158. $this->read_configuration($config);
  159. if ((api_is_allowed_to_edit() || api_is_platform_admin()) && isset($this->Config['BlockCopyPaste'])) {
  160. $this->Config['BlockCopyPaste'] = false;
  161. }
  162. //----------------------------------------------------------------------------------------
  163. $HtmlValue = htmlspecialchars( $this->Value ) ;
  164. $Html = '' ;
  165. if ( $this->IsCompatible() )
  166. {
  167. if ( api_get_setting('server_type') == 'test' )
  168. $File = 'fckeditor.original.html' ;
  169. else
  170. $File = 'fckeditor.html' ;
  171. $Link = "{$this->BasePath}editor/{$File}?InstanceName={$this->InstanceName}" ;
  172. if ( $this->ToolbarSet != '' )
  173. $Link .= "&amp;Toolbar={$this->ToolbarSet}" ;
  174. // Render the linked hidden field.
  175. $Html .= "<input type=\"hidden\" id=\"{$this->InstanceName}\" name=\"{$this->InstanceName}\" value=\"{$HtmlValue}\" style=\"display:none\" />" ;
  176. // Render the configurations hidden field.
  177. $Html .= "<input type=\"hidden\" id=\"{$this->InstanceName}___Config\" value=\"" . $this->GetConfigFieldString() . "\" style=\"display:none\" />" ;
  178. // Render the editor IFRAME.
  179. $Html .= "<iframe id=\"{$this->InstanceName}___Frame\" src=\"{$Link}\" width=\"{$this->Width}\" height=\"{$this->Height}\" frameborder=\"0\" scrolling=\"no\"></iframe>" ;
  180. }
  181. else
  182. {
  183. if ( strpos( $this->Width, '%' ) === false )
  184. $WidthCSS = $this->Width . 'px' ;
  185. else
  186. $WidthCSS = $this->Width ;
  187. if ( strpos( $this->Height, '%' ) === false )
  188. $HeightCSS = $this->Height . 'px' ;
  189. else
  190. $HeightCSS = $this->Height ;
  191. $Html .= "<textarea name=\"{$this->InstanceName}\" rows=\"4\" cols=\"40\" style=\"width: {$WidthCSS}; height: {$HeightCSS}\">{$HtmlValue}</textarea>" ;
  192. }
  193. return $Html ;
  194. }
  195. /**
  196. * Returns true if browser is compatible with FCKeditor.
  197. *
  198. * @return boolean
  199. */
  200. public function IsCompatible()
  201. {
  202. return FCKeditor_IsCompatibleBrowser() ;
  203. }
  204. /**
  205. * Get settings from Config array as a single string.
  206. *
  207. * @access protected
  208. * @return string
  209. */
  210. public function GetConfigFieldString()
  211. {
  212. $sParams = '' ;
  213. $bFirst = true ;
  214. foreach ( $this->Config as $sKey => $sValue )
  215. {
  216. if ( $bFirst == false ) {
  217. $sParams .= '&amp;' ;
  218. } else {
  219. $bFirst = false ;
  220. }
  221. if ( is_string( $sValue ) ) {
  222. $sParams .= $this->EncodeConfig( $sKey ) . '=' . $this->EncodeConfig( $sValue ) ;
  223. } else {
  224. $sParams .= $this->EncodeConfig( $sKey ) . '=' . $this->EncodeConfig( $this->to_js( $sValue ) ) ;
  225. }
  226. }
  227. return $sParams ;
  228. }
  229. /**
  230. * Encode characters that may break the configuration string
  231. * generated by GetConfigFieldString().
  232. *
  233. * @access protected
  234. * @param string $valueToEncode
  235. * @return string
  236. */
  237. public function EncodeConfig( $valueToEncode )
  238. {
  239. $chars = array(
  240. '&' => '%26',
  241. '=' => '%3D',
  242. '"' => '%22',
  243. '%' => '%25'
  244. ) ;
  245. return strtr( $valueToEncode, $chars ) ;
  246. }
  247. /**
  248. * Converts a PHP variable into its Javascript equivalent.
  249. * The code of this method has been "borrowed" from the funcion drupal_to_js() within the Drupal CMS.
  250. * @param mixed $var The variable to be converted into Javascript syntax
  251. * @return string Returns a string
  252. * Note: This function is similar to json_encode(), in addition it produces HTML-safe strings, i.e. with <, > and & escaped.
  253. * @link http://drupal.org/
  254. */
  255. private function to_js( $var ) {
  256. switch ( gettype( $var ) ) {
  257. case 'boolean' :
  258. return $var ? 'true' : 'false' ; // Lowercase necessary!
  259. case 'integer' :
  260. case 'double' :
  261. return (string) $var ;
  262. case 'resource' :
  263. case 'string' :
  264. return '"' . str_replace( array( "\r", "\n", "<", ">", "&" ), array( '\r', '\n', '\x3c', '\x3e', '\x26' ), addslashes( $var ) ) . '"' ;
  265. case 'array' :
  266. // Arrays in JSON can't be associative. If the array is empty or if it
  267. // has sequential whole number keys starting with 0, it's not associative
  268. // so we can go ahead and convert it as an array.
  269. if ( empty( $var ) || array_keys( $var ) === range( 0, sizeof( $var ) - 1 ) ) {
  270. $output = array() ;
  271. foreach ( $var as $v ) {
  272. $output[] = $this->to_js( $v ) ;
  273. }
  274. return '[ ' . implode( ', ', $output ) . ' ]' ;
  275. }
  276. // Otherwise, fall through to convert the array as an object.
  277. case 'object' :
  278. $output = array() ;
  279. foreach ( $var as $k => $v ) {
  280. $output[] = $this->to_js( strval( $k ) ) . ': ' . $this->to_js( $v ) ;
  281. }
  282. return '{ ' . implode(', ', $output) . ' }' ;
  283. default:
  284. return 'null' ;
  285. }
  286. }
  287. /**
  288. * This method reads configuration data for the current editor's instance without overriding settings that already exist.
  289. * @return array
  290. */
  291. function read_configuration(& $config) {
  292. $toolbar_set = $this->ToolbarSet;
  293. $toolbar_set_maximized = $this->ToolbarSet.'Maximized';
  294. foreach ($config as $key => $value) {
  295. switch ($key) {
  296. case 'ToolbarSets':
  297. if (!empty($toolbar_set) && $toolbar_set != 'Default') {
  298. if (is_array($value)) {
  299. if (isset($value['Normal'])) {
  300. if (!isset($this->Config[$key][$toolbar_set])) {
  301. $this->Config[$key][$toolbar_set] = $value['Normal'];
  302. }
  303. }
  304. if (isset($value['Maximized'])) {
  305. if (!isset($this->Config[$key][$toolbar_set_maximized])) {
  306. $this->Config[$key][$toolbar_set_maximized] = $value['Maximized'];
  307. }
  308. }
  309. }
  310. }
  311. break;
  312. case 'Width':
  313. $this->Config[$key] = (string) $value;
  314. $this->Width = $this->Config[$key];
  315. break;
  316. case 'Height':
  317. $this->Config[$key] = (string) $value;
  318. $this->Height = $this->Config[$key];
  319. break;
  320. default:
  321. if (!isset($this->Config[$key])) {
  322. $this->Config[$key] = $value;
  323. }
  324. }
  325. }
  326. }
  327. /**
  328. * This method returns editor's custom configuration settings read from php-files.
  329. * @return array Custom configuration data.
  330. */
  331. private function & get_custom_configuration() {
  332. static $config;
  333. if (!isset($config)) {
  334. require api_get_path(LIBRARY_PATH).'fckeditor/myconfig.php';
  335. }
  336. $toolbar_dir = isset($config['ToolbarSets']['Directory']) ? $config['ToolbarSets']['Directory'] : 'default';
  337. return array_merge($config, $this->get_custom_toolbar_configuration($toolbar_dir));
  338. }
  339. /**
  340. * This method returns editor's toolbar configuration settings read from a php-file.
  341. * @return array Toolbar configuration data.
  342. */
  343. private function & get_custom_toolbar_configuration($toolbar_dir) {
  344. static $toolbar_config = array('Default' => array());
  345. if (!isset($toolbar_config[$this->ToolbarSet])) {
  346. $toolbar_config[$this->ToolbarSet] = array();
  347. if (preg_match('/[a-zA-Z_]+/', $toolbar_dir) && preg_match('/[a-zA-Z_]+/', $this->ToolbarSet)) { // A security check.
  348. // Seeking the toolbar.
  349. @include api_get_path(LIBRARY_PATH).'fckeditor/toolbars/'.$toolbar_dir.'/'.self::camel_case_to_underscore($this->ToolbarSet).'.php';
  350. if (!isset($config['ToolbarSets']['Normal'])) {
  351. // No toolbar has been found yet.
  352. if ($toolbar_dir == 'default') {
  353. // It does not exist in default toolbar definitions, giving up.
  354. $this->ToolbarSet = 'Default';
  355. } else {
  356. // The custom toolbar does not exist, then trying to load the default one.
  357. @include api_get_path(LIBRARY_PATH).'fckeditor/toolbars/default/'.self::camel_case_to_underscore($this->ToolbarSet).'.php';
  358. if (!isset($config['ToolbarSets']['Normal'])) {
  359. // It does not exist in default toolbar definitions, giving up.
  360. $this->ToolbarSet = 'Default';
  361. } else {
  362. $toolbar_config[$this->ToolbarSet] = $config;
  363. }
  364. }
  365. } else {
  366. $toolbar_config[$this->ToolbarSet] = $config;
  367. }
  368. } else {
  369. $this->ToolbarSet = 'Default';
  370. }
  371. }
  372. return $toolbar_config[$this->ToolbarSet];
  373. }
  374. /**
  375. * This method returns automatically determined editor's configuration settings (default settings).
  376. * @return array
  377. */
  378. private function & get_default_configuration() {
  379. return array_merge(
  380. self::get_javascript_custom_configuration_file(),
  381. self::get_css_configuration(),
  382. self::get_editor_language(),
  383. $this->get_repository_configuration(),
  384. self::get_media_configuration(),
  385. self::get_user_configuration_data(),
  386. $this->get_mimetex_plugin_configuration()
  387. );
  388. }
  389. /**
  390. * This method returns the path to the javascript custom configuration file.
  391. * @return array
  392. */
  393. private function & get_javascript_custom_configuration_file() {
  394. return array('CustomConfigurationsPath' => api_get_path(REL_PATH).'main/inc/lib/fckeditor/myconfig.js');
  395. }
  396. /**
  397. * This method returns CSS-related configuration data that has been determined by the system.
  398. * @return array
  399. */
  400. private function & get_css_configuration() {
  401. $config['EditorAreaCSS'] = api_get_path(REL_PATH).'main/css/'.api_get_setting('stylesheets').'/default.css';
  402. /*
  403. // TODO: Check whether these CSS are appropriate.
  404. if (file_exists(api_get_path(SYS_PATH).'main/css/'.api_get_setting('stylesheets').'/course.css')) {
  405. $config['EditorAreaCSS'] = api_get_path(REL_PATH).'main/css/'.api_get_setting('stylesheets').'/course.css';
  406. } else {
  407. $config['EditorAreaCSS'] = api_get_path(REL_PATH).'main/css/public_admin/course.css';
  408. }
  409. */
  410. $config['ToolbarComboPreviewCSS'] = $config['EditorAreaCSS'];
  411. return $config;
  412. }
  413. /**
  414. * This method determines editor's interface language and returns it as compatible with the editor langiage code.
  415. * @return array
  416. */
  417. private function & get_editor_language() {
  418. static $config;
  419. if (!is_array($config)) {
  420. global $language_interface;
  421. @ $editor_lang = Database :: get_language_isocode($language_interface);
  422. $editor_lang = strtolower(str_replace('_', '-', $editor_lang));
  423. if (empty ($editor_lang)) {
  424. $editor_lang = 'en';
  425. }
  426. $language_file = api_get_path(SYS_PATH).'main/inc/lib/fckeditor/editor/lang/'.$editor_lang.'.js';
  427. if (!file_exists($language_file)) {
  428. // If there was no language file, use the English one.
  429. $editor_lang = 'en';
  430. }
  431. $config['DefaultLanguage'] = $editor_lang;
  432. }
  433. return $config;
  434. }
  435. /**
  436. * This method returns default configuration for document repository that is to be used by the editor.
  437. * @return array
  438. */
  439. private function & get_repository_configuration() {
  440. // Preliminary calculations for assembling required paths.
  441. $base_path = $this->BasePath;
  442. $script_name = substr($_SERVER['PHP_SELF'], strlen(api_get_path(REL_PATH)));
  443. $script_path = explode('/', $script_name);
  444. $script_path[count($script_path) - 1] = '';
  445. if (api_is_in_course()) {
  446. $relative_path_prefix = str_repeat('../', count($script_path) - 1);
  447. } else {
  448. $relative_path_prefix = str_repeat('../', count($script_path) - 2);
  449. }
  450. $script_path = implode('/', $script_path);
  451. $script_path = api_get_path(WEB_PATH).$script_path;
  452. $use_advanced_filemanager = api_get_setting('advanced_filemanager') == 'true';
  453. // Let javascripts "know" which file manager has been chosen.
  454. $config['AdvancedFileManager'] = $use_advanced_filemanager;
  455. if (api_is_in_course()) {
  456. if (!api_is_in_group()) {
  457. // 1. We are inside a course and not in a group.
  458. if (api_is_allowed_to_edit()) {
  459. // 1.1. Teacher (tutor and coach are not authorized to change anything in the "content creation" tools)
  460. $config['CreateDocumentWebDir'] = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/';
  461. $config['CreateDocumentDir'] = $relative_path_prefix.'courses/'.api_get_course_path().'/document/';
  462. $config['BaseHref'] = $script_path;
  463. } else {
  464. // 1.2. Student
  465. $config['CreateDocumentWebDir'] = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/shared_folder/sf_user_'.api_get_user_id().'/';
  466. $config['CreateDocumentDir'] = $relative_path_prefix.'courses/'.api_get_course_path().'/document/shared_folder/sf_user_'.api_get_user_id().'/';
  467. $config['BaseHref'] = $script_path;
  468. }
  469. } else {
  470. // 2. Inside a course and inside a group.
  471. global $group_properties;
  472. $config['CreateDocumentWebDir'] = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document'.$group_properties['directory'].'/';
  473. $config['CreateDocumentDir'] = $relative_path_prefix.'courses/'.api_get_course_path().'/document'.$group_properties['directory'].'/';
  474. $config['BaseHref'] = $script_path;
  475. }
  476. } else {
  477. if (api_is_platform_admin() && $_SESSION['this_section'] == 'platform_admin') {
  478. // 3. Platform administration activities.
  479. $config['CreateDocumentWebDir'] = api_get_path(WEB_PATH).'home/default_platform_document/';
  480. $config['CreateDocumentDir'] = api_get_path(WEB_PATH).'home/default_platform_document/'; // A side-effect is in use here.
  481. $config['BaseHref'] = api_get_path(WEB_PATH).'home/default_platform_document/';
  482. } else {
  483. // 4. The user is outside courses.
  484. $config['CreateDocumentWebDir'] = api_get_path('WEB_PATH').'main/upload/users/'.api_get_user_id().'/my_files/';
  485. $config['CreateDocumentDir'] = $relative_path_prefix.'upload/users/'.api_get_user_id().'/my_files/';
  486. $config['BaseHref'] = $script_path;
  487. }
  488. }
  489. // URLs for opening the file browser for different resource types (file types):
  490. if ($use_advanced_filemanager) {
  491. // Double slashes within the following URLs for the advanced file manager are put intentionally. Please, keep them.
  492. // for images
  493. $config['ImageBrowserURL'] = $base_path.'/editor/plugins/ajaxfilemanager/ajaxfilemanager.php';
  494. // for flash
  495. $config['FlashBrowserURL'] = $base_path.'/editor/plugins/ajaxfilemanager/ajaxfilemanager.php';
  496. // for audio files (mp3)
  497. $config['MP3BrowserURL'] = $base_path.'/editor/plugins/ajaxfilemanager/ajaxfilemanager.php';
  498. // for video
  499. $config['VideoBrowserURL'] = $base_path.'/editor/plugins/ajaxfilemanager/ajaxfilemanager.php';
  500. // for video (flv)
  501. $config['MediaBrowserURL'] = $base_path.'/editor/plugins/ajaxfilemanager/ajaxfilemanager.php';
  502. // for links (any resource type)
  503. $config['LinkBrowserURL'] = $base_path.'/editor/plugins/ajaxfilemanager/ajaxfilemanager.php';
  504. } else {
  505. // for images
  506. $config['ImageBrowserURL'] = $base_path.'editor/filemanager/browser/default/browser.html?Type=Images&Connector='.$base_path.'editor/filemanager/connectors/php/connector.php';
  507. // for flash
  508. $config['FlashBrowserURL'] = $base_path.'editor/filemanager/browser/default/browser.html?Type=Flash&Connector='.$base_path.'editor/filemanager/connectors/php/connector.php';
  509. // for audio files (mp3)
  510. $config['MP3BrowserURL'] = $base_path.'editor/filemanager/browser/default/browser.html?Type=MP3&Connector='.$base_path.'editor/filemanager/connectors/php/connector.php';
  511. // for video
  512. $config['VideoBrowserURL'] = $base_path.'editor/filemanager/browser/default/browser.html?Type=Video&Connector='.$base_path.'editor/filemanager/connectors/php/connector.php';
  513. // for video (flv)
  514. $config['MediaBrowserURL'] = $base_path.'editor/filemanager/browser/default/browser.html?Type=Video/flv&Connector='.$base_path.'editor/filemanager/connectors/php/connector.php';
  515. // for links (any resource type)
  516. $config['LinkBrowserURL'] = $base_path.'editor/filemanager/browser/default/browser.html?Type=File&Connector='.$base_path.'editor/filemanager/connectors/php/connector.php';
  517. }
  518. // URLs for making quick uplods for different resource types (file types).
  519. // These URLs are used by the dialogs' quick upload tabs:
  520. // for images
  521. $config['ImageUploadURL'] = $base_path.'editor/filemanager/connectors/php/upload.php?Type=Images';
  522. // for flash
  523. $config['FlashUploadURL'] = $base_path.'editor/filemanager/connectors/php/upload.php?Type=Flash';
  524. // for audio files (mp3)
  525. $config['MP3UploadURL'] = $base_path.'editor/filemanager/connectors/php/upload.php?Type=MP3';
  526. // for video
  527. $config['VideoUploadURL'] = $base_path.'editor/filemanager/connectors/php/upload.php?Type=Video';
  528. // for video (flv)
  529. $config['MediaUploadURL'] = $base_path.'editor/filemanager/connectors/php/upload.php?Type=Video/flv';
  530. // for links (any resource type)
  531. $config['LinkUploadURL'] = $base_path.'editor/filemanager/connectors/php/upload.php?Type=File';
  532. return $config;
  533. }
  534. /**
  535. * This method returns multi-media related configuration data.
  536. * @return array
  537. */
  538. private function & get_media_configuration() {
  539. $config['FlashPlayerAudio'] = Media::get_path(FLASH_PLAYER_AUDIO, REL_PATH);
  540. $config['FlashPlayerVideo'] = Media::get_path(FLASH_PLAYER_VIDEO, REL_PATH);
  541. $config['ScriptSWFObject'] = Media::get_path(SCRIPT_SWFOBJECT, REL_PATH);
  542. $config['ScriptASCIIMathML'] = Media::get_path(SCRIPT_ASCIIMATHML, REL_PATH);
  543. return $config;
  544. }
  545. /**
  546. * This method returns current user specific configuration data.
  547. * @return array
  548. */
  549. private function & get_user_configuration_data() {
  550. $config['UserIsCourseAdmin'] = api_is_allowed_to_edit() ? true : false;
  551. $config['UserIsPlatformAdmin'] = api_is_platform_admin() ? true : false;
  552. return $config;
  553. }
  554. /**
  555. * This method returns detected configuration data about editor's MimeTeX plugin.
  556. * @return array
  557. */
  558. private function & get_mimetex_plugin_configuration() {
  559. static $config;
  560. if (!isset($config)) {
  561. $config = array();
  562. if (is_array($this->Config['LoadPlugin']) && in_array('mimetex', $this->Config['LoadPlugin'])) {
  563. $server_base = api_get_path(WEB_SERVER_ROOT_PATH);
  564. $server_base_parts = explode('/', $server_base);
  565. $url_relative = 'cgi-bin/mimetex' . ( IS_WINDOWS_OS ? '.exe' : '.cgi' );
  566. if (!isset($this->Config['MimetexExecutableInstalled'])) {
  567. $this->Config['MimetexExecutableDetectionMethod'] = 'detect';
  568. }
  569. if ($this->Config['MimetexExecutableInstalled'] == 'detect') {
  570. $detection_method = isset($this->Config['MimetexExecutableDetectionMethod']) ? $this->Config['MimetexExecutableDetectionMethod'] : 'bootstrap_ip';
  571. $detection_timeout = isset($this->Config['MimetexExecutableDetectionTimeout']) ? $this->Config['MimetexExecutableDetectionTimeout'] : 0.05;
  572. switch ($detection_method) {
  573. case 'bootstrap_ip':
  574. $detection_url = $server_base_parts[0] . '//127.0.0.1/';
  575. break;
  576. case 'localhost':
  577. $detection_url = $server_base_parts[0] . '//localhost/';
  578. break;
  579. case 'ip':
  580. $detection_url = $server_base_parts[0] . '//' . $_SERVER['SERVER_ADDR'] . '/';
  581. break;
  582. default:
  583. $detection_url = $server_base_parts[0] . '//' . $_SERVER['SERVER_NAME'] . '/';
  584. }
  585. $detection_url .= $url_relative . '?' . rand();
  586. $config['IsMimetexInstalled'] = self::url_exists($detection_url, $detection_timeout);
  587. } else {
  588. $config['IsMimetexInstalled'] = $this->Config['MimetexExecutableInstalled'];
  589. }
  590. $config['MimetexUrl'] = $server_base . $url_relative;
  591. }
  592. // Cleaning detection related settings, we don't need them anymore.
  593. unset($this->Config['MimetexExecutableInstalled']);
  594. unset($this->Config['MimetexExecutableDetectionMethod']);
  595. unset($this->Config['MimetexExecutableDetectionTimeout']);
  596. }
  597. return $config;
  598. }
  599. /*
  600. * Checks whether a given url exists.
  601. * @param string $url
  602. * @param int $timeout
  603. * @return boolean
  604. * @author Ivan Tcholakov, FEB-2009
  605. */
  606. private function url_exists($url, $timeout = 30) {
  607. $parsed = parse_url($url);
  608. $scheme = isset($parsed['scheme']) ? $parsed['scheme'] : 'http';
  609. $host = $parsed['host'];
  610. $port = isset($parsed['port']) ? $parsed['port'] : ($scheme == 'http' ? 80 : ($scheme == 'https' ? 443 : -1 ));
  611. $file_exists = false;
  612. $fp = @fsockopen($host, $port, $errno, $errstr, $timeout);
  613. if ($fp) {
  614. $request = "HEAD ".$url." / HTTP/1.1\r\n";
  615. $request .= "Host: ".$host."\r\n";
  616. $request .= "Connection: Close\r\n\r\n";
  617. @fwrite($fp, $request);
  618. while (!@feof($fp)) {
  619. $header = @fgets($fp, 128);
  620. if(@preg_match('#HTTP/1.1 200 OK#', $header)) {
  621. $file_exists = true;
  622. break;
  623. }
  624. }
  625. }
  626. @fclose($fp);
  627. return $file_exists;
  628. }
  629. /*
  630. * Convers a string from camel case to underscore.
  631. * @param string $string
  632. * @return string
  633. */
  634. private function camel_case_to_underscore($string) {
  635. return strtolower(preg_replace('/([a-z])([A-Z])/', "$1_$2", $string));
  636. }
  637. }