<?php 
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST, ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace admin\model;
use \temmoku\db;
class upgrade {
	
	public function get_modular(){
		$data=db::select('*')->from(jab.'modular' )->query();
		foreach($data AS $row){
			$row['authorization']=C($row['initial_name'].'_authorization');
			$_dataDB[]=$row;
		}
		return $_dataDB;
	}
	
	public function get_plugin(){
		
		$data=db::select('*')->from(jab.'plugin' )->query();
		foreach($data AS $row){
			$row['authorization']=C($row['initial_name'].'_authorization');
			$_dataDB[]=$row;
		}
		return $_dataDB;
	}
	
	public function upgarde_plugin($olddata){
		$request_array=[
			'id'=>$_POST['id'],
			'upgrade_temporary_number'=>$_POST['upgrade_temporary_number'],
			'i'=>$_POST['to_i']
		];
		$_data=http_request(Upgrade_URL.'upgrade/update/',$request_array);
		$data=json_decode($_data,true);
		if($data['status']=='-9999'){
			$i=$_POST['i']+1;
			$ii=$_POST['ii']+1;
			
			$_i=0;
			foreach($olddata['plugin'] AS $name=>$row){
				$_i++;
				//当前正在升级的模块
				if($_POST['i']==$_i){
					//当前正在升级的数量，大于了存在的数量
					if($ii>count($row)){
						if($i>count($olddata['plugin'])){
							db::update(jab.'plugin')->cols(['version'=>$data['version'],'update'=>$data['update']])->where("dir= '$name' ")->query();
							$array=[
								'action'=>'',
								'text'=>'插件升级完毕',
							];
							break;
						}else{
							$h_i=0;
							foreach($olddata['plugin'] AS $_name=>$_row){
								$h_i++;
								if($h_i==$i){
									$h_ii=0;
									foreach($_row AS $aid=>$version){
										$h_ii++;
										if($h_ii==1){
											break;
										}
									}
									break;
								}
							}
							db::update(jab.'plugin')->cols(['version'=>$data['version'],'update'=>$data['update']])->where("dir= '$name' ")->query();
							$array=[
								'action'=>'plugin',
								'i'=>$i,
								'ii'=>1,
								'to_i'=>0,
								'id'=>$aid,
								'text'=>'插件“'.$name.'”已经升级完毕',
								'text1'=>'开始升级插件“'.$_name.'”至"'.$version,
								'data'=>$data
							];
						}
					}else{
						$_ii=0;
						foreach($row AS $aid=>$version){
							$_ii++;
							
							if($_POST['ii']==$_ii){
								$version1=$version;
							}
								
							if($_ii==$ii){
								$_aid=$aid;
								$_version=$version;
							}
						}
						db::update(jab.'plugin')->cols(['version'=>$data['version'],'update'=>$data['update']])->where("dir= '$name' ")->query();
						$array=[
							'action'=>'plugin',
							'i'=>$_POST['i'],
							'ii'=>$ii,
							'to_i'=>0,
							'id'=>$_aid,
							'text'=>'插件“'.$name.'”已经升级至'.$version1."将继续升级至".$_version."版本"
						];
						break;
					}
				}
			}
		}else{
			$i==0;
			foreach($olddata['plugin'] AS $name=>$row){
				$i++;
				if($i==$_POST['i']){
					$ii=0;
					foreach($row AS $aid=>$_version){
						$ii++;
						if($ii==$_POST['ii']){
							break;
						}
					}
					break;
				}
			}
			$file=APP_PATH.'plugin/'.$name.'/'.$data['return']['file'];
			$is_create=true;
			if(substr(strrchr($file, '.'), 1)=='sql'){
				$rows=db::select('*')->from(jab.'plugin')->where("dir= '$name' ")->row();
				//替换表前戳
				$sql=str_replace ('temmoku_'.$row['initial_name']."_",jab.'plugin_'.$rows['table'],$data['return']['text']);
				$explode = sql_regular_replace($sql);
				$this->sql($explode);
				$text="倒入数据库数据成功";
				$is_create=false;
			}elseif(substr(strrchr($file, '.'), 1)=='newsql'){//替换源站的数据库文件
				$oldsql=APP_PATH.'plugin/'.$name."/data.sql";
				write_file($oldsql,$data['return']['text']);
				$text="替换源数据库文件成功";
				$is_create=false;
			}else{
				//如果是模板需要重新定义模板位置
				if(in_array('view',explode('/',$data['return']['file']))){
					$file=Temmoku_PATH.$data['return']['file'];
				}
				//如果是公共静态文件也需要重新定义目录
				if(in_array('public',explode('/',$data['return']['file']))){
					$file=Temmoku_PATH.$data['return']['file'];
				}
				if(is_file($file)){
					//定义一个本地文件名称
					$backups=Temmoku_PATH.'backups/plugin/'.$name.'/'.date("ymd",NOWTIME).'/'.$_version.'/'.$data['return']['file'];
					//创建备份目录
					write_dir($backups);
					//备份文件
					rename ( $file , $backups );
					if(!is_file($backups)){
						copy( $file , $backups );
						//卸载文件
						@unlink ($file);
					}
					$text="本地存在此文件，已经备份，已经升级文件".$data['return']['file'];
				}else{
					$text="本地不存在此文件，无需备份，直接".$data['return']['file'];
				}
			}
			
			if($data['return']['is_img']==true){
				write_dir($file);
				copy($data['return']['text'],$file);
				$is_create=false;
			}
			
			$t_i=0;
			foreach($olddata['plugin'] AS $name=>$row){
				$t_i++;
				if($t_i==$_POST['i']){
					break;
				}
			}
			
			$row=db::select('*')->from(jab.'plugin')->where("dir= '$name' ")->row();
			$content=$this->replace('plugin\\\\'.$row['initial_name'],'plugin\\\\'.$name,$data['return']['text']);
			if($is_create==true){
				//写入这个文件
				write_file($file,$content);
			}
			$array=[
					'action'=>'plugin',
					'i'=>$_POST['i'],
					'ii'=>$_POST['ii'],
					'to_i'=>$_POST['to_i']+1,
					'id'=>$_POST['id'],
					'text'=>$text,
					'data'=>$data
				];
		}
		echo json_encode($array);
	}
	public function upgarde_modular($olddata){
		$request_array=[
			'id'=>$_POST['id'],
			'upgrade_temporary_number'=>$_POST['upgrade_temporary_number'],
			'i'=>$_POST['to_i']
		];
		$_data=http_request(Upgrade_URL.'upgrade/update/',$request_array);
		$data=json_decode($_data,true);
	
		if($data['status']=='-9999'){
			$i=$_POST['i']+1;
			$ii=$_POST['ii']+1;
			
			$_i=0;
			foreach($olddata['modular'] AS $name=>$row){
				$_i++;
				//当前正在升级的模块
				if($_POST['i']==$_i){
					//当前正在升级的数量，大于了存在的数量
					if($ii>count($row)){
						//大于所有模块的数量
						if($i>count($olddata['modular'])){
							if($olddata['plugin']){
								$p_i=0;
								foreach($olddata['plugin'] AS $name=>$row){
									$p_i++;
									if($p_i==1){
										$p_ii=0;
										foreach($row AS $aid=>$version){
											$p_ii++;
											if($p_ii==1){
												break;
											}
										}
										break;
									}
								}
								$array=[
									'action'=>'plugin',
									'i'=>1,
									'ii'=>1,
									'to_i'=>0,
									'id'=>$aid,
									'text'=>'模块升级完毕,开始升级插件功能',
									'text1'=>'开始升级'.$name."插件至".$version."版本",
								];
							}else{
								
								
								db::update(jab.'modular')->cols(['version'=>$data['version'],'update'=>$data['update']])->where("dir= '$name' ")->query();
								
								$array=[
									'action'=>'',
									'text'=>'模块升级完毕',
								];
							}
							break;
						}else{
							//还存在模块
							$h_i=0;
							foreach($olddata['modular'] AS $_name=>$_row){
								$h_i++;
								if($h_i==$i){
									$h_ii=0;
									foreach($_row AS $aid=>$version){
										$h_ii++;
										if($h_ii==1){
											break;
										}
									}
									break;
								}
							}
							db::update(jab.'modular')->cols(['version'=>$data['version'],'update'=>$data['update']])->where("dir= '$name' ")->query();
							$array=[
								'action'=>'modular',
								'i'=>$i,
								'ii'=>1,
								'to_i'=>0,
								'id'=>$aid,
								'text'=>'模块“'.$name.'”已经升级完毕',
								'text1'=>'开始升级模块“'.$_name.'”模块至"'.$version,
								'data'=>$data
							];	
						}
					}else{
						$_ii=0;
						foreach($row AS $aid=>$version){
							$_ii++;
							if($_POST['ii']==$_ii){
								$version1=$version;
							}
							if($_ii==$ii){
								$_aid=$aid;
								$_version=$version;
							}
						}
						
						db::update(jab.'modular')->cols(['version'=>$data['version'],'update'=>$data['update']])->where("dir= '$name' ")->query();
						$array=[
							'action'=>'modular',
							'i'=>$_POST['i'],
							'ii'=>$ii,
							'to_i'=>0,
							'id'=>$_aid,
							'text'=>'模块“'.$name.'”已经升级至'.$version1."将继续升级至".$_version."版本"
						];
						break;
					}
				}
			}
		}else{
			$i==0;
			foreach($olddata['modular'] AS $name=>$row){
				$i++;
				if($i==$_POST['i']){
					$ii=0;
					foreach($row AS $aid=>$_version){
						$ii++;
						if($ii==$_POST['ii']){
							break;
						}
					}
					break;
				}
			}
			
			$file=APP_PATH.$name.'/'.$data['return']['file'];
			$is_create=true;
			if(substr(strrchr($file, '.'), 1)=='sql'){
				$rows=db::select('*')->from(jab.'modular')->where("dir= '$name' ")->row();
				//替换表前戳
				$sql=str_replace ('temmoku_'.$row['initial_name']."_",jab.$rows['table'],$data['return']['text']);
				
				$explode = sql_regular_replace($sql);
				$this->sql($explode);
				$text="倒入数据库数据成功";
				$is_create=false;
			}elseif(substr(strrchr($file, '.'), 1)=='newsql'){//替换源站的数据库文件
				$oldsql=APP_PATH.$name."/data.sql";
				write_file($oldsql,$data['return']['text']);
				$text="替换源数据库文件成功";
				$is_create=false;
			}else{
				//如果是模板需要重新定义模板位置
				if(in_array('view',explode('/',$data['return']['file']))){
					$file=Temmoku_PATH.$data['return']['file'];
				}
				
				//如果是公共静态文件也需要重新定义目录
				if(in_array('public',explode('/',$data['return']['file']))){
					$file=Temmoku_PATH.$data['return']['file'];
				}
				
				if(is_file($file)){
					//定义一个本地文件名称
					$backups=Temmoku_PATH.'backups/modular/'.$name.'/'.date("ymd",NOWTIME).'/'.$_version.'/'.$data['return']['file'];
					//创建备份目录
					write_dir($backups);
					//备份文件
					rename ( $file , $backups );
					if(!is_file($backups)){
						copy( $file , $backups );
						//卸载文件
						@unlink ($file);
					}
					$text="本地存在此文件，已经备份，已经升级文件".$data['return']['file'];
				}else{
					$text="本地不存在此文件，无需备份，直接升级文件".$data['return']['file'];
				}
			}
			
			if($data['return']['is_img']==true){
				write_dir($file);
				copy($data['return']['text'],$file);
				$is_create=false;
			}
				
			$t_i=0;
			foreach($olddata['modular'] AS $name=>$row){
				$t_i++;
				if($t_i==$_POST['i']){
					break;
				}
			}
			
			$row=db::select('*')->from(jab.'modular')->where("dir= '$name' ")->row();
			
			$oldname=$row['initial_name'];
			$newname=$name;
			$content=$this->replace($oldname,$newname,$data['return']['text']);
			if($is_create==true){
				//写入这个文件
				write_file($file,$content);
			}
			$array=[
					'action'=>'modular',
					'i'=>$_POST['i'],
					'ii'=>$_POST['ii'],
					'to_i'=>$_POST['to_i']+1,
					'id'=>$_POST['id'],
					'text'=>$text,
					'data'=>$data
				];
		}
		echo json_encode($array);
	}
	
	public function replace($oldname,$newname,$content){
		$replace='/'.$oldname.'\\\\/is';
		$content=preg_replace( $replace , "$newname\\\\"  , $content );
		$content=preg_replace( '/\\\\/' , "\\\\\\"  , $content );
		$replace="/namespace[\s]+".$oldname."/i";
		$content=preg_replace($replace, "namespace ".$newname, $content);
		return $content;
	}
	
	public function upgarde_main($olddata){
		
		$request_array=[
			'id'=>$_POST['id'],
			'upgrade_temporary_number'=>$_POST['upgrade_temporary_number'],
			'i'=>$_POST['to_i']
		];
		$_data=http_request(Upgrade_URL.'upgrade/update/',$request_array);
		$data=json_decode($_data,true);
		if($data['status']=='-9999'){
			$i=$_POST['i']+1;
			if($i > count($olddata['mvc'])){
				if($olddata['modular']){
					$I=0;
					foreach($olddata['modular'] AS $modular=>$row){
						$I++;
						if($I==1){
							$ii=0;
							foreach($row AS $aid=>$version){
								$ii++;
								if($ii==1){
									break;
								}
							}
							break;
						}
					}
					$array=[
						'action'=>'modular',
						'i'=>1,
						'ii'=>1,
						'to_i'=>0,
						'id'=>$aid,
						'text'=>'主程序已经升级完毕',
						'text1'=>'开始升级“'.$modular.'”模块'
					];
				}elseif($olddata['plugin']){
					
				}else{
					$array=[
						'action'=>'ok',
						'text'=>'程序已经升级完毕'
					];
				}
			}else{
				$ii=0;
				foreach($olddata['mvc'] AS $aid=>$version){
					$ii++;
					if($ii==$_POST['i']){
						$version1=$version;
					}
					if($ii==$i){
						$version2=$version;
					}
				}
				$array=[
					'action'=>'mvc',
					'i'=>$i,
					'to_i'=>0,
					'id'=>$_POST['id'],
					'text'=>'主程序已经升级至'.$version1."将继续升级至".$version2."版本"
				];
			}
		}else{
			$file=Temmoku_PATH.$data['return']['file'];
			if(substr(strrchr($file, '.'), 1)=='sql'){
				//替换表前戳
				$sql=str_replace ('temmoku_',jab,$data['return']['text']);
				$explode = sql_regular_replace($sql);
				$this->sql($explode);
				$text="倒入数据库数据成功";
			}elseif(substr(strrchr($file, '.'), 1)=='newsql'){//替换源站的数据库文件
				$oldsql=APP_PATH."install/conf/data.sql";
				write_file($oldsql,$data['return']['text']);
				$text="替换源数据库文件成功";
			}else{
				if(is_file($file)){
					//定义一个本地文件名称
					$backups=Temmoku_PATH.'backups/cms/'.date("ymd",NOWTIME).'/'.$data['version'].'/'.$data['return']['file'];
					//创建备份目录
					write_dir($backups);
					//备份文件
					rename ( $file , $backups );
					if(!is_file($backups)){
						copy( $file , $backups );
						//卸载文件
						@unlink ($file);
					}
					$text="本地存在此文件，已经备份，已经升级".$data['return']['file'];
				}else{
					$text="本地不存在此文件，无需备份，直接升级".$data['return']['file'];
				}
				if($data['return']['is_img']==true){
					write_dir($file);
					copy($data['return']['text'],$file);
					$is_create=false;
				}else{
					//写入这个文件
					write_file($file,$data['return']['text']);
				}
			}
			$array=[
				'action'=>'mvc',
				'i'=>$_POST[i],
				'to_i'=>$_POST['to_i']+1,
				'id'=>$_POST['id'],
				'text'=>$text
			];
		}
		echo json_encode($array);
	}
	
	private function sql($sql){
		foreach($sql AS $table){
			db::query($table);
		}
	}
}
?>