<?php
/**
 * Copyright (C) LJCMS All rights reserved.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2016.03.19
 */

class settingAModel extends X {
    
    public function getOptions($option) {
        if($option=="site_base" || $option=="site_base_en"){
            $sql = "SELECT * FROM ".DB_PREFIX."options WHERE optionname='$option'";
            $data= parent::$obj->fetch_first($sql);
            parent::loadUtil('handle');
            $arr=XHandle::dounSerialize($data['optionvalue']);
            $arr['tjcode']=$data['tjcode'];
            return $arr;
        } else {
               parent::loadLib('option');
               return XOption::get($option); 
        }
       
    }
     public function getcode($option) {
        
            $sql = "SELECT * FROM ".DB_PREFIX."options WHERE optionname='$option'";
            $data= parent::$obj->fetch_first($sql);
            return $data;
        
       
    }   
  
    public function addcode($arr){
        $result = parent::$obj->insert(DB_PREFIX.'options', $arr);
        return $result;
    }
    public function savecode($option,$array){
        $result = parent::$obj->update(DB_PREFIX.'options', $array, "optionname='".$option."'");
        return $result;
    }

    public function doSave($option, $array,$tjcode) {
        
        $data = serialize($array);
        if($option=="site_base" || $option=="site_base_en"){
              parent::loadLib('option');
             XOption::updateOption1($option, $data,$tjcode);
        } else {
               parent::loadLib('option');
             XOption::updateOption($option, $data);
        }
       
        return true;
    }
    
    
    public function doUpdate($option, $string){
        $array = array(
            'optionvalue'=>$string,
        );
        $result = parent::$obj->update(DB_PREFIX.'options', $array, "optionname='".$option."'");
        if (true === $result){
            
            $cache = parent::import('cache', 'lib');
            $cache->updateCache('options');
            unset($cache);
            return true;
        }
        else {
            return false;
        } 
    }
    
    
    public function doUpdateCache() {
        $cache = parent::import('cache', 'lib');
        $cache->updateCache();
        unset($cache);
    }
    
    
    public function doRewrite($type='') {
        $res = false;
        $rewrite_name = ".htaccess";
        parent::loadUtil('file');
        if (empty($type)) {
            
            $res = XFile::delFile($rewrite_name);
        }
        
        elseif ($type == 'apache') {
            $rewrite_content = "#LJCMS rewrite for apache
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]
</IfModule>
            ";
            
            XFile::delFile($rewrite_name);
            XFile::createFile($rewrite_name);
            return XFile::writeFile($rewrite_name, $rewrite_content);
        }
        
        elseif ($type == 'iis6') {
            $rewrite_content = "#LJCMS rewrite or iis rewrite 3.1
RewriteBase ".LJCMS_ROOT."
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
            ";
            
            XFile::delFile($rewrite_name);
            XFile::createFile($rewrite_name);
            return XFile::writeFile($rewrite_name, $rewrite_content);
        }

        
        elseif ($type == "iis7") {
            $rewrite_name = "Web.config";
            $rewrite_content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".
"<!-- LJCMS Rewrite to IIS7+ -->
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name=\"OrgPage\" stopProcessing=\"true\">
          <match url=\"^(.*)$\" />
          <conditions logicalGrouping=\"MatchAll\">
            <add input=\"{HTTP_HOST}\" pattern=\"^(.*)$\" />
            <add input=\"{REQUEST_FILENAME}\" matchType=\"IsFile\" negate=\"true\" />
            <add input=\"{REQUEST_FILENAME}\" matchType=\"IsDirectory\" negate=\"true\" />
          </conditions>
          <action type=\"Rewrite\" url=\"index.php/{R:1}\" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>";
            
            XFile::delFile($rewrite_name);
            XFile::createFile($rewrite_name);
            $res = XFile::writeFile($rewrite_name, $rewrite_content);
        }
        elseif ($type == 'nginx') {
            $rewrite_content = "#LJCMS rewrite for nginx
location / {
    index index.php;
    if (!-e \$request_filename) {
        rewrite (.*) /index.php break;
    }
}
            ";
            
            XFile::delFile($rewrite_name);
            XFile::createFile($rewrite_name);
            return XFile::writeFile($rewrite_name, $rewrite_content);
        }
        return $res;
    }
    public function getsetup(){
        $attr_sql = "SELECT * FROM ".DB_PREFIX."setup ";
        $attr_data = parent::$obj->getall($attr_sql);
        return $attr_data;
    }
    public function doUpdatesetup($name,$value){
        $array['setvalue']=$value;
        $result = parent::$obj->update(DB_PREFIX.'setup', $array, "setname='".$name."'");
        if (true === $result){
            return true;
        } else {
            return false;
        }
    }
     
}
?>
