fckeditor.php 28 KB

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