Project

General

Profile

1
<?php
2
/**
3
 * @category     Core
4
 * @package      Core_security
5
 * @author       Werner v.d.Decken
6
 * @copyright    ISTeasy-project(http://isteasy.de/)
7
 * @license      Creative Commons BY-SA 3.0 http://creativecommons.org/licenses/by-sa/3.0/
8
 * @version      $Id: ModLanguage.php 1680 2012-05-02 22:17:37Z darkviper $
9
 * @filesource   $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/ModLanguage.php $
10
 * @since        Datei vorhanden seit Release 2.8.2
11
 * @lastmodified $Date: 2012-05-03 00:17:37 +0200 (Thu, 03 May 2012) $
12
 */
13
class ModLanguage {
14

    
15
	private $_sCurrentLanguage   = '';
16
	private $_sDefaultLanguage   = '';
17
	private $_sLanguageDirectory = '';
18
	private $_sLanguageFile      = '';
19
	private $_LanguageTable      = array();
20
	private $_bLoaded            = false;
21

    
22
	private static $_oInstance   = null;
23
/* prevent from public instancing */
24
	protected function  __construct() { }
25
/* prevent from cloning */
26
	private function __clone() {}
27
/**
28
 * get a valid instance of this class
29
 * @return object
30
 */
31
	public function getInstance() {
32
		if( is_null(self::$_oInstance) ) {
33
            $c = __CLASS__;
34
            self::$_oInstance = new $c;
35
		}
36
		return self::$_oInstance;
37
	}
38
/**
39
 * set language and load needed language file
40
 * @param string $sDirectory full path to the language files
41
 * @param string $sLanguage 2-letters language code
42
 * @param string $sDefault 2-letters default-language code
43
 */
44
	public function setLanguage($sDirectory, $sLanguage, $sDefault = 'EN')
45
	{
46
		$sBasePath = realpath(dirname(dirname(__FILE__)));
47
		$sLangDir = realpath($sDirectory);
48
		if(!preg_match('/^'.preg_quote($sBasePath, '/').'/', $sLangDir)) {
49
			throw new SecDirectoryTraversalException();
50
		}
51
		$sLangDir = str_replace('\\', '/', $sLangDir);
52
		$sLangDir = rtrim($sLangDir, '/').'/';
53
		$sLanguage = strtoupper($sLanguage);
54
		$sLanguage = strtoupper($sDefault);
55
		if($this->_sLanguageDirectory != $sLangDir ||
56
		   $this->_sCurrentLanguage != $sLanguage ||
57
		   $this->_sDefaultLanguage != $sDefault)
58
		{
59
			$this->_sLanguageDirectory = rtrim($sLangDir, '/').'/';
60
			$this->_sCurrentLanguage = $sLanguage;
61
			$this->_sDefaultLanguage = $sDefault;
62

    
63
			if(!$this->_findLanguageFile()) {
64
				$msg  = 'unable to find valid language definition file in<br />';
65
				$msg .= '"'.str_replace($sBasePath, '', $this->_sLanguageDirectory).'"';
66
				throw new TranslationException($msg);
67
			}
68
			$this->_importArrays();
69
		}
70
		$this->_bLoaded = (sizeof($this->_LanguageTable) > 0);
71
	}
72
/**
73
 * return requested translation for a key
74
 * @param string $sLanguageKey 2-uppercase letters language code
75
 * @return string found translation or empty string 
76
 */
77
	public function __get($sLanguageKey)
78
	{
79
		$sRetval = (isset($this->_LanguageTable[$sLanguageKey])
80
		            ? $this->_LanguageTable[$sLanguageKey] : '{missing: '.$sLanguageKey.'}');
81
		return $sRetval;
82
	}
83
/**
84
 * returns the whoole language array for use in templateengine
85
 * @return array
86
 */
87
	public function getLangArray()
88
	{
89
		return $this->_LanguageTable;
90
	}
91
/**
92
 * search language file in order: LANGUAGE - DEFAULT_LANGUAGE - FIRST_FOUND
93
 * @return boolean
94
 */
95
	private function _findLanguageFile()
96
	{
97
		$bMatch = false;
98
		$dir = $this->_sLanguageDirectory;
99
		if(is_readable($dir.$this->_sCurrentLanguage.'.php')) {
100
		// check actual language
101
			$this->_sLanguageFile = $dir.$this->_sCurrentLanguage.'.php';
102
			$bMatch = true;
103
		}else {
104
			if(is_readable($dir.$this->_sDefaultLanguage.'.php')) {
105
			// check default language
106
				$this->_sLanguageFile = $dir.$this->_sDefaultLanguage.'.php';
107
				$bMatch = true;
108
			}else {
109
			// search for first available and readable language file
110
				if(is_readable($dir)) {
111
					$iterator = new DirectoryIterator($dir);
112
					foreach ($iterator as $fileinfo) {
113
						if(!preg_match('/^[A-Z]{2}\.php$/', $fileinfo->getBasename())) { continue; }
114
						$sLanguageFile = str_replace('\\', '/', $fileinfo->getPathname());
115
						if(is_readable($sLanguageFile)) {
116
							$this->_sLanguageFile = $sLanguageFile;
117
							$bMatch = true;
118
							break;
119
						}
120
					}
121
				}
122
			}
123
		}
124
		return $bMatch;
125
	}
126
/**
127
 * import key-values from language file
128
 */
129
	private function _importArrays()
130
	{
131
		include($this->_sLanguageFile);
132
		$aLangSections = array('HEADING', 'TEXT', 'MESSAGE', 'MENU', 'OVERVIEW', 'GENERIC');
133
		foreach($aLangSections as $sSection) {
134
			if(isset(${$sSection}) && is_array(${$sSection})) {
135
				foreach(${$sSection} as $key => $value) {
136
					$this->_LanguageTable[$sSection.'_'.$key] = $value;
137
				}
138
			}
139
		}
140
	}
141
} // end class Translate
142
/**
143
 *  Exception class for Translation
144
 */
145
class TranslationException extends AppException {}
146

    
(3-3/23)