File indexing completed on 2024-11-17 05:19:46
0001 <?php 0002 0003 /** 0004 * Flooer Framework 0005 * 0006 * LICENSE: BSD License (2 Clause) 0007 * 0008 * @category Flooer 0009 * @package Flooer_Filter 0010 * @author Akira Ohgaki <akiraohgaki@gmail.com> 0011 * @copyright Akira Ohgaki 0012 * @license https://opensource.org/licenses/BSD-2-Clause BSD License (2 Clause) 0013 * @link https://github.com/akiraohgaki/flooer 0014 */ 0015 0016 /** 0017 * Usage 0018 * 0019 * $filter = new Flooer_Filter(); 0020 * $filter->setEncoding('UTF-8'); 0021 * $filter->setNewline('LF'); 0022 * $filter->filter($input); 0023 */ 0024 0025 /** 0026 * String filter class 0027 * 0028 * @category Flooer 0029 * @package Flooer_Filter 0030 * @author Akira Ohgaki <akiraohgaki@gmail.com> 0031 */ 0032 class Flooer_Filter 0033 { 0034 0035 /** 0036 * Configuration options 0037 * 0038 * @var array 0039 */ 0040 protected $_config = array( 0041 'convertEncoding' => true, 0042 'convertNewline' => true, 0043 'stripNull' => true, 0044 'stripSlashes' => true, 0045 'trimWhitespace' => true, 0046 'encoding' => 'UTF-8', 0047 'newline' => 'LF' 0048 ); 0049 0050 /** 0051 * Constructor 0052 * 0053 * @param array $config 0054 * @return void 0055 */ 0056 public function __construct(array $config = null) 0057 { 0058 if ($config) { 0059 $this->_config = $config + $this->_config; 0060 } 0061 } 0062 0063 /** 0064 * Filtering 0065 * 0066 * @param mixed &$data 0067 * @return void 0068 */ 0069 public function filter(&$data) 0070 { 0071 // Filter an array/object recursively 0072 if (is_array($data) || is_object($data)) { 0073 if ($this->_config['convertEncoding']) { 0074 array_walk_recursive($data, array($this, 'convertEncoding')); 0075 } 0076 if ($this->_config['convertNewline']) { 0077 array_walk_recursive($data, array($this, 'convertNewline')); 0078 } 0079 if ($this->_config['stripNull']) { 0080 array_walk_recursive($data, array($this, 'stripNull')); 0081 } 0082 if ($this->_config['stripSlashes']) { 0083 array_walk_recursive($data, array($this, 'stripSlashes')); 0084 } 0085 if ($this->_config['trimWhitespace']) { 0086 array_walk_recursive($data, array($this, 'trimWhitespace')); 0087 } 0088 } 0089 // Filter a string 0090 else { 0091 if ($this->_config['convertEncoding']) { 0092 $this->convertEncoding($data); 0093 } 0094 if ($this->_config['convertNewline']) { 0095 $this->convertNewline($data); 0096 } 0097 if ($this->_config['stripNull']) { 0098 $this->stripNull($data); 0099 } 0100 if ($this->_config['stripSlashes']) { 0101 $this->stripSlashes($data); 0102 } 0103 if ($this->_config['trimWhitespace']) { 0104 $this->trimWhitespace($data); 0105 } 0106 } 0107 } 0108 0109 /** 0110 * Convert character encoding 0111 * 0112 * @param string &$value 0113 * @param mixed $key 0114 * @return void 0115 */ 0116 public function convertEncoding(&$value, $key = null) 0117 { 0118 $value = mb_convert_encoding($value, $this->_config['encoding'], mb_detect_order()); 0119 } 0120 0121 /** 0122 * Convert a newline 0123 * 0124 * @param string &$value 0125 * @param mixed $key 0126 * @return void 0127 */ 0128 public function convertNewline(&$value, $key = null) 0129 { 0130 switch (strtoupper($this->_config['newline'])) { 0131 case 'LF': 0132 $value = str_replace(array("\r\n", "\r"), "\n", $value); 0133 break; 0134 case 'CRLF': 0135 $value = str_replace(array("\r\n", "\r"), "\n", $value); 0136 $value = str_replace("\n", "\r\n", $value); 0137 break; 0138 case 'CR': 0139 $value = str_replace(array("\r\n", "\n"), "\r", $value); 0140 break; 0141 default: 0142 break; 0143 } 0144 } 0145 0146 /** 0147 * Strip a null characters 0148 * 0149 * @param string &$value 0150 * @param mixed $key 0151 * @return void 0152 */ 0153 public function stripNull(&$value, $key = null) 0154 { 0155 $value = str_replace("\0", '', $value); 0156 } 0157 0158 /** 0159 * Un-quote a quoted string 0160 * 0161 * @param string &$value 0162 * @param mixed $key 0163 * @return void 0164 */ 0165 public function stripSlashes(&$value, $key = null) 0166 { 0167 $value = stripslashes($value); 0168 } 0169 0170 /** 0171 * Trim whitespace 0172 * 0173 * @param string &$value 0174 * @param mixed $key 0175 * @return void 0176 */ 0177 public function trimWhitespace(&$value, $key = null) 0178 { 0179 $value = trim($value); 0180 } 0181 0182 /** 0183 * Set an executable flag for convertEncoding() 0184 * 0185 * @param bool $bool 0186 * @return void 0187 */ 0188 public function setConvertEncoding($bool) 0189 { 0190 $this->_config['convertEncoding'] = $bool; 0191 } 0192 0193 /** 0194 * Get an executable flag for convertEncoding() 0195 * 0196 * @return bool 0197 */ 0198 public function getConvertEncoding() 0199 { 0200 return $this->_config['convertEncoding']; 0201 } 0202 0203 /** 0204 * Set an executable flag for convertNewline() 0205 * 0206 * @param bool $bool 0207 * @return void 0208 */ 0209 public function setConvertNewline($bool) 0210 { 0211 $this->_config['convertNewline'] = $bool; 0212 } 0213 0214 /** 0215 * Get an executable flag for convertNewline() 0216 * 0217 * @return bool 0218 */ 0219 public function getConvertNewline() 0220 { 0221 return $this->_config['convertNewline']; 0222 } 0223 0224 /** 0225 * Set an executable flag for stripNull() 0226 * 0227 * @param bool $bool 0228 * @return void 0229 */ 0230 public function setStripNull($bool) 0231 { 0232 $this->_config['stripNull'] = $bool; 0233 } 0234 0235 /** 0236 * Get an executable flag for stripNull() 0237 * 0238 * @return bool 0239 */ 0240 public function getStripNull() 0241 { 0242 return $this->_config['stripNull']; 0243 } 0244 0245 /** 0246 * Set an executable flag for stripSlashes() 0247 * 0248 * @param bool $bool 0249 * @return void 0250 */ 0251 public function setStripSlashes($bool) 0252 { 0253 $this->_config['stripSlashes'] = $bool; 0254 } 0255 0256 /** 0257 * Get an executable flag for stripSlashes() 0258 * 0259 * @return bool 0260 */ 0261 public function getStripSlashes() 0262 { 0263 return $this->_config['stripSlashes']; 0264 } 0265 0266 /** 0267 * Set an executable flag for trimWhitespace() 0268 * 0269 * @param bool $bool 0270 * @return void 0271 */ 0272 public function setTrimWhitespace($bool) 0273 { 0274 $this->_config['trimWhitespace'] = $bool; 0275 } 0276 0277 /** 0278 * Get an executable flag for trimWhitespace() 0279 * 0280 * @return bool 0281 */ 0282 public function getTrimWhitespace() 0283 { 0284 return $this->_config['trimWhitespace']; 0285 } 0286 0287 /** 0288 * Set a character encoding 0289 * 0290 * @param string $encoding Available values to mb_convert_encoding() 0291 * @return void 0292 */ 0293 public function setEncoding($encoding) 0294 { 0295 $this->_config['encoding'] = $encoding; 0296 } 0297 0298 /** 0299 * Get a character encoding 0300 * 0301 * @return string 0302 */ 0303 public function getEncoding() 0304 { 0305 return $this->_config['encoding']; 0306 } 0307 0308 /** 0309 * Set a newline 0310 * 0311 * @param string $newline Available values: LF, CRLF, CR 0312 * @return void 0313 */ 0314 public function setNewline($newline) 0315 { 0316 $this->_config['newline'] = $newline; 0317 } 0318 0319 /** 0320 * Get a newline 0321 * 0322 * @return string 0323 */ 0324 public function getNewline() 0325 { 0326 return $this->_config['newline']; 0327 } 0328 0329 }