00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 class Sanitize{
00039
00040
00041
00042
00043
00044
00045
00046 function paranoid($string, $allowed = array()) {
00047 $allow = null;
00048 if (!empty($allowed)) {
00049 foreach ($allowed as $value) {
00050 $allow .= "\\$value";
00051 }
00052 }
00053
00054 if (is_array($string)) {
00055 foreach ($string as $key => $clean) {
00056 $cleaned[$key] = preg_replace("/[^{$allow}a-zA-Z0-9]/", "", $clean);
00057 }
00058 } else {
00059 $cleaned = preg_replace("/[^{$allow}a-zA-Z0-9]/", "", $string);
00060 }
00061 return $cleaned;
00062 }
00063
00064
00065
00066
00067
00068
00069
00070 function sql($string) {
00071 if (!ini_get('magic_quotes_gpc')) {
00072 $string = addslashes($string);
00073 }
00074 return $string;
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084 function html($string, $remove = false) {
00085 if ($remove) {
00086 $string = strip_tags($string);
00087 } else {
00088 $patterns = array("/\&/", "/%/", "/</", "/>/", '/"/', "/'/", "/\(/", "/\)/", "/\+/", "/-/");
00089 $replacements = array("&", "%", "<", ">", """, "'", "(", ")", "+", "-");
00090 $string = preg_replace($patterns, $replacements, $string);
00091 }
00092 return $string;
00093 }
00094
00095
00096
00097
00098
00099
00100
00101 function cleanArray(&$toClean) {
00102 return $this->cleanArrayR($toClean);
00103 }
00104
00105
00106
00107
00108
00109
00110
00111
00112 function cleanArrayR(&$toClean) {
00113 if (is_array($toClean)) {
00114 while (list($k, $v) = each($toClean)) {
00115 if (is_array($toClean[$k])) {
00116 $this->cleanArray($toClean[$k]);
00117 } else {
00118 $toClean[$k] = $this->cleanValue($v);
00119 }
00120 }
00121 } else {
00122 return null;
00123 }
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 function cleanValue($val) {
00152 if ($val == "") {
00153 return "";
00154 }
00155 //Replace odd spaces with safe ones
00156 $val = str_replace(" ", " ", $val);
00157 $val = str_replace(chr(0xCA), "", $val);
00158 //Encode any HTML to entities.
00159 $val = $this->html($val);
00160 //Double-check special chars and replace carriage returns with new lines
00161 $val = preg_replace("/\\\$/", "$", $val);
00162 $val = preg_replace("/\r\n/", "\n", $val);
00163 $val = str_replace("!", "!", $val);
00164 $val = str_replace("'", "'", $val);
00165 //Allow unicode (?)
00166 $val = preg_replace("/&#([0-9]+);/s", "&#\\1;", $val);
00167 //Add slashes for SQL
00168 $val = $this->sql($val);
00169 //Swap user-inputted backslashes (?)
00170 $val = preg_replace("/\\\(?!&#|\?#)/", "\\", $val);
00171 return $val;
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181 function formatColumns(&$model) {
00182 foreach ($model->data as $name => $values) {
00183 if ($name == $model->name) {
00184 $curModel =& $model;
00185 } elseif (isset($model->{$name}) && is_object($model->{$name}) && is_subclass_of($model->{$name}, 'Model')) {
00186 $curModel =& $model->{$name};
00187 } else {
00188 $curModel = null;
00189 }
00190
00191 if ($curModel != null) {
00192 foreach ($values as $column => $data) {
00193 $colType = $curModel->getColumnType($column);
00194
00195 if ($colType != null) {
00196 $db =& ConnectionManager::getDataSource($curModel->useDbConfig);
00197 $colData = $db->columns[$colType];
00198
00199 if (isset($colData['limit']) && strlen(strval($data)) > $colData['limit']) {
00200 $data = substr(strval($data), 0, $colData['limit']);
00201 }
00202
00203 if (isset($colData['formatter']) || isset($colData['format'])) {
00204
00205 switch(strtolower($colData['formatter'])) {
00206 case 'date':
00207 $data = date($colData['format'], strtotime($data));
00208 break;
00209 case 'sprintf':
00210 $data = sprintf($colData['format'], $data);
00211 break;
00212 case 'intval':
00213 $data = intval($data);
00214 break;
00215 case 'floatval':
00216 $data = floatval($data);
00217 break;
00218 }
00219 }
00220 $model->data[$name][$column]=$data;
00221 /*
00222 switch($colType) {
00223 case 'integer':
00224 case 'int':
00225 return $data;
00226 break;
00227 case 'string':
00228 case 'text':
00229 case 'binary':
00230 case 'date':
00231 case 'time':
00232 case 'datetime':
00233 case 'timestamp':
00234 case 'date':
00235 return "'" . $data . "'";
00236 break;
00237 }
00238 */
00239 }
00240 }
00241 }
00242 }
00243 }
00244 }
00245 ?>