JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
revamped uploaded image handling, added thumbnailing support to metaform
[wfpl.git] / format.php
1 <?php
2
3 #  Copyright (C) 2005 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
19 # This file contains basic encodings
20
21 function format_caption($str) {
22         $str = str_replace('_', ' ', $str);
23         $str = ucwords($str);
24         return str_replace('Email', 'E-mail', $str);
25 }
26
27 # This function makes sure that $str is in the list of options, and returns "" otherwise
28 function format_options($str, $name) {
29         if(!isset($GLOBALS[$name . '_options'])) {
30                 die("Couldn't find options for \"$name\". Be sure to call pulldown().");
31         }
32
33         foreach($GLOBALS[$name . '_options']['options'] as $keyval) {
34                 list($key, $value) = $keyval;
35                 if($str == $key) {
36                         return $str;
37                 }
38         }
39
40         return $str;
41 }
42
43 function format_int($str) {
44         $str = ereg_replace('[^0-9]', '', $str);
45         return ereg_replace('^0*([0-9])', '\1', $str);
46 }
47
48 function format_decimals($str) {
49         $str = ereg_replace('[^0-9]', '', $str);
50         return ereg_replace('^([0-9])0*', '\1', $str);
51 }
52
53 function _format_positive_decimal($str) {
54         $str = ereg_replace('[^0-9.]', '', $str);
55         $pos = strpos($str, '.');
56         if($pos !== false) {
57                 $str = str_replace('.', '', $str);
58                 if($pos == 0) {
59                         return '0.' . format_decimals($str);
60                 } elseif($pos == strlen($str)) {
61                         return format_int($str);
62                 } else {
63                         return format_int(substr($str, 0, $pos)) . '.' . format_decimals(substr($str, $pos));
64                 }
65         }
66         return format_int($str);
67 }
68
69 function format_positive_decimal($str) {
70         $str = _format_positive_decimal($str);
71         if($str === '0.0') {
72                 $str = '0';
73         }
74         return $str;
75 }
76
77 function format_decimal($str) {
78         $str = ereg_replace('[^0-9.-]', '', $str);
79         if(substr($str, 0, 1) == '-') {
80                 $str = format_positive_decimal(substr($str, 1));
81                 if($str !== '' && $str !== '0') {
82                         $str = '-' . $str;
83                 }
84                 return $str;
85         } else {
86                 return format_positive_decimal($str);
87         }
88 }
89
90 # return 0 of there's no digits
91 function format_int_0($str) {
92         $str = format_int($str);
93         if($str == '') {
94                 return '0';
95         }
96         return $str;
97 }
98
99 function format_zip($str) {
100         $str = ereg_replace('[^0-9]', '', $str);
101         if(strlen($str) > 5) {
102                 return substr($str, 0, 5) . '-' . substr($str, 5);
103         }
104         return $str;
105 }
106
107 function format_filename($str, $allow_uppercase = false) {
108         if(!$allow_uppercase) {
109                 $str = strtolower($str);
110         }
111         $str = ereg_replace('[^a-zA-Z0-9_.-]', '_', $str);
112         return ereg_replace('^[.-]', '_', $str);
113 }
114
115 function format_path($str, $allow_uppercase = false) {
116         if(!$allow_uppercase) {
117                 $str = strtolower($str);
118         }
119         $str = ereg_replace('[^a-zA-Z0-9_./-]', '_', $str);
120         return ereg_replace('^[.-]', '_', $str);
121 }
122
123 function client_path_to_filename($path) {
124         $filename = ereg_replace(".*[:/\\]", '', $path);
125         return format_filename($filename, true);
126 }
127
128
129 function format_image_w_h($str) {
130         $fields = explode(' ', $str);
131         if(count($fields) != 3) {
132                 return '';
133         }
134
135         list($filename, $width, $height) = $fields;
136         $filename = format_path($filename);
137         $width = format_int_0($width);
138         $height = format_int_0($height);
139
140         return "$filename $width $height";
141 }
142
143 function format_image_w_h_thumb_w_h($str) {
144         $fields = explode(' ', $str);
145         if(count($fields) != 6) {
146                 die('count: ' . count($fields));
147                 return '';
148         }
149
150         list($filename, $width, $height, $thumb_filename, $thumb_width, $thumb_height) = $fields;
151         $filename = format_path($filename);
152         $width = format_int_0($width);
153         $height = format_int_0($height);
154         $thumb_filename = format_path($thumb_filename);
155         $thumb_width = format_int_0($thumb_width);
156         $thumb_height = format_int_0($thumb_height);
157
158         return "$filename $width $height $thumb_filename $thumb_width $thumb_height";
159 }
160
161 function format_varname($str) {
162         $str = strtolower($str);
163         $str = ereg_replace('[^a-z0-9_]', '_', $str);
164         return ereg_replace('^[0-9]*', '', $str);
165 }
166
167 function format_oneline($str) {
168         $str = str_replace("\r", '', $str);
169         return str_replace("\n", '', $str);
170 }
171
172 function format_unix($str) {
173         return unix_newlines($str);
174 }
175
176 function format_bool($str) {
177         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
178                 return 1;
179         } else {
180                 return 0;
181         }
182 }
183
184 function format_yesno($str) {
185         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
186                 return 'Yes';
187         } else {
188                 return 'No';
189         }
190 }
191
192 function format_email($str) {
193         # FIXME
194         return trim(format_oneline($str));
195 }
196
197 function format_url($str) {
198         # FIXME check for TLD? encode special chars?
199         $str = trim(format_oneline($str));
200         if($str !== '') {
201                 if(strpos($str, ':') === false) {
202                         $str = 'http://' . $str;
203                 }
204         }
205         return $str;
206 }
207
208 function format_money($str, $display_cents = true) {
209         $str = ereg_replace('[^0-9.]', '', $str);
210         if($display_cents) {
211                 $int = (int)($str * 100);
212                 $cents = $int % 100;
213                 $cents = sprintf('.%02d', $cents);
214                 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
215         } else {
216                 $cents = '';
217                 $int = round($str);
218         }
219         $chars = (string)$int;
220         $output = '';
221         $comma = 4;
222         $index = strlen($chars);
223         while($index) {
224                 --$index;
225                 --$comma;
226                 if($comma == 0) {
227                         $comma = 3;
228                         $output = ',' . $output;
229                 }
230                 $char = substr($chars, $index, 1);
231                 $output = $char . $output;
232         }
233         $output = '$' . $output . $cents;
234         return $output;
235 }
236
237 function format_dollars($str) {
238         return format_money($str, false);
239 }
240
241 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
242 function format_mdy_to_ymd($str) {
243         require_once('code/wfpl/time.php');
244         return mdy_to_ymd(format_oneline($str));
245 }
246
247 # date is yyyy-mm-dd
248 function format_ymd($str) {
249         require_once('code/wfpl/time.php');
250         list($year, $month, $day) = ymd_clean($str);
251         return sprintf('%04u-%02u-%02u', $year, $month, $day);
252 }
253
254 # takes any of: HH  :MM  HH:MM
255 # returns decimal number of hours
256 #
257 # You probably want to use format_hours() instead because it handles hours with a decimal point.
258 function format_hours_minutes($str) {
259         if(strlen($str) == 0) {
260                 return $str;
261         }
262         $pos = strpos($str, ':');
263         if($pos === false) {
264                 $hours = format_int_0($str);
265                 $minutes = 0;
266         } elseif($pos == 0) {
267                 $hours = 0;
268                 $minutes = format_int_0($str);
269         } else {
270                 $hours = format_int_0(substr($str, 0, $pos));
271                 $minutes = format_int_0(substr($str, $pos + 1));
272         }
273         return $hours + ($minutes / 60.0);
274 }
275
276 # takes any of: HH  :MM  HH:MM  HH.hh(decimal hours)
277 # returns decimal number of hours
278 function format_hours($str) {
279         if(strlen($str) == 0) {
280                 return $str;
281         }
282         if(strpos($str, ':') !== false) {
283                 return format_hours_minutes($str);
284         } else {
285                 return format_decimal($str);
286         }
287 }
288
289 # takes eg 12:23am
290 # returns decimal number of hours since midnight
291 function format_12hr_to_hours($str) {
292         if(eregi('noon', $str)) {
293                 return 12;
294         }
295         $hours = format_hours($str);
296         if($hours < 12 && eregi('p', $str)) {
297                 $hours += 12;
298         }
299         return $hours;
300 }
301
302
303 function format_phone($str) {
304         $str = ereg_replace('[^0-9]', '', $str);
305         $str = ereg_replace('^1*', '', $str);
306         $len = strlen($str);
307         $output = '';
308
309         if($len < 10 && $len != 7) {
310                 #NOT A VALID PHONE NUMBER
311                 return $str;
312         }
313
314         if($len > 10) {
315                 $output = ' ext: ' . substr($str, 10);
316                 $len = 10;
317         }
318
319         if($len == 10) {
320                 $area = substr($str, 0, 3);
321                 $str = substr($str, 3);
322         }
323
324         $output = substr($str, 3) . $output;
325         $output = substr($str, 0, 3) . '-' . $output;
326
327         if($area) {
328                 $output = "($area) " . $output;
329         }
330
331         return $output;
332 }
333
334 ?>