]>
Commit | Line | Data |
---|---|---|
c47f0dc2 JP |
1 | <?php |
2 | /* | |
3 | (c) 2005 Joe Presbrey | |
4 | joepresbrey@gmail.com | |
c47f0dc2 JP |
5 | */ |
6 | ||
9835a21d | 7 | function isPost() { |
c47f0dc2 JP |
8 | if($_SERVER['REQUEST_METHOD'] == 'POST') { |
9 | return true; | |
10 | } else { | |
11 | return false; | |
12 | } | |
13 | } | |
9835a21d | 14 | function isFormPost() { return isPost(); } |
c47f0dc2 JP |
15 | |
16 | function isSess($id) { | |
17 | return isset($_SESSION[$id]); | |
18 | } | |
19 | ||
9835a21d | 20 | function sess($id,$val=null) { |
c47f0dc2 | 21 | if (is_null($val)) { |
9835a21d | 22 | return (isSess($id)?$_SESSION[$id]:null); |
c47f0dc2 JP |
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) { | |
9835a21d | 43 | @unlink(session_save_path().'/sess_'.$id); |
c47f0dc2 JP |
44 | } |
45 | } | |
46 | ||
47 | function sessTime($query=null) { | |
9835a21d | 48 | global $timingc; |
c47f0dc2 JP |
49 | global $timings; |
50 | ||
51 | if(!isset($timings)) { | |
52 | $timings = array(); | |
53 | } | |
54 | ||
9835a21d JP |
55 | if (!isset($timingc) || empty($timingc)) { |
56 | $timingc = 1; | |
c47f0dc2 | 57 | } elseif (!is_null($query)) { |
9835a21d JP |
58 | $current = $timingc; |
59 | $timingc = ++$current; | |
c47f0dc2 | 60 | } |
9835a21d | 61 | $key = $timingc; |
c47f0dc2 JP |
62 | |
63 | if (is_null($query)) { | |
64 | $timings[$key]['time'] = microtime(true)-$timings[$key]['time']; | |
9835a21d JP |
65 | if (mysql_error()) |
66 | $timings[$key]['error'] = mysql_error(); | |
c47f0dc2 JP |
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) { | |
c47f0dc2 JP |
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 | ||
9835a21d JP |
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 | } | |
c47f0dc2 | 115 | } |
9835a21d | 116 | echo (count($err)>1?'</ul>':''),'</div>'; |
c47f0dc2 JP |
117 | } |
118 | } | |
119 | ||
ba40a1ca JP |
120 | function buildSQLSet($fields, $values=null, $safeFields=false) { |
121 | $ex = array('NOW()','NULL','/FROM_UNIXTIME\(\d+\)/'); | |
122 | $sql = ''; | |
c47f0dc2 JP |
123 | $c = 0; |
124 | if (!is_null($values)) { | |
125 | foreach($fields as $field) { | |
126 | if ($c++) $sql .= ','; | |
ba40a1ca JP |
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)."'"; | |
c47f0dc2 JP |
132 | } |
133 | } else { | |
134 | foreach($fields as $field=>$value) { | |
135 | if ($c++) $sql .= ','; | |
ba40a1ca JP |
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 { | |
3fd9e3cb | 141 | $sql .= " `$field`='".mysql_real_escape_string($value)."'"; |
c47f0dc2 JP |
142 | } |
143 | } | |
144 | } | |
145 | return $sql; | |
146 | } | |
147 | ||
ba40a1ca JP |
148 | function buildSQLInsert($array, $table=null, $safeFields=false) { |
149 | $ex = array('NOW()','NULL','/FROM_UNIXTIME\(\d+\)/'); | |
c47f0dc2 JP |
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) { | |
c47f0dc2 | 159 | if ($c++) $sql .= ','; |
ba40a1ca JP |
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 | } | |
c47f0dc2 JP |
170 | } |
171 | $sql .= ')'; | |
3fd9e3cb | 172 | return (is_null($table)?$sql:('INSERT INTO `'.$table.'` '.$sql)); |
c47f0dc2 JP |
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 | ||
9835a21d | 186 | function newQS($key, $val=null) { |
9835a21d | 187 | return newQSA(array($key=>$val)); |
c47f0dc2 JP |
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 | ?> |