<?php
/*
+--------------------------------------------------------------------------
|   TanyaCMS - Question and Answer Sofware
|   -----------------------------------------------------------------------
|   by fbschool dev team
|   (c) 2011 - 2012 FBSCHOOL
|   http://www.fbschool.com/tanyacms
|   -----------------------------------------------------------------------
|   This is NOT a freeware, use is subject to license terms
|   Support: 13455n@gmail.com
+--------------------------------------------------------------------------
*/


if (!defined('IN_TANYACMS'))
{
	die;
}

class setting extends ASK_CONTROLLER
{

	function get_permission_action()
	{
	
	}

	public function setup()
	{
		admin_session_class::init($this->get_permission_action());
	}

	public function index_action()
	{
		$this->setting_action();
	}

	public function setting_action()
	{
		$menu = array(
			101 => 'site_info',
			102 => 'reg_visit',
			103 => 'site_func',
			104 => 'content',
			106 => 'integral_reputation', 
			107 => 'email', 
			108 => 'open', 
			109 => 'cache', 
			110 => 'sys_info', 
			201 => 'view_set', 
			305 => 'sensitive_words', 
			404 => 'user_reputation', 
			111 => 'user_privilege',
			203 => 'editor',
			603 => 'today_topics',
		);
		
		$type = $_GET['type'];
		
		if (!in_array($type, $menu))
		{
			HTTP::redirect('?/admin/main/');
		}
		
		if ($type == 'view_set')
		{
			TPL::assign('styles', $this->model('setting')->get_ui_styles());
		}
		
		$this->crumb('System setting', 'admin/setting/type-' . $type);
		
		TPL::import_js('admin/js/setting.js');
		
		TPL::assign('setting', get_setting());
		
		TPL::assign('menu_list', $this->model('admin_group')->get_menu_list($this->user_info['group_id'], array_search($type, $menu)));
		
		TPL::output("admin/setting/" . $type);
	}


	/**
	 * Save setting
	 */
	public function sys_save_ajax_action()
	{
		define('IN_AJAX', TRUE);
		
		if ($_POST['upload_dir'] && preg_match('/(.*)\/$/i', $_POST['upload_dir']))
		{
			H::ajax_json_output(ASK_APP::RSM(null, "-1", "Absolute path of uploaded file without end /"));
		}
		
		if ($_POST['upload_url'] && preg_match('/(.*)\/$/i', $_POST['upload_url']))
		{
			H::ajax_json_output(ASK_APP::RSM(null, "-1", "External URL adress of uploaded category without end /"));
		}
		
		if ($_POST['request_route_custom'])
		{
			$_POST['request_route_custom'] = trim($_POST['request_route_custom']);
			
			if ($request_routes = explode("\n", $_POST['request_route_custom']))
			{
				foreach ($request_routes as $key => $val)
				{
					if (! strstr($val, '==='))
					{
						continue;
					}
					
					list($m, $n) = explode('===', $val);
					
					if (substr($n, 0, 1) != '/' || substr($m, 0, 1) != '/')
					{
						H::ajax_json_output(ASK_APP::RSM(null, -1, "URL Customize route rule error: URL Must begin with /"));
					}
					
					if (strstr($m, '/admin') || strstr($n, '/admin'))
					{
						H::ajax_json_output(ASK_APP::RSM(null, -1, "URL custom route rule cannot set /admin route"));
					}
				}
			}
		}
		
		if ($_POST['censoruser'])
		{
			$_POST['censoruser'] = trim($_POST['censoruser']);
		}
		
		if ($_POST['report_reason'])
		{
			$_POST['report_reason'] = trim($_POST['report_reason']);
		}
		
		if ($_POST['sensitive_words'])
		{
			$_POST['sensitive_words'] = trim($_POST['sensitive_words']);
		}
		
		
		if (!$_POST['newer_content_type'])
		{
			$_POST['newer_content_type'] = array();
		}

		$vars = $this->model('setting')->check_vars($_POST);	//Filtering parameters
		
		$retval = $this->model('setting')->set_vars($vars);		//Save the parameters to the database
		
		if ($retval)
		{
			H::ajax_json_output(ASK_APP::RSM(null, "1", "System setting has been updated"));
		}
		else
		{
			H::ajax_json_output(ASK_APP::RSM(null, "-1", "Failed to modify system settings"));
		}
	}

	public function test_email_setting_action()
	{
		define('IN_AJAX', TRUE);
		
		$smtp_config = array(
			'smtp_server' => $_POST['smtp_server'], 
			'smtp_ssl' => $_POST['smtp_ssl'], 
			'smtp_port' => $_POST['smtp_port'], 
			'smtp_username' => $_POST['smtp_username'], 
			'smtp_password' => $_POST['smtp_password']
		);
		
		$core_mail = new core_mail();
		
		$connect_result = $core_mail->connect($_POST['email_type'], $smtp_config);
		
		if (is_string($connect_result))
		{
			H::ajax_json_output(ASK_APP::RSM(null, -1, "Mail server connection failed, return information: " . strip_tags($connect_result)));
		}
		
		$sendmail_result = $core_mail->send_mail($_POST['from_email'], get_setting('site_name'), $_POST['test_email'], $_POST['test_email'], get_setting('site_name') . " - Mailbox server configuration testing", 'This is a test message, received the message indicates that the Mailbox server configuration is successful website');
		
		if (is_object($sendmail_result))
		{			
			H::ajax_json_output(ASK_APP::RSM(null, 1, "Testing the Email has been sent, please check your email in order to determine the configuration is correct"));
		}
		else
		{
			H::ajax_json_output(ASK_APP::RSM(null, 1, "Mail test failed, return: " . strip_tags($sendmail_result)));
		}
	}
}