2016年7月29日 星期五

On Ubuntu 16.04 downgrade from php 7 to php 5

話說Azure新的Ubuntu可以挑16.04版本,可內建式PHP 7 ... 目前開發用的CI是2.X版,貌似在SESSION操作上有問題回500錯誤(屬於系統錯誤~Error Log無),急件還沒空Debug~先備註降版的過程!

引用來源
  1.   安裝套件
  2. apt-get install software-properties-common
    add-apt-repository ppa:ondrej/php
    apt-get update
    apt-get install php7.0 php5.6 php5.6-mysql php-gettext php5.6-mbstring php-xdebug libapache2-mod-php5.6 libapache2-mod-php7.0 php5.6-curl php5.6-mcrypt php5.6-xml   
  3.  切換版本

    php 5 to 7
      a2dismod php5.6
      a2enmod php7.0
      service apache2 restart
      ln -sfn /usr/bin/php7.0 /etc/alternatives/php

    php 7 to 5
      a2dismod php7.0
      a2enmod php5.6
      service apache2 restart
      ln -sfn /usr/bin/php5.6 /etc/alternatives/php
  4. 檢查

    php -v  看版本
    php -m 看module
備忘: CI 3.1版發布了~熊熊回去看開發版本與最新版版的差異有點大...等Ubuntu 16.04跑穩後在動手吧。不過DB Drive在php 7已不支援mysql而要改成mysqli就先行處理,在php 5.X跑mysqli尚未發現異常。

2016年7月26日 星期二

CodeIgniter套用language備忘

在CI預設FrameWork是沒加上語系表,不過語系表方便多國語言套用~這很好用就Memo一下!!

在language新增相關語系資料夾 etc: en, zh ...

在config
  : autoload
  $autoload['model'] = array('Appconfig', ...

  :config
  $config['language'] = 'zh';

  :hooks
  $hook['post_controller_constructor'] = array(
                                'class'    => '',
                                'function' => 'load_config',
                                'filename' => 'load_config.php',
                                'filepath' => 'hooks'
                                );

在hook
  新增load_config.php

function load_config(){
    $CI =& get_instance();
    foreach( $CI->Appconfig->get_all()->result() as $app_config )
    {
        $CI->config->set_item( $app_config->key, $app_config->value );
    }
    
    //Set language from config database
    //Loads all the language files from the language directory
    
    if ( $CI->config->item( 'language' ) )
    {
        $CI->config->set_item( 'language', $CI->config->item( 'language' ) );
        
        $map = directory_map('./application/language/' . $CI->config->item( 'language' ));

        foreach($map as $file)
        {
            if ( substr(strrchr($file,'.'),1) == "php")
            {
                $CI->lang->load( str_replace( '_lang.php', '', $file ) );    
            }
        }
    }
    
    //Set timezone from config database
    if ( $CI->config->item( 'timezone' ) )
    {
        date_default_timezone_set( $CI->config->item( 'timezone' ) );
    }
    else
    {
        date_default_timezone_set( 'America/New_York' );
    }
}
  
在model
  新增appconfig.php

class Appconfig extends CI_Model {
function exists($key)
{
$this->db->from('app_config');
$this->db->where('app_config.key',$key);
$query = $this->db->get();

return ($query->num_rows()==1);
}

function get_all()
{
$this->db->from('app_config');
$this->db->order_by("key", "asc");
return $this->db->get();
}

function get($key)
{
$query = $this->db->get_where('app_config', array('key' => $key), 1);

if($query->num_rows()==1)
{
return $query->row()->value;
}

return "";

}

function save($key,$value)
{
$config_data=array(
'key'=>$key,
'value'=>$value
);

if (!$this->exists($key))
{
return $this->db->insert('app_config',$config_data);
}

$this->db->where('key', $key);
return $this->db->update('app_config',$config_data);
}

function batch_save($data)
{
$success=true;

//Run these queries as a transaction, we want to make sure we do all or nothing
$this->db->trans_start();
foreach($data as $key=>$value)
{
if(!$this->save($key,$value))
{
$success=false;
break;
}
}

$this->db->trans_complete();
return $success;

}

function delete($key)
{
return $this->db->delete('app_config', array('key' => $key)); 
}

function delete_all()
{
return $this->db->empty_table('app_config'); 
}
}

2016年7月20日 星期三

WordPress 的 WP-Mail-SMTP設定

針對Contact Form 7需要所以得開通SMTP服務。

1. WordPress安裝WP MAIL SMTP 的PlugIn,用搜尋外掛即可找到。

2. WIN SERVER安裝hmailserver並完成相關安全性設定,參考此篇... 避免被當跳板所以SMTP認證都得開啟,同時關閉外部至外部郵件的路由。

3. 自WordPress設定項找到EMail進入,設定參考此篇... 原則上這樣就行了,不過測試信一直失敗,顯示權限不足~

補充:
(1). vim /etc/selinux/config 將 SELINUX=disabled ,不過關掉有些冒險...但是習慣透過IPtables來管理所以沒差。

(2). 調整Code ... 實測影響不大!

第一步:找到 wp-includes/pluggable.php 這個文件,將 262 行的 
    $phpmailer->IsMail(); 
    改為 
    $phpmailer->IsSMTP(); 

第二步:修改 wp-includes/class-phpmailer.php 這個文件裡的 SMTP 參數 
    var $Host = "smtp.server.com"; 
    var $SMTPAuth = true; 
    var $Username = "mail address"; 
    var $Password = "security";

(3) 將密碼的明碼顯示改掉,把smtp_pass的 type="text" 換成 type="password" 即可
 vim wp-content/plugins/wp-mail-smtp/wp_mail_smtp.php