JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
wrote exec thingie, but it doesn't work
[wfpl.git] / misc.php
1 <?php
2
3 #  Copyright (C) 2006 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it under the
8 #  terms of the GNU Lesser General Public License as published by the Free
9 #  Software Foundation; either version 2.1 of the License, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 #  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
15 #  more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public License
18 #  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 #  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21 # returns an array containing just the elements of $pipes that are readable (without blocking)
22 # timeout 0 means don't wait, timeout NULL means wait indefinitely
23 function readable_sockets($pipes, $timeout = 0){
24         $read = array_values($pipes);
25         $ret = stream_select($read, $write = NULL, $exceptions = NULL, $timeout);
26         if($ret === false) {
27                 return false;
28         }
29         if($ret) {
30                 return $read;
31         } else {
32                 return array();
33         }
34 }
35
36
37 # Parameters:
38 #     command
39 #     stdin
40 # Returns: (as array)
41 #     exit code
42 #     stdout
43 #     stderr
44 function exec_pipe($command, $stdin) {
45         $descriptorspec = array(
46            0 => array('pipe', 'r'),  // stdin is a pipe that the child will read from
47            1 => array('pipe', 'w'),  // stdout is a pipe that the child will write to
48            2 => array('pipe', 'w')   // stderr is a pipe that the child will write to
49         );
50
51         $process = proc_open($command, $descriptorspec, $pipes);
52
53         if (is_resource($process)) {
54                 fwrite($pipes[0], $stdin);
55                 fclose($pipes[0]);
56
57                 $stdout_open = true;
58                 $stderr_open = true;
59                 while($stdout_open || $stderr_open) {
60                         $pipes_to_check = array();
61                         if($stdout_open) {
62                                 $pipes_to_check[] = $pipes[1];
63                         }
64                         if($stderr_open) {
65                                 $pipes_to_check[] = $pipes[2];
66                         }
67                         $readables = readable_sockets($pipes_to_check);
68                         if($readables === false) {
69                                 die('select failed');
70                         }
71                         foreach($readables as $pipe) {
72                                 $ret = fread($pipe, 4096);
73                                 if($ret === false) {
74                                         die('fread (in exec_pipe) failed');
75                                 }
76                                 if($pipe = $pipes[1]) {
77                                         if($ret == '') {
78                                                 fclose($pipes[1]);
79                                                 $stdout_open = false;
80                                         } else {
81                                                 $stdout .= $ret;
82                                         }
83                                 }
84                                 if($pipe = $pipes[2]) {
85                                         if($ret == '') {
86                                                 fclose($pipes[2]);
87                                                 $stderr_open = false;
88                                         } else {
89                                                 $stderr .= $ret;
90                                         }
91                                 }
92                         }
93                 }
94
95                 // It is important that you close any pipes before calling
96                 // proc_close in order to avoid a deadlock
97                 $return_value = proc_close($process);
98
99                 return array($return_value, $stdout, $stderr);
100         }
101 }
102
103
104
105
106
107
108 function unix_newlines($str) {
109         $str = str_replace("\r\n", "\n", $str);
110         return str_replace("\r", "\n", $str);
111 }
112
113 # return current year (all 4 digits)
114 function this_year() {
115         return strftime('%Y');
116 }
117
118 # return the number of the current month (1..12)
119 function this_month() {
120         return ereg_replace('^0', '', strftime('%m'));
121 }
122
123 # return today's date in yyyy-mm-dd format
124 function today_ymd() {
125         return strftime('%Y-%m-%d');
126 }
127
128 # php4 is broken, in that you cannot set a default value for a parameter that
129 # is passed by reference. So, this is set up to use the following screwy
130 # syntax:
131 #
132 # function foo($bar = 0) {
133 #   if($bar !== 0) {
134 #     $bar = $bar->ref;
135 #   }
136 #       ...
137 # }
138 #
139 # foo();
140 # foo(ref($baz));
141
142 class stupid_reference {
143         var $ref;
144         function stupid_reference(&$ref) {
145                 $this->ref = &$ref;
146         }
147 }
148 function ref(&$foo) {
149         return new stupid_reference($foo);
150 }
151
152 ?>