<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * AMP的相关方法
 */

class Amplib
{

    var $CI;

    public function __construct()
    {
        $this->CI = &get_instance();
        $this->CI->load->model('Information_model');
        $this->CI->load->model('InfoMetas_model');
        $this->CI->load->model('Logs_model');
    }

    public function edit($ic_id)
    {
        $data = array();
        $data['information'] = $this->CI->Information_model->detail_by_ic_id($ic_id);
        if ($data['information'] == FALSE) {
            show_404();
        }
        //查询上一次保存记录
        $amp_log = $this->CI->Logs_model->get_last_edit_amp($ic_id);
        if (!empty($amp_log)) {
            $data['last_edit_amp_id'] = $amp_log->log_id;
        } else {
            $data['last_edit_amp_id'] = '';
        }

        $this->CI->load->view('bootstrap3/header', $data);
        $this->CI->load->view('amp_editor');
        $this->CI->load->view('bootstrap3/footer');
    }

    public function auto_create($ic_id)
    {
        $data = array();
        if (strtolower($this->CI->config->item('site_code')) == 'cht') {
            $this->CI->Information_model->ic_type = "AND ic.ic_type != 'none'";
        }
        $data['information'] = $this->CI->Information_model->detail_by_ic_id($ic_id);
        if ($data['information'] == FALSE) {
            return json_encode(array('result' => 'no', 'data' => '找不到这个页面'));
        }

        //144.76.185.44:8029
        //根据站点不同，配置不同参数
        $site_code = strtolower($this->CI->config->item('site_code'));
        switch ($site_code) {
            case 'ah':
                //websitehost是表示资源下载的域名，在网页代码中有/css/xxx.css之类的路径，程序需要添加域名组成完整URL去下载文件，一般是data域名或者www域名
                $websitehost = 'https://data.asiahighlights.com';
                $html_source_url = 'https://www.asiahighlights.com/index.php/information/detail/?no_cache=true&static_html_url=' . $data['information']->ic_url;
                break;
            case 'cht':
                $websitehost = 'https://data.chinahighlights.com';
                $html_source_url = 'http://169.54.172.35:3588' . $data['information']->ic_url;
                break;
            case 'gm':
                $websitehost = 'https://data.chinarundreisen.com';
                $html_source_url = 'https://www.chinarundreisen.com/' . $data['information']->ic_url;
                break;
            case 'ct':
                $websitehost = 'https://data.chinatravel.com';
                $html_source_url = 'http://158.85.210.78:2222' . $data['information']->ic_url;
                break;
            default:
                $websitehost = $this->CI->config->item('site_url');
                $html_source_url = $this->CI->config->item('site_url') . $data['information']->ic_url;
        }
        //获取网页当前源码，然后发送到信息平台
        $html_source = GET_HTTP($html_source_url);
        if (!empty($html_source)) {
            $post_data = array('websitehost' => $websitehost, 'template_name' => $site_code, 'create_amp' => 'true', 'htmlsource' => $html_source);
            //德语站点走另外一个控制器，其他站点还是走原来的流程
            if ($site_code == 'gm') {
                if ($data['information']->ic_url == '/massgeschneiderte-china-reisen/') {
                    return json_encode(array());
                } else {
                    return GET_HTTP(site_url('/apps/htmlcompressor/index_gm/choose_way'), $post_data, 'POST');
                }
            } elseif ($site_code == 'ah') {
                return GET_HTTP(site_url('/apps/htmlcompressor/index_ah/choose_way'), $post_data, 'POST');
            } else {
                return GET_HTTP(site_url('/apps/htmlcompressor/index/optimize'), $post_data, 'POST');
            }
        }
        return json_encode(array('result' => 'no', 'data' => '不知道哪里错了，看代码'));
    }

    public function edit_save($ic_id = false, $textarea_htmlcode = false, $amp_status = false)
    {
        $data = array();
        if ($ic_id === false) {
            $ic_id = $this->CI->input->post('ic_id');
        }
        if ($textarea_htmlcode === false) {
            $textarea_htmlcode = $this->CI->input->post('textarea_htmlcode');
        }
        if ($amp_status === false) {
            $amp_status = $this->CI->input->post('amp_status');
        }
        $data['information'] = $this->CI->Information_model->detail_by_ic_id($ic_id);

        if ($data['information'] == FALSE) {
            show_404();
        }

        //CH的页面没有canonical内容，在这里帮补上
        $textarea_htmlcode = str_replace('<!--@CANONICAL@-->', $this->CI->config->item('site_url') . $data['information']->ic_url, $textarea_htmlcode);

        //备份修改的代码
        $this->CI->Logs_model->write('backup_amp', $ic_id, $textarea_htmlcode);

        //AMP格式验证
        if ($amp_status !== '0') {//只有发布的时候才需要验证
            //把AMP网页内容到purifycss处理，内置了AMP-Validator
            $purifycss_server = 'http://169.54.167.189:33033/';
            if ($this->CI->config->item('site_code' == 'gm')) {//德语站点使用自己的css处理服务器
                $purifycss_server = 'http://158.177.67.52:33033/';
            }
            $validator_result = GET_HTTP($purifycss_server, 'amp_source=' . urlencode($textarea_htmlcode), 'POST');
            $validator_result = json_decode($validator_result);
            if ($validator_result->status == 'FAIL') {
                return json_encode(array('name' => 'no', 'value' => $validator_result->errors));
            }
        }
        $amp = $this->CI->InfoMetas_model->get($ic_id, 'AMP');
        if ($amp === false) {
            $this->CI->InfoMetas_model->add($ic_id, 'AMP', $textarea_htmlcode);
        } else {
            $this->CI->InfoMetas_model->update($ic_id, 'AMP', $textarea_htmlcode);
        }

        $amp_status_value = $this->CI->InfoMetas_model->get($ic_id, 'AMP_STATUS');
        if ($amp_status_value === false) {
            $this->CI->InfoMetas_model->add($ic_id, 'AMP_STATUS', $amp_status);
        } else {
            $this->CI->InfoMetas_model->update($ic_id, 'AMP_STATUS', $amp_status);
        }

        return json_encode(array('name' => 'ok', 'value' => $this->CI->lang->line('form_info_success')));
    }

}
