Jex’s Note

PHP - Codeigniter

判斷DB是否執行成功

判斷select

if ($query->result_array())
{
    // display data..
}
return FALSE;

判斷insert

if ($this->db->insert_id())
{
    return TRUE;
}

判斷update是否成功更新

if ($this->db->update('shares'))
{
    return TRUE;
}

return ($this->db->affected_rows() > 0) ? TRUE : FALSE;

判斷delete是否成功刪除

$this->db->delete('question_options');
if ($this->db->affected_rows() > 0)
{
  return TRUE;
}
return FALSE;

欄位更新 ex: +1

$this->db->set('lose', 'lose+1', FALSE);
$this->db->where('guid', $data['guid']);
$this->db->update('users');

Form Validation 的 callback

 array(
     'field' => 'current_passwd',
     'label' => 'lang:users-current_passwd',
     'rules' => 'trim|required|callback_check_password[13]'
 )

 function check_password($current_passwd, $uid)
 {
      // $current_passwd : CI 會自已捕捉 HTML 裡 name="current_passwd" post 進來的值,放入第一個參數
      // $uid = 13

      if ( // Condition )
      {
          return TRUE;
      }
      return FALSE;
 }

給 callback 的參數是從第二個參數開始給,第一個參數都是保留給自已那個欄位的值(current_passwd的值)

Load model in Library

private $CI = NULL;

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

codeigniter 寄信錯誤 “無法使用 mail() 函式發送信件. 主機可能未設定此方法發送信件”

可能沒有安裝sendmail

sudo apt-get install sendmail

測試sendmail 寄信

$ sendmail jex_lin@example.com      <= enter
Subject: test                        <= enter
test                                 <= enter
.                                    <= enter

OR

echo "My test email being sent from sendmail" | /usr/sbin/sendmail jex_lin@example.com

CI input會自動過濾 %00~%08 %0b %0c %0e %0f

system/core/Input.phpCI_INPUT class__contruct() 會執行 _sanitize_globals metheod

_sanitize_globals method 會過濾一切 $_GET$_POST 變數所以即使拿原生函數 $_GET 去抓都沒有用,會先被ci的 method 給過濾掉

而在這裡面它執行的是 _clean_input_data 這個 method 而它又會去執行

remove_invisible_characters 這個 function,而它是在 system/core/Common.php

就是由它去過濾 %00-%08 %0b %0c %0e %0f,把它們轉成空字串

可藉由 remove_invisible_characters 的第二個參數指定為 false 關閉過濾

自訂 controller

/application/core/MY_Controller :

require_once 'Base_Controller.php';

class MY_Controller extends CI_Controller {

CI 的 log_message

log_message("INFO", "data: " . var_export($data, TRUE));

第二個參數預設為 FALSE : If you would like to capture the output of print_r(), use the return parameter. When this parameter is set to TRUE, print_r() will return the information rather than print it.

crop avatar

$crop_x = $this->input->post('crop_x');
$crop_y = $this->input->post('crop_y');
$crop_w = $this->input->post('crop_w');
$crop_h = $this->input->post('crop_h');

// calculate new coordinate, width and height
list($ori_width, $ori_height) = getimagesize($original_image_path);
list($pre_width, $pre_height) = getimagesize($preview_image_path);
$width_percent = round($ori_width / $pre_width, 2);
$height_percent = round($ori_height / $pre_height, 2);
$new_x = (int) round($crop_x * $width_percent);
$new_y = (int) round($crop_y * $height_percent);
$new_w = (int) round($crop_w * $width_percent);
$new_h = (int) round($crop_h * $height_percent);

//crop
$config = array(
    'image_library'     => 'gd2',
    'source_image'      => $original_image_path,
    'new_image'         => $destination_image,
    'quality'           => 100,
    'maintain_ratio'    => FALSE,
    'x_axis'            => $new_x,
    'y_axis'            => $new_y,
    'width'             => $new_w,
    'height'            => $new_h
);
$this->image_lib->initialize($config);
if ($this->image_lib->crop())
{
    // resize to 180 * 180
    $config = array(
        'image_library'     => 'ImageMagick',
        'library_path'      => '/usr/bin',
        'source_image'      => $destination_image,
        'quality'           => 100,
        'maintain_ratio'    => TRUE,
        'width'             => 180,
        'height'            => 180
    );
    $this->image_lib->initialize($config);
    if ($this->image_lib->resize())
    {
        // update last_avatar_time
        $this->load->model(array('users_model', 'userlog_model'));
        $time = time();
        if ($this->users_model->update($user['account'], array('last_avatar_time' => $time)))
        {
            // update session
            $update_session_data = array('avatar_url' => get_avatar_path($user['id'], FALSE) .'?'. $time);
            $this->login->update_session($update_session_data);

            $this->userlog_model->insert($user['id'], $user['name'], UserLog_model::USER_AVATAR_UPDATE);
        }

        $this->_outputJSON(array('status' => 'ok'));
    }
}

parseImg

$url = $this->input->get_post('url');
if ( ! $html_content = @file_get_contents($url))
{
    return $this->_outputJSON(array('status' => 'fail', 'error' => 'Wrong url'));
}
$patten = "/<img.*src=\"(((http[s]?):\/\/|www\.)([^\s\[\\\"'])+)\".*>/";
if (preg_match_all($patten, $html_content, $matches, PREG_SET_ORDER))
{
    foreach ($matches as $key => $match)
    {
        if (list($width, $height) = @getimagesize($match[1]))
        {
            if ($width >= 100 OR $height >= 100)
            {
                return $this->_outputJSON(array('status' => 'ok', 'url' => $match[1]));
            }
        }
        else
        {
            unset($matches[$key]);
        }
    }
    if ( ! empty($matches))
    {
        reset($matches);
        return $this->_outputJSON(array('status' => 'ok', 'url' => current($matches)[1]));
    }
}
return $this->_outputJSON(array('status' => 'fail', 'error' => 'Image not found'));

Comments