JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
removed debugging from format_image_w_h_thumb_w_h()
[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         if(strlen($str)) {
51                 $str = substr($str, 0, 1) . ereg_replace('0*$', '', substr($str, 1));
52         }
53         return $str;
54 }
55
56 function _format_positive_decimal($str) {
57         $str = ereg_replace('[^0-9.]', '', $str);
58         $pos = strpos($str, '.');
59         if($pos !== false) {
60                 $str = str_replace('.', '', $str);
61                 if($pos == 0) {
62                         return '0.' . format_decimals($str);
63                 } elseif($pos == strlen($str)) {
64                         return format_int($str);
65                 } else {
66                         return format_int(substr($str, 0, $pos)) . '.' . format_decimals(substr($str, $pos));
67                 }
68         }
69         return format_int($str);
70 }
71
72 function format_positive_decimal($str) {
73         $str = _format_positive_decimal($str);
74         if($str === '0.0') {
75                 $str = '0';
76         }
77         return $str;
78 }
79
80 function format_decimal($str) {
81         $str = ereg_replace('[^0-9.-]', '', $str);
82         if(substr($str, 0, 1) == '-') {
83                 $str = format_positive_decimal(substr($str, 1));
84                 if($str !== '' && $str !== '0') {
85                         $str = '-' . $str;
86                 }
87                 return $str;
88         } else {
89                 return format_positive_decimal($str);
90         }
91 }
92
93 # return 0 of there's no digits
94 function format_int_0($str) {
95         $str = format_int($str);
96         if($str == '') {
97                 return '0';
98         }
99         return $str;
100 }
101
102 function format_zip($str) {
103         $str = ereg_replace('[^0-9]', '', $str);
104         if(strlen($str) > 5) {
105                 return substr($str, 0, 5) . '-' . substr($str, 5);
106         }
107         return $str;
108 }
109
110 function format_filename($str, $allow_uppercase = false) {
111         if(!$allow_uppercase) {
112                 $str = strtolower($str);
113         }
114         $str = ereg_replace('[^a-zA-Z0-9_.-]', '_', $str);
115         return ereg_replace('^[.-]', '_', $str);
116 }
117
118 function format_path($str, $allow_uppercase = false) {
119         if(!$allow_uppercase) {
120                 $str = strtolower($str);
121         }
122         $str = ereg_replace('[^a-zA-Z0-9_./-]', '_', $str);
123         return ereg_replace('^[.-]', '_', $str);
124 }
125
126 function client_path_to_filename($path) {
127         $filename = ereg_replace(".*[:/\\]", '', $path);
128         return format_filename($filename, true);
129 }
130
131
132 function format_image_w_h($str) {
133         $fields = explode(' ', $str);
134         if(count($fields) != 3) {
135                 return '';
136         }
137
138         list($filename, $width, $height) = $fields;
139         $filename = format_path($filename);
140         $width = format_int_0($width);
141         $height = format_int_0($height);
142
143         return "$filename $width $height";
144 }
145
146 function format_image_w_h_thumb_w_h($str) {
147         $fields = explode(' ', $str);
148         if(count($fields) != 6) {
149                 return '';
150         }
151
152         list($filename, $width, $height, $thumb_filename, $thumb_width, $thumb_height) = $fields;
153         $filename = format_path($filename);
154         $width = format_int_0($width);
155         $height = format_int_0($height);
156         $thumb_filename = format_path($thumb_filename);
157         $thumb_width = format_int_0($thumb_width);
158         $thumb_height = format_int_0($thumb_height);
159
160         return "$filename $width $height $thumb_filename $thumb_width $thumb_height";
161 }
162
163 function format_varname($str) {
164         $str = strtolower($str);
165         $str = ereg_replace('[^a-z0-9_]', '_', $str);
166         return ereg_replace('^[0-9]*', '', $str);
167 }
168
169 function format_oneline($str) {
170         $str = str_replace("\r", '', $str);
171         return str_replace("\n", '', $str);
172 }
173
174 function format_unix($str) {
175         return unix_newlines($str);
176 }
177
178 function format_bool($str) {
179         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
180                 return 1;
181         } else {
182                 return 0;
183         }
184 }
185
186 function format_yesno($str) {
187         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
188                 return 'Yes';
189         } else {
190                 return 'No';
191         }
192 }
193
194 function format_email($str) {
195         # FIXME
196         return trim(format_oneline($str));
197 }
198
199 function format_url($str) {
200         # FIXME check for TLD? encode special chars?
201         $str = trim(format_oneline($str));
202         if($str !== '') {
203                 if(strpos($str, ':') === false) {
204                         $str = 'http://' . $str;
205                 }
206         }
207         return $str;
208 }
209
210 # pass a string containing only numeric digits.
211 # returns a string containing only numeric digits which is 1 greater.
212 function strplusone($str) {
213         $ret = '';
214         $a = str_split($str);
215         $carry = 1;
216         while($a) {
217                 $digit = array_pop($a);
218                 $digit += $carry;
219                 $carry = 0;
220                 if($digit == 10) {
221                         $carry = 1;
222                         $digit = 0;
223                 }
224                 $ret = "$digit$ret";
225         }
226         if($carry) {
227                 $ret = "$carry$ret";
228         }
229
230         return $ret;
231 }
232
233 # rounds properly to the nearest penny (or dollar if you pass false for the 2nd
234 # parameter) and prints like so: $12,456.79 or $12,457
235 function format_money($str, $display_cents = true) {
236         $str = format_decimal($str);
237         if($str == '') {
238                 $str = '0';
239         }
240         if(strpos($str, '.')) {
241                 list($int, $decimals) = explode('.', $str);
242                 if(strlen($decimals) == 1) {
243                         $decimals .= '0';
244                 }
245                 if($display_cents) {
246                         if(strlen($decimals) > 2) {
247                                 # round up to the nearest penny
248                                 if(substr($decimals, 2, 1) >= 5) {
249                                         $decimals = strplusone(substr($decimals, 0, 2));
250                                         if($decimals == '100') {
251                                                 $decimals = '00';
252                                                 $int = strplusone($int);
253                                         }
254                                 } else {
255                                         $decimals = substr($decimals, 0, 2);
256                                 }
257                         }
258                         $cents = ".$decimals";
259                 } else {
260                         if(substr($decimals, 0, 1) >= 5) {
261                                 $int = strplusone($int);
262                         }
263                 }
264         } else {
265                 $int = $str;
266                 if($display_cents) {
267                         $cents = '.00';
268                 }
269         }
270         $chars = str_split($int);
271         $output = '';
272         $comma = 4;
273         while($chars) {
274                 --$comma;
275                 if($comma == 0) {
276                         $comma = 3;
277                         $output = ',' . $output;
278                 }
279                 $char = array_pop($chars);
280                 $output = $char . $output;
281         }
282         $output = '$' . $output . $cents;
283         return $output;
284 }
285
286
287 function format_dollars($str) {
288         return format_money($str, false);
289 }
290
291 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
292 function format_mdy_to_ymd($str) {
293         require_once('code/wfpl/time.php');
294         return mdy_to_ymd(format_oneline($str));
295 }
296
297 # date is yyyy-mm-dd
298 function format_ymd($str) {
299         require_once('code/wfpl/time.php');
300         list($year, $month, $day) = ymd_clean($str);
301         return sprintf('%04u-%02u-%02u', $year, $month, $day);
302 }
303
304 # takes any of: HH  :MM  HH:MM
305 # returns decimal number of hours
306 #
307 # You probably want to use format_hours() instead because it handles hours with a decimal point.
308 function format_hours_minutes($str) {
309         if(strlen($str) == 0) {
310                 return $str;
311         }
312         $pos = strpos($str, ':');
313         if($pos === false) {
314                 $hours = format_int_0($str);
315                 $minutes = 0;
316         } elseif($pos == 0) {
317                 $hours = 0;
318                 $minutes = format_int_0($str);
319         } else {
320                 $hours = format_int_0(substr($str, 0, $pos));
321                 $minutes = format_int_0(substr($str, $pos + 1));
322         }
323         return $hours + ($minutes / 60.0);
324 }
325
326 # takes any of: HH  :MM  HH:MM  HH.hh(decimal hours)
327 # returns decimal number of hours
328 function format_hours($str) {
329         if(strlen($str) == 0) {
330                 return $str;
331         }
332         if(strpos($str, ':') !== false) {
333                 return format_hours_minutes($str);
334         } else {
335                 return format_decimal($str);
336         }
337 }
338
339 # takes eg 12:23am
340 # returns decimal number of hours since midnight
341 function format_12hr_to_hours($str) {
342         if(eregi('noon', $str)) {
343                 return 12;
344         }
345         $hours = format_hours($str);
346         if($hours < 12 && eregi('p', $str)) {
347                 $hours += 12;
348         }
349         return $hours;
350 }
351
352
353 function format_phone($str) {
354         $str = ereg_replace('[^0-9]', '', $str);
355         $str = ereg_replace('^1*', '', $str);
356         $len = strlen($str);
357         $output = '';
358
359         if($len < 10 && $len != 7) {
360                 #NOT A VALID PHONE NUMBER
361                 return $str;
362         }
363
364         if($len > 10) {
365                 $output = ' ext: ' . substr($str, 10);
366                 $len = 10;
367         }
368
369         if($len == 10) {
370                 $area = substr($str, 0, 3);
371                 $str = substr($str, 3);
372         }
373
374         $output = substr($str, 3) . $output;
375         $output = substr($str, 0, 3) . '-' . $output;
376
377         if($area) {
378                 $output = "($area) " . $output;
379         }
380
381         return $output;
382 }
383
384 ?>