<?php

//////////////////////////////////////////////////////////////
//===========================================================
// upgrade_theme.php
//===========================================================
// SOFTACULOUS 
// Version : 1.1
// Inspired by the DESIRE to be the BEST OF ALL
// ----------------------------------------------------------
// Started by: Alons
// Date:       10th Jan 2009
// Time:       21:00 hrs
// Site:       http://www.softaculous.com/ (SOFTACULOUS)
// ----------------------------------------------------------
// Please Read the Terms of use at http://www.softaculous.com
// ----------------------------------------------------------
//===========================================================
// (c)Softaculous Inc.
//===========================================================
//////////////////////////////////////////////////////////////

if(!defined('SOFTACULOUS')){

	die('Hacking Attempt');

}


function upgrade_theme(){

	global $user, $globals, $l, $theme, $softpanel, $iscripts, $catwise, $error, $scripts;
	global $software, $soft, $info, $settings, $softins, $dbtype, $dbs, $upgraded, $__settings, $setupcontinue, $installations, $notes, $upgradableto, $act;
	
	
	// Give the staus
	if(optGET('ajaxstatus')){
		
		$_status = soft_progress(optGET('ajaxstatus'));
		
		$tmp_status = unserialize($_status[1]);
		$_status[1] = $tmp_status['current_status'];
		
		if(!empty($_status)){
			echo implode('|', $_status);
			return true;
		}
		
		// False call
		echo 0;
		return false;
	
	}
	
	softheader($globals['sn'].' - '.$software['name']);
	echo '
	<div id="install_win">';
	
		//First add the installation info
		if(empty($upgraded)){
			
			if(!empty($GLOBALS['alreadyupdated'])){
				
				echo '
				<form accept-charset="'.$globals['charset'].'" method="post" name="uprec" id="uprec" action="">
					<input type="hidden" name="updaterecord" value="1" />
					
					<div class="alert alert-warning">
						<a href="#close" class="close" data-dismiss="alert" aria-label="close">&times;</a>
						<center><a href="javascript:$_(\'uprec\').submit();" style="text-decoration:none;"><img src="'.$theme['images'].'notice.gif" /> &nbsp;  '.__('Hmm ... it looks like the actual version of the installation is <b>$0</b> and not <b>$1</b>. <br />Click here to update $2 records.', array($GLOBALS['alreadyupdated'], $softins['ver'], APP)).'</a></center>
					</div>
				'.csrf_display().'	
				</form><br />';
			}
			
			// You may not need an upgrade anymore.
			if(!empty($GLOBALS['no_update_required'])){
				
				echo '<div class="bg">
				<div class="alert alert-warning">
					<center><img src="'.$theme['images'].'notice.gif" /> &nbsp; '.__("It looks like you don't need to update your installation as it is already uptodate").'<center>
				</div>
				</div>
				<br />';
			
			}else{

				$return = '';
				$role = '';

				if(can_wordpress_manager($soft)){
					$return .= '<a role="button" href="'.$globals['ind'].'act=wordpress&insid='.$softins['insid'].'"><span class="btn btn-outline-primary">'.__('Return to WordPress Management').'</span></a>&nbsp';
					
					$role = 'role="button"';
				}

				$return .= '<a '.$role.' href="'.script_link($soft).'"><span class="btn btn-outline-primary">'.__('Return to Overview').'</span></a>';
				
				//Filter to customize the return to link
				$return = apply_filters('return_link', $return, $act);
				
				echo '
				<div id="fadeout_div">
					<form accept-charset="'.$globals['charset'].'" name="installsoftware" method="post" action="" onsubmit="return checkform();" id="installsoftware">
					
						<script language="javascript" type="text/javascript"><!-- // --><![CDATA[
						
						function checkform(dosubmit){
							try{
								if(!formcheck() || !checkdbname(\'softdb\', true)){
									return false;
								}
							}catch(e){
								//Do nothing
							}
							$_("softsubmitbut").disabled = true;
							
							if(useprog){
								
								// Send a request to check the status
								progressbar.start();
								
								// Return false so that the form is not submitted
								return false;
							
							// This is OLD School !
							}else{		
								if(dosubmit == 1){			
									$_("installsoftware").submit();	
								}
								show_msg("'.__js('Upgrading...').'");
							}
							return true;
						};
						
						var progressbar = {
							timer: 0,
							total_width: 0,	
							status_key: "",
							synctimer: 0,
							fadeout_div: "#fadeout_div",
							win_div: "#install_win",
							progress_div: "#progress_bar",
							formid: "#installsoftware",
							frequency: 8000,
							
							current: function(){
								try{
									var tmp_cur = Math.round(parseInt($_("progress_color").width)/parseInt($_("table_progress").width)*100);
									if(tmp_cur > 100){
										tmp_cur = 99;
									}
									return tmp_cur;
								}catch(e){
									return -1;	
								}
							},
							
							reset: function(){ try{
								clearTimeout(this.timer);
								$_("progress_color").width = 1;
							}catch(e){ }},
							
							move: function(dest, speed, todo){ try{
								var cur = this.current();
								if(cur < 0){
									clearTimeout(this.timer);
									return false;
								}
								var cent = cur + 1;
								var new_width = cent/100*this.total_width;
								if(new_width < 1){
									new_width = 1;
								}
								//alert(new_width+" "+dest+" "+cent);
								
								$_("progress_color").width = new_width;
								$_("progress_percent").innerHTML = "("+cent+" %)";
								
								if(cent < dest){
									this.timer = setTimeout("progressbar.move("+dest+", "+speed+")", speed);
								}else{
									eval(todo);	
								}
							}catch(e){ }},
							
							text: function(txt){ try{
								$_("progress_txt").innerHTML = txt;
							}catch(e){ }},
							
							sync: function(){
								if(progressbar.status_key.length < 2){
									return false;
								}
								$.ajax({
									url: window.location+"&ajaxstatus="+progressbar.status_key+"&random="+Math.random(),
									type: "GET",
									success: function(data){
										if(data == 0) return false;
										var tmp = data.split("|");
										var cur = progressbar.current();
										tmp[2] = (3000/(tmp[0]-cur));
										//alert(tmp);
										if(tmp[0] > cur){
											if(parseInt(tmp[2]) == 0){
												tmp[2] = 800;
											}
											progressbar.move(tmp[0], tmp[2]);
										}
										progressbar.text(tmp[1]);
										progressbar.synctimer = setTimeout("progressbar.sync()", progressbar.frequency);
									}
								});
							},
							
							sync_abort: function(){
								clearTimeout(this.synctimer);
							},
							
							start: function(){ try{
								this.post();
								this.reset();
								this.total_width = parseInt($_("table_progress").width);
								this.move(95, 800);
								this.status_key = $("#soft_status_key").attr("value");
								this.sync();
							}catch(e){ }},
							
							post: function(){
								
								// Scroll to the Top and show the progress bar
								goto_top();
								$(progressbar.fadeout_div).fadeOut(500, 
									function(){
										$(progressbar.progress_div).fadeOut(1);
										$(progressbar.progress_div).fadeIn(500);
									}
								);
								
								
								try{
									var sid = 0;
									sid = $_("softbranch").value;
								}catch(e){ 
									var sid = 0;
								}
								
								$.ajax({
									url: window.location+"&jsnohf=1&softbranch="+sid,
									type: "POST",
									data: $(progressbar.formid).serialize(),
									complete: function( jqXHR, status, responseText ) {
										
										progressbar.sync_abort();
										
										// Store the response as specified by the jqXHR object
										responseText = jqXHR.responseText;
										
										try{
											//alert(responseText);
											if(responseText.match(/\<\!\-\-PROC_DONE\-\-\>/gi)){
												progressbar.text("'.__js('Finishing Upgradation').'");
												progressbar.move(99, 10, "$(progressbar.progress_div).fadeOut(1)");
											}else{
												progressbar.reset();
											}
										}catch(e){ }
										
										if ( jqXHR.state() == "resolved" ) {
										
											// #4825: Get the actual response in case
											// a dataFilter is present in ajaxSettings
											jqXHR.done(function( r ) {
												responseText = r;
											});
											//alert(responseText);
											// Create a dummy div to hold the results
											// inject the contents of the document in, removing the scripts
											// to avoid any "Permission Denied" errors in IE
											var newhtml = jQuery("<div>").append(responseText).find(progressbar.win_div).html();
											
											$(progressbar.win_div).animate({opacity: 0}, 1000, "", function(){
												$(progressbar.win_div).html(newhtml);
												new_theme_funcs_init();
											}).delay(50).animate({opacity: 1}, 500,function(){
												if($("#setupcontinue_url").length > 0){
													$(".third_url").css({"border": "2px solid #FBDB0C", "padding":"5px"});
													alert("'.__js('You are one more step away from completing the upgrade process').'");
													$(".third_url").css({"border-color": "#fff", "transition":"60s"});
												}				
											});
											
											//alert(newhtml);
											
										}else{
											alert("Oops ... the connection was lost");
										}
									}
								});
							}
						};
						
						function show_msg(msg){	
							$_("show_txt").innerHTML = "<br /><br /> &nbsp; "+msg+" &nbsp; <img src=\"'.$theme['images'].'progress.gif\" />";
							$_("show_txt").style.display = "";
						}
						
						// Use the Progress Bar ?
						var useprog = 1;
						
						softemail = new Array();
						function softmail(){
							try{
								var sofdom = "softdomain";
								for(x in softemail){
									if(softemail[x] == true) continue;
									var temp = $_(x).value.split("@");
									$_(x).value = temp[0] + "@" + $_(sofdom).value;
								}
							}catch(e){
								return false;
							}
						};
						
						function reload_backup_options(){
							
							if($("#create_backup").is(":checked") == true){
								
								$("#backup_options").show();
								
								if($("#backup_type").val() == "advanced"){
									$("#advanced_backup").show();
								}else{
									$("#advanced_backup").hide();
								}
							
								if($("#backup_type").val() == "advanced"){
									$("#advanced_backup").show();
								}else{
									$("#advanced_backup").hide();
								}
								
							}else{
								$("#backup_options").hide();
								$("#advanced_backup").hide();
							}
							
						}
						
						$(document).ready(function(){
							
							reload_backup_options();
						
							$("#create_backup").change(function(){
								reload_backup_options();
							}); 
							
							$("#backup_type").change(function(){
								reload_backup_options();
							});
						}); 
						
						'.(!empty($_POST) ? '' : 'addonload(\'softmail();\');');
						
						echo '
						// ]]></script>';
						
						$array_keys = array_keys($upgradableto);
						
						echo '
						<div class="bg"><br />
						<div class="row sai_main_head mt-8 mb-4">
							<div class="col-12 text-center">
								<i class="fas fa-sync-alt fa-2x mr-2" style="color:#00A0D2;"></i>
								<span class="form-title">'.__('Upgrading ').$scripts[$array_keys[0]]['name'].'</span>
							</div>
						</div>
						<div class="mt-4">
						<div class="m-3 card p-3">';

							if(!can_use_soft($soft, $use_msg)){
								echo '<center class="alert alert-warning">'.$use_msg.'</center>';
							}
						
							echo error_handle($error, "100%", 0, 1);
						
							echo '
							<div class="row mb-2">
								<div class="col-sm-5">
									<span class="sai_head mr-3 mb-2 d-inline-block">'.__('URL').'</span>
								</div>
								<div class="col-sm-7">
									<a href="'.$softins['softurl'].'" target="_blank">'.$softins['softurl'].'</a>
								</div>
							</div>
							
							<div class="row mb-2">
								<div class="col-sm-5">
									<span class="sai_head mr-2">'.__('Path').'</span>
								</div>
								<div class="col-sm-7">
									<span class="sai_info">'.$softins['softpath'].'</span>
								</div>
							</div>';
								
							if(count($array_keys) >= 2){
							
							echo '<div class="row mb-2 mt-2">
								<div class="col-sm-5">
									<label for="softbranch" class="sai_head">'.__('Select a version to upgrade to').'</label>
								</div>
								<div class="col-sm-7">
									<select name="softbranch" id="softbranch" class="form-control">';
										foreach($upgradableto as $uk => $uv){
											echo '<option value='.$uk.' '.(@trim($_POST['softbranch']) == $uk ? 'selected="selected"' : '').'>'.$uv.'</option>';
										}
									echo '
									</select>';
								echo '</div>
							</div>';
							
							}

							if(empty($globals['disable_backup_upgrade'])){
							
							echo '<div class="row mb-2 mt-2">
								<div class="col-sm-5">
									<label class="sai_head" for="create_backup">'.__('Create Backup').'</label><br />
									<span class="sai_exp2">'.__('Create a Backup before upgrading').'</span>
								</div>
								<div class="col-sm-7">
									<input type="checkbox" name="create_backup" id="create_backup" '.POSTchecked('create_backup', !empty($globals['upgrade_backup_on']) ? 1 : '').' />
								</div>
							</div>
							
							<div class="row mb-2" id="backup_options" style="display:none;">
								<div class="col-sm-5">
									<label class="sai_head">'.__('Backup Options').'</label><br />
									<span class="sai_exp2">'.__('Choose full backup or advanced options').'</span>
								</div>
								<div class="col-sm-7">
									<select name="backup_type" class="form-control" id="backup_type">
										<option value="">'.__('Full Backup (Default)').'</option>
										<option value="advanced" '.(@trim($_POST['backup_type']) == 'advanced' ? 'selected="selected"' : '').'>'.__('Advanced Options').'</option>
									</select>
								</div>
							</div>
							
							<div id="advanced_backup" class="row mb-2" style="display:none;">
								<div class="col-sm-7 offset-sm-5">
								<input type="checkbox" name="backup_dir" id="backup_dir" '.POSTchecked('backup_dir', true).' />&nbsp;&nbsp;<label for="backup_dir" class="sai_head m-2">'.__('Backup Directory').'</label><br />';

								// WWW directory									
								if(!empty($softins['wwwdir'])){
									echo '<input type="checkbox" name="backup_wwwdir" id="backup_wwwdir" '.POSTchecked('backup_wwwdir', true).' />&nbsp;&nbsp;<label for="backup_wwwdir" class="sai_head m-2">'.__('Backup Web Directory').'</label>';
								}
								
								// Data directory
								if(!empty($softins['softdatadir'])){
									echo '<input type="checkbox" name="backup_datadir" id="backup_datadir" '.POSTchecked('backup_datadir', true).' />&nbsp;&nbsp;<label for="backup_datadir" class="sai_head m-2">'.__('Backup Data Directory').'</label>';
								}

								// Database									
								if(!empty($softins['softdb'])){
									echo '<input type="checkbox" name="backup_db" id="backup_db" '.POSTchecked('backup_db', true).' />&nbsp;&nbsp;<label for="backup_db" class="sai_head m-2">'.__('Backup Database').'</label>';
								}
							echo '</div>
							</div>';
							
							}
						
							if(!empty($settings)){
								
								if(file_exists($software['path'].'/upgrade.js')){
									echo '<script language="javascript" type="text/javascript"><!--// --><![CDATA[';
									echo @parselanguages(str_replace('$(', '$_(', file_get_contents($software['path'].'/upgrade.js')));
									echo '// ]]></script>';
								}
							
								foreach($settings as $group => $sets){
									
									if($group == 'hidden') continue;
								
									echo '<br /><br />
									<div class="mt-3">
										<div class="sai_sub_head">'.$group.'</div>
										<hr class="sai_main_head_hr">';
							
										foreach($sets as $sk => $sv){
											
											// Add class form-control
											if(preg_match('/type="text"/is', $sv['tag'])){
												$sv['tag'] = preg_replace('/>/is', ' class="form-control">',$sv['tag']);
											}
											if(preg_match('/<select/is', $sv['tag'])){
												$sv['tag'] = preg_replace('/<select/is', '<select class="form-control"',$sv['tag']);
											}
									
											echo '
											<div class="row mb-3">
												<div class="col-sm-5">
													<label class="sai_head">'.$sv['head'].'</label>
													'.(empty($sv['exp']) ? '' : '<span class="sai_exp">'.$sv['exp'].'</span>').'
												</div>
												<div class="col-sm-7">
													'.$sv['tag'].'
													'.(preg_match('/softmail/is', $sv['tag']) ? '<script language="javascript" type="text/javascript"><!-- // --><![CDATA[
													softemail["'.$sk.'"] = false;
													//Add an event handler
													$_("'.$sk.'").onkeydown = function(){
														softemail["'.$sk.'"] = true;
													}
													// ]]></script>
													' : '').'
												</div>
											</div>';
												
										}
									echo '
									</div><br /><!---end of bg class--->
								';
								
								}
								
								echo '</div>
								</div>';
							
								//The Hidden groups 
								if(!empty($settings['hidden'])){
								
									foreach($settings['hidden'] as $sk => $sv){
								
										echo $sv['tag'].'
										'.(preg_match('/softmail/is', $sv['tag']) ? '<script language="javascript" type="text/javascript"><!-- // --><![CDATA[
										softemail["'.$sk.'"] = false;
										//Add an event handler
										$_("'.$sk.'").onkeydown = function(){
											softemail["'.$sk.'"] = true;
										}
										// ]]></script>
										' : '');
							
									}
								
								}
							
							}//End of if($settings)
						
							//Is it a SOFTCOPY, then Leave a NOTE
							if(!empty($setupcontinue)){
								echo '<br />
								<div class="px-4">
									<span class="sai_info">'.__('<b>NOTE</b>: This software will perform an upgrade using its own upgrade utility. To complete the upgrade, please visit the URL that will be shown once the files have been copied.').'</span>
								</div><br />';
							}
						
							echo '
							<div class="px-4">
								<span class="sai_info">'.__('Its recommended that you take a $0 backup$1 of the installation before upgrading.', array('<a href="'.$globals['index'].'act=backup&insid='.$softins['insid'].'">', '</a>')).'</span>
							</div><br />';
						
						
							if(count($array_keys) < 2){
							
								echo '
								<div class="px-4">
									<span class="sai_info">'.__('You will be Upgrading to : ').'<b>'.(!empty($scripts[$array_keys[0]]['name']) ? $scripts[$array_keys[0]]['name'] : $iscripts[$array_keys[0]]['name']).'</b>, '.__('Version ').' : <b>'.(!empty($scripts[$array_keys[0]]['version']) ? $scripts[$array_keys[0]]['version'] : $iscripts[$array_keys[0]]['ver']).'</b></span>
								</div><br />';
						
							}
						
							echo '<br />
							<p align="center">
								<input type="hidden" name="softsubmit" id="softsubmit" value="'.__('Upgrade').'" />
								<input type="submit" name="softsubmitbut" class="btn sai-button" id="softsubmitbut" value="'.__('Upgrade').'" /><span id="show_txt" style="display:none;"></span>
							</p><br />
									
							<input type="hidden" name="soft_status_key" id="soft_status_key" value="'.POSTval('soft_status_key', generateRandStr(32)).'" />
							
							<center>'.$return.'</center><br /><br />
							
						</div><br /><!--end of bg class-->
						'.csrf_display().'
					</form>
				</div>
				
				<div id="progress_bar" style="display: none; width: 100%;">
					<br />
					<div class="bg">
						<center>
							<div class="row sai_main_head" style="width:100%;" align="center">
								<div class="col-sm-5 col-xs-5" style="padding:0 10px 0 0; text-align:right;">
									<i class="fas fa-sync-alt fa-2x" style="color:#00A0D2;"></i>
								</div>
								<div class="col-sm-7 col-xs-7" style="padding-top:10px; padding-left:0; text-align:left;">'.__('Upgrading ').$scripts[$array_keys[0]]['name'].'</div>
							</div><hr /><br />
							
							<font size="4" id="progress_txt" style="width: 100%;">'.__('Checking the submitted data').'</font>
							<font style="font-size: 18px;font-weight: 400; width: 100%;" id="progress_percent">(0 %)</font><br /><br />
						</center>
						<table width="500" cellpadding="0" cellspacing="0" id="table_progress" border="0" align="center" height="28" style="border:1px solid #CCC; -moz-border-radius: 5px;
					-webkit-border-radius: 5px; border-radius: 5px;background-color:#efefef; width: 50%;">
							<tr>
								<td id="progress_color" width="1" style="background-image: url('.$theme['images'].'bar.gif); -moz-border-radius: 4px; -webkit-border-radius: 4px; border-radius: 4px;"></td>
								<td id="progress_nocolor">&nbsp;</td>
							</tr>
						</table>
						<br /><center>'.__('<b>NOTE:</b> This may take 3-4 minutes. Please do not leave this page until the progress bar reaches 100%').'</center>
						<br /><br />
					</div><!--end of bg class-->
				</div><br /><br />';
			}
		
		}elseif(!empty($upgraded)){
	
			// Check if the script supports Sign On
			$has_sign_on = has_sign_on($soft);
			
			$tmp_admiurl = $__settings['softurl'].'/'.(!empty($softins['admin_folder']) ? $softins['admin_folder'] : $software['adminurl']);
			
			$ahref_url = (empty($globals['disable_sign_on']) && !empty($has_sign_on) ? $globals['ind'].'act=sign_on&insid='.$softins['insid'].'&autoid='.srandstr(32) : $tmp_admiurl);

			$return = '';
			$role = '';

			if(can_wordpress_manager($soft)){
				$return .= '<a role="button" href="'.$globals['ind'].'act=wordpress&insid='.$softins['insid'].'"><span class="btn btn-outline-primary">'.__('Return to WordPress Management').'</span></a>&nbsp';
				
				$role = 'role="button"';
			}

			$return .= '<a '.$role.' href="'.script_link($soft).'&highlight='.$softins['insid'].'&postact=upgrade"><span class="btn btn-outline-primary">'.__('Return to Overview').'</span></a>';
			
			//Filter to customize the return to link
			$return = apply_filters('return_link', $return, $act);
			
			echo '
			<div class="bg">
				<div class="third_url p-3"><h5>'.(!empty($setupcontinue) ? __('You are one more step away from completing the upgrade process').'</h5><div id="setupcontinue_url" style="display:none;"></div>
				'.__('Please visit below link to complete the upgrade process.').'<br />
				<a href="'.$__settings['softurl'].'/'.$setupcontinue.'" target="_blank">'.$__settings['softurl'].'/'.$setupcontinue.'</a></div><div class="p-3">' : __('Congratulations, the software was upgraded successfully').'</h5><hr class="sai_main_head_hr" style="width:20%;">').'
				<p style="font-size:13px;">'.(!empty($setupcontinue) ?  __('After completing the upgrade process, you can access the $0 installation here', array($software['name'])) : $software['name'].' '.__('has been successfully upgraded at')).' : <br />
				<a href="'.$__settings['softurl'].'" target="_blank">'.$__settings['softurl'].'</a>
				'.(!empty($software['adminurl']) ? '<br />'.__('Admin URL').' : <a href="'.$ahref_url.'" target="_blank">'.$tmp_admiurl.'</a>' : '').'<br /><br />
				'.__('We hope the upgrade process was easy.').'<br /><br />
				'.(!empty($notes) ? __('The following are some important notes. It is highly recommended that you read them ').' : <br /> 
				<div class="sai_notes">'.softparse($notes, $soft).'</div><br /><br />' : '').'
				'.__("<b>NOTE</b>: $0 is just an auto installer and does not provide any support for the software. Please visit the software vendor's web site for support!", array(APP)).'<br /><br />
				'.__('Regards').',<br />
				'.__('$0 Auto Installer', array(APP)).'<br /><br />
				</p>
				<center>'.$return.'</center><br /><br />';
				if(empty($globals['upgrade_tweet_off']) || empty($globals['lictype'])){
					echo '
					<form method="get" action="https://twitter.com/intent/tweet" id="tweet" onsubmit="return dotweet(this);">
						<div class="row justify-content-center">
						<div class="col-sm-7">
						<div class="card">
							<div class="card-header" style="font-size:14px;">'.__('Tell your friends').' :</div>
							<div class="card-body">
								<div class="row">
									<div class="col-sm-12">
										<textarea name="text" cols="80" row="3" style="resize:none; width:100%;">'.loadtweetdata('upgrade_tweet', __('I just upgraded #[[SCRIPTNAME]] on [[softurl]] via #[[APP]] #[[TYPE]]')).'</textarea>
									</div>
								</div>
								<br />
								<div class="row">
									<div class="col-sm-12">
										<input type="submit" value="Tweet!" class="btn btn-info pull-right" onsubmit="return false;" id="twitter-btn" />
									</div>
								</div>
							</div>
						</div>
						</div>
						</div>
					</form>';
				}
					
			echo '
			</div><!--end of bg class--><!--PROC_DONE-->
			<br /><br /><br /><br />';
			
		}
		
	echo '
	</div>';
	
	softfooter();

}

?>