<?php
/*
|--------------------------------------------------------------------------
| config.php 后台管理 基本配置管理
|--------------------------------------------------------------------------
*/
namespace DeverApp\Manage;

use Dever;
use Dever\Security\Internal;

class Config
{
	/**
     * project
     *
     * @var string
     */
	private $project;

	/**
     * dir
     *
     * @var string
     */
	protected $type;
	
	/**
     * config
     *
     * @var array
     */
	protected $config;

	/**
     * __construct
     * 
     * @return mixed
     */
	public function __construct()
	{
		$this->project 	= Dever::input('key');
		$this->type 	= Dever::input('type', 'config');
		
		$config = Dever::load('manage/project.get');
		if(isset($config[$this->project]))
		{
			$this->config 		= $config[$this->project];
		}
		
		$type = array('config', 'view', 'database', 'api');

		if(!in_array($this->type, $type))
		{
			Dever::out('error');
		}
	}
	
	/**
     * get
     * 
     * @return mixed
     */
	public function get()
	{
		$dir 	= Dever::input('dir');
		$name 	= Dever::input('name');
		
		if($dir && $name)
		{
			$dir = $dir . '/' . $name;
		}
		else
		{
			$dir = $this->type;
		}

		Dever::setInput('dir', $dir);
		
		$result = $this->read($dir);
		
		return $result;
	}
	
	/**
     * read
     * @param string $dir
     * 
     * @return mixed
     */
	private function read($dir)
	{
		$file	= $this->dir($dir);

		if(is_dir($file))
		{
			$result = scandir($file);
			foreach($result as $k => $v)
			{
				if($v == '.' || $v == '..')
				{
					unset($result[$k]);
				}
			}
			
			return $result;
		}
		elseif(is_file($file))
		{
			Dever::$global['temp']['delete'] = '';
			$data_dir = $this->dir($dir, 'data/');
			if(is_file($data_dir))
			{
				$file = $data_dir;
				$name = str_replace(DEVER_PATH, '', $file);
				$md5 = Internal::encode($name);

				$delete = Dever::url('config.del?file=' . $name . '&md5=' . $md5);
				Dever::$global['temp']['delete'] = '<br /><a href="'.$delete.'" class="oper_6">点此删除</a>';
			}
			else
			{
				$name = str_replace(DEVER_PATH, '', $file);
				$md5 = Internal::encode($name);
			}

			$result[] = array('content' => file_get_contents($file), 'file' => $name, 'md5' => $md5, 'key' => $this->project);
			
			return $result;
		}
		else
		{
			Dever::out('error');
		}
	}
	
	/**
     * dir
     * @param string $dir
     * 
     * @return mixed
     */
	private function dir($dir, $path = '')
	{
		if(!$this->config['path'])
		{
			
		}
		if($path)
		{
			return Dever::path(DEVER_PATH . $path, $this->config['path'] . $dir);
		}
		return DEVER_PATH . $this->config['path'] . $dir;
	}

	/**
     * set 将内容写入到配置文件中（备份）
     * 
     * @return string
     */
	public function set()
	{
		$file 	= Dever::input('file');
		$value	= Dever::input('value');
		$name 	= Dever::input('name');
		$md5	= Internal::decode(Dever::input('md5'));

		if($file == $md5)
		{
			$file = DEVER_PATH . $file;
			if(is_file($file))
			{
				file_put_contents($this->dir($name, 'data/'), $value);
			}
		}

		highlight_string($value);die;
	}

	/**
     * del 删除后台建立的内容
     * 
     * @return mixed
     */
	public function del()
	{
		$file 	= Dever::input('file');
		$md5	= Internal::decode(Dever::input('md5'));

		if(strpos($file, 'data/') !== false && $file == $md5)
		{
			$file = DEVER_PATH . $file;
			if(is_file($file))
			{
				@unlink($file);
			}
		}

		Dever::out('yes');
	}
}
