b2evolution PHP Cross Reference Blogging Systems

Source: /inc/locales/locales.ctrl.php - 400 lines - 15392 bytes - Text - Print

Description: This file implements the UI controller for settings management.

   1  <?php
   2  /**
   3   * This file implements the UI controller for settings management.
   4   *
   5   * This file is part of the evoCore framework - {@link http://evocore.net/}
   6   * See also {@link http://sourceforge.net/projects/evocms/}.
   7   *
   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}
   9   * Parts of this file are copyright (c)2004-2006 by Daniel HAHLER - {@link http://thequod.de/contact}.
  10   *
  11   * {@internal License choice
  12   * - If you have received this file as part of a package, please find the license.txt file in
  13   *   the same folder or the closest folder above for complete license terms.
  14   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
  15   *   then you must choose one of the following licenses before using the file:
  16   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  17   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  18   * }}
  19   *
  20   * {@internal Open Source relicensing agreement:
  21   * Daniel HAHLER grants Francois PLANQUE the right to license
  22   * Daniel HAHLER's contributions to this file and the b2evolution project
  23   * under any OSI approved OSS license (http://www.opensource.org/licenses/).
  24   * }}
  25   *
  26   * @package admin
  27   *
  28   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  29   * @author blueyed: Daniel HAHLER
  30   * @author fplanque: Francois PLANQUE
  31   *
  32   * @version $Id: locales.ctrl.php 6664 2014-05-12 12:23:58Z yura $
  33   */
  34  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  35  
  36  // Check minimum permission:
  37  $current_User->check_perm( 'options', 'view', true );
  38  
  39  // Memorize this as the last "tab" used in the Global Settings:
  40  $UserSettings->set( 'pref_glob_settings_tab', $ctrl );
  41  $UserSettings->set( 'pref_glob_regional_tab', $ctrl );
  42  $UserSettings->dbupdate();
  43  
  44  $AdminUI->set_path( 'options', 'regional', 'locales' );
  45  
  46  param( 'action', 'string' );
  47  param( 'edit_locale', 'string' );
  48  param( 'loc_transinfo', 'integer', 0 );
  49  
  50  // Load all available locale defintions:
  51  locales_load_available_defs();
  52  
  53  switch( $action )
  54  {
  55      case 'update':
  56          // UPDATE regional settings
  57  
  58          // Check that this action request is not a CSRF hacked request:
  59          $Session->assert_received_crumb( 'locales' );
  60  
  61          // Check permission:
  62          $current_User->check_perm( 'options', 'edit', true );
  63  
  64          param( 'newdefault_locale', 'string', true );
  65          $Settings->set( 'default_locale', $newdefault_locale );
  66  
  67          if( ! $Messages->has_errors() )
  68          {
  69              locale_updateDB();
  70              $Settings->dbupdate();
  71              $Messages->add( T_('Regional settings updated.'), 'success' );
  72              // Redirect so that a reload doesn't write to the DB twice:
  73              header_redirect( '?ctrl=locales'.( $loc_transinfo ? '&loc_transinfo=1' : '' ), 303 ); // Will EXIT
  74              // We have EXITed already at this point!!
  75          }
  76  
  77          // load locales from DB into $locales array:
  78          // fp> not sure we need this...
  79          locale_overwritefromDB();
  80          break;
  81  
  82  
  83      case 'updatelocale':
  84      case 'createlocale':
  85          // CREATE/EDIT locale
  86  
  87          // Check that this action request is not a CSRF hacked request:
  88          $Session->assert_received_crumb( 'locales' );
  89  
  90          // Check permission:
  91          $current_User->check_perm( 'options', 'edit', true );
  92  
  93          param( 'newloc_locale', 'string', true );
  94          param_check_regexp( 'newloc_locale', '/^[a-z]{2,3}-[A-Z]{2}.*$/', T_('Please use valid locale format.') );
  95          param( 'newloc_enabled', 'integer', 0 );
  96          param( 'newloc_name', 'string', true );
  97          param( 'newloc_charset', 'string', true );
  98          param_check_not_empty( 'newloc_charset', T_('Charset cannot be empty.') );
  99          param( 'newloc_datefmt', 'string', true );
 100          param_check_not_empty( 'newloc_datefmt', T_('Date format cannot be empty.') );
 101          param( 'newloc_timefmt', 'string', true );
 102          param_check_not_empty( 'newloc_timefmt', T_('Time format cannot be empty.') );
 103          param( 'newloc_startofweek', 'integer', 0 );
 104          param( 'newloc_priority', 'integer', 1 );
 105          param_check_range( 'newloc_priority', 1, 255, T_('Priority must be numeric (1-255).') );
 106          param( 'newloc_messages', 'string', true );
 107          param( 'newloc_transliteration_map', 'string', true );
 108  
 109          if( param_errors_detected() )
 110          { // Don't save locale if errors exist
 111              $action = 'edit';
 112              break;
 113          }
 114  
 115          if( $action == 'updatelocale' )
 116          {
 117              param( 'oldloc_locale', 'string', true );
 118  
 119              if( $DB->get_var( 'SELECT loc_locale FROM T_locales WHERE loc_locale = '.$DB->quote( $oldloc_locale ) ) )
 120              { // old locale exists in DB
 121                  if( $oldloc_locale != $newloc_locale )
 122                  { // locale key was renamed, we delete the old locale in DB and remember to create the new one
 123                      $q = $DB->query( 'DELETE FROM T_locales
 124                                                              WHERE loc_locale = '.$DB->quote( $oldloc_locale ) );
 125                      if( $DB->rows_affected )
 126                      {
 127                          $Messages->add( sprintf( T_('Deleted settings for locale &laquo;%s&raquo; in database.'), $oldloc_locale ), 'success' );
 128                      }
 129                  }
 130              }
 131              elseif( isset( $locales[ $oldloc_locale ] ) )
 132              { // old locale is not in DB yet. Insert it.
 133  
 134                  $transliteration_map = '';
 135                  if( isset( $locales[$oldloc_locale]['transliteration_map']) && is_array( $locales[$oldloc_locale]['transliteration_map'] ) )
 136                  {
 137                      $transliteration_map = base64_encode( serialize( $locales[$oldloc_locale]['transliteration_map'] ) );
 138                  }
 139  
 140                  $query = "INSERT INTO T_locales
 141                                      ( loc_locale, loc_charset, loc_datefmt, loc_timefmt, loc_startofweek, loc_name, loc_messages, loc_priority, loc_transliteration_map, loc_enabled )
 142                                      VALUES ( '$oldloc_locale',
 143                                      '{$locales[$oldloc_locale]['charset']}', '{$locales[$oldloc_locale]['datefmt']}',
 144                                      '{$locales[$oldloc_locale]['timefmt']}', '{$locales[$oldloc_locale]['startofweek']}',
 145                                      '{$locales[$oldloc_locale]['name']}', '{$locales[$oldloc_locale]['messages']}',
 146                                      '{$locales[$oldloc_locale]['priority']}', '$transliteration_map'";
 147                  if( $oldloc_locale != $newloc_locale )
 148                  { // disable old locale
 149                      $query .= ', 0 )';
 150                      $Messages->add( sprintf( T_('Inserted (and disabled) locale &laquo;%s&raquo; into database.'), $oldloc_locale ), 'success' );
 151                  }
 152                  else
 153                  { // keep old state
 154                      $query .= ', '.( $locales[$oldloc_locale]['enabled'] ).' )';
 155                      $Messages->add( sprintf( T_('Inserted locale &laquo;%s&raquo; into database.'), $oldloc_locale ), 'success' );
 156                  }
 157                  $q = $DB->query( $query );
 158              }
 159          }
 160  
 161          $query = 'REPLACE INTO T_locales
 162                              ( loc_locale, loc_charset, loc_datefmt, loc_timefmt, loc_startofweek, loc_name, loc_messages, loc_priority, loc_transliteration_map, loc_enabled )
 163                              VALUES ( '.$DB->quote( $newloc_locale ).', '.$DB->quote( $newloc_charset ).', '.$DB->quote( $newloc_datefmt ).', '
 164                                  .$DB->quote( $newloc_timefmt ).', '.$DB->quote( $newloc_startofweek ).', '.$DB->quote( $newloc_name ).', '
 165                                  .$DB->quote( $newloc_messages ).', '.$DB->quote( $newloc_priority ).', '.$DB->quote( $newloc_transliteration_map ).', '
 166                                  .$DB->quote( $newloc_enabled ).' )';
 167          $q = $DB->query( $query );
 168          $Messages->add( sprintf(T_('Saved locale &laquo;%s&raquo;.'), $newloc_locale), 'success' );
 169  
 170          // Redirect so that a reload doesn't write to the DB twice:
 171          header_redirect( '?ctrl=locales'.( $loc_transinfo ? '&loc_transinfo=1' : '' ), 303 ); // Will EXIT
 172          // We have EXITed already at this point!!
 173  
 174          /*
 175          // reload locales: an existing one could have been renamed (but we keep $evo_charset, which may have changed)
 176          $old_evo_charset = $evo_charset;
 177          unset( $locales );
 178          include $conf_path.'_locales.php';
 179          if( file_exists($conf_path.'_overrides_TEST.php') )
 180          { // also overwrite settings again:
 181              include $conf_path.'_overrides_TEST.php';
 182          }
 183          $evo_charset = $old_evo_charset;
 184  
 185          // Load all available locale defintions:
 186          locales_load_available_defs();
 187  
 188          // load locales from DB into $locales array:
 189          locale_overwritefromDB();
 190          */
 191          break;
 192  
 193  
 194      case 'reset':
 195          // RESET locales in DB
 196  
 197          // Check that this action request is not a CSRF hacked request:
 198          $Session->assert_received_crumb( 'locales' );
 199  
 200          // Check permission:
 201          $current_User->check_perm( 'options', 'edit', true );
 202  
 203          // forget DB locales:
 204          unset( $locales );
 205  
 206          // delete everything from locales table
 207          $q = $DB->query( 'DELETE FROM T_locales' );
 208  
 209          if( !isset( $locales[$current_locale] ) )
 210          { // activate default locale
 211              locale_activate( $default_locale );
 212          }
 213  
 214          // reset default_locale
 215          $Settings->set( 'default_locale', $default_locale );
 216          $Settings->dbupdate();
 217  
 218          // Reload locales from files:
 219          unset( $locales );
 220          include $conf_path.'_locales.php';
 221          if( file_exists($conf_path.'_overrides_TEST.php') )
 222          { // also overwrite settings again (just in case we override some local erelated things):
 223              include $conf_path.'_overrides_TEST.php';
 224          }
 225  
 226          // Load all available locale defintions from locale folders:
 227          locales_load_available_defs();
 228  
 229          // Reenable default locale
 230          locale_insert_default();
 231  
 232          $Messages->add( T_('Locale definitions reset to defaults. (<code>/conf/_locales.php</code>)'), 'success' );
 233  
 234          // Redirect so that a reload doesn't write to the DB twice:
 235          header_redirect( '?ctrl=locales'.( $loc_transinfo ? '&loc_transinfo=1' : '' ), 303 ); // Will EXIT
 236          // We have EXITed already at this point!!
 237          break;
 238  
 239  
 240      case 'extract':
 241          // EXTRACT locale
 242  
 243          // Check that this action request is not a CSRF hacked request:
 244          $Session->assert_received_crumb( 'locales' );
 245  
 246          // Check permission:
 247          $current_User->check_perm( 'options', 'edit', true );
 248  
 249          // Get PO file for that edit_locale:
 250          $AdminUI->append_to_titlearea( 'Extracting language file for '.$edit_locale.'...' );
 251  
 252          $po_file = $locales_path.$locales[$edit_locale]['messages'].'/LC_MESSAGES/messages.po';
 253          if( ! is_file( $po_file ) )
 254          {
 255              $Messages->add( sprintf(T_('File <code>%s</code> not found.'), '/'.$locales_subdir.$locales[$edit_locale]['messages'].'/LC_MESSAGES/messages.po'), 'error' );
 256              break;
 257          }
 258  
 259          $outfile = $locales_path.$locales[$edit_locale]['messages'].'/_global.php';
 260          if( file_exists( $outfile ) && ( !is_writable( $outfile ) ) )
 261          { // The '_global.php' file exists but it is not writable
 262              $Messages->add( sprintf( 'The file &laquo;%s&raquo; is not writable.', $outfile ) );
 263              break;
 264          }
 265  
 266          load_class( 'locales/_pofile.class.php', 'POFile' );
 267          $POFile = new POFile($po_file);
 268          $POFile->read(true); // adds info about sources to $Messages
 269          $POFile->write_evo_trans($outfile, $locales[$edit_locale]['messages']);
 270  
 271          // Redirect so that a reload doesn't write to the DB twice:
 272          header_redirect( '?ctrl=locales'.( $loc_transinfo ? '&loc_transinfo=1' : '' ), 303 ); // Will EXIT
 273          // We have EXITed already at this point!!
 274          break;
 275  
 276  
 277      case 'resetlocale':
 278          // Reset a specific Locale:
 279  
 280          // Check that this action request is not a CSRF hacked request:
 281          $Session->assert_received_crumb( 'locales' );
 282  
 283          // Check permission:
 284          $current_User->check_perm( 'options', 'edit', true );
 285  
 286          // --- DELETE locale from DB
 287          if( $DB->query( 'DELETE FROM T_locales
 288                                              WHERE loc_locale = "'.$DB->escape( $edit_locale ).'"' ) )
 289          {
 290              $Messages->add( sprintf(T_('Deleted locale &laquo;%s&raquo; from database.'), $edit_locale), 'success' );
 291          }
 292  
 293          // Redirect so that a reload doesn't write to the DB twice:
 294          header_redirect( '?ctrl=locales'.( $loc_transinfo ? '&loc_transinfo=1' : '' ), 303 ); // Will EXIT
 295          // We have EXITed already at this point!!
 296  
 297          /*
 298          // reload locales
 299          unset( $locales );
 300          require $conf_path.'_locales.php';
 301          if( file_exists($conf_path.'_overrides_TEST.php') )
 302          { // also overwrite settings again:
 303              include $conf_path.'_overrides_TEST.php';
 304          }
 305  
 306          // Load all available locale defintions:
 307          locales_load_available_defs();
 308  
 309          // load locales from DB into $locales array:
 310          locale_overwritefromDB();
 311          */
 312          break;
 313  
 314  
 315      case 'prioup':
 316      case 'priodown':
 317          // --- SWITCH PRIORITIES -----------------
 318  
 319          // Check that this action request is not a CSRF hacked request:
 320          $Session->assert_received_crumb( 'locales' );
 321  
 322          // Check permission:
 323          $current_User->check_perm( 'options', 'edit', true );
 324  
 325          $switchcond = '';
 326          if( $action == 'prioup' )
 327          {
 328              $switchcond = 'return ($lval[\'priority\'] > $i && $lval[\'priority\'] < $locales[ $edit_locale ][\'priority\']);';
 329              $i = -1;
 330          }
 331          elseif( $action == 'priodown' )
 332          {
 333              $switchcond = 'return ($lval[\'priority\'] < $i && $lval[\'priority\'] > $locales[ $edit_locale ][\'priority\']);';
 334              $i = 256;
 335          }
 336  
 337          if( !empty($switchcond) )
 338          { // we want to switch priorities
 339  
 340              foreach( $locales as $lkey => $lval )
 341              { // find nearest priority
 342                  if( eval($switchcond) )
 343                  {
 344                      // remember it
 345                      $i = $lval['priority'];
 346                      $lswitchwith = $lkey;
 347                  }
 348              }
 349              if( $i > -1 && $i < 256 )
 350              { // switch
 351                  #echo 'Switching prio '.$locales[ $lswitchwith ]['priority'].' with '.$locales[ $lswitch ]['priority'].'<br />';
 352                  $locales[ $lswitchwith ]['priority'] = $locales[ $edit_locale ]['priority'];
 353                  $locales[ $edit_locale ]['priority'] = $i;
 354  
 355                  $lswitchwith_transliteration_map = is_array($locales[ $lswitchwith ]['transliteration_map']) ? base64_encode(serialize($locales[ $lswitchwith ]['transliteration_map'])) : '';
 356                  $edit_transliteration_map = is_array($locales[ $edit_locale ]['transliteration_map']) ? base64_encode(serialize($locales[ $edit_locale ]['transliteration_map'])) : '';
 357  
 358                  $query = "REPLACE INTO T_locales ( loc_locale, loc_charset, loc_datefmt, loc_timefmt, loc_name, loc_messages, loc_priority, loc_transliteration_map, loc_enabled )    VALUES
 359                      ( '$edit_locale', '{$locales[ $edit_locale ]['charset']}', '{$locales[ $edit_locale ]['datefmt']}', '{$locales[ $edit_locale ]['timefmt']}', '{$locales[ $edit_locale ]['name']}', '{$locales[ $edit_locale ]['messages']}', '{$locales[ $edit_locale ]['priority']}', '$edit_transliteration_map', '{$locales[ $edit_locale ]['enabled']}'),
 360                      ( '$lswitchwith', '{$locales[ $lswitchwith ]['charset']}', '{$locales[ $lswitchwith ]['datefmt']}', '{$locales[ $lswitchwith ]['timefmt']}', '{$locales[ $lswitchwith ]['name']}', '{$locales[ $lswitchwith ]['messages']}', '{$locales[ $lswitchwith ]['priority']}', '$lswitchwith_transliteration_map', '{$locales[ $lswitchwith ]['enabled']}')";
 361                  $q = $DB->query( $query );
 362  
 363                  $Messages->add( T_('Switched priorities.'), 'success' );
 364  
 365                  // Redirect so that a reload doesn't write to the DB twice:
 366                  header_redirect( '?ctrl=locales'.( $loc_transinfo ? '&loc_transinfo=1' : '' ), 303 ); // Will EXIT
 367                  // We have EXITed already at this point!!
 368              }
 369  
 370              // load locales from DB into $locales array:
 371              locale_overwritefromDB();
 372          }
 373          break;
 374  }
 375  
 376  $AdminUI->breadcrumbpath_init( false );
 377  $AdminUI->breadcrumbpath_add( T_('System'), '?ctrl=system',
 378          T_('Global settings are shared between all blogs; see Blog settings for more granular settings.') );
 379  $AdminUI->breadcrumbpath_add( T_('Regional settings'), '?ctrl=locales' );
 380  $AdminUI->breadcrumbpath_add( T_('Locales'), '?ctrl=locales' );
 381  
 382  // Display <html><head>...</head> section! (Note: should be done early if actions do not redirect)
 383  $AdminUI->disp_html_head();
 384  
 385  // Display title, menu, messages, etc. (Note: messages MUST be displayed AFTER the actions)
 386  $AdminUI->disp_body_top();
 387  
 388  // Begin payload block:
 389  $AdminUI->disp_payload_begin();
 390  
 391  // Display VIEW:
 392  $AdminUI->disp_view( 'locales/_locale_settings.form.php' );
 393  
 394  // End payload block:
 395  $AdminUI->disp_payload_end();
 396  
 397  // Display body bottom, debug info and close </html>:
 398  $AdminUI->disp_global_footer();
 399  
 400  ?>

title

Description

title

Description

title

Description

title

title

Body