[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/admin/ -> test_langs.php (source)

   1  <?php
   2  # MantisBT - A PHP based bugtracking system
   3  
   4  # MantisBT is free software: you can redistribute it and/or modify
   5  # it under the terms of the GNU General Public License as published by
   6  # the Free Software Foundation, either version 2 of the License, or
   7  # (at your option) any later version.
   8  #
   9  # MantisBT is distributed in the hope that it will be useful,
  10  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  # GNU General Public License for more details.
  13  #
  14  # You should have received a copy of the GNU General Public License
  15  # along with MantisBT.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * @package MantisBT
  19   * @copyright Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
  20   * @copyright Copyright (C) 2002 - 2011  MantisBT Team - mantisbt-dev@lists.sourceforge.net
  21   * @link http://www.mantisbt.org
  22   */
  23  
  24  define( 'PLUGINS_DISABLED', true );
  25  define( 'LANG_LOAD_DISABLED', true );
  26  
  27  /**
  28   * MantisBT Core API's
  29   */
  30  require_once( dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'core.php' );
  31  
  32  access_ensure_global_level( config_get_global( 'admin_site_threshold' ) );
  33  
  34  if( function_exists( 'xdebug_disable' ) ) {
  35      xdebug_disable();
  36  }
  37  
  38  if( !defined( 'T_ML_COMMENT' ) ) {
  39      define( 'T_ML_COMMENT', T_COMMENT );
  40  }
  41  else {
  42      define( 'T_DOC_COMMENT', T_ML_COMMENT );
  43  }
  44  
  45  if (!checkfile( dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR, 'strings_english.txt', true)) {
  46      print_error( "FAILED: Language file 'strings_english.txt' failed." );
  47      die;
  48  }
  49  
  50  lang_push( 'english' );
  51  
  52  set_time_limit( 0 );
  53  
  54  html_page_top();
  55  
  56  // check core language files
  57  if( function_exists( 'opendir' ) && function_exists( 'readdir' ) ) {
  58      $t_lang_files = Array();
  59      if( $t_handle = opendir( dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'lang' ) ) {
  60          while( false !== ( $t_file = readdir( $t_handle ) ) ) {
  61              if ($t_file == 'strings_english.txt' ) {
  62                  echo "Testing english language file '$t_file' (phase 1)...<br />";
  63                  flush();
  64                  checkfile( dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR, $t_file );
  65              }
  66              if( $t_file[0] != '.' && $t_file != 'langreadme.txt' && !is_dir( $t_file ) ) {
  67                  $t_lang_files[] = $t_file;
  68              }
  69          }
  70          closedir( $t_handle );
  71      }
  72  }
  73  else {
  74      $t_lang_files = Array();
  75      foreach( $g_language_choices_arr as $t_lang ) {
  76          if( $t_lang == 'auto' ) {
  77              continue;
  78          }
  79          $t_lang_files[] = $t_lang;
  80      }
  81  }
  82  
  83  if( count( $t_lang_files ) > 0 ) {
  84      echo 'Retrieved ', count( $t_lang_files ), ' languages<br />';
  85  
  86      foreach( $t_lang_files as $t_file ) {
  87          echo "Testing language file '$t_file' (phase 1)...<br />";
  88          flush();
  89  
  90          checkfile( dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR, $t_file );
  91      }
  92  }
  93  
  94  // attempt to find plugin language files
  95  echo "Trying to find+check plugin language files...<br />";
  96  if( function_exists( 'opendir' ) && function_exists( 'readdir' ) ) {
  97      checklangdir ( config_get( 'plugin_path' ) );
  98  } else {
  99      echo 'php opendir/readdir are disabled - skipping<br />';
 100  }
 101  
 102  function checklangdir( $p_path, $p_subpath = '' ) {
 103      $p_path = $p_path . DIRECTORY_SEPARATOR . $p_subpath . DIRECTORY_SEPARATOR;
 104      if( $handle = opendir( $p_path ) ) {
 105          while( false !== ( $file = readdir( $handle ) ) ) {
 106              if ( $file[0] == '.' )
 107                  continue;
 108              if ( $p_subpath == '' ) {
 109                  echo "Checking language files for plugin $file:<br />";
 110  
 111                  if (file_exists( $p_path . DIRECTORY_SEPARATOR . $p_subpath . DIRECTORY_SEPARATOR . $file . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . 'strings_english.txt' ) ) {
 112                      echo "Testing english language for plugin '$file' (phase 1)...<br />";
 113                      flush();
 114                      checkfile( $p_path . DIRECTORY_SEPARATOR . $p_subpath . DIRECTORY_SEPARATOR . $file . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR,  'strings_english.txt' );
 115                  }
 116              }
 117  
 118              if( !is_dir( $p_path . DIRECTORY_SEPARATOR . $file ) && $p_subpath == 'lang' ) {
 119                  checkfile( $p_path, $file );
 120              } else {
 121                  if ( is_dir( $p_path . DIRECTORY_SEPARATOR . $file ) )
 122                      checklangdir( $p_path, $file);
 123              }
 124          }
 125          closedir( $handle );
 126      }
 127  }
 128  
 129  
 130  function checkfile( $p_path, $p_file, $p_quiet = false ) {
 131          if( !$p_quiet) {
 132              echo "Testing language file '$p_file' (phase 1)...<br />";
 133              flush();
 134          }
 135  
 136          $file = $p_path . $p_file;
 137  
 138          set_error_handler( 'lang_error_handler' );
 139          $result = checktoken( $file, ($p_file == 'strings_english.txt' ? true : false) );
 140          restore_error_handler();
 141  
 142          if( !$result ) {
 143              print_error( "FAILED: Language file '$p_file' failed at phase 1." );
 144              if( $p_quiet ) {
 145                  return false;
 146              }
 147          }
 148  
 149          if( !$p_quiet ) {
 150              echo "Testing language file '$p_file' (phase 2)...<br />";
 151              flush();
 152          } else {
 153              return true;
 154          }
 155  
 156          set_error_handler( 'lang_error_handler' );
 157          ob_start();
 158          $result = eval( "require_once( '$file' );" );
 159          $data = ob_get_contents();
 160          ob_end_clean();
 161          restore_error_handler();
 162  
 163          if( $result === false ) {
 164              print_error( "FAILED: Language file '$p_file' failed at eval" );
 165              if( $p_quiet ) {
 166                  return false;
 167              }
 168          }
 169  
 170          if( !empty( $data ) ) {
 171              print_error( "FAILED: Language file '$p_file' failed at require_once (data output: " . var_export( $data, true ) . ")" );
 172              if( $p_quiet ) {
 173                  return false;
 174              }
 175          }
 176          return true;
 177  }
 178  
 179  $basevariables = Array();
 180  
 181  function checktoken( $file, $base = false ) {
 182      $in_php_code = false;
 183      $variables = Array();
 184      global $basevariables;
 185      $current_var = null;
 186      $last_token = 0;
 187      $set_variable = false;
 188      $variablearr = false;
 189      $twopartstring = false;
 190      $need_end_variable = false;
 191      $source = file_get_contents( $file );
 192      $tokens = @token_get_all( $source );
 193      $expectendarr = false;
 194      $settingvariable = false;
 195      $pass = true;
 196      $fatal = false;
 197      foreach( $tokens as $token ) {
 198          $last_token2 = 0;
 199          if( is_string( $token ) ) {
 200              switch( $token ) {
 201                  case '=':
 202                      if( $last_token != T_VARIABLE ) {
 203                          print_error( "ERROR: = sign without variable" );
 204                          $pass = false;
 205                      }
 206                      $set_variable = true;
 207                      break;
 208                  case '[':
 209                      if( $last_token != T_VARIABLE ) {
 210                          $pass = false;
 211                      }
 212                      $variablearr = true;
 213                      break;
 214                  case ']':
 215                      if( !$expectendarr ) {
 216                          $pass = false;
 217                      }
 218                      $expectendarr = false;
 219                      break;
 220                  case ';':
 221                      if( !$need_end_variable ) {
 222                          print_error( "ERROR: function seperator found at unexpected location (line $line)" );
 223                          $pass = false;
 224                      }
 225                      $need_end_variable = false;
 226                      break;
 227                  case '.':
 228                      if( $last_token == T_CONSTANT_ENCAPSED_STRING ) {
 229                          $twopartstring = true;
 230                      } else {
 231                          print_error( "ERROR: string concat found at unexpected location (line $line)" );
 232                          $pass = false;
 233                      }
 234                      break;
 235                  default:
 236                      print_error( "UNKNOWN TOKEN" . $token );
 237                      $pass = false;
 238                      break;
 239              }
 240          } else {
 241              // token array
 242              list( $id, $text, $line ) = $token;
 243  
 244              if( $id == T_WHITESPACE || $id == T_COMMENT || $id == T_DOC_COMMENT || $id == T_ML_COMMENT ) {
 245                  continue;
 246              }
 247              if( $need_end_variable ) {
 248                  if( $twopartstring && $id == T_CONSTANT_ENCAPSED_STRING ) {
 249                      $twopartstring = false;
 250                      continue;
 251                  }
 252                  if( $settingvariable && $id == T_STRING ) {
 253                      $last_token = T_VARIABLE;
 254                      $expectendarr = true;
 255                      continue;
 256                  }
 257  
 258                  print_error( "ERROR" . $id . token_name( $id ) . $text . $line );
 259                  $pass = false;
 260              }
 261  
 262              switch( $id ) {
 263                  case T_OPEN_TAG:
 264                      $in_php_code = true;
 265                      break;
 266                  case T_CLOSE_TAG:
 267                      $in_php_code = false;
 268                      break;
 269                  case T_INLINE_HTML:
 270                      print_error( "ERROR: Whitespace in language file outside of PHP code block (line $line)" );
 271                      $pass = false;
 272                      break;
 273                  case T_VARIABLE:
 274                      if( $set_variable && $current_var != null ) {
 275                          $need_end_variable = true;
 276                          $settingvariable = true;
 277                          $current_var = null;
 278                          break;
 279                      }
 280                      $current_var = $text;
 281                      break;
 282                  case T_STRING:
 283                      if( $variablearr ) {
 284                          $current_var .= $text;
 285                          if( !defined( $text ) ) {
 286                              print_error( "undefined constant: $current_var" );
 287                          }
 288                      } else {
 289                          print_error( "ERROR: T_STRING found at unexpected location (line $line)" );
 290                          $pass = false;
 291                      }
 292                      if ( strpos($current_var,"\n") !== false ) {
 293                          print_error( "PARSER - NEW LINE IN STRING: " . $id . token_name( $id ) . $text . $line );
 294                          $pass = false;
 295                          $fatal = true;
 296                      }
 297                      $last_token2 = T_VARIABLE;
 298                      $expectendarr = true;
 299                      break;
 300                  case T_CONSTANT_ENCAPSED_STRING:
 301                      if ( $token[1][0] != '\'' ) {
 302                              print_error( "ERROR: Language strings should be single-quoted (line $line)" );
 303                      }
 304                      if( $last_token == T_VARIABLE && $set_variable && $current_var != null ) {
 305                          if( isset( $variables[$current_var] ) ) {
 306                              print_error( "ERROR: duplicate language string ($current_var ) (line $line)" );
 307                          } else {
 308                              $variables[$current_var] = $text;
 309                          }
 310  
 311                          if ( $base ) {
 312                              // english
 313                              //if( isset( $basevariables[$current_var] ) ) {
 314                              //    print_error( "WARN: english string redefined - plugin? $current_var" );
 315                              //}
 316                              $basevariables[$current_var] = true;
 317                          } else {
 318                              if( !isset( $basevariables[$current_var] ) ) {
 319                                  print_error( "WARN: String defined in non-english file that does not exist ( $current_var )" );
 320                              //} else {
 321                                  // missing translation
 322                              }
 323                          }
 324  
 325                      }
 326                      if ( strpos($current_var,"\n") !== false ) {
 327                          print_error( "PARSER - NEW LINE IN STRING: " . $id . token_name( $id ) . $text . $line );
 328                          $pass = false;
 329                          $fatal = true;
 330                      }
 331                      $current_var = null;
 332                      $need_end_variable = true;
 333                      break;
 334                  default:
 335                      // if (!$in_php_code)
 336                      print_error( "PARSER: " . $id . token_name( $id ) . $text . $line );
 337                      $pass = false;
 338                      break;
 339              }
 340  
 341              $last_token = $id;
 342              if( $last_token2 > 0 ) {
 343                  $last_token = $last_token2;
 344              }
 345          }
 346  
 347          if ($fatal)
 348              break;
 349      }
 350  
 351      return $pass;
 352  }
 353  
 354  function lang_error_handler( $p_type, $p_error, $p_file, $p_line, $p_context ) {
 355      print_error( "error handler thrown: " . $p_type . '<br />' . $p_error . '<br />' . $p_file . '<br />' . $p_line . '<br />' . $p_context );
 356  }
 357  
 358  function print_error( $p_string ) {
 359      echo '<p class="error-msg">ERROR: ', $p_string, '</p>';
 360  }
 361  
 362  html_page_bottom();


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7