12 function runExecStatement($sql, $dbh)
15 $sth = db_query($sql,$dbh);
16 $newError= trim(db_last_error());
22 return array(
"sql" => array($sql),
"error" => $errors);
30 function runTransaction($sqls, $dbh)
34 db_query(
"begin", $dbh);
35 foreach($sqls as $sql)
38 $error= trim(db_last_error());
42 db_query(
"rollback", $dbh);
46 db_query(
"commit", $dbh);
48 return array(
"sql" => $sqls,
"error" => $errors);
58 function runSelectStatement($sql, $dbh, $translationArray, $changeToBooleanArray= array())
61 $sth = db_query($sql,$dbh);
62 $newError= trim(db_last_error());
73 while($dRecord = db_fetch_assoc($sth, $i++))
75 foreach($translationArray as $column => $value)
77 $columnValue= trim($dRecord[$column]);
78 if (isset($changeToBooleanArray[$column]))
79 $columnValue= $columnValue == $changeToBooleanArray[$column];
80 $gridRow[$value]= $columnValue;
82 $gridData[]= $gridRow;
87 return array(
"sql" => array($sql),
"error" => $errors,
"record" => $gridData);
91 function runSelectStatementMap($sql, $dbh)
94 $sth = db_query($sql,$dbh);
95 $newError= trim(db_last_error());
100 db_free_result($sth);
106 while($dRecord = db_fetch_row($sth, $i++))
108 $gridData[$dRecord[0]]= $dRecord[1];
110 db_free_result($sth);
113 return array(
"sql" => array($sql),
"error" => $errors,
"record" => $gridData);
121 function prepSave($text, $msize=0)
124 $text = substr($text, 0, $msize);
125 $text = pg_escape_string(trim($text));
132 function cleanPhone($text)
135 $normalPat=
'/^[1-9]\d{2}-\d{3}-\d{4}$/';
137 $pats[]=
'/^[1-9]\d{9}$/';
138 $pats[]=
'/^\(\s*[1-9]\d{2}\s*\)\s*\d{3}\s*-\s*\d{4}$/';
139 $pats[]=
'/^[1-9]\d{2}\s*-\s*\d{3}\s*-\s*\d{4}$/';
140 $pats[]=
'/^[1-9]\d{2}\s*\.\s*\d{3}\s*\.\s*\d{4}$/';
141 $patReplace=
'/[\s\.\-\(\)]*/';
142 if (preg_match($normalPat, $text))
144 if (preg_match($pats[0], $text))
145 return substr($text, 0,3) .
"-" . substr($text, 3,3) .
"-" . substr($text, 6,4);
146 if (preg_match($pats[1], $text) || preg_match($pats[2], $text) || preg_match($pats[3], $text))
148 $text= preg_replace($patReplace,
"", $text);
149 $text= substr($text, 0,3) .
"-" . substr($text, 3,3) .
"-" . substr($text, 6,4);
159 function cleanEmail($text, $maxlength=0)
161 $text= trim(prepSave($text, $maxlength));
167 $pattern=
'/^[^\s@\.]+(\.[^\s@\.]+)*@[^\s@\.]+(\.[^\s@\.]+)*\.\w{2,}$/';
168 if (preg_match($pattern, $text))
176 function cleanDate($text)
179 if (preg_match(
'/^\d{8}$/', $text))
180 $text= substr($text, 0, 4) .
"-" . substr($text, 4,2) .
"-" . substr($text, 6,2);
181 if (!preg_match(
'/^\d{4}-\d{2}-\d{2}$/', $text))
183 $timestamp= date_create_from_format(
'Y-m-d', $text);
184 if ($timestamp ==
false)
192 function cleanTime($text)
195 if (preg_match(
'/^\d{2}:\d{2}(:\d{2})?$/', $text))
197 $hours= intval(substr($text, 0,2));
198 $minutes= intval(substr($text, 3,2));
199 if ($hours >= 0 && $hours <= 23 && $minutes >= 0 && $minutes <= 59)
209 function cleanInteger($text, $allowNegatives=
false, $subtype=
"")
212 $pattern=
'/^[+-]?\d+$/';
213 if (preg_match($pattern, $text))
215 $number= intval($text);
216 if (!$allowNegatives && $number < 0)
218 if (trim($subtype) !=
"")
222 if ($number >= 0 && $number <= 12)
227 if ($number >= 1000 && $number <= 9999)
229 else if ($number >= 1 && $number <= 99)
236 return strval($number);
244 function cleanNumber($text)
247 $pattern=
'/^[+-]?\d+\.?\d*$/';
248 if (preg_match($pattern, $text))
249 return strval($text);
257 function cleanBoolean($text, $required, $firstValue =
"Y", $secondValue =
"N")
259 $text= trim(strtoupper($text));
260 $firstValue= trim(strtoupper($firstValue));
261 $secondValue= trim(strtoupper($secondValue));
262 if ($text == $firstValue || $text == $secondValue)
276 function cleanValuesForDatabase(&$parameters, $validation)
280 if (!is_array($parameters))
281 $errors[]=
"Expected an array but parameters is not an array.";
282 if (!is_array($validation))
283 $errors[]=
"Expected an array but validation is not an array.";
285 if (count($errors) == 0)
286 foreach($parameters as $key => $value)
288 if (!isset($validation[$key]))
291 $record= $validation[$key];
292 if (!isset($record[
"required"]) || !isset($record[
"type"]))
294 $errors[]=
'validation record for ' . $key .
' is malformed. "required" and "type" attributes are required.';
298 if (trim($value) ==
"")
300 $parameters[$key]=
"";
301 if ($record[
"required"] ==
true)
302 $errors[]=
"$key is required.";
303 else if ($record[
"type"] ==
"number")
304 $parameters[$key]= 0;
308 switch ($record[
"type"])
311 if (isset($record[
"maxlength"]))
312 $parameters[$key]= prepSave($value, intval($record[
"maxlength"]));
314 $parameters[$key]= prepSave($value);
317 $phone= cleanPhone($value);
319 $errors[]=
"$key has an invalid phone format.";
320 $parameters[$key]= $phone;
323 if (isset($record[
"maxlength"]))
324 $email= cleanEmail($value, intval($record[
"maxlength"]));
326 $email= cleanEmail($value);
328 $errors[]=
"$key has an invalid email format.";
329 $parameters[$key]= $email;
332 $date= cleanDate($value);
334 $errors[]=
"$key has an invalid date format.";
335 $parameters[$key]= $date;
339 $allowNegatives= isset($record[
"allowNegatives"]) && $record[
"allowNegatives"];
340 if (isset($record[
"subtype"]))
341 $number= cleanInteger($value, $allowNegatives, $record[
"subtype"]);
343 $number= cleanInteger($value, $allowNegatives);
344 if ($number ==
"ERROR")
345 $errors[]=
"$key has an invalid integer format.";
346 $parameters[$key]= $number;
349 $number= cleanNumber($value);
350 if ($number ==
"ERROR")
351 $errors[]=
"$key has an invalid number format.";
352 $parameters[$key]= $number;
355 if (isset($record[
"firstValue"]) && isset($record[
"secondValue"]))
356 $boolean= cleanBoolean($value, $record[
"required"], $record[
"firstValue"], $record[
"secondValue"]);
358 $boolean= cleanBoolean($value, $record[
"required"]);
360 $errors[]=
"$key has an invalid boolean format.";
361 $parameters[$key]= $boolean;
364 $time= cleanTime($value);
366 $errors[]=
"$key has an invalid time format.";
367 $parameters[$key]= $time;
370 $parameters[$key]=
"";
371 $errors[]=
"$key doesn't have an appropriate type.";
377 function dbErrorNoFormatting() {
382 return @sybase_get_last_message();
384 return @mysql_error();
386 return @pg_errormessage();