<?php
/*************************************************************************/
/* DOCEBO LCMS - Learning Content Managment System                       */
/* ======================================================================*/
/* Docebo is the new name of SpaghettiLearning Project                   */
/*                                                                       */
/* Copyright (c) 2005 by Emanuele Sandri (esandri@tiscali.it)            */
/*                                                                       */
/* This program is free software. You can redistribute it and/or modify  */
/* it under the terms of the GNU General Public License as published by  */
/* the Free Software Foundation; either version 2 of the License.        */
/*************************************************************************/

function strmid ($str, $start, $howManyCharsToRetrieve = 0)
{
  $start--;
  if ($howManyCharsToRetrieve === 0)
    $howManyCharsToRetrieve = strlen ($str) - $start;

  return substr ($str, $start, $howManyCharsToRetrieve);
}

function strright ($str, $howManyCharsFromRight)
{
  $strLen = strlen ($str);
  return substr ($str, $strLen - $howManyCharsFromRight, $strLen);
}

function pc_process_dir($dir_name,$max_depth = 10,$depth = 0) {
    if ($depth >= $max_depth) {
        error_log("Reached max depth $max_depth in $dir_name.");
        return false;
    }
    $subdirectories = array();
    $files = array();
    if (is_dir($dir_name) && is_readable($dir_name)) {
        $d = dir($dir_name);
        while (false !== ($f = $d->read())) {
            // skip . and .. 
            if (('.' == $f) || ('..' == $f)) {
                continue;
            }
            if (is_dir("$dir_name/$f")) {
                array_push($subdirectories,"$dir_name/$f");
            } else {
                array_push($files,"$dir_name/$f");
            }
        }
        $d->close();
        foreach ($subdirectories as $subdirectory) {
            $files = array_merge($files,pc_process_dir($subdirectory,$max_depth,$depth+1));
        }
    } 
    return $files;
}

function parsePHP( $filename ) {
	$status = "SEARCH";
	$result = array();
	$handle = fopen($filename, "r");
	$text = fread($handle, filesize($filename));
	fclose($handle);
	$tokens = token_get_all($text);
	//echo "<br />$filename<br />";
	//print_r($tokens);
	foreach($tokens as $c)
	{
		switch ($status) {
			case "SEARCH":
				if(is_array($c))
					if( ($c[0] == T_STRING) && (strcasecmp($c[1],'define') == 0 ) )
						$status = "GETOPENBRACKET";
			break;
			case "GETOPENBRACKET":
				if(is_string($c))
					if($c == '(')
						$status = "GETKEY";
			break;
			case "GETKEY":
				if(is_array($c))
					if( ($c[0] == T_CONSTANT_ENCAPSED_STRING) ) {
						$key = substr($c[1],1,strlen($c[1])-2);
						$status = "GETCOMMA";
					}
			break;
			case "GETCOMMA":
				if(is_string($c))
					if($c == ',')
						$status = "GETVALUE";
			break;
			case "GETVALUE":
				if(is_array($c))
					if( ($c[0] == T_CONSTANT_ENCAPSED_STRING) ) {
						$result[$key] = substr($c[1],1,strlen($c[1])-2);
						$status = "GETCLOSEBRACKET";
					}
			break;
			case "GETCLOSEBRACKET":
				if(is_string($c))
					if($c == ')')
						$status = "SEARCH";
			break;			
		} 
	}
	return $result;
}

global $globLangManager;
ob_end_flush();
$scan_path = array( 'menu'=>'', 'modules'=>'', 'admin/menu'=>'admin_', 'admin/modules'=>'admin_' );
$map_languages = array( 'italian/language.php' => array('italian','/^.*\/([^\/]+)\/italian\/language.php$/'),
						'english/language.php' => array('english','/^.*\/([^\/]+)\/english\/language.php$/'),
						'italian/do.language.php' => array('italian','/^.*\/([^\/]+)\/italian\/do.language.php$/'),
						'english/do.language.php' => array('english','/^.*\/([^\/]+)\/english\/do.language.php$/'),
						'lang-italian.php' => array('italian','/^.*\/([^\/]+)\/lang-italian.php$/'),
						'lang-english.php' => array('english','/^.*\/([^\/]+)\/lang-english.php$/') );
						
$base_path = dirname(__FILE__).'/../../../';						
$files = array();


echo '<table border="1">';
echo '<thead>';
echo '<tr><th>File</th><th>lang_code</th><th>Module</th><th># termini</th></tr>';
echo '</thead>';
echo '<tbody>';
foreach( $scan_path as $path => $kind ) {
	$files = pc_process_dir($base_path.$path);
	foreach( $files as $f ) {
		foreach( $map_languages as $map_key => $map ) {
			if( strright($f, strlen($map_key)) == $map_key ) {
				echo '<tr><td>';
				echo strmid($f, strlen($base_path)+1);
				echo '</td><td>';
				echo $map[0];
				$globLangManager->setLanguage( $map[0] );
				echo '</td><td>';
				$arrModule = array();
				$resMatch = preg_match ( $map[1], $f, $arrModule);
				if( $resMatch === FALSE ) {
					$newEntries = 0;
					echo "error in match";
				} elseif( $resMatch == 0 ) {
					$newEntries = 0;
					echo "module not found";
				} else {
					echo $kind.$arrModule[1];
					$defines = parsePHP($f);
					foreach( $defines as $key_def => $key_val ) {
						$globLangManager->updateKey( $key_def, $kind.$arrModule[1] );
						$globLangManager->updateTranslation($key_def, 
															$kind.$arrModule[1],
															$key_val,
															$map[0]);
					}
					$newEntries = count($defines);
				}
				echo '</td><td>';
				echo $newEntries;
				echo '</td>';
				echo '</tr>';
			}
		}
	}
}
echo '</tbody>';
echo '</table>';
/*$defines = parsePHP('admin/modules/lang/italian/language.php');
foreach( $defines as $key => $val ) {
	echo "Chiave: $key, Valore: $val <br />\n";
}*/
?>
