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);
43 function exec_pipe($command, $stdin) {
44 $descriptorspec = array(
45 0 => array('pipe', 'r'), // stdin is a pipe that the child will read from
46 1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
47 2 => array('file', '/dev/null', 'w') // stderr is a pipe that the child will write to
50 $process = proc_open($command, $descriptorspec, $pipes);
52 if (is_resource($process)) {
53 fwrite($pipes[0], $stdin);
56 while (!feof($pipes[1])) {
57 $chunk = fread($pipes[1], 1024);
64 // It is important that you close any pipes before calling
65 // proc_close in order to avoid a deadlock
66 $return_value = proc_close($process);
68 return array($return_value, $stdout);
77 function unix_newlines($str) {
78 $str = str_replace("\r\n", "\n", $str);
79 return str_replace("\r", "\n", $str);
82 # return current year (all 4 digits)
83 function this_year() {
84 return strftime('%Y');
87 # return the number of the current month (1..12)
88 function this_month() {
89 return ereg_replace('^0', '', strftime('%m'));
92 # return today's date in yyyy-mm-dd format
93 function today_ymd() {
94 return strftime('%Y-%m-%d');
97 # php4 is broken, in that you cannot set a default value for a parameter that
98 # is passed by reference. So, this is set up to use the following screwy
101 # function foo($bar = 0) {
111 class stupid_reference {
113 function stupid_reference(&$ref) {
117 function ref(&$foo) {
118 return new stupid_reference($foo);