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