]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | /* | |
3 | (c) 2005 Joe Presbrey | |
4 | joepresbrey@gmail.com | |
5 | */ | |
6 | ||
7 | function isPost() { | |
8 | if($_SERVER['REQUEST_METHOD'] == 'POST') { | |
9 | return true; | |
10 | } else { | |
11 | return false; | |
12 | } | |
13 | } | |
14 | function isFormPost() { return isPost(); } | |
15 | ||
16 | function isSess($id) { | |
17 | return isset($_SESSION[$id]); | |
18 | } | |
19 | ||
20 | function sess($id,$val=null) { | |
21 | if (is_null($val)) { | |
22 | return (isSess($id)?$_SESSION[$id]:null); | |
23 | } elseif (empty($val)) { | |
24 | unset($_SESSION[$id]); | |
25 | } else { | |
26 | $prev = sess($id); | |
27 | $_SESSION[$id] = $val; | |
28 | return $prev; | |
29 | } | |
30 | } | |
31 | ||
32 | function stopSess() { | |
33 | $sid[] = session_id(); | |
34 | @session_destroy(); | |
35 | session_start(); | |
36 | $sid[] = session_id(); | |
37 | session_regenerate_id(); | |
38 | $sid[] = session_id(); | |
39 | session_write_close(); | |
40 | @session_destroy(); | |
41 | ||
42 | foreach($sid as $id) { | |
43 | @unlink(session_save_path().'/sess_'.$id); | |
44 | } | |
45 | } | |
46 | ||
47 | function sessTime($query=null) { | |
48 | global $timingc; | |
49 | global $timings; | |
50 | ||
51 | if(!isset($timings)) { | |
52 | $timings = array(); | |
53 | } | |
54 | ||
55 | if (!isset($timingc) || empty($timingc)) { | |
56 | $timingc = 1; | |
57 | } elseif (!is_null($query)) { | |
58 | $current = $timingc; | |
59 | $timingc = ++$current; | |
60 | } | |
61 | $key = $timingc; | |
62 | ||
63 | if (is_null($query)) { | |
64 | $timings[$key]['time'] = microtime(true)-$timings[$key]['time']; | |
65 | if (mysql_error()) | |
66 | $timings[$key]['error'] = mysql_error(); | |
67 | return true; | |
68 | } else { | |
69 | $timings[$key] = array(); | |
70 | $timings[$key]['time'] = microtime(true); | |
71 | $timings[$key]['query'] = $query; | |
72 | return false; | |
73 | } | |
74 | } | |
75 | ||
76 | function fetchRows($rs, $key = null) { | |
77 | if (!$rs) return array(); | |
78 | $kn = is_null($key); | |
79 | $n = mysql_num_rows($rs); | |
80 | if ($n > 0) { | |
81 | $arr = array(); | |
82 | if (is_null($key)) { | |
83 | while ($r = mysql_fetch_assoc($rs)) { | |
84 | $arr[] = $r; | |
85 | } | |
86 | } elseif (is_numeric($key)) { | |
87 | while ($r = mysql_fetch_row($rs)) { | |
88 | $arr[$r[$key]] = $r; | |
89 | } | |
90 | } else { | |
91 | while ($r = mysql_fetch_assoc($rs)) { | |
92 | $arr[$r[$key]] = $r; | |
93 | } | |
94 | } | |
95 | mysql_free_result($rs); | |
96 | return $arr; | |
97 | } else { | |
98 | mysql_free_result($rs); | |
99 | return array(); | |
100 | } | |
101 | } | |
102 | ||
103 | function printErrors($err) { printList('err', $err); } | |
104 | function printMsgs($err) { printList('msg', $err); } | |
105 | ||
106 | function printList($class,$err) { | |
107 | if (is_array($err) && count($err)) { | |
108 | echo '<div class="',$class,'">',(count($err)>1?'<ul>':''); | |
109 | foreach($err as $e) { | |
110 | if (count($err)>1) { | |
111 | echo '<li><p>',$e,'</p></li>'; | |
112 | } else { | |
113 | echo '<p>',$e,'</p>'; | |
114 | } | |
115 | } | |
116 | echo (count($err)>1?'</ul>':''),'</div>'; | |
117 | } | |
118 | } | |
119 | ||
120 | function buildSQLSet($fields, $values=null, $safeFields=false) { | |
121 | $ex = array('NOW()','NULL','/FROM_UNIXTIME\(\d+\)/'); | |
122 | $sql = ''; | |
123 | $c = 0; | |
124 | if (!is_null($values)) { | |
125 | foreach($fields as $field) { | |
126 | if ($c++) $sql .= ','; | |
127 | $value = array_shift($values); | |
128 | if (is_numeric($value)) | |
129 | $sql .= " `$field`=".mysql_real_escape_string($value); | |
130 | else | |
131 | $sql .= " `$field`='".mysql_real_escape_string($value)."'"; | |
132 | } | |
133 | } else { | |
134 | foreach($fields as $field=>$value) { | |
135 | if ($c++) $sql .= ','; | |
136 | if (in_array($value,$ex) || (is_array($safeFields) && in_array($field,$safeFields))) { | |
137 | $sql .= " `$field`=$value"; | |
138 | } elseif (is_numeric($value)) { | |
139 | $sql .= " `$field`=".mysql_real_escape_string($value); | |
140 | } else { | |
141 | $sql .= " `$field`='".mysql_real_escape_string($value)."'"; | |
142 | } | |
143 | } | |
144 | } | |
145 | return $sql; | |
146 | } | |
147 | ||
148 | function buildSQLInsert($array, $table=null, $safeFields=false) { | |
149 | $ex = array('NOW()','NULL','/FROM_UNIXTIME\(\d+\)/'); | |
150 | $sql = '('; | |
151 | $c = 0; | |
152 | foreach($array as $field=>$value) { | |
153 | if ($c++) $sql .= ','; | |
154 | $sql .= " `$field` "; | |
155 | } | |
156 | $sql .= ') VALUES ('; | |
157 | $c = 0; | |
158 | foreach($array as $field=>$value) { | |
159 | if ($c++) $sql .= ','; | |
160 | if (in_array($value, $ex) || (is_array($safeFields) && in_array($field,$safeFields))) { | |
161 | $sql .= " $value "; | |
162 | } else { | |
163 | $value = mysql_real_escape_string($value); | |
164 | if (is_numeric($value)) { | |
165 | $sql .= " $value "; | |
166 | } else { | |
167 | $sql .= " '$value' "; | |
168 | } | |
169 | } | |
170 | } | |
171 | $sql .= ')'; | |
172 | return (is_null($table)?$sql:('INSERT INTO `'.$table.'` '.$sql)); | |
173 | } | |
174 | ||
175 | function build_str($query_array) { | |
176 | $query_string = array(); | |
177 | foreach ($query_array as $k => $v) { | |
178 | $new = $k; | |
179 | if (strlen($v)) | |
180 | $new .= '='.$v; | |
181 | $query_string[] = $new; | |
182 | } | |
183 | return join('&', $query_string); | |
184 | } | |
185 | ||
186 | function newQS($key, $val=null) { | |
187 | return newQSA(array($key=>$val)); | |
188 | } | |
189 | ||
190 | function newQSA($array=array()) { | |
191 | parse_str($_SERVER['QUERY_STRING'], $arr); | |
192 | $s = count($arr); | |
193 | foreach($array as $key=>$val) { | |
194 | $arr[$key] = $val; | |
195 | if (is_null($val)) | |
196 | unset($arr[$key]); | |
197 | } | |
198 | return (count($arr)||$s)?'?'.build_str($arr):''; | |
199 | } | |
200 | ||
201 | function formQSA($array=array()) { | |
202 | if (!count($array)) $array = $_SERVER['QUERY_STRING']; | |
203 | parse_str($array, $arr); | |
204 | $text = ''; | |
205 | foreach($arr as $key=>$val) { | |
206 | $text .= sprintf('<input type="hidden" name="%s" value="%s">', $key, $val); | |
207 | } | |
208 | return $text; | |
209 | } | |
210 | ||
211 | ?> |