fckeditor.php 28 KB

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