JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixed/rewrote display_messages(), added columnize
[wfpl.git] / misc.php
1 <?php
2
3 #  Copyright (C) 2006 Jason Woofenden
4 #
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.
9 #  
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.
14 #  
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/>.
17
18 # returns an array containing just the elements of $pipes that are readable (without blocking)
19 # timeout 0 means don't wait, timeout NULL means wait indefinitely
20 function readable_sockets($pipes, $timeout = 0){
21         $read = array_values($pipes);
22         $ret = stream_select($read, $write = NULL, $exceptions = NULL, $timeout);
23         if($ret === false) {
24                 return false;
25         }
26         if($ret) {
27                 return $read;
28         } else {
29                 return array();
30         }
31 }
32
33
34 # Parameters:
35 #     command
36 #     stdin
37 # Returns: (as array)
38 #     exit code
39 #     stdout
40 function exec_pipe($command, $stdin) {
41         $descriptorspec = array(
42            0 => array('pipe', 'r'),  // stdin is a pipe that the child will read from
43            1 => array('pipe', 'w'),  // stdout is a pipe that the child will write to
44            2 => array('file', '/dev/null', 'w')   // stderr is a pipe that the child will write to
45         );
46
47         $process = proc_open($command, $descriptorspec, $pipes);
48
49         if (is_resource($process)) {
50                 fwrite($pipes[0], $stdin);
51                 fclose($pipes[0]);
52
53                 while (!feof($pipes[1])) {
54                         $chunk = fread($pipes[1], 1024);
55                         $stdout .= $chunk;
56                         sleep(0.5);
57                 }
58
59                 fclose($pipes[1]);
60
61                 // It is important that you close any pipes before calling
62                 // proc_close in order to avoid a deadlock
63                 $return_value = proc_close($process);
64
65                 return array($return_value, $stdout);
66         }
67 }
68
69
70 function unix_newlines($str) {
71         $str = str_replace("\r\n", "\n", $str);
72         return str_replace("\r", "\n", $str);
73 }
74
75
76 # return current year (all 4 digits)
77 function this_year() {
78         return strftime('%Y');
79 }
80
81 # return the number of the current month (1..12)
82 function this_month() {
83         return ereg_replace('^0', '', strftime('%m'));
84 }
85
86 # return today's date in yyyy-mm-dd format
87 function today_ymd() {
88         return strftime('%Y-%m-%d');
89 }
90
91
92 function get_text_between($text, $start_text, $end_text) {
93         $start = strpos($text, $start_text);
94         if($start === false) {
95                 return false;
96         }
97         $text = substr($text, $start + strlen($start_text));
98         $end = strpos($text, $end_text);
99         if($end === false) {
100                 return false;
101         }
102         return substr($text, 0, $end);
103 }
104
105 # Make it easy to insert an array into the template data structure so that each
106 # element of the array gets its own row.
107 #
108 # passed this: columnate(array('a', 'b', 'c'), 'k');
109 # it returns: array(array('k' => 'a'),
110 #                   array('k' => 'b'),
111 #                   array('k' => 'c'));
112 # passed this: columnate(array(), 'k');
113 # it returns: false
114 function columnize($arr, $key = 'data') {
115         if(!$arr) {
116                 return false;
117         }
118         $ret = array();
119         foreach($arr as $val) {
120                 $ret[] = array($key => $val);
121         }
122         return $ret;
123 }
124
125 # php4 is broken, in that you cannot set a default value for a parameter that
126 # is passed by reference. So, this is set up to use the following screwy
127 # syntax:
128 #
129 # function foo($bar = 0) {
130 #   if($bar !== 0) {
131 #     $bar = $bar->ref;
132 #   }
133 #       ...
134 # }
135 #
136 # foo();
137 # foo(ref($baz));
138
139 class stupid_reference {
140         var $ref;
141         function stupid_reference(&$ref) {
142                 $this->ref = &$ref;
143         }
144 }
145 function ref(&$foo) {
146         return new stupid_reference($foo);
147 }
148
149 function &last(&$array) {
150         if(count($array)) return $array[count($array) - 1];
151 }
152
153 ?>