2012年6月24日 星期日

在 CodeIgniter 中整合 Discuz X2.5, 引用超級變數 $_G 來獲取用戶名, uid等資訊.

origin: https://github.com/QueenbyeR/blog-text/blob/master/%E5%9C%A8%20CodeIgniter%20%E4%B8%AD%E6%95%B4%E5%90%88%20Discuz%20X2.5%2C%20%E5%BC%95%E7%94%A8%E8%B6%85%E7%B4%9A%E8%AE%8A%E6%95%B8%20%24_G%20%E4%BE%86%E7%8D%B2%E5%8F%96%E7%94%A8%E6%88%B6%E5%90%8D%2C%20uid%E7%AD%89%E8%B3%87%E8%A8%8A/page-001.md



環境:
  • CodeIgniter 2.1.2
  • Discuz X2.5 (2012年4月7日繁體中文UTF8)
目錄:
  • CodeIgniter 在 /
  • Discuz 在 /bbs/ (程式碼會因路徑而稍微有所不同)
這次是我第一次使用 Discuz 也是第一次使用 CI。主要網站應用程式用 CI 寫,另外架了個 Discuz 作論壇以及網站會員管理。所以我必須要能夠在 CI 程式中,取得用戶從 Discuz 登入後的用戶資訊 (當前用戶名、uid等)。
試過了 UCenter 來取得資訊,行不通 (一定是我太蠢了),而且網路上都找不到良好的解決方案。
要透過 UCenter 來取得用戶資訊,必須先取得用戶名或其uid,但我就是卡在無法從 cookie 上解密 Discuz 的用戶名出來;網上找了一下,應該是 Discuz 的加密方法不一樣,爬了一些 Disucz 核心,還是找不到好的方法,只好以最少的核心改動來兼容 CI。

改動檔案

本改動未經測試過安全性、性能等指標。僅供參考。
打開 \www\bbs\source\class\discuz\discuz_application.php
找到
    class discuz_application extends discuz_base{
在之後加入
var $is_in_bbs = true;

找到
public function __construct() {
在之後加入
if ( ! strpos($_SERVER['REQUEST_URI'], 'bbs') ) $this->is_in_bbs = false;

找到
    $this->_init_env();
    $this->_init_config();
    $this->_init_input();
    $this->_init_output();
取代為
    $this->_init_env();
    $this->_init_config();
    $this->_init_input();
    if ($this->is_in_bbs) $this->_init_output();

找到
define('IS_ROBOT', checkrobot());

foreach ($GLOBALS as $key => $value) {
    if (!isset($this->superglobal[$key])) {
        $GLOBALS[$key] = null; unset($GLOBALS[$key]);
    }
}
取代為
define('IS_ROBOT', checkrobot());

if ($this->is_in_bbs) {
    foreach ($GLOBALS as $key => $value) {
        if (!isset($this->superglobal[$key])) {
            $GLOBALS[$key] = null; unset($GLOBALS[$key]);
        }
    }
}

2012年6月22日 星期五

為 CodeIgniter 掛載 hook 將網頁所有 img tag 自動補足 alt 屬性

此操作引用了 simple_html_dom 類庫。

本 hook 將可以為 CI 中,所有輸出的網頁裡的 <img> 自動作補足 alt 屬性的操作,提升 SEO 效果。例如原始 view 中的程式碼為:
    <div class="img_slide">
        <img src="a1.png" alt="第一章">
        <img src="a2.png" alt="">
        <img src="a3.png" alt="呵呵">
        <img src="a4.png" alt="第四章 - 你的網站名稱">
    </div>
透過此 hook 後,用戶及搜尋引擎將會在瀏覽器中見到如下:
    <div class="img_slide">
        <img src="a1.png" alt="第一章 - 你的網站名稱">
        <img src="a2.png" alt="你的網站名稱">
        <img src="a3.png" alt="呵呵 - 你的網站名稱">
        <img src="a4.png" alt="第四章 - 你的網站名稱">
    </div>

1.
打開檔案:
    \application\config\config.php
搜尋:
    $config['enable_hooks']
確保它為:
    $config['enable_hooks'TRUE;


2.
打開檔案:
    \application\config\hooks.php
任意空白處插入程式碼:
    // 處理<img>缺省alt=""
    $hook['display_override'][array(
        'class'    => '',
        'function' => 'images_auto_set_alt',
        'filename' => 'output.php',
        'filepath' => 'hooks',
        'params'   => array'output_display' => TRUE )
    );


3.
新增檔案: \application\hooks\output.php
    /**
     * 將輸出的 圖片(<img>) 進行處理;自動配置 alt 屬性。
     *
     * @param array   $setting=array() 設定
     *                $setting['output_display'] => 是否直接輸出給瀏覽器。FALSE代表不直接輸出,僅回存至buffer。
     * @return NULL                    不會回傳東西。
     */
    function images_auto_set_alt$setting=array({
        $CI =& get_instance();
        $CI->load->library'simple_html_dom' )// require_once APPPATH . 'libraries/simple_html_dom.php';
        $buffer $CI->output->get_output();

        $DOM str_get_html$buffer );
        foreach $DOM->find'img' as $key => $img {
            if empty$img->alt && strpos$img->alt$CI->config->item'site_name' {
                $img->alt $CI->config->item'site_name' );
            }
            else {
                $img->alt .' - ' $CI->config->item'site_name' );
            }
        }

        $CI->output->set_output$DOM->save();

        if $setting['output_display'{
            $CI->output->_display();
        }
    }


4.
完成!