PageRenderTime 68ms CodeModel.GetById 2ms app.highlight 54ms RepoModel.GetById 1ms app.codeStats 0ms

/e107_admin/lancheck.php

https://github.com/CasperGemini/e107
PHP | 1832 lines | 684 code | 248 blank | 900 comment | 134 complexity | 8a2b0e5ff02c7e6c514a40eef1914f24 MD5 | raw file
   1<?php
   2/*
   3 * e107 website system
   4 *
   5 * Copyright (C) 2008-2013 e107 Inc (e107.org)
   6 * Released under the terms and conditions of the
   7 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
   8 *
   9 * Administration Area - Language check
  10 * With code from Izydor and Lolo.
  11 *
  12*/
  13require_once("../class2.php");
  14if (!getperms("L")) 
  15{
  16	header("location:".e_BASE."index.php");
  17	exit;
  18}
  19
  20include_lan(e_LANGUAGEDIR.e_LANGUAGE.'/admin/lan_'.e_PAGE);
  21
  22$e_sub_cat = 'language';
  23// require_once("auth.php");
  24
  25$frm = e107::getForm();
  26$mes = e107::getMessage();
  27$lck = new lancheck;
  28
  29
  30/*
  31$qry = explode("|",e_QUERY);
  32$f = $qry[0];
  33$lan = $qry[1];
  34$mode = $qry[2];
  35
  36// Write the language file.
  37if(isset($_POST['submit']))
  38{
  39	unset($input);
  40	$kom_start = chr(47)."*";
  41	$kom_end = "*".chr(47);
  42
  43	if($_POST['root'])
  44	{
  45		$writeit = $_POST['root'];
  46	}
  47
  48	$old_kom = "";
  49	$in_kom=0;
  50	$data = file($writeit);
  51	foreach($data as $line)
  52	{
  53
  54		if (strpos($line,$kom_start) !== False && $old_kom == "")
  55		{
  56			$in_kom=1;
  57		}
  58		if ($in_kom) { $old_kom.=$line; }
  59		if (strpos($line,$kom_end) !== False && $in_kom) {$in_kom = 0;}
  60	}
  61
  62
  63	$message = "<div style='text-align:left'><br />";
  64	$input .= chr(60)."?php\n";
  65	if ($old_kom == "")
  66	{
  67		// create CVS compatible description.
  68		$diz = chr(47)."*\n";
  69		$diz .= " * e107 website system\n";
  70		$diz .= " *\n";
  71		$diz .= " * Copyright (C) 2008-2009 e107 Inc (e107.org)\n";
  72		$diz .= " * Released under the terms and conditions of the\n";
  73		$diz .= " * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)\n";
  74		$diz .= " *\n";
  75		$diz .= " * Language File\n";
  76		$diz .= " *\n";
  77		$diz .= " * ".chr(36)."Source: ".str_replace(array(e_LANGUAGEDIR, e_PLUGIN), array(e_LANGUAGEDIR_ABS, e_PLUGIN_ABS), $writeit)." ".chr(36)."\n";
  78		$diz .= " * ".chr(36)."Revision: 1.0 ".chr(36)."\n";
  79		$diz .= " * ".chr(36)."Date: ".date("Y/m/d H:i:s")." ".chr(36)."\n";
  80		$diz .= " *  ".chr(36)."Author: ".USERNAME." ".chr(36)."\n";
  81		$diz .= " *\n";
  82		$diz .= "*".chr(47)."\n\n";
  83	}
  84	else
  85	{
  86		$diz = $old_kom;
  87	}
  88
  89	$input .= $diz;
  90	$message .= str_replace("\n","<br />",$diz);
  91
  92	for ($i=0; $i<count($_POST['newlang']); $i++)
  93	{
  94		$notdef_start = "";
  95		$notdef_end = "\n";
  96		$deflang = (MAGIC_QUOTES_GPC === TRUE) ? stripslashes($_POST['newlang'][$i]) : $_POST['newlang'][$i];
  97		$func = "define";
  98		$quote = chr(34);
  99
 100		if (strpos($_POST['newdef'][$i],"ndef++") !== FALSE )
 101		{
 102			$defvar = str_replace("ndef++","",$_POST['newdef'][$i]);
 103			$notdef_start = "if (!defined(".chr(34).$defvar.chr(34).")) {";
 104			$notdef_end = "}\n";
 105		}
 106		else
 107		{
 108			$defvar = $_POST['newdef'][$i];
 109		}
 110
 111		if($_POST['newdef'][$i] == "LC_ALL" && isset($_POST['root']))
 112		{
 113			$message .= $notdef_start.'setlocale('.htmlentities($defvar).','.$deflang.');<br />'.$notdef_end;
 114			$input .= $notdef_start."setlocale(".$defvar.",".$deflang.");".$notdef_end;
 115		}
 116		else
 117		{
 118			$message .= $notdef_start.$func.'('.$quote.htmlentities($defvar).$quote.',"'.$deflang.'");<br />'.$notdef_end;
 119			$input .= $notdef_start.$func."(".$quote.$defvar.$quote.", ".chr(34).$deflang.chr(34).");".$notdef_end;
 120		}
 121	}
 122
 123	$message .="<br />";
 124	$message .="</div>";
 125	$input .= "\n\n?>";
 126
 127	// Write to file.
 128	$fp = @fopen($writeit,"w");
 129	if(!@fwrite($fp, $input))
 130	{
 131		$caption = LAN_CHECK_PAGE_TITLE.' - '.LAN_ERROR;
 132		$message = '';
 133		$mes->addError(LAN_CHECK_17);
 134	}
 135	else
 136	{
 137		$caption = LAN_CHECK_PAGE_TITLE.' - '.LAN_CHECK_24;
 138		$mes->addSuccess(sprintXXX(str_replace("[x]", "%s", LAN_CHECK_23), basename($writeit)));
 139	}
 140	fclose($writeit);
 141
 142	$message .= "
 143	<form method='post' action='".e_SELF."' id='core-lancheck-save-file-form'>
 144	<div class='center'>
 145		".$frm->admin_button('language_sel', LAN_BACK)."
 146		".$frm->hidden('language', $lan)."
 147	</div>
 148	</form>";
 149	
 150
 151
 152	$ns->tablerender($caption, $mes->render().$message);
 153//	require_once(e_ADMIN."footer.php");
 154	exit;
 155}
 156
 157// ============================================================================
 158
 159// Edit the Language File.
 160
 161if($f != ""){
 162
 163	if (!$mode)
 164	{
 165		$dir1 =  e_BASE.$LANGUAGES_DIRECTORY."English/";
 166		$f1=$f;
 167		$dir2 =  e_BASE.$LANGUAGES_DIRECTORY.$lan."/";
 168		$f2=$f;
 169	}
 170	else
 171	{
 172		$fullpath_orig = $f;
 173		$fullpath_trans = str_replace("English",$lan,$f);
 174
 175		$f1 = basename($fullpath_orig);
 176		$f2 = basename($fullpath_trans);
 177		$dir1 = dirname($fullpath_orig)."/";
 178		$dir2 = dirname($fullpath_trans)."/";
 179	}
 180
 181	$lck->edit_lanfiles($dir1,$dir2,$f1,$f2);
 182
 183}
 184
 185// ===========================================================================
 186
 187$core_plugins = array(
 188	"alt_auth", "banner_menu", "blogcalendar_menu", "calendar_menu", "chatbox_menu",
 189	"clock_menu", "comment_menu", "content", 'download', "featurebox", "forum",
 190	"gsitemap", "links_page", "linkwords", "list_new", "log", "login_menu",
 191	"newforumposts_main", "newsfeed", "newsletter", "online", "other_news_menu",
 192	"pdf", "pm", "poll", "rss_menu", "search_menu", "siteinfo", "trackback",
 193	"tree_menu", "user_menu"
 194);
 195
 196$core_themes = array("bootstrap", $pref['sitetheme']);
 197
 198
 199if(isset($_POST['language_sel']) && isset($_POST['language']))
 200{
 201
 202	$text = $lck->check_core_lanfiles($_POST['language']).$lck->check_core_lanfiles($_POST['language'],"admin/");
 203
 204	$text .= "
 205		<fieldset id='core-lancheck-plugin'>
 206			<legend>".ADLAN_CL_7."</legend>
 207			<table class='table adminlist'>
 208				<colgroup>
 209					<col style='width: 25%' />
 210					<col style='width: 25%' />
 211					<col style='width: 40%' />
 212					<col style='width: 10%' />
 213				</colgroup>
 214				<thead>
 215					<tr>
 216						<th>".LAN_PLUGIN."</th>
 217						<th>".LAN_CHECK_16."</th>
 218						<th>".$_POST['language']."</th>
 219						<th class='center last'>".LAN_OPTIONS."</th>
 220					</tr>
 221				</thead>
 222				<tbody>
 223	";
 224
 225	foreach($core_plugins as $plugs)
 226	{
 227		if(is_readable(e_PLUGIN.$plugs))
 228		{
 229			$text .= $lck->check_lanfiles('P',$plugs,"English",$_POST['language']);
 230		}
 231	}
 232	$text .= "
 233				</tbody>
 234			</table>
 235		</fieldset>
 236	";
 237
 238	$text .= "
 239		<fieldset id='core-lancheck-theme'>
 240			<legend>".LAN_CHECK_22."</legend>
 241			<table class='table adminlist'>
 242				<colgroup>
 243					<col style='width: 25%' />
 244					<col style='width: 25%' />
 245					<col style='width: 40%' />
 246					<col style='width: 10%' />
 247				</colgroup>
 248				<thead>
 249					<tr>
 250						<th>".LAN_CHECK_21."</th>
 251						<th>".LAN_CHECK_16."</th>
 252						<th>".$_POST['language']."</th>
 253						<th class='center last'>".LAN_OPTIONS."</th>
 254					</tr>
 255				</thead>
 256				<tbody>
 257	";
 258	foreach($core_themes as $them)
 259	{
 260		if(is_readable(e_THEME.$them))
 261		{
 262			$text .= $lck->check_lanfiles('T',$them,"English",$_POST['language']);
 263		}
 264	}
 265	$text .= "
 266				</tbody>
 267			</table>
 268		</fieldset>
 269	";
 270	
 271	$mes = e107::getMessage();
 272	if($lck->error_count == 0)
 273	{
 274		e107::getConfig()->setPref('lancheck/'.$_POST['language'],1);
 275		e107::getConfig()->save(FALSE);
 276		$mes->addSuccess(LAN_CHECK_27.'<b>: '.$lck->error_count.'</b>');		
 277	}
 278	else  
 279	{
 280		$mes->addWarning(LAN_CHECK_27.'<b>: '.$lck->error_count.'</b>');
 281	}
 282	
 283
 284	$ns->tablerender(LAN_CHECK_25, $mes->render(). $text);
 285
 286
 287	
 288	require_once(e_ADMIN."footer.php");
 289	exit;
 290}
 291
 292
 293*/
 294
 295class lancheck
 296{
 297	
 298	var $core_plugins = array(
 299		"alt_auth","banner","blogcalendar_menu","calendar_menu","chatbox_menu",
 300		"clock_menu","comment_menu","download","faqs", "featurebox", "forum","gallery", "gsitemap","import", "links_page",
 301		"linkwords","list_new","log","login_menu","newforumposts_main","newsfeed",
 302		"news", "newsletter","online", "page",
 303		"pm","poll","rss_menu","search_menu","siteinfo","tagwords", "tinymce",
 304		"trackback","tree_menu","user_menu"
 305		);
 306		
 307	/*
 308	 * $core_plugins = array(
 309	"alt_auth", "banner_menu", "blogcalendar_menu", "calendar_menu", "chatbox_menu",
 310	"clock_menu", "comment_menu", "content", 'download', "featurebox", "forum",
 311	"gsitemap", "links_page", "linkwords", "list_new", "log", "login_menu",
 312	"newforumposts_main", "newsfeed", "newsletter", "online", "other_news_menu",
 313	"pdf", "pm", "poll", "rss_menu", "search_menu", "siteinfo", "trackback",
 314	"tree_menu", "user_menu"
 315);
 316	 */	
 317		
 318	var $core_themes = array("bootstrap"); 
 319			
 320	var $errorsOnly = FALSE;
 321	
 322	var $coreImage = array();
 323
 324	
 325	function init()
 326	{
 327		$ns = e107::getRender();
 328		$tp = e107::getParser();
 329		$pref = e107::getPref();
 330		
 331		// Check current theme also (but do NOT add to generated zip)
 332		$this->core_themes[] = $pref['sitetheme'];
 333		$this->core_themes = array_unique($this->core_themes);
 334		
 335		$acceptedLans = explode(",",e_LANLIST);
 336	
 337		
 338		if(!isset($_SESSION['lancheck-core-image']))
 339		{
 340			$core = array();	
 341
 342			$coredir = array('admin' => 'e107_admin', 'files' => 'e107_files', 'images' => 'e107_images', 'themes' => 'e107_themes', 'plugins' => 'e107_plugins', 'handlers' => 'e107_handlers', 'languages' => 'e107_languages', 'downloads' => 'e107_downloads', 'docs' => 'e107_docs');
 343			
 344			require_once(e_ADMIN."core_image.php");
 345			
 346			unset($core_image['e107_images'],$core_image['e107_files'],$core_image['e107_admin']);
 347			
 348			$_SESSION['lancheck-core-image'] = $core_image;	
 349		}
 350		
 351
 352			
 353		if(isset($_POST['language_sel'])) // Verify
 354		{
 355			
 356			$_SESSION['lancheck-errors-only'] 	= ($_POST['errorsonly']==1 ) ?  1 : 0;	
 357			$this->errorsOnly 					= ($_POST['errorsonly']==1) ?  TRUE : FALSE;
 358			$this->check_all();
 359			return TRUE;
 360		}
 361		
 362		// Write the language file.
 363		if(isset($_POST['submit']) && varsettrue($_POST['lan']) && in_array($_POST['lan'],$acceptedLans))
 364		{
 365			
 366			$this->write_lanfile($_POST['lan']);	
 367			return TRUE;	
 368		} 
 369		
 370		// Edit the Language File.
 371		if(varsettrue($_GET['f']) && varsettrue($_GET['lan']) && in_array($_GET['lan'],$acceptedLans))
 372		{
 373			
 374			if (!$_GET['mode'])
 375			{
 376				$dir1 =  e_LANGUAGEDIR."English/";
 377				$f1= $tp->toDB($_GET['f']);
 378				$dir2 =  e_LANGUAGEDIR.$_GET['lan']."/";
 379				$f2= $tp->toDB($_GET['f']);
 380			}
 381			else
 382			{
 383				$fullpath_orig = $tp->toDB($_GET['f']);
 384				$fullpath_trans = str_replace("English",$_GET['lan'],$tp->toDB($_GET['f']));
 385		
 386				$f1 = basename($fullpath_orig);
 387				$f2 = basename($fullpath_trans);
 388				$dir1 = dirname($fullpath_orig)."/";
 389				$dir2 = dirname($fullpath_trans)."/";
 390			}
 391			
 392			$this->edit_lanfiles($dir1,$dir2,$f1,$f2,$_GET['lan']);	
 393			return TRUE;	
 394		}	
 395		
 396		return FALSE;
 397	}
 398	
 399	
 400	function countFiles($array)
 401	{
 402		foreach($array as $k=>$val)
 403		{
 404			if(is_array($val))
 405			{
 406				$key = key($val);
 407				$this->coreImage[$key] = $val;
 408			}
 409			elseif($val)
 410			{
 411			//	$this->totalFiles++;		
 412			}	
 413			
 414		}	
 415	}
 416	
 417	
 418	
 419	function check_all($mode='render')
 420	{
 421		// global $ns,$tp;
 422		$mes = e107::getMessage();
 423		$ns = e107::getRender();
 424		$tp = e107::getParser();
 425		
 426			
 427		$_POST['language'] = key($_POST['language_sel']);
 428
 429		$_SESSION['lancheck'][$_POST['language']] = array();
 430		$_SESSION['lancheck'][$_POST['language']]['file']	= 0;
 431		$_SESSION['lancheck'][$_POST['language']]['def']	= 0;
 432		$_SESSION['lancheck'][$_POST['language']]['bom']	= 0;
 433		$_SESSION['lancheck'][$_POST['language']]['utf']	= 0;
 434		$_SESSION['lancheck'][$_POST['language']]['total']	= 0;
 435	
 436	
 437		$core_text 	= $this->check_core_lanfiles($_POST['language']);
 438		$core_admin = $this->check_core_lanfiles($_POST['language'],"admin/");
 439		$plug_text = "";
 440		$theme_text = "";
 441	
 442	
 443		// Plugins -------------
 444		$plug_header = "<table class='table table-striped'>
 445		<tr>
 446		<td class='fcaption'>".LAN_PLUGIN."</td>
 447		<td class='fcaption'>".LAN_CHECK_16."</td>
 448		<td class='fcaption'>".$_POST['language']."</td>
 449		<td class='fcaption'>".LAN_OPTIONS."</td></tr>";
 450	
 451		foreach($this->core_plugins as $plugs)
 452		{
 453			if(is_readable(e_PLUGIN.$plugs))
 454			{
 455				$plug_text .= $this->check_lanfiles('P',$plugs,"English",$_POST['language']);
 456			}
 457		}
 458		
 459		$plug_footer = "</table>";
 460	
 461		// Themes  -------------
 462		$theme_header = "<table class='table table-striped'>
 463		<tr>
 464		<td class='fcaption'>".LAN_CHECK_22."</td>
 465		<td class='fcaption'>".LAN_CHECK_16."</td>
 466		<td class='fcaption'>".$_POST['language']."</td>
 467		<td class='fcaption'>".LAN_OPTIONS."</td></tr>";
 468		foreach($this->core_themes as $them)
 469		{
 470			if(is_readable(e_THEME.$them))
 471			{
 472				$theme_text .= $this->check_lanfiles('T',$them,"English",$_POST['language']);
 473			}
 474		}
 475		$theme_footer = "</table>";
 476		
 477		// -------------------------
 478		
 479
 480		
 481		
 482		if($mode != 'render')
 483		{
 484			 return;
 485		}
 486	
 487		$message .= "
 488		<form id='lancheck' method='post' action='".e_ADMIN."language.php?tools'>
 489		<div>\n";
 490		
 491		$icon = ($_SESSION['lancheck'][$_POST['language']]['total']>0) ? ADMIN_FALSE_ICON : ADMIN_TRUE_ICON;	
 492		
 493		
 494		$errors_diz = (defsettrue('LAN_CHECK_23')) ? LAN_CHECK_23 : "Errors Found";
 495		
 496		$message .= $errors_diz.": ".$_SESSION['lancheck'][$_POST['language']]['total'];	
 497	
 498		$just_go_diz = (defsettrue('LAN_CHECK_20')) ? LAN_CHECK_20 : "Generate Language Pack";
 499		$lang_sel_diz = (defsettrue('LAN_CHECK_21')) ? LAN_CHECK_21 : "Verify Again";
 500		$lan_pleasewait = (defsettrue('LAN_PLEASEWAIT')) ?  $tp->toJS(LAN_PLEASEWAIT) : "Please Wait";
 501		
 502		$message .= "
 503		<br /><br />
 504		<input type='hidden' name='language' value='".$_POST['language']."' />
 505		<input type='hidden' name='errorsonly' value='".$_SESSION['lancheck-errors-only']."' />    
 506	    <input class='btn btn-primary' type='submit' name='ziplang[".$_POST['language']."]' value=\"".$just_go_diz."\" class='btn button' onclick=\"this.value = '".$lan_pleasewait."'\" />
 507	    <input type='submit' name='language_sel[".$_POST['language']."]' value=\"".$lang_sel_diz."\" class='btn button' />
 508		</div>
 509	    </form>
 510		";
 511		
 512//	print_a($_SESSION['lancheck'][$_POST['language']]);
 513
 514		$plug_text = ($plug_text) ? $plug_header.$plug_text.$plug_footer : "<div>".LAN_OK."</div>";	
 515		$theme_text = ($theme_text) ? $theme_header.$theme_text.$theme_footer : "<div>".LAN_OK."</div>";	
 516
 517		$mesStatus = ($_SESSION['lancheck'][$_POST['language']]['total']>0) ? E_MESSAGE_INFO : E_MESSAGE_SUCCESS;
 518			
 519		$mes->add($message, $mesStatus);	
 520			
 521	//	$ns -> tablerender(LAN_CHECK_24.": ".$_POST['language'],$message);
 522		
 523		echo $mes->render();
 524	
 525		$ns -> tablerender(LANG_LAN_21.SEP.$_POST['language'].SEP.LAN_CHECK_2, $core_text);
 526		$ns -> tablerender(LAN_CHECK_3.": ".$_POST['language']."/admin", $core_admin);
 527		$ns -> tablerender(ADLAN_CL_7, $plug_text);
 528		$ns -> tablerender(LAN_CHECK_25, $theme_text);	
 529		
 530	}
 531	
 532	
 533	function write_lanfile($lan='')
 534	{
 535		if(!$lan){ 	return; }
 536		
 537		global $ns;
 538		
 539		unset($input);
 540		$kom_start = chr(47)."*";
 541		$kom_end = "*".chr(47);
 542	
 543		if(varsettrue($_SESSION['lancheck-edit-file']))
 544		{
 545			$writeit = $_SESSION['lancheck-edit-file'];
 546		}
 547		else
 548		{
 549			return;	
 550		}
 551	
 552		$old_kom = "";
 553		$in_kom=0;
 554		
 555		if(is_readable($writeit)) // File Exists; 
 556		{
 557			$data = file($writeit);
 558			foreach($data as $line)
 559			{
 560		
 561				if (strpos($line,$kom_start) !== False && $old_kom == "")
 562				{
 563					$in_kom=1;
 564				}
 565				if ($in_kom) { $old_kom .= $line; }
 566				if (strpos($line,$kom_end) !== False && $in_kom) {$in_kom = 0;}
 567			}	
 568		}
 569		
 570	
 571	
 572		$message = "<div style='text-align:left'><br />";
 573		$input .= chr(60)."?php\n";
 574		if ($old_kom == "")
 575		{
 576			// create CVS compatible description.
 577			$diz = chr(47)."*\n";
 578			$diz .= "+---------------------------------------------------------------+\n";
 579			$diz .= "|        e107 website content management system ".$lan." Language File\n";
 580			$diz .= "|        Released under the terms and conditions of the\n";
 581			$diz .= "|        GNU General Public License (http://gnu.org).\n";
 582			$diz .= "|\n";
 583			$diz .= "|        ".chr(36)."URL: $writeit ".chr(36)."\n";
 584			$diz .= "|        ".chr(36)."Revision: 1.0 ".chr(36)."\n";
 585			$diz .= "|        ".chr(36)."Id: ".date("Y/m/d H:i:s")." ".chr(36)."\n";
 586			$diz .= "|        ".chr(36)."Author: ".USERNAME." ".chr(36)."\n";
 587			$diz .= "+---------------------------------------------------------------+\n";
 588			$diz .= "*".chr(47)."\n\n";
 589		}
 590		else
 591		{
 592			$diz = $old_kom;
 593		}
 594	
 595		$input .= $diz;
 596		$message .= str_replace("\n","<br />",$diz);
 597	
 598		for ($i=0; $i<count($_POST['newlang']); $i++)
 599		{
 600			$notdef_start = "";
 601			$notdef_end = "\n";
 602			$deflang = (MAGIC_QUOTES_GPC === TRUE) ? stripslashes($_POST['newlang'][$i]) : $_POST['newlang'][$i];
 603			$func = "define";
 604			$quote = chr(34);
 605	
 606			if (strpos($_POST['newdef'][$i],"ndef++") !== FALSE )
 607			{
 608				$defvar = str_replace("ndef++","",$_POST['newdef'][$i]);
 609				$notdef_start = "if (!defined(".chr(34).$defvar.chr(34).")) {";
 610				$notdef_end = "}\n";
 611			}
 612			else
 613			{
 614				$defvar = $_POST['newdef'][$i];
 615			}
 616	
 617			if($_POST['newdef'][$i] == "LC_ALL" && varsettrue($_SESSION['lancheck-edit-file']))
 618			{
 619				$message .= $notdef_start.'setlocale('.htmlentities($defvar).','.$deflang.');<br />'.$notdef_end;
 620				$input .= $notdef_start."setlocale(".$defvar.",".$deflang.");".$notdef_end;
 621			}
 622			else
 623			{
 624				$message .= $notdef_start.$func.'('.$quote.htmlentities($defvar).$quote.',"'.$deflang.'");<br />'.$notdef_end;
 625				$input .= $notdef_start.$func."(".$quote.$defvar.$quote.", ".chr(34).$deflang.chr(34).");".$notdef_end;
 626			}
 627		}
 628	
 629		$message .="<br />";
 630		$message .="</div>";
 631		$input .= "\n\n?>";
 632			//<?
 633		// Write to file.
 634		
 635		$writeit = str_replace("//","/",$writeit); // Quick Fix. 
 636		
 637		$fp = @fopen($writeit,"w");
 638		if(!@fwrite($fp, $input))
 639		{
 640			$caption = LAN_ERROR;
 641			$message = LAN_CHECK_17;
 642		}
 643		else
 644		{
 645			$caption = LAN_SAVED." <b>".$lan."/".$writeit."</b>";
 646		}
 647		fclose($fp);
 648	
 649		$message .= "<form method='post' action='".e_SELF."?tools' id='select_lang'>
 650		<div style='text-align:center'><br />";
 651		$message .= "<br /><br /><input class='btn' type='submit' name='language_sel[".$lan."]' value=\"".LAN_BACK."\" />
 652		</div></form>";
 653	
 654		unset($_SESSION['lancheck-edit-file']);
 655		$ns -> tablerender($caption, $message);
 656	}
 657	
 658		
 659		
 660		
 661	function check_core_lanfiles($checklan,$subdir='')
 662	{
 663		global $lanfiles,$_POST,$sql;
 664	
 665	//	$sql->db_Mark_Time('Start Get Core Lan Phrases English');
 666		$English = $this->get_comp_lan_phrases(e_LANGUAGEDIR."English/".$subdir,$checklan);
 667		
 668	//	$sql->db_Mark_Time('End Get Core Lan Phrases English');
 669		
 670		$check = $this->get_comp_lan_phrases(e_LANGUAGEDIR.$checklan."/".$subdir,$checklan);
 671		
 672	//	print_a($check);
 673	//	return;
 674		$text = "";
 675	
 676		$header = "<table class='table table-striped'>
 677		<tr>
 678		<th>".LAN_CHECK_16."</th>
 679		<th>".$_POST['language']." ".LAN_CHECK_26."</th>
 680		<th>".LAN_OPTIONS."</th></tr>";
 681	
 682		$keys = array_keys($English);
 683	
 684		sort($keys);
 685		$er = "";
 686	
 687		foreach($keys as $k)
 688		{
 689			if($k != "bom")
 690			{
 691				$lnk = $k;
 692				$k_check = str_replace("English",$checklan,$k);
 693				if(array_key_exists($k,$check))
 694				{
 695					// $text .= "<tr><td class='forumheader3' style='width:45%'>{$lnk}</td>";
 696					$subkeys = array_keys($English[$k]);
 697	
 698					$er="";
 699					$utf_error = "";
 700	
 701					$bomkey = str_replace(".php","",$k_check);
 702				//	$bom_error = ($check['bom'][$bomkey]) ? "<i>".LAN_CHECK_15."</i><br />" : ""; // illegal chars
 703	
 704					if($check['bom'][$bomkey])
 705					{
 706						$bom_error = "<i>".LAN_CHECK_15."</i><br />";
 707						$this->checkLog('bom',1);;	
 708					}
 709					else
 710					{
 711						$bom_error = "";	
 712					}
 713	
 714					foreach($subkeys as $sk)
 715					{
 716						if($utf_error == "" && !$this->is_utf8($check[$k][$sk]))
 717						{
 718							$utf_error = "<i>".LAN_CHECK_19."</i><br />";
 719							$this->checkLog('utf',1);
 720						}
 721	
 722						if($sk == "LC_ALL"){
 723							$check[$k][$sk] = str_replace(chr(34).chr(34),"",$check[$k][$sk]);
 724						}
 725					
 726						$er .= $this->check_lan_errors($English[$k],$check[$k],$sk);
 727					}
 728					
 729					if($this->errorsOnly == TRUE && !$er && !$utf_error && !$bom_error)
 730					{
 731						continue;		
 732					}
 733						
 734					$text .= "<tr><td class='forumheader3' style='width:45%'>{$lnk}</td>";
 735					$style = ($er) ? "forumheader2" : "forumheader3";
 736					$text .= "<td class='{$style}' style='width:50%'><div class='smalltext'>";
 737					$text .= $bom_error . $utf_error;
 738					$text .= (!$er && !$bom_error && !$utf_error) ? "<img src='".e_IMAGE."fileinspector/integrity_pass.png' alt='".LAN_OK."' />" : $er."<br />";
 739					$text .= "</div></td>";
 740				}
 741				else
 742				{
 743					$this->checkLog('file',1);
 744					$this->newFile(e_LANGUAGEDIR.$checklan."/".$subdir.$lnk,$checklan);
 745					$text .= "<tr>
 746					<td class='forumheader3' style='width:45%'>{$lnk}</td>
 747					<td class='forumheader' style='width:50%'>".LAN_CHECK_4."</td>"; // file missing.
 748				}
 749				// Leave in EDIT button for all entries - to allow re-translation of bad entries.
 750				$subpath = ($subdir!='') ? $subdir.$k : $k;
 751				$text .="<td class='center' style='width:5%'>
 752				<input class='btn btn-primary' type='button' style='width:60px' name='but_$i' value=\"".LAN_EDIT."\" onclick=\"window.location='".e_SELF."?f=".$subpath."&amp;lan=".$_POST['language']."'\" /> ";
 753				$text .="</td></tr>";
 754			}
 755		}
 756
 757		$footer = "</table>";
 758		
 759		if($text)
 760		{
 761			return $header.$text.$footer;	
 762		}
 763		else
 764		{
 765		 	return "<div>".LAN_OK."</div>";
 766		}
 767	}
 768
 769
 770
 771	
 772	function check_lan_errors($english,$translation,$def)
 773	{
 774		$eng_line = $english[$def];
 775		$trans_line = $translation[$def];
 776		
 777		// return $eng_line."<br />".$trans_line."<br /><br />";
 778			
 779		$error = array();
 780			
 781		if((!array_key_exists($def,$translation) && $eng_line != "") || (trim($trans_line) == "" && $eng_line != ""))
 782		{
 783			$this->checkLog('def',1);
 784			return $def.": ".LAN_CHECK_5."<br />";
 785		}
 786		
 787		if((strpos($eng_line,"[link=")!==FALSE && strpos($trans_line,"[link=")===FALSE) || (strpos($eng_line,"[b]")!==FALSE && strpos($trans_line,"[b]")===FALSE))
 788		{
 789			$error[] = $def. ": Missing bbcodes";
 790		}
 791		elseif((strpos($eng_line,"[")!==FALSE && strpos($trans_line,"[")===FALSE) || (strpos($eng_line,"]")!==FALSE && strpos($trans_line, "]")===FALSE))
 792		{
 793			$error[] = $def. ": Missing [ and/or ] character(s)";
 794		}
 795		
 796		if((strpos($eng_line,"--LINK--")!==FALSE && strpos($trans_line,"--LINK--")==FALSE))
 797		{
 798			$error[] = $def. ": Missing --LINK--";
 799		}
 800		
 801		if((strpos($eng_line,"e107.org")!==FALSE && strpos($trans_line,"e107.org")==FALSE))
 802		{
 803			$error[] = $def. ": Missing e107.org URL";
 804		}
 805		
 806		if((strpos($eng_line,"e107coders.org")!==FALSE && strpos($trans_line,"e107coders.org")==FALSE))
 807		{
 808			$error[] = $def. ": Missing e107coders.org URL";
 809		}
 810		
 811		if(strip_tags($eng_line) != $eng_line)
 812		{
 813			$stripped = strip_tags($trans_line);
 814					
 815			if(($stripped == $trans_line))
 816			{					
 817				// echo "<br /><br />".$def. "<br />".$stripped."<br />".$trans_line;
 818				$error[] = $def. ": Missing HTML tags" ; 		
 819			}
 820		}
 821		
 822		$this->checkLog('def',count($error));
 823	
 824		return ($error) ? implode("<br />",$error)."<br />" : "";
 825		
 826	}
 827	
 828	
 829	
 830	
 831	function checkLog($type='error',$count)
 832	{
 833		$_SESSION['lancheck'][$_POST['language']][$type] += $count;
 834		$_SESSION['lancheck'][$_POST['language']]['total'] += $count;
 835	}
 836	
 837	
 838	
 839	function get_lan_file_phrases($dir1,$dir2,$file1,$file2){
 840	
 841		$ret = array();
 842		$fname = $dir1.$file1;
 843		$type='orig';
 844	
 845		if(is_file($fname))
 846		{
 847			$data = file_get_contents($fname);
 848			$ret= $ret + $this->fill_phrases_array($data,$type);
 849			if(substr($data,0,5) != "<?php")
 850			{
 851				$key = str_replace(".php","",$fname);
 852				$ret['bom'][$key] = $fname;
 853			}
 854		}
 855	
 856		$fname = $dir2.$file2;
 857		$type='tran';
 858	
 859		if(is_file($fname))
 860		{
 861			$data = file_get_contents($fname);
 862			$ret=$ret + $this->fill_phrases_array($data,$type);
 863			if(substr($data,0,5) != "<?php")
 864			{
 865				$key = str_replace(".php","",$fname);
 866				$ret['bom'][$key] = $fname;
 867			}
 868		}
 869		elseif(substr($fname,-4) == ".php") 
 870		{
 871			file_put_contents($fname,"<?php\n\n?>");
 872		}
 873		
 874	
 875		return $ret;
 876	}
 877	
 878	
 879	
 880	
 881	function get_comp_lan_phrases($comp_dir,$lang,$depth=0)
 882	{
 883		if(!is_dir($comp_dir))
 884		{
 885			return array();
 886		}
 887		
 888		
 889		require_once(e_HANDLER."file_class.php");
 890		$fl = new e_file;
 891		$ret = array();
 892			
 893		if($lang_array = $fl->get_files($comp_dir, ".php$","standard",$depth)){
 894			sort($lang_array);
 895		}
 896	
 897
 898		$regexp = (strpos($comp_dir,e_LANGUAGEDIR) !== FALSE) ? "#.php#" : "#".$lang."#";
 899	
 900		foreach($lang_array as $f)
 901		{
 902			if(preg_match($regexp,$f['path'].$f['fname']) && is_file($f['path'].$f['fname']))
 903			{
 904				$allData = file_get_contents($f['path'].$f['fname']);
 905				$data = explode("\n",$allData);
 906				// $data = file($f['path'].$f['fname']);
 907				$relpath = str_replace($comp_dir,"",$f['path']);
 908				
 909				$key = str_replace(".php","",$relpath.$f['fname']);
 910				
 911				if(substr($data[0],0,5) != "<?php")
 912				{
 913					
 914					$ret['bom'][$key] = $f['fname'];
 915				}
 916						
 917				$end_of_file = 0;
 918							
 919				foreach($data as $line)
 920				{
 921					if($end_of_file == 1)
 922					{
 923						$ret['bom'][$key] = $f['fname'];
 924					}
 925											
 926					$line = trim($line);
 927					if($line == "?>")
 928					{
 929						$end_of_file = 1;  	
 930					}
 931				}
 932					
 933			
 934				
 935				if($f['path'].$f['fname'] == e_LANGUAGEDIR.$lang."/".$lang.".php")
 936				{
 937					$f['fname'] = "English.php";  // change the key for the main language file.
 938				}
 939	
 940				if($f['path'].$f['fname'] == e_LANGUAGEDIR.$lang."/".$lang."_custom.php")
 941				{
 942					$f['fname'] = "English_custom.php";  // change the key for the main language file.
 943				}
 944	
 945				$ret=$ret + $this->fill_phrases_array($allData,$relpath.$f['fname']);
 946	
 947			}
 948		}
 949	
 950		return $ret;
 951	
 952	}
 953	
 954	
 955	
 956	// for plugins and themes - checkes what kind of language files directory structure we have
 957	function check_lanfiles($mode,$comp_name,$base_lan="English",$target_lan){
 958		global $ns,$sql;
 959	
 960		$folder['P'] = e_PLUGIN.$comp_name;
 961		$folder['T'] = e_THEME.$comp_name;
 962		$comp_dir = $folder[$mode];
 963	
 964		$baselang 	= $this->get_comp_lan_phrases($comp_dir."/languages/","English",1);
 965		$check 		= $this->get_comp_lan_phrases($comp_dir."/languages/",$target_lan,1);	
 966	
 967		$text = "";
 968		$keys = array_keys($baselang);
 969		sort($keys);
 970	
 971		foreach($keys as $k)
 972		{
 973			
 974			if($k == 'bom')
 975			{
 976				continue;
 977			}
 978			
 979			$lnk = $k;
 980			//echo "klucz ".$k."<br />";
 981			$k_check = str_replace("English",$target_lan,$k);
 982			if(array_key_exists($k_check,$check))
 983			{
 984				
 985	
 986				$subkeys = array_keys($baselang[$k]);
 987				$er="";
 988				$utf_error = "";
 989	
 990				$bomkey = str_replace(".php","",$k_check);
 991				if($check['bom'][$bomkey])
 992				{
 993					$bom_error = "<i>".LAN_CHECK_15."</i><br />";
 994					$this->checkLog('bom',1); 
 995				}
 996				else
 997				{
 998					$bom_error = "";	
 999				}
1000			// 	$bom_error = ($check['bom'][$bomkey]) ? "<i>".LAN_CHECK_15."</i><br />" : ""; // illegal chars
1001			
1002				foreach($subkeys as $sk)
1003				{
1004					if($utf_error == "" && !$this->is_utf8($check[$k_check][$sk]))
1005					{
1006						$utf_error = "<i>".LAN_CHECK_19."</i><br />";
1007						$this->checkLog('utf',1);
1008					}
1009					
1010					/*
1011					if(!array_key_exists($sk,$check[$k_check]) || (trim($check[$k_check][$sk]) == "" && $baselang[$k][$sk] != ""))
1012					{
1013						$er .= ($er) ? "<br />" : "";
1014						$er .= $sk." ".LAN_CHECK_5;
1015					}
1016					*/
1017					$er .= $this->check_lan_errors($baselang[$k],$check[$k_check],$sk);
1018				}
1019	
1020				if($this->errorsOnly == TRUE && !$er && !$utf_error && !$bom_error)
1021				{
1022					continue;		
1023				}
1024	
1025				$text .= "<tr>
1026				<td class='forumheader3' style='width:20%'>".$comp_name."</td>
1027				<td class='forumheader3' style='width:25%'>".str_replace("English/","",$lnk)."</td>";
1028	
1029				$style = ($er) ? "forumheader2" : "forumheader3";
1030				$text .= "<td class='{$style}' style='width:50%'><div class='smalltext'>";
1031				$text .= $bom_error . $utf_error;
1032				$text .= (!$er && !$bom_error && !$utf_error) ? "<img src='".e_IMAGE."fileinspector/integrity_pass.png' alt='".LAN_OK."' />" : $er."<br />";
1033				$text .= "</div></td>";
1034			}
1035			else
1036			{
1037				$this->checkLog('file',1);
1038				$this->newFile($comp_dir."/languages/".$lnk,$target_lan);
1039				
1040				$text .= "<tr>
1041				<td class='forumheader3' style='width:20%'>".$comp_name."</td>
1042				<td class='forumheader3' style='width:25%'>".str_replace("English/","",$lnk)."</td>
1043				<td class='forumheader' style='width:50%'><span style='cursor:pointer' title=\"".str_replace("English",$target_lan,$lnk)."\">".LAN_CHECK_4."</span></td>";
1044			}
1045	
1046			$text .="<td class='forumheader3' style='width:5%;text-align:center'>
1047			<input class='btn btn-primary' type='button' style='width:60px' name='but_$i' value=\"".LAN_EDIT."\" onclick=\"window.location='".e_SELF."?f=".$comp_dir."/languages/".$lnk."&amp;lan=".$target_lan."&amp;mode={$mode}'\" /> ";
1048			$text .="</td></tr>";
1049		}
1050	
1051	
1052	
1053		// if (!$known) {$text = LAN_CHECK_18." : --> ".$fname." :: ".$dname;}
1054		return $text;
1055	}
1056	
1057	function newFile($lnk,$target_lan)
1058	{
1059		if($target_lan == 'English') 
1060		{
1061			return;	
1062		}
1063				
1064		$newfile = str_replace("English",$target_lan,$lnk);
1065		$dir = dirname($newfile);
1066				
1067		if($dir != '.' && !is_dir($dir))
1068		{
1069		//	echo "<br />dir: ".$dir;
1070			mkdir($dir,0755);	
1071		} 
1072		
1073		if(!file_exists($newfile))
1074		{
1075		//	echo "<br />file: ".$newfile;
1076			$data = chr(60)."?php\n\ndefine(\"EXAMPLE\",\"Generated Empty Language File\");";
1077			file_put_contents($newfile,$data);	
1078		}
1079	}
1080	
1081	
1082	function edit_lanfiles($dir1,$dir2,$f1,$f2,$lan)
1083	{
1084		if($lan == '')
1085		{
1086			echo "Language selection was lost. ";
1087			return;
1088		}
1089		
1090		$ns = e107::getRender();
1091		$sql = e107::getDb();
1092
1093	
1094		/*    echo "<br />dir1 = $dir1";
1095		echo "<br />file1 = $f1";
1096	
1097		echo "<br />dir2 = $dir2";
1098		echo "<br />file2 = $f2";*/
1099		
1100	
1101	
1102		if($dir2.$f2 == e_LANGUAGEDIR.$lan."/English.php") // it's a language config file.
1103		{
1104			$f2 = $lan.".php";
1105			$root_file = e_LANGUAGEDIR.$lan."/".$lan.".php";
1106		}
1107		else
1108		{
1109			$root_file = $dir2.$f2;
1110		}
1111	
1112		if($dir2.$f2 == e_LANGUAGEDIR.$lan."/English_custom.php") // it's a language config file.
1113		{
1114			$f2 = $lan."_custom.php";
1115			$root_file = e_LANGUAGEDIR.$lan."/".$lan."_custom.php";
1116		}
1117	
1118	
1119		$writable = (is_writable($dir2)) ? TRUE : FALSE;
1120		$trans = $this->get_lan_file_phrases($dir1,$dir2,$f1,$f2);
1121		$keys = array_keys($trans);
1122		sort($keys);
1123	
1124		$text = "<div style='text-align:center'>
1125		<form method='post' action='".e_SELF."?".e_QUERY."' id='transform'>
1126		<table class='table table-striped'>
1127		<thead>
1128		<tr>
1129			<th>LAN</th>
1130			<th>English</th>
1131			<th>".$lan."</th>
1132		</tr>";
1133	
1134		$subkeys = array_keys($trans['orig']);
1135		foreach($subkeys as $sk)
1136		{
1137			$rowamount = round(strlen($trans['orig'][$sk])/34)+1;
1138			$hglt1=""; $hglt2="";
1139			if ($trans['tran'][$sk] == "" && $trans['orig'][$sk]!="") {
1140				$hglt1="<span style='font-style:italic;font-weight:bold;color:red'>";
1141				$hglt2="</span>";
1142			}
1143			$text .="<tr>
1144			<td style='width:10%;vertical-align:top'>".$hglt1.htmlentities($sk).$hglt2."</td>
1145			<td style='width:40%;vertical-align:top'>".htmlentities(str_replace("ndef++","",$trans['orig'][$sk])) ."</td>";
1146			$text .= "<td class='forumheader3' style='width:50%;vertical-align:top'>";
1147			$text .= ($writable) ? "<textarea  class='input-xxlarge' name='newlang[]' rows='$rowamount' cols='45' style='height:100%'>" : "";
1148			$text .= str_replace("ndef++","",$trans['tran'][$sk]);
1149			$text .= ($writable) ? "</textarea>" : "";
1150			//echo "orig --> ".$trans['orig'][$sk]."<br />";
1151			if (strpos($trans['orig'][$sk],"ndef++") !== False)
1152			{
1153				//echo "+orig --> ".$trans['orig'][$sk]." <> ".strpos($trans['orig'][$sk],"ndef++")."<br />";
1154				$text .= "<input type='hidden' name='newdef[]' value='ndef++".$sk."' />";
1155			}
1156			else
1157			{
1158				$text .= "<input type='hidden' name='newdef[]' value='".$sk."' />";
1159			}
1160			$text .="</td></tr>";
1161		}
1162	
1163		unset($_SESSION['lancheck-edit-file']);
1164	
1165		//Check if directory is writable
1166		
1167		$text .= "</table>";
1168		
1169		if($writable)
1170		{
1171			$text .="<div class='buttons-bar center'>
1172			<input type='hidden' name='lan' value='{$lan}' />
1173			<input class='btn btn-primary' type='submit' name='submit' value=\"".LAN_SAVE." ".str_replace($dir2,"",$root_file)." \" />
1174			</div>";
1175	
1176			if($root_file)
1177			{			
1178				$_SESSION['lancheck-edit-file'] = $root_file;
1179			}
1180	
1181			
1182		}
1183	
1184		$text .= "
1185		
1186		</form>
1187		</div>";
1188	
1189		$text .= "<form method='post' action='".e_SELF."?tools' id='select_lang'>
1190		<div style='text-align:center'><br />";
1191		$text .= (!$writable) ? "<br />".$dir2.$f2.LAN_NOTWRITABLE : "";
1192		$text .= "<br /><br /><input class='btn' type='submit' name='language_sel[{$lan}]' value=\"".LAN_BACK."\" />
1193		</div></form>";
1194	
1195	
1196		$caption = LANG_LAN_21.SEP.$lan.SEP.LAN_CHECK_2.SEP.LAN_EDIT.SEP.str_replace("../","",$dir2.$f2);
1197		
1198		$ns->tablerender($caption, $text);
1199
1200	
1201	}
1202	
1203	
1204	
1205	function fill_phrases_array($data,$type)
1206	{	
1207		$retloc = array();
1208		
1209		if(preg_match_all('/(\/\*[\s\S]*?\*\/)/i',$data, $multiComment))
1210		{
1211			$data = str_replace($multiComment[1],'',$data);	// strip multi-line comments. 	
1212		}
1213					
1214		if(preg_match('/^\s*?setlocale\s*?\(\s*?([\w]+)\s*?,\s*?(.+)\s*?\)\s*?;/im',$data,$locale)) // check for setlocale();
1215		{
1216			$retloc[$type][$locale[1]]= $locale[2];	
1217		}
1218				
1219		if(preg_match_all('/^\s*?define\s*?\(\s*?(\'|\")([\w]+)(\'|\")\s*?,\s*?(\'|\")([\s\S]*?)\s*?(\'|\")\s*?\)\s*?;/im',$data,$matches))
1220		{
1221			$def = $matches[2];
1222			$values = $matches[5];	
1223	
1224			foreach($def as $k=>$d)
1225			{
1226				$retloc[$type][$d]= $values[$k];
1227			}	
1228		}
1229			
1230		return $retloc;
1231		
1232		/*
1233		echo "<h2>Raw Data ".$type."</h2><pre>";
1234		echo htmlentities($data);
1235		echo "</pre>";	
1236	
1237		*/
1238			
1239	}
1240	
1241	
1242	
1243	//--------------------------------------------------------------------
1244	
1245	
1246	function is_utf8($str) {
1247		/*
1248		* @see http://hsivonen.iki.fi/php-utf8/   validation.php
1249		*/
1250		if(strtolower(CHARSET) != "utf-8" || $str == "")
1251		{
1252			return TRUE;
1253		}
1254	
1255		return (preg_match('/^.{1}/us',$str,$ar) == 1);
1256	}
1257	
1258	
1259}
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271/*
1272
1273class lancheck_old
1274{
1275	var $error_count=0;
1276	
1277	function check_core_lanfiles($checklan,$subdir=''){
1278		$frm = e107::getForm();
1279		
1280		$English = $this->get_comp_lan_phrases(e_LANGUAGEDIR."English/".$subdir,$checklan);
1281		$check = $this->get_comp_lan_phrases(e_LANGUAGEDIR.$checklan."/".$subdir,$checklan);
1282		$legend_txt = LAN_CHECK_3.": ".$_POST['language']."/".$subdir;
1283		$fieldset_id = $subdir ? str_replace('/', '', $_POST['language'])."-".str_replace('/', '', $subdir) : str_replace('/', '', $_POST['language']);
1284		$text .= "
1285			<fieldset id='core-lancheck-{$fieldset_id}'>
1286				<legend>{$legend_txt}</legend>
1287				<table class='table adminlist'>
1288					<colgroup>
1289						<col style='width: 50%' />
1290						<col style='width: 40%' />
1291						<col style='width: 10%' />
1292					</colgroup>
1293					<thead>
1294						<tr>
1295							<th>".LAN_CHECK_16."</th>
1296							<th>".$_POST['language'].' '.LAN_CHECK_20."</th>
1297							<th class='center last'>".LAN_OPTIONS."</th>
1298						</tr>
1299					</thead>
1300					<tbody>
1301		";
1302	
1303		$keys = array_keys($English);
1304	
1305		sort($keys);
1306	
1307		$i = 0;
1308		foreach($keys as $k)
1309		{
1310			if($k != "bom")
1311			{
1312				$lnk = $k;
1313				$k_check = str_replace("English",$checklan,$k);
1314				$text .= "
1315						<tr>
1316				";
1317				
1318				if(array_key_exists($k,$check))
1319				{
1320					$text .= "
1321							<td>{$lnk}</td>
1322					";
1323					$subkeys = array_keys($English[$k]);
1324	
1325					$er="";
1326					$utf_error = "";
1327	
1328					$bomkey = str_replace(".php","",$k_check);
1329					$bom_error = ($check['bom'][$bomkey]) ? "<span class='error'><em>".str_replace("[php]", "<?php", LAN_CHECK_15)."</em></span><br />" : ""; // illegal chars
1330	
1331					foreach($subkeys as $sk)
1332					{
1333						if($utf_error == "" && !$this->is_utf8($check[$k][$sk]))
1334						{
1335							$utf_error = "<span class='error'><em>".LAN_CHECK_19."</em></span><br />";
1336						}
1337	
1338						if($sk == "LC_ALL"){
1339							$check[$k][$sk] = str_replace(chr(34).chr(34),"",$check[$k][$sk]);
1340						}
1341	
1342						if((!array_key_exists($sk,$check[$k]) && $English[$k][$sk] != "") || (trim($check[$k][$sk]) == "" && $English[$k][$sk] != ""))
1343						{
1344	
1345							$er .= ($er) ? "<br />" : "";
1346							$er .= $sk." ".LAN_CHECK_5;
1347							$this->error_count++;
1348						}
1349					}
1350	
1351					$style = ($er) ? "warning" : "success";
1352					$text .= "
1353							<td class='{$style}' style='width:50%'>
1354								<div class='smalltext'>
1355					";
1356					$text .= $bom_error . $utf_error;
1357					if(!$er && !$bom_error && !$utf_error)
1358					{
1359						$text .= LAN_OK;
1360					}
1361					else
1362					{
1363						$text .= $er."<br />";
1364						$this->error_count++;
1365					}
1366					$text .= "
1367								</div>
1368							</td>
1369					";
1370				}
1371				else
1372				{
1373				// file missing
1374					$text .= "
1375							<td>{$lnk}</td>
1376							<td><span class='error'>".LAN_CHECK_4."</span></td>
1377					";
1378					$this->error_count++;
1379				}
1380				
1381				// Leave in EDIT button for all entries - to allow re-translation of bad entries.
1382				$subpath = ($subdir!='') ? $subdir.$k : $k;
1383				$text .= "
1384							<td class='center'>
1385								".$frm->admin_button('but-corelan-'.str_replace(array('/', '\\'), '-', $subdir).$i, LAN_EDIT, 'edit', '', array('other' => "onclick=\"window.location='".e_SELF."?".$subpath."|".$_POST['language']."'\""))."
1386				";
1387				$text .= "
1388							</td>
1389						</tr>
1390				";
1391			}
1392			$i++;
1393		}
1394		$text .= "
1395					</tbody>
1396				</table>
1397			</fieldset>
1398		";
1399		
1400		return $text;
1401	}
1402	
1403	
1404	function get_lan_file_phrases($dir1,$dir2,$file1,$file2){
1405	
1406		$ret = array();
1407		$fname = $dir1.$file1;
1408		$type='orig';
1409	
1410		if(is_file($fname))
1411		{
1412			$data = file($fname);
1413			$ret=$ret + $this->fill_phrases_array($data,$type);
1414			if(substr($data[0],0,5) != "<?php")
1415			{
1416				$key = str_replace(".php","",$fname);
1417				$ret['bom'][$key] = $fname;
1418			}
1419		}
1420	
1421		$fname = $dir2.$file2;
1422		$type='tran';
1423	
1424		if(is_file($fname))
1425		{
1426			$data = file($fname);
1427			$ret=$ret + $this->fill_phrases_array($data,$type);
1428			if(substr($data[0],0,5) != "<?php")
1429			{
1430				$key = str_replace(".php","",$fname);
1431				$ret['bom'][$key] = $fname;
1432			}
1433		}
1434		return $ret;
1435	}
1436	
1437	
1438	function get_comp_lan_phrases($comp_dir,$lang,$depth=0)
1439	{
1440		$fl = e107::getFile();
1441
1442		$ret = array();
1443	
1444		if($lang_array = $fl->get_files($comp_dir, '\.php','standard',$depth)){
1445			sort($lang_array);
1446		}
1447	
1448		$regexp = (strpos($comp_dir,e_LANGUAGEDIR) !== FALSE) ? "#.php#" : "#".$lang."#";
1449	
1450		foreach($lang_array as $f)
1451		{
1452			if(preg_match($regexp,$f['path'].$f['fname']))
1453			{
1454				
1455				$data = file($f['path'].$f['fname']);
1456				$relpath = str_replace($comp_dir,"",$f['path']);
1457				if(substr($data[0],0,5) != "<?php")
1458				{
1459					$key = str_replace(".php","",$relpath.$f['fname']);
1460					$ret['bom'][$key] = $f['fname'];
1461				}
1462				if($f['path'].$f['fname'] == e_LANGUAGEDIR.$lang."/".$lang.".php")
1463				{
1464					$f['fname'] = "English.php";  // change the key for the main language file.
1465				}
1466	
1467				if($f['path'].$f['fname'] == e_LANGUAGEDIR.$lang."/".$lang."_custom.php")
1468				{
1469					$f['fname'] = "English_custom.php";  // change the key for the main language file.
1470				}
1471	
1472				$ret=$ret + $this->fill_phrases_array($data,$relpath.$f['fname']);
1473	
1474			}
1475		}
1476	
1477		return $ret;
1478	
1479	}
1480	
1481	// for plugins and themes - checks what kind of language files directory structure we have
1482	function check_lanfiles($mode, $comp_name, $base_lan="English", $target_lan)
1483	{
1484		$frm = e107::getForm();
1485		
1486		$folder['P'] = e_PLUGIN.$comp_name;
1487		$folder['T'] = e_THEME.$comp_name;
1488		$comp_dir = $folder[$mode];
1489	
1490		$baselang = $this->get_comp_lan_phrases($comp_dir."/languages/","English",1);
1491		$check = $this->get_comp_lan_phrases($comp_dir."/languages/",$target_lan,1);
1492	
1493		$text = "";
1494		$keys = array_keys($baselang);
1495		sort($keys);
1496	
1497		$i = 0;
1498		foreach($keys as $k)
1499		{
1500			$lnk = $k;
1501			//echo "klucz ".$k."<br />";
1502			$k_check = str_replace("English",$target_lan,$k);
1503			$text .= "
1504				<tr>
1505			";
1506			if(array_key_exists($k_check,$check))
1507			{
1508				$text .= "
1509						<td>".$comp_name."</td>
1510						<td>".str_replace("English/","",$lnk)."</td>
1511				";
1512	
1513				$subkeys = array_keys($baselang[$k]);
1514				$er = "";
1515				$utf_error = "";
1516	
1517				$bomkey = str_replace(".php","",$k_check);
1518				$bom_error = ($check['bom'][$bomkey]) ? "<span class='error'><em>".LAN_CHECK_15."</em></span><br />" : ""; // illegal chars
1519	
1520				foreach($subkeys as $sk)
1521				{
1522					if($utf_error == "" && !$this->is_utf8($check[$k_check][$sk]))
1523					{
1524						$utf_error = "<span class='error'><em>".LAN_CHECK_19."</em></span><br />";
1525					}
1526	
1527					if(!array_key_exists($sk,$check[$k_check]) || (trim($check[$k_check][$sk]) == "" && $baselang[$k][$sk] != ""))
1528					{
1529						$er .= ($er) ? "<br />" : "";
1530						$er .= $sk." ".LAN_CHECK_5;
1531						$this->error_count++;
1532					}
1533				}
1534	
1535				$style = ($er) ? "warning" : "success";
1536				$text .= "
1537					<td class='{$style}' style='width:50%'>
1538						<div class='smalltext'>
1539				";
1540				$text .= $bom_error . $utf_error;
1541				$text .= (!$er && !$bom_error && !$utf_error) ? LAN_OK : $er."<br />";
1542				$text .= "
1543						</div>
1544					</td>
1545				";
1546			}
1547			else
1548			{
1549				$text .= "
1550					<td>".$comp_name."</td>
1551					<td>".str_replace("English/","",$lnk)."</td>
1552					<td><span class='error' style='cursor:pointer' title='".str_replace("English",$target_lan,$lnk)."'>".LAN_CHECK_4."</span></td>
1553				";
1554				$this->error_count++;
1555			}
1556	
1557			$text .="
1558					<td class='center'>
1559						".$frm->admin_button('but-corelan-'.str_replace(array('/', '\\'), '-', $comp_dir).$i, LAN_EDIT, 'edit', '', array('other'=> "onclick=\"window.location='".e_SELF."?".$comp_dir."/languages/".$lnk."|".$target_lan."|file'\""))."
1560			";
1561			$text .="
1562					</td>
1563				</tr>
1564			";
1565			$i++;
1566		}
1567	
1568		return $text;
1569	}
1570	
1571	function edit_lanfiles($dir1,$dir2,$f1,$f2){
1572		global $e107, $lan;
1573		$mes = e107::getMessage();
1574		$ns = e107::getRender();
1575	
1576		//   echo "<br />dir1 = $dir1";
1577		//echo "<br />file1 = $f1";
1578	
1579		//echo "<br />dir2 = $dir2";
1580		//echo "<br />file2 = $f2";
1581	
1582		if($dir2.$f2 == e_LANGUAGEDIR.$lan."/English.php") // it's a language config file.
1583		{
1584			$f2 = $lan.".php";
1585			$root_file = e_LANGUAGEDIR.$lan."/".$lan.".php";
1586		}
1587		else
1588		{
1589			$root_file = $dir2.$f2;
1590		}
1591	
1592		if($dir2.$f2 == e_LANGUAGEDIR.$lan."/English_custom.php") // it's a language config file.
1593		{
1594			$f2 = $lan."_custom.php";
1595			$root_file = e_LANGUAGEDIR.$lan."/".$lan."_custom.php";
1596		}
1597	
1598	
1599		$writable = (is_writable($dir2)) ? TRUE : FALSE;
1600		$trans = $this->get_lan_file_phrases($dir1,$dir2,$f1,$f2);
1601		$keys = array_keys($trans);
1602		sort($keys);
1603	
1604		$text = "
1605			<form method='post' action='".e_SELF."?".e_QUERY."' id='transform'>
1606				<fieldset id='core-lancheck-edit'>
1607					<legend>".LAN_CHECK_3." ".str_replace(array(e_PLUGIN, e_LANGUAGEDIR), array(e_PLUGIN_ABS, e_LANGUAGEDIR_ABS), $dir2)."{$f2} -&gt; {$lan}</legend>
1608					<table class='table table-striped'>
1609						<colgroup>
1610							<col style='width: 20%' />
1611							<col style='width: 40%' />
1612							<col style='width: 40%' />
1613						</colgroup>
1614						<thead>
1615							<tr>
1616								<th>&nbsp;</th>
1617								<th>".LAN_CHECK_16."</th>
1618								<th class='last'>Translate to ".$lan."</th>
1619							</tr>
1620						</thead>
1621						<tbody>
1622		";
1623	
1624		$subkeys = array_keys($trans['orig']);
1625		foreach($subkeys as $sk)
1626		{
1627			$rowamount = round(strlen($trans['orig'][$sk])/34)+1;
1628			$hglt1=""; $hglt2="";
1629			if ($trans['tran'][$sk] == "" && $trans['orig'][$sk]!="") {
1630				$hglt1="<span class='error'>";
1631				$hglt2="</span>";
1632			}
1633			$text .= "
1634							<tr>
1635								<td>".$hglt1.htmlentities($sk).$hglt2."</td>
1636								<td>".htmlentities(str_replace("ndef++", "", $trans['orig'][$sk])) ."</td>
1637								<td>
1638									".(($writable) ? "<textarea  class='input-xxlarge' name='newlang[]' rows='{$rowamount}' cols='45'>" : "")
1639									.str_replace("ndef++","",$trans['tran'][$sk])
1640									.(($writable) ? "</textarea>" : "")."
1641			";
1642			//echo "orig --> ".$trans['orig'][$sk]."<br />";
1643			if (strpos($trans['orig'][$sk],"ndef++") !== False)
1644			{
1645				//echo "+orig --> ".$trans['orig'][$sk]." <> ".strpos($trans['orig'][$sk],"ndef++")."<br />";
1646				$text .= "
1647									<input type='hidden' name='newdef[]' value='ndef++".$sk."' />
1648				";
1649			}
1650			else
1651			{
1652				$text .= "
1653									<input type='hidden' name='newdef[]' value='".$sk."' />
1654				";
1655			}
1656			$text .="
1657								</td>
1658							</tr>
1659			";
1660		}
1661		$text .= "
1662						</tbody>
1663					</table>
1664		";
1665		//Check if directory is writable
1666		if($writable)
1667		{
1668			//FIXME  place of LAN_SAVE
1669			$text .="
1670					<div class='buttons-bar center'>
1671						<button class='update btn btn-success' type='submit' name='submit' value='sprintXXf'><span>".LAN_SAVE." ".str_replace($dir2, "", $root_file)."</span></button>
1672						".(($root_file) ? "<input type='hidden' name='root' value='".$root_file."' />" : "")."
1673					</div>
1674			";
1675		}
1676	
1677		$text .= "
1678				</fieldset>
1679			</form>
1680		";
1681	
1682		$text .= "
1683			<form method='post' action='".e_SELF."' id='select_lang'>
1684				<div style='text-align:center'>
1685					".((!$writable) ? $dir2.$f2.LAN_NOTWRITABLE : "")."
1686					<br />
1687					<button class='submit btn' type='submit' name='language_sel' value='no-value'><span>".LAN_BACK."</span></button>
1688					<input type='hidden' name='language' value='$lan' />
1689				</div>
1690			</form>
1691		";
1692	
1693		$ns->tablerender(LAN_CHECK_PAGE_TITLE.' - '.LAN_CHECK_24, $text);
1694		require_once(e_ADMIN."footer.php");
1695		exit;
1696	
1697	}
1698	
1699	function fill_phrases_array($data,$type) {
1700	
1701		$retloc = array();
1702	
1703		foreach($data as $line){
1704			//echo "line--> ".$line."<br />";
1705			if (strpos($line,"define(") !== FALSE && strpos($line,");") === FALSE)
1706			{
1707				$indef=1;
1708				$bigline="";
1709				// echo "big1 -->".$line."<br />";
1710			}
1711			if ($indef)
1712			{
1713				$bigline.=str_replace("\n","",$line);
1714				// echo "big2 -->".$line."<br />";
1715			}
1716			if (strpos($line,"define(") === FALSE && strpos($line,");") !== FALSE)
1717			{
1718				$indef=0;
1719				$we_have_bigline=1;
1720				// echo "big3 -->".$line."<br />";
1721			}
1722	
1723			if(strpos($line,"setlocale(") !== FALSE)
1724			{
1725				$indef=1;
1726				$we_have_bigline=0;
1727			}
1728	
1729			if ((strpos($line,"define(") !== FALSE && strpos($line,");") !== FALSE && substr(ltrim($line),0,2) != "//") || $we_have_bigline || strpos($line,"setlocale(") !== FALSE)
1730			{
1731	
1732				if ($we_have_bigline)
1733				{
1734					$we_have_bigline=0;
1735					$line=$bigline;
1736					// echo "big -->".$line."<br />";
1737				}
1738				$ndef = "";
1739				//echo "_ndefline -->".$line."<br />";
1740				if (strpos($line,"defined(") !== FALSE )
1741				{
1742					$ndef = "ndef++";
1743					$line = substr($line,strpos($line,"define("));
1744				}
1745	
1746				if(strpos($line,"setlocale(") !== FALSE)
1747				{
1748					$pos = substr(strstr($line,","),1);
1749					$rep = array(");","\n",'""');
1750					$val = str_replace($rep,"",$pos);
1751					$retloc[$type]['LC_ALL']= $val;
1752	//				$retloc['orig']['LC_ALL']= "'en'";
1753				}
1754				else
1755				{
1756	
1757					//echo "ndefline: ".$line."<br />";
1758					if(preg_match("#\"(.*?)\".*?\"(.*)\"#",$line,$matches) ||
1759					preg_match("#\'(.*?)\'.*?\"(.*)\"#",$line,$matches) ||
1760					preg_match("#\"(.*?)\".*?\'(.*)\'#",$line,$matches) ||
1761					preg_match("#\'(.*?)\'.*?\'(.*)\'#",$line,$matches) ||
1762					preg_match("#\((.*?)\,.*?\"(.*)\"#",$line,$matches) ||
1763					preg_match("#\((.*?)\,.*?\'(.*)\'#",$line,$matches))
1764					{
1765						//echo "get_lan -->".$matches[1]." :: ".$ndef.$matches[2]."<br />";
1766						if(!isset($retloc[$type][$matches[1]]))
1767						{
1768							$retloc[$type][$matches[1]]= $ndef.$matches[2];
1769						}
1770					}
1771				}
1772			}
1773		}
1774	
1775		return $retloc;
1776	}
1777	
1778	//--------------------------------------------------------------------
1779	
1780	
1781	function is_utf8($str) {
1782		// @see http://hsivonen.iki.fi/php-utf8/   validation.php
1783	
1784	//@TODO: always TRUE
1785	//	if(strtolower(CHARSET) != "utf-8" || $str == "")
1786		{
1787			return TRUE;
1788		}
1789	
1790		return (preg_match('/^.{1}/us',$str,$ar) == 1);
1791	}
1792	
1793}
1794
1795*/
1796/*
1797function lancheck_adminmenu() 
1798{
1799
1800	include_lan(e_LANGUAGEDIR.e_LANGUAGE."/admin/lan_language.php");
1801
1802	global $action;
1803	$pref = e107::getPref();
1804	
1805	if ($action == "") {
1806		$action = "tools";
1807	}
1808
1809	if($action == "modify")
1810	{
1811		$action = "db";
1812	}
1813	$var['main']['text'] = LAN_PREFS;
1814	$var['main']['link'] = e_ADMIN_ABS."language.php";
1815
1816	if(isset($pref['multilanguage']) && $pref['multilanguage']){
1817		$var['db']['text'] = LANG_LAN_03;
1818		$var['db']['link'] = e_ADMIN_ABS."language.php?db";
1819	}
1820
1821	$var['tools']['text'] = ADLAN_CL_6;
1822	$var['tools']['link'] = e_ADMIN_ABS."language.php?tools";
1823
1824
1825	e107::getNav()->admin(ADLAN_132, $action, $var);
1826}
1827
1828	$ns->tablerender(LAN_CHECK_PAGE_TITLE.' - '.LAN_CHECK_1, LAN_CHECK_26);
1829	require_once(e_ADMIN."footer.php");
1830*/
1831
1832