JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: merge ckeditor settings from cms
[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 # pass an integer, returns the suffix
19 function ordinal_suffix($i) {
20         # teens are all __th
21         if(strlen($i) > 1 && substr($i, -2, 1) === '1') {
22                 return 'th';
23         }
24         switch(substr($i, -1)) {
25                 case '':
26                         return '';
27                 case '1':
28                         return 'st';
29                 case '2':
30                         return 'nd';
31                 case '3':
32                         return 'rd';
33                 default;
34                         return 'th';
35         }
36 }
37
38 # ordinalize(1) => "1st"
39 # ordinalize(2) => "2nd"
40 # ordinalize(3) => "3rd"
41 # ordinalize(111) => "111th"
42 # ordinalize("62") => "62nd"
43 function ordinalize($i) {
44         return $i . ordinal_suffix($i);
45 }
46
47 # remove the $key from $_REQUEST and return it's value (or null if it's not there)
48 function _REQUEST_cut($key) {
49         if(!isset($_REQUEST[$key])) {
50                 return null;
51         }
52         $ret = $_REQUEST[$key];
53         unset($_REQUEST[$key]);
54         return $ret;
55 }
56
57 # returns an array containing just the elements of $pipes that are readable (without blocking)
58 # timeout 0 means don't wait, timeout NULL means wait indefinitely
59 function readable_sockets($pipes, $timeout = 0){
60         $read = array_values($pipes);
61         $ret = stream_select($read, $write = NULL, $exceptions = NULL, $timeout);
62         if($ret === false) {
63                 return false;
64         }
65         if($ret) {
66                 return $read;
67         } else {
68                 return array();
69         }
70 }
71
72
73 # Parameters:
74 #     command
75 #     stdin
76 # Returns: (as array)
77 #     exit code
78 #     stdout
79 function exec_pipe($command, $stdin) {
80         $descriptorspec = array(
81            0 => array('pipe', 'r'),  // stdin is a pipe that the child will read from
82            1 => array('pipe', 'w'),  // stdout is a pipe that the child will write to
83            2 => array('file', '/dev/null', 'w')   // stderr is a pipe that the child will write to
84         );
85
86         $process = proc_open($command, $descriptorspec, $pipes);
87
88         if (is_resource($process)) {
89                 fwrite($pipes[0], $stdin);
90                 fclose($pipes[0]);
91
92                 while (!feof($pipes[1])) {
93                         $chunk = fread($pipes[1], 1024);
94                         $stdout .= $chunk;
95                         sleep(0.5);
96                 }
97
98                 fclose($pipes[1]);
99
100                 // It is important that you close any pipes before calling
101                 // proc_close in order to avoid a deadlock
102                 $return_value = proc_close($process);
103
104                 return array($return_value, $stdout);
105         }
106 }
107
108
109 function unix_newlines($str) {
110         $str = str_replace("\r\n", "\n", $str);
111         return str_replace("\r", "\n", $str);
112 }
113
114
115 # return current year (all 4 digits)
116 function this_year() {
117         return strftime('%Y');
118 }
119
120 # return the number of the current month (1..12)
121 function this_month() {
122         return preg_replace('|^0|', '', strftime('%m'));
123 }
124
125 # return today's date in yyyy-mm-dd format
126 function today_ymd() {
127         return strftime('%Y-%m-%d');
128 }
129
130 function now_yyyymmdd() {
131         return strftime('%Y-%m-%d');
132 }
133
134 function now_yyyymmddhhmmss() {
135         return strftime('%Y-%m-%d %H:%M:%S');
136 }
137
138
139 function get_text_between($text, $start_text, $end_text) {
140         $start = strpos($text, $start_text);
141         if($start === false) {
142                 return false;
143         }
144         $text = substr($text, $start + strlen($start_text));
145         $end = strpos($text, $end_text);
146         if($end === false) {
147                 return false;
148         }
149         return substr($text, 0, $end);
150 }
151
152 # Make it easy to insert an array into the template data structure so that each
153 # element of the array gets its own row.
154 #
155 # passed this: columnize(array('a', 'b', 'c'), 'k');
156 # it returns: array(array('k' => 'a'),
157 #                   array('k' => 'b'),
158 #                   array('k' => 'c'));
159 # passed this: columnate(array(), 'k');
160 # it returns: false
161 function columnize($arr, $key = 'data') {
162         if(!$arr) {
163                 return false;
164         }
165         $ret = array();
166         foreach($arr as $val) {
167                 $ret[] = array($key => $val);
168         }
169         return $ret;
170 }
171
172 # php4 is broken, in that you cannot set a default value for a parameter that
173 # is passed by reference. So, this is set up to use the following screwy
174 # syntax:
175 #
176 # function foo($bar = 0) {
177 #   if($bar !== 0) {
178 #     $bar = $bar->ref;
179 #   }
180 #       ...
181 # }
182 #
183 # foo();
184 # foo(ref($baz));
185
186 class stupid_reference {
187         var $ref;
188         function stupid_reference(&$ref) {
189                 $this->ref = &$ref;
190         }
191 }
192 function ref(&$foo) {
193         return new stupid_reference($foo);
194 }
195
196 function &last(&$array) {
197         if(count($array)) return $array[count($array) - 1];
198 }
199
200 ?>