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