forked from rcconvict/Baffi-Theme--Newznab-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasepage.php
More file actions
252 lines (217 loc) · 7.34 KB
/
basepage.php
File metadata and controls
252 lines (217 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
require_once(SMARTY_DIR.'Smarty.class.php');
require_once(WWW_DIR."/lib/users.php");
require_once(WWW_DIR."/lib/site.php");
require_once(WWW_DIR."/lib/sabnzbd.php");
class BasePage
{
public $title = '';
public $content = '';
public $head = '';
public $body = '';
public $meta_keywords = '';
public $meta_title = '';
public $meta_description = '';
public $page = '';
public $page_template = '';
public $smarty = '';
public $userdata = array();
public $serverurl = '';
public $templates = 'templates_baffi';
public $template_dir = 'frontend';
public $site = '';
public $secure_connection = false;
const FLOOD_THREE_REQUESTS_WITHIN_X_SECONDS = 1.000;
const FLOOD_PUNISHMENT_SECONDS = 3.0;
function BasePage()
{
@session_start();
if((function_exists("get_magic_quotes_gpc") && get_magic_quotes_gpc()) || ini_get('magic_quotes_sybase'))
{
foreach($_GET as $k => $v) $_GET[$k] = (is_array($v)) ? array_map("stripslashes", $v) : stripslashes($v);
foreach($_POST as $k => $v) $_POST[$k] = (is_array($v)) ? array_map("stripslashes", $v) : stripslashes($v);
foreach($_REQUEST as $k => $v) $_REQUEST[$k] = (is_array($v)) ? array_map("stripslashes", $v) : stripslashes($v);
foreach($_COOKIE as $k => $v) $_COOKIE[$k] = (is_array($v)) ? array_map("stripslashes", $v) : stripslashes($v);
}
// set site variable
$s = new Sites();
$this->site = $s->get();
$this->smarty = new Smarty();
$this->smarty->template_dir = WWW_DIR.'views'.DIRECTORY_SEPARATOR.$this->templates.DIRECTORY_SEPARATOR.$this->template_dir;
$this->smarty->compile_dir = SMARTY_DIR.'templates_c'.DIRECTORY_SEPARATOR;
$this->smarty->config_dir = SMARTY_DIR.'configs'.DIRECTORY_SEPARATOR;
$this->smarty->cache_dir = SMARTY_DIR.'cache'.DIRECTORY_SEPARATOR;
$this->smarty->error_reporting = (E_ALL - E_NOTICE);
$this->secure_connection = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443)) ;
$this->smarty->assign('site', $this->site);
$this->smarty->assign('page', $this);
if (isset($_SERVER["SERVER_NAME"]))
{
$this->serverurl = ($this->secure_connection ? "https://" : "http://").$_SERVER["SERVER_NAME"].(($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") ? ":".$_SERVER["SERVER_PORT"] : "").WWW_TOP.'/';
$this->smarty->assign('serverroot', $this->serverurl);
}
$this->page = (isset($_GET['page'])) ? $_GET['page'] : 'content';
$users = new Users();
if ($users->isLoggedIn())
{
$this->userdata = $users->getById($users->currentUserId());
//
// user can still be logged in but have been disabled by admin, so if they are, log them off
//
if ($this->userdata["role"] == Users::ROLE_DISABLED)
{
$users->logout();
$this->show403();
}
$this->userdata["categoryexclusions"] = $users->getCategoryExclusion($users->currentUserId());
//update lastlogin every 15 mins
if (strtotime($this->userdata['now'])-900 > strtotime($this->userdata['lastlogin']))
$users->updateSiteAccessed($this->userdata['ID']);
$this->smarty->assign('userdata',$this->userdata);
$this->smarty->assign('loggedin',"true");
$sab = new SABnzbd($this);
if ($sab->integrated !== false && $sab->url !='' && $sab->apikey != '')
{
$this->smarty->assign('sabintegrated', $sab->integrated);
$this->smarty->assign('sabapikeytype', $sab->apikeytype);
}
if ($this->userdata["role"] == Users::ROLE_ADMIN)
$this->smarty->assign('isadmin',"true");
$this->floodCheck(true, $this->userdata["role"]);
}
else
{
$this->smarty->assign('isadmin',"false");
$this->smarty->assign('loggedin',"false");
$this->floodCheck(false, "");
}
}
public function floodCheck($loggedin, $role)
{
//
// if flood wait set, the user must wait x seconds until they can access a page
//
if (empty($argc) &&
$role != Users::ROLE_ADMIN &&
isset($_SESSION['flood_wait_until']) &&
$_SESSION['flood_wait_until'] > microtime(true))
{
$this->showFloodWarning();
}
else
{
//
// if user not an admin, they are allowed three requests in FLOOD_THREE_REQUESTS_WITHIN_X_SECONDS seconds
//
if(empty($argc) && $role != Users::ROLE_ADMIN)
{
if (!isset($_SESSION['flood_check']))
{
$_SESSION['flood_check'] = "1_".microtime(true);
}
else
{
$hit = substr($_SESSION['flood_check'], 0, strpos($_SESSION['flood_check'], "_", 0));
if ($hit >= 3)
{
$onetime = substr($_SESSION['flood_check'], strpos($_SESSION['flood_check'], "_") + 1);
if ($onetime + BasePage::FLOOD_THREE_REQUESTS_WITHIN_X_SECONDS > microtime(true))
{
$_SESSION['flood_wait_until'] = microtime(true) + BasePage::FLOOD_PUNISHMENT_SECONDS;
unset($_SESSION['flood_check']);
$this->showFloodWarning();
}
else
{
$_SESSION['flood_check'] = "1_".microtime(true);
}
}
else
{
$hit++;
$_SESSION['flood_check'] = $hit.substr($_SESSION['flood_check'], strpos($_SESSION['flood_check'], "_", 0));
}
}
}
}
}
//
// Done in html here to reduce any smarty processing burden if a large flood is underway
//
public function showFloodWarning()
{
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Retry-After: '.BasePage::FLOOD_PUNISHMENT_SECONDS);
echo "
<html>
<head>
<title>Service Unavailable</title>
</head>
<body>
<h1>Service Unavailable</h1>
<p>Too many requests!</p>
<p>You must <b>wait ".BasePage::FLOOD_PUNISHMENT_SECONDS." seconds</b> before trying again.</p>
</body>
</html>";
die();
}
//
// Inject content into the html head
//
public function addToHead($headcontent)
{
$this->head = $this->head."\n".$headcontent;
}
//
// Inject js/attributes into the html body tag
//
public function addToBody($attr)
{
$this->body = $this->body." ".$attr;
}
public function render()
{
$this->smarty->display($this->page_template);
}
public function isPostBack()
{
return (strtoupper($_SERVER["REQUEST_METHOD"]) === "POST");
}
public function show404()
{
header("HTTP/1.1 404 Not Found");
die();
}
public function show403($from_admin = false)
{
$redirect_path = ($from_admin) ? str_replace('/admin', '', WWW_TOP) : WWW_TOP;
header("Location: $redirect_path/login?redirect=".urlencode($_SERVER["REQUEST_URI"]));
die();
}
public function show503($retry='')
{
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
if ($retry != '')
header('Retry-After: '.$retry);
echo "
<html>
<head>
<title>Service Unavailable</title>
</head>
<body>
<h1>Service Unavailable</h1>
<p>Your maximum api or download limit has been reached for the day</p>
</body>
</html>";
die();
}
//
// paths cannot be relative anymore so use full path
//
public function getCommonTemplate($tpl)
{
$len = strlen($this->smarty->template_dir[0]) - (strlen($this->template_dir) + 1);
return substr($this->smarty->template_dir[0], 0, $len)."common".DIRECTORY_SEPARATOR.$tpl;
}
}