3 # Copyright (C) 2006 Jason Woofenden
5 # This file is part of wfpl.
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)
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
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
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);
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
51 $process = proc_open($command, $descriptorspec, $pipes);
53 if (is_resource($process)) {
54 fwrite($pipes[0], $stdin);
59 while($stdout_open || $stderr_open) {
60 $pipes_to_check = array();
62 $pipes_to_check[] = $pipes[1];
65 $pipes_to_check[] = $pipes[2];
67 $readables = readable_sockets($pipes_to_check);
68 if($readables === false) {
71 foreach($readables as $pipe) {
72 $ret = fread($pipe, 4096);
74 die('fread (in exec_pipe) failed');
76 if($pipe = $pipes[1]) {
84 if($pipe = $pipes[2]) {
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);
99 return array($return_value, $stdout, $stderr);
108 function unix_newlines($str) {
109 $str = str_replace("\r\n", "\n", $str);
110 return str_replace("\r", "\n", $str);
113 # return current year (all 4 digits)
114 function this_year() {
115 return strftime('%Y');
118 # return the number of the current month (1..12)
119 function this_month() {
120 return ereg_replace('^0', '', strftime('%m'));
123 # return today's date in yyyy-mm-dd format
124 function today_ymd() {
125 return strftime('%Y-%m-%d');
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
132 # function foo($bar = 0) {
142 class stupid_reference {
144 function stupid_reference(&$ref) {
148 function ref(&$foo) {
149 return new stupid_reference($foo);