Last active
August 29, 2015 14:08
-
-
Save zachflower/11be5c18d8ba7a6494b5 to your computer and use it in GitHub Desktop.
CodeIgniter input class, implementing PATCH and DELETE HTTP request methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class MY_Input extends CI_Input { | |
protected $raw; | |
protected $delete; | |
protected $patch; | |
public function __construct() { | |
parent::__construct(); | |
$this->raw = file_get_contents('php://input'); | |
if ( !empty($this->raw) ) { | |
if ( $this->request_method() == 'DELETE' ) { | |
$this->delete = $this->_sanitize_request($this->raw); | |
} else if ( $this->request_method() == 'PATCH' ) { | |
$this->patch = $this->_sanitize_request($this->raw); | |
} | |
} | |
} | |
public function request_method() { | |
return strtoupper($this->server('REQUEST_METHOD')); | |
} | |
public function patch($index = NULL, $xss_clean = FALSE) { | |
if ($index === NULL AND ! empty($this->patch) ) { | |
$patch = array(); | |
foreach (array_keys($this->patch) as $key) { | |
$patch[$key] = $this->_fetch_from_array($this->patch, $key, $xss_clean); | |
} | |
return $patch; | |
} | |
return $this->_fetch_from_array($this->patch, $index, $xss_clean); | |
} | |
public function delete($index = NULL, $xss_clean = FALSE) { | |
if ($index === NULL AND ! empty($this->delete) ) { | |
$delete = array(); | |
foreach (array_keys($this->delete) as $key) { | |
$delete[$key] = $this->_fetch_from_array($this->delete, $key, $xss_clean); | |
} | |
return $delete; | |
} | |
return $this->_fetch_from_array($this->delete, $index, $xss_clean); | |
} | |
private function _sanitize_request($raw) { | |
$object = json_decode($raw, TRUE); | |
$data = array(); | |
if ( is_array($object) ) { | |
foreach( $object as $key => &$val) { | |
if(!is_object($val) ) { | |
$data[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); | |
} | |
} | |
unset($val); | |
} | |
return $data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment