|
From: Culprit <cul...@us...> - 2008-02-28 20:38:24
|
Update of /cvsroot/mxbb/mx_langtools/includes In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv8405/includes Added Files: translator.php index.htm Log Message: --- NEW FILE: index.htm --- <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body bgcolor="#FFFFFF" text="#000000"> </body> </html> --- NEW FILE: translator.php --- <?php if (!defined('IN_PORTAL')) { exit; } define( 'MXP_LANG_TOOLS_COOKIE_NAME', 'lT_'); /** * mxp_translator * * @package Translator * @author culprit_cz * @copyright Copyright (c) 2008 * @version $Id: translator.php,v 1.1 2008/02/28 20:38:19 culprit_cz Exp $ * @access public */ class mxp_translator { var $language_list = array(); var $module_list = array(); var $language_file_list = array(); var $lang = array(); var $orig_ary = array(); var $tran_ary = array(); var $language_from = 'lang_english'; var $langauge_into = ''; var $module_select = ''; var $module_file = ''; var $file_encoding = 'UTF-8'; var $file_save_path = ''; var $file_save_content = ''; /** * Constructor * * @return */ function mxp_translator() { global $mx_root_path, $phpEx, $lang; $this->language_into = $this->__cookie( MXP_LANG_TOOLS_COOKIE_NAME . 'language_into', @$_POST['language']['into'] ); $this->module_select = $this->__cookie( MXP_LANG_TOOLS_COOKIE_NAME . 'module_select', @$_POST['translate']['module']); $this->module_file = $this->__cookie( MXP_LANG_TOOLS_COOKIE_NAME . 'module_file' , @$_POST['translate']['file']); $this->get_lang_list(); $this->get_module_list(); $this->get_file_list(); /** * SELECT encoding of language file */ $lang_enc = $this->_load_file_to_translate( $mx_root_path . 'includes/shared/phpbb2/language/' . $this->language_from . '/lang_main.' . $phpEx); $lang_enc = $this->_load_file_to_translate( $mx_root_path . 'includes/shared/phpbb2/language/' . $this->language_into . '/lang_main.' . $phpEx); if ( isset( $lang_enc['ENCODING']) && $lang_enc != '') { $this->file_encoding = $lang_enc['ENCODING']; } $this->file_save_path = $mx_root_path . ( $_GET['s']=='MODS'? $this->module_select:'') . 'language/' . $this->language_into . '/' . $this->module_file; } /** * Set and get value from posted or cookie * @return mixed value generated from posted, geted or cookie * @param $name string cookie name of the value * @param $value mixed value which should be setted for cookie */ function __cookie( $name, $value = '') { global $board_config; $cookie_board_name = $name; $return = ''; if ( $value != '') { $return = $value; // Currently not working under linux machines [Ubuntu GG] //setcookie( $cookie_board_name, $value, (time()+21600), $board_config['cookie_path'], $board_config['cookie_domain'], $board_config['cookie_secure']); setcookie( $cookie_board_name, $value, (time()+21600), $board_config['cookie_path']); $_COOKIE[ $cookie_board_name] = $value; } else if( isset( $_COOKIE[ $cookie_board_name]) && $_COOKIE[ $cookie_board_name] != '') { $value = $_COOKIE[ $cookie_board_name]; // Currently not working under linux machines [Ubuntu GG] //setcookie( $cookie_board_name, $_COOKIE[ $cookie_board_name], (time()+21600), $board_config['cookie_path'], $board_config['cookie_domain'], $board_config['cookie_secure']); setcookie( $cookie_board_name, $_COOKIE[ $cookie_board_name], (time()+21600), $board_config['cookie_path']); } $_COOKIE['test' . $name] = $value; return $value; } /** * Load available languages list * * @return array available languages list: KEY = folder name */ function get_lang_list() { global $mx_root_path; if ( count( $this->language_list) ) { return $this->language_list; } $dir = opendir( $mx_root_path . 'language/'); while( $f = readdir( $dir)) { if ( ( $f == '.' || $f == '..') || !is_dir( $mx_root_path . 'language/' . $f) || strpos( $f, 'lang_') === false || $f == $this->language_from) { continue; } if ( $this->language_from == '') { $this->language_from = $this->__cookie( MXP_LANG_TOOLS_COOKIE_NAME . 'language_from', $f ); } $this->language_list[$f] = ucfirst(str_replace( 'lang_', '', $f)); } closedir( $dir); return $this->language_list; } function get_file_list( $module = '') { $file_list =array(); switch ( $_GET['s']) { case 'MXP': $file_list = $this->__load_lang_files( '', $this->language_from); $this->language_file_list['MXP'] = $file_list; break; case 'MODS': if ( $this->module_select == '') { return array(); } $file_list = $this->__load_lang_files( $module, $this->language_from); $this->language_file_list[$module] = $file_list; break; default: break; } return $file_list; } function __load_lang_files( $path, $language, $add_path = '') { global $mx_root_path, $phpEx; if ( $this->language_from == '') { return null; } $lang_files = array(); $folder_path = $mx_root_path . $path . 'language/' . $language . $add_path; if ( !file_exists( $folder_path)) { return array(); } $dir = opendir( $folder_path); while( $file = readdir( $dir)) { if ( $file == '.' || $file == '..' || $file == 'CVS' || eregi( '\.' . $phpEx . '$', $file) == false) { continue; } if ( is_dir( $folder_path . '/' . $file)) { $sub_files = $this->__load_lang_files( $path, $language, $add_path . '/'. $file ); $lang_files = array_merge( $lang_files, $sub_files); } else if( is_file( $folder_path . '/' . $file)) { $lang_files[$add_path . $file] = $add_path . $file; } } closedir( $dir); return $lang_files; } function get_module_list() { global $mx_root_path, $db; $sql = "SELECT module_path, module_name FROM " . MODULE_TABLE . " ORDER BY module_name"; if ( !($rs = $db->sql_query( $sql))) { mx_message_die( GENERAL_ERROR, 'Could not obtain module list', __LINE__, __FILE__, $sql); } while( $row = $db->sql_fetchrow( $rs)) { $file_list = $this->__load_lang_files( $row['module_path'], $this->language_from); if ( count( $file_list) == 0) { continue; } if ( $this->module_select == '') { $this->module_select = $this->__cookie( MXP_LANG_TOOLS_COOKIE_NAME . 'module_select', $row['module_path']); } $this->module_list[$row['module_path']] = $row['module_name']; $this->language_file_list[$row['module_path']] = $file_list; } return $this->module_list; } /** * Generate option list * @return HMTML option list * @param $html string generate option list in HTML or JS format /now available only HTML/ * @param $which_list string which option list should be generated /' * @param $selected string key of selected item * @param $disabled mixed list of disabed key items * @param $from_select boolean is the list initial? */ function gen_select_list( $html, $which_list, $selected = '', $disabled = '' ) { switch ( $which_list) { case 'language': $list_ary = $this->language_list; break; case 'modules': $list_ary = $this->module_list; break; case 'core': $list_ary = $this->language_file_list['MXP']; break; case 'files': switch( $_GET['s']) { case 'MXP': $list_ary = $this->language_file_list['MXP']; break; case 'MODS': $list_ary = $this->language_file_list[$this->module_select]; break; } break; default: return ''; break; } if ( count( $list_ary) < 1) { return ''; } asort( $list_ary); reset( $list_ary); $option_list = ''; $num_args = func_num_args(); switch ( $html) { case 'html': foreach( $list_ary as $key => $value) { if ( ( is_array( $disabled) && in_array( $key, $disabled)) || (!is_array( $disabled) && $key == $disabled)) { continue; } $option_list .= '<option value="' . $key . '"'; if ( $selected == $key ) { $option_list .= ' selected'; } $option_list .= '>' . $value . '</option>'; } break; } return $option_list; } function _load_lang( $path, $filename, $require = true) { global $phpEx, $board_config, $mx_user, $userdata; $mx_user_path = $path . 'language/lang_' . $userdata['user_lang'] . '/' . $filename . '.' . $phpEx; $mx_board_path = $path . 'language/lang_' . $board_config['default_lang'] . '/' . $filename . '.' . $phpEx; $mx_default_path = $path . 'language/lang_english/' . $filename . '.' . $phpEx; $lang = array(); if ( file_exists( $mx_user_path)) { include_once( $mx_user_path); } else if ( $require) { if ( file_exists( $mx_board_path)) { include_once( $mx_board_path); } else if ( file_exists( $mx_default_path)) { include_once( $mx_default_path); } } $this->lang = array_merge( $this->lang, $lang); } function assign_template_vars( &$template) { global $module_root_path; $this->_load_lang( $module_root_path, 'lang_admin'); reset( $this->lang); foreach( $this->lang as $key => $value) { $template->assign_var( 'L_' . strtoupper( $key), $value); } } function file_translate() { global $lang, $mx_root_path, $template; if ( !isset( $_POST['set_file']) || $_POST['set_file'] != $lang['Submit']) { return; } $original_file_path = ( $_GET['s']=='MODS'? $this->module_select:'') . 'language/' . $this->language_from . '/' . $this->module_file; $translate_file_path = ( $_GET['s']=='MODS'? $this->module_select:'') . 'language/' . $this->language_into . '/' . $this->module_file; //$original_file_content = file_get_contents( ) //print $this->_get_file_perms($mx_root_path . $translate_file_path); $template->assign_vars( array( //# 'FILE_FULL_ROOT_PATH_ORIGINAL' => '/' . $original_file_path, 'FILE_FULL_ROOT_PATH_TRANSLATE' => '/' . $translate_file_path, 'FILE_IS_WRITABLE' => $this->__is_writable( $mx_root_path . $translate_file_path )? '1': '0', )); $this->orig_ary = $this->_load_file_to_translate($mx_root_path . $original_file_path); $this->tran_ary = $this->_load_file_to_translate($mx_root_path . $translate_file_path); if ( count( $this->orig_ary) == 0) { //nic neni v souboru die( 'v souboryu nic neni'); return; } else { $counter = 0; foreach( $this->orig_ary as $l_key => $l_value ) { $template->assign_block_vars( 'language_item', array( //# 'U_KEY' => strtoupper( $l_key), 'KEY' => $l_key, 'ORIGINAL_VALUE' => htmlentities(preg_replace( '#<br[^>]*>#i', '\0'. "\n", $l_value)), 'TRANSLATE_VALUE' => preg_replace( '#<br[^>]*>#i', '\0'. "\n",$this->tran_ary[$l_key]), 'COUNTER' => $counter, )); $counter++; } } } function __is_writable( $file) { if ( file_exists( $file) ) { return is_writable( $file); } else if ( file_exists( dirname( $file) ) ) { return is_writable( dirname( $file)); } else { return is_writable( dirname( dirname( $file))); } } function _load_file_to_translate( $filename) { if ( !file_exists( $filename)) { return array(); } include( $filename); return $lang; } function file_preparesave() { global $userdata, $mx_user; if ( file_exists( $this->file_save_path ) && !isset( $_POST['resetheader']) ) { $file_content = file_get_contents( $this->file_save_path); $file_content = substr( $file_content, 0, strpos( $file_content, '*/')); $file_content = substr( $file_content, strpos( $file_content, '/*')); } else { $file_content = "/** * Language file [" . $this->module_file . "] * * @package language * @author " . $userdata['username'] . " * @version $Id: translator.php,v 1.1 2008/02/28 20:38:19 culprit_cz Exp $this->module_file . ",v 1.-1 " . date( 'Y/m/d H:i:s') ." " . $userdata['username'] . " Exp $ * @copyright (c) 2002-2008 [Jon Ohlsson] MX-Publisher Project Team * @license http://opensource.org/licenses/gpl-license.php GNU General Public License v2 * @link http://www.mx-publisher.com "; } $file_content = preg_replace( '#\* (Encoding|1 tab).*'. "\n" . '#', '', $file_content); $file_content .= '* Encoding: ' . $this->file_encoding . "\n* 1 tab = 4 spaces\n */"; $file_content = preg_replace( "#\n[^*]*\*#i", "\n *", $file_content); $v_cnt = preg_match_all( '#(@version[^,]*,v [0-9]*\.)([^ ]*)#', $file_content, $version); $version[2][0]++; $file_content = preg_replace( '#(@version[^,]*,v [0-9]*)\.([^ ]*)#', '\1.'. $version[2][0], $file_content); $file_key = array_keys( $_POST['l']); $key_m_len = 0; foreach( $file_key as $key => $value) { $key_m_len = ( strlen($value)>$key_m_len ) ? strlen($value):$key_m_len; } $file_content_values = ''; foreach( $_POST['l'] as $key => $value) { if ( !empty($value) ) { $value = preg_replace( "#(<br[^>]*>)\r?\n#si", '\1', $value); $value = preg_replace( '#\\\\"#si', '"', $value); $file_content_values .= "\t'" . $key . "'" . str_repeat( " ", $key_m_len - strlen( $key)+2) . "=> '" . $value . "',\n"; } } if ( $file_content_values != '' ) { $file_content .= "\n\nif ( !isset(\$lang) )\n{\n\t\$lang = array();\n}\n\n\$lang = array_merge( \$lang, array( // #\n"; $file_content .= str_replace( ' =>', '=>',preg_replace( '# [ ]{1,3}#', "\t", $file_content_values)); $file_content .= '));'; } else { $file_content .= "\n\n// Nothing translated\n\n"; } $this->file_save_content = "<" . "?php\n" . $file_content . "\n\n//\n// That's all Folks!\n// -------------------------------------------------\n?" . ">"; } function file_save() { // Control path id exists function __control_folder( $folder) { if ( !file_exists( $folder) ) { __control_folder( dirname( $folder)); mkdir( $folder); @chmod( $folder, 0755); $fp = fopen( $folder . '/index.htm', 'w'); fwrite( $fp, "<html>\n<head>\n<title></title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n</head>\n\n<body bgcolor=\"#FFFFFF\" text=\"#000000\">\n\n</body>\n</html>"); fclose( $fp); @chmod( $folder. '/index.htm', 0644); } } __control_folder( dirname( $this->file_save_path)); $fp = fopen( $this->file_save_path, 'w'); fwrite( $fp, $this->file_save_content); fclose( $fp); @chmod( $this->file_save_path, 0644); } function file_download() { header('Content-Type: application/x-download; name="' . $this->module_file . '"'); header('Content-Disposition: inline; filename="' . $this->module_file . '"'); header('Content-length: ' . strlen($this->file_save_content)); header('Content-encoding: ' . $this->file_encoding); print $this->file_save_content; die(); } } ?> |