JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
copyrighted dwt.php, metaform textboxes aren't puny by default, added kill_session()
[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 # This exists because file_get_contents() is not documented well. (It says that
22 # the second parameter is optional, but does not specify what happens when you
23 # do not pass anything.) And because it's nice to work in PHP4.2
24 function read_whole_file($name) {
25         $fd = fopen($name, 'r');
26         if($fd === false) {
27                 die("Failed to read file: '$name'");
28         }
29         $file_data = fread($fd, filesize($name));
30         fclose($fd);
31         return $file_data;
32 }
33
34 function unix_newlines($str) {
35         $str = str_replace("\r\n", "\n", $str);
36         return str_replace("\r", "\n", $str);
37 }
38
39 # return current year (all 4 digits)
40 function this_year() {
41         return strftime('%Y');
42 }
43
44 # return the number of the current month (1..12)
45 function this_month() {
46         return ereg_replace('^0', '', strftime('%m'));
47 }
48
49
50 # php4 is broken, in that you cannot set a default value for a parameter that
51 # is passed by reference. So, this is set up to use the following screwy
52 # syntax:
53 #
54 # function foo($bar = 0) {
55 #   if($bar !== 0) {
56 #     $bar = $bar->ref;
57 #   }
58 #       ...
59 # }
60 #
61 # foo();
62 # foo(ref($baz));
63
64 class stupid_reference {
65         var $ref;
66         function stupid_reference(&$ref) {
67                 $this->ref = &$ref;
68         }
69 }
70 function ref(&$foo) {
71         return new stupid_reference($foo);
72 }
73
74 ?>