| <?php
/**
Abstract generic file parser and uploader
Copyright (C) 2007  Johan Barbier <[email protected] >
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
/**
 * @author Johan Barbier <[email protected] >
 * @version 20071101
 * @desc Abstract generic file parser and uploader
 *
 */
abstract class genparser {
	
	/**
	 * upload encoding
	 *
	 * @var string
	 */
	protected $sEncoding;
	
	/**
	 * backoffice object
	 *
	 * @var backoffice
	 */
	protected $subject;
	
	/**
	 * Array of correspondance between html entities and decimal entities
	 *
	 * @var array
	 */
	public static $aCor = array(
		'"' => '"',
		'&' => '&',
		''' => ''',
		'<' => '<',
		'>' => '>',
		' ' => ' ',
		'¡' => '¡',
		'¢' => '¢',
		'£' => '£',
		'¤' => '¤',
		'¥' => '¥',
		'¦' => '¦',
		'§' => '§',
		'¨' => '¨',
		'©' => '©',
		'ª' => 'ª',
		'«' => '«',
		'¬' => '¬',
		'­' => '­',
		'®' => '®',
		'¯' => '¯',
		'°' => '°',
		'±' => '±',
		'²' => '²',
		'³' => '³',
		'´' => '´',
		'µ' => 'µ',
		'¶' => '¶',
		'·' => '·',
		'¸' => '¸',
		'¹' => '¹',
		'º' => 'º',
		'»' => '»',
		'¼' => '¼',
		'½' => '½',
		'¾' => '¾',
		'¿' => '¿',
		'À' => 'À',
		'Á' => 'Á',
		'Â' => 'Â',
		'Ã' => 'Ã',
		'Ä' => 'Ä',
		'Å' => 'Å',
		'Æ' => 'Æ',
		'Ç' => 'Ç',
		'È' => 'È',
		'É' => 'É',
		'Ê' => 'Ê',
		'Ë' => 'Ë',
		'Ì' => 'Ì',
		'Í' => 'Í',
		'Î' => 'Î',
		'Ï' => 'Ï',
		'Ð' => 'Ð',
		'Ñ' => 'Ñ',
		'Ò' => 'Ò',
		'Ó' => 'Ó',
		'Ô' => 'Ô',
		'Õ' => 'Õ',
		'Ö' => 'Ö',
		'×' => '×',
		'Ø' => 'Ø',
		'Ù' => 'Ù',
		'Ú' => 'Ú',
		'Û' => 'Û',
		'Ü' => 'Ü',
		'Ý' => 'Ý',
		'Þ' => 'Þ',
		'ß' => 'ß',
		'à' => 'à',
		'á' => 'á',
		'â' => 'â',
		'ã' => 'ã',
		'ä' => 'ä',
		'å' => 'å',
		'æ' => 'æ',
		'ç' => 'ç',
		'è' => 'è',
		'é' => 'é',
		'ê' => 'ê',
		'ë' => 'ë',
		'ì' => 'ì',
		'í' => 'í',
		'î' => 'î',
		'ï' => 'ï',
		'ð' => 'ð',
		'ñ' => 'ñ',
		'ò' => 'ò',
		'ó' => 'ó',
		'ô' => 'ô',
		'õ' => 'õ',
		'ö' => 'ö',
		'÷' => '÷',
		'ø' => 'ø',
		'ù' => 'ù',
		'ú' => 'ú',
		'û' => 'û',
		'ü' => 'ü',
		'ý' => 'ý',
		'þ' => 'þ',
		'ÿ' => 'ÿ',
		'Œ' => 'Œ',
		'œ' => 'œ',
		'Š' => 'Š',
		'š' => 'š',
		'Ÿ' => 'Ÿ',
		'ƒ' => 'ƒ',
		'ˆ' => 'ˆ',
		'˜' => '˜',
		'Α' => 'Α',
		'Β' => 'Β',
		'Γ' => 'Γ',
		'Δ' => 'Δ',
		'Ε' => 'Ε',
		'Ζ' => 'Ζ',
		'Η' => 'Η',
		'Θ' => 'Θ',
		'Ι' => 'Ι',
		'Κ' => 'Κ',
		'Λ' => 'Λ',
		'Μ' => 'Μ',
		'Ν' => 'Ν',
		'Ξ' => 'Ξ',
		'Ο' => 'Ο',
		'Π' => 'Π',
		'Ρ' => 'Ρ',
		'Σ' => 'Σ',
		'Τ' => 'Τ',
		'Υ' => 'Υ',
		'Φ' => 'Φ',
		'Χ' => 'Χ',
		'Ψ' => 'Ψ',
		'Ω' => 'Ω',
		'α' => 'α',
		'β' => 'β',
		'γ' => 'γ',
		'δ' => 'δ',
		'ε' => 'ε',
		'ζ' => 'ζ',
		'η' => 'η',
		'θ' => 'θ',
		'ι' => 'ι',
		'κ' => 'κ',
		'λ' => 'λ',
		'μ' => 'μ',
		'ν' => 'ν',
		'ξ' => 'ξ',
		'ο' => 'ο',
		'π' => 'π',
		'ρ' => 'ρ',
		'ς' => 'ς',
		'σ' => 'σ',
		'τ' => 'τ',
		'υ' => 'υ',
		'φ' => 'φ',
		'χ' => 'χ',
		'ψ' => 'ψ',
		'ω' => 'ω',
		'ϑ' => 'ϑ',
		'ϒ' => 'ϒ',
		'ϖ' => 'ϖ',
		' ' => ' ',
		' ' => ' ',
		' ' => ' ',
		'‌' => '‌',
		'‍' => '‍',
		'‎' => '‎',
		'‏' => '‏',
		'–' => '–',
		'—' => '—',
		'‘' => '‘',
		'’' => '’',
		'‚' => '‚',
		'“' => '“',
		'”' => '”',
		'„' => '„',
		'†' => '†',
		'‡' => '‡',
		'•' => '•',
		'…' => '…',
		'‰' => '‰',
		'′' => '′',
		'″' => '″',
		'‹' => '‹',
		'›' => '›',
		'‾' => '‾',
		'⁄' => '⁄',
		'€' => '€',
		'ℑ' => 'ℑ',
		'℘' => '℘',
		'ℜ' => 'ℜ',
		'™' => '™',
		'ℵ' => 'ℵ',
		'←' => '←',
		'↑' => '↑',
		'→' => '→',
		'↓' => '↓',
		'↔' => '↔',
		'↵' => '↵',
		'⇐' => '⇐',
		'⇑' => '⇑',
		'⇒' => '⇒',
		'⇓' => '⇓',
		'⇔' => '⇔',
		'∀' => '∀',
		'∂' => '∂',
		'∃' => '∃',
		'∅' => '∅',
		'∇' => '∇',
		'∈' => '∈',
		'∉' => '∉',
		'∋' => '∋',
		'∏' => '∏',
		'∑' => '∑',
		'−' => '−',
		'∗' => '∗',
		'√' => '√',
		'∝' => '∝',
		'∞' => '∞',
		'∠' => '∠',
		'∧' => '∧',
		'∨' => '∨',
		'∩' => '∩',
		'∪' => '∪',
		'∫' => '∫',
		'∴' => '∴',
		'∼' => '∼',
		'≅' => '≅',
		'≈' => '≈',
		'≠' => '≠',
		'≡' => '≡',
		'≤' => '≤',
		'≥' => '≥',
		'⊂' => '⊂',
		'⊃' => '⊃',
		'⊄' => '⊄',
		'⊆' => '⊆',
		'⊇' => '⊇',
		'⊕' => '⊕',
		'⊗' => '⊗',
		'⊥' => '⊥',
		'⋅' => '⋅',
		'⌈' => '⌈',
		'⌉' => '⌉',
		'⌊' => '⌊',
		'⌋' => '⌋',
		'⟨' => '〈',
		'⟩' => '〉',
		'◊' => '◊',
		'♠' => '♠',
		'♣' => '♣',
		'♥' => '♥',
		'♦' => '♦'
		);
		
	/**
	 * @desc Constructor. Just assigns the subject
	 *
	 * @param backoffice $subject
	 */
	public function __construct(backoffice $subject) {
		$this->subject = $subject;	
	}
		
	/**
	 * @desc Checks if upload was successful
	 *
	 * @return mixed : false on failure, file path on success
	 */
	protected function uploadFile() {
		if (!empty ($_FILES['addedTranslation']['error'])) {
			switch ($_FILES['addedTranslation']['error']) {
				case UPLOAD_ERR_INI_SIZE :
					throw new fileUploadExceptions(fileUploadExceptions::_UPLOAD_ERR_INI_SIZE_.' : '.$_FILES['addedTranslation']['size']);
					break;
				case UPLOAD_ERR_FORM_SIZE :
					throw new fileUploadExceptions(fileUploadExceptions::_UPLOAD_ERR_FORM_SIZE_.' : '.$_FILES['addedTranslation']['size']);
					break;
				case UPLOAD_ERR_PARTIAL :
					throw new fileUploadExceptions(fileUploadExceptions::_UPLOAD_ERR_PARTIAL_);
					break;
				case UPLOAD_ERR_NO_FILE :
					throw new fileUploadExceptions(fileUploadExceptions::_UPLOAD_ERR_NO_FILE_);
					break;
				case UPLOAD_ERR_NO_TMP_DIR :
					throw new fileUploadExceptions(fileUploadExceptions::_UPLOAD_ERR_NO_TMP_DIR_);
					break;
				case UPLOAD_ERR_CANT_WRITE :
					throw new fileUploadExceptions(fileUploadExceptions::_UPLOAD_ERR_CANT_WRITE_);
					break;
				default:
					throw new genericExceptions(genericExceptions::_UNKWOWN_);
					break;
			}
		}
		if (move_uploaded_file($_FILES['addedTranslation']['tmp_name'],'tmp/'.$_FILES['addedTranslation']['name'])) {
			chmod ('tmp/'.$_FILES['addedTranslation']['name'], 0775);
			return ('tmp/'.$_FILES['addedTranslation']['name']);
		}
		return false;
	}
	
	/**
	 * @desc : Builder. Also tries to get the alpha3 code for countries name, in case in the file, countries name are fully written
	 *
	 * @param string $sEncoding : upload encoding
	 */
	public function getTranslationFromFile($sEncoding='UTF-8') {
		if(empty($sEncoding)) {
			$sEncoding='UTF-8';
		}
		$this->sEncoding = $sEncoding;
		if(empty($_FILES)) {
			throw new fileUploadExceptions(fileUploadExceptions::_MISSING_UPLOADED_FILE);
		}
		if(false === ($sFile = $this->uploadFile())) {
			throw new genericExceptions(genericExceptions::_UNKWOWN_);
		}
		if(!function_exists('getCodes')) {
			if(!file_exists('countries/get.alpha.cn.php')) {
				throw new boException(str_replace('__FUNC__', 'getCodes', boException::_MISSING_FUNCTION_)); 
			}
			require_once 'countries/get.alpha.cn.php';
		}
		$this->parseUploadedTranslation($sFile);
	}
	
	abstract protected function parseUploadedTranslation($sFile);
}
?>
 |