diff options
Diffstat (limited to 'text.class')
-rw-r--r-- | text.class | 73 |
1 files changed, 24 insertions, 49 deletions
@@ -21,42 +21,22 @@ class it_text var $languages_available = array(); # Available languages var $language_failsafe; # First available language. Must be complete (used as fallback) var $statictext = array(); # Text array, read from php file on init - var $debug; # Non-empty if debug mode is desired - var $cookiename; # Name of the language cookie, default is "LANGUAGE" var $unknown_labels = array(); # array for spooling unknown labels in debug mode /** * Constructor - * @param $defaultlanguage Optional default language to use - * @param $tablename Optional name of text database table (default: 'it_texts') - * @param $debug Optional debug mode: display label of unknown texts, set to 'label' to always display labels instead of actual text - * @param $db Optional database object, defaults to global $it_db - * @param $cookiename Optional cookie name (default: 'LANGUAGE') + * @param $p['cookiename'] optional cookie name (default: 'LANGUAGE') + * @param $p['debug'] optional debug mode: display label of unknown texts, set to 'label' to always display labels instead of actual text + * @param $p['forcelanguage'] optional language to use instead of user's prefered language + * @param $p['phpfile'] optional texts file, defaults to service's phpinclude/texts.php */ -function it_text($defaultlanguage = 'de', $tablename = 'it_texts', $debug = false, $db = null, $cookiename = 'LANGUAGE') +function it_text($p = null) { - $this->phpfile = isset($phpfile) ? $phpfile : ($GLOBALS['ULTRAHOME'] . '/phpinclude/texts.php'); - $this->debug = $debug; - $this->cookiename = $cookiename; + $this->p = (array)$p + array('cookiename' => 'LANGUAGE', 'debug' => false, 'phpfile' => $GLOBALS['ULTRAHOME'] . '/phpinclude/texts.php'); # Read $this->statictext from php file if it's not defined yet if (!$this->statictext) - include($this->phpfile); - - # $$$ MIGRATION: Try to create texts.php from database if it doesn't exist - if (!$this->statictext) - { - $db = is_object($db) ? $db : $GLOBALS['it_db']; - $table = new it_db_table($db, $tablename); - $record = new it_db_record($table, 'Label'); - for ($record->select(); $record->fetch_next();) - { - $data = $record->data; - unset($data['Label']); - $this->statictext[$record->data['Label']] = $data; - } - $this->dump_php(); - } + include($this->p['phpfile']); # Get array of supported languages and their names foreach(array_keys($this->statictext['_']) as $code) @@ -65,6 +45,7 @@ function it_text($defaultlanguage = 'de', $tablename = 'it_texts', $debug = fals if ($languagename = $this->statictext['_'][$code]) { $this->languages_available[$code] = $languagename; + # Only use a language in browser/cookie detection below if it's not diasbled by a leading '-' if (substr($languagename, 0, 1) != '-') { @@ -101,31 +82,25 @@ function it_text($defaultlanguage = 'de', $tablename = 'it_texts', $debug = fals debug("No active languages found!", 5); } - # If no language matched, use the one that was supplied by our caller - if (!isset($this->defaultlanguage)) - $this->defaultlanguage = $defaultlanguage; - - # If language is still invalid, use failsafe language + # If no language matched, use failsafe language if (!$this->languages[$this->defaultlanguage]) - { - debug("Bad language \"{$this->defaultlanguage}\", using first entry \"{$this->languages[$this->language_failsafe]}\"", 5); $this->defaultlanguage = $this->language_failsafe; - } - # If a cookie is set, we use its value as our active language - if (isset($_COOKIE[$this->cookiename]) && ($cookie = $_COOKIE[$this->cookiename])) + # If a cookie is set, use its value as our active language + if (isset($_COOKIE[$this->p['cookiename']]) && ($this->languages[$cookie = $_COOKIE[$this->p['cookiename']]])) { - if ($this->languages[$cookie]) # Is this language available? - { - $this->actlanguage = $cookie; - debug("Got language from cookie: {$this->actlanguage}", 6); - } + $this->actlanguage = $cookie; + debug("Got language from cookie: {$this->actlanguage}", 6); } # If the cookie supplied no valid language, we use our calculated default language if (!isset($this->actlanguage)) $this->actlanguage = $this->defaultlanguage; + # If the user wants something else, comply + if (isset($this->p['forcelanguge']) && isset($this->languages[$this->p['forcelanguage']])) + $this->actlanguage = $this->p['forcelanguage']; + # And finally, record the name of our active language. $this->actlanguagename = $this->languages[$this->actlanguage]; @@ -142,7 +117,7 @@ function it_text($defaultlanguage = 'de', $tablename = 'it_texts', $debug = fals */ function text($label, $raw = null, $language = null) { - if ($this->debug === 'label') + if ($this->p['debug'] === 'label') return $label; if (!isset($language)) @@ -152,7 +127,7 @@ function text($label, $raw = null, $language = null) return $this->statictext[$label][$language]; $this->unknown_labels[] = $label; - return $this->debug ? "<blink>$label ($language)</blink>" : $this->statictext[$label][$this->language_failsafe]; + return $this->p['debug'] ? "<blink>$label ($language)</blink>" : $this->statictext[$label][$this->language_failsafe]; } @@ -178,20 +153,20 @@ function etext($label, $values = null, $language = null) */ function set_language($language, $setcookie = true) { - /* If set to an invalid language (via forged GET?) we delete the cookie */ + # If $language is unknown, delete the cookie if ($this->languages_available[$language]) { if (!$this->languages[$language]) debug('Selected an existing but currently disabled language!', 6); $this->actlanguage = $language; if ($setcookie) - SetCookie($this->cookiename, $this->actlanguage, time() + 31536000, '/'); # 1 year + SetCookie($this->p['cookiename'], $this->actlanguage, time() + 31536000, '/'); # 1 year } else { $this->actlanguage = $this->defaultlanguage; if ($setcookie) - SetCookie($this->cookiename, '', time() + 31536000, '/'); + SetCookie($this->p['cookiename'], '', time() + 31536000, '/'); } } @@ -345,9 +320,9 @@ function dump_php() $this->statictext = array_merge(array('_' => $text['_']), $text); $oldmask = umask(002); - if ($f = fopen($this->phpfile, 'w')) + if ($f = fopen($this->p['phpfile'], 'w')) { - $result = (fputs($f, '<?php $this->statictext = ' . strtr(var_export($this->statictext, TRUE), array("=> \n array (" => "=> array(", "array (\n '_'" => "array(\n'_'", "\n ),\n " => "\n),\n", "\n ),\n" => "\n)\n")) . ";\n?>\n") !== false); + $result = (fputs($f, '<?php $this->statictext = ' . strtr(var_export($this->statictext, true), array("=> \n array (" => "=> array(", "array (\n '_'" => "array(\n'_'", "\n ),\n " => "\n),\n", "\n ),\n" => "\n)\n")) . ";\n?>\n") !== false); fclose($f); } |