File: BaseLanguage.class

package info (click to toggle)
gforge 4.5.14-22etch13
  • links: PTS
  • area: main
  • in suites: etch
  • size: 13,004 kB
  • ctags: 11,918
  • sloc: php: 36,047; sql: 29,050; sh: 10,538; perl: 6,496; xml: 3,810; makefile: 341; python: 263; ansic: 256
file content (480 lines) | stat: -rw-r--r-- 13,326 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<?php

/**
 * GForge Localization Facility
 *
 * Portions Copyright 1999-2000 (c) VA Linux Systems
 * The rest Copyright 2003-2004 (c) Guillaume Smet
 *
 * http://gforge.org
 *
 * @version $Id: BaseLanguage.class 4321 2005-04-29 19:05:12Z marcelo $
 */

/*

	Tim Perdue, September 7, 2000

	Base class for adding multilingual support to SF.net

	Contains variables which can be overridden optionally by other
	language files.

	Base language is english - an english class will extend this one,
	but won't override anything

	As new languages are added, they can override what they wish, and
		as we extend our class, other languages can follow suit
		as they are translated without holding up our progress
*/

class BaseLanguage {

	/**
	 * associative array to hold the string value
	 *
	 * @var array $textArray
	 */
	var $textArray ;
	
	/**
	 * selected language
	 *
	 * @var string $lang
	 */
	var $lang;
	
	/**
	 * name of the current language
	 *
	 * @var string $name
	 */
	var $name;
	
	/**
	 * language id
	 *
	 * @var int $id
	 */
	var $id;
	
	/**
	 * language code
	 *
	 * @var string $code
	 */
	var $code;
	
	/**
	 * result set handle for supported languages
	 *
	 * @var resource $languagesRes
	 */
	var $languagesRes;
	
	/**
	 * array containing dependencies of the cache file
	 *
	 * @var array $cacheDependencies
	 */
	var $cacheDependencies = array();

	/**
	 * array containing the plugins which are loaded
	 *
	 * @var array $pluginDependencies
	 */
	var $pluginDependencies = array();

	/**
	 * Constructor
	 */
	function BaseLanguage() {
		// disable localization caching system if configuration is wrong
		if(!isset($GLOBALS['sys_localization_cache_path']) || !is_writable($GLOBALS['sys_localization_cache_path'])) {
			$GLOBALS['sys_localization_enable_caching'] = false;
		}
	}

	/**
	 * loadLanguageFile - load the localized strings from a file
	 *
	 * @param $fname file name
	 */
	function loadLanguageFile($fname) {
		
		$lines = file($fname, 1);

		$this->cacheDependencies[] = $fname;
		
		for( $i=0, $max = sizeof($lines); $i < $max; $i++) {
			$currentLine = $lines[$i];
			if (substr($currentLine, 0, 1) == '#') {
				continue;
			}
			// Language files can include others for defaults.
			// e.g. an English-Canada.tab file might "include English" first,
			// then override all those whacky American spellings.
			if (preg_match("/^include ([a-zA-Z]+)/", $currentLine, $matches)) {
				$dir = dirname($fname);
				$fileName = $dir.'/'.$matches[1].'.tab';
				if(file_exists($fileName)) {
					$this->loadLanguageFile($fileName);
				}
			} else {
				$line = explode("\t", $currentLine, 3);

				if($line && count($line) == 3) {
					$this->textArray[$line[0]][$line[1]] = chop($line[2]);
				}
			}
		}
	}

	/**
	 * loadLanguageID - load the selected language
	 *
	 * @param int $languageId language id
	 */
	function loadLanguageID($languageId) {
		$res = db_query('SELECT classname FROM supported_languages WHERE language_id=\''.$languageId.'\'');
		$this->loadLanguage(db_result($res, 0, 'classname'));
	}

	/**
	 * loadLanguage - load localized strings of the selected language
	 *
	 * @param string $lang language name
	 */	
	function loadLanguage($lang) {
		$cachePath = $this->getLocalizationCachePath($lang);

		if (function_exists('plugin_manager_get_object')) {
			$pluginManager =& plugin_manager_get_object();
			$pluginNames =& $pluginManager->getPlugins();
		} else {
			$pluginNames = array();
		}
		if($GLOBALS['sys_localization_enable_caching'] && file_exists($cachePath)) {
			$this->textArray =& $this->getLocalizationCache($cachePath);
			$this->lang = $lang ;
			if(!$GLOBALS['sys_localization_enable_timestamp_checking'] || !$this->isCacheOutdated($cachePath, $pluginNames)) {
				return;
			}
		}
		$this->dependencies = array();
		if($lang != 'Base') {
			$this->loadGlobalLanguage('Base');
		}
		$this->loadGlobalLanguage($lang);

		$this->pluginDependencies = array();
		if ( $lang != 'Base' ) {
			$this->loadPluginSpecificLanguage('Base', $pluginNames);
		}
		$this->loadPluginSpecificLanguage($lang, $pluginNames);

		$this->lang = $lang ;
		if($GLOBALS['sys_localization_enable_caching']) {
			$this->writeLocalizationCache($cachePath);
		}
	}

	/**
	 * loadGlobalLanguage - load localized strings of the selected language for the global site
	 *
	 * @param string $lang language name
	 */
	function loadGlobalLanguage($lang) {
		global $sys_theme, $sys_urlroot;

		// Customization by language in $sys_urlroot/include/languages/<Language>.tab
		$fname = $sys_urlroot.'/include/languages/'.$lang.'.tab';
		$this->loadLanguageFile($fname) ;
		// Customization by theme by language in $sys_urlroot/themes/<theme_name>/<Language>.tab
		$ftname = $sys_urlroot.'/themes/'.$sys_theme.'/'.$lang.'.tab' ;
		if (file_exists ($ftname)) {
			$this->loadLanguageFile($ftname) ;
		}
		// Site-local customizations in /etc/gforge/languages-local/<Language>.tab
		$fname = '/etc/gforge/languages-local/'.$lang.'.tab' ;
		if (file_exists ($fname)) {
			$this->loadLanguageFile($fname) ;
		}
		// Site-local Customization by theme in /etc/gforge/languages-local/<theme_name>/<Language>.tab
		$fltname = '/etc/gforge/languages-local/'.$sys_theme.'/'.$lang.'.tab' ;
		if (file_exists ($fltname)) {
			$this->loadLanguageFile($fltname) ;
		}
	}

	/**
	 * loadPluginSpecificLanguage - load localized strings of the selected language for installed plugins
	 *
	 * @param string $lang language name
	 */
	function loadPluginSpecificLanguage($lang, $pluginNames) {
		if (is_array($pluginNames)) {
			foreach($pluginNames AS $pluginName) {
				$plugin =& plugin_get_object($pluginName);
				if($plugin && !$plugin->isError()) {
					$languagePath = $plugin->GetLanguagePath().$lang.'.tab';
					$specificLanguagePath = $plugin->GetSpecificLanguagePath().$lang.'.tab';
					if(file_exists($languagePath)) {
						$this->loadLanguageFile($languagePath);
					}
					if(file_exists($specificLanguagePath)) {
						$this->loadLanguageFile($specificLanguagePath);
					}
					$this->pluginDependencies[] = $pluginName;
				}
			}
		}
	}

	/**
	 * getText - get a localized string
	 *
	 * @param string $pagename name of the current page
	 * @param string $category key
	 * @param mixed $args array which will replace the $1, $2, etc before it is returned
	 */
	function getText($pagename, $category, $args = '') {
		if ($args) {
			for ($i=1, $max = sizeof($args)+1; $i <= $max; $i++) {
				$patterns[] = '/\$'.$i.'/';
			}
			$tstring = preg_replace($patterns, $args, $this->textArray[$pagename][$category]);
		} else {
			$tstring = $this->textArray[$pagename][$category];
		}
		return $tstring;
	}

	/**
	 * getLanguages - returns database result of supported languages
	 *
	 * @return resource supported languages
	 */
	function getLanguages() {
		if (!isset($this->languagesRes)) {
			$this->languagesRes = db_query('SELECT * FROM supported_languages ORDER BY name ASC');
		}
		return $this->languagesRes;
	}

	/**
	 * getLanguageId - returns the language id corresponding to the language name
	 *
	 * @return int language id
	 */
	function getLanguageId() {
		if (!$this->id) {
			$this->id = db_result(db_query("SELECT language_id FROM supported_languages WHERE classname='".$this->lang."'"), 0, 0) ;
		}
		return $this->id ;
	}

	/**
	 * getLanguageName - returns the language name corresponding to the language id
	 *
	 * @return string language name
	 */
	function getLanguageName() {
		if (!$this->name) {
			$id = $this->getLanguageId () ;
			$this->name = db_result(db_query("SELECT name FROM supported_languages WHERE language_id='$id'"), 0, 0) ;
		}
		return $this->name ;
	}

	/**
	 * getLanguageCode - returns the language code corresponding to the language id
	 *
	 * @return string language code
	 */
	function getLanguageCode() {
		if (!$this->code) {
			$id = $this->getLanguageId () ;
			$this->code = db_result(db_query("SELECT language_code FROM supported_languages WHERE language_id='$id'"), 0, 0) ;
		}
		return $this->code ;
	}

	/**
	 * getEncoding - returns the content encoding for the selected language
	 *
	 * @return string content encoding
	 */
	function getEncoding() {
		if(isset($this->textArray['conf']['content_encoding'])) {
			return $this->textArray['conf']['content_encoding'];
		}
		else {
			return '';
		}
	}

	/**
	 * getFont - returns the default font for selected language if exists
	 *
	 * @return string the default font
	 */
	function getFont() {
		if(isset($this->textArray['conf']['default_font'])) {
			return $this->textArray['conf']['default_font'];
		}
		else {
			return '';
		}
	}


	/* Localization Caching System */
	
	/**
	 * getLocalizationCache - get the localization strings array from cache
	 *
	 * @param string $path path of the cache file
	 * @return array an array containing localization string
	 */
	function & getLocalizationCache($cachePath) {
		$fp = fopen($cachePath, 'r');
		flock($fp, LOCK_SH);
		$array = unserialize(fread($fp, filesize($cachePath)));
		fclose($fp);
		$this->cacheDependencies =& $array['dependencies'];
		if(isset($array['pluginDependencies'])) {
			$this->pluginDependencies =& $array['pluginDependencies'];
		} else {
			$this->pluginDependencies = array();
		}
		return $array['text'];
	}

	/**
	 * writeLocalizationCache - caches the localization array to filesystem
	 *
	 * @param string $path path of the cache file
	 */
	function writeLocalizationCache($cachePath) {
		if(posix_getuid() != 0) {
			$content = array();
			$content['dependencies'] =& $this->cacheDependencies;
			$content['pluginDependencies'] =& $this->pluginDependencies;
			$content['text'] =& $this->textArray;
			$fp = fopen($cachePath, 'a');
			flock($fp, LOCK_EX);
			ftruncate($fp, 0);
			$content = serialize($content);
			fwrite($fp, $content, strlen($content));
			fclose($fp);
		}
	}

	/**
	 * getLocalizationCachePath - get the path to the localization cache for the selected language
	 *
	 * @param string $lang language
	 * @return string path to the cache file
	 */
	function getLocalizationCachePath($lang) {
		return $GLOBALS['sys_localization_cache_path'].$lang.'.cache';
	}

	/**
	 * isCacheOutdated - test if the localization cache is deprecated
	 *
	 * @param string $path path of the cache file
	 * @param array $pluginNames list of installed plugins
	 * @return boolean true if the cache is deprecated, false if it's still valid
	 */
	function isCacheOutdated($cachePath, $pluginNames) {
		if(count(array_diff($pluginNames, $this->pluginDependencies)) > 0) {
			return true;
		}
		if(!empty($this->cacheDependencies)) {
			$cacheTimestamp = filemtime($cachePath);
			$cacheDependencies =& $this->cacheDependencies;
			for($i = 0, $max = sizeof($cacheDependencies); $i < $max; $i++) {
				if(file_exists($cacheDependencies[$i]) && $cacheTimestamp < filemtime($cacheDependencies[$i])) {
					return true;
				}
			}
		}
		return false;
	}

}

/**
 * getLanguageClassName - get the classname for a language id
 * 
 * @param string $acceptedLanguages HTTP_ACCEPT_LANGUAGE header string
 * @return string the language class name.
 */
function getLanguageClassName($acceptedLanguages) {
	global $cookie_language_id;

	/*
		Determine which language file to use

		It depends on whether the user has set a cookie or not using
		the account page or the left-hand nav or how their browser is
		set or whether they are logged in or not

		if logged in, use language from users table
		else check for cookie and use that value if valid
		if no cookie check browser preference and use that language if valid
		else just use default language as configured for the installation
	*/

	if ($cookie_language_id) {
		$lang=$cookie_language_id;
		$res=db_query("select classname from supported_languages where language_id='".addslashes($lang)."'");
		if (!$res || db_numrows($res) < 1) {
			return false; // we will use default language
		} else {
			return db_result($res,0,'classname');
		}
	} else {
		$matches = array();
		preg_match_all('/([a-z]{2}(?:-[a-z]{2})?)(?:;q=([0-9\.]{1,4}))?/', $acceptedLanguages, $matches, PREG_SET_ORDER);
		$languages = array();
		
		$languagesCount = count($matches);
		
		if($languagesCount > 0) {
			$delta = 0.009/$languagesCount;
		
			for($i = 0, $max = count($matches); $i < $max; $i++) {
				$languageCode = $matches[$i][1];
				$quality = (!isset($matches[$i][2]) || empty($matches[$i][2])) ? '1' : $matches[$i][2];
				$languages[$languageCode] = $quality + $delta * ($languagesCount - $i);
			}

			arsort($languages, SORT_NUMERIC);
			$languages = array_keys($languages);

			for( $i=0, $max = sizeof($languages); $i < $max; $i++){
				$languageCode = $languages[$i];
				$res = db_query("select classname from supported_languages where language_code = '".addslashes($languageCode)."'");
				if (db_numrows($res) > 0) {
					return db_result($res,0,'classname');
				}
				// If that didn't work, check if we have sublanguage specifier
				// If so, try to strip it and look for for main language only
				if (strstr($languageCode, '-')) {
					$languageCode = substr($languageCode, 0, 2);
					$res = db_query("select classname from supported_languages where language_code = '".addslashes($languageCode)."'");
					if (db_numrows($res) > 0) {
						return db_result($res,0,'classname');
					}
				}
			}
		}
		return false; // we will use default language
	}
}

?>