diff --git a/main/inc/lib/fckeditor/fckeditor.php b/main/inc/lib/fckeditor/fckeditor.php index ceb8c130e8..506b02e976 100755 --- a/main/inc/lib/fckeditor/fckeditor.php +++ b/main/inc/lib/fckeditor/fckeditor.php @@ -37,642 +37,642 @@ function FCKeditor_IsCompatibleBrowser() { - if ( isset( $_SERVER ) ) { - $sAgent = $_SERVER['HTTP_USER_AGENT'] ; - } - else { - global $HTTP_SERVER_VARS ; - if ( isset( $HTTP_SERVER_VARS ) ) { - $sAgent = $HTTP_SERVER_VARS['HTTP_USER_AGENT'] ; - } - else { - global $HTTP_USER_AGENT ; - $sAgent = $HTTP_USER_AGENT ; - } - } - - if ( strpos($sAgent, 'MSIE') !== false && strpos($sAgent, 'mac') === false && strpos($sAgent, 'Opera') === false ) - { - $iVersion = (float)substr($sAgent, strpos($sAgent, 'MSIE') + 5, 3) ; - return ($iVersion >= 5.5) ; - } - else if ( strpos($sAgent, 'Gecko/') !== false ) - { - $iVersion = (int)substr($sAgent, strpos($sAgent, 'Gecko/') + 6, 8) ; - return ($iVersion >= 20030210) ; - } - else if ( strpos($sAgent, 'Opera/') !== false ) - { - $fVersion = (float)substr($sAgent, strpos($sAgent, 'Opera/') + 6, 4) ; - return ($fVersion >= 9.5) ; - } - else if ( preg_match( "|AppleWebKit/(\d+)|i", $sAgent, $matches ) ) - { - $iVersion = $matches[1] ; - return ( $matches[1] >= 522 ) ; - } - else - return false ; + if ( isset( $_SERVER ) ) { + $sAgent = $_SERVER['HTTP_USER_AGENT'] ; + } + else { + global $HTTP_SERVER_VARS ; + if ( isset( $HTTP_SERVER_VARS ) ) { + $sAgent = $HTTP_SERVER_VARS['HTTP_USER_AGENT'] ; + } + else { + global $HTTP_USER_AGENT ; + $sAgent = $HTTP_USER_AGENT ; + } + } + + if ( strpos($sAgent, 'MSIE') !== false && strpos($sAgent, 'mac') === false && strpos($sAgent, 'Opera') === false ) + { + $iVersion = (float)substr($sAgent, strpos($sAgent, 'MSIE') + 5, 3) ; + return ($iVersion >= 5.5) ; + } + else if ( strpos($sAgent, 'Gecko/') !== false ) + { + $iVersion = (int)substr($sAgent, strpos($sAgent, 'Gecko/') + 6, 8) ; + return ($iVersion >= 20030210) ; + } + else if ( strpos($sAgent, 'Opera/') !== false ) + { + $fVersion = (float)substr($sAgent, strpos($sAgent, 'Opera/') + 6, 4) ; + return ($fVersion >= 9.5) ; + } + else if ( preg_match( "|AppleWebKit/(\d+)|i", $sAgent, $matches ) ) + { + $iVersion = $matches[1] ; + return ( $matches[1] >= 522 ) ; + } + else + return false ; } class FCKeditor { - /** - * Name of the FCKeditor instance. - * - * @access protected - * @var string - */ - public $InstanceName ; - /** - * Path to FCKeditor relative to the document root. - * - * @var string - */ - public $BasePath ; - /** - * Width of the FCKeditor. - * Examples: 100%, 600 - * - * @var mixed - */ - public $Width ; - /** - * Height of the FCKeditor. - * Examples: 400, 50% - * - * @var mixed - */ - public $Height ; - /** - * Name of the toolbar to load. - * - * @var string - */ - public $ToolbarSet ; - /** - * Initial value. - * - * @var string - */ - public $Value ; - /** - * This is where additional configuration can be passed. - * Example: - * $oFCKeditor->Config['EnterMode'] = 'br'; - * - * @var array - */ - public $Config ; - - /** - * Main Constructor. - * Refer to the _samples/php directory for examples. - * - * @param string $instanceName - */ - public function __construct( $instanceName ) - { - $this->InstanceName = $instanceName ; - $this->BasePath = '/fckeditor/' ; - $this->Width = '100%' ; - $this->Height = '200' ; - $this->ToolbarSet = 'Default' ; - $this->Value = '' ; - - $this->Config = array() ; - } - - /** - * Display FCKeditor. - * - */ - public function Create() - { - echo $this->CreateHtml() ; - } - - /** - * Return the HTML code required to run FCKeditor. - * - * @return string - */ - public function CreateHtml() - { - // Adaptation for the Chamilo LMS --------------------------------------------------------- - - $this->BasePath = api_get_path(REL_PATH).'main/inc/lib/fckeditor/'; - - $config = $this->get_custom_configuration(); - $this->read_configuration($config); - - $config = $this->get_default_configuration(); - $this->read_configuration($config); - - if ((api_is_allowed_to_edit() || api_is_platform_admin()) && isset($this->Config['BlockCopyPaste'])) { - $this->Config['BlockCopyPaste'] = false; - } - - //---------------------------------------------------------------------------------------- - - $HtmlValue = htmlspecialchars( $this->Value ) ; - - $Html = '' ; - - if ( $this->IsCompatible() ) - { - if ( api_get_setting('server_type') == 'test' ) - $File = 'fckeditor.original.html' ; - else - $File = 'fckeditor.html' ; - - $Link = "{$this->BasePath}editor/{$File}?InstanceName={$this->InstanceName}" ; - - if ( $this->ToolbarSet != '' ) - $Link .= "&Toolbar={$this->ToolbarSet}" ; - - // Render the linked hidden field. - $Html .= "InstanceName}\" name=\"{$this->InstanceName}\" value=\"{$HtmlValue}\" style=\"display:none\" />" ; - - // Render the configurations hidden field. - $Html .= "InstanceName}___Config\" value=\"" . $this->GetConfigFieldString() . "\" style=\"display:none\" />" ; - - // Render the editor IFRAME. - $Html .= "" ; - } - else - { - if ( strpos( $this->Width, '%' ) === false ) - $WidthCSS = $this->Width . 'px' ; - else - $WidthCSS = $this->Width ; - - if ( strpos( $this->Height, '%' ) === false ) - $HeightCSS = $this->Height . 'px' ; - else - $HeightCSS = $this->Height ; - - $Html .= "" ; - } - - return $Html ; - } - - /** - * Returns true if browser is compatible with FCKeditor. - * - * @return boolean - */ - public function IsCompatible() - { - return FCKeditor_IsCompatibleBrowser() ; - } - - /** - * Get settings from Config array as a single string. - * - * @access protected - * @return string - */ - public function GetConfigFieldString() - { - $sParams = '' ; - $bFirst = true ; - - foreach ( $this->Config as $sKey => $sValue ) - { - if ( !$bFirst ) { - $sParams .= '&' ; - } else { - $bFirst = false ; - } - if ( is_string( $sValue ) ) { - $sParams .= $this->EncodeConfig( $sKey ) . '=' . $this->EncodeConfig( $sValue ) ; - } else { - $sParams .= $this->EncodeConfig( $sKey ) . '=' . $this->EncodeConfig( $this->to_js( $sValue ) ) ; - } - } - - return $sParams ; - } - - /** - * Encode characters that may break the configuration string - * generated by GetConfigFieldString(). - * - * @access protected - * @param string $valueToEncode - * @return string - */ - public function EncodeConfig( $valueToEncode ) - { - $chars = array( - '&' => '%26', - '=' => '%3D', - '"' => '%22', - '%' => '%25' - ) ; - - return strtr( $valueToEncode, $chars ) ; - } - - /** - * Converts a PHP variable into its Javascript equivalent. - * The code of this method has been "borrowed" from the funcion drupal_to_js() within the Drupal CMS. - * @param mixed $var The variable to be converted into Javascript syntax - * @return string Returns a string - * Note: This function is similar to json_encode(), in addition it produces HTML-safe strings, i.e. with <, > and & escaped. - * @link http://drupal.org/ - */ - private function to_js( $var ) { - switch ( gettype( $var ) ) { - case 'boolean' : - return $var ? 'true' : 'false' ; // Lowercase necessary! - case 'integer' : - case 'double' : - return (string) $var ; - case 'resource' : - case 'string' : - return '"' . str_replace( array( "\r", "\n", "<", ">", "&" ), array( '\r', '\n', '\x3c', '\x3e', '\x26' ), addslashes( $var ) ) . '"' ; - case 'array' : - // Arrays in JSON can't be associative. If the array is empty or if it - // has sequential whole number keys starting with 0, it's not associative - // so we can go ahead and convert it as an array. - if ( empty( $var ) || array_keys( $var ) === range( 0, sizeof( $var ) - 1 ) ) { - $output = array() ; - foreach ( $var as $v ) { - $output[] = $this->to_js( $v ) ; - } - return '[ ' . implode( ', ', $output ) . ' ]' ; - } - // Otherwise, fall through to convert the array as an object. - case 'object' : - $output = array() ; - foreach ( $var as $k => $v ) { - $output[] = $this->to_js( strval( $k ) ) . ': ' . $this->to_js( $v ) ; - } - return '{ ' . implode(', ', $output) . ' }' ; - default: - return 'null' ; - } - } - - /** - * This method reads configuration data for the current editor's instance without overriding settings that already exist. - * @return array - */ - function read_configuration(& $config) { - $toolbar_set = $this->ToolbarSet; - $toolbar_set_maximized = $this->ToolbarSet.'Maximized'; - foreach ($config as $key => $value) { - switch ($key) { - case 'ToolbarSets': - if (!empty($toolbar_set) && $toolbar_set != 'Default') { - if (is_array($value)) { - if (isset($value['Normal'])) { - if (!isset($this->Config[$key][$toolbar_set])) { - $this->Config[$key][$toolbar_set] = $value['Normal']; - } - } - if (isset($value['Maximized'])) { - if (!isset($this->Config[$key][$toolbar_set_maximized])) { - $this->Config[$key][$toolbar_set_maximized] = $value['Maximized']; - } - } - } - } - break; - case 'Width': - $this->Config[$key] = (string) $value; - $this->Width = $this->Config[$key]; - break; - case 'Height': - $this->Config[$key] = (string) $value; - $this->Height = $this->Config[$key]; - break; - default: - if (!isset($this->Config[$key])) { - $this->Config[$key] = $value; - } - } - } - } - - /** - * This method returns editor's custom configuration settings read from php-files. - * @return array Custom configuration data. - */ - private function & get_custom_configuration() { - static $config; - if (!isset($config)) { - require api_get_path(LIBRARY_PATH).'fckeditor/myconfig.php'; - } - $toolbar_dir = isset($config['ToolbarSets']['Directory']) ? $config['ToolbarSets']['Directory'] : 'default'; - return array_merge($config, $this->get_custom_toolbar_configuration($toolbar_dir)); - } - - /** - * This method returns editor's toolbar configuration settings read from a php-file. - * @return array Toolbar configuration data. - */ - private function & get_custom_toolbar_configuration($toolbar_dir) { - static $toolbar_config = array('Default' => array()); - if (!isset($toolbar_config[$this->ToolbarSet])) { - $toolbar_config[$this->ToolbarSet] = array(); - if (preg_match('/[a-zA-Z_]+/', $toolbar_dir) && preg_match('/[a-zA-Z_]+/', $this->ToolbarSet)) { // A security check. - // Seeking the toolbar. - @include api_get_path(LIBRARY_PATH).'fckeditor/toolbars/'.$toolbar_dir.'/'.api_camel_case_to_underscore($this->ToolbarSet).'.php'; - if (!isset($config['ToolbarSets']['Normal'])) { - // No toolbar has been found yet. - if ($toolbar_dir == 'default') { - // It does not exist in default toolbar definitions, giving up. - $this->ToolbarSet = 'Default'; - } else { - // The custom toolbar does not exist, then trying to load the default one. - @include api_get_path(LIBRARY_PATH).'fckeditor/toolbars/default/'.api_camel_case_to_underscore($this->ToolbarSet).'.php'; - if (!isset($config['ToolbarSets']['Normal'])) { - // It does not exist in default toolbar definitions, giving up. - $this->ToolbarSet = 'Default'; - } else { - $toolbar_config[$this->ToolbarSet] = $config; - } - } - } else { - $toolbar_config[$this->ToolbarSet] = $config; - } - } else { - $this->ToolbarSet = 'Default'; - } - } - return $toolbar_config[$this->ToolbarSet]; - } - - /** - * This method returns automatically determined editor's configuration settings (default settings). - * @return array - */ - private function & get_default_configuration() { - return array_merge( - self::get_javascript_custom_configuration_file(), - self::get_css_configuration(), - self::get_editor_language(), - $this->get_repository_configuration(), - self::get_media_configuration(), - self::get_user_configuration_data(), - $this->get_mimetex_plugin_configuration() - ); - } - - /** - * This method returns the path to the javascript custom configuration file. - * @return array - */ - private function & get_javascript_custom_configuration_file() { - return array('CustomConfigurationsPath' => api_get_path(REL_PATH).'main/inc/lib/fckeditor/myconfig.js'); - } - - /** - * This method returns CSS-related configuration data that has been determined by the system. - * @return array - */ - private function & get_css_configuration() { - $config['EditorAreaCSS'] = api_get_path(REL_PATH).'main/css/'.api_get_setting('stylesheets').'/default.css'; - $config['ToolbarComboPreviewCSS'] = $config['EditorAreaCSS']; - return $config; - } - - /** - * This method determines editor's interface language and returns it as compatible with the editor langiage code. - * @return array - */ - private function & get_editor_language() { - static $config; - if (!is_array($config)) { - $code_translation_table = array('' => 'en', 'sr' => 'sr-latn', 'zh' => 'zh-cn', 'zh-tw' => 'zh'); - $editor_lang = strtolower(str_replace('_', '-', api_get_language_isocode())); - $editor_lang = isset($code_translation_table[$editor_lang]) ? $code_translation_table[$editor_lang] : $editor_lang; - $editor_lang = file_exists(api_get_path(SYS_PATH).'main/inc/lib/fckeditor/editor/lang/'.$editor_lang.'.js') ? $editor_lang : 'en'; - $config['DefaultLanguage'] = $editor_lang; - $config['ContentLangDirection'] = api_get_text_direction($editor_lang); - } - return $config; - } - - /** - * This method returns default configuration for document repository that is to be used by the editor. - * @return array - */ - private function & get_repository_configuration() { - - // Preliminary calculations for assembling required paths. - $base_path = $this->BasePath; - $script_name = substr($_SERVER['PHP_SELF'], strlen(api_get_path(REL_PATH))); - $script_path = explode('/', $script_name); - $script_path[count($script_path) - 1] = ''; - if (api_is_in_course()) { - $relative_path_prefix = str_repeat('../', count($script_path) - 1); - } else { - $relative_path_prefix = str_repeat('../', count($script_path) - 2); - } - $script_path = implode('/', $script_path); - $script_path = api_get_path(WEB_PATH).$script_path; - - $use_advanced_filemanager = api_get_setting('advanced_filemanager') == 'true'; - // Let javascripts "know" which file manager has been chosen. - $config['AdvancedFileManager'] = $use_advanced_filemanager; - - if (api_is_in_course()) { - if (!api_is_in_group()) { - // 1. We are inside a course and not in a group. - if (api_is_allowed_to_edit()) { - // 1.1. Teacher (tutor and coach are not authorized to change anything in the "content creation" tools) - $config['CreateDocumentWebDir'] = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/'; - $config['CreateDocumentDir'] = $relative_path_prefix.'courses/'.api_get_course_path().'/document/'; - $config['BaseHref'] = $script_path; - } else { - // 1.2. Student - $current_session_id = api_get_session_id(); - if($current_session_id==0) - { - - $config['CreateDocumentWebDir'] = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/shared_folder/sf_user_'.api_get_user_id().'/'; - $config['CreateDocumentDir'] = $relative_path_prefix.'courses/'.api_get_course_path().'/document/shared_folder/sf_user_'.api_get_user_id().'/'; - $config['BaseHref'] = $script_path; - } - else - { - $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().'/'; - $config['CreateDocumentDir'] = $relative_path_prefix.'courses/'.api_get_course_path().'/document/shared_folder_session_'.$current_session_id.'/sf_user_'.api_get_user_id().'/'; - $config['BaseHref'] = $script_path; - } - } - } else { - // 2. Inside a course and inside a group. - global $group_properties; - $config['CreateDocumentWebDir'] = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document'.$group_properties['directory'].'/'; - $config['CreateDocumentDir'] = $relative_path_prefix.'courses/'.api_get_course_path().'/document'.$group_properties['directory'].'/'; - $config['BaseHref'] = $script_path; - } - } else { - if (api_is_platform_admin() && $_SESSION['this_section'] == 'platform_admin') { - // 3. Platform administration activities. - $config['CreateDocumentWebDir'] = api_get_path(WEB_PATH).'home/default_platform_document/'; - $config['CreateDocumentDir'] = api_get_path(WEB_PATH).'home/default_platform_document/'; // A side-effect is in use here. - $config['BaseHref'] = api_get_path(WEB_PATH).'home/default_platform_document/'; - } else { - // 4. The user is outside courses. - $config['CreateDocumentWebDir'] = api_get_path(WEB_PATH).'main/upload/users/'.api_get_user_id().'/my_files/'; - $config['CreateDocumentDir'] = $relative_path_prefix.'upload/users/'.api_get_user_id().'/my_files/'; - $config['BaseHref'] = $script_path; - } - } - - // URLs for opening the file browser for different resource types (file types): - if ($use_advanced_filemanager) { - // Double slashes within the following URLs for the advanced file manager are put intentionally. Please, keep them. - // for images - $config['ImageBrowserURL'] = $base_path.'/editor/plugins/ajaxfilemanager/ajaxfilemanager.php'; - // for flash - $config['FlashBrowserURL'] = $base_path.'/editor/plugins/ajaxfilemanager/ajaxfilemanager.php'; - // for audio files (mp3) - $config['MP3BrowserURL'] = $base_path.'/editor/plugins/ajaxfilemanager/ajaxfilemanager.php'; - // for video - $config['VideoBrowserURL'] = $base_path.'/editor/plugins/ajaxfilemanager/ajaxfilemanager.php'; - // for video (flv) - $config['MediaBrowserURL'] = $base_path.'/editor/plugins/ajaxfilemanager/ajaxfilemanager.php'; - // for links (any resource type) - $config['LinkBrowserURL'] = $base_path.'/editor/plugins/ajaxfilemanager/ajaxfilemanager.php'; - } else { - // for images - $config['ImageBrowserURL'] = $base_path.'editor/filemanager/browser/default/browser.html?Type=Images&Connector='.$base_path.'editor/filemanager/connectors/php/connector.php'; - // for flash - $config['FlashBrowserURL'] = $base_path.'editor/filemanager/browser/default/browser.html?Type=Flash&Connector='.$base_path.'editor/filemanager/connectors/php/connector.php'; - // for audio files (mp3) - $config['MP3BrowserURL'] = $base_path.'editor/filemanager/browser/default/browser.html?Type=MP3&Connector='.$base_path.'editor/filemanager/connectors/php/connector.php'; - // for video - $config['VideoBrowserURL'] = $base_path.'editor/filemanager/browser/default/browser.html?Type=Video&Connector='.$base_path.'editor/filemanager/connectors/php/connector.php'; - // for video (flv) - $config['MediaBrowserURL'] = $base_path.'editor/filemanager/browser/default/browser.html?Type=Video/flv&Connector='.$base_path.'editor/filemanager/connectors/php/connector.php'; - // for links (any resource type) - $config['LinkBrowserURL'] = $base_path.'editor/filemanager/browser/default/browser.html?Type=File&Connector='.$base_path.'editor/filemanager/connectors/php/connector.php'; - } - - // URLs for making quick uplods for different resource types (file types). - // These URLs are used by the dialogs' quick upload tabs: - // for images - $config['ImageUploadURL'] = $base_path.'editor/filemanager/connectors/php/upload.php?Type=Images'; - // for flash - $config['FlashUploadURL'] = $base_path.'editor/filemanager/connectors/php/upload.php?Type=Flash'; - // for audio files (mp3) - $config['MP3UploadURL'] = $base_path.'editor/filemanager/connectors/php/upload.php?Type=MP3'; - // for video - $config['VideoUploadURL'] = $base_path.'editor/filemanager/connectors/php/upload.php?Type=Video'; - // for video (flv) - $config['MediaUploadURL'] = $base_path.'editor/filemanager/connectors/php/upload.php?Type=Video/flv'; - // for links (any resource type) - $config['LinkUploadURL'] = $base_path.'editor/filemanager/connectors/php/upload.php?Type=File'; - - return $config; - } - - /** - * This method returns multi-media related configuration data. - * @return array - */ - private function & get_media_configuration() { - $config['FlashPlayerAudio'] = api_get_path(TO_REL, FLASH_PLAYER_AUDIO); - $config['FlashPlayerVideo'] = api_get_path(TO_REL, FLASH_PLAYER_VIDEO); - $config['ScriptSWFObject'] = api_get_path(TO_REL, SCRIPT_SWFOBJECT); - $config['ScriptASCIIMathML'] = api_get_path(TO_REL, SCRIPT_ASCIIMATHML); - return $config; - } - - /** - * This method returns current user specific configuration data. - * @return array - */ - private function & get_user_configuration_data() { - $config['UserIsCourseAdmin'] = api_is_allowed_to_edit() ? true : false; - $config['UserIsPlatformAdmin'] = api_is_platform_admin() ? true : false; - return $config; - } - - /** - * This method returns detected configuration data about editor's MimeTeX plugin. - * @return array - */ - private function & get_mimetex_plugin_configuration() { - static $config; - if (!isset($config)) { - $config = array(); - if (is_array($this->Config['LoadPlugin']) && in_array('mimetex', $this->Config['LoadPlugin'])) { - $server_base = api_get_path(WEB_SERVER_ROOT_PATH); - $server_base_parts = explode('/', $server_base); - $url_relative = 'cgi-bin/mimetex' . ( IS_WINDOWS_OS ? '.exe' : '.cgi' ); - if (!isset($this->Config['MimetexExecutableInstalled'])) { - $this->Config['MimetexExecutableDetectionMethod'] = 'detect'; - } - if ($this->Config['MimetexExecutableInstalled'] == 'detect') { - $detection_method = isset($this->Config['MimetexExecutableDetectionMethod']) ? $this->Config['MimetexExecutableDetectionMethod'] : 'bootstrap_ip'; - $detection_timeout = isset($this->Config['MimetexExecutableDetectionTimeout']) ? $this->Config['MimetexExecutableDetectionTimeout'] : 0.05; - switch ($detection_method) { - case 'bootstrap_ip': - $detection_url = $server_base_parts[0] . '//127.0.0.1/'; - break; - case 'localhost': - $detection_url = $server_base_parts[0] . '//localhost/'; - break; - case 'ip': - $detection_url = $server_base_parts[0] . '//' . $_SERVER['SERVER_ADDR'] . '/'; - break; - default: - $detection_url = $server_base_parts[0] . '//' . $_SERVER['SERVER_NAME'] . '/'; - } - $detection_url .= $url_relative . '?' . rand(); - $config['IsMimetexInstalled'] = self::url_exists($detection_url, $detection_timeout); - } else { - $config['IsMimetexInstalled'] = $this->Config['MimetexExecutableInstalled']; - } - $config['MimetexUrl'] = api_add_trailing_slash($server_base) . $url_relative; - } - // Cleaning detection related settings, we don't need them anymore. - unset($this->Config['MimetexExecutableInstalled']); - unset($this->Config['MimetexExecutableDetectionMethod']); - unset($this->Config['MimetexExecutableDetectionTimeout']); - } - return $config; - } - - /* - * Checks whether a given url exists. - * @param string $url - * @param int $timeout - * @return boolean - * @author Ivan Tcholakov, FEB-2009 - */ - private function url_exists($url, $timeout = 30) { - $parsed = parse_url($url); - $scheme = isset($parsed['scheme']) ? $parsed['scheme'] : 'http'; - $host = $parsed['host']; - $port = isset($parsed['port']) ? $parsed['port'] : ($scheme == 'http' ? 80 : ($scheme == 'https' ? 443 : -1 )); - - $file_exists = false; - $fp = @fsockopen($host, $port, $errno, $errstr, $timeout); - if ($fp) { - $request = "HEAD ".$url." / HTTP/1.1\r\n"; - $request .= "Host: ".$host."\r\n"; - $request .= "Connection: Close\r\n\r\n"; - - @fwrite($fp, $request); - while (!@feof($fp)) { - $header = @fgets($fp, 128); - if(@preg_match('#HTTP/1.1 200 OK#', $header)) { - $file_exists = true; - break; - } - } - } - @fclose($fp); - return $file_exists; - } + /** + * Name of the FCKeditor instance. + * + * @access protected + * @var string + */ + public $InstanceName ; + /** + * Path to FCKeditor relative to the document root. + * + * @var string + */ + public $BasePath ; + /** + * Width of the FCKeditor. + * Examples: 100%, 600 + * + * @var mixed + */ + public $Width ; + /** + * Height of the FCKeditor. + * Examples: 400, 50% + * + * @var mixed + */ + public $Height ; + /** + * Name of the toolbar to load. + * + * @var string + */ + public $ToolbarSet ; + /** + * Initial value. + * + * @var string + */ + public $Value ; + /** + * This is where additional configuration can be passed. + * Example: + * $oFCKeditor->Config['EnterMode'] = 'br'; + * + * @var array + */ + public $Config ; + + /** + * Main Constructor. + * Refer to the _samples/php directory for examples. + * + * @param string $instanceName + */ + public function __construct( $instanceName ) + { + $this->InstanceName = $instanceName ; + $this->BasePath = '/fckeditor/' ; + $this->Width = '100%' ; + $this->Height = '200' ; + $this->ToolbarSet = 'Default' ; + $this->Value = '' ; + + $this->Config = array() ; + } + + /** + * Display FCKeditor. + * + */ + public function Create() + { + echo $this->CreateHtml() ; + } + + /** + * Return the HTML code required to run FCKeditor. + * + * @return string + */ + public function CreateHtml() + { + // Adaptation for the Chamilo LMS --------------------------------------------------------- + + $this->BasePath = api_get_path(REL_PATH).'main/inc/lib/fckeditor/'; + + $config = $this->get_custom_configuration(); + $this->read_configuration($config); + + $config = $this->get_default_configuration(); + $this->read_configuration($config); + + if ((api_is_allowed_to_edit() || api_is_platform_admin()) && isset($this->Config['BlockCopyPaste'])) { + $this->Config['BlockCopyPaste'] = false; + } + + //---------------------------------------------------------------------------------------- + + $HtmlValue = htmlspecialchars( $this->Value ) ; + + $Html = '' ; + + if ( $this->IsCompatible() ) + { + if ( api_get_setting('server_type') == 'test' ) + $File = 'fckeditor.original.html' ; + else + $File = 'fckeditor.html' ; + + $Link = "{$this->BasePath}editor/{$File}?InstanceName={$this->InstanceName}" ; + + if ( $this->ToolbarSet != '' ) + $Link .= "&Toolbar={$this->ToolbarSet}" ; + + // Render the linked hidden field. + $Html .= "InstanceName}\" name=\"{$this->InstanceName}\" value=\"{$HtmlValue}\" style=\"display:none\" />" ; + + // Render the configurations hidden field. + $Html .= "InstanceName}___Config\" value=\"" . $this->GetConfigFieldString() . "\" style=\"display:none\" />" ; + + // Render the editor IFRAME. + $Html .= "" ; + } + else + { + if ( strpos( $this->Width, '%' ) === false ) + $WidthCSS = $this->Width . 'px' ; + else + $WidthCSS = $this->Width ; + + if ( strpos( $this->Height, '%' ) === false ) + $HeightCSS = $this->Height . 'px' ; + else + $HeightCSS = $this->Height ; + + $Html .= "" ; + } + + return $Html ; + } + + /** + * Returns true if browser is compatible with FCKeditor. + * + * @return boolean + */ + public function IsCompatible() + { + return FCKeditor_IsCompatibleBrowser() ; + } + + /** + * Get settings from Config array as a single string. + * + * @access protected + * @return string + */ + public function GetConfigFieldString() + { + $sParams = '' ; + $bFirst = true ; + + foreach ( $this->Config as $sKey => $sValue ) + { + if ( !$bFirst ) { + $sParams .= '&' ; + } else { + $bFirst = false ; + } + if ( is_string( $sValue ) ) { + $sParams .= $this->EncodeConfig( $sKey ) . '=' . $this->EncodeConfig( $sValue ) ; + } else { + $sParams .= $this->EncodeConfig( $sKey ) . '=' . $this->EncodeConfig( $this->to_js( $sValue ) ) ; + } + } + + return $sParams ; + } + + /** + * Encode characters that may break the configuration string + * generated by GetConfigFieldString(). + * + * @access protected + * @param string $valueToEncode + * @return string + */ + public function EncodeConfig( $valueToEncode ) + { + $chars = array( + '&' => '%26', + '=' => '%3D', + '"' => '%22', + '%' => '%25' + ) ; + + return strtr( $valueToEncode, $chars ) ; + } + + /** + * Converts a PHP variable into its Javascript equivalent. + * The code of this method has been "borrowed" from the funcion drupal_to_js() within the Drupal CMS. + * @param mixed $var The variable to be converted into Javascript syntax + * @return string Returns a string + * Note: This function is similar to json_encode(), in addition it produces HTML-safe strings, i.e. with <, > and & escaped. + * @link http://drupal.org/ + */ + private function to_js( $var ) { + switch ( gettype( $var ) ) { + case 'boolean' : + return $var ? 'true' : 'false' ; // Lowercase necessary! + case 'integer' : + case 'double' : + return (string) $var ; + case 'resource' : + case 'string' : + return '"' . str_replace( array( "\r", "\n", "<", ">", "&" ), array( '\r', '\n', '\x3c', '\x3e', '\x26' ), addslashes( $var ) ) . '"' ; + case 'array' : + // Arrays in JSON can't be associative. If the array is empty or if it + // has sequential whole number keys starting with 0, it's not associative + // so we can go ahead and convert it as an array. + if ( empty( $var ) || array_keys( $var ) === range( 0, sizeof( $var ) - 1 ) ) { + $output = array() ; + foreach ( $var as $v ) { + $output[] = $this->to_js( $v ) ; + } + return '[ ' . implode( ', ', $output ) . ' ]' ; + } + // Otherwise, fall through to convert the array as an object. + case 'object' : + $output = array() ; + foreach ( $var as $k => $v ) { + $output[] = $this->to_js( strval( $k ) ) . ': ' . $this->to_js( $v ) ; + } + return '{ ' . implode(', ', $output) . ' }' ; + default: + return 'null' ; + } + } + + /** + * This method reads configuration data for the current editor's instance without overriding settings that already exist. + * @return array + */ + function read_configuration(& $config) { + $toolbar_set = $this->ToolbarSet; + $toolbar_set_maximized = $this->ToolbarSet.'Maximized'; + foreach ($config as $key => $value) { + switch ($key) { + case 'ToolbarSets': + if (!empty($toolbar_set) && $toolbar_set != 'Default') { + if (is_array($value)) { + if (isset($value['Normal'])) { + if (!isset($this->Config[$key][$toolbar_set])) { + $this->Config[$key][$toolbar_set] = $value['Normal']; + } + } + if (isset($value['Maximized'])) { + if (!isset($this->Config[$key][$toolbar_set_maximized])) { + $this->Config[$key][$toolbar_set_maximized] = $value['Maximized']; + } + } + } + } + break; + case 'Width': + $this->Config[$key] = (string) $value; + $this->Width = $this->Config[$key]; + break; + case 'Height': + $this->Config[$key] = (string) $value; + $this->Height = $this->Config[$key]; + break; + default: + if (!isset($this->Config[$key])) { + $this->Config[$key] = $value; + } + } + } + } + + /** + * This method returns editor's custom configuration settings read from php-files. + * @return array Custom configuration data. + */ + private function & get_custom_configuration() { + static $config; + if (!isset($config)) { + require api_get_path(LIBRARY_PATH).'fckeditor/myconfig.php'; + } + $toolbar_dir = isset($config['ToolbarSets']['Directory']) ? $config['ToolbarSets']['Directory'] : 'default'; + return array_merge($config, $this->get_custom_toolbar_configuration($toolbar_dir)); + } + + /** + * This method returns editor's toolbar configuration settings read from a php-file. + * @return array Toolbar configuration data. + */ + private function & get_custom_toolbar_configuration($toolbar_dir) { + static $toolbar_config = array('Default' => array()); + if (!isset($toolbar_config[$this->ToolbarSet])) { + $toolbar_config[$this->ToolbarSet] = array(); + if (preg_match('/[a-zA-Z_]+/', $toolbar_dir) && preg_match('/[a-zA-Z_]+/', $this->ToolbarSet)) { // A security check. + // Seeking the toolbar. + @include api_get_path(LIBRARY_PATH).'fckeditor/toolbars/'.$toolbar_dir.'/'.api_camel_case_to_underscore($this->ToolbarSet).'.php'; + if (!isset($config['ToolbarSets']['Normal'])) { + // No toolbar has been found yet. + if ($toolbar_dir == 'default') { + // It does not exist in default toolbar definitions, giving up. + $this->ToolbarSet = 'Default'; + } else { + // The custom toolbar does not exist, then trying to load the default one. + @include api_get_path(LIBRARY_PATH).'fckeditor/toolbars/default/'.api_camel_case_to_underscore($this->ToolbarSet).'.php'; + if (!isset($config['ToolbarSets']['Normal'])) { + // It does not exist in default toolbar definitions, giving up. + $this->ToolbarSet = 'Default'; + } else { + $toolbar_config[$this->ToolbarSet] = $config; + } + } + } else { + $toolbar_config[$this->ToolbarSet] = $config; + } + } else { + $this->ToolbarSet = 'Default'; + } + } + return $toolbar_config[$this->ToolbarSet]; + } + + /** + * This method returns automatically determined editor's configuration settings (default settings). + * @return array + */ + private function & get_default_configuration() { + return array_merge( + self::get_javascript_custom_configuration_file(), + self::get_css_configuration(), + self::get_editor_language(), + $this->get_repository_configuration(), + self::get_media_configuration(), + self::get_user_configuration_data(), + $this->get_mimetex_plugin_configuration() + ); + } + + /** + * This method returns the path to the javascript custom configuration file. + * @return array + */ + private function & get_javascript_custom_configuration_file() { + return array('CustomConfigurationsPath' => api_get_path(REL_PATH).'main/inc/lib/fckeditor/myconfig.js'); + } + + /** + * This method returns CSS-related configuration data that has been determined by the system. + * @return array + */ + private function & get_css_configuration() { + $config['EditorAreaCSS'] = api_get_path(REL_PATH).'main/css/'.api_get_setting('stylesheets').'/default.css'; + $config['ToolbarComboPreviewCSS'] = $config['EditorAreaCSS']; + return $config; + } + + /** + * This method determines editor's interface language and returns it as compatible with the editor langiage code. + * @return array + */ + private function & get_editor_language() { + static $config; + if (!is_array($config)) { + $code_translation_table = array('' => 'en', 'sr' => 'sr-latn', 'zh' => 'zh-cn', 'zh-tw' => 'zh'); + $editor_lang = strtolower(str_replace('_', '-', api_get_language_isocode())); + $editor_lang = isset($code_translation_table[$editor_lang]) ? $code_translation_table[$editor_lang] : $editor_lang; + $editor_lang = file_exists(api_get_path(SYS_PATH).'main/inc/lib/fckeditor/editor/lang/'.$editor_lang.'.js') ? $editor_lang : 'en'; + $config['DefaultLanguage'] = $editor_lang; + $config['ContentLangDirection'] = api_get_text_direction($editor_lang); + } + return $config; + } + + /** + * This method returns default configuration for document repository that is to be used by the editor. + * @return array + */ + private function & get_repository_configuration() { + + // Preliminary calculations for assembling required paths. + $base_path = $this->BasePath; + $script_name = substr($_SERVER['PHP_SELF'], strlen(api_get_path(REL_PATH))); + $script_path = explode('/', $script_name); + $script_path[count($script_path) - 1] = ''; + if (api_is_in_course()) { + $relative_path_prefix = str_repeat('../', count($script_path) - 1); + } else { + $relative_path_prefix = str_repeat('../', count($script_path) - 2); + } + $script_path = implode('/', $script_path); + $script_path = api_get_path(WEB_PATH).$script_path; + + $use_advanced_filemanager = api_get_setting('advanced_filemanager') == 'true'; + // Let javascripts "know" which file manager has been chosen. + $config['AdvancedFileManager'] = $use_advanced_filemanager; + + if (api_is_in_course()) { + if (!api_is_in_group()) { + // 1. We are inside a course and not in a group. + if (api_is_allowed_to_edit()) { + // 1.1. Teacher (tutor and coach are not authorized to change anything in the "content creation" tools) + $config['CreateDocumentWebDir'] = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/'; + $config['CreateDocumentDir'] = $relative_path_prefix.'courses/'.api_get_course_path().'/document/'; + $config['BaseHref'] = $script_path; + } else { + // 1.2. Student + $current_session_id = api_get_session_id(); + if($current_session_id==0) + { + + $config['CreateDocumentWebDir'] = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/shared_folder/sf_user_'.api_get_user_id().'/'; + $config['CreateDocumentDir'] = $relative_path_prefix.'courses/'.api_get_course_path().'/document/shared_folder/sf_user_'.api_get_user_id().'/'; + $config['BaseHref'] = $script_path; + } + else + { + $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().'/'; + $config['CreateDocumentDir'] = $relative_path_prefix.'courses/'.api_get_course_path().'/document/shared_folder_session_'.$current_session_id.'/sf_user_'.api_get_user_id().'/'; + $config['BaseHref'] = $script_path; + } + } + } else { + // 2. Inside a course and inside a group. + global $group_properties; + $config['CreateDocumentWebDir'] = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document'.$group_properties['directory'].'/'; + $config['CreateDocumentDir'] = $relative_path_prefix.'courses/'.api_get_course_path().'/document'.$group_properties['directory'].'/'; + $config['BaseHref'] = $script_path; + } + } else { + if (api_is_platform_admin() && $_SESSION['this_section'] == 'platform_admin') { + // 3. Platform administration activities. + $config['CreateDocumentWebDir'] = api_get_path(WEB_PATH).'home/default_platform_document/'; + $config['CreateDocumentDir'] = api_get_path(WEB_PATH).'home/default_platform_document/'; // A side-effect is in use here. + $config['BaseHref'] = api_get_path(WEB_PATH).'home/default_platform_document/'; + } else { + // 4. The user is outside courses. + $config['CreateDocumentWebDir'] = api_get_path(WEB_PATH).'main/upload/users/'.api_get_user_id().'/my_files/'; + $config['CreateDocumentDir'] = $relative_path_prefix.'upload/users/'.api_get_user_id().'/my_files/'; + $config['BaseHref'] = $script_path; + } + } + + // URLs for opening the file browser for different resource types (file types): + if ($use_advanced_filemanager) { + // Double slashes within the following URLs for the advanced file manager are put intentionally. Please, keep them. + // for images + $config['ImageBrowserURL'] = $base_path.'/editor/plugins/ajaxfilemanager/ajaxfilemanager.php'; + // for flash + $config['FlashBrowserURL'] = $base_path.'/editor/plugins/ajaxfilemanager/ajaxfilemanager.php'; + // for audio files (mp3) + $config['MP3BrowserURL'] = $base_path.'/editor/plugins/ajaxfilemanager/ajaxfilemanager.php'; + // for video + $config['VideoBrowserURL'] = $base_path.'/editor/plugins/ajaxfilemanager/ajaxfilemanager.php'; + // for video (flv) + $config['MediaBrowserURL'] = $base_path.'/editor/plugins/ajaxfilemanager/ajaxfilemanager.php'; + // for links (any resource type) + $config['LinkBrowserURL'] = $base_path.'/editor/plugins/ajaxfilemanager/ajaxfilemanager.php'; + } else { + // for images + $config['ImageBrowserURL'] = $base_path.'editor/filemanager/browser/default/browser.html?Type=Images&Connector='.$base_path.'editor/filemanager/connectors/php/connector.php'; + // for flash + $config['FlashBrowserURL'] = $base_path.'editor/filemanager/browser/default/browser.html?Type=Flash&Connector='.$base_path.'editor/filemanager/connectors/php/connector.php'; + // for audio files (mp3) + $config['MP3BrowserURL'] = $base_path.'editor/filemanager/browser/default/browser.html?Type=MP3&Connector='.$base_path.'editor/filemanager/connectors/php/connector.php'; + // for video + $config['VideoBrowserURL'] = $base_path.'editor/filemanager/browser/default/browser.html?Type=Video&Connector='.$base_path.'editor/filemanager/connectors/php/connector.php'; + // for video (flv) + $config['MediaBrowserURL'] = $base_path.'editor/filemanager/browser/default/browser.html?Type=Video/flv&Connector='.$base_path.'editor/filemanager/connectors/php/connector.php'; + // for links (any resource type) + $config['LinkBrowserURL'] = $base_path.'editor/filemanager/browser/default/browser.html?Type=File&Connector='.$base_path.'editor/filemanager/connectors/php/connector.php'; + } + + // URLs for making quick uplods for different resource types (file types). + // These URLs are used by the dialogs' quick upload tabs: + // for images + $config['ImageUploadURL'] = $base_path.'editor/filemanager/connectors/php/upload.php?Type=Images'; + // for flash + $config['FlashUploadURL'] = $base_path.'editor/filemanager/connectors/php/upload.php?Type=Flash'; + // for audio files (mp3) + $config['MP3UploadURL'] = $base_path.'editor/filemanager/connectors/php/upload.php?Type=MP3'; + // for video + $config['VideoUploadURL'] = $base_path.'editor/filemanager/connectors/php/upload.php?Type=Video'; + // for video (flv) + $config['MediaUploadURL'] = $base_path.'editor/filemanager/connectors/php/upload.php?Type=Video/flv'; + // for links (any resource type) + $config['LinkUploadURL'] = $base_path.'editor/filemanager/connectors/php/upload.php?Type=File'; + + return $config; + } + + /** + * This method returns multi-media related configuration data. + * @return array + */ + private function & get_media_configuration() { + $config['FlashPlayerAudio'] = api_get_path(TO_REL, FLASH_PLAYER_AUDIO); + $config['FlashPlayerVideo'] = api_get_path(TO_REL, FLASH_PLAYER_VIDEO); + $config['ScriptSWFObject'] = api_get_path(TO_REL, SCRIPT_SWFOBJECT); + $config['ScriptASCIIMathML'] = api_get_path(TO_REL, SCRIPT_ASCIIMATHML); + return $config; + } + + /** + * This method returns current user specific configuration data. + * @return array + */ + private function & get_user_configuration_data() { + $config['UserIsCourseAdmin'] = api_is_allowed_to_edit() ? true : false; + $config['UserIsPlatformAdmin'] = api_is_platform_admin() ? true : false; + return $config; + } + + /** + * This method returns detected configuration data about editor's MimeTeX plugin. + * @return array + */ + private function & get_mimetex_plugin_configuration() { + static $config; + if (!isset($config)) { + $config = array(); + if (is_array($this->Config['LoadPlugin']) && in_array('mimetex', $this->Config['LoadPlugin'])) { + $server_base = api_get_path(WEB_SERVER_ROOT_PATH); + $server_base_parts = explode('/', $server_base); + $url_relative = 'cgi-bin/mimetex' . ( IS_WINDOWS_OS ? '.exe' : '.cgi' ); + if (!isset($this->Config['MimetexExecutableInstalled'])) { + $this->Config['MimetexExecutableDetectionMethod'] = 'detect'; + } + if ($this->Config['MimetexExecutableInstalled'] == 'detect') { + $detection_method = isset($this->Config['MimetexExecutableDetectionMethod']) ? $this->Config['MimetexExecutableDetectionMethod'] : 'bootstrap_ip'; + $detection_timeout = isset($this->Config['MimetexExecutableDetectionTimeout']) ? $this->Config['MimetexExecutableDetectionTimeout'] : 0.05; + switch ($detection_method) { + case 'bootstrap_ip': + $detection_url = $server_base_parts[0] . '//127.0.0.1/'; + break; + case 'localhost': + $detection_url = $server_base_parts[0] . '//localhost/'; + break; + case 'ip': + $detection_url = $server_base_parts[0] . '//' . $_SERVER['SERVER_ADDR'] . '/'; + break; + default: + $detection_url = $server_base_parts[0] . '//' . $_SERVER['SERVER_NAME'] . '/'; + } + $detection_url .= $url_relative . '?' . rand(); + $config['IsMimetexInstalled'] = self::url_exists($detection_url, $detection_timeout); + } else { + $config['IsMimetexInstalled'] = $this->Config['MimetexExecutableInstalled']; + } + $config['MimetexUrl'] = api_add_trailing_slash($server_base) . $url_relative; + } + // Cleaning detection related settings, we don't need them anymore. + unset($this->Config['MimetexExecutableInstalled']); + unset($this->Config['MimetexExecutableDetectionMethod']); + unset($this->Config['MimetexExecutableDetectionTimeout']); + } + return $config; + } + + /* + * Checks whether a given url exists. + * @param string $url + * @param int $timeout + * @return boolean + * @author Ivan Tcholakov, FEB-2009 + */ + private function url_exists($url, $timeout = 30) { + $parsed = parse_url($url); + $scheme = isset($parsed['scheme']) ? $parsed['scheme'] : 'http'; + $host = $parsed['host']; + $port = isset($parsed['port']) ? $parsed['port'] : ($scheme == 'http' ? 80 : ($scheme == 'https' ? 443 : -1 )); + + $file_exists = false; + $fp = @fsockopen($host, $port, $errno, $errstr, $timeout); + if ($fp) { + $request = "HEAD ".$url." / HTTP/1.1\r\n"; + $request .= "Host: ".$host."\r\n"; + $request .= "Connection: Close\r\n\r\n"; + + @fwrite($fp, $request); + while (!@feof($fp)) { + $header = @fgets($fp, 128); + if(@preg_match('#HTTP/1.1 200 OK#', $header)) { + $file_exists = true; + break; + } + } + } + @fclose($fp); + return $file_exists; + } } diff --git a/tests/main/inc/lib/main_api.lib.test_standalone.php b/tests/main/inc/lib/main_api.lib.test_standalone.php index f00b1c0172..a0af9636b0 100755 --- a/tests/main/inc/lib/main_api.lib.test_standalone.php +++ b/tests/main/inc/lib/main_api.lib.test_standalone.php @@ -7,332 +7,332 @@ class TestMainApi extends UnitTestCase { - function TestMainApi() { + function TestMainApi() { $this->UnitTestCase('Main API tests'); - } - - public function testApiGetPath() { - - $common_paths = array( - WEB_PATH, - SYS_PATH, - REL_PATH, - WEB_SERVER_ROOT_PATH, - SYS_SERVER_ROOT_PATH, - WEB_COURSE_PATH, - SYS_COURSE_PATH, - REL_COURSE_PATH, - REL_CODE_PATH, - WEB_CODE_PATH, - SYS_CODE_PATH, - SYS_LANG_PATH, - WEB_IMG_PATH, - WEB_CSS_PATH, - SYS_PLUGIN_PATH, - WEB_PLUGIN_PATH, - SYS_ARCHIVE_PATH, - WEB_ARCHIVE_PATH, - INCLUDE_PATH, - LIBRARY_PATH, - CONFIGURATION_PATH, - WEB_LIBRARY_PATH - ); - - $specific_paths = array( - FLASH_PLAYER_AUDIO, - FLASH_PLAYER_VIDEO, - SCRIPT_SWFOBJECT, - SCRIPT_ASCIIMATHML - ); - - $res = array(); - $is_ok = array(); - $message = array(); - $paths = array(); - - $message[] = ''; - $message[] = 'A test about api_get_path()'; - $message[] = '---------------------------------------------------------------------------------------------------------------'; - $message[] = ''; - - $message[] = ''; - $message[] = 'Changed behaviour of the function api_get_path() after Dokeos 1.8.6.1, i.e. as of Chamilo 1.8.6.2.'; - $message[] = '---------------------------------------------------------------------------------------------------------------'; - $message[] = ''; - $message[] = 'Old behaviour (1.8.6.1) api_get_path(INCLUDE_PATH) = '.api_get_path_1_8_6_1(INCLUDE_PATH).'   |   '.'New behaviour (1.8.6.2) api_get_path(INCLUDE_PATH) = '.api_get_path(INCLUDE_PATH); - $message[] = '* Reason for this change: Difference here is due to the fact that the etalonic old function api_get_path() has ben moved in this file ( see api_get_path_1_8_6_1() ). Even for such rare, hypothetical cases, this widely used function should be stable. Now, after installation, the function returns results based on configuration settings only, as it should be.'; - $message[] = ''; - $message[] = 'Old behaviour (1.8.6.1) api_get_path(WEB_CSS_PATH) = '.api_get_path_1_8_6_1(WEB_CSS_PATH).'   |   '.'New behaviour (1.8.6.2) api_get_path(WEB_CSS_PATH) = '.api_get_path(WEB_CSS_PATH); - $message[] = '* This is a proposed implementation. Retrieving css paths through user\'s configuration options has not been implemented yet.'; - $message[] = ''; - - $message[] = ''; - $message[] = 'Reading common purpose paths'; - $message[] = '---------------------------------------------------------------------------------------------------------------'; - $message[] = ''; - - foreach ($common_paths as $path) { - - $test_case = "api_get_path($path)"; - $res[$test_case] = api_get_path($path); - switch ($path) { - case INCLUDE_PATH: - case WEB_CSS_PATH: - $is_ok[$test_case] = is_string($res[$test_case]) && !empty($res[$test_case]); - break; - default: - $is_ok[$test_case] = is_string($res[$test_case]) && !empty($res[$test_case]) && $res[$test_case] == api_get_path_1_8_6_1($path); - } - $message[] = ($is_ok[$test_case] ? 'Ok' : 'Failed').' : '.$test_case.' => '.$res[$test_case]; - } - - $message[] = ''; - $message[] = ''; - $message[] = 'Reading specific purpose paths'; - $message[] = '---------------------------------------------------------------------------------------------------------------'; - $message[] = ''; - - foreach ($specific_paths as $path) { - - $test_case = "api_get_path(TO_WEB, $path)"; - $test_case = str_replace(array('{', '}'), '', $test_case); - $res[$test_case] = api_get_path(TO_WEB, $path); - $is_ok[$test_case] = is_string($res[$test_case]) && !empty($res[$test_case]); - $message[] = ($is_ok[$test_case] ? 'Ok' : 'Failed').' : '.$test_case.' => '.$res[$test_case]; - $paths[] = $path; - - $test_case = "api_get_path(TO_SYS, $path)"; - $test_case = str_replace(array('{', '}'), '', $test_case); - $res[$test_case] = api_get_path(TO_SYS, $path); - $is_ok[$test_case] = is_string($res[$test_case]) && !empty($res[$test_case]); - $message[] = ($is_ok[$test_case] ? 'Ok' : 'Failed').' : '.$test_case.' => '.$res[$test_case]; - $paths[] = $path; - - $test_case = "api_get_path(TO_REL, $path)"; - $test_case = str_replace(array('{', '}'), '', $test_case); - $res[$test_case] = api_get_path(TO_REL, $path); - $is_ok[$test_case] = is_string($res[$test_case]) && !empty($res[$test_case]); - $message[] = ($is_ok[$test_case] ? 'Ok' : 'Failed').' : '.$test_case.' => '.$res[$test_case]; - $paths[] = $path; - } - - $message[] = ''; - $message[] = ''; - $message[] = 'Testing path conversions'; - $message[] = '---------------------------------------------------------------------------------------------------------------'; - $message[] = ''; - - $paths = array(); - foreach ($common_paths as $path) { - $paths[] = array($path, api_get_path($path)); - } - foreach ($specific_paths as $path) { - $paths[] = array($path, api_get_path(TO_WEB, $path)); - $paths[] = array($path, api_get_path(TO_SYS, $path)); - $paths[] = array($path, api_get_path(TO_REL, $path)); - } - - foreach ($paths as $path) { - - $test_case = 'api_get_path(TO_WEB, '.$path[0].')'; - $test_case = str_replace(array('{', '}'), '', $test_case); - $res[$test_case] = api_get_path(TO_WEB, $path[0]); - $test_case_1 = 'api_get_path(TO_WEB, \''.$path[1].'\')'; - $res[$test_case_1] = api_get_path(TO_WEB, $path[1]); - $is_ok[$test_case] = - is_string($res[$test_case]) && !empty($res[$test_case]) - && is_string($res[$test_case_1]) && !empty($res[$test_case_1]) - && $res[$test_case] == $res[$test_case_1]; - $message[] = ($is_ok[$test_case] ? 'Ok' : 'Failed').' : '; - $message[] = $test_case.' => '.$res[$test_case]; - $message[] = $test_case_1.' => '.$res[$test_case_1]; - $message[] = ''; - - $test_case = 'api_get_path(TO_SYS, '.$path[0].')'; - $test_case = str_replace(array('{', '}'), '', $test_case); - $res[$test_case] = api_get_path(TO_SYS, $path[0]); - $test_case_1 = 'api_get_path(TO_SYS, \''.$path[1].'\')'; - $res[$test_case_1] = api_get_path(TO_SYS, $path[1]); - $is_ok[$test_case] = - is_string($res[$test_case]) && !empty($res[$test_case]) - && is_string($res[$test_case_1]) && !empty($res[$test_case_1]) - && $res[$test_case] == $res[$test_case_1]; - $message[] = ($is_ok[$test_case] ? 'Ok' : 'Failed').' : '; - $message[] = $test_case.' => '.$res[$test_case]; - $message[] = $test_case_1.' => '.$res[$test_case_1]; - $message[] = ''; - - $test_case = 'api_get_path(TO_REL, '.$path[0].')'; - $test_case = str_replace(array('{', '}'), '', $test_case); - $res[$test_case] = api_get_path(TO_REL, $path[0]); - $test_case_1 = 'api_get_path(TO_REL, \''.$path[1].'\')'; - $res[$test_case_1] = api_get_path(TO_REL, $path[1]); - $is_ok[$test_case] = - is_string($res[$test_case]) && !empty($res[$test_case]) - && is_string($res[$test_case_1]) && !empty($res[$test_case_1]) - && $res[$test_case] == $res[$test_case_1]; - $message[] = ($is_ok[$test_case] ? 'Ok' : 'Failed').' : '; - $message[] = $test_case.' => '.$res[$test_case]; - $message[] = $test_case_1.' => '.$res[$test_case_1]; - $message[] = ''; - - } - - $message[] = ''; - $message[] = 'Random examples, check them visually'; - $message[] = '---------------------------------------------------------------------------------------------------------------'; - $message[] = ''; - $message[] = '$_SERVER[\'REQUEST_URI\'] => '.$_SERVER['REQUEST_URI']; - $message[] = 'Note: Try some query strings. They should be removed from the results.'; - $message[] = 'api_get_path(TO_WEB, $_SERVER[\'REQUEST_URI\']) => '.api_get_path(TO_WEB, $_SERVER['REQUEST_URI']); - $message[] = 'api_get_path(TO_SYS, $_SERVER[\'REQUEST_URI\']) => '.api_get_path(TO_SYS, $_SERVER['REQUEST_URI']); - $message[] = 'api_get_path(TO_REL, $_SERVER[\'REQUEST_URI\']) => '.api_get_path(TO_REL, $_SERVER['REQUEST_URI']); - $message[] = ''; - $message[] = '__FILE__ => '.__FILE__; - $message[] = 'api_get_path(TO_WEB, __FILE__) => '.api_get_path(TO_WEB, __FILE__); - $message[] = 'api_get_path(TO_SYS, __FILE__) => '.api_get_path(TO_SYS, __FILE__); - $message[] = 'api_get_path(TO_REL, __FILE__) => '.api_get_path(TO_REL, __FILE__); - $message[] = ''; - $message[] = '$_SERVER[\'PHP_SELF\'] => '.$_SERVER['PHP_SELF']; - $message[] = 'api_get_path(TO_WEB, $_SERVER[\'PHP_SELF\']) => '.api_get_path(TO_WEB, $_SERVER['PHP_SELF']); - $message[] = 'api_get_path(TO_SYS, $_SERVER[\'PHP_SELF\']) => '.api_get_path(TO_SYS, $_SERVER['PHP_SELF']); - $message[] = 'api_get_path(TO_REL, $_SERVER[\'PHP_SELF\']) => '.api_get_path(TO_REL, $_SERVER['PHP_SELF']); - $message[] = ''; - - $message[] = ''; - $message[] = '---------------------------------------------------------------------------------------------------------------'; - $message[] = 'This test and changes of behaviour of api_get_path() have been done by Ivan Tcholakov, September 22, 2009.'; - $message[] = ''; - - $result = !in_array(false, $is_ok); - $this->assertTrue($result); - //var_dump($res); - foreach ($message as $line) { echo $line.'
'; } - - // Sample code for showing results in different context. - /* - $common_paths = array( - WEB_PATH, - SYS_PATH, - REL_PATH, - WEB_SERVER_ROOT_PATH, - SYS_SERVER_ROOT_PATH, - WEB_COURSE_PATH, - SYS_COURSE_PATH, - REL_COURSE_PATH, - REL_CODE_PATH, - WEB_CODE_PATH, - SYS_CODE_PATH, - SYS_LANG_PATH, - WEB_IMG_PATH, - WEB_CSS_PATH, - SYS_PLUGIN_PATH, - WEB_PLUGIN_PATH, - SYS_ARCHIVE_PATH, - WEB_ARCHIVE_PATH, - INCLUDE_PATH, - LIBRARY_PATH, - CONFIGURATION_PATH, - WEB_LIBRARY_PATH - ); - - $specific_paths = array( - FLASH_PLAYER_AUDIO, - FLASH_PLAYER_VIDEO, - SCRIPT_SWFOBJECT, - SCRIPT_ASCIIMATHML - ); - - $res = array(); - $is_ok = array(); - $message = array(); - $paths = array(); - - $message[] = ''; - $message[] = 'Reading common purpose paths'; - $message[] = '---------------------------------------------------------------------------------------------------------------'; - $message[] = ''; - - foreach ($common_paths as $path) { - - $test_case = "api_get_path($path)"; - $res[$test_case] = api_get_path($path); - $message[] = $test_case.' => '.$res[$test_case]; - } - - $message[] = ''; - $message[] = ''; - $message[] = 'Reading specific purpose paths'; - $message[] = '---------------------------------------------------------------------------------------------------------------'; - $message[] = ''; - - foreach ($specific_paths as $path) { - - $test_case = "api_get_path(TO_WEB, $path)"; - $test_case = str_replace(array('{', '}'), '', $test_case); - $res[$test_case] = api_get_path(TO_WEB, $path); - $message[] = $test_case.' => '.$res[$test_case]; - $paths[] = $path; - - $test_case = "api_get_path(TO_SYS, $path)"; - $test_case = str_replace(array('{', '}'), '', $test_case); - $res[$test_case] = api_get_path(TO_SYS, $path); - $message[] = $test_case.' => '.$res[$test_case]; - $paths[] = $path; - - $test_case = "api_get_path(TO_REL, $path)"; - $test_case = str_replace(array('{', '}'), '', $test_case); - $res[$test_case] = api_get_path(TO_REL, $path); - $message[] = $test_case.' => '.$res[$test_case]; - $paths[] = $path; - } - - foreach ($message as $line) { echo $line.'
'; } - */ - } - - public function testApiIsInternalPath() { - $path1 = api_get_path(WEB_IMG_PATH); - $path2 = 'http://kdflskfsenfnmzsdn/fnefsdsmdsdmsdfsdcmxaddfdafada/index.html'; - $path3 = api_get_path(TO_SYS, WEB_IMG_PATH); - $path4 = 'C:\Inetpub\wwwroot\fnefsdsmdsdmsdfsdcmxaddfdafada/index.html'; - $path5 = api_get_path(TO_REL, WEB_IMG_PATH); - $path6 = '/fnefsdsmdsdmsdfsdcmxaddfdafada/index.html'; - $res1 = api_is_internal_path($path1); - $res2 = api_is_internal_path($path2); - $res3 = api_is_internal_path($path3); - $res4 = api_is_internal_path($path4); - $res5 = api_is_internal_path($path5); - $res6 = api_is_internal_path($path6); - $this->assertTrue(is_bool($res1) && is_bool($res2) && is_bool($res3) && is_bool($res4) && is_bool($res5) && is_bool($res6) - && $res1 && !$res2 && $res3 && !$res4 && $res5 && !$res6); - //var_dump($res1); - //var_dump($res2); - //var_dump($res1); - //var_dump($res2); - //var_dump($res1); - //var_dump($res2); - } - - public function testApiAddTrailingSlash() { - $string1 = 'path'; - $string2 = 'path/'; - $res1 = api_add_trailing_slash($string1); - $res2 = api_add_trailing_slash($string2); - $this->assertTrue(is_string($res1) && is_string($res2) && $res1 == $res2); - //var_dump($res1); - //var_dump($res2); - } - - public function testRemoveAddTrailingSlash() { - $string1 = 'path'; - $string2 = 'path/'; - $res1 = api_remove_trailing_slash($string1); - $res2 = api_remove_trailing_slash($string2); - $this->assertTrue(is_string($res1) && is_string($res2) && $res1 == $res2); - //var_dump($res1); - //var_dump($res2); - } + } + + public function testApiGetPath() { + + $common_paths = array( + WEB_PATH, + SYS_PATH, + REL_PATH, + WEB_SERVER_ROOT_PATH, + SYS_SERVER_ROOT_PATH, + WEB_COURSE_PATH, + SYS_COURSE_PATH, + REL_COURSE_PATH, + REL_CODE_PATH, + WEB_CODE_PATH, + SYS_CODE_PATH, + SYS_LANG_PATH, + WEB_IMG_PATH, + WEB_CSS_PATH, + SYS_PLUGIN_PATH, + WEB_PLUGIN_PATH, + SYS_ARCHIVE_PATH, + WEB_ARCHIVE_PATH, + INCLUDE_PATH, + LIBRARY_PATH, + CONFIGURATION_PATH, + WEB_LIBRARY_PATH + ); + + $specific_paths = array( + FLASH_PLAYER_AUDIO, + FLASH_PLAYER_VIDEO, + SCRIPT_SWFOBJECT, + SCRIPT_ASCIIMATHML + ); + + $res = array(); + $is_ok = array(); + $message = array(); + $paths = array(); + + $message[] = ''; + $message[] = 'A test about api_get_path()'; + $message[] = '---------------------------------------------------------------------------------------------------------------'; + $message[] = ''; + + $message[] = ''; + $message[] = 'Changed behaviour of the function api_get_path() after Dokeos 1.8.6.1, i.e. as of Chamilo 1.8.6.2.'; + $message[] = '---------------------------------------------------------------------------------------------------------------'; + $message[] = ''; + $message[] = 'Old behaviour (1.8.6.1) api_get_path(INCLUDE_PATH) = '.api_get_path_1_8_6_1(INCLUDE_PATH).'   |   '.'New behaviour (1.8.6.2) api_get_path(INCLUDE_PATH) = '.api_get_path(INCLUDE_PATH); + $message[] = '* Reason for this change: Difference here is due to the fact that the etalonic old function api_get_path() has ben moved in this file ( see api_get_path_1_8_6_1() ). Even for such rare, hypothetical cases, this widely used function should be stable. Now, after installation, the function returns results based on configuration settings only, as it should be.'; + $message[] = ''; + $message[] = 'Old behaviour (1.8.6.1) api_get_path(WEB_CSS_PATH) = '.api_get_path_1_8_6_1(WEB_CSS_PATH).'   |   '.'New behaviour (1.8.6.2) api_get_path(WEB_CSS_PATH) = '.api_get_path(WEB_CSS_PATH); + $message[] = '* This is a proposed implementation. Retrieving css paths through user\'s configuration options has not been implemented yet.'; + $message[] = ''; + + $message[] = ''; + $message[] = 'Reading common purpose paths'; + $message[] = '---------------------------------------------------------------------------------------------------------------'; + $message[] = ''; + + foreach ($common_paths as $path) { + + $test_case = "api_get_path($path)"; + $res[$test_case] = api_get_path($path); + switch ($path) { + case INCLUDE_PATH: + case WEB_CSS_PATH: + $is_ok[$test_case] = is_string($res[$test_case]) && !empty($res[$test_case]); + break; + default: + $is_ok[$test_case] = is_string($res[$test_case]) && !empty($res[$test_case]) && $res[$test_case] == api_get_path_1_8_6_1($path); + } + $message[] = ($is_ok[$test_case] ? 'Ok' : 'Failed').' : '.$test_case.' => '.$res[$test_case]; + } + + $message[] = ''; + $message[] = ''; + $message[] = 'Reading specific purpose paths'; + $message[] = '---------------------------------------------------------------------------------------------------------------'; + $message[] = ''; + + foreach ($specific_paths as $path) { + + $test_case = "api_get_path(TO_WEB, $path)"; + $test_case = str_replace(array('{', '}'), '', $test_case); + $res[$test_case] = api_get_path(TO_WEB, $path); + $is_ok[$test_case] = is_string($res[$test_case]) && !empty($res[$test_case]); + $message[] = ($is_ok[$test_case] ? 'Ok' : 'Failed').' : '.$test_case.' => '.$res[$test_case]; + $paths[] = $path; + + $test_case = "api_get_path(TO_SYS, $path)"; + $test_case = str_replace(array('{', '}'), '', $test_case); + $res[$test_case] = api_get_path(TO_SYS, $path); + $is_ok[$test_case] = is_string($res[$test_case]) && !empty($res[$test_case]); + $message[] = ($is_ok[$test_case] ? 'Ok' : 'Failed').' : '.$test_case.' => '.$res[$test_case]; + $paths[] = $path; + + $test_case = "api_get_path(TO_REL, $path)"; + $test_case = str_replace(array('{', '}'), '', $test_case); + $res[$test_case] = api_get_path(TO_REL, $path); + $is_ok[$test_case] = is_string($res[$test_case]) && !empty($res[$test_case]); + $message[] = ($is_ok[$test_case] ? 'Ok' : 'Failed').' : '.$test_case.' => '.$res[$test_case]; + $paths[] = $path; + } + + $message[] = ''; + $message[] = ''; + $message[] = 'Testing path conversions'; + $message[] = '---------------------------------------------------------------------------------------------------------------'; + $message[] = ''; + + $paths = array(); + foreach ($common_paths as $path) { + $paths[] = array($path, api_get_path($path)); + } + foreach ($specific_paths as $path) { + $paths[] = array($path, api_get_path(TO_WEB, $path)); + $paths[] = array($path, api_get_path(TO_SYS, $path)); + $paths[] = array($path, api_get_path(TO_REL, $path)); + } + + foreach ($paths as $path) { + + $test_case = 'api_get_path(TO_WEB, '.$path[0].')'; + $test_case = str_replace(array('{', '}'), '', $test_case); + $res[$test_case] = api_get_path(TO_WEB, $path[0]); + $test_case_1 = 'api_get_path(TO_WEB, \''.$path[1].'\')'; + $res[$test_case_1] = api_get_path(TO_WEB, $path[1]); + $is_ok[$test_case] = + is_string($res[$test_case]) && !empty($res[$test_case]) + && is_string($res[$test_case_1]) && !empty($res[$test_case_1]) + && $res[$test_case] == $res[$test_case_1]; + $message[] = ($is_ok[$test_case] ? 'Ok' : 'Failed').' : '; + $message[] = $test_case.' => '.$res[$test_case]; + $message[] = $test_case_1.' => '.$res[$test_case_1]; + $message[] = ''; + + $test_case = 'api_get_path(TO_SYS, '.$path[0].')'; + $test_case = str_replace(array('{', '}'), '', $test_case); + $res[$test_case] = api_get_path(TO_SYS, $path[0]); + $test_case_1 = 'api_get_path(TO_SYS, \''.$path[1].'\')'; + $res[$test_case_1] = api_get_path(TO_SYS, $path[1]); + $is_ok[$test_case] = + is_string($res[$test_case]) && !empty($res[$test_case]) + && is_string($res[$test_case_1]) && !empty($res[$test_case_1]) + && $res[$test_case] == $res[$test_case_1]; + $message[] = ($is_ok[$test_case] ? 'Ok' : 'Failed').' : '; + $message[] = $test_case.' => '.$res[$test_case]; + $message[] = $test_case_1.' => '.$res[$test_case_1]; + $message[] = ''; + + $test_case = 'api_get_path(TO_REL, '.$path[0].')'; + $test_case = str_replace(array('{', '}'), '', $test_case); + $res[$test_case] = api_get_path(TO_REL, $path[0]); + $test_case_1 = 'api_get_path(TO_REL, \''.$path[1].'\')'; + $res[$test_case_1] = api_get_path(TO_REL, $path[1]); + $is_ok[$test_case] = + is_string($res[$test_case]) && !empty($res[$test_case]) + && is_string($res[$test_case_1]) && !empty($res[$test_case_1]) + && $res[$test_case] == $res[$test_case_1]; + $message[] = ($is_ok[$test_case] ? 'Ok' : 'Failed').' : '; + $message[] = $test_case.' => '.$res[$test_case]; + $message[] = $test_case_1.' => '.$res[$test_case_1]; + $message[] = ''; + + } + + $message[] = ''; + $message[] = 'Random examples, check them visually'; + $message[] = '---------------------------------------------------------------------------------------------------------------'; + $message[] = ''; + $message[] = '$_SERVER[\'REQUEST_URI\'] => '.$_SERVER['REQUEST_URI']; + $message[] = 'Note: Try some query strings. They should be removed from the results.'; + $message[] = 'api_get_path(TO_WEB, $_SERVER[\'REQUEST_URI\']) => '.api_get_path(TO_WEB, $_SERVER['REQUEST_URI']); + $message[] = 'api_get_path(TO_SYS, $_SERVER[\'REQUEST_URI\']) => '.api_get_path(TO_SYS, $_SERVER['REQUEST_URI']); + $message[] = 'api_get_path(TO_REL, $_SERVER[\'REQUEST_URI\']) => '.api_get_path(TO_REL, $_SERVER['REQUEST_URI']); + $message[] = ''; + $message[] = '__FILE__ => '.__FILE__; + $message[] = 'api_get_path(TO_WEB, __FILE__) => '.api_get_path(TO_WEB, __FILE__); + $message[] = 'api_get_path(TO_SYS, __FILE__) => '.api_get_path(TO_SYS, __FILE__); + $message[] = 'api_get_path(TO_REL, __FILE__) => '.api_get_path(TO_REL, __FILE__); + $message[] = ''; + $message[] = '$_SERVER[\'PHP_SELF\'] => '.$_SERVER['PHP_SELF']; + $message[] = 'api_get_path(TO_WEB, $_SERVER[\'PHP_SELF\']) => '.api_get_path(TO_WEB, $_SERVER['PHP_SELF']); + $message[] = 'api_get_path(TO_SYS, $_SERVER[\'PHP_SELF\']) => '.api_get_path(TO_SYS, $_SERVER['PHP_SELF']); + $message[] = 'api_get_path(TO_REL, $_SERVER[\'PHP_SELF\']) => '.api_get_path(TO_REL, $_SERVER['PHP_SELF']); + $message[] = ''; + + $message[] = ''; + $message[] = '---------------------------------------------------------------------------------------------------------------'; + $message[] = 'This test and changes of behaviour of api_get_path() have been done by Ivan Tcholakov, September 22, 2009.'; + $message[] = ''; + + $result = !in_array(false, $is_ok); + $this->assertTrue($result); + //var_dump($res); + foreach ($message as $line) { echo $line.'
'; } + + // Sample code for showing results in different context. + /* + $common_paths = array( + WEB_PATH, + SYS_PATH, + REL_PATH, + WEB_SERVER_ROOT_PATH, + SYS_SERVER_ROOT_PATH, + WEB_COURSE_PATH, + SYS_COURSE_PATH, + REL_COURSE_PATH, + REL_CODE_PATH, + WEB_CODE_PATH, + SYS_CODE_PATH, + SYS_LANG_PATH, + WEB_IMG_PATH, + WEB_CSS_PATH, + SYS_PLUGIN_PATH, + WEB_PLUGIN_PATH, + SYS_ARCHIVE_PATH, + WEB_ARCHIVE_PATH, + INCLUDE_PATH, + LIBRARY_PATH, + CONFIGURATION_PATH, + WEB_LIBRARY_PATH + ); + + $specific_paths = array( + FLASH_PLAYER_AUDIO, + FLASH_PLAYER_VIDEO, + SCRIPT_SWFOBJECT, + SCRIPT_ASCIIMATHML + ); + + $res = array(); + $is_ok = array(); + $message = array(); + $paths = array(); + + $message[] = ''; + $message[] = 'Reading common purpose paths'; + $message[] = '---------------------------------------------------------------------------------------------------------------'; + $message[] = ''; + + foreach ($common_paths as $path) { + + $test_case = "api_get_path($path)"; + $res[$test_case] = api_get_path($path); + $message[] = $test_case.' => '.$res[$test_case]; + } + + $message[] = ''; + $message[] = ''; + $message[] = 'Reading specific purpose paths'; + $message[] = '---------------------------------------------------------------------------------------------------------------'; + $message[] = ''; + + foreach ($specific_paths as $path) { + + $test_case = "api_get_path(TO_WEB, $path)"; + $test_case = str_replace(array('{', '}'), '', $test_case); + $res[$test_case] = api_get_path(TO_WEB, $path); + $message[] = $test_case.' => '.$res[$test_case]; + $paths[] = $path; + + $test_case = "api_get_path(TO_SYS, $path)"; + $test_case = str_replace(array('{', '}'), '', $test_case); + $res[$test_case] = api_get_path(TO_SYS, $path); + $message[] = $test_case.' => '.$res[$test_case]; + $paths[] = $path; + + $test_case = "api_get_path(TO_REL, $path)"; + $test_case = str_replace(array('{', '}'), '', $test_case); + $res[$test_case] = api_get_path(TO_REL, $path); + $message[] = $test_case.' => '.$res[$test_case]; + $paths[] = $path; + } + + foreach ($message as $line) { echo $line.'
'; } + */ + } + + public function testApiIsInternalPath() { + $path1 = api_get_path(WEB_IMG_PATH); + $path2 = 'http://kdflskfsenfnmzsdn/fnefsdsmdsdmsdfsdcmxaddfdafada/index.html'; + $path3 = api_get_path(TO_SYS, WEB_IMG_PATH); + $path4 = 'C:\Inetpub\wwwroot\fnefsdsmdsdmsdfsdcmxaddfdafada/index.html'; + $path5 = api_get_path(TO_REL, WEB_IMG_PATH); + $path6 = '/fnefsdsmdsdmsdfsdcmxaddfdafada/index.html'; + $res1 = api_is_internal_path($path1); + $res2 = api_is_internal_path($path2); + $res3 = api_is_internal_path($path3); + $res4 = api_is_internal_path($path4); + $res5 = api_is_internal_path($path5); + $res6 = api_is_internal_path($path6); + $this->assertTrue(is_bool($res1) && is_bool($res2) && is_bool($res3) && is_bool($res4) && is_bool($res5) && is_bool($res6) + && $res1 && !$res2 && $res3 && !$res4 && $res5 && !$res6); + //var_dump($res1); + //var_dump($res2); + //var_dump($res1); + //var_dump($res2); + //var_dump($res1); + //var_dump($res2); + } + + public function testApiAddTrailingSlash() { + $string1 = 'path'; + $string2 = 'path/'; + $res1 = api_add_trailing_slash($string1); + $res2 = api_add_trailing_slash($string2); + $this->assertTrue(is_string($res1) && is_string($res2) && $res1 == $res2); + //var_dump($res1); + //var_dump($res2); + } + + public function testRemoveAddTrailingSlash() { + $string1 = 'path'; + $string2 = 'path/'; + $res1 = api_remove_trailing_slash($string1); + $res2 = api_remove_trailing_slash($string2); + $this->assertTrue(is_string($res1) && is_string($res2) && $res1 == $res2); + //var_dump($res1); + //var_dump($res2); + } } /** @@ -377,161 +377,161 @@ class TestMainApi extends UnitTestCase { */ function api_get_path_1_8_6_1($path_type) { - global $_configuration; - if (!isset($_configuration['access_url']) || $_configuration['access_url']==1 || $_configuration['access_url']=='') { - //by default we call the $_configuration['root_web'] we don't query to the DB - //$url_info= api_get_access_url(1); - //$root_web = $url_info['url']; - if(isset($_configuration['root_web'])) - $root_web = $_configuration['root_web']; - } else { - //we look into the DB the function api_get_access_url - //this funcion have a problem because we can't called to the Database:: functions - $url_info= api_get_access_url($_configuration['access_url']); - if ($url_info['active']==1) { - $root_web = $url_info['url']; - } else { - $root_web = $_configuration['root_web']; - } - } - - switch ($path_type) { - - case WEB_SERVER_ROOT_PATH: - // example: http://www.mydokeos.com/ - $result = preg_replace('@'.api_get_path(REL_PATH).'$@', '', api_get_path(WEB_PATH)); - if (substr($result, -1) == '/') { - return $result; - } else { - return $result.'/'; - } - break; - - case SYS_SERVER_ROOT_PATH: - $result = preg_replace('@'.api_get_path(REL_PATH).'$@', '', api_get_path(SYS_PATH)); - if (substr($result, -1) == '/') { - return $result; - } else { - return $result.'/'; - } - break; - - case WEB_PATH : - // example: http://www.mydokeos.com/ or http://www.mydokeos.com/dokeos/ if you're using - // a subdirectory of your document root for Dokeos - if (substr($root_web,-1) == '/') { - return $root_web; - } else { - return $root_web.'/'; - } - break; - - case SYS_PATH : - // example: /var/www/dokeos/ - if (substr($_configuration['root_sys'],-1) == '/') { - return $_configuration['root_sys']; - } else { - return $_configuration['root_sys'].'/'; - } - break; - - case REL_PATH : - // example: dokeos/ - if (substr($_configuration['url_append'], -1) === '/') { - return $_configuration['url_append']; - } else { - return $_configuration['url_append'].'/'; - } - break; - - case WEB_COURSE_PATH : - // example: http://www.mydokeos.com/courses/ - return $root_web.$_configuration['course_folder']; - break; - - case SYS_COURSE_PATH : - // example: /var/www/dokeos/courses/ - return $_configuration['root_sys'].$_configuration['course_folder']; - break; - - case REL_COURSE_PATH : - // example: courses/ or dokeos/courses/ - return api_get_path(REL_PATH).$_configuration['course_folder']; - break; - - case REL_CODE_PATH : - // example: main/ or dokeos/main/ - return api_get_path(REL_PATH).$_configuration['code_append']; - break; - - case WEB_CODE_PATH : - // example: http://www.mydokeos.com/main/ - //return $GLOBALS['clarolineRepositoryWeb']; // this was changed - return $root_web.$_configuration['code_append']; - break; - - case SYS_CODE_PATH : - // example: /var/www/dokeos/main/ - return $GLOBALS['clarolineRepositorySys']; - break; - - case SYS_LANG_PATH : - // example: /var/www/dokeos/main/lang/ - return api_get_path(SYS_CODE_PATH).'lang/'; - break; - - case WEB_IMG_PATH : - // example: http://www.mydokeos.com/main/img/ - return api_get_path(WEB_CODE_PATH).'img/'; - break; - - case SYS_PLUGIN_PATH : - // example: /var/www/dokeos/plugin/ - return api_get_path(SYS_PATH).'plugin/'; - break; - - case WEB_PLUGIN_PATH : - // example: http://www.mydokeos.com/plugin/ - return api_get_path(WEB_PATH).'plugin/'; - break; + global $_configuration; + if (!isset($_configuration['access_url']) || $_configuration['access_url']==1 || $_configuration['access_url']=='') { + //by default we call the $_configuration['root_web'] we don't query to the DB + //$url_info= api_get_access_url(1); + //$root_web = $url_info['url']; + if(isset($_configuration['root_web'])) + $root_web = $_configuration['root_web']; + } else { + //we look into the DB the function api_get_access_url + //this funcion have a problem because we can't called to the Database:: functions + $url_info= api_get_access_url($_configuration['access_url']); + if ($url_info['active']==1) { + $root_web = $url_info['url']; + } else { + $root_web = $_configuration['root_web']; + } + } + + switch ($path_type) { + + case WEB_SERVER_ROOT_PATH: + // example: http://www.mydokeos.com/ + $result = preg_replace('@'.api_get_path(REL_PATH).'$@', '', api_get_path(WEB_PATH)); + if (substr($result, -1) == '/') { + return $result; + } else { + return $result.'/'; + } + break; + + case SYS_SERVER_ROOT_PATH: + $result = preg_replace('@'.api_get_path(REL_PATH).'$@', '', api_get_path(SYS_PATH)); + if (substr($result, -1) == '/') { + return $result; + } else { + return $result.'/'; + } + break; + + case WEB_PATH : + // example: http://www.mydokeos.com/ or http://www.mydokeos.com/dokeos/ if you're using + // a subdirectory of your document root for Dokeos + if (substr($root_web,-1) == '/') { + return $root_web; + } else { + return $root_web.'/'; + } + break; + + case SYS_PATH : + // example: /var/www/dokeos/ + if (substr($_configuration['root_sys'],-1) == '/') { + return $_configuration['root_sys']; + } else { + return $_configuration['root_sys'].'/'; + } + break; + + case REL_PATH : + // example: dokeos/ + if (substr($_configuration['url_append'], -1) === '/') { + return $_configuration['url_append']; + } else { + return $_configuration['url_append'].'/'; + } + break; + + case WEB_COURSE_PATH : + // example: http://www.mydokeos.com/courses/ + return $root_web.$_configuration['course_folder']; + break; + + case SYS_COURSE_PATH : + // example: /var/www/dokeos/courses/ + return $_configuration['root_sys'].$_configuration['course_folder']; + break; + + case REL_COURSE_PATH : + // example: courses/ or dokeos/courses/ + return api_get_path(REL_PATH).$_configuration['course_folder']; + break; + + case REL_CODE_PATH : + // example: main/ or dokeos/main/ + return api_get_path(REL_PATH).$_configuration['code_append']; + break; + + case WEB_CODE_PATH : + // example: http://www.mydokeos.com/main/ + //return $GLOBALS['clarolineRepositoryWeb']; // this was changed + return $root_web.$_configuration['code_append']; + break; + + case SYS_CODE_PATH : + // example: /var/www/dokeos/main/ + return $GLOBALS['clarolineRepositorySys']; + break; + + case SYS_LANG_PATH : + // example: /var/www/dokeos/main/lang/ + return api_get_path(SYS_CODE_PATH).'lang/'; + break; + + case WEB_IMG_PATH : + // example: http://www.mydokeos.com/main/img/ + return api_get_path(WEB_CODE_PATH).'img/'; + break; + + case SYS_PLUGIN_PATH : + // example: /var/www/dokeos/plugin/ + return api_get_path(SYS_PATH).'plugin/'; + break; + + case WEB_PLUGIN_PATH : + // example: http://www.mydokeos.com/plugin/ + return api_get_path(WEB_PATH).'plugin/'; + break; case GARBAGE_PATH : //now set to be same as archive - case SYS_ARCHIVE_PATH : - // example: /var/www/dokeos/archive/ - return api_get_path(SYS_PATH).'archive/'; - break; - - case WEB_ARCHIVE_PATH : - // example: http://www.mydokeos.com/archive/ - return api_get_path(WEB_PATH).'archive/'; - break; - - case INCLUDE_PATH : - // Generated by main/inc/global.inc.php - // example: /var/www/dokeos/main/inc/ + case SYS_ARCHIVE_PATH : + // example: /var/www/dokeos/archive/ + return api_get_path(SYS_PATH).'archive/'; + break; + + case WEB_ARCHIVE_PATH : + // example: http://www.mydokeos.com/archive/ + return api_get_path(WEB_PATH).'archive/'; + break; + + case INCLUDE_PATH : + // Generated by main/inc/global.inc.php + // example: /var/www/dokeos/main/inc/ $incpath = realpath(dirname(__FILE__).'/../'); return str_replace('\\', '/', $incpath).'/'; - break; - - case LIBRARY_PATH : - // example: /var/www/dokeos/main/inc/lib/ - return api_get_path(INCLUDE_PATH).'lib/'; - break; - - case WEB_LIBRARY_PATH : - // example: http://www.mydokeos.com/main/inc/lib/ - return api_get_path(WEB_CODE_PATH).'inc/lib/'; - break; - - case CONFIGURATION_PATH : - // example: /var/www/dokeos/main/inc/conf/ - return api_get_path(INCLUDE_PATH).'conf/'; - break; - - default : - return; - break; - } + break; + + case LIBRARY_PATH : + // example: /var/www/dokeos/main/inc/lib/ + return api_get_path(INCLUDE_PATH).'lib/'; + break; + + case WEB_LIBRARY_PATH : + // example: http://www.mydokeos.com/main/inc/lib/ + return api_get_path(WEB_CODE_PATH).'inc/lib/'; + break; + + case CONFIGURATION_PATH : + // example: /var/www/dokeos/main/inc/conf/ + return api_get_path(INCLUDE_PATH).'conf/'; + break; + + default : + return; + break; + } } ?>