]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/action.php
bring lib/noticelist.php into line with PEAR code standards
[quix0rs-gnu-social.git] / lib / action.php
1 <?php
2 /**
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) {
21     exit(1);
22 }
23
24 class Action // lawsuit
25 {
26
27     var $args;
28
29     function Action()
30     {
31     }
32
33     // For initializing members of the class
34
35     function prepare($argarray)
36     {
37         $this->args =& common_copy_args($argarray);
38         return true;
39     }
40
41     // For comparison with If-Last-Modified
42     // If not applicable, return null
43
44     function last_modified()
45     {
46         return null;
47     }
48
49     function etag()
50     {
51         return null;
52     }
53
54     function is_readonly()
55     {
56         return false;
57     }
58
59     function arg($key, $def=null)
60     {
61         if (array_key_exists($key, $this->args)) {
62             return $this->args[$key];
63         } else {
64             return $def;
65         }
66     }
67
68     function trimmed($key, $def=null)
69     {
70         $arg = $this->arg($key, $def);
71         return (is_string($arg)) ? trim($arg) : $arg;
72     }
73
74     // Note: argarray ignored, since it's now passed in in prepare()
75
76     function handle($argarray=null)
77     {
78
79         $lm = $this->last_modified();
80         $etag = $this->etag();
81
82         if ($etag) {
83             header('ETag: ' . $etag);
84         }
85
86         if ($lm) {
87             header('Last-Modified: ' . date(DATE_RFC1123, $lm));
88             $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
89             if ($if_modified_since) {
90                 $ims = strtotime($if_modified_since);
91                 if ($lm <= $ims) {
92                     if (!$etag ||
93                         $this->_has_etag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
94                         header('HTTP/1.1 304 Not Modified');
95                         // Better way to do this?
96                         exit(0);
97                     }
98                 }
99             }
100         }
101     }
102
103     function _has_etag($etag, $if_none_match)
104     {
105         return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
106     }
107
108     function boolean($key, $def=false)
109     {
110         $arg = strtolower($this->trimmed($key));
111
112         if (is_null($arg)) {
113             return $def;
114         } else if (in_array($arg, array('true', 'yes', '1'))) {
115             return true;
116         } else if (in_array($arg, array('false', 'no', '0'))) {
117             return false;
118         } else {
119             return $def;
120         }
121     }
122
123     function server_error($msg, $code=500)
124     {
125         $action = $this->trimmed('action');
126         common_debug("Server error '$code' on '$action': $msg", __FILE__);
127         common_server_error($msg, $code);
128     }
129
130     function client_error($msg, $code=400)
131     {
132         $action = $this->trimmed('action');
133         common_debug("User error '$code' on '$action': $msg", __FILE__);
134         common_user_error($msg, $code);
135     }
136
137     function self_url()
138     {
139         $action = $this->trimmed('action');
140         $args = $this->args;
141         unset($args['action']);
142         foreach (array_keys($_COOKIE) as $cookie) {
143             unset($args[$cookie]);
144         }
145         return common_local_url($action, $args);
146     }
147
148     function nav_menu($menu)
149     {
150         $action = $this->trimmed('action');
151         common_element_start('ul', array('id' => 'nav_views'));
152         foreach ($menu as $menuaction => $menudesc) {
153             common_menu_item(common_local_url($menuaction,
154                                               isset($menudesc[2]) ? $menudesc[2] : null),
155                              $menudesc[0],
156                              $menudesc[1],
157                              $action == $menuaction);
158         }
159         common_element_end('ul');
160     }
161 }