<?php

/*

  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  
  Freeway eCommerce from ZacWare
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
	require('includes/application_top.php');
	// current file selected to edit
	$lang_file=(isset($HTTP_GET_VARS["select_file"])?$HTTP_GET_VARS["select_file"]:'index.php');
	// current language selected
	$selected_language=(isset($HTTP_GET_VARS["selected_language"])?$HTTP_GET_VARS["selected_language"]:'english');
	$view=(isset($HTTP_GET_VARS["view"])?$HTTP_GET_VARS["view"]:'A');
	if($view=='A')
		$path=DIR_FS_CATALOG.'admin/includes/languages/';
	else
		$path=DIR_FS_CATALOG.'includes/languages/';	
	// current action to be done
	$get_action=(isset($HTTP_GET_VARS["get_action"])?$HTTP_GET_VARS["get_action"]:'');
	$post_action=(isset($HTTP_POST_VARS["post_action"])?$HTTP_POST_VARS["post_action"]:'');
	// get all the installed languages
	$languages = tep_get_languages();
	// update the changes by post_action
	if ($post_action=="save"){
		$data_lines=$HTTP_POST_VARS["data_lines"];
		$language_content=$HTTP_POST_VARS["language_content"];
	  for($i=0;$i<sizeof($languages);$i++){
	    $key=$languages[$i]['directory'];		
		 for ($icnt=1;$icnt<=count($data_lines[$i]);$icnt++){
			// is the normal data or constant data to be written
			if (is_array($data_lines[$i][$icnt])){																
				$data_lines[$i][$icnt]["key"]=stripslashes($data_lines[$i][$icnt]["key"]);								
				reset($language_content);															
					$value=stripslashes($data_lines[$i][$icnt][$data_lines[$i][$icnt]['key']]);					
					$language_content[$key].="define(\"" . $data_lines[$i][$icnt]["key"] . "\",\"" . $value . "\");".chr(13) . chr(10);
			} else {				
				reset($language_content);
				while(list($key,)=each($language_content)){
					//$write_data=addslashes(stripslashes($data_lines[$icnt]));
					$language_content[$key].=stripslashes($data_lines[$i][$icnt]);
				}
			}
		}
	  } 
	  // replacement for return key
	  $error=false;
	  reset($language_content);
	  while(list($key,$write_data)=each($language_content)){
			//$write_data=str_replace("<e>",chr(13) . chr(10),$write_data);
			if (substr($lang_file,0,strpos($lang_file,"."))==$selected_language){
				$update_file=$path  . $key . ".php";
			} else {
				$update_file=$path . $key . "/" . $lang_file;
			}						
			$handle=@fopen($update_file,"w");
			if($write_data){
				$write_data_ini='<?php 
 	/*
 	 osCommerce, Open Source E-Commerce Solutions
	 http://www.oscommerce.com
							
	 Copyright (c) 2003 osCommerce
							  
	 osCommRes, Services Online
	 http://www.oscommres.com
							
	 Copyright (c) 2005 osCommRes
							
	 Released under the GNU General Public License
	*/'.chr(13) . chr(10);
				$write_data=$write_data_ini.$write_data.chr(13) . chr(10).' ?>';
			}
			// error opening file for update
			if (!$handle){
				$messageStack->add(sprintf(ERROR_UPDATING_FILE, $lang_file), 'error');
				$error=true;
			} else {
				fwrite($handle,$write_data,strlen($write_data));
				fclose($handle);
			}
		}
		
		if (!error) $messageStack->add(sprintf(SUCCESS_FILE_UPDATED, $file), 'success');
		tep_redirect(tep_href_link(FILENAME_LANGUAGE_ADMIN,"view=" . $view ."&selected_language=" . $selected_language));
		return;
	} 
	// get action
	$lang_array=array(); 
	if ($get_action=="define"){
		// default action
	  $language_total_values=array();
	  $lang_keys_array=array();	
	  for($i=0;$i<sizeof($languages);$i++){
		$lang=$languages[$i]['directory'];
		if (substr($lang_file,0,strpos($lang_file,"."))==$selected_language){
			$edit_file=$path . $lang . "/" . $lang_file;
		} else {
			$edit_file=$path . $lang. "/" . $lang_file;
		}				
		 $handle=@fopen($edit_file,"r");		
		if (!$handle){		  
		  $messageStack->add(sprintf(ERROR_OPENING_FILE, $lang_file), 'error');		  
		  if(!file_exists($edit_file) && file_exists($path . $languages[0]['directory'] ."/" . $lang_file)){
		  	copy($path . $language . "/" . $lang_file,$edit_file);
		  	$handle=@fopen($edit_file,"r");		  	
		  }
		  if(!$handle) tep_redirect(tep_href_link(FILENAME_LANGUAGE_ADMIN,"selected_language=" . $selected_language.'&view='.$view));
		}		
			$cc=@fread($handle,filesize($edit_file));
			$cc=str_replace(chr(13) . chr(10),"<e>",$cc);
			$lang_array[$lang]=$cc;		
			$offset=0;
			$my_cnt="";
			$index_vals="";
			$lang_values_array=array();		
		while(true){ 
 			$match=preg_match("/define\((.*?)\);/",$cc,$matches,PREG_OFFSET_CAPTURE,$offset);
 			//$match=preg_match("/define{1}\((.*?)\);/",$cc,$matches,PREG_OFFSET_CAPTURE,$offset);
 			if(!$my_cnt && $edit_file=='F:/apache/htdocs/main/oscommres2_new/includes/languages/'.$lang.'/index.php'  && (strpos($cc,"define('TEXT_MAIN") || strpos($cc,'define("TEXT_MAIN'))){ 			  
 			  $main_strpos=strpos($cc,"define('TEXT_MAIN','");
 			  $head_strpos=strpos($cc,"define('TABLE_HEADING_NEW_PRODUCTS'"); 			   		
 			  if($main_strpos<=0) { 			  	 			  	
 			  	$head_strpos=strpos($cc,'define("TABLE_HEADING_NEW_PRODUCTS"');
 			  	$main_strpos=strpos($cc,'define("TEXT_MAIN","');
 			  }
 			  $index_vals=substr($cc,$main_strpos+20,($head_strpos-$main_strpos)-26);			    			  	 			
 			} 			 			 
			if($match<=0) break;
			//both key and value
			//key			
			$key=substr($matches[1][0],1,strpos($matches[1][0],",")-2);			
			//key value								
			$lang_value=substr($matches[1][0],strpos($matches[1][0],","),-1);
			if(strpos($lang_value,"'")) $lang_value=substr($lang_value,strpos($lang_value,"'")+1);
			else $lang_value=substr($lang_value,strpos($lang_value,'"')+1);			
			if($key=='TEXT_MAIN' && !$my_cnt){
			  $lang_value=$index_vals;
			  $my_cnt++;				  			  			  	  		
			}
			$middle_content=substr($cc,$old_offset,($matches[0][1]-$old_offset));
			$lang_values_array[]=array('key'=>$key,'val'=>$lang_value);
			$lang_keys_array[$key][$i]=array('id'=>$icnt,'key'=>$key,'val'=>$lang_value);
			if($key=='TEXT_MAIN' && $my_cnt==1) $offset=strlen($lang_value);	
			else $offset=$matches[0][1]+strlen($matches[0][0]);
			$old_offset=$offset;
		 	$icnt++; 
		 }
		$language_total_values[]=$lang_values_array; 
	  }
	}
	// create the languages to get as input
	$input_languages=array($selected_language=>"");
	// get the current languages
	$languages_array = array();
	for ($i=0, $n=sizeof($languages); $i<$n; $i++) {
		$languages_array[] = array('id' => $languages[$i]['directory'],
								   'text' => $languages[$i]['name']);
		$input_languages[$languages[$i]['directory']]=$languages[$i]['name'];
	}
	$view_array=array(array('id' => 'A','text' =>'Admin'), array('id' => 'F','text' => 'Front End'));
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script language="javascript">
	function edit_file(file_name){
		frm=document.choose_lang;
		//translate='N';
		//if (frm.translate.checked) translate='Y';
		location.href="<?php echo tep_href_link("language_admin.php","selected_language=" . $selected_language . "&view=" . $view . "&select_file=");?>"+file_name+"&get_action=define";
	}
	function select_language(){
		document.choose_lang.submit();
	}
	
</script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
<tr> 
	<!-- body_text //-->	  
    <td width=100% align=left valign="top">
		<table border="0" cellpadding="2" cellspacing="0" width="100%">
			<tr>
				<td>
				<table border="0" cellpadding="0" cellspacing="0" width="100%">
					<td class="pageHeading" width="40%"><?php echo "Language tool";?></td>
					<td align="right" class="main">
						<?php 
							echo tep_draw_form("choose_lang",FILENAME_LANGUAGE_ADMIN,"","get");
							echo tep_draw_hidden_field("get_action",$get_action);
							if ($select_file!="") echo tep_draw_hidden_field("select_file",$select_file);								
							echo "View:". '&nbsp;&nbsp;' . tep_draw_pull_down_menu("view",$view_array,$view,'onChange="javascript:select_language();"') .'&nbsp;&nbsp;';
							//echo "Language:" . '&nbsp;&nbsp;' . tep_draw_pull_down_menu("selected_language",$languages_array,$selected_language,'onChange="javascript:select_language();"') ;
							echo '</form>';
						?>
					</td>
				</table>
			</tr>
			<tr>
				<td><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td>
			</tr>
			<?php if ($get_action=="define") {  // editing language file
					$width=(75/count($input_languages));
			?>
					<tr>
						<td>
						<table border="0" cellpadding="1" cellspacing="0" width="100%">
							<tr>
								<td height="15" valign="middle" class="smallText"><b><?php echo $input_languages[$selected_language] . " : " . $lang_file;?></b></td>
							</tr>
							<tr>
								<td><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td>
							</tr>
							<?php   
									echo tep_draw_form("save_content",'language_admin.php','top=1&selected_language=' . $selected_language .'&view=' . $view . '&select_file=' . $lang_file);
									echo tep_draw_hidden_field("post_action","save");
							?>
							<tr>
								<td>
								<table border="0" cellpadding="1" cellspacing="0">
								<tr class="dataTableHeadingRow">									
									<?php 
										reset($input_languages);
										while(list($key,$value)=each($input_languages)) {  // print titles for input languages
											echo tep_draw_hidden_field("language_content[$key]","");
									?>
										<td width="<?php echo $width; ?>" class="dataTableHeadingContent">
										<?php echo $value;?></td>
									<?php } ?>
								</tr>
								<tr>
								  <td class="main" valign="top" width="25%">
								   <table border="0" cellspacing="0" cellpadding="0" width="100%">
								<?php 	
							      for($i=0;$i<sizeof($language_total_values);$i++){											
									$icnt=0;
//									//print_r($lang_keys_array);
									$lang_array=$language_total_values[$i];
									if($i>0){
											echo '</table></td>'.
										  	'<td class="main" valign="top" width="25%">'.
								   		    '<table border="0" cellspacing="0" cellpadding="0">';
									}  
									$second_key=array();
									$len=sizeof($lang_array);
									if(sizeof($prime_lang_id)>sizeof($lang_array)) $len=sizeof($prime_lang_id);									
									for($j=0;$j<$len;$j++){		
										if($i==0) {
											$prime_lang_id[]=array('id'=>$j,'key'=>$lang_array[$j]['key'],'val'=>$lang_array[$j]['val']);
											$prime_lang_key[$lang_array[$j]['key']]=array('key'=>$lang_array[$j]['key'],'id'=>$j,'val'=>$lang_array[$j]['val']);	
										}										
										$key=$lang_array[$j]['key'];
										$lang_value=$lang_array[$j]['val'];										
									    if($i>0){ 										  										    
										    if($prime_lang_id[$j]['key']==$lang_array[$j]['key']){										       
										       $lang_value=$lang_array[$j]['val'];
										       $key=$lang_array[$j]['key'];
										    }else  {										      										      										      										       										       
										      if(!$prime_lang_key[$lang_array[$j]['key']]) $second_key[]=array('id'=>$j,'key'=>$lang_array[$j]['key'],'val'=>$lang_array[$j]['val']);
										      //echo $lang_keys_array[$prime_lang_id[$j]['key']][$i]['key'].'<br>';
										      if($lang_keys_array[$prime_lang_id[$j]['key']][$i]['val']){										      											      	 										      											      	 										      	  
										      	  $lang_value=$lang_keys_array[$prime_lang_id[$j]['key']][$i]['val'];
										       	  $key=$prime_lang_id[$j]['key'];
										      }else {										      	 										      	 
										      	 $key=$prime_lang_id[$j]['key'];
										      	 $lang_value=' ';
										      }	 										    
										   } 
									    } 										    
									    if($key){
										$icnt++;
									?>	
										  <Tr>
										    <td>
										    <?php 
											 echo tep_draw_hidden_field("data_lines[$i][$icnt][key]",$key);
											 if($i==0) echo $key; 
											 else echo '&nbsp;';
											?>
										    </td>
										  </tr>
										  <tr>
											 <td class="main" valign="top">
													<?php echo tep_draw_textarea_field("data_lines[$i][$icnt][$key]","off",20,5,$lang_value);?>
											 </td>
										</tr>
									<?php	
									   }
							       }							 							              													               
									if($i>0 && sizeof($second_key)>0 && $len==$j){
									  for($si=0;$si<sizeof($second_key);$si++){  									   	
									   	  $key=$second_key[$si]['key'];									   	  
									   	  $lang_value=$second_key[$si]['val'];
									   	if($key){									   	  									   	  
									   	   $icnt++;
									   	   ?>
									   	  <Tr>
										    <td>
										    <?php 
											  echo tep_draw_hidden_field("data_lines[$i][$icnt][key]",$key);
											  echo $key;												 
											?>
										    </td>
										  </tr>
										  <tr>
											 <td class="main" valign="top">
												<?php echo tep_draw_textarea_field("data_lines[$i][$icnt][$key]","off",20,5,$lang_value);?>
											 </td>
										</tr> 	 
									 <?php
									   	   }	
									     }
									   }
									}
									$middle_content=substr($cc,$offset);
									echo tep_draw_hidden_field("data_lines[$icnt]",$middle_content);
								?>
								      </table>
									 </td>
									</tr>
									<tr>
										<td><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td>
									</tr>
									<tr>
										<td>
											<?php 
												echo tep_image_submit("button_save.gif",IMAGE_SUBMIT) . '&nbsp;';
												echo '<a href="' . tep_href_link(FILENAME_LANGUAGE_ADMIN,'selected_language=' . $selected_language.'&view='.$view ) . '">' . tep_image_button("button_back.gif",IMAGE_BACK) . '</a>';
											?>
										</td>
									</tr>
									</form>
									</table>
									</td>
									</tr>
								</table>
							</td>
						</tr>
				<?php } else { // default action show all selected language files ?>
					<tr>
						<td>
						<table border="0" cellpadding="0" cellspacing="0" width="100%">
							<tr class="dataTableHeadingRow" height="15" valign="middle">
								<td class="dataTableHeadingContent"><?php echo "Language File";?></td>
							</tr>
							<tr>
								<td><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td>
							</tr>
							<tr>
								<td>
									<table border="0" cellpadding="0" cellspacing="0" width="100%">
										<tr height="10">
											<td class="smallText"><?php echo '<a href="'.$selected_language . '".php>' . '<b>' . $selected_language . '.php' . '</b></a>';?></td>
									<?php   //load files from directory
											$col_count=1;
											$lang_dir=$path . $selected_language;
											$file_extension = substr($PHP_SELF, strrpos($PHP_SELF, '.'));
											if ($dir=dir($lang_dir)){
											while($file=$dir->read()){
											$file_array[]=$file;
											}
											sort($file_array);
												//while($file=$dir->read()){
												foreach($file_array as $file){
													if ($col_count%3==0){
														if($col_count!=1) 
															echo '</tr>';
														 echo '<tr>';
													 }
													if (substr($file,strpos($file,"."))==$file_extension){
														echo '<td class="smallText"><a href="'.tep_href_link(FILENAME_LANGUAGE_ADMIN,'top=1&get_action=define&view='.$view.'&lang='.$selected_language.'&select_file='.$file).'">' . $file . '</a></td>';
														$col_count++;
													}
												}
											}
									?>
									</table>
								</td>
							</tr>
						</table>
						<td>
					</tr>
				<?php } // check action ?>
		</table>
	</td>
</table>
<td>
</tr>
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>