/** * Checks whether the specified encoding is supported by the html-entitiy related functions. * @param string $encoding The specified encoding. * @return bool Returns TRUE when the specified encoding is supported, FALSE othewise. */ function _api_html_entity_supports($encoding) { static $supports = array(); if (!isset($supports[$encoding])) { // See http://php.net/manual/en/function.htmlentities.php $html_entity_encodings = array('ISO-8859-1', 'ISO-8859-15', 'UTF-8', 'CP866', 'CP1251', 'CP1252', 'KOI8-R', 'BIG5', '950', 'GB2312', '936', 'BIG5-HKSCS', 'Shift_JIS', 'SJIS', '932', 'EUC-JP', 'EUCJP'); $supports[$encoding] = api_equal_encodings($encoding, $html_entity_encodings); } return $supports[$encoding]; }
/** * Checks whether the specified encoding is supported by the PHP mbstring extension. * @param string $encoding The specified encoding. * @return bool Returns TRUE when the specified encoding is supported, FALSE othewise. */ function _api_mb_supports($encoding) { static $supported = array(); if (!isset($supported[$encoding])) { if (MBSTRING_INSTALLED) { $supported[$encoding] = api_equal_encodings($encoding, mb_list_encodings(), true); } else { $supported[$encoding] = false; } } return $supported[$encoding]; }
/** * Converts an encoding identificator to MySQL-specific encoding identifier, * i.e. 'UTF-8' --> 'utf8'. * @param string $encoding The conventional encoding identifier. * @return string Returns the corresponding MySQL-specific encoding identifier if any, otherwise returns NULL. * @author Ivan Tcholakov */ public static function to_db_encoding($encoding) { static $result = array(); if (!isset($result[$encoding])) { $result[$encoding] = null; $encoding_map =& self::get_db_encoding_map(); foreach ($encoding_map as $key => $value) { if (api_equal_encodings($encoding, $key)) { $result[$encoding] = $value; break; } } } return $result[$encoding]; }
/** * Converts to the system encoding all the language-sensitive fields in the imported course. */ public function to_system_encoding() { if (api_equal_encodings($this->encoding, api_get_system_encoding())) { return; } foreach ($this->resources as $type => &$resources) { if (count($resources) > 0) { foreach ($resources as &$resource) { switch ($type) { case RESOURCE_ANNOUNCEMENT: $resource->title = api_to_system_encoding($resource->title, $this->encoding); $resource->content = api_to_system_encoding($resource->content, $this->encoding); break; case RESOURCE_DOCUMENT: $resource->title = api_to_system_encoding($resource->title, $this->encoding); $resource->comment = api_to_system_encoding($resource->comment, $this->encoding); break; case RESOURCE_EVENT: $resource->title = api_to_system_encoding($resource->title, $this->encoding); $resource->content = api_to_system_encoding($resource->content, $this->encoding); break; case RESOURCE_FORUM: $resource->title = api_to_system_encoding($resource->title, $this->encoding); $resource->description = api_to_system_encoding($resource->description, $this->encoding); break; case RESOURCE_FORUMCATEGORY: $resource->title = api_to_system_encoding($resource->title, $this->encoding); $resource->description = api_to_system_encoding($resource->description, $this->encoding); break; case RESOURCE_FORUMPOST: $resource->title = api_to_system_encoding($resource->title, $this->encoding); $resource->text = api_to_system_encoding($resource->text, $this->encoding); $resource->poster_name = api_to_system_encoding($resource->poster_name, $this->encoding); break; case RESOURCE_FORUMTOPIC: $resource->title = api_to_system_encoding($resource->title, $this->encoding); $resource->topic_poster_name = api_to_system_encoding($resource->topic_poster_name, $this->encoding); $resource->title_qualify = api_to_system_encoding($resource->title_qualify, $this->encoding); break; case RESOURCE_GLOSSARY: $resource->name = api_to_system_encoding($resource->name, $this->encoding); $resource->description = api_to_system_encoding($resource->description, $this->encoding); break; case RESOURCE_LEARNPATH: $resource->name = api_to_system_encoding($resource->name, $this->encoding); $resource->description = api_to_system_encoding($resource->description, $this->encoding); $resource->content_maker = api_to_system_encoding($resource->content_maker, $this->encoding); $resource->content_license = api_to_system_encoding($resource->content_license, $this->encoding); break; case RESOURCE_LINK: $resource->title = api_to_system_encoding($resource->title, $this->encoding); $resource->description = api_to_system_encoding($resource->description, $this->encoding); break; case RESOURCE_LINKCATEGORY: $resource->title = api_to_system_encoding($resource->title, $this->encoding); $resource->description = api_to_system_encoding($resource->description, $this->encoding); break; case RESOURCE_QUIZ: $resource->title = api_to_system_encoding($resource->title, $this->encoding); $resource->description = api_to_system_encoding($resource->description, $this->encoding); break; case RESOURCE_QUIZQUESTION: $resource->question = api_to_system_encoding($resource->question, $this->encoding); $resource->description = api_to_system_encoding($resource->description, $this->encoding); if (is_array($resource->answers) && count($resource->answers) > 0) { foreach ($resource->answers as $index => &$answer) { $answer['answer'] = api_to_system_encoding($answer['answer'], $this->encoding); $answer['comment'] = api_to_system_encoding($answer['comment'], $this->encoding); } } break; case RESOURCE_TEST_CATEGORY: $resource->title = api_to_system_encoding($resource->title, $this->encoding); $resource->description = api_to_system_encoding($resource->description, $this->encoding); break; case RESOURCE_SCORM: $resource->title = api_to_system_encoding($resource->title, $this->encoding); break; case RESOURCE_SURVEY: $resource->title = api_to_system_encoding($resource->title, $this->encoding); $resource->subtitle = api_to_system_encoding($resource->subtitle, $this->encoding); $resource->author = api_to_system_encoding($resource->author, $this->encoding); $resource->intro = api_to_system_encoding($resource->intro, $this->encoding); $resource->surveythanks = api_to_system_encoding($resource->surveythanks, $this->encoding); break; case RESOURCE_SURVEYQUESTION: $resource->survey_question = api_to_system_encoding($resource->survey_question, $this->encoding); $resource->survey_question_comment = api_to_system_encoding($resource->survey_question_comment, $this->encoding); break; case RESOURCE_TOOL_INTRO: $resource->intro_text = api_to_system_encoding($resource->intro_text, $this->encoding); break; case RESOURCE_WIKI: $resource->title = api_to_system_encoding($resource->title, $this->encoding); $resource->content = api_to_system_encoding($resource->content, $this->encoding); $resource->reflink = api_to_system_encoding($resource->reflink, $this->encoding); break; case RESOURCE_WORK: $resource->url = api_to_system_encoding($resource->url, $this->encoding); $resource->title = api_to_system_encoding($resource->title, $this->encoding); $resource->description = api_to_system_encoding($resource->description, $this->encoding); break; default: break; } } } } $this->encoding = api_get_system_encoding(); }
/** * Write a string to the specified row and column (zero indexed). * This is the BIFF8 version (no 255 chars limit). * $format is optional. * Returns 0 : normal termination * -2 : row or column out of range * -3 : long string truncated to 255 chars * * @access public * @param integer $row Zero indexed row * @param integer $col Zero indexed column * @param string $str The string to write * @param mixed $format The XF format for the cell * @return integer */ function writeStringBIFF8($row, $col, $str, $format = null) { // Modified by Ivan Tcholakov, 06-AUG-2010. //if ($this->_input_encoding == 'UTF-16LE') if (api_equal_encodings($this->_input_encoding, 'UTF-16LE')) // { // Modified by Ivan Tcholakov, 06-AUG-2010. //$strlen = function_exists('mb_strlen') ? mb_strlen($str, 'UTF-16LE') : (strlen($str) / 2); $strlen = api_is_encoding_supported('UTF-16LE') ? api_strlen($str, 'UTF-16LE') : (strlen($str) / 2); // $encoding = 0x1; } elseif ($this->_input_encoding != '') { // Modified by Ivan Tcholakov, 06-AUG-2010. //$str = iconv($this->_input_encoding, 'UTF-16LE', $str); //$strlen = function_exists('mb_strlen') ? mb_strlen($str, 'UTF-16LE') : (strlen($str) / 2); $str = api_convert_encoding($str, 'UTF-16LE', $this->_input_encoding); $strlen = api_is_encoding_supported('UTF-16LE') ? api_strlen($str, 'UTF-16LE') : (strlen($str) / 2); // $encoding = 0x1; } else { $strlen = strlen($str); $encoding = 0x0; } $record = 0x00FD; // Record identifier $length = 0x000A; // Bytes to follow $xf = $this->_XF($format); // The cell format $str_error = 0; // Check that row and col are valid and store max and min values if ($this->_checkRowCol($row, $col) == false) { return -2; } $str = pack('vC', $strlen, $encoding).$str; /* check if string is already present */ if (!isset($this->_str_table[$str])) { $this->_str_table[$str] = $this->_str_unique++; } $this->_str_total++; $header = pack('vv', $record, $length); $data = pack('vvvV', $row, $col, $xf, $this->_str_table[$str]); $this->_append($header.$data); return $str_error; }
function generate_settings_form($settings, $settings_by_access_list) { global $_configuration, $settings_to_avoid, $convert_byte_to_mega_list; $table_settings_current = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT); $form = new FormValidator('settings', 'post', 'settings.php?category=' . Security::remove_XSS($_GET['category'])); $form->addElement('hidden', 'search_field', !empty($_GET['search_field']) ? Security::remove_XSS($_GET['search_field']) : null); $url_id = api_get_current_access_url_id(); if (!empty($_configuration['multiple_access_urls']) && api_is_global_platform_admin() && $url_id == 1) { $group = array(); $group[] = $form->createElement('button', 'mark_all', get_lang('MarkAll')); $group[] = $form->createElement('button', 'unmark_all', get_lang('UnmarkAll')); $form->addGroup($group, 'buttons_in_action_right'); } $default_values = array(); $url_info = api_get_access_url($url_id); $i = 0; foreach ($settings as $row) { if (in_array($row['variable'], array_keys($settings_to_avoid))) { continue; } if (!empty($_configuration['multiple_access_urls'])) { if (api_is_global_platform_admin()) { if ($row['access_url_locked'] == 0) { if ($url_id == 1) { if ($row['access_url_changeable'] == '1') { $form->addElement('html', '<div style="float: right;"><a class="share_this_setting" data_status = "0" data_to_send = "' . $row['variable'] . '" href="javascript:void(0);">' . Display::return_icon('shared_setting.png', get_lang('ChangeSharedSetting')) . '</a></div>'); } else { $form->addElement('html', '<div style="float: right;"><a class="share_this_setting" data_status = "1" data_to_send = "' . $row['variable'] . '" href="javascript:void(0);">' . Display::return_icon('shared_setting_na.png', get_lang('ChangeSharedSetting')) . '</a></div>'); } } else { if ($row['access_url_changeable'] == '1') { $form->addElement('html', '<div style="float: right;">' . Display::return_icon('shared_setting.png', get_lang('ChangeSharedSetting')) . '</div>'); } else { $form->addElement('html', '<div style="float: right;">' . Display::return_icon('shared_setting_na.png', get_lang('ChangeSharedSetting')) . '</div>'); } } } } } $hideme = array(); $hide_element = false; if ($_configuration['access_url'] != 1) { if ($row['access_url_changeable'] == 0) { // We hide the element in other cases (checkbox, radiobutton) we 'freeze' the element. $hide_element = true; $hideme = array('disabled'); } elseif ($url_info['active'] == 1) { // We show the elements. if (empty($row['variable'])) { $row['variable'] = 0; } if (empty($row['subkey'])) { $row['subkey'] = 0; } if (empty($row['category'])) { $row['category'] = 0; } if (is_array($settings_by_access_list[$row['variable']][$row['subkey']][$row['category']])) { // We are sure that the other site have a selected value. if ($settings_by_access_list[$row['variable']][$row['subkey']][$row['category']]['selected_value'] != '') { $row['selected_value'] = $settings_by_access_list[$row['variable']][$row['subkey']][$row['category']]['selected_value']; } } // There is no else{} statement because we load the default $row['selected_value'] of the main Chamilo site. } } switch ($row['type']) { case 'textfield': if (in_array($row['variable'], $convert_byte_to_mega_list)) { $form->addElement('text', $row['variable'], array(get_lang($row['title']), get_lang($row['comment']), get_lang('MB')), array('class' => 'span1', 'maxlength' => '8')); $form->applyFilter($row['variable'], 'html_filter'); $default_values[$row['variable']] = round($row['selected_value'] / 1024 / 1024, 1); } elseif ($row['variable'] == 'account_valid_duration') { $form->addElement('text', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])), array('maxlength' => '5')); $form->applyFilter($row['variable'], 'html_filter'); $default_values[$row['variable']] = $row['selected_value']; // For platform character set selection: Conversion of the textfield to a select box with valid values. } elseif ($row['variable'] == 'platform_charset') { $current_system_encoding = api_refine_encoding_id(trim($row['selected_value'])); $valid_encodings = array_flip(api_get_valid_encodings()); if (!isset($valid_encodings[$current_system_encoding])) { $is_alias_encoding = false; foreach ($valid_encodings as $encoding) { if (api_equal_encodings($encoding, $current_system_encoding)) { $is_alias_encoding = true; $current_system_encoding = $encoding; break; } } if (!$is_alias_encoding) { $valid_encodings[$current_system_encoding] = $current_system_encoding; } } foreach ($valid_encodings as $key => &$encoding) { if (api_is_encoding_supported($key) && Database::is_encoding_supported($key)) { $encoding = $key; } else { unset($valid_encodings[$key]); } } $form->addElement('select', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])), $valid_encodings); $default_values[$row['variable']] = $current_system_encoding; } else { $hideme['class'] = 'span4'; $form->addElement('text', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])), $hideme); $form->applyFilter($row['variable'], 'html_filter'); $default_values[$row['variable']] = $row['selected_value']; } break; case 'textarea': if ($row['variable'] == 'header_extra_content') { $file = api_get_path(SYS_PATH) . api_get_home_path() . 'header_extra_content.txt'; $value = ''; if (file_exists($file)) { $value = file_get_contents($file); } $form->addElement('textarea', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])), array('class' => 'span6', 'rows' => '10'), $hideme); $default_values[$row['variable']] = $value; } elseif ($row['variable'] == 'footer_extra_content') { $file = api_get_path(SYS_PATH) . api_get_home_path() . 'footer_extra_content.txt'; $value = ''; if (file_exists($file)) { $value = file_get_contents($file); } $form->addElement('textarea', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])), array('rows' => '10', 'class' => 'span6'), $hideme); $default_values[$row['variable']] = $value; } else { $form->addElement('textarea', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])), array('rows' => '10', 'class' => 'span6'), $hideme); $default_values[$row['variable']] = $row['selected_value']; } break; case 'radio': $values = api_get_settings_options($row['variable']); $group = array(); if (is_array($values)) { foreach ($values as $key => $value) { $element =& $form->createElement('radio', $row['variable'], '', get_lang($value['display_text']), $value['value']); if ($hide_element) { $element->freeze(); } $group[] = $element; } } $form->addGroup($group, $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])), '', false); //julio $default_values[$row['variable']] = $row['selected_value']; break; case 'checkbox': // 1. We collect all the options of this variable. $sql = "SELECT * FROM {$table_settings_current} WHERE variable='" . $row['variable'] . "' AND access_url = 1"; $result = Database::query($sql); $group = array(); while ($rowkeys = Database::fetch_array($result)) { //if ($rowkeys['variable'] == 'course_create_active_tools' && $rowkeys['subkey'] == 'enable_search') { continue; } // Profile tab option should be hidden when the social tool is enabled. if (api_get_setting('allow_social_tool') == 'true') { if ($rowkeys['variable'] == 'show_tabs' && $rowkeys['subkey'] == 'my_profile') { continue; } } // Hiding the gradebook option. if ($rowkeys['variable'] == 'show_tabs' && $rowkeys['subkey'] == 'my_gradebook') { continue; } $element =& $form->createElement('checkbox', $rowkeys['subkey'], '', get_lang($rowkeys['subkeytext'])); if ($row['access_url_changeable'] == 1) { // 2. We look into the DB if there is a setting for a specific access_url. $access_url = $_configuration['access_url']; if (empty($access_url)) { $access_url = 1; } $sql = "SELECT selected_value FROM {$table_settings_current} WHERE variable='" . $rowkeys['variable'] . "' AND subkey='" . $rowkeys['subkey'] . "' AND subkeytext='" . $rowkeys['subkeytext'] . "' AND access_url = {$access_url}"; $result_access = Database::query($sql); $row_access = Database::fetch_array($result_access); if ($row_access['selected_value'] == 'true' && !$form->isSubmitted()) { $element->setChecked(true); } } else { if ($rowkeys['selected_value'] == 'true' && !$form->isSubmitted()) { $element->setChecked(true); } } if ($hide_element) { $element->freeze(); } $group[] = $element; } $form->addGroup($group, $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])), ''); break; case 'link': $form->addElement('static', null, array(get_lang($row['title']), get_lang($row['comment'])), get_lang('CurrentValue') . ' : ' . $row['selected_value'], $hideme); break; case 'select': /* * To populate the list of options, the select type dynamically calls a function that must be called select_ + the name of the variable being displayed. * The functions being called must be added to the file settings.lib.php. */ $form->addElement('select', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])), call_user_func('select_' . $row['variable']), $hideme); $default_values[$row['variable']] = $row['selected_value']; break; case 'custom': break; } switch ($row['variable']) { case 'pdf_export_watermark_enable': $url = PDF::get_watermark(null); if ($url != false) { $delete_url = '<a href="?delete_watermark">' . get_lang('DelImage') . ' ' . Display::return_icon('delete.png', get_lang('DelImage')) . '</a>'; $form->addElement('html', '<div style="max-height:100px; max-width:100px; margin-left:162px; margin-bottom:10px; clear:both;"><img src="' . $url . '" style="margin-bottom:10px;" />' . $delete_url . '</div>'); } $form->addElement('file', 'pdf_export_watermark_path', get_lang('AddWaterMark')); $allowed_picture_types = array('jpg', 'jpeg', 'png', 'gif'); $form->addRule('pdf_export_watermark_path', get_lang('OnlyImagesAllowed') . ' (' . implode(',', $allowed_picture_types) . ')', 'filetype', $allowed_picture_types); break; case 'timezone_value': $timezone = $row['selected_value']; if (empty($timezone)) { $timezone = _api_get_timezone(); } $form->addElement('html', sprintf(get_lang('LocalTimeUsingPortalTimezoneXIsY'), $timezone, api_get_local_time())); break; } } // end for if (!empty($settings)) { $form->setDefaults($default_values); } $form->addElement('button', 'submit_fixed_in_bottom', get_lang('SaveSettings'), 'class="save"'); return $form; }
/** * Detects encoding of plain text. * @param string $string The input text. * @param string $language (optional) The language of the input text, provided if it is known. * @return string Returns the detected encoding. */ function api_detect_encoding($string, $language = null) { // Testing against valid UTF-8 first. if (api_is_valid_utf8($string)) { return 'UTF-8'; } $result = null; $delta_points_min = LANGUAGE_DETECT_MAX_DELTA; // Testing non-UTF-8 encodings. $encodings = api_get_valid_encodings(); foreach ($encodings as &$encoding) { if (api_is_encoding_supported($encoding) && !api_is_utf8($encoding)) { $stringToParse = api_substr($string, 0, LANGUAGE_DETECT_MAX_LENGTH, $encoding); $strintToParse2 = _api_generate_n_grams($stringToParse, $encoding); $result_array = _api_compare_n_grams($strintToParse2, $encoding); if (!empty($result_array)) { list($key, $delta_points) = each($result_array); if ($delta_points < $delta_points_min) { $pos = strpos($key, ':'); $result_encoding = api_refine_encoding_id(substr($key, $pos + 1)); if (api_equal_encodings($encoding, $result_encoding)) { if ($string == api_utf8_decode(api_utf8_encode($string, $encoding), $encoding)) { $delta_points_min = $delta_points; $result = $encoding; } } } } } } // "Broken" UTF-8 texts are to be detected as UTF-8. // This functionality is enabled when language of the text is known. $language = api_purify_language_id((string) $language); if (!empty($language)) { $encoding = 'UTF-8'; $result_array =& _api_compare_n_grams(_api_generate_n_grams(api_substr($string, 0, LANGUAGE_DETECT_MAX_LENGTH, $encoding), $encoding), $encoding); if (!empty($result_array)) { list($key, $delta_points) = each($result_array); if ($delta_points < $delta_points_min) { $pos = strpos($key, ':'); $result_encoding = api_refine_encoding_id(substr($key, $pos + 1)); $result_language = substr($key, 0, $pos); if ($language == $result_language && api_is_utf8($result_encoding)) { $delta_points_min = $delta_points; $result = $encoding; } } } } return $result; }