JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform's html type supports image uploads by default
[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 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
48         );
49
50         $process = proc_open($command, $descriptorspec, $pipes);
51
52         if (is_resource($process)) {
53                 fwrite($pipes[0], $stdin);
54                 fclose($pipes[0]);
55
56                 while (!feof($pipes[1])) {
57                         $chunk = fread($pipes[1], 1024);
58                         $stdout .= $chunk;
59                         sleep(0.5);
60                 }
61
62                 fclose($pipes[1]);
63
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);
67
68                 return array($return_value, $stdout);
69         }
70 }
71
72
73
74
75
76
77 function unix_newlines($str) {
78         $str = str_replace("\r\n", "\n", $str);
79         return str_replace("\r", "\n", $str);
80 }
81
82 # return current year (all 4 digits)
83 function this_year() {
84         return strftime('%Y');
85 }
86
87 # return the number of the current month (1..12)
88 function this_month() {
89         return ereg_replace('^0', '', strftime('%m'));
90 }
91
92 # return today's date in yyyy-mm-dd format
93 function today_ymd() {
94         return strftime('%Y-%m-%d');
95 }
96
97
98 function get_text_between($text, $start_text, $end_text) {
99         $start = strpos($text, $start_text);
100         if($start === false) {
101                 return false;
102         }
103         $text = substr($text, $start + strlen($start_text));
104         $end = strpos($text, $end_text);
105         if($end === false) {
106                 return false;
107         }
108         return substr($text, 0, $end);
109 }
110
111 # php4 is broken, in that you cannot set a default value for a parameter that
112 # is passed by reference. So, this is set up to use the following screwy
113 # syntax:
114 #
115 # function foo($bar = 0) {
116 #   if($bar !== 0) {
117 #     $bar = $bar->ref;
118 #   }
119 #       ...
120 # }
121 #
122 # foo();
123 # foo(ref($baz));
124
125 class stupid_reference {
126         var $ref;
127         function stupid_reference(&$ref) {
128                 $this->ref = &$ref;
129         }
130 }
131 function ref(&$foo) {
132         return new stupid_reference($foo);
133 }
134
135 ?>