Upgrade to Pro — share decks privately, control downloads, hide ads and more …

實戰 Fight with CodeIgniter

Bo-Yi Wu
September 29, 2014

實戰 Fight with CodeIgniter

成功大學電算中心教育訓練

Bo-Yi Wu

September 29, 2014
Tweet

More Decks by Bo-Yi Wu

Other Decks in Technology

Transcript

  1. 2 關於我 Bo-Yi Wu (appleboy) – Blog: http://blog.wu-boy.com/ – Github:

    https://github.com/appleboy – CodeIgniter 站長 – Laravel 站長
  2. 8 網站安全性 • 管理介面 URL • 目錄 (Index of) •

    錯誤訊息 • 暫存測試資訊 • 版本控管 • SQL Injection
  3. 11 常用管理介面路徑 • /admin/ • /phpMyAdmin/ • /adminLogin/ • /manage/

    • /management/ • /root/ • /wp-admin/ 政府機關、學校機構、電信業者
  4. 13 網站安全性 • 管理介面 URL • 目錄 (Index of) •

    錯誤訊息 • 暫存測試資訊 • 版本控管 • SQL Injection
  5. 14

  6. 17 網站安全性 • 管理介面 URL • 目錄 (Index of) •

    錯誤訊息 • 暫存測試資訊 • 版本控管 • SQL Injection
  7. 19 如何防禦 • 關閉錯誤顯示 – php.ini – display_errors = off

    • 使用 Framework 都可以直接設定在專案裡
  8. 20 網站安全性 • 管理介面 URL • 目錄 (Index of) •

    錯誤訊息 • 暫存測試資訊 • 版本控管 • SQL Injection
  9. 23 如何防禦 • 不要在正式產品環境中進行開發 • 關閉編輯器自動備份 • 伺服器過濾檔案下載 – Nginx

    # Prevent clients from accessing to backup/config/source files location ~* (?:\.(?:bak|config|sql|fla|psd|ini|log|sh|inc|swp|dist)|~)$ { deny all; }
  10. 24 網站安全性 • 管理介面 URL • 目錄 (Index of) •

    錯誤訊息 • 暫存測試資訊 • 版本控管 • SQL Injection
  11. 26

  12. 28 網站安全性 • 管理介面 URL • 目錄 (Index of) •

    錯誤訊息 • 暫存測試資訊 • 版本控管 • SQL Injection
  13. 29 SQL Injection SELECT id FROM users WHERE email =

    ‘$email‘ and password = ‘$password’; SELECT id FROM users WHERE email = ‘$email‘ and password = ‘anything' OR ‘1’ = ‘1’;
  14. 31 型態轉換 Typecasting $var = (array) $var; $var = (binary)

    $var; $var = (bool) $var; $var = (boolean) $var; $var = (double) $var; $var = (float) $var; $var = (int) $var; $var = (integer) $var; $var = (object) $var; $var = (real) $var; $var = (string) $var;
  15. 33

  16. 34

  17. 41

  18. 44 CodeIgniter 架構 • application 開發 MVC 程式碼 • system

    系統程式碼 • user_guide 線上文件 • index.php 主檔案
  19. 54 作業一 • 建立一個 virtual host: ci.localhost • 安裝 codeigniter

    2.2.0 • 移除 url 的 index.php 字串 – http://ci.localhost/welcome
  20. 56 核心架構 • core – Controller, Model, Router, Loader, Input

    … • database – MySQL, ODBC, MSSQL, Sqlite … • helpers – email, url, text, number, language … • language – zh-tw, zh-cn, english … • libraries – Form, Image, Session, Email, Pagination
  21. 58 擴充核心函式庫 • core – MY_Model, MY_Controller, MY_Input …. •

    libraries – MY_Email, MY_Upload …. • helpers – MY_array_helper ….
  22. 72 簡介 class Topic_model extends MY_Model { } $this->load->model('topic_model', 'topic');

    $this->topic->get_all(); $this->topic->get(1); $this->topic->get_by('title', 'Pigs CAN Fly!'); $this->topic->get_many_by('status', 'open'); $this->topic->insert(array( 'status' => 'open', 'title' => "I'm too sexy for my shirt" )); $this->topic->update(1, array( 'status' => 'closed' )); $this->topic->delete(1);
  23. 76 指定 Primary Key // 預設為 id class Topic_model extends

    MY_Model { public $primary_key = ‘topic_id'; }
  24. 77 Callbacks • $before_create • $after_create • $before_update • $after_update

    • $before_get • $after_get • $before_delete • $after_delete
  25. 78 範例 class Topic_model extends MY_Model { public $before_create =

    array( 'timestamps' ); protected function timestamps($topic) { $topic['created_at'] = date('Y-m-d H:i:s'); $topic['updated_at'] = date('Y-m-d H:i:s'); return $topic; } }
  26. 79 參數處理 public $before_create = array('data_process(name)'); public $before_update = array('data_process(date)');

    protected function data_process($row) { $row[$this->callback_parameters[0]] = $this- >_process($row[$this->callback_parameters[0]]); return $row; }
  27. 80 驗證 Validation class User_model extends MY_Model { public $validate

    = array( array( 'field' => 'email', 'label' => 'email', 'rules' => 'required|valid_email|is_unique[users.email]' ), array( 'field' => 'password', 'label' => 'password', 'rules' => 'required' ), array( 'field' => 'password_confirmation', 'label' => 'confirm password', 'rules' => 'required|matches[password]' ), ); }
  28. 84 $this->topic_model->insert(array( 'id' => 2, 'title' => 'A new topic'

    )); // INSERT INTO topics (title) VALUES ('A new topic')
  29. 87 Soft Delete class Topic_model extends MY_Model { protected $soft_delete

    = true; protected $soft_delete_key = 'deleted'; }
  30. 88 $this->topic_model->get(1); // SELECT * FROM topics WHERE id =

    1 and deleted = 0 $this->topic_model->only_deleted()->get(1); // SELECT * FROM topics WHERE id = 1 AND deleted = 1 $this->topic_model->with_deleted()->get(1); // SELECT * FROM topics WHERE id = 1
  31. 89 內建 Observers class Topic_model extends MY_Model { public $before_create

    = ['created_at', 'updated_at']; public $before_update = ['updated_at']; }
  32. 91 作業三 • 實做最新消息系統 – 建立 topics 資料表 • id,

    title, description, is_feature, created_at, updated_at • 實做 CRUD ( 新增 , 刪除 , 修改 , 查詢 ) • 實做置頂功能
  33. 93 新增相關 Model • class User_model extends MY_Model { }

    • class Comment_model extends MY_Model { }
  34. 94 指定相關資料表 class Topic_model extends MY_Model { public $belongs_to =

    ['user' => ['model' => 'user_m']]; public $has_many = ['comments' => ['model' => 'model_comments']]; }
  35. 96 SQL 表示 • SELECT * FROM users WHERE id

    = $topic- >user_id • SELECT * FROM comments WHERE topic_id = $topic->id
  36. 97 更換 Primary Key class Topic_model extends MY_Model { public

    $belongs_to = ['user' => ['primary_key' => 'post_user_id']]; public $has_many = ['comments' => ['primary_key' => 'parent_topic_id']]; }
  37. 98 作業四 • 實做最新消息系統 – 建立 Users 資料表 • id,

    username – 增加欄位在 Topic 資料表 • user_id – 顯示使用者帳號在 Topic 列表
  38. 100 功能列表 • 會員登入 ( 支援帳號或電子郵件 ) • 會員登出 •

    會員註冊 • 會員更新 • 忘記密碼 • 電子郵件認證 • 會員群組權限 • 驗證登入錯誤次數
  39. 102 設定檔資料表 $config['tables']['users'] = 'users'; $config['tables']['groups'] = 'groups'; $config['tables']['users_groups'] =

    'users_groups'; $config['tables']['login_attempts'] = 'login_attempts'; $config['join']['users'] = 'user_id'; $config['join']['groups'] = 'group_id';
  40. 103 會員認證設定 $config['admin_email'] = "[email protected]"; $config['default_group'] = 'members'; $config['admin_group'] =

    'admin'; $config['identity'] = 'email'; $config['min_password_length'] = 8; $config['max_password_length'] = 20; $config['email_activation'] = FALSE; $config['manual_activation'] = FALSE; $config['remember_users'] = TRUE; $config['user_expire'] = 86500; $config['user_extend_on_login'] = FALSE; $config['track_login_attempts'] = FALSE; $config['track_login_ip_address'] = TRUE; $config['maximum_login_attempts'] = 3; $config['lockout_time'] = 600; $config['forgot_password_expiration'] = 0;
  41. 105 自訂錯誤訊息 • $config['message_start_delimiter'] = '<p>'; • $config['message_end_delimiter'] = '</p>';

    • $config['error_start_delimiter'] = '<p>'; • $config['error_end_delimiter'] = '</p>';
  42. 111 動態新增 Meta Tag $this->template->add_meta_tag("og:title", "Test Title", 'property'); //output <meta

    property="og:title" content="Test Title" /> $this->template->add_meta_tag("keywords", "some keywords"); // output <meta name="keywords" content="some keywords" />
  43. 115 作業五 • 整合會員到最新消息系統 – 登入後才可以修改及發表 – 管理者才可以刪除文章 • 整合

    Template 模組 – 加入 jQuery 元件 http://jquery.com/ – 加入 Bootstraphttp://getbootstrap.com/
  44. 118 HTTP Methods • GET => 讀取 • PUT =>

    更新 • POST => 新增 • DELETE => 刪除
  45. 120 JSON usage in JavaScript var output = { ‘title’:

    ‘I am appleboy.’, ‘desctiption’: ‘CodeIgniter in Action.’ };
  46. 123 傳統 • /topic/create • /topic/show/1 • /topic/update/1 • /topic/destroy/1

    現在 • POST /topic • GET /topic/1 • PUT /topic/1 • DELETE /topic/1
  47. 126 範例 class Topic extends REST_Controller { public function index_get()

    { // Display all topics } public function index_post() { // Create a new topic } }
  48. 127 參數 • $this->get('blah'); // GET • $this->post('blah'); // POST

    • $this->put('blah'); // PUT • $this->delete('blah'); // DELETE
  49. 129 Response 回覆 • // Send an HTTP 201 Created

    • $this->response($book, 201); • // HTTP 404 Not Found • $this->response([]);
  50. 131 建立 API KEY 資料表 CREATE TABLE `keys` ( `id`

    int(11) NOT NULL AUTO_INCREMENT, `key` varchar(40) NOT NULL, `level` int(2) NOT NULL, `ignore_limits` tinyint(1) NOT NULL DEFAULT '0', `date_created` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  51. 132 測試 API KEY $ curl -X POST -H "X-API-KEY:

    some_key_here" http://example.com/topics
  52. 133 作業六 • 實作 Topic RESTful API 撰寫 – /api/topic

    GET 新聞列表 – /api/topic/{id} GET 單一新聞列表 – /api/topic/{id} PUT 更新新聞 – /api/topic POST 建立新聞 – /api/topic/{id} DELETE 刪除新聞 • 用 jQuery AJAX 搭配後端 CRUD 功能 • 整合 Facebook 登入 API