+++ /dev/null
-<?php\r
-/****************************************************************/\r
-/* ATutor */\r
-/****************************************************************/\r
-/* Copyright (c) 2002-2008 by Greg Gay & Cindy Qi Li */\r
-/* Adaptive Technology Resource Centre / University of Toronto */\r
-/* http://atutor.ca */\r
-/* */\r
-/* This program is free software. You can redistribute it and/or*/\r
-/* modify it under the terms of the GNU General Public License */\r
-/* as published by the Free Software Foundation. */\r
-/****************************************************************/\r
-// $Id: utf8conv.php 2008-01-23 14:49:24Z cindy $\r
-/*\r
- * This script only works when being included in the scripts\r
- * where vitals.inc.php is included as:\r
- * \r
- * define('AT_INCLUDE_PATH', '../../include/');\r
- * require (AT_INCLUDE_PATH.'vitals.inc.php');\r
- * \r
- * structure of this document (in order):\r
- * \r
- * 1. Unzip uploaded file to module's content directory\r
- * 2. Read content folder recursively and search through all html and xml files \r
- * to find "charset" defined in html "meta" tag and "charset" defined in xml files. \r
- * If:\r
- * (1) Only 1 character set found:\r
- * The module converts the files with file types to convert from found charset \r
- * to UTF-8.\r
- * (2) More than 1 character set found:\r
- * The module displays a drop-down listbox with all found character sets. User \r
- * selects one and clicks "Go" button. The module will do the conversion from \r
- * the selcted character set to UTF-8.\r
- * (3) No character set found:\r
- * The module displays a drop-down listbox with default character sets. User \r
- * selects one and clicks "Go" button. The module will do the conversion from \r
- * the selcted character set to UTF-8.\r
- * 3. Zip converted files\r
- * 4. force zipped converted file to download\r
- * 5. clear all temp files\r
- *\r
- ***/\r
-\r
-// Define character set to convert to\r
-$charset_to = "UTF-8";\r
-\r
-// Define all file types to be converted\r
-$filetypes = ARRAY('html', 'xml', 'csv', 'txt', 'sql');\r
-\r
-$filetypes = array_change_key_case($filetypes); // use lower case\r
-\r
-$default_charsets = ARRAY(\r
-'ISO-8859-1','ISO-8859-2','ISO-8859-3','ISO-8859-4','ISO-8859-5','ISO-8859-6','ISO-8859-7',\r
-'ISO-8859-8','ISO-8859-9','ISO-8859-10','ISO-8859-11','ISO-8859-13','ISO-8859-14',\r
-'ISO-8859-15','ISO-8859-16','BIG5','EUC-JP','GB2312','US-ASCII','WINDOWS-874','WINDOWS-936',\r
-'WINDOWS-1250','WINDOWS-1251','WINDOWS-1252','WINDOWS-1253','WINDOWS-1254','WINDOWS-1255',\r
-'WINDOWS-1256','WINDOWS-1257','WINDOWS-1258');\r
-\r
-$charsets = array();\r
-\r
-/**\r
-* This function finds charset definition from html & xml files\r
-* @access public\r
-* @param string $filename The name of the file to find charset definition\r
-* output of readDir\r
-* @author Cindy Qi Li\r
-*/\r
-function find_charset($filename)\r
-{\r
- if (preg_match("/\.html$/i", $filename)) \r
- { \r
- $pattern = '/<meta.*charset=(.*) /i';\r
- preg_match($pattern, file_get_contents($filename), $matches);\r
- \r
- // remove quote signs in the match\r
- $charset = strtoupper(trim(preg_replace('/(\'|\")/', '', $matches[1])));\r
- }\r
- \r
- if (preg_match("/\.xml$/i", $filename)) \r
- { \r
- if (preg_match("#<charset>(.*)</charset>#i", file_get_contents($filename), $matches)) \r
- {\r
- $charset = strtoupper(trim($matches[1]));\r
- }\r
- }\r
- \r
- return $charset;\r
-}\r
-\r
-/**\r
-* This function finds all charsets defined in all html & xml files in the given zip\r
-* and save the charsets in global variable $charsets\r
-* @access public\r
-* @param string $path The full path and name of the files to find charset definition\r
-* output of readDir\r
-* @author Cindy Qi Li\r
-*/\r
-function find_all_charsets($path, $filename)\r
-{\r
- global $charsets;\r
- \r
- $charset = find_charset($path);\r
-\r
- if (strlen($charset) > 0 && !in_array($charset, $charsets))\r
- {\r
- array_push($charsets, $charset);\r
- }\r
-}\r
-\r
-/**\r
-* This function:\r
-* 1. replaces the charset strings defined in html "meta" tag and "charset" tag in xml files to "UTF-8";\r
-* 2. convert files from old character set to UTF-8.\r
-* @access public\r
-* @param string $path The full path and name of the files to find charset definition\r
-* output of readDir\r
-* @author Cindy Qi Li\r
-*/\r
-function convert($path, $filename)\r
-{\r
- global $charset_from, $charset_to;\r
- global $filetypes;\r
- global $charsets;\r
- \r
- // 1. html & xml files:\r
- // if charset is defined, convert from defined charset,\r
- // otherwise, convert from $charset_from\r
- // 2. Other files with defined file type\r
- // convert from $charset_from\r
- if ((in_array('html', $filetypes) && preg_match("/\.html$/i", $path)) ||\r
- (in_array('xml', $filetypes) && preg_match("/\.xml$/i", $path))) \r
- { \r
- $charset_in = find_charset($path);\r
- \r
- $content = file_get_contents($path);\r
- // convert file\r
- if (strlen($charset_in) > 0)\r
- {\r
- // replace old charset in <meta> tag to new charset\r
- $content = str_ireplace($charset_in,$charset_to,$content);\r
- \r
- // convert file from old charset to new charset\r
- $content = iconv($charset_in, $charset_to. '//IGNORE', $content);\r
- }\r
- else\r
- {\r
- $content = iconv($charset_from, $charset_to. '//IGNORE', $content);\r
- } // end inner if\r
- \r
- $fp = fopen($path,'w');\r
- fwrite($fp,$content);\r
- fclose($fp);\r
- }\r
- elseif (in_array(strtolower(substr($path, (strrpos($path, '.')+1))),$filetypes))\r
- {\r
- $content = file_get_contents($path);\r
- $content = iconv($charset_from, $charset_to. '//IGNORE', $content);\r
-\r
- $fp = fopen($path,'w');\r
- fwrite($fp,$content);\r
- fclose($fp);\r
- }\r
-}\r
-\r
-/**\r
-* This function displays all values in $charsets_array in a drop-down box \r
-* @access public\r
-* @param array $charsets_array The options to display\r
-* @author Cindy Qi Li\r
-*/\r
-function display_options($charsets_array)\r
-{\r
-?>\r
- <select name="charfrom" id="charfrom" class="input">\r
-<?php\r
- foreach($charsets_array as $charset)\r
- {\r
-?>\r
- <option><?php echo $charset; ?></option>\r
-<?php\r
- }\r
-?>\r
- </select>\r
-<?php\r
-}\r
-\r
-/**\r
-* This function deletes $dir recrusively without deleting $dir itself.\r
-* @access public\r
-* @param string $charsets_array The name of the directory where all files and folders under needs to be deleted\r
-* @author Cindy Qi Li\r
-*/\r
-function clear_dir($dir) {\r
- include_once(AT_INCLUDE_PATH . '/lib/filemanager.inc.php');\r
- \r
- if(!$opendir = @opendir($dir)) {\r
- return false;\r
- }\r
- \r
- while(($readdir=readdir($opendir)) !== false) {\r
- if (($readdir !== '..') && ($readdir !== '.')) {\r
- $readdir = trim($readdir);\r
-\r
- clearstatcache(); /* especially needed for Windows machines: */\r
-\r
- if (is_file($dir.'/'.$readdir)) {\r
- if(!@unlink($dir.'/'.$readdir)) {\r
- return false;\r
- }\r
- } else if (is_dir($dir.'/'.$readdir)) {\r
- /* calls lib function to clear subdirectories recrusively */\r
- if(!clr_dir($dir.'/'.$readdir)) {\r
- return false;\r
- }\r
- }\r
- }\r
- } /* end while */\r
-\r
- @closedir($opendir);\r
- \r
- return true;\r
-}\r
-\r
-/**\r
-* Main convert process:\r
-* 1. Unzip uploaded file to module's content directory\r
-* 2. Convert unzipped files with file types to convert\r
-* 3. Zip converted files\r
-* 4. force zipped converted file to download\r
-* 5. clear all temp files\r
-* @access public\r
-* @author Cindy Qi Li\r
-*/\r
-$module_content_folder = AT_CONTENT_DIR . "utf8conv";\r
- \r
-include_once(AT_INCLUDE_PATH . '/classes/pclzip.lib.php');\r
- \r
-if (isset($_POST['Convert']))\r
-{\r
- // clean up module content folder\r
- clear_dir($module_content_folder);\r
- \r
- // 1. unzip uploaded file to module's content directory\r
- $archive = new PclZip($_FILES['userfile']['tmp_name']);\r
-\r
- if ($archive->extract(PCLZIP_OPT_PATH, $module_content_folder) == 0)\r
- {\r
- clear_dir($module_content_folder);\r
- die("Cannot unzip file " . $_FILES['userfile']['tmp_name'] . "<br>Error : ".$archive->errorInfo(true));\r
- }\r
-}\r
-\r
-if (isset($_POST['Convert']) || $_POST['Go'])\r
-{\r
- // 2. Read content folder recursively to convert.\r
- include_once("readDir.php");\r
- \r
- $dir = new readDir();\r
- // set the directory to read\r
- if (!$dir->setPath( $module_content_folder )) \r
- { \r
- clear_dir($module_content_folder);\r
- die($dir->error());\r
- } \r
-\r
- // set recursive reading of sub folders\r
- $dir->readRecursive(true); \r
- \r
- // set a function to call when a new file is read\r
- if (!$dir->setEvent( 'readDir_file', 'find_all_charsets' ))\r
- { \r
- clear_dir($module_content_folder);\r
- die($dir->error());\r
- } \r
- \r
- // read the dir\r
- if ( !$dir->read() ) \r
- { \r
- clear_dir($module_content_folder);\r
- die($dir->error());\r
- }\r
- \r
- // If only one character set is found in all html & xml files\r
- if ((count($charsets) == 1 && $_POST['Convert']) ||\r
- (count($charsets) != 1 && $_POST['Go']))\r
- {\r
- if (count($charsets) == 1 && $_POST['Convert'])\r
- $charset_from = $charsets[0];\r
- elseif (count($charsets) != 1 && $_POST['Go'])\r
- $charset_from = $_POST['charfrom'];\r
-\r
- // Real conversion\r
- $dir = new readDir();\r
- $dir->setPath( $module_content_folder );\r
- $dir->readRecursive(true); \r
- $dir->setEvent( 'readDir_file', 'convert' );\r
- $dir->read();\r
- \r
- // 3. ZIP converted files\r
- if ($_POST['Convert']) $orig_filename=$_FILES['userfile']['name'];\r
- elseif ($_POST['Go']) $orig_filename=$_POST['filename'];\r
- \r
- $zip_filename = AT_CONTENT_DIR . "/" . str_replace('.zip','_'.$charset_to . '.zip', $orig_filename);\r
- \r
- $archive = new PclZip($zip_filename);\r
- \r
- if ($archive->add($module_content_folder, PCLZIP_OPT_REMOVE_PATH, $module_content_folder) == 0) {\r
- clear_dir($module_content_folder);\r
- die("Cannot zip converted files. <br>Error : ".$archive->errorInfo(true));\r
- }\r
- \r
- // 4. force zipped converted file to download\r
- ob_end_clean();\r
- \r
- header('Content-Type: application/x-zip');\r
- header('Content-transfer-encoding: binary'); \r
- header('Content-Disposition: attachment; filename="'.htmlspecialchars(basename($zip_filename)) . '"');\r
- header('Expires: 0');\r
- header('Cache-Control: must-revalidate, post-check=0, pre-check=0');\r
- header('Pragma: public');\r
- \r
- readfile($zip_filename);\r
- \r
- // 5. clear all temp files\r
- unlink($zip_filename);\r
- clear_dir($module_content_folder);\r
- }\r
-}\r
-// End of main convert process\r
-\r
-include_once (AT_INCLUDE_PATH.'header.inc.php');\r
-\r
-// Check ICONV library is installed and enabled\r
-if (!extension_loaded('iconv') || !function_exists('iconv'))\r
-{\r
- die ('<font color="red">Warning: This utility is not available as PHP ICONV module is not installed or not enabled.</font>');\r
-}\r
-?>\r
-\r
-<HTML>\r
-\r
-<SCRIPT LANGUAGE="JavaScript">\r
-<!--\r
-\r
-String.prototype.trim = function() {\r
- return this.replace(/^\s+|\s+$/g,"");\r
-}\r
-\r
-// This function validates if and only if a zip file is given\r
-function validate_filename() {\r
- // check file type\r
- var file = document.frm_upload.userfile.value;\r
- if (!file || file.trim()=='') {\r
- alert('Please give a zip file!');\r
- return false;\r
- }\r
- \r
- if(file.slice(file.lastIndexOf(".")).toLowerCase() != '.zip') {\r
- alert('Please upload ZIP file only!');\r
- return false;\r
- }\r
-}\r
-\r
-// End -->\r
-//-->\r
-</script>\r
-\r
-<BODY>\r
- <FORM NAME="frm_upload" ENCTYPE="multipart/form-data" METHOD=POST ACTION="<?php echo $_SERVER['PHP_SELF']; ?>" >\r
- \r
- <div class="input-form">\r
-\r
- <div class="row">\r
- Upload a zip file to convert the character set to UTF-8:\r
- </div>\r
-\r
- <div class="row">\r
- <INPUT TYPE="hidden" name="MAX_FILE_SIZE" VALUE="52428800">\r
- <INPUT TYPE="file" NAME="userfile" SIZE=50>\r
- </div>\r
- \r
- <div class="row buttons">\r
- <INPUT TYPE="submit" name="Convert" value="Convert" onClick="javascript: return validate_filename(); " class="submit" />\r
- <INPUT TYPE="hidden" name="filename">\r
- </div>\r
-<?php\r
-if ($_POST["Convert"] && count($charsets) != 1)\r
-{\r
-?>\r
-<?php\r
- if (count($charsets) > 1)\r
- {\r
-?>\r
- <div class="row">\r
- <font color="red">Multiple character sets are found, please select one to convert from:</font>\r
- </div>\r
-<?php\r
- display_options($charsets);\r
- }\r
- else\r
- {\r
-?>\r
- <div class="row">\r
- <font color="red">No character set found in zip file, please choose one character set to convert from:</font>\r
- </div>\r
-\r
-<?php\r
- display_options($default_charsets);\r
- }\r
-?>\r
- <div class="row buttons">\r
- <INPUT TYPE="submit" name="Go" value="Go" class="submit" />\r
- </div>\r
-\r
-<?php\r
-}\r
-?>\r
- </div>\r
- \r
- </FORM>\r
- <hr/> \r
-</BODY> \r
-</HTML>\r
-\r
-<SCRIPT LANGUAGE="JavaScript">\r
-<!--\r
-\r
-<?php\r
-// store the upload zip file name in a hidden field for the future use for charsets selection\r
-if ($_POST['Convert'])\r
-{\r
-?>\r
-document.frm_upload.filename.value = '<?php echo $_FILES['userfile']['name']; ?>';\r
-<?php\r
-}\r
-?>\r
-\r
-// End -->\r
-//-->\r
-</script>\r
-\r
-<?php include_once (AT_INCLUDE_PATH.'footer.inc.php'); ?>\r