<?php
$tpl_version = "1.3";
if (isset($_GET['ver'])) {
  echo $tpl_version;
  exit;
	// Date: 2014-12-29
}
include("include/password_protect.php"); 
if (isset($_GET['alert'])) {
	echo '<script type="text/javascript">';
	echo 'alert("'.$_GET['alert'].'")';
	echo '</script>';
}
error_reporting(E_ALL);
// Base directory for updates
$file_dir = 'http://weatherbyyou.com/updates/weather_sunset/';
$lang_dir = 'http://weatherbyyou.com/updates/lang/';
$local_lang_dir = 'lang/';
$upd_lang = FALSE;
$upd_file = FALSE;
// Is parameter file given?
if (isset($_GET['file'])) {
	$upd_file = TRUE;
	$action = $_GET['action'];
	$file_do=$_GET['file'];
	$remote_file = $file_dir.$file_do;
}
if (isset($_GET['lang_file'])) {
	$upd_lang = TRUE;
	$action = $_GET['action'];
	$file_do=$_GET['lang_file'];
	$remote_lang = $lang_dir.$file_do;
	$file_do = $local_lang_dir.$file_do;
}
// Some includes to do

// Include files functions
require_once('include/update_list.php');

// Import latest template files list
$site = fetchURL($file_dir.'available.txt');
$writed = save_local($site, 'include/latest.php');
include('include/latest.php');

// Import latest languages list
$site = fetchURL($lang_dir.'lang_file.txt');
$writed = save_local($site, 'include/all_languages.php');
include('include/all_languages.php');

// Search local language files and write it as a file
$nb_langues = 0;
foreach (glob($local_lang_dir."text_standard_*.php") as $filename) {
  $lang_file_list[$nb_langues] = $filename;
  $lang_abv['abv'][$nb_langues] = substr($lang_file_list[$nb_langues], 19,2);
  $lang_abv['name'][$nb_langues] = $languages[$lang_abv['abv'][$nb_langues]];
  $nb_langues = $nb_langues + 1;
}
// Sort languages list on language name
array_multisort($lang_abv['name'], $lang_abv['abv']);
save_lang($lang_abv, 'include/local_lang.php');
$lang="";
// $nb_langues = "";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <title>Template management screen</title>
	<meta name="keywords" content="Current, actually, last minute" />
	<meta name="description" content="Current weather observations" />
	<link type="text/css" rel="stylesheet" href="style.css" />
	<script type="text/javascript">
		var dayname = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
		var monthname = new Array ("January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");	
	</script>
</head>
<body>
<div id="header">
	<?php
		// Construct 2 js list for Template files name and id.
		echo '<script type="text/javascript">';
		echo 'var name_files = new Array("'.$available[0][0].'"';
		$nb_new = 0;
		for ($inc = 1; $inc < count($available); $inc++) {
			if (!file_exists($available[$inc][0])) {
				$new_files[$nb_new] = $available[$inc];
				array_splice($available, $inc, 1);
				$nb_new = $nb_new + 1;
				$inc = $inc - 1;
			}
			else {
				print ', "'.$available[$inc][0].'"';
			}
		}
		echo ");\n";
		echo 'var name_id = new Array("'.$available[0][2].'"';
		for ($inc = 1; $inc < count($available); $inc++) {
			print ', "'.$available[$inc][2].'"';
		}
		echo ");\n";
		echo 'nb_remote = '.$inc.';';
		
		// Construct 2 js list for language name and id.
		echo 'var name_lang = new Array("'.$local_lang_dir.$lang_available[0][0].'"';
		$nb_new = 0;
		for ($inc = 1; $inc < count($lang_available); $inc++) {
			if (!file_exists($local_lang_dir.$lang_available[$inc][0])) {
				$new_lang_files[$nb_new] = $lang_available[$inc];
				array_splice($lang_available, $inc, 1);
				$nb_new = $nb_new + 1;
				$inc = $inc - 1;
			}
			else {
				print ', "'.$local_lang_dir.$lang_available[$inc][0].'"';
			}
		}
		echo ");\n";
		echo 'var lang_id = new Array("'.$lang_available[0][2].'"';
		for ($inc = 1; $inc < count($lang_available); $inc++) {
			print ', "'.$lang_available[$inc][2].'"';
		}
		echo ");\n";
		echo 'nb_remote_lang = '.$inc.';';
		echo "</script>";
	?>
	<div id="title">
		<h1>Weather Sunset</h1>
		<p><span>by Weather by You!</span></p>
	</div>
	<div id="top_menu" style="margin-top: 35px;">
		<ul>
			<!-- Menu item in inverted order, due to float -->
			<li><a href="system.php">System</a></li>
			<li><a href="infos.php">About</a></li>
			<li><a href="images.php">Images</a></li>
			<li><a href="ss_gauges.php">Gauges</a></li>
			<li><a href="index.php">Home</a></li>
		</ul>
	</div>
</div>
<div id="base">
  <!-- Right Column -->
	<div id="sidebar">
		<div class="sidebar2">
			<div id="calendar">
				<script type="text/javascript">
					var timestamp = new Date();
					var date_yyyy = timestamp.getFullYear();
					var date_mmm = timestamp.getMonth();
					var date_dd = timestamp.getDate();
					var date_day = timestamp.getDay();
				</script>
				<span class="time_month"><script type="text/javascript">document.write(date_yyyy);</script><br />
				<script type="text/javascript">document.write(monthname[date_mmm]);</script></span>
				<h3 style="margin: 15px 0px 8px 0px"><script type="text/javascript">document.write(date_dd);</script></h3>
				<p style="margin: 0px;padding: 0px"><script type="text/javascript">document.write(dayname[date_day]);</script></p>
				<div class="clear"></div>
			</div>
			<div>
				<h3 style="margin-bottom:0px">Downloads:</h3>
				<p style="text-align: left; margin:5px 0px 0px 35px"><img src="style/down.png" alt="." title="Not available" /> Not Available.<br />
				<img src="style/down_b.png" alt="." title="Available, same version as your site" /> Same version.<br />
				<img src="style/down_y.png" alt="." title="Available, newer version" /> New version.<br />
				<img src="style/down_g.png" alt="." title="Available, new page not installed on your site" /> New page.</p>
				<h3 style="margin-bottom:0px">Informations:</h3>
				<p style="text-align: left; margin:5px 0px 0px 35px">
				<img src="style/info_b.png" alt="." title="Available, same version as your site" /> Available.<br />
				<img src="style/info.png" alt="." title="Not available" /> Not Available.<br />
				<img src="style/info_y.png" alt="." title="Omportant note to see" /> <strong>SEE link</strong> before.<br />
				<img src="style/new.png" alt="." title="New page available" /> New page available</p>
				<h3 style="margin-bottom:0px">Notes:</h3>
				<p><em>- Over icon for short info<br />
				- Click icon for more info</em></p>
				<hr />
				<p><strong>All files are backed up<br />before update process.</strong></p>
				<p style="height: 140px; margin-left: 15px"><img src="style/safe_seal.png" alt="Safe seal" title="All files are allways backuped BEFORE!" /></p>
			</div>
		</div>
    <div id="sidebar-credit">
			Design <a href="http://wptemplates.org/" title="Wordpress Themes">Wp Templates</a><p> and <a href="http://www.thoughtmechanics.com/chicago-web-design-company/" title="Designed By TM">Designed By TM</a>.</p>
		</div>
	</div>
  <!-- Centre  -->
	<div id="base_header">
		<h2 style="margin:15px 0px; text-align: center"><strong>Template configuration screen</strong></h2>
	</div>
	<div id="wrap">
		<div id="content_top" style="margin-top: 0px"></div>
		<div id="content">
			<?php
				if ($upd_lang || $upd_file) {
			?>
			<div id="manage_do_top"></div>
			<div id="manage_do">
				<table summary="Update status frame">
					<col width="225"/>
					<col width="275"/>
					<col width="225"/>
				<tr>
					<td valign="middle" style="text-align: center">
						<img src="style/update.png" alt="Template manager logo" title="Template manager module" />
					</td>
					<td style="padding-left: 35px;">
					<h2>Update process</h2>
					<?php
							$file_ext = pathinfo($file_do, PATHINFO_EXTENSION);
							$file_name = pathinfo($file_do, PATHINFO_FILENAME);
							$dot_ext = '.'.$file_ext;
						if ($upd_lang) {
							$remote_upd = str_replace($dot_ext, ".upd", $remote_lang);
							echo '<p>Langusge to '.$action.': <strong>'.substr($file_do,5)."</strong></p>\n";
						}
						else {
							$remote_upd = str_replace($dot_ext, ".upd", $remote_file);
							echo '<p>File to '.$action.': <strong>'.$file_do."</strong></p>\n";
						}
						$file_test = fopen($remote_upd, "r");
						if ($file_test) {
							echo '<p><img src="style/ok.png" alt="OK" title="Success" /> Remote file: is <strong>available</strong>.<br />';
							// If file already exist, rename to .bak (delete .bak prior, if needed)
							if (file_exists($file_do)) {
								echo '<img src="style/ok.png" alt="OK" title="Succes" /> Local file: is <strong>available</strong>.<br />';
								$back_name = $file_name.'.bak';
								if ($upd_lang) 
									$back_name = $local_lang_dir.$back_name;
								$result = copy($file_do, $back_name);
								if (!$result) { 
									echo '<img src="style/nok.png" alt="ERR" title="Error" /> Copy failed.</p>';
									return;
								}
								else
									echo '<img src="style/ok.png" alt="OK" title="Success" /> Backup file <strong>successfull</strong>.</p>';
							}
							// Read remote file and write it locally
							$site = fetchURL($remote_upd);
							$writed = save_local($site, $file_do);
							if ($writed) {
								if ($action == 'install')
									echo '<p style="margin-bottom: 0px"><img src="style/ok.png" alt="OK" title="Success" /> <strong>New file succesfully installed!</strong></p></td><td style="text-align: center;">';
								else
									echo '<p style="margin-bottom: 0px"><img src="style/ok.png" alt="OK" title="Success" /> <strong>File succesfully updated!</strong></p></td><td style="text-align: center;">';
								if (!$upd_lang)
									echo '<p class="slim"><strong>See new page</strong></p><p class="upd_btn"><a href="'.$file_do.'" onclick="window.open(this.href); return false;">See page</a></p>';
							}
							else {
								echo '<p style="margin-bottom: 0px"><img src="style/nok.png" alt="ERR" title="Error" /> <strong>Error writing file!!</strong></p></td><td style="text-align: center;">';
							}
						}
						else {
							echo '<p><img src="style/nok.png" alt="ERR" title="Error" /> Remote file: <strong>Not found!</strong></p><h3 class="slim">Process aborted</h3></td><td style="text-align: center;">';
						}
						echo '<p class="slim"><strong>Reload this page</strong></p><p class="upd_btn"><a href="manage.php">Refresh</a></p></td>';
					?>
				</tr>
				</table>
			</div>
			<?php
			}
			?>
      <div id="bg_manage">
        <div id="temp_files">
          <h3 class="manage_stitle">Files installed</h3>
          <table class="tb_temp_files" summary="Template files">
            <col width="150" />
            <col width="55" />
            <col width="40" />
            <col width="30" />
            <col width="30" />
            <tr style="line-height: 16px">
              <th>Filename</th>
              <th>local</th>
              <th>web</th>
            </tr>
					<?php
						// Template installed
 						for ($inc = 0; $inc < count($available); $inc++) {
							echo "<tr>\n<td>".$available[$inc][0]."</td>\n";
							echo '<td id="l_'.$available[$inc][2].'">--</td>';
							echo '<td id="r_'.$available[$inc][2].'">'.$available[$inc][1]."</td>\n";
							echo '<td id="d_'.$available[$inc][2].'"><a href="manage.php?file='.$available[$inc][0].'&amp;action=update"><img src="style/down.png" alt="." title="Available, same version as your site" /></a></td>'."\n";
							echo '<td id="i_'.$available[$inc][2].'">';
							if ($available[$inc][4] != "")
								echo '<img src="style/info_b.png" alt="." title="'.$available[$inc][4].'" /></td>';
							else
								echo '<img src="style/info.png" alt="." title="File is up to date" /></td>';
							echo "</tr>\n";
						}
					?>
          </table>
        </div>
        <div id="lang_files">
				<br />
			<div style="padding:10px; border: 1px solid #C7B482">
          <h3 class="manage_stitle">New files</h3>
				<!-- Display new files not installed in current template -->
				<table class="tb_temp_files" summary="Template files">
            <col width="135" />
            <col width="55" />
            <col width="40" />
            <col width="30" />
            <col width="30" />
            <tr style="line-height: 16px">
              <th>Filename</th>
              <th>local</th>
              <th>web</th>
            </tr>
					<?php
						// Not installed template pages
						if (isset($new_files)){
							for ($inc = 0; $inc < count($new_files); $inc++) {
								echo "<tr>\n<td>".$new_files[$inc][0]."</td>\n";
								echo '<td id="l_'.$new_files[$inc][2].'"><img src="style/new.png" alt="." title="New file" /></td>';
								echo '<td id="r_'.$new_files[$inc][2].'">'.$new_files[$inc][1].'</td>';
								echo '<td id="d_'.$new_files[$inc][2].'"><a href="manage.php?file='.$new_files[$inc][0].'&amp;action=install"><img src="style/down_g.png" alt="." title="Download new file" /></a></td>';
								if ($new_files[$inc][4] != "")
									echo '<td><img src="style/info_b.png" alt="." title="'.$new_files[$inc][4].'" /></td></tr>';
								else
									echo '<td><img src="style/info.png" alt="." title="File is up to date" /></td></tr>';
							}
						}
						else {
							echo "<tr>\n<td colspan=\"5\"><br />No new files available!</td>\n</tr>\n";
						}
					?>
				</table>
				<br />
          <h3 class="manage_stitle">New language</h3>
          <table class="tb_lang" summary="Languages file">
            <col width="45" />
            <col width="110" />
            <col width="50" />
            <col width="45" />
            <col width="30" />
            <tr style="line-height: 16px">
              <th>&nbsp;</th>
              <th>Language</th>
              <th>local</th>
              <th>web</th>
            </tr>
            <?php
						// Not installed language files
						if (isset($new_lang_files)){
							for ($inc = 0; $inc < count($new_lang_files); $inc++) {
								echo "<tr>\n<td>".$new_lang_files[$inc][2]."</td>\n";
								echo '<td id="'.$new_lang_files[$inc][2].'">'.$new_lang_files[$inc][3].'</td>';
								echo '<td id="l_'.$new_lang_files[$inc][2].'"><img src="style/new.png" alt="." title="New language available" /></td>';
								echo '<td id="r_'.$new_lang_files[$inc][2].'">'.$new_lang_files[$inc][1].'</td>';
								echo '<td id="d_'.$new_lang_files[$inc][2].'"><a href="manage.php?lang_file='.$new_lang_files[$inc][0].'&amp;action=install"><img src="style/down_g.png" alt="." title="Add this language" /></a></td>';
								echo '<td id="i_'.$new_lang_files[$inc][2].'"><img src="style/info.png" alt="." title="'.$new_lang_files[$inc][5].'" /></td>';
								echo "</tr>\n";
							}
						}
						else {
							echo "<tr>\n<td> </td><td colspan=\"5\"><br />No new language available!</td>\n</tr>\n";
						}
					?>
				</table>
				</div>
					<br />
          <h3 class="manage_stitle">Translations</h3>
          <table class="tb_lang" summary="Languages file">
            <col width="45" />
            <col width="110" />
            <col width="50" />
            <col width="45" />
            <col width="30" />
            <tr style="line-height: 16px">
              <th>&nbsp;</th>
              <th>Language</th>
              <th>local</th>
              <th>web</th>
            </tr>
            <?php
						// Installed language files
 						for ($inc = 0; $inc < count($lang_available); $inc++) {
							echo "<tr>\n<td>".$lang_available[$inc][2]."</td>\n";
							echo '<td id="'.$lang_available[$inc][2].'">'.$languages[$lang_available[$inc][2]].'</td>';
							echo '<td id="l_'.$lang_available[$inc][2].'">--</td>';
							echo '<td id="r_'.$lang_available[$inc][2].'">'.$lang_available[$inc][1].'</td>';
							echo '<td id="d_'.$lang_available[$inc][2].'"><a href="manage.php?lang_file='.$lang_available[$inc][0].'&amp;action=update"><img src="style/down_b.png" alt="." title="Available, same version as your site" /></a></td>';
							echo '<td id="i_'.$lang_available[$inc][2].'">';
							if ($lang_available[$inc][5] != "")
								echo '<img src="style/info_b.png" alt="." title="'.$lang_available[$inc][5].'" /></td>';
							else
								echo '<img src="style/info.png" alt="." title="File is up to date" /></td>';
							echo "</tr>\n";
							}
              ?>
            </table>
						<br />
          </div>
        </div>
      </div>
		</div>
    <!--  End Center  -->
  </div>
  <!-- content ends --> 
<!-- footer begins -->
<div id="bg_btm">
	<div id="footer">
		<p id="footer_notes">Copyright (c) 2014 <a href="http://weatherbyyou.com">Weather by You!</a></p>
	</div>
</div>
		<!-- footer ends -->
<script type='text/javascript'>
//<![CDATA[
function SetHeight() {
  document.getElementById("bg_manage").style.height = document.getElementById("temp_files").clientHeight +  'px';
}
function ChkLocalVer(nbFiles, ids, files) {
	var local;
	var serial = Math.floor((Math.random()*10000)+1);
	for (inc = 0; inc < nbFiles; inc++) {
		ides = "l_" + ids[inc];
		if (files[inc][0] != 0) {
			var xmlhttp;
			if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
				xmlhttp=new XMLHttpRequest();
				}
			else {// code for IE6, IE5
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
			xmlhttp.onreadystatechange=function() {
				if (xmlhttp.readyState==4 && xmlhttp.status==200) {
					document.getElementById(ides).innerHTML=xmlhttp.responseText;
				}
				else {
					document.getElementById(ides).innerHTML = '0';
				}
			}
			xmlhttp.open("GET", files[inc]+"?ver&node="+serial,false);
			xmlhttp.send();
		}
		else {
			document.getElementById(ides).innerHTML = '<img src="style/not.png" alt="." title="File not installed" />';
		}
	}
}

ChkLocalVer(nb_remote, name_id, name_files);
// Wait 1 sec before checking languages
var t1=setTimeout(function(){ChkLocalVer(nb_remote_lang, lang_id, name_lang)},1000);

inc = 0;
while (inc < nb_remote) {
	tmp_local= "l_" + name_id[inc];
	tmp_remote= "r_" + name_id[inc];
	tmp_down= "d_" + name_id[inc];
	ver_local = parseFloat(document.getElementById(tmp_local).innerHTML);
	if (isNaN(ver_local)) ver_local = 0; // document.write(ver_local);
	ver_remote = parseFloat(document.getElementById(tmp_remote).innerHTML);
	if ((ver_local < ver_remote) || (ver_local == ""))  {
		document.getElementById(tmp_down).getElementsByTagName("img")[0].src = "style/down_y.png";
		document.getElementById(tmp_down).getElementsByTagName("img")[0].title = "Newer version available";
	}
	else {
		document.getElementById(tmp_down).getElementsByTagName("img")[0].src = "style/down_b.png";
	}
	inc++;
}
inc = 0;
while (inc < nb_remote_lang) {
	tmp_local= "l_" + lang_id[inc];
	tmp_remote= "r_" + lang_id[inc];
	tmp_down= "d_" + lang_id[inc];
	ver_local = parseFloat(document.getElementById(tmp_local).innerHTML);
	ver_remote = parseFloat(document.getElementById(tmp_remote).innerHTML);
	if ((ver_local < ver_remote) || (ver_local == "")) {
		document.getElementById(tmp_down).getElementsByTagName("img")[0].src = "style/down_y.png";
		document.getElementById(tmp_down).getElementsByTagName("img")[0].title = "Newer version available";
	}
	inc++;
}
//]]>
</script>
<!-- Piwik --> 
<script type="text/javascript">
 var pkBaseURL = (("https:" == document.location.protocol) ? "https://meteoduquebec.com/piwik/" : "http://meteoduquebec.com/piwik/");
 document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
 </script><script type="text/javascript">
 try {
 var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 4);
 piwikTracker.trackPageView();
 piwikTracker.enableLinkTracking();
 } catch( err ) {}
 </script><noscript><p><img src="http://meteoduquebec.com/piwik/piwik.php?idsite=4" style="border:0" alt="" /></p></noscript>
 <!-- End Piwik Tracking Code -->
</body>
</html>