ACC SHELL

Path : /srv/www/vhosts/agroing/web/controllers/
File Upload :
Current File : /srv/www/vhosts/agroing/web/controllers/install.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Install extends MY_Controller {
	private $db_structure_sql_file='./sql/full_db_structure.sql';
	private $multiple_query=array();
	private $db_structure_is_ready=false;
	private $db_is_ready=false;
	private $count_of_sites=0;

	function __construct(){
		parent::__construct();
	}

	/**
	 * vytvori db strukturu a naplni ji pocatecnimi daty
	 */
	public function index () {
		var_dump($this);
		$this->create_db_structure();
		$this->insert_first_data();
		if($this->db_is_ready){
			header('location:http://'.$_SERVER['SERVER_NAME'].'/admin');
			exit();
		}
		else{
			echo 'Databaze neni pripravena.';
		}
	}
	
	public function update($v){
		if($v==19){
			$res = $this->db->query("select * from urls")->result_array();
			foreach($res as $row){
				$idr = array_pop(explode("/",trim($row['url'],"/")));
				if(is_numeric($idr)){
					$this->db->query("update urls set id_relations='".$idr."' where id='".$row['id']."'");
				}
			}
			echo "urls updated";
		}
		if($v=="slashes"){
			$r = $this->db->query("select * from urls where sef like '%/%'")->result_array();
			foreach($r as $row){
				$this->db->query("update urls set sef='".trim($row['sef'],"/")."' where id='".$row['id']."'");
			}
		}
		if($v=="w"){
			$r = $this->db->query("select * from sites_parts where content like '%i_files%'")->result_array();
			$names = $this->db->query("select * from settings where name='name'")->result_array();
			foreach($r as $row){
				foreach($names as $n){
					$row['content'] = str_replace("views/".$n['value']."/www/i_files","w/".$n['value']."/files",$row['content']);
				}
				$this->db->query("update sites_parts set content='".str_replace("'","\\'",$row['content'])."' where id='".$row['id']."'");
			}
			foreach($names as $n){
				$count = 0; $ok = true;
				$path = './views/'.$n['value'].'/www/i_files';
				$dest = './w/'.$n['value'];
				if ($handle = @opendir($path)) {
					if(!is_dir($dest)){
						mkdir($dest);
						mkdir($dest."/files");
						mkdir($dest."/cache");
					}
					echo "copy files from: ".$n['value']."<br>";
					while (false !== ($entry = readdir($handle))) {
						if(strlen($entry)>2){
							if(!file_exists($dest."/files/".$entry)){
								
								if(copy($path."/".$entry,$dest."/files/".$entry)){
									$count++;
								}else{
									$ok = false;
								}
							}
						}
					}
					closedir($handle);
					if($ok && $count && is_dir($path)){
						$this->delete_folder($path);
						$this->delete_folder('./views/'.$n['value'].'/www/i_cache');
					}
				}
			}
		}	
	}

	/**
	 * zjisti pocet stran v db
	 */
	private function set_count_of_sites(){
		$count=$this->db->query("SELECT COUNT(*) AS `count` FROM `sites`;")->row_array();
		$this->count_of_sites=$count['count'];
	}

	/**
	 * do pripravene db nahrne pocatecni data
	 */
	private function insert_first_data(){
		$res=array();
		if($this->db_structure_is_ready){
			$this->set_count_of_sites();
			if(!$this->count_of_sites){
				$res[]=$this->db->query("SET FOREIGN_KEY_CHECKS=0;");
				$res[]=$this->db->query("INSERT INTO `sites` (`id`, `tags`, title) VALUES ('1', '#root#', 'root')");
				$res[]=$this->db->query("INSERT INTO `relations` (`id`, `id_sites`, `id_tests`, `parent_id`, `parent_site_id`, `table`, `list`, `depth`, `lft`, `rgt`) VALUES ('1', '1', NULL, NULL, NULL, 'sites', '1', '0', '1', '2');");
				$res[]=$this->db->query("INSERT INTO `users` (`id`, `login`, `email`, `password`, `access_root`, `admin`, `programator`) VALUES ('1', 'admin', '', MD5(''), '1', '1', '1');");
				if(!in_array(false, $res)){
					$this->db_is_ready=true;
					Tools::succ("pokud byla databáze prázdná, byla vložena první stránka z stagem 'root'
						a administrátor admin bez hesla");
				}
			}
			// v tabulce uz data jsou...
			else{
				$this->db_is_ready=true;
			}
		}
	}

	/**
	 * vytvori databazovou strukturu
	 * pokud uz tabulky existuji pak zustanou stavajici a tabulky se budou povazovat za pripravene
	 */
	private function create_db_structure(){
		$return=array();
		$this->parse_mysql_dump();
		if(count($this->multiple_query)){
			foreach($this->multiple_query as $query){
				$res=mysql_query($query);
				if($res){
					$return[]=true;
				}
				else{
					switch(mysql_errno()){
						// duplicity nebere jako chybu
						case 1050:
							$return[]=true;
							break;
						// vsechno ostatni je chyba...
						default:
							$return[]=false;
							break;
					}
				}
			}
		}
		$this->db_structure_is_ready=(!count($return) || in_array(false, $return) ? false : true);
	}

	/**
	 * rozparsuje soubor s dumpem db na jednotlive dotazy a vlozi je do pole
	 */
	private function parse_mysql_dump() {
		$file_content = file($this->db_structure_sql_file);
		$query = false;
		foreach($file_content as $sql_line) {
			$tsl = trim($sql_line);
			if (($sql_line != "") && (substr($tsl, 0, 2) != "--") && (substr($tsl, 0, 1) != "#")) {
				$query .= $sql_line;
				if(preg_match("/;\s*$/", $sql_line)) {
					$this->multiple_query[]=trim($query);
					$query = false;
				}
			}
		}
	}
	
	private function delete_folder($tmp_path){ 
		if(!is_writeable($tmp_path) && is_dir($tmp_path)){chmod($tmp_path,0777);} 
			$handle = opendir($tmp_path); 
		while($tmp=readdir($handle)){ 
			if($tmp!='..' && $tmp!='.' && $tmp!=''){ 
				if(is_writeable($tmp_path."/".$tmp) && is_file($tmp_path."/".$tmp)){ 
						unlink($tmp_path."/".$tmp); 
				}elseif(!is_writeable($tmp_path."/".$tmp) && is_file($tmp_path."/".$tmp)){ 
					chmod($tmp_path."/".$tmp,0666); 
					unlink($tmp_path."/".$tmp); 
				} 

				if(is_writeable($tmp_path."/".$tmp) && is_dir($tmp_path."/".$tmp)){ 
						delete_folder($tmp_path."/".$tmp); 
				}elseif(!is_writeable($tmp_path."/".$tmp) && is_dir($tmp_path."/".$tmp)){ 
						chmod($tmp_path."/".$tmp,0777); 
						delete_folder($tmp_path."/".$tmp); 
				} 
			} 
		} 
		closedir($handle); 
		rmdir($tmp_path); 
		if(!is_dir($tmp_path)){return true;} 
		else{return false;} 
	} 
}

?>

ACC SHELL 2018