JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
changed licence to lgpl, metaform includes wfpl in tarbal
[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 function read_whole_file($name) {
22         $fd = fopen($name, 'r');
23         if($fd === false) {
24                 die("Failed to read file: '$name'");
25         }
26         $file_data = fread($fd, filesize($name));
27         fclose($fd);
28         return $file_data;
29 }
30
31 function unix_newlines($str) {
32         $str = str_replace("\r\n", "\n", $str);
33         return str_replace("\r", "\n", $str);
34 }
35
36 # return current year (all 4 digits)
37 function this_year() {
38         return strftime('%Y');
39 }
40
41 # return the number of the current month (1..12)
42 function this_month() {
43         return ereg_replace('^0', '', strftime('%m'));
44 }
45
46
47 # php4 is broken, in that you cannot set a default value for a parameter that
48 # is passed by reference. So, this is set up to use the following screwy
49 # syntax:
50 #
51 # function foo($bar = 0) {
52 #   if($bar !== 0) {
53 #     $bar = $bar->ref;
54 #   }
55 #       ...
56 # }
57 #
58 # foo();
59 # foo(ref($baz));
60
61 class stupid_reference {
62         var $ref;
63         function stupid_reference(&$ref) {
64                 $this->ref = &$ref;
65         }
66 }
67 function ref(&$foo) {
68         return new stupid_reference($foo);
69 }
70
71 ?>