205
+ − 1
<?php
+ − 2
+ − 3
/*
+ − 4
* Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
+ − 5
* Version 1.1.1
+ − 6
* Copyright (C) 2006-2007 Dan Fuhry
+ − 7
*
+ − 8
* This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
+ − 9
* as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
+ − 10
*
+ − 11
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
+ − 12
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
+ − 13
*/
+ − 14
+ − 15
/**
+ − 16
* Language class - processes, stores, and retrieves language strings.
+ − 17
* @package Enano
+ − 18
* @subpackage Localization
+ − 19
* @copyright 2007 Dan Fuhry
+ − 20
* @license GNU General Public License
+ − 21
*/
+ − 22
+ − 23
class Language
+ − 24
{
+ − 25
+ − 26
/**
+ − 27
* The numerical ID of the loaded language.
+ − 28
* @var int
+ − 29
*/
+ − 30
+ − 31
var $lang_id;
+ − 32
+ − 33
/**
+ − 34
* The ISO-639-3 code for the loaded language. This should be grabbed directly from the database.
+ − 35
* @var string
+ − 36
*/
+ − 37
+ − 38
var $lang_code;
210
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 39
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 40
/**
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 41
* Used to track when a language was last changed, to allow browsers to cache language data
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 42
* @var int
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 43
*/
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 44
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 45
var $lang_timestamp;
205
+ − 46
+ − 47
/**
+ − 48
* Will be an object that holds an instance of the class configured with the site's default language. Only instanciated when needed.
+ − 49
* @var object
+ − 50
*/
+ − 51
+ − 52
var $default;
+ − 53
+ − 54
/**
+ − 55
* The list of loaded strings.
+ − 56
* @var array
+ − 57
* @access private
+ − 58
*/
+ − 59
+ − 60
var $strings = array();
+ − 61
+ − 62
/**
504
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 63
* Switch for debug mode. If true, will show an asterisk after localized strings. This
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 64
* can be useful if you're localizing a component and need to see what's already done.
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 65
* @var bool
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 66
*/
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 67
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 68
var $debug = false;
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 69
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 70
/**
205
+ − 71
* Constructor.
+ − 72
* @param int|string Language ID or code to load.
+ − 73
*/
+ − 74
+ − 75
function __construct($lang)
+ − 76
{
+ − 77
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 78
+ − 79
if ( defined('IN_ENANO_INSTALL') )
+ − 80
{
+ − 81
// special case for the Enano installer: it will load its own strings from a JSON file and just use this API for fetching and templatizing them.
243
+ − 82
$this->lang_id = 1;
+ − 83
$this->lang_code = $lang;
205
+ − 84
return true;
+ − 85
}
+ − 86
if ( is_string($lang) )
+ − 87
{
371
dc6026376919
Improved compatibility with PostgreSQL and fixed a number of installer bugs; fixed missing "meta" category declaration in language files
Dan
diff
changeset
+ − 88
$sql_col = 'lang_code=\'' . $db->escape($lang) . '\'';
205
+ − 89
}
+ − 90
else if ( is_int($lang) )
+ − 91
{
+ − 92
$sql_col = 'lang_id=' . $lang . '';
+ − 93
}
+ − 94
else
+ − 95
{
+ − 96
$db->_die('lang.php - attempting to pass invalid value to constructor');
+ − 97
}
+ − 98
239
0f1b353570a7
Fix a comparison logic SQL error in lang.php; fix attempt to call mysql_real_escape_string() in install without a working DB connection
Dan
diff
changeset
+ − 99
$lang_default = ( $x = getConfig('default_language') ) ? intval($x) : '\'def\'';
372
5bd429428101
A number of scattered changes. Profiler added and only enabled in debug mode (currently on), but awfully useful for fixing performance in the future. Started work on Admin:LangManager
Dan
diff
changeset
+ − 100
5bd429428101
A number of scattered changes. Profiler added and only enabled in debug mode (currently on), but awfully useful for fixing performance in the future. Started work on Admin:LangManager
Dan
diff
changeset
+ − 101
$q = $db->sql_query("SELECT lang_id, lang_code, last_changed, ( lang_id = $lang_default ) AS is_default FROM " . table_prefix . "language WHERE $sql_col OR lang_id = $lang_default ORDER BY is_default ASC LIMIT 1;");
205
+ − 102
+ − 103
if ( !$q )
+ − 104
$db->_die('lang.php - main select query');
+ − 105
+ − 106
if ( $db->numrows() < 1 )
+ − 107
$db->_die('lang.php - There are no languages installed');
+ − 108
+ − 109
$row = $db->fetchrow();
+ − 110
+ − 111
$this->lang_id = intval( $row['lang_id'] );
+ − 112
$this->lang_code = $row['lang_code'];
210
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 113
$this->lang_timestamp = $row['last_changed'];
205
+ − 114
}
+ − 115
+ − 116
/**
+ − 117
* Fetches language strings from the database, or a cache file if it's available.
+ − 118
* @param bool If true (default), allows the cache to be used.
+ − 119
*/
+ − 120
+ − 121
function fetch($allow_cache = true)
+ − 122
{
+ − 123
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 124
+ − 125
$lang_file = ENANO_ROOT . "/cache/lang_{$this->lang_id}.php";
+ − 126
// Attempt to load the strings from a cache file
+ − 127
if ( file_exists($lang_file) && $allow_cache )
+ − 128
{
+ − 129
// Yay! found it
+ − 130
$this->load_cache_file($lang_file);
+ − 131
}
+ − 132
else
+ − 133
{
+ − 134
// No cache file - select and retrieve from the database
+ − 135
$q = $db->sql_unbuffered_query("SELECT string_category, string_name, string_content FROM " . table_prefix . "language_strings WHERE lang_id = {$this->lang_id};");
+ − 136
if ( !$q )
+ − 137
$db->_die('lang.php - selecting language string data');
+ − 138
if ( $row = $db->fetchrow() )
+ − 139
{
+ − 140
$strings = array();
+ − 141
do
+ − 142
{
+ − 143
$cat =& $row['string_category'];
371
dc6026376919
Improved compatibility with PostgreSQL and fixed a number of installer bugs; fixed missing "meta" category declaration in language files
Dan
diff
changeset
+ − 144
if ( !is_array(@$strings[$cat]) )
205
+ − 145
{
+ − 146
$strings[$cat] = array();
+ − 147
}
+ − 148
$strings[$cat][ $row['string_name'] ] = $row['string_content'];
+ − 149
}
+ − 150
while ( $row = $db->fetchrow() );
+ − 151
// all done fetching
+ − 152
$this->merge($strings);
+ − 153
}
+ − 154
else
+ − 155
{
241
c671f3bb8aed
Trying to get lang import to work in the installer; it's not working ATM - cache file is generated with lang_id = 0. Syncing to Nighthawk.
Dan
diff
changeset
+ − 156
if ( !defined('ENANO_ALLOW_LOAD_NOLANG') )
c671f3bb8aed
Trying to get lang import to work in the installer; it's not working ATM - cache file is generated with lang_id = 0. Syncing to Nighthawk.
Dan
diff
changeset
+ − 157
$db->_die('lang.php - No strings for language ' . $this->lang_code);
205
+ − 158
}
+ − 159
}
+ − 160
}
+ − 161
+ − 162
/**
+ − 163
* Loads a file from the disk cache (treated as PHP) and merges it into RAM.
+ − 164
* @param string File to load
+ − 165
*/
+ − 166
+ − 167
function load_cache_file($file)
+ − 168
{
+ − 169
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 170
+ − 171
// We're using eval() here because it makes handling scope easier.
+ − 172
+ − 173
if ( !file_exists($file) )
+ − 174
$db->_die('lang.php - requested cache file doesn\'t exist');
+ − 175
+ − 176
$contents = file_get_contents($file);
+ − 177
$contents = preg_replace('/([\s]*)<\?php/', '', $contents);
+ − 178
+ − 179
@eval($contents);
+ − 180
+ − 181
if ( !isset($lang_cache) || ( isset($lang_cache) && !is_array($lang_cache) ) )
+ − 182
$db->_die('lang.php - the cache file is invalid (didn\'t set $lang_cache as an array)');
+ − 183
+ − 184
$this->merge($lang_cache);
+ − 185
}
+ − 186
+ − 187
/**
243
+ − 188
* Loads a JSON language file and parses the strings into RAM. Will use the cache if possible, but stays far away from the database,
+ − 189
* which we assume doesn't exist yet.
+ − 190
*/
+ − 191
+ − 192
function load_file($file)
+ − 193
{
+ − 194
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 195
+ − 196
if ( !file_exists($file) )
348
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 197
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 198
if ( defined('IN_ENANO_INSTALL') )
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 199
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 200
die('lang.php - requested JSON file (' . htmlspecialchars($file) . ') doesn\'t exist');
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 201
}
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 202
else
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 203
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 204
$db->_die('lang.php - requested JSON file doesn\'t exist');
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 205
}
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 206
}
243
+ − 207
+ − 208
$contents = trim(@file_get_contents($file));
+ − 209
if ( empty($contents) )
+ − 210
$db->_die('lang.php - empty language file...');
+ − 211
+ − 212
// Trim off all text before and after the starting and ending braces
+ − 213
$contents = preg_replace('/^([^{]+)\{/', '{', $contents);
+ − 214
$contents = preg_replace('/\}([^}]+)$/', '}', $contents);
+ − 215
$contents = trim($contents);
+ − 216
+ − 217
if ( empty($contents) )
+ − 218
$db->_die('lang.php - no meat to the language file...');
+ − 219
+ − 220
$checksum = md5($contents);
+ − 221
if ( file_exists("./cache/lang_json_{$checksum}.php") )
+ − 222
{
+ − 223
$this->load_cache_file("./cache/lang_json_{$checksum}.php");
+ − 224
}
+ − 225
else
+ − 226
{
348
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 227
// Correct syntax to be nice to the json parser
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 228
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 229
// eliminate comments
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 230
$contents = preg_replace(array(
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 231
// eliminate single line comments in '// ...' form
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 232
'#^\s*//(.+)$#m',
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 233
// eliminate multi-line comments in '/* ... */' form, at start of string
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 234
'#^\s*/\*(.+)\*/#Us',
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 235
// eliminate multi-line comments in '/* ... */' form, at end of string
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 236
'#/\*(.+)\*/\s*$#Us'
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 237
), '', $contents);
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 238
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 239
$contents = preg_replace('/([,\{\[])([\s]*?)([a-z0-9_]+)([\s]*?):/', '\\1\\2"\\3" :', $contents);
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 240
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 241
try
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 242
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 243
$langdata = enano_json_decode($contents);
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 244
}
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 245
catch(Zend_Json_Exception $e)
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 246
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 247
$db->_die('lang.php - Exception caught by JSON parser</p><pre>' . htmlspecialchars(print_r($e, true)) . '</pre><p>');
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 248
exit;
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 249
}
243
+ − 250
+ − 251
if ( !is_array($langdata) )
+ − 252
$db->_die('lang.php - invalid language file');
+ − 253
+ − 254
if ( !isset($langdata['categories']) || !isset($langdata['strings']) )
+ − 255
$db->_die('lang.php - language file does not contain the proper items');
+ − 256
+ − 257
$this->merge($langdata['strings']);
+ − 258
+ − 259
$lang_file = "./cache/lang_json_{$checksum}.php";
+ − 260
+ − 261
$handle = @fopen($lang_file, 'w');
+ − 262
if ( !$handle )
+ − 263
// Couldn't open the file. Silently fail and let the strings come from RAM.
+ − 264
return false;
+ − 265
+ − 266
// The file's open, that means we should be good.
+ − 267
fwrite($handle, '<?php
+ − 268
// This file was generated automatically by Enano. You should not edit this file because any changes you make
+ − 269
// to it will not be visible in the ACP and all changes will be lost upon any changes to strings in the admin panel.
+ − 270
+ − 271
$lang_cache = ');
+ − 272
+ − 273
$exported = $this->var_export_string($this->strings);
+ − 274
if ( empty($exported) )
+ − 275
// Ehh, that's not good
+ − 276
$db->_die('lang.php - load_file(): var_export_string() failed');
+ − 277
+ − 278
fwrite($handle, $exported . '; ?>');
+ − 279
+ − 280
// Clean up
+ − 281
unset($exported, $langdata);
+ − 282
+ − 283
// Done =)
+ − 284
fclose($handle);
+ − 285
}
+ − 286
}
+ − 287
+ − 288
/**
205
+ − 289
* Merges a standard language assoc array ($arr[cat][stringid]) with the master in RAM.
+ − 290
* @param array
+ − 291
*/
+ − 292
+ − 293
function merge($strings)
+ − 294
{
+ − 295
// This is stupidly simple.
+ − 296
foreach ( $strings as $cat_id => $contents )
+ − 297
{
243
+ − 298
if ( !isset($this->strings[$cat_id]) || ( isset($this->strings[$cat_id]) && !is_array($this->strings[$cat_id]) ) )
205
+ − 299
$this->strings[$cat_id] = array();
+ − 300
foreach ( $contents as $string_id => $string )
+ − 301
{
+ − 302
$this->strings[$cat_id][$string_id] = $string;
+ − 303
}
+ − 304
}
+ − 305
}
+ − 306
+ − 307
/**
+ − 308
* Imports a JSON-format language file into the database and merges with current strings.
+ − 309
* @param string Path to the JSON file to load
514
+ − 310
* @param bool Enable debugging output, makes the process over CLI more interesting
205
+ − 311
*/
+ − 312
514
+ − 313
function import($file, $debug = false)
205
+ − 314
{
+ − 315
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 316
+ − 317
if ( !file_exists($file) )
+ − 318
$db->_die('lang.php - can\'t import language file: string file doesn\'t exist');
+ − 319
241
c671f3bb8aed
Trying to get lang import to work in the installer; it's not working ATM - cache file is generated with lang_id = 0. Syncing to Nighthawk.
Dan
diff
changeset
+ − 320
if ( $this->lang_id == 0 )
c671f3bb8aed
Trying to get lang import to work in the installer; it's not working ATM - cache file is generated with lang_id = 0. Syncing to Nighthawk.
Dan
diff
changeset
+ − 321
$db->_die('lang.php - BUG: trying to perform import when $lang->lang_id == 0');
c671f3bb8aed
Trying to get lang import to work in the installer; it's not working ATM - cache file is generated with lang_id = 0. Syncing to Nighthawk.
Dan
diff
changeset
+ − 322
514
+ − 323
if ( $debug )
516
+ − 324
$br = ( isset($_SERVER['REQUEST_URI']) ) ? '<br />' : '';
+ − 325
+ − 326
if ( $debug )
+ − 327
echo "Importing file: $file$br\n Checking file...$br\n";
514
+ − 328
205
+ − 329
$contents = trim(@file_get_contents($file));
+ − 330
+ − 331
if ( empty($contents) )
+ − 332
$db->_die('lang.php - can\'t load the contents of the language file');
+ − 333
514
+ − 334
if ( $debug )
516
+ − 335
echo " Cleaning up JSON$br\n";
514
+ − 336
205
+ − 337
// Trim off all text before and after the starting and ending braces
+ − 338
$contents = preg_replace('/^([^{]+)\{/', '{', $contents);
+ − 339
$contents = preg_replace('/\}([^}]+)$/', '}', $contents);
+ − 340
334
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 341
// Correct syntax to be nice to the json parser
372
5bd429428101
A number of scattered changes. Profiler added and only enabled in debug mode (currently on), but awfully useful for fixing performance in the future. Started work on Admin:LangManager
Dan
diff
changeset
+ − 342
$contents = enano_clean_json($contents);
334
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 343
514
+ − 344
if ( $debug )
516
+ − 345
echo " Decoding JSON stream$br\n";
514
+ − 346
334
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 347
try
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 348
{
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 349
$langdata = enano_json_decode($contents);
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 350
}
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 351
catch(Zend_Json_Exception $e)
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 352
{
335
67bd3121a12e
Replaced TinyMCE 2.x with 3.0 beta 3. Supports everything but IE. Also rewrote the editor interface completely from the ground up.
Dan
diff
changeset
+ − 353
$db->_die('lang.php - Exception caught by JSON parser</p><pre>' . htmlspecialchars(print_r($e, true)) . '</pre><p>');
334
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 354
exit;
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 355
}
205
+ − 356
+ − 357
if ( !is_array($langdata) )
334
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 358
{
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 359
$db->_die('lang.php - invalid or non-well-formed language file');
c72b545f1304
More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
diff
changeset
+ − 360
}
205
+ − 361
514
+ − 362
if ( $debug )
516
+ − 363
echo " Starting string import$br\n";
514
+ − 364
+ − 365
return $this->import_array($langdata, $debug);
504
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 366
}
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 367
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 368
/**
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 369
* Imports a JSON-format language file into the database and merges with current strings.
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 370
* @param string Path to plugin file
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 371
*/
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 372
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 373
function import_plugin($file)
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 374
{
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 375
global $db, $session, $paths, $template, $plugins; // Common objects
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 376
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 377
if ( !file_exists($file) )
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 378
$db->_die('lang.php - can\'t import language file: string file doesn\'t exist');
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 379
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 380
if ( $this->lang_id == 0 )
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 381
$db->_die('lang.php - BUG: trying to perform import when $lang->lang_id == 0');
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 382
507
586fd7d3202d
Fixed some stray version numbers (again!); added support for Diffie-Hellman logins in the normal login form (not AJAX) - even works in IE
Dan
diff
changeset
+ − 383
$block = pluginLoader::parse_plugin_blocks($file, 'language');
586fd7d3202d
Fixed some stray version numbers (again!); added support for Diffie-Hellman logins in the normal login form (not AJAX) - even works in IE
Dan
diff
changeset
+ − 384
if ( !is_array($block) )
586fd7d3202d
Fixed some stray version numbers (again!); added support for Diffie-Hellman logins in the normal login form (not AJAX) - even works in IE
Dan
diff
changeset
+ − 385
return false;
586fd7d3202d
Fixed some stray version numbers (again!); added support for Diffie-Hellman logins in the normal login form (not AJAX) - even works in IE
Dan
diff
changeset
+ − 386
if ( !isset($block[0]) )
586fd7d3202d
Fixed some stray version numbers (again!); added support for Diffie-Hellman logins in the normal login form (not AJAX) - even works in IE
Dan
diff
changeset
+ − 387
return false;
504
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 388
507
586fd7d3202d
Fixed some stray version numbers (again!); added support for Diffie-Hellman logins in the normal login form (not AJAX) - even works in IE
Dan
diff
changeset
+ − 389
$contents =& $block[0]['value'];
504
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 390
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 391
// Trim off all text before and after the starting and ending braces
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 392
$contents = preg_replace('/^([^{]+)\{/', '{', $contents);
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 393
$contents = preg_replace('/\}([^}]+)$/', '}', $contents);
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 394
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 395
// Correct syntax to be nice to the json parser
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 396
$contents = enano_clean_json($contents);
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 397
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 398
try
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 399
{
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 400
$langdata = enano_json_decode($contents);
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 401
}
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 402
catch(Zend_Json_Exception $e)
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 403
{
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 404
$db->_die('lang.php - Exception caught by JSON parser</p><pre>' . htmlspecialchars(print_r($e, true)) . '</pre><p>');
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 405
exit;
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 406
}
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 407
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 408
if ( !is_array($langdata) )
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 409
{
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 410
$db->_die('lang.php - invalid or non-well-formed language file');
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 411
}
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 412
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 413
// Does the plugin support the current language?
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 414
if ( isset($langdata[$this->lang_code]) )
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 415
{
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 416
// Yes, import that
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 417
return $this->import_array($langdata[$this->lang_code]);
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 418
}
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 419
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 420
// Just import the first language we run across.
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 421
$supported_langs = array_keys($langdata);
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 422
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 423
if ( !isset($supported_langs[0]) )
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 424
{
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 425
$db->_die('lang.php - plugin has an invalid or corrupt language block');
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 426
}
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 427
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 428
$first_lang = $supported_langs[0];
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 429
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 430
return $this->import_array($langdata[$first_lang]);
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 431
}
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 432
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 433
/**
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 434
* Performs the actual import of string data.
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 435
* @param array Parsed JSON object, should be in the form of an array
514
+ − 436
* @param bool Enable debugging output
504
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 437
* @access private
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 438
*/
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 439
514
+ − 440
protected function import_array($langdata, $debug = false)
504
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 441
{
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 442
global $db, $session, $paths, $template, $plugins; // Common objects
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 443
205
+ − 444
if ( !isset($langdata['categories']) || !isset($langdata['strings']) )
+ − 445
$db->_die('lang.php - language file does not contain the proper items');
+ − 446
516
+ − 447
if ( $debug )
+ − 448
$br = ( isset($_SERVER['REQUEST_URI']) ) ? '<br />' : '';
+ − 449
205
+ − 450
$insert_list = array();
+ − 451
$delete_list = array();
+ − 452
+ − 453
foreach ( $langdata['categories'] as $category )
+ − 454
{
+ − 455
if ( isset($langdata['strings'][$category]) )
+ − 456
{
514
+ − 457
if ( $debug )
+ − 458
{
+ − 459
$desc = ( isset($langdata['strings']['meta'][$category]) ) ? $langdata['strings']['meta'][$category] : $this->get("meta_$category");
516
+ − 460
echo " Indexing category: $category ({$desc})$br\n";
514
+ − 461
}
205
+ − 462
foreach ( $langdata['strings'][$category] as $string_name => $string_value )
+ − 463
{
+ − 464
$string_name = $db->escape($string_name);
+ − 465
$string_value = $db->escape($string_value);
+ − 466
$category_name = $db->escape($category);
+ − 467
$insert_list[] = "({$this->lang_id}, '$category_name', '$string_name', '$string_value')";
+ − 468
$delete_list[] = "( lang_id = {$this->lang_id} AND string_category = '$category_name' AND string_name = '$string_name' )";
+ − 469
}
+ − 470
}
+ − 471
}
+ − 472
514
+ − 473
if ( $debug )
+ − 474
{
+ − 475
echo " Running deletion of old strings...";
+ − 476
$start = microtime_float();
+ − 477
}
205
+ − 478
$delete_list = implode(" OR\n ", $delete_list);
+ − 479
377
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 480
if ( !empty($delete_list) )
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 481
{
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 482
$sql = "DELETE FROM " . table_prefix . "language_strings WHERE $delete_list;";
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 483
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 484
// Free some memory
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 485
unset($delete_list);
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 486
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 487
// Run the query
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 488
$q = $db->sql_query($sql);
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 489
if ( !$q )
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 490
$db->_die('lang.php - couldn\'t kill off them old strings');
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 491
}
205
+ − 492
514
+ − 493
if ( $debug )
+ − 494
{
+ − 495
$time = round(microtime_float() - $start, 5);
516
+ − 496
echo "({$time}s)$br\n";
514
+ − 497
}
+ − 498
377
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 499
if ( !empty($insert_list) )
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 500
{
514
+ − 501
if ( $debug )
+ − 502
{
+ − 503
echo " Inserting strings...";
+ − 504
$start = microtime_float();
+ − 505
}
377
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 506
$insert_list = implode(",\n ", $insert_list);
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 507
$sql = "INSERT INTO " . table_prefix . "language_strings(lang_id, string_category, string_name, string_content) VALUES\n $insert_list;";
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 508
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 509
// Free some memory
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 510
unset($insert_list);
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 511
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 512
// Run the query
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 513
$q = $db->sql_query($sql);
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 514
if ( !$q )
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 515
$db->_die('lang.php - couldn\'t insert strings in import()');
514
+ − 516
+ − 517
if ( $debug )
+ − 518
{
+ − 519
$time = round(microtime_float() - $start, 5);
516
+ − 520
echo "({$time}s)$br\n";
514
+ − 521
}
377
bb3e6c3bd4f4
Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
diff
changeset
+ − 522
}
205
+ − 523
+ − 524
// YAY! done!
+ − 525
// This will regenerate the cache file if possible.
514
+ − 526
if ( $debug )
516
+ − 527
echo " Regenerating cache file$br\n";
205
+ − 528
$this->regen_caches();
+ − 529
}
+ − 530
+ − 531
/**
+ − 532
* Refetches the strings and writes out the cache file.
+ − 533
*/
+ − 534
+ − 535
function regen_caches()
+ − 536
{
+ − 537
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 538
+ − 539
$lang_file = ENANO_ROOT . "/cache/lang_{$this->lang_id}.php";
+ − 540
+ − 541
// Refresh the strings in RAM to the latest copies in the DB
+ − 542
$this->fetch(false);
+ − 543
+ − 544
$handle = @fopen($lang_file, 'w');
+ − 545
if ( !$handle )
+ − 546
// Couldn't open the file. Silently fail and let the strings come from the database.
+ − 547
return false;
+ − 548
+ − 549
// The file's open, that means we should be good.
+ − 550
fwrite($handle, '<?php
+ − 551
// This file was generated automatically by Enano. You should not edit this file because any changes you make
+ − 552
// to it will not be visible in the ACP and all changes will be lost upon any changes to strings in the admin panel.
+ − 553
+ − 554
$lang_cache = ');
+ − 555
+ − 556
$exported = $this->var_export_string($this->strings);
+ − 557
if ( empty($exported) )
+ − 558
// Ehh, that's not good
+ − 559
$db->_die('lang.php - var_export_string() failed');
+ − 560
+ − 561
fwrite($handle, $exported . '; ?>');
+ − 562
210
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 563
// Update timestamp in database
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 564
$q = $db->sql_query('UPDATE ' . table_prefix . 'language SET last_changed = ' . time() . ' WHERE lang_id = ' . $this->lang_id . ';');
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 565
if ( !$q )
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 566
$db->_die('lang.php - updating timestamp on language');
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 567
205
+ − 568
// Done =)
+ − 569
fclose($handle);
+ − 570
}
+ − 571
+ − 572
/**
+ − 573
* Calls var_export() on whatever, and returns the function's output.
+ − 574
* @param mixed Whatever you want var_exported. Usually an array.
+ − 575
* @return string
+ − 576
*/
+ − 577
+ − 578
function var_export_string($val)
+ − 579
{
+ − 580
ob_start();
+ − 581
var_export($val);
+ − 582
$contents = ob_get_contents();
+ − 583
ob_end_clean();
+ − 584
return $contents;
+ − 585
}
+ − 586
+ − 587
/**
+ − 588
* Fetches a language string from the cache in RAM. If it isn't there, it will call fetch() again and then try. If it still can't find it, it will ask for the string
+ − 589
* in the default language. If even then the string can't be found, this function will return what was passed to it.
+ − 590
*
+ − 591
* This will also templatize strings. If a string contains variables in the format %foo%, you may specify the second parameter as an associative array in the format
+ − 592
* of 'foo' => 'foo substitute'.
+ − 593
*
+ − 594
* @param string ID of the string to fetch. This will always be in the format of category_stringid.
+ − 595
* @param array Optional. Associative array of substitutions.
+ − 596
* @return string
+ − 597
*/
+ − 598
+ − 599
function get($string_id, $substitutions = false)
+ − 600
{
376
+ − 601
if ( !is_array($substitutions) )
+ − 602
$substitutions = array();
+ − 603
return $this->substitute($this->get_uncensored($string_id), $substitutions);
+ − 604
}
+ − 605
+ − 606
/**
+ − 607
* The same as get(), but does not perform any substitution or filtering. Used in get() (of course) and in the admin panel, where
+ − 608
* strings are updated only if they were changed.
+ − 609
*
+ − 610
* @param string ID of the string to fetch. This will always be in the format of category_stringid.
+ − 611
* @param array Optional. Associative array of substitutions.
+ − 612
* @return string
+ − 613
*/
+ − 614
+ − 615
function get_uncensored($string_id, $substitutions = false)
+ − 616
{
205
+ − 617
// Extract the category and string ID
+ − 618
$category = substr($string_id, 0, ( strpos($string_id, '_') ));
+ − 619
$string_name = substr($string_id, ( strpos($string_id, '_') + 1 ));
+ − 620
$found = false;
+ − 621
if ( isset($this->strings[$category]) && isset($this->strings[$category][$string_name]) )
+ − 622
{
+ − 623
$found = true;
+ − 624
$string = $this->strings[$category][$string_name];
+ − 625
}
+ − 626
if ( !$found )
+ − 627
{
+ − 628
// Ehh, the string wasn't found. Rerun fetch() and try again.
243
+ − 629
if ( defined('IN_ENANO_INSTALL') )
+ − 630
{
+ − 631
return $string_id;
+ − 632
}
205
+ − 633
$this->fetch();
+ − 634
if ( isset($this->strings[$category]) && isset($this->strings[$category][$string_name]) )
+ − 635
{
+ − 636
$found = true;
+ − 637
$string = $this->strings[$category][$string_name];
+ − 638
}
+ − 639
if ( !$found )
+ − 640
{
+ − 641
// STILL not found. Check the default language.
+ − 642
$lang_default = ( $x = getConfig('default_language') ) ? intval($x) : $this->lang_id;
+ − 643
if ( $lang_default != $this->lang_id )
+ − 644
{
+ − 645
if ( !is_object($this->default) )
+ − 646
$this->default = new Language($lang_default);
376
+ − 647
return $this->default->get_uncensored($string_id);
205
+ − 648
}
+ − 649
}
+ − 650
}
+ − 651
if ( !$found )
+ − 652
{
+ − 653
// Alright, it's nowhere. Return the input, grumble grumble...
+ − 654
return $string_id;
+ − 655
}
+ − 656
// Found it!
376
+ − 657
return $string;
205
+ − 658
}
+ − 659
+ − 660
/**
+ − 661
* Processes substitutions.
+ − 662
* @param string
+ − 663
* @param array
+ − 664
* @return string
+ − 665
*/
+ − 666
+ − 667
function substitute($string, $subs)
+ − 668
{
+ − 669
preg_match_all('/%this\.([a-z0-9_]+)%/', $string, $matches);
+ − 670
if ( count($matches[0]) > 0 )
+ − 671
{
+ − 672
foreach ( $matches[1] as $i => $string_id )
+ − 673
{
+ − 674
$result = $this->get($string_id);
+ − 675
$string = str_replace($matches[0][$i], $result, $string);
+ − 676
}
+ − 677
}
209
+ − 678
preg_match_all('/%config\.([a-z0-9_]+)%/', $string, $matches);
+ − 679
if ( count($matches[0]) > 0 )
+ − 680
{
+ − 681
foreach ( $matches[1] as $i => $string_id )
+ − 682
{
+ − 683
$result = getConfig($string_id);
+ − 684
$string = str_replace($matches[0][$i], $result, $string);
+ − 685
}
+ − 686
}
205
+ − 687
foreach ( $subs as $key => $value )
+ − 688
{
209
+ − 689
$subs[$key] = strval($value);
+ − 690
$string = str_replace("%{$key}%", "{$subs[$key]}", $string);
205
+ − 691
}
504
bc8e0e9ee01d
Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
diff
changeset
+ − 692
return ( $this->debug ) ? "$string*" : $string;
205
+ − 693
}
+ − 694
+ − 695
} // class Language
+ − 696
+ − 697
?>