JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Fix db_get_value after mysql->mysqli upgrade
[wfpl.git] / misc.php
1 <?php
2
3 # This program is in the public domain within the United States. Additionally,
4 # we waive copyright and related rights in the work worldwide through the CC0
5 # 1.0 Universal public domain dedication, which can be found at
6 # http://creativecommons.org/publicdomain/zero/1.0/
7
8
9 # pass an integer, returns the suffix
10 function ordinal_suffix($i) {
11         # teens are all __th
12         if(strlen($i) > 1 && substr($i, -2, 1) === '1') {
13                 return 'th';
14         }
15         switch(substr($i, -1)) {
16                 case '':
17                         return '';
18                 case '1':
19                         return 'st';
20                 case '2':
21                         return 'nd';
22                 case '3':
23                         return 'rd';
24                 default;
25                         return 'th';
26         }
27 }
28
29 # ordinalize(1) => "1st"
30 # ordinalize(2) => "2nd"
31 # ordinalize(3) => "3rd"
32 # ordinalize(111) => "111th"
33 # ordinalize("62") => "62nd"
34 function ordinalize($i) {
35         return $i . ordinal_suffix($i);
36 }
37
38 # remove the $key from $_REQUEST and return it's value (or null if it's not there)
39 function _REQUEST_cut($key) {
40         if(!isset($_REQUEST[$key])) {
41                 return null;
42         }
43         $ret = $_REQUEST[$key];
44         unset($_REQUEST[$key]);
45         return $ret;
46 }
47
48 # returns an array containing just the elements of $pipes that are readable (without blocking)
49 # timeout 0 means don't wait, timeout NULL means wait indefinitely
50 function readable_sockets($pipes, $timeout = 0){
51         $read = array_values($pipes);
52         $ret = stream_select($read, $write = NULL, $exceptions = NULL, $timeout);
53         if($ret === false) {
54                 return false;
55         }
56         if($ret) {
57                 return $read;
58         } else {
59                 return array();
60         }
61 }
62
63
64 # Parameters:
65 #     command
66 #     stdin
67 # Returns: (as array)
68 #     exit code
69 #     stdout
70 function exec_pipe($command, $stdin) {
71         $descriptorspec = array(
72            0 => array('pipe', 'r'),  // stdin is a pipe that the child will read from
73            1 => array('pipe', 'w'),  // stdout is a pipe that the child will write to
74            2 => array('file', '/dev/null', 'w')   // stderr is a pipe that the child will write to
75         );
76
77         $process = proc_open($command, $descriptorspec, $pipes);
78
79         if (is_resource($process)) {
80                 fwrite($pipes[0], $stdin);
81                 fclose($pipes[0]);
82
83                 while (!feof($pipes[1])) {
84                         $chunk = fread($pipes[1], 1024);
85                         $stdout .= $chunk;
86                         sleep(0.5);
87                 }
88
89                 fclose($pipes[1]);
90
91                 // It is important that you close any pipes before calling
92                 // proc_close in order to avoid a deadlock
93                 $return_value = proc_close($process);
94
95                 return array($return_value, $stdout);
96         }
97 }
98
99
100 function unix_newlines($str) {
101         $str = str_replace("\r\n", "\n", $str);
102         return str_replace("\r", "\n", $str);
103 }
104
105
106 # return current year (all 4 digits)
107 function this_year() {
108         return strftime('%Y');
109 }
110
111 # return the number of the current month (1..12)
112 function this_month() {
113         return preg_replace('|^0|', '', strftime('%m'));
114 }
115
116 # return today's date in yyyy-mm-dd format
117 function today_ymd() {
118         return strftime('%Y-%m-%d');
119 }
120
121 function now_yyyymmdd() {
122         return strftime('%Y-%m-%d');
123 }
124
125 function now_yyyymmddhhmmss() {
126         return strftime('%Y-%m-%d %H:%M:%S');
127 }
128
129
130 function get_text_between($text, $start_text, $end_text) {
131         $start = strpos($text, $start_text);
132         if($start === false) {
133                 return false;
134         }
135         $text = substr($text, $start + strlen($start_text));
136         $end = strpos($text, $end_text);
137         if($end === false) {
138                 return false;
139         }
140         return substr($text, 0, $end);
141 }
142
143 # Make it easy to insert an array into the template data structure so that each
144 # element of the array gets its own row.
145 #
146 # passed this: columnize(array('a', 'b', 'c'), 'k');
147 # it returns: array(array('k' => 'a'),
148 #                   array('k' => 'b'),
149 #                   array('k' => 'c'));
150 # passed this: columnize(array(), 'k');
151 # it returns: false
152 function columnize($arr, $key = 'data') {
153         if(!$arr) {
154                 return false;
155         }
156         $ret = array();
157         foreach($arr as $val) {
158                 $ret[] = array($key => $val);
159         }
160         return $ret;
161 }
162
163 # php4 is broken, in that you cannot set a default value for a parameter that
164 # is passed by reference. So, this is set up to use the following screwy
165 # syntax:
166 #
167 # function foo($bar = 0) {
168 #   if($bar !== 0) {
169 #     $bar = $bar->ref;
170 #   }
171 #       ...
172 # }
173 #
174 # foo();
175 # foo(ref($baz));
176
177 class stupid_reference {
178         var $ref;
179         function stupid_reference(&$ref) {
180                 $this->ref = &$ref;
181         }
182 }
183 function ref(&$foo) {
184         return new stupid_reference($foo);
185 }
186
187 function &last(&$array) {
188         if(count($array)) return $array[count($array) - 1];
189 }