3 # Copyright (C) 2006 Jason Woofenden
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # pass an integer, returns the suffix
19 function ordinal_suffix($i) {
21 if(strlen($i) > 1 && substr($i, -2, 1) === '1') {
24 switch(substr($i, -1)) {
38 # ordinalize(1) => "1st"
39 # ordinalize(2) => "2nd"
40 # ordinalize(3) => "3rd"
41 # ordinalize(111) => "111th"
42 # ordinalize("62") => "62nd"
43 function ordinalize($i) {
44 return $i . ordinal_suffix($i);
47 # returns an array containing just the elements of $pipes that are readable (without blocking)
48 # timeout 0 means don't wait, timeout NULL means wait indefinitely
49 function readable_sockets($pipes, $timeout = 0){
50 $read = array_values($pipes);
51 $ret = stream_select($read, $write = NULL, $exceptions = NULL, $timeout);
69 function exec_pipe($command, $stdin) {
70 $descriptorspec = array(
71 0 => array('pipe', 'r'), // stdin is a pipe that the child will read from
72 1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
73 2 => array('file', '/dev/null', 'w') // stderr is a pipe that the child will write to
76 $process = proc_open($command, $descriptorspec, $pipes);
78 if (is_resource($process)) {
79 fwrite($pipes[0], $stdin);
82 while (!feof($pipes[1])) {
83 $chunk = fread($pipes[1], 1024);
90 // It is important that you close any pipes before calling
91 // proc_close in order to avoid a deadlock
92 $return_value = proc_close($process);
94 return array($return_value, $stdout);
99 function unix_newlines($str) {
100 $str = str_replace("\r\n", "\n", $str);
101 return str_replace("\r", "\n", $str);
105 # return current year (all 4 digits)
106 function this_year() {
107 return strftime('%Y');
110 # return the number of the current month (1..12)
111 function this_month() {
112 return ereg_replace('^0', '', strftime('%m'));
115 # return today's date in yyyy-mm-dd format
116 function today_ymd() {
117 return strftime('%Y-%m-%d');
121 function get_text_between($text, $start_text, $end_text) {
122 $start = strpos($text, $start_text);
123 if($start === false) {
126 $text = substr($text, $start + strlen($start_text));
127 $end = strpos($text, $end_text);
131 return substr($text, 0, $end);
134 # Make it easy to insert an array into the template data structure so that each
135 # element of the array gets its own row.
137 # passed this: columnize(array('a', 'b', 'c'), 'k');
138 # it returns: array(array('k' => 'a'),
140 # array('k' => 'c'));
141 # passed this: columnate(array(), 'k');
143 function columnize($arr, $key = 'data') {
148 foreach($arr as $val) {
149 $ret[] = array($key => $val);
154 # php4 is broken, in that you cannot set a default value for a parameter that
155 # is passed by reference. So, this is set up to use the following screwy
158 # function foo($bar = 0) {
168 class stupid_reference {
170 function stupid_reference(&$ref) {
174 function ref(&$foo) {
175 return new stupid_reference($foo);
178 function &last(&$array) {
179 if(count($array)) return $array[count($array) - 1];