diff options
author | ArthurHoaro <arthur@hoa.ro> | 2016-05-18 21:48:24 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2016-06-11 09:30:56 +0200 |
commit | 684e662a58b02bde225e44d3677987b6fc3adf0b (patch) | |
tree | db0d4ca1d9b53341cc108b0e7671ffde0e9caee9 /index.php | |
parent | 59404d7909b21682ec0782778452a8a70e38b25e (diff) | |
download | Shaarli-684e662a58b02bde225e44d3677987b6fc3adf0b.tar.gz Shaarli-684e662a58b02bde225e44d3677987b6fc3adf0b.tar.zst Shaarli-684e662a58b02bde225e44d3677987b6fc3adf0b.zip |
Replace $GLOBALS configuration with the configuration manager in the whole code base
Diffstat (limited to 'index.php')
-rw-r--r-- | index.php | 443 |
1 files changed, 200 insertions, 243 deletions
@@ -22,114 +22,13 @@ if (date_default_timezone_get() == '') { | |||
22 | date_default_timezone_set('UTC'); | 22 | date_default_timezone_set('UTC'); |
23 | } | 23 | } |
24 | 24 | ||
25 | /* ----------------------------------------------------------------------------- | ||
26 | * Hardcoded parameters | ||
27 | * You should not touch any code below (or at your own risks!) | ||
28 | * (These parameters can be overwritten by editing the file /data/config.php) | ||
29 | * ----------------------------------------------------------------------------- | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * Shaarli directories & configuration files | ||
34 | */ | ||
35 | // Data subdirectory | ||
36 | $GLOBALS['config']['DATADIR'] = 'data'; | ||
37 | |||
38 | // Main configuration file | ||
39 | $GLOBALS['config']['CONFIG_FILE'] = $GLOBALS['config']['DATADIR'].'/config.php'; | ||
40 | |||
41 | // Link datastore | ||
42 | $GLOBALS['config']['DATASTORE'] = $GLOBALS['config']['DATADIR'].'/datastore.php'; | ||
43 | |||
44 | // Banned IPs | ||
45 | $GLOBALS['config']['IPBANS_FILENAME'] = $GLOBALS['config']['DATADIR'].'/ipbans.php'; | ||
46 | |||
47 | // Processed updates file. | ||
48 | $GLOBALS['config']['UPDATES_FILE'] = $GLOBALS['config']['DATADIR'].'/updates.txt'; | ||
49 | |||
50 | // Access log | ||
51 | $GLOBALS['config']['LOG_FILE'] = $GLOBALS['config']['DATADIR'].'/log.txt'; | ||
52 | |||
53 | // For updates check of Shaarli | ||
54 | $GLOBALS['config']['UPDATECHECK_FILENAME'] = $GLOBALS['config']['DATADIR'].'/lastupdatecheck.txt'; | ||
55 | |||
56 | // Set ENABLE_UPDATECHECK to disabled by default. | ||
57 | $GLOBALS['config']['ENABLE_UPDATECHECK'] = false; | ||
58 | |||
59 | // RainTPL cache directory (keep the trailing slash!) | ||
60 | $GLOBALS['config']['RAINTPL_TMP'] = 'tmp/'; | ||
61 | // Raintpl template directory (keep the trailing slash!) | ||
62 | $GLOBALS['config']['RAINTPL_TPL'] = 'tpl/'; | ||
63 | |||
64 | // Thumbnail cache directory | ||
65 | $GLOBALS['config']['CACHEDIR'] = 'cache'; | ||
66 | |||
67 | // Atom & RSS feed cache directory | ||
68 | $GLOBALS['config']['PAGECACHE'] = 'pagecache'; | ||
69 | |||
70 | /* | ||
71 | * Global configuration | ||
72 | */ | ||
73 | // Ban IP after this many failures | ||
74 | $GLOBALS['config']['BAN_AFTER'] = 4; | ||
75 | // Ban duration for IP address after login failures (in seconds) | ||
76 | $GLOBALS['config']['BAN_DURATION'] = 1800; | ||
77 | |||
78 | // Feed options | ||
79 | // Enable RSS permalinks by default. | ||
80 | // This corresponds to the default behavior of shaarli before this was added as an option. | ||
81 | $GLOBALS['config']['ENABLE_RSS_PERMALINKS'] = true; | ||
82 | // If true, an extra "ATOM feed" button will be displayed in the toolbar | ||
83 | $GLOBALS['config']['SHOW_ATOM'] = false; | ||
84 | |||
85 | // Link display options | ||
86 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'] = false; | ||
87 | $GLOBALS['config']['HIDE_TIMESTAMPS'] = false; | ||
88 | $GLOBALS['config']['LINKS_PER_PAGE'] = 20; | ||
89 | |||
90 | // Open Shaarli (true): anyone can add/edit/delete links without having to login | ||
91 | $GLOBALS['config']['OPEN_SHAARLI'] = false; | ||
92 | |||
93 | // Thumbnails | ||
94 | // Display thumbnails in links | ||
95 | $GLOBALS['config']['ENABLE_THUMBNAILS'] = true; | ||
96 | // Store thumbnails in a local cache | ||
97 | $GLOBALS['config']['ENABLE_LOCALCACHE'] = true; | ||
98 | |||
99 | // Update check frequency for Shaarli. 86400 seconds=24 hours | ||
100 | $GLOBALS['config']['UPDATECHECK_BRANCH'] = 'stable'; | ||
101 | $GLOBALS['config']['UPDATECHECK_INTERVAL'] = 86400; | ||
102 | |||
103 | $GLOBALS['config']['REDIRECTOR_URLENCODE'] = true; | ||
104 | |||
105 | /* | ||
106 | * Plugin configuration | ||
107 | * | ||
108 | * Warning: order matters! | ||
109 | * | ||
110 | * These settings may be be overriden in: | ||
111 | * - data/config.php | ||
112 | * - each plugin's configuration file | ||
113 | */ | ||
114 | //$GLOBALS['config']['ENABLED_PLUGINS'] = array( | ||
115 | // 'qrcode', 'archiveorg', 'readityourself', 'demo_plugin', 'playvideos', | ||
116 | // 'wallabag', 'markdown', 'addlink_toolbar', | ||
117 | //); | ||
118 | $GLOBALS['config']['ENABLED_PLUGINS'] = array('qrcode'); | ||
119 | |||
120 | // Initialize plugin parameters array. | ||
121 | $GLOBALS['plugins'] = array(); | ||
122 | |||
123 | // PubSubHubbub support. Put an empty string to disable, or put your hub url here to enable. | ||
124 | $GLOBALS['config']['PUBSUBHUB_URL'] = ''; | ||
125 | |||
126 | /* | 25 | /* |
127 | * PHP configuration | 26 | * PHP configuration |
128 | */ | 27 | */ |
129 | define('shaarli_version', '0.7.0'); | 28 | define('shaarli_version', '0.7.0'); |
130 | 29 | ||
131 | // http://server.com/x/shaarli --> /shaarli/ | 30 | // http://server.com/x/shaarli --> /shaarli/ |
132 | define('WEB_PATH', substr($_SERVER["REQUEST_URI"], 0, 1+strrpos($_SERVER["REQUEST_URI"], '/', 0))); | 31 | define('WEB_PATH', substr($_SERVER['REQUEST_URI'], 0, 1+strrpos($_SERVER['REQUEST_URI'], '/', 0))); |
133 | 32 | ||
134 | // High execution time in case of problematic imports/exports. | 33 | // High execution time in case of problematic imports/exports. |
135 | ini_set('max_input_time','60'); | 34 | ini_set('max_input_time','60'); |
@@ -144,12 +43,6 @@ error_reporting(E_ALL^E_WARNING); | |||
144 | // See all errors (for debugging only) | 43 | // See all errors (for debugging only) |
145 | //error_reporting(-1); | 44 | //error_reporting(-1); |
146 | 45 | ||
147 | /* | ||
148 | * User configuration | ||
149 | */ | ||
150 | if (is_file($GLOBALS['config']['CONFIG_FILE'])) { | ||
151 | require_once $GLOBALS['config']['CONFIG_FILE']; | ||
152 | } | ||
153 | 46 | ||
154 | // Shaarli library | 47 | // Shaarli library |
155 | require_once 'application/ApplicationUtils.php'; | 48 | require_once 'application/ApplicationUtils.php'; |
@@ -166,10 +59,12 @@ require_once 'application/PageBuilder.php'; | |||
166 | require_once 'application/TimeZone.php'; | 59 | require_once 'application/TimeZone.php'; |
167 | require_once 'application/Url.php'; | 60 | require_once 'application/Url.php'; |
168 | require_once 'application/Utils.php'; | 61 | require_once 'application/Utils.php'; |
169 | require_once 'application/Config.php'; | 62 | require_once 'application/config/ConfigManager.php'; |
63 | require_once 'application/config/ConfigPlugin.php'; | ||
170 | require_once 'application/PluginManager.php'; | 64 | require_once 'application/PluginManager.php'; |
171 | require_once 'application/Router.php'; | 65 | require_once 'application/Router.php'; |
172 | require_once 'application/Updater.php'; | 66 | require_once 'application/Updater.php'; |
67 | require_once 'inc/rain.tpl.class.php'; | ||
173 | 68 | ||
174 | // Ensure the PHP version is supported | 69 | // Ensure the PHP version is supported |
175 | try { | 70 | try { |
@@ -210,16 +105,16 @@ if (isset($_COOKIE['shaarli']) && !is_session_id_valid($_COOKIE['shaarli'])) { | |||
210 | $_COOKIE['shaarli'] = session_id(); | 105 | $_COOKIE['shaarli'] = session_id(); |
211 | } | 106 | } |
212 | 107 | ||
213 | include "inc/rain.tpl.class.php"; //include Rain TPL | 108 | $conf = ConfigManager::getInstance(); |
214 | raintpl::$tpl_dir = $GLOBALS['config']['RAINTPL_TPL']; // template directory | 109 | |
215 | raintpl::$cache_dir = $GLOBALS['config']['RAINTPL_TMP']; // cache directory | 110 | RainTPL::$tpl_dir = $conf->get('config.RAINTPL_TPL'); // template directory |
111 | RainTPL::$cache_dir = $conf->get('config.RAINTPL_TMP'); // cache directory | ||
216 | 112 | ||
217 | $pluginManager = PluginManager::getInstance(); | 113 | $pluginManager = PluginManager::getInstance(); |
218 | $pluginManager->load($GLOBALS['config']['ENABLED_PLUGINS']); | 114 | $pluginManager->load($conf->get('config.ENABLED_PLUGINS')); |
219 | 115 | ||
220 | ob_start(); // Output buffering for the page cache. | 116 | ob_start(); // Output buffering for the page cache. |
221 | 117 | ||
222 | |||
223 | // In case stupid admin has left magic_quotes enabled in php.ini: | 118 | // In case stupid admin has left magic_quotes enabled in php.ini: |
224 | if (get_magic_quotes_gpc()) | 119 | if (get_magic_quotes_gpc()) |
225 | { | 120 | { |
@@ -236,17 +131,25 @@ header("Cache-Control: post-check=0, pre-check=0", false); | |||
236 | header("Pragma: no-cache"); | 131 | header("Pragma: no-cache"); |
237 | 132 | ||
238 | // Handling of old config file which do not have the new parameters. | 133 | // Handling of old config file which do not have the new parameters. |
239 | if (empty($GLOBALS['title'])) $GLOBALS['title']='Shared links on '.escape(index_url($_SERVER)); | 134 | if (! $conf->exists('title')) { |
240 | if (empty($GLOBALS['timezone'])) $GLOBALS['timezone']=date_default_timezone_get(); | 135 | $conf->set('title', 'Shared links on '. escape(index_url($_SERVER))); |
241 | if (empty($GLOBALS['redirector'])) $GLOBALS['redirector']=''; | 136 | } |
242 | if (empty($GLOBALS['disablesessionprotection'])) $GLOBALS['disablesessionprotection']=false; | 137 | if (! $conf->exists('timezone')) { |
243 | if (empty($GLOBALS['privateLinkByDefault'])) $GLOBALS['privateLinkByDefault']=false; | 138 | $conf->set('timezone', date_default_timezone_get()); |
244 | if (empty($GLOBALS['titleLink'])) $GLOBALS['titleLink']='?'; | 139 | } |
245 | // I really need to rewrite Shaarli with a proper configuation manager. | 140 | if (! $conf->exists('disablesessionprotection')) { |
246 | 141 | $conf->set('disablesessionprotection', false); | |
247 | if (! is_file($GLOBALS['config']['CONFIG_FILE'])) { | 142 | } |
143 | if (! $conf->exists('privateLinkByDefault')) { | ||
144 | $conf->set('privateLinkByDefault', false); | ||
145 | } | ||
146 | if (! $conf->exists('titleLink')) { | ||
147 | $conf->set('titleLink', '?'); | ||
148 | } | ||
149 | |||
150 | if (! is_file($conf->getConfigFile())) { | ||
248 | // Ensure Shaarli has proper access to its resources | 151 | // Ensure Shaarli has proper access to its resources |
249 | $errors = ApplicationUtils::checkResourcePermissions($GLOBALS['config']); | 152 | $errors = ApplicationUtils::checkResourcePermissions(); |
250 | 153 | ||
251 | if ($errors != array()) { | 154 | if ($errors != array()) { |
252 | $message = '<p>Insufficient permissions:</p><ul>'; | 155 | $message = '<p>Insufficient permissions:</p><ul>'; |
@@ -265,12 +168,13 @@ if (! is_file($GLOBALS['config']['CONFIG_FILE'])) { | |||
265 | install(); | 168 | install(); |
266 | } | 169 | } |
267 | 170 | ||
268 | $GLOBALS['title'] = !empty($GLOBALS['title']) ? escape($GLOBALS['title']) : ''; | 171 | // FIXME! Update these value with Updater and escpae it during the install/config save. |
269 | $GLOBALS['titleLink'] = !empty($GLOBALS['titleLink']) ? escape($GLOBALS['titleLink']) : ''; | 172 | $conf->set('title', escape($conf->get('title'))); |
270 | $GLOBALS['redirector'] = !empty($GLOBALS['redirector']) ? escape($GLOBALS['redirector']) : ''; | 173 | $conf->set('titleLink', escape($conf->get('titleLink'))); |
174 | $conf->set('redirector', escape($conf->get('redirector'))); | ||
271 | 175 | ||
272 | // a token depending of deployment salt, user password, and the current ip | 176 | // a token depending of deployment salt, user password, and the current ip |
273 | define('STAY_SIGNED_IN_TOKEN', sha1($GLOBALS['hash'].$_SERVER["REMOTE_ADDR"].$GLOBALS['salt'])); | 177 | define('STAY_SIGNED_IN_TOKEN', sha1($conf->get('hash') . $_SERVER['REMOTE_ADDR'] . $conf->get('salt'))); |
274 | 178 | ||
275 | // Sniff browser language and set date format accordingly. | 179 | // Sniff browser language and set date format accordingly. |
276 | if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { | 180 | if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
@@ -283,12 +187,14 @@ header('Content-Type: text/html; charset=utf-8'); // We use UTF-8 for proper int | |||
283 | //================================================================================================== | 187 | //================================================================================================== |
284 | 188 | ||
285 | function setup_login_state() { | 189 | function setup_login_state() { |
286 | if ($GLOBALS['config']['OPEN_SHAARLI']) { | 190 | $conf = ConfigManager::getInstance(); |
191 | |||
192 | if ($conf->get('config.OPEN_SHAARLI')) { | ||
287 | return true; | 193 | return true; |
288 | } | 194 | } |
289 | $userIsLoggedIn = false; // By default, we do not consider the user as logged in; | 195 | $userIsLoggedIn = false; // By default, we do not consider the user as logged in; |
290 | $loginFailure = false; // If set to true, every attempt to authenticate the user will fail. This indicates that an important condition isn't met. | 196 | $loginFailure = false; // If set to true, every attempt to authenticate the user will fail. This indicates that an important condition isn't met. |
291 | if (!isset($GLOBALS['login'])) { | 197 | if (! $conf->exists('login')) { |
292 | $userIsLoggedIn = false; // Shaarli is not configured yet. | 198 | $userIsLoggedIn = false; // Shaarli is not configured yet. |
293 | $loginFailure = true; | 199 | $loginFailure = true; |
294 | } | 200 | } |
@@ -300,9 +206,9 @@ function setup_login_state() { | |||
300 | $userIsLoggedIn = true; | 206 | $userIsLoggedIn = true; |
301 | } | 207 | } |
302 | // If session does not exist on server side, or IP address has changed, or session has expired, logout. | 208 | // If session does not exist on server side, or IP address has changed, or session has expired, logout. |
303 | if (empty($_SESSION['uid']) || | 209 | if (empty($_SESSION['uid']) |
304 | ($GLOBALS['disablesessionprotection']==false && $_SESSION['ip']!=allIPs()) || | 210 | || ($conf->get('disablesessionprotection') == false && $_SESSION['ip'] != allIPs()) |
305 | time() >= $_SESSION['expires_on']) | 211 | || time() >= $_SESSION['expires_on']) |
306 | { | 212 | { |
307 | logout(); | 213 | logout(); |
308 | $userIsLoggedIn = false; | 214 | $userIsLoggedIn = false; |
@@ -325,17 +231,19 @@ $userIsLoggedIn = setup_login_state(); | |||
325 | // ------------------------------------------------------------------------------------------ | 231 | // ------------------------------------------------------------------------------------------ |
326 | // PubSubHubbub protocol support (if enabled) [UNTESTED] | 232 | // PubSubHubbub protocol support (if enabled) [UNTESTED] |
327 | // (Source: http://aldarone.fr/les-flux-rss-shaarli-et-pubsubhubbub/ ) | 233 | // (Source: http://aldarone.fr/les-flux-rss-shaarli-et-pubsubhubbub/ ) |
328 | if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) include './publisher.php'; | ||
329 | function pubsubhub() | 234 | function pubsubhub() |
330 | { | 235 | { |
331 | if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) | 236 | $conf = ConfigManager::getInstance(); |
237 | $pshUrl = $conf->get('config.PUBSUBHUB_URL'); | ||
238 | if (!empty($pshUrl)) | ||
332 | { | 239 | { |
333 | $p = new Publisher($GLOBALS['config']['PUBSUBHUB_URL']); | 240 | include_once './publisher.php'; |
334 | $topic_url = array ( | 241 | $p = new Publisher($pshUrl); |
335 | index_url($_SERVER).'?do=atom', | 242 | $topic_url = array ( |
336 | index_url($_SERVER).'?do=rss' | 243 | index_url($_SERVER).'?do=atom', |
337 | ); | 244 | index_url($_SERVER).'?do=rss' |
338 | $p->publish_update($topic_url); | 245 | ); |
246 | $p->publish_update($topic_url); | ||
339 | } | 247 | } |
340 | } | 248 | } |
341 | 249 | ||
@@ -345,7 +253,7 @@ function pubsubhub() | |||
345 | // Returns the IP address of the client (Used to prevent session cookie hijacking.) | 253 | // Returns the IP address of the client (Used to prevent session cookie hijacking.) |
346 | function allIPs() | 254 | function allIPs() |
347 | { | 255 | { |
348 | $ip = $_SERVER["REMOTE_ADDR"]; | 256 | $ip = $_SERVER['REMOTE_ADDR']; |
349 | // Then we use more HTTP headers to prevent session hijacking from users behind the same proxy. | 257 | // Then we use more HTTP headers to prevent session hijacking from users behind the same proxy. |
350 | if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip=$ip.'_'.$_SERVER['HTTP_X_FORWARDED_FOR']; } | 258 | if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip=$ip.'_'.$_SERVER['HTTP_X_FORWARDED_FOR']; } |
351 | if (isset($_SERVER['HTTP_CLIENT_IP'])) { $ip=$ip.'_'.$_SERVER['HTTP_CLIENT_IP']; } | 259 | if (isset($_SERVER['HTTP_CLIENT_IP'])) { $ip=$ip.'_'.$_SERVER['HTTP_CLIENT_IP']; } |
@@ -353,23 +261,25 @@ function allIPs() | |||
353 | } | 261 | } |
354 | 262 | ||
355 | function fillSessionInfo() { | 263 | function fillSessionInfo() { |
264 | $conf = ConfigManager::getInstance(); | ||
356 | $_SESSION['uid'] = sha1(uniqid('',true).'_'.mt_rand()); // Generate unique random number (different than phpsessionid) | 265 | $_SESSION['uid'] = sha1(uniqid('',true).'_'.mt_rand()); // Generate unique random number (different than phpsessionid) |
357 | $_SESSION['ip']=allIPs(); // We store IP address(es) of the client to make sure session is not hijacked. | 266 | $_SESSION['ip']=allIPs(); // We store IP address(es) of the client to make sure session is not hijacked. |
358 | $_SESSION['username']=$GLOBALS['login']; | 267 | $_SESSION['username']= $conf->get('login'); |
359 | $_SESSION['expires_on']=time()+INACTIVITY_TIMEOUT; // Set session expiration. | 268 | $_SESSION['expires_on']=time()+INACTIVITY_TIMEOUT; // Set session expiration. |
360 | } | 269 | } |
361 | 270 | ||
362 | // Check that user/password is correct. | 271 | // Check that user/password is correct. |
363 | function check_auth($login,$password) | 272 | function check_auth($login,$password) |
364 | { | 273 | { |
365 | $hash = sha1($password.$login.$GLOBALS['salt']); | 274 | $conf = ConfigManager::getInstance(); |
366 | if ($login==$GLOBALS['login'] && $hash==$GLOBALS['hash']) | 275 | $hash = sha1($password . $login . $conf->get('salt')); |
276 | if ($login == $conf->get('login') && $hash == $conf->get('hash')) | ||
367 | { // Login/password is correct. | 277 | { // Login/password is correct. |
368 | fillSessionInfo(); | 278 | fillSessionInfo(); |
369 | logm($GLOBALS['config']['LOG_FILE'], $_SERVER['REMOTE_ADDR'], 'Login successful'); | 279 | logm($conf->get('config.LOG_FILE'), $_SERVER['REMOTE_ADDR'], 'Login successful'); |
370 | return True; | 280 | return True; |
371 | } | 281 | } |
372 | logm($GLOBALS['config']['LOG_FILE'], $_SERVER['REMOTE_ADDR'], 'Login failed for user '.$login); | 282 | logm($conf->get('config.LOG_FILE'), $_SERVER['REMOTE_ADDR'], 'Login failed for user '.$login); |
373 | return False; | 283 | return False; |
374 | } | 284 | } |
375 | 285 | ||
@@ -395,44 +305,64 @@ function logout() { | |||
395 | // ------------------------------------------------------------------------------------------ | 305 | // ------------------------------------------------------------------------------------------ |
396 | // Brute force protection system | 306 | // Brute force protection system |
397 | // Several consecutive failed logins will ban the IP address for 30 minutes. | 307 | // Several consecutive failed logins will ban the IP address for 30 minutes. |
398 | if (!is_file($GLOBALS['config']['IPBANS_FILENAME'])) file_put_contents($GLOBALS['config']['IPBANS_FILENAME'], "<?php\n\$GLOBALS['IPBANS']=".var_export(array('FAILURES'=>array(),'BANS'=>array()),true).";\n?>"); | 308 | if (!is_file($conf->get('config.IPBANS_FILENAME', 'data/ipbans.php'))) { |
399 | include $GLOBALS['config']['IPBANS_FILENAME']; | 309 | // FIXME! globals |
310 | file_put_contents( | ||
311 | $conf->get('config.IPBANS_FILENAME', 'data/ipbans.php'), | ||
312 | "<?php\n\$GLOBALS['IPBANS']=".var_export(array('FAILURES'=>array(),'BANS'=>array()),true).";\n?>" | ||
313 | ); | ||
314 | } | ||
315 | include $conf->get('config.IPBANS_FILENAME', 'data/ipbans.php'); | ||
400 | // Signal a failed login. Will ban the IP if too many failures: | 316 | // Signal a failed login. Will ban the IP if too many failures: |
401 | function ban_loginFailed() | 317 | function ban_loginFailed() |
402 | { | 318 | { |
403 | $ip=$_SERVER["REMOTE_ADDR"]; $gb=$GLOBALS['IPBANS']; | 319 | $conf = ConfigManager::getInstance(); |
320 | $ip = $_SERVER['REMOTE_ADDR']; | ||
321 | $gb = $GLOBALS['IPBANS']; | ||
404 | if (!isset($gb['FAILURES'][$ip])) $gb['FAILURES'][$ip]=0; | 322 | if (!isset($gb['FAILURES'][$ip])) $gb['FAILURES'][$ip]=0; |
405 | $gb['FAILURES'][$ip]++; | 323 | $gb['FAILURES'][$ip]++; |
406 | if ($gb['FAILURES'][$ip]>($GLOBALS['config']['BAN_AFTER']-1)) | 324 | if ($gb['FAILURES'][$ip] > ($conf->get('config.BAN_AFTER') - 1)) |
407 | { | 325 | { |
408 | $gb['BANS'][$ip]=time()+$GLOBALS['config']['BAN_DURATION']; | 326 | $gb['BANS'][$ip] = time() + $conf->get('config.BAN_DURATION', 1800); |
409 | logm($GLOBALS['config']['LOG_FILE'], $_SERVER['REMOTE_ADDR'], 'IP address banned from login'); | 327 | logm($conf->get('config.LOG_FILE'), $_SERVER['REMOTE_ADDR'], 'IP address banned from login'); |
410 | } | 328 | } |
411 | $GLOBALS['IPBANS'] = $gb; | 329 | $GLOBALS['IPBANS'] = $gb; |
412 | file_put_contents($GLOBALS['config']['IPBANS_FILENAME'], "<?php\n\$GLOBALS['IPBANS']=".var_export($gb,true).";\n?>"); | 330 | file_put_contents( |
331 | $conf->get('config.IPBANS_FILENAME', 'data/ipbans.php'), | ||
332 | "<?php\n\$GLOBALS['IPBANS']=".var_export($gb,true).";\n?>" | ||
333 | ); | ||
413 | } | 334 | } |
414 | 335 | ||
415 | // Signals a successful login. Resets failed login counter. | 336 | // Signals a successful login. Resets failed login counter. |
416 | function ban_loginOk() | 337 | function ban_loginOk() |
417 | { | 338 | { |
418 | $ip=$_SERVER["REMOTE_ADDR"]; $gb=$GLOBALS['IPBANS']; | 339 | $conf = ConfigManager::getInstance(); |
340 | $ip = $_SERVER['REMOTE_ADDR']; | ||
341 | $gb = $GLOBALS['IPBANS']; | ||
419 | unset($gb['FAILURES'][$ip]); unset($gb['BANS'][$ip]); | 342 | unset($gb['FAILURES'][$ip]); unset($gb['BANS'][$ip]); |
420 | $GLOBALS['IPBANS'] = $gb; | 343 | $GLOBALS['IPBANS'] = $gb; |
421 | file_put_contents($GLOBALS['config']['IPBANS_FILENAME'], "<?php\n\$GLOBALS['IPBANS']=".var_export($gb,true).";\n?>"); | 344 | file_put_contents( |
345 | $conf->get('config.IPBANS_FILENAME', 'data/ipbans.php'), | ||
346 | "<?php\n\$GLOBALS['IPBANS']=".var_export($gb,true).";\n?>" | ||
347 | ); | ||
422 | } | 348 | } |
423 | 349 | ||
424 | // Checks if the user CAN login. If 'true', the user can try to login. | 350 | // Checks if the user CAN login. If 'true', the user can try to login. |
425 | function ban_canLogin() | 351 | function ban_canLogin() |
426 | { | 352 | { |
353 | $conf = ConfigManager::getInstance(); | ||
427 | $ip=$_SERVER["REMOTE_ADDR"]; $gb=$GLOBALS['IPBANS']; | 354 | $ip=$_SERVER["REMOTE_ADDR"]; $gb=$GLOBALS['IPBANS']; |
428 | if (isset($gb['BANS'][$ip])) | 355 | if (isset($gb['BANS'][$ip])) |
429 | { | 356 | { |
430 | // User is banned. Check if the ban has expired: | 357 | // User is banned. Check if the ban has expired: |
431 | if ($gb['BANS'][$ip]<=time()) | 358 | if ($gb['BANS'][$ip]<=time()) |
432 | { // Ban expired, user can try to login again. | 359 | { // Ban expired, user can try to login again. |
433 | logm($GLOBALS['config']['LOG_FILE'], $_SERVER['REMOTE_ADDR'], 'Ban lifted.'); | 360 | logm($conf->get('config.LOG_FILE'), $_SERVER['REMOTE_ADDR'], 'Ban lifted.'); |
434 | unset($gb['FAILURES'][$ip]); unset($gb['BANS'][$ip]); | 361 | unset($gb['FAILURES'][$ip]); unset($gb['BANS'][$ip]); |
435 | file_put_contents($GLOBALS['config']['IPBANS_FILENAME'], "<?php\n\$GLOBALS['IPBANS']=".var_export($gb,true).";\n?>"); | 362 | file_put_contents( |
363 | $conf->get('config.IPBANS_FILENAME', 'data/ipbans.php'), | ||
364 | "<?php\n\$GLOBALS['IPBANS']=".var_export($gb,true).";\n?>" | ||
365 | ); | ||
436 | return true; // Ban has expired, user can login. | 366 | return true; // Ban has expired, user can login. |
437 | } | 367 | } |
438 | return false; // User is banned. | 368 | return false; // User is banned. |
@@ -546,7 +476,8 @@ if (!isset($_SESSION['tokens'])) $_SESSION['tokens']=array(); // Token are atta | |||
546 | // Returns a token. | 476 | // Returns a token. |
547 | function getToken() | 477 | function getToken() |
548 | { | 478 | { |
549 | $rnd = sha1(uniqid('',true).'_'.mt_rand().$GLOBALS['salt']); // We generate a random string. | 479 | $conf = ConfigManager::getInstance(); |
480 | $rnd = sha1(uniqid('', true) .'_'. mt_rand() . $conf->get('salt')); // We generate a random string. | ||
550 | $_SESSION['tokens'][$rnd]=1; // Store it on the server side. | 481 | $_SESSION['tokens'][$rnd]=1; // Store it on the server side. |
551 | return $rnd; | 482 | return $rnd; |
552 | } | 483 | } |
@@ -568,10 +499,11 @@ function tokenOk($token) | |||
568 | // Gives the last 7 days (which have links). | 499 | // Gives the last 7 days (which have links). |
569 | // This RSS feed cannot be filtered. | 500 | // This RSS feed cannot be filtered. |
570 | function showDailyRSS() { | 501 | function showDailyRSS() { |
502 | $conf = ConfigManager::getInstance(); | ||
571 | // Cache system | 503 | // Cache system |
572 | $query = $_SERVER['QUERY_STRING']; | 504 | $query = $_SERVER['QUERY_STRING']; |
573 | $cache = new CachedPage( | 505 | $cache = new CachedPage( |
574 | $GLOBALS['config']['PAGECACHE'], | 506 | $conf->get('config.PAGE_CACHE'), |
575 | page_url($_SERVER), | 507 | page_url($_SERVER), |
576 | startsWith($query,'do=dailyrss') && !isLoggedIn() | 508 | startsWith($query,'do=dailyrss') && !isLoggedIn() |
577 | ); | 509 | ); |
@@ -584,11 +516,11 @@ function showDailyRSS() { | |||
584 | // If cached was not found (or not usable), then read the database and build the response: | 516 | // If cached was not found (or not usable), then read the database and build the response: |
585 | // Read links from database (and filter private links if used it not logged in). | 517 | // Read links from database (and filter private links if used it not logged in). |
586 | $LINKSDB = new LinkDB( | 518 | $LINKSDB = new LinkDB( |
587 | $GLOBALS['config']['DATASTORE'], | 519 | $conf->get('config.DATASTORE'), |
588 | isLoggedIn(), | 520 | isLoggedIn(), |
589 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'], | 521 | $conf->get('config.HIDE_PUBLIC_LINKS'), |
590 | $GLOBALS['redirector'], | 522 | $conf->get('redirector'), |
591 | $GLOBALS['config']['REDIRECTOR_URLENCODE'] | 523 | $conf->get('config.REDIRECTOR_URLENCODE') |
592 | ); | 524 | ); |
593 | 525 | ||
594 | /* Some Shaarlies may have very few links, so we need to look | 526 | /* Some Shaarlies may have very few links, so we need to look |
@@ -600,7 +532,7 @@ function showDailyRSS() { | |||
600 | } | 532 | } |
601 | rsort($linkdates); | 533 | rsort($linkdates); |
602 | $nb_of_days = 7; // We take 7 days. | 534 | $nb_of_days = 7; // We take 7 days. |
603 | $today = Date('Ymd'); | 535 | $today = date('Ymd'); |
604 | $days = array(); | 536 | $days = array(); |
605 | 537 | ||
606 | foreach ($linkdates as $linkdate) { | 538 | foreach ($linkdates as $linkdate) { |
@@ -622,7 +554,7 @@ function showDailyRSS() { | |||
622 | $pageaddr = escape(index_url($_SERVER)); | 554 | $pageaddr = escape(index_url($_SERVER)); |
623 | echo '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0">'; | 555 | echo '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0">'; |
624 | echo '<channel>'; | 556 | echo '<channel>'; |
625 | echo '<title>Daily - '. $GLOBALS['title'] . '</title>'; | 557 | echo '<title>Daily - '. $conf->get('title') . '</title>'; |
626 | echo '<link>'. $pageaddr .'</link>'; | 558 | echo '<link>'. $pageaddr .'</link>'; |
627 | echo '<description>Daily shared links</description>'; | 559 | echo '<description>Daily shared links</description>'; |
628 | echo '<language>en-en</language>'; | 560 | echo '<language>en-en</language>'; |
@@ -641,7 +573,7 @@ function showDailyRSS() { | |||
641 | // We pre-format some fields for proper output. | 573 | // We pre-format some fields for proper output. |
642 | foreach ($linkdates as $linkdate) { | 574 | foreach ($linkdates as $linkdate) { |
643 | $l = $LINKSDB[$linkdate]; | 575 | $l = $LINKSDB[$linkdate]; |
644 | $l['formatedDescription'] = format_description($l['description'], $GLOBALS['redirector']); | 576 | $l['formatedDescription'] = format_description($l['description'], $conf->get('redirector')); |
645 | $l['thumbnail'] = thumbnail($l['url']); | 577 | $l['thumbnail'] = thumbnail($l['url']); |
646 | $l_date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $l['linkdate']); | 578 | $l_date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $l['linkdate']); |
647 | $l['timestamp'] = $l_date->getTimestamp(); | 579 | $l['timestamp'] = $l_date->getTimestamp(); |
@@ -653,7 +585,7 @@ function showDailyRSS() { | |||
653 | 585 | ||
654 | // Then build the HTML for this day: | 586 | // Then build the HTML for this day: |
655 | $tpl = new RainTPL; | 587 | $tpl = new RainTPL; |
656 | $tpl->assign('title', $GLOBALS['title']); | 588 | $tpl->assign('title', $conf->get('title')); |
657 | $tpl->assign('daydate', $dayDate->getTimestamp()); | 589 | $tpl->assign('daydate', $dayDate->getTimestamp()); |
658 | $tpl->assign('absurl', $absurl); | 590 | $tpl->assign('absurl', $absurl); |
659 | $tpl->assign('links', $links); | 591 | $tpl->assign('links', $links); |
@@ -677,7 +609,8 @@ function showDailyRSS() { | |||
677 | */ | 609 | */ |
678 | function showDaily($pageBuilder, $LINKSDB) | 610 | function showDaily($pageBuilder, $LINKSDB) |
679 | { | 611 | { |
680 | $day=Date('Ymd',strtotime('-1 day')); // Yesterday, in format YYYYMMDD. | 612 | $conf = ConfigManager::getInstance(); |
613 | $day=date('Ymd',strtotime('-1 day')); // Yesterday, in format YYYYMMDD. | ||
681 | if (isset($_GET['day'])) $day=$_GET['day']; | 614 | if (isset($_GET['day'])) $day=$_GET['day']; |
682 | 615 | ||
683 | $days = $LINKSDB->days(); | 616 | $days = $LINKSDB->days(); |
@@ -705,7 +638,7 @@ function showDaily($pageBuilder, $LINKSDB) | |||
705 | $taglist = explode(' ',$link['tags']); | 638 | $taglist = explode(' ',$link['tags']); |
706 | uasort($taglist, 'strcasecmp'); | 639 | uasort($taglist, 'strcasecmp'); |
707 | $linksToDisplay[$key]['taglist']=$taglist; | 640 | $linksToDisplay[$key]['taglist']=$taglist; |
708 | $linksToDisplay[$key]['formatedDescription'] = format_description($link['description'], $GLOBALS['redirector']); | 641 | $linksToDisplay[$key]['formatedDescription'] = format_description($link['description'], $conf->get('redirector')); |
709 | $linksToDisplay[$key]['thumbnail'] = thumbnail($link['url']); | 642 | $linksToDisplay[$key]['thumbnail'] = thumbnail($link['url']); |
710 | $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); | 643 | $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); |
711 | $linksToDisplay[$key]['timestamp'] = $date->getTimestamp(); | 644 | $linksToDisplay[$key]['timestamp'] = $date->getTimestamp(); |
@@ -763,17 +696,17 @@ function showLinkList($PAGE, $LINKSDB) { | |||
763 | // Render HTML page (according to URL parameters and user rights) | 696 | // Render HTML page (according to URL parameters and user rights) |
764 | function renderPage() | 697 | function renderPage() |
765 | { | 698 | { |
699 | $conf = ConfigManager::getInstance(); | ||
766 | $LINKSDB = new LinkDB( | 700 | $LINKSDB = new LinkDB( |
767 | $GLOBALS['config']['DATASTORE'], | 701 | $conf->get('config.DATASTORE'), |
768 | isLoggedIn(), | 702 | isLoggedIn(), |
769 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'], | 703 | $conf->get('config.HIDE_PUBLIC_LINKS'), |
770 | $GLOBALS['redirector'], | 704 | $conf->get('redirector'), |
771 | $GLOBALS['config']['REDIRECTOR_URLENCODE'] | 705 | $conf->get('config.REDIRECTOR_URLENCODE') |
772 | ); | 706 | ); |
773 | 707 | ||
774 | $updater = new Updater( | 708 | $updater = new Updater( |
775 | read_updates_file($GLOBALS['config']['UPDATES_FILE']), | 709 | read_updates_file($conf->get('config.UPDATES_FILE')), |
776 | $GLOBALS, | ||
777 | $LINKSDB, | 710 | $LINKSDB, |
778 | isLoggedIn() | 711 | isLoggedIn() |
779 | ); | 712 | ); |
@@ -781,7 +714,7 @@ function renderPage() | |||
781 | $newUpdates = $updater->update(); | 714 | $newUpdates = $updater->update(); |
782 | if (! empty($newUpdates)) { | 715 | if (! empty($newUpdates)) { |
783 | write_updates_file( | 716 | write_updates_file( |
784 | $GLOBALS['config']['UPDATES_FILE'], | 717 | $conf->get('config.UPDATES_FILE'), |
785 | $updater->getDoneUpdates() | 718 | $updater->getDoneUpdates() |
786 | ); | 719 | ); |
787 | } | 720 | } |
@@ -820,7 +753,7 @@ function renderPage() | |||
820 | // -------- Display login form. | 753 | // -------- Display login form. |
821 | if ($targetPage == Router::$PAGE_LOGIN) | 754 | if ($targetPage == Router::$PAGE_LOGIN) |
822 | { | 755 | { |
823 | if ($GLOBALS['config']['OPEN_SHAARLI']) { header('Location: ?'); exit; } // No need to login for open Shaarli | 756 | if ($conf->get('config.OPEN_SHAARLI')) { header('Location: ?'); exit; } // No need to login for open Shaarli |
824 | $token=''; if (ban_canLogin()) $token=getToken(); // Do not waste token generation if not useful. | 757 | $token=''; if (ban_canLogin()) $token=getToken(); // Do not waste token generation if not useful. |
825 | $PAGE->assign('token',$token); | 758 | $PAGE->assign('token',$token); |
826 | if (isset($_GET['username'])) { | 759 | if (isset($_GET['username'])) { |
@@ -833,7 +766,7 @@ function renderPage() | |||
833 | // -------- User wants to logout. | 766 | // -------- User wants to logout. |
834 | if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=logout')) | 767 | if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=logout')) |
835 | { | 768 | { |
836 | invalidateCaches($GLOBALS['config']['PAGECACHE']); | 769 | invalidateCaches($conf->get('config.PAGECACHE')); |
837 | logout(); | 770 | logout(); |
838 | header('Location: ?'); | 771 | header('Location: ?'); |
839 | exit; | 772 | exit; |
@@ -933,7 +866,7 @@ function renderPage() | |||
933 | // Cache system | 866 | // Cache system |
934 | $query = $_SERVER['QUERY_STRING']; | 867 | $query = $_SERVER['QUERY_STRING']; |
935 | $cache = new CachedPage( | 868 | $cache = new CachedPage( |
936 | $GLOBALS['config']['PAGECACHE'], | 869 | $conf->get('config.PAGECACHE'), |
937 | page_url($_SERVER), | 870 | page_url($_SERVER), |
938 | startsWith($query,'do='. $targetPage) && !isLoggedIn() | 871 | startsWith($query,'do='. $targetPage) && !isLoggedIn() |
939 | ); | 872 | ); |
@@ -946,10 +879,11 @@ function renderPage() | |||
946 | // Generate data. | 879 | // Generate data. |
947 | $feedGenerator = new FeedBuilder($LINKSDB, $feedType, $_SERVER, $_GET, isLoggedIn()); | 880 | $feedGenerator = new FeedBuilder($LINKSDB, $feedType, $_SERVER, $_GET, isLoggedIn()); |
948 | $feedGenerator->setLocale(strtolower(setlocale(LC_COLLATE, 0))); | 881 | $feedGenerator->setLocale(strtolower(setlocale(LC_COLLATE, 0))); |
949 | $feedGenerator->setHideDates($GLOBALS['config']['HIDE_TIMESTAMPS'] && !isLoggedIn()); | 882 | $feedGenerator->setHideDates($conf->get('config.HIDE_TIMESTAMPS') && !isLoggedIn()); |
950 | $feedGenerator->setUsePermalinks(isset($_GET['permalinks']) || !$GLOBALS['config']['ENABLE_RSS_PERMALINKS']); | 883 | $feedGenerator->setUsePermalinks(isset($_GET['permalinks']) || !$conf->get('config.ENABLE_RSS_PERMALINKS')); |
951 | if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) { | 884 | $pshUrl = $conf->get('config.PUBSUBHUB_URL'); |
952 | $feedGenerator->setPubsubhubUrl($GLOBALS['config']['PUBSUBHUB_URL']); | 885 | if (!empty($pshUrl)) { |
886 | $feedGenerator->setPubsubhubUrl($pshUrl); | ||
953 | } | 887 | } |
954 | $data = $feedGenerator->buildData(); | 888 | $data = $feedGenerator->buildData(); |
955 | 889 | ||
@@ -1110,19 +1044,23 @@ function renderPage() | |||
1110 | // -------- User wants to change his/her password. | 1044 | // -------- User wants to change his/her password. |
1111 | if ($targetPage == Router::$PAGE_CHANGEPASSWORD) | 1045 | if ($targetPage == Router::$PAGE_CHANGEPASSWORD) |
1112 | { | 1046 | { |
1113 | if ($GLOBALS['config']['OPEN_SHAARLI']) die('You are not supposed to change a password on an Open Shaarli.'); | 1047 | if ($conf->get('config.OPEN_SHAARLI')) { |
1048 | die('You are not supposed to change a password on an Open Shaarli.'); | ||
1049 | } | ||
1050 | |||
1114 | if (!empty($_POST['setpassword']) && !empty($_POST['oldpassword'])) | 1051 | if (!empty($_POST['setpassword']) && !empty($_POST['oldpassword'])) |
1115 | { | 1052 | { |
1116 | if (!tokenOk($_POST['token'])) die('Wrong token.'); // Go away! | 1053 | if (!tokenOk($_POST['token'])) die('Wrong token.'); // Go away! |
1117 | 1054 | ||
1118 | // Make sure old password is correct. | 1055 | // Make sure old password is correct. |
1119 | $oldhash = sha1($_POST['oldpassword'].$GLOBALS['login'].$GLOBALS['salt']); | 1056 | $oldhash = sha1($_POST['oldpassword'].$conf->get('login').$conf->get('salt')); |
1120 | if ($oldhash!=$GLOBALS['hash']) { echo '<script>alert("The old password is not correct.");document.location=\'?do=changepasswd\';</script>'; exit; } | 1057 | if ($oldhash!= $conf->get('hash')) { echo '<script>alert("The old password is not correct.");document.location=\'?do=changepasswd\';</script>'; exit; } |
1121 | // Save new password | 1058 | // Save new password |
1122 | $GLOBALS['salt'] = sha1(uniqid('',true).'_'.mt_rand()); // Salt renders rainbow-tables attacks useless. | 1059 | // Salt renders rainbow-tables attacks useless. |
1123 | $GLOBALS['hash'] = sha1($_POST['setpassword'].$GLOBALS['login'].$GLOBALS['salt']); | 1060 | $conf->set('salt', sha1(uniqid('', true) .'_'. mt_rand())); |
1061 | $conf->set('hash', sha1($_POST['setpassword'] . $conf->get('login') . $conf->get('salt'))); | ||
1124 | try { | 1062 | try { |
1125 | writeConfig($GLOBALS, isLoggedIn()); | 1063 | $conf->write(isLoggedIn()); |
1126 | } | 1064 | } |
1127 | catch(Exception $e) { | 1065 | catch(Exception $e) { |
1128 | error_log( | 1066 | error_log( |
@@ -1159,17 +1097,17 @@ function renderPage() | |||
1159 | ) { | 1097 | ) { |
1160 | $tz = $_POST['continent'] . '/' . $_POST['city']; | 1098 | $tz = $_POST['continent'] . '/' . $_POST['city']; |
1161 | } | 1099 | } |
1162 | $GLOBALS['timezone'] = $tz; | 1100 | $conf->set('timezone', $tz); |
1163 | $GLOBALS['title']=$_POST['title']; | 1101 | $conf->set('title', $_POST['title']); |
1164 | $GLOBALS['titleLink']=$_POST['titleLink']; | 1102 | $conf->set('titleLink', $_POST['titleLink']); |
1165 | $GLOBALS['redirector']=$_POST['redirector']; | 1103 | $conf->set('redirector', $_POST['redirector']); |
1166 | $GLOBALS['disablesessionprotection']=!empty($_POST['disablesessionprotection']); | 1104 | $conf->set('disablesessionprotection', !empty($_POST['disablesessionprotection'])); |
1167 | $GLOBALS['privateLinkByDefault']=!empty($_POST['privateLinkByDefault']); | 1105 | $conf->set('privateLinkByDefault', !empty($_POST['privateLinkByDefault'])); |
1168 | $GLOBALS['config']['ENABLE_RSS_PERMALINKS']= !empty($_POST['enableRssPermalinks']); | 1106 | $conf->set('config.ENABLE_RSS_PERMALINKS', !empty($_POST['enableRssPermalinks'])); |
1169 | $GLOBALS['config']['ENABLE_UPDATECHECK'] = !empty($_POST['updateCheck']); | 1107 | $conf->set('config.ENABLE_UPDATECHECK', !empty($_POST['updateCheck'])); |
1170 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'] = !empty($_POST['hidePublicLinks']); | 1108 | $conf->set('config.HIDE_PUBLIC_LINKS', !empty($_POST['hidePublicLinks'])); |
1171 | try { | 1109 | try { |
1172 | writeConfig($GLOBALS, isLoggedIn()); | 1110 | $conf->write(isLoggedIn()); |
1173 | } | 1111 | } |
1174 | catch(Exception $e) { | 1112 | catch(Exception $e) { |
1175 | error_log( | 1113 | error_log( |
@@ -1178,20 +1116,24 @@ function renderPage() | |||
1178 | ); | 1116 | ); |
1179 | 1117 | ||
1180 | // TODO: do not handle exceptions/errors in JS. | 1118 | // TODO: do not handle exceptions/errors in JS. |
1181 | echo '<script>alert("'. $e->getMessage() .'");document.location=\'?do=tools\';</script>'; | 1119 | echo '<script>alert("'. $e->getMessage() .'");document.location=\'?do=configure\';</script>'; |
1182 | exit; | 1120 | exit; |
1183 | } | 1121 | } |
1184 | echo '<script>alert("Configuration was saved.");document.location=\'?do=tools\';</script>'; | 1122 | echo '<script>alert("Configuration was saved.");document.location=\'?do=configure\';</script>'; |
1185 | exit; | 1123 | exit; |
1186 | } | 1124 | } |
1187 | else // Show the configuration form. | 1125 | else // Show the configuration form. |
1188 | { | 1126 | { |
1189 | $PAGE->assign('token',getToken()); | 1127 | $PAGE->assign('token',getToken()); |
1190 | $PAGE->assign('title', empty($GLOBALS['title']) ? '' : $GLOBALS['title'] ); | 1128 | $PAGE->assign('title', $conf->get('title')); |
1191 | $PAGE->assign('redirector', empty($GLOBALS['redirector']) ? '' : $GLOBALS['redirector'] ); | 1129 | $PAGE->assign('redirector', $conf->get('redirector')); |
1192 | list($timezone_form, $timezone_js) = generateTimeZoneForm($GLOBALS['timezone']); | 1130 | list($timezone_form, $timezone_js) = generateTimeZoneForm($conf->get('timezone')); |
1193 | $PAGE->assign('timezone_form', $timezone_form); | 1131 | $PAGE->assign('timezone_form', $timezone_form); |
1194 | $PAGE->assign('timezone_js',$timezone_js); | 1132 | $PAGE->assign('timezone_js',$timezone_js); |
1133 | $PAGE->assign('private_links_default', $conf->get('privateLinkByDefault')); | ||
1134 | $PAGE->assign('enable_rss_permalinks', $conf->get('config.ENABLE_RSS_PERMALINKS')); | ||
1135 | $PAGE->assign('enable_update_check', $conf->get('config.ENABLE_UPDATECHECK')); | ||
1136 | $PAGE->assign('hide_public_links', $conf->get('config.HIDE_PUBLIC_LINKS')); | ||
1195 | $PAGE->renderPage('configure'); | 1137 | $PAGE->renderPage('configure'); |
1196 | exit; | 1138 | exit; |
1197 | } | 1139 | } |
@@ -1223,7 +1165,7 @@ function renderPage() | |||
1223 | $value['tags']=trim(implode(' ',$tags)); | 1165 | $value['tags']=trim(implode(' ',$tags)); |
1224 | $LINKSDB[$key]=$value; | 1166 | $LINKSDB[$key]=$value; |
1225 | } | 1167 | } |
1226 | $LINKSDB->savedb($GLOBALS['config']['PAGECACHE']); | 1168 | $LINKSDB->savedb($conf->get('config.PAGECACHE')); |
1227 | echo '<script>alert("Tag was removed from '.count($linksToAlter).' links.");document.location=\'?\';</script>'; | 1169 | echo '<script>alert("Tag was removed from '.count($linksToAlter).' links.");document.location=\'?\';</script>'; |
1228 | exit; | 1170 | exit; |
1229 | } | 1171 | } |
@@ -1240,7 +1182,7 @@ function renderPage() | |||
1240 | $value['tags']=trim(implode(' ',$tags)); | 1182 | $value['tags']=trim(implode(' ',$tags)); |
1241 | $LINKSDB[$key]=$value; | 1183 | $LINKSDB[$key]=$value; |
1242 | } | 1184 | } |
1243 | $LINKSDB->savedb($GLOBALS['config']['PAGECACHE']); // Save to disk. | 1185 | $LINKSDB->savedb($conf->get('config.PAGECACHE')); // Save to disk. |
1244 | echo '<script>alert("Tag was renamed in '.count($linksToAlter).' links.");document.location=\'?searchtags='.urlencode($_POST['totag']).'\';</script>'; | 1186 | echo '<script>alert("Tag was renamed in '.count($linksToAlter).' links.");document.location=\'?searchtags='.urlencode($_POST['totag']).'\';</script>'; |
1245 | exit; | 1187 | exit; |
1246 | } | 1188 | } |
@@ -1291,7 +1233,7 @@ function renderPage() | |||
1291 | $pluginManager->executeHooks('save_link', $link); | 1233 | $pluginManager->executeHooks('save_link', $link); |
1292 | 1234 | ||
1293 | $LINKSDB[$linkdate] = $link; | 1235 | $LINKSDB[$linkdate] = $link; |
1294 | $LINKSDB->savedb($GLOBALS['config']['PAGECACHE']); | 1236 | $LINKSDB->savedb($conf->get('config.PAGECACHE')); |
1295 | pubsubhub(); | 1237 | pubsubhub(); |
1296 | 1238 | ||
1297 | // If we are called from the bookmarklet, we must close the popup: | 1239 | // If we are called from the bookmarklet, we must close the popup: |
@@ -1333,7 +1275,7 @@ function renderPage() | |||
1333 | $pluginManager->executeHooks('delete_link', $LINKSDB[$linkdate]); | 1275 | $pluginManager->executeHooks('delete_link', $LINKSDB[$linkdate]); |
1334 | 1276 | ||
1335 | unset($LINKSDB[$linkdate]); | 1277 | unset($LINKSDB[$linkdate]); |
1336 | $LINKSDB->savedb($GLOBALS['config']['PAGECACHE']); // save to disk | 1278 | $LINKSDB->savedb('config.PAGECACHE'); // save to disk |
1337 | 1279 | ||
1338 | // If we are called from the bookmarklet, we must close the popup: | 1280 | // If we are called from the bookmarklet, we must close the popup: |
1339 | if (isset($_GET['source']) && ($_GET['source']=='bookmarklet' || $_GET['source']=='firefoxsocialapi')) { echo '<script>self.close();</script>'; exit; } | 1281 | if (isset($_GET['source']) && ($_GET['source']=='bookmarklet' || $_GET['source']=='firefoxsocialapi')) { echo '<script>self.close();</script>'; exit; } |
@@ -1533,7 +1475,7 @@ function renderPage() | |||
1533 | // Split plugins into 2 arrays: ordered enabled plugins and disabled. | 1475 | // Split plugins into 2 arrays: ordered enabled plugins and disabled. |
1534 | $enabledPlugins = array_filter($pluginMeta, function($v) { return $v['order'] !== false; }); | 1476 | $enabledPlugins = array_filter($pluginMeta, function($v) { return $v['order'] !== false; }); |
1535 | // Load parameters. | 1477 | // Load parameters. |
1536 | $enabledPlugins = load_plugin_parameter_values($enabledPlugins, $GLOBALS['plugins']); | 1478 | $enabledPlugins = load_plugin_parameter_values($enabledPlugins, $conf->get('plugins', array())); |
1537 | uasort( | 1479 | uasort( |
1538 | $enabledPlugins, | 1480 | $enabledPlugins, |
1539 | function($a, $b) { return $a['order'] - $b['order']; } | 1481 | function($a, $b) { return $a['order'] - $b['order']; } |
@@ -1552,13 +1494,13 @@ function renderPage() | |||
1552 | if (isset($_POST['parameters_form'])) { | 1494 | if (isset($_POST['parameters_form'])) { |
1553 | unset($_POST['parameters_form']); | 1495 | unset($_POST['parameters_form']); |
1554 | foreach ($_POST as $param => $value) { | 1496 | foreach ($_POST as $param => $value) { |
1555 | $GLOBALS['plugins'][$param] = escape($value); | 1497 | $conf->set('plugins.'. $param, escape($value)); |
1556 | } | 1498 | } |
1557 | } | 1499 | } |
1558 | else { | 1500 | else { |
1559 | $GLOBALS['config']['ENABLED_PLUGINS'] = save_plugin_config($_POST); | 1501 | $conf->set('config.ENABLED_PLUGINS', save_plugin_config($_POST)); |
1560 | } | 1502 | } |
1561 | writeConfig($GLOBALS, isLoggedIn()); | 1503 | $conf->write(isLoggedIn()); |
1562 | } | 1504 | } |
1563 | catch (Exception $e) { | 1505 | catch (Exception $e) { |
1564 | error_log( | 1506 | error_log( |
@@ -1584,6 +1526,7 @@ function renderPage() | |||
1584 | function importFile($LINKSDB) | 1526 | function importFile($LINKSDB) |
1585 | { | 1527 | { |
1586 | if (!isLoggedIn()) { die('Not allowed.'); } | 1528 | if (!isLoggedIn()) { die('Not allowed.'); } |
1529 | $conf = ConfigManager::getInstance(); | ||
1587 | 1530 | ||
1588 | $filename=$_FILES['filetoupload']['name']; | 1531 | $filename=$_FILES['filetoupload']['name']; |
1589 | $filesize=$_FILES['filetoupload']['size']; | 1532 | $filesize=$_FILES['filetoupload']['size']; |
@@ -1654,7 +1597,7 @@ function importFile($LINKSDB) | |||
1654 | } | 1597 | } |
1655 | } | 1598 | } |
1656 | } | 1599 | } |
1657 | $LINKSDB->savedb($GLOBALS['config']['PAGECACHE']); | 1600 | $LINKSDB->savedb($conf->get('config.PAGECACHE')); |
1658 | 1601 | ||
1659 | echo '<script>alert("File '.json_encode($filename).' ('.$filesize.' bytes) was successfully processed: '.$import_count.' links imported.");document.location=\'?\';</script>'; | 1602 | echo '<script>alert("File '.json_encode($filename).' ('.$filesize.' bytes) was successfully processed: '.$import_count.' links imported.");document.location=\'?\';</script>'; |
1660 | } | 1603 | } |
@@ -1673,6 +1616,7 @@ function importFile($LINKSDB) | |||
1673 | */ | 1616 | */ |
1674 | function buildLinkList($PAGE,$LINKSDB) | 1617 | function buildLinkList($PAGE,$LINKSDB) |
1675 | { | 1618 | { |
1619 | $conf = ConfigManager::getInstance(); | ||
1676 | // Used in templates | 1620 | // Used in templates |
1677 | $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : ''; | 1621 | $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : ''; |
1678 | $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : ''; | 1622 | $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : ''; |
@@ -1700,7 +1644,7 @@ function buildLinkList($PAGE,$LINKSDB) | |||
1700 | 1644 | ||
1701 | // If there is only a single link, we change on-the-fly the title of the page. | 1645 | // If there is only a single link, we change on-the-fly the title of the page. |
1702 | if (count($linksToDisplay) == 1) { | 1646 | if (count($linksToDisplay) == 1) { |
1703 | $GLOBALS['pagetitle'] = $linksToDisplay[$keys[0]]['title'].' - '.$GLOBALS['title']; | 1647 | $conf->set('pagetitle', $linksToDisplay[$keys[0]]['title'] .' - '. $conf->get('title')); |
1704 | } | 1648 | } |
1705 | 1649 | ||
1706 | // Select articles according to paging. | 1650 | // Select articles according to paging. |
@@ -1716,7 +1660,7 @@ function buildLinkList($PAGE,$LINKSDB) | |||
1716 | while ($i<$end && $i<count($keys)) | 1660 | while ($i<$end && $i<count($keys)) |
1717 | { | 1661 | { |
1718 | $link = $linksToDisplay[$keys[$i]]; | 1662 | $link = $linksToDisplay[$keys[$i]]; |
1719 | $link['description'] = format_description($link['description'], $GLOBALS['redirector']); | 1663 | $link['description'] = format_description($link['description'], $conf->get('redirector')); |
1720 | $classLi = ($i % 2) != 0 ? '' : 'publicLinkHightLight'; | 1664 | $classLi = ($i % 2) != 0 ? '' : 'publicLinkHightLight'; |
1721 | $link['class'] = $link['private'] == 0 ? $classLi : 'private'; | 1665 | $link['class'] = $link['private'] == 0 ? $classLi : 'private'; |
1722 | $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); | 1666 | $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); |
@@ -1758,14 +1702,14 @@ function buildLinkList($PAGE,$LINKSDB) | |||
1758 | 'result_count' => count($linksToDisplay), | 1702 | 'result_count' => count($linksToDisplay), |
1759 | 'search_term' => $searchterm, | 1703 | 'search_term' => $searchterm, |
1760 | 'search_tags' => $searchtags, | 1704 | 'search_tags' => $searchtags, |
1761 | 'redirector' => empty($GLOBALS['redirector']) ? '' : $GLOBALS['redirector'], // Optional redirector URL. | 1705 | 'redirector' => $conf->get('redirector'), // Optional redirector URL. |
1762 | 'token' => $token, | 1706 | 'token' => $token, |
1763 | 'links' => $linkDisp, | 1707 | 'links' => $linkDisp, |
1764 | 'tags' => $LINKSDB->allTags(), | 1708 | 'tags' => $LINKSDB->allTags(), |
1765 | ); | 1709 | ); |
1766 | // FIXME! temporary fix - see #399. | 1710 | // FIXME! temporary fix - see #399. |
1767 | if (!empty($GLOBALS['pagetitle']) && count($linkDisp) == 1) { | 1711 | if ($conf->exists('pagetitle') && count($linkDisp) == 1) { |
1768 | $data['pagetitle'] = $GLOBALS['pagetitle']; | 1712 | $data['pagetitle'] = $conf->get('pagetitle'); |
1769 | } | 1713 | } |
1770 | 1714 | ||
1771 | $pluginManager = PluginManager::getInstance(); | 1715 | $pluginManager = PluginManager::getInstance(); |
@@ -1789,7 +1733,8 @@ function buildLinkList($PAGE,$LINKSDB) | |||
1789 | // Return an empty array if no thumbnail available. | 1733 | // Return an empty array if no thumbnail available. |
1790 | function computeThumbnail($url,$href=false) | 1734 | function computeThumbnail($url,$href=false) |
1791 | { | 1735 | { |
1792 | if (!$GLOBALS['config']['ENABLE_THUMBNAILS']) return array(); | 1736 | $conf = ConfigManager::getInstance(); |
1737 | if (!$conf->get('config.ENABLE_THUMBNAILS')) return array(); | ||
1793 | if ($href==false) $href=$url; | 1738 | if ($href==false) $href=$url; |
1794 | 1739 | ||
1795 | // For most hosts, the URL of the thumbnail can be easily deduced from the URL of the link. | 1740 | // For most hosts, the URL of the thumbnail can be easily deduced from the URL of the link. |
@@ -1857,7 +1802,7 @@ function computeThumbnail($url,$href=false) | |||
1857 | // So we deport the thumbnail generation in order not to slow down page generation | 1802 | // So we deport the thumbnail generation in order not to slow down page generation |
1858 | // (and we also cache the thumbnail) | 1803 | // (and we also cache the thumbnail) |
1859 | 1804 | ||
1860 | if (!$GLOBALS['config']['ENABLE_LOCALCACHE']) return array(); // If local cache is disabled, no thumbnails for services which require the use a local cache. | 1805 | if (! $conf->get('config.ENABLE_LOCALCACHE')) return array(); // If local cache is disabled, no thumbnails for services which require the use a local cache. |
1861 | 1806 | ||
1862 | if ($domain=='flickr.com' || endsWith($domain,'.flickr.com') | 1807 | if ($domain=='flickr.com' || endsWith($domain,'.flickr.com') |
1863 | || $domain=='vimeo.com' | 1808 | || $domain=='vimeo.com' |
@@ -1880,7 +1825,7 @@ function computeThumbnail($url,$href=false) | |||
1880 | $path = parse_url($url,PHP_URL_PATH); | 1825 | $path = parse_url($url,PHP_URL_PATH); |
1881 | if ("/talks/" !== substr($path,0,7)) return array(); // This is not a single video URL. | 1826 | if ("/talks/" !== substr($path,0,7)) return array(); // This is not a single video URL. |
1882 | } | 1827 | } |
1883 | $sign = hash_hmac('sha256', $url, $GLOBALS['salt']); // We use the salt to sign data (it's random, secret, and specific to each installation) | 1828 | $sign = hash_hmac('sha256', $url, $conf->get('salt')); // We use the salt to sign data (it's random, secret, and specific to each installation) |
1884 | return array('src'=>index_url($_SERVER).'?do=genthumbnail&hmac='.$sign.'&url='.urlencode($url), | 1829 | return array('src'=>index_url($_SERVER).'?do=genthumbnail&hmac='.$sign.'&url='.urlencode($url), |
1885 | 'href'=>$href,'width'=>'120','style'=>'height:auto;','alt'=>'thumbnail'); | 1830 | 'href'=>$href,'width'=>'120','style'=>'height:auto;','alt'=>'thumbnail'); |
1886 | } | 1831 | } |
@@ -1891,7 +1836,7 @@ function computeThumbnail($url,$href=false) | |||
1891 | $ext=strtolower(pathinfo($url,PATHINFO_EXTENSION)); | 1836 | $ext=strtolower(pathinfo($url,PATHINFO_EXTENSION)); |
1892 | if ($ext=='jpg' || $ext=='jpeg' || $ext=='png' || $ext=='gif') | 1837 | if ($ext=='jpg' || $ext=='jpeg' || $ext=='png' || $ext=='gif') |
1893 | { | 1838 | { |
1894 | $sign = hash_hmac('sha256', $url, $GLOBALS['salt']); // We use the salt to sign data (it's random, secret, and specific to each installation) | 1839 | $sign = hash_hmac('sha256', $url, $conf->get('salt')); // We use the salt to sign data (it's random, secret, and specific to each installation) |
1895 | return array('src'=>index_url($_SERVER).'?do=genthumbnail&hmac='.$sign.'&url='.urlencode($url), | 1840 | return array('src'=>index_url($_SERVER).'?do=genthumbnail&hmac='.$sign.'&url='.urlencode($url), |
1896 | 'href'=>$href,'width'=>'120','style'=>'height:auto;','alt'=>'thumbnail'); | 1841 | 'href'=>$href,'width'=>'120','style'=>'height:auto;','alt'=>'thumbnail'); |
1897 | } | 1842 | } |
@@ -1988,21 +1933,28 @@ function install() | |||
1988 | 1933 | ||
1989 | if (!empty($_POST['setlogin']) && !empty($_POST['setpassword'])) | 1934 | if (!empty($_POST['setlogin']) && !empty($_POST['setpassword'])) |
1990 | { | 1935 | { |
1936 | $conf = ConfigManager::getInstance(); | ||
1991 | $tz = 'UTC'; | 1937 | $tz = 'UTC'; |
1992 | if (!empty($_POST['continent']) && !empty($_POST['city']) | 1938 | if (!empty($_POST['continent']) && !empty($_POST['city']) |
1993 | && isTimeZoneValid($_POST['continent'], $_POST['city']) | 1939 | && isTimeZoneValid($_POST['continent'], $_POST['city']) |
1994 | ) { | 1940 | ) { |
1995 | $tz = $_POST['continent'].'/'.$_POST['city']; | 1941 | $tz = $_POST['continent'].'/'.$_POST['city']; |
1996 | } | 1942 | } |
1997 | $GLOBALS['timezone'] = $tz; | 1943 | $conf->set('timezone', $tz); |
1998 | // Everything is ok, let's create config file. | 1944 | $login = $_POST['setlogin']; |
1999 | $GLOBALS['login'] = $_POST['setlogin']; | 1945 | $conf->set('login', $login); |
2000 | $GLOBALS['salt'] = sha1(uniqid('',true).'_'.mt_rand()); // Salt renders rainbow-tables attacks useless. | 1946 | $salt = sha1(uniqid('', true) .'_'. mt_rand()); |
2001 | $GLOBALS['hash'] = sha1($_POST['setpassword'].$GLOBALS['login'].$GLOBALS['salt']); | 1947 | $conf->set('salt', $salt); |
2002 | $GLOBALS['title'] = (empty($_POST['title']) ? 'Shared links on '.escape(index_url($_SERVER)) : $_POST['title'] ); | 1948 | $conf->set('hash', sha1($_POST['setpassword'] . $login . $salt)); |
2003 | $GLOBALS['config']['ENABLE_UPDATECHECK'] = !empty($_POST['updateCheck']); | 1949 | if (!empty($_POST['title'])) { |
1950 | $conf->set('title', $_POST['title']); | ||
1951 | } else { | ||
1952 | $conf->set('title', 'Shared links on '.escape(index_url($_SERVER))); | ||
1953 | } | ||
1954 | $conf->set('config.ENABLE_UPDATECHECK', !empty($_POST['updateCheck'])); | ||
2004 | try { | 1955 | try { |
2005 | writeConfig($GLOBALS, isLoggedIn()); | 1956 | // Everything is ok, let's create config file. |
1957 | $conf->write(isLoggedIn()); | ||
2006 | } | 1958 | } |
2007 | catch(Exception $e) { | 1959 | catch(Exception $e) { |
2008 | error_log( | 1960 | error_log( |
@@ -2043,24 +1995,26 @@ function install() | |||
2043 | */ | 1995 | */ |
2044 | function genThumbnail() | 1996 | function genThumbnail() |
2045 | { | 1997 | { |
1998 | $conf = ConfigManager::getInstance(); | ||
2046 | // Make sure the parameters in the URL were generated by us. | 1999 | // Make sure the parameters in the URL were generated by us. |
2047 | $sign = hash_hmac('sha256', $_GET['url'], $GLOBALS['salt']); | 2000 | $sign = hash_hmac('sha256', $_GET['url'], $conf->get('salt')); |
2048 | if ($sign!=$_GET['hmac']) die('Naughty boy!'); | 2001 | if ($sign!=$_GET['hmac']) die('Naughty boy!'); |
2049 | 2002 | ||
2003 | $cacheDir = $conf->get('config.CACHEDIR', 'cache'); | ||
2050 | // Let's see if we don't already have the image for this URL in the cache. | 2004 | // Let's see if we don't already have the image for this URL in the cache. |
2051 | $thumbname=hash('sha1',$_GET['url']).'.jpg'; | 2005 | $thumbname=hash('sha1',$_GET['url']).'.jpg'; |
2052 | if (is_file($GLOBALS['config']['CACHEDIR'].'/'.$thumbname)) | 2006 | if (is_file($cacheDir .'/'. $thumbname)) |
2053 | { // We have the thumbnail, just serve it: | 2007 | { // We have the thumbnail, just serve it: |
2054 | header('Content-Type: image/jpeg'); | 2008 | header('Content-Type: image/jpeg'); |
2055 | echo file_get_contents($GLOBALS['config']['CACHEDIR'].'/'.$thumbname); | 2009 | echo file_get_contents($cacheDir .'/'. $thumbname); |
2056 | return; | 2010 | return; |
2057 | } | 2011 | } |
2058 | // We may also serve a blank image (if service did not respond) | 2012 | // We may also serve a blank image (if service did not respond) |
2059 | $blankname=hash('sha1',$_GET['url']).'.gif'; | 2013 | $blankname=hash('sha1',$_GET['url']).'.gif'; |
2060 | if (is_file($GLOBALS['config']['CACHEDIR'].'/'.$blankname)) | 2014 | if (is_file($cacheDir .'/'. $blankname)) |
2061 | { | 2015 | { |
2062 | header('Content-Type: image/gif'); | 2016 | header('Content-Type: image/gif'); |
2063 | echo file_get_contents($GLOBALS['config']['CACHEDIR'].'/'.$blankname); | 2017 | echo file_get_contents($cacheDir .'/'. $blankname); |
2064 | return; | 2018 | return; |
2065 | } | 2019 | } |
2066 | 2020 | ||
@@ -2107,7 +2061,7 @@ function genThumbnail() | |||
2107 | list($headers, $content) = get_http_response($imageurl, 10); | 2061 | list($headers, $content) = get_http_response($imageurl, 10); |
2108 | if (strpos($headers[0], '200 OK') !== false) { | 2062 | if (strpos($headers[0], '200 OK') !== false) { |
2109 | // Save image to cache. | 2063 | // Save image to cache. |
2110 | file_put_contents($GLOBALS['config']['CACHEDIR'].'/' . $thumbname, $content); | 2064 | file_put_contents($cacheDir .'/'. $thumbname, $content); |
2111 | header('Content-Type: image/jpeg'); | 2065 | header('Content-Type: image/jpeg'); |
2112 | echo $content; | 2066 | echo $content; |
2113 | return; | 2067 | return; |
@@ -2128,7 +2082,7 @@ function genThumbnail() | |||
2128 | list($headers, $content) = get_http_response($imageurl, 10); | 2082 | list($headers, $content) = get_http_response($imageurl, 10); |
2129 | if (strpos($headers[0], '200 OK') !== false) { | 2083 | if (strpos($headers[0], '200 OK') !== false) { |
2130 | // Save image to cache. | 2084 | // Save image to cache. |
2131 | file_put_contents($GLOBALS['config']['CACHEDIR'] . '/' . $thumbname, $content); | 2085 | file_put_contents($cacheDir .'/'. $thumbname, $content); |
2132 | header('Content-Type: image/jpeg'); | 2086 | header('Content-Type: image/jpeg'); |
2133 | echo $content; | 2087 | echo $content; |
2134 | return; | 2088 | return; |
@@ -2151,7 +2105,7 @@ function genThumbnail() | |||
2151 | // No control on image size, so wait long enough | 2105 | // No control on image size, so wait long enough |
2152 | list($headers, $content) = get_http_response($imageurl, 20); | 2106 | list($headers, $content) = get_http_response($imageurl, 20); |
2153 | if (strpos($headers[0], '200 OK') !== false) { | 2107 | if (strpos($headers[0], '200 OK') !== false) { |
2154 | $filepath=$GLOBALS['config']['CACHEDIR'].'/'.$thumbname; | 2108 | $filepath = $cacheDir .'/'. $thumbname; |
2155 | file_put_contents($filepath, $content); // Save image to cache. | 2109 | file_put_contents($filepath, $content); // Save image to cache. |
2156 | if (resizeImage($filepath)) | 2110 | if (resizeImage($filepath)) |
2157 | { | 2111 | { |
@@ -2179,7 +2133,7 @@ function genThumbnail() | |||
2179 | // No control on image size, so wait long enough | 2133 | // No control on image size, so wait long enough |
2180 | list($headers, $content) = get_http_response($imageurl, 20); | 2134 | list($headers, $content) = get_http_response($imageurl, 20); |
2181 | if (strpos($headers[0], '200 OK') !== false) { | 2135 | if (strpos($headers[0], '200 OK') !== false) { |
2182 | $filepath=$GLOBALS['config']['CACHEDIR'].'/'.$thumbname; | 2136 | $filepath = $cacheDir.'/'.$thumbname; |
2183 | // Save image to cache. | 2137 | // Save image to cache. |
2184 | file_put_contents($filepath, $content); | 2138 | file_put_contents($filepath, $content); |
2185 | if (resizeImage($filepath)) | 2139 | if (resizeImage($filepath)) |
@@ -2199,7 +2153,7 @@ function genThumbnail() | |||
2199 | // We allow 30 seconds max to download (and downloads are limited to 4 Mb) | 2153 | // We allow 30 seconds max to download (and downloads are limited to 4 Mb) |
2200 | list($headers, $content) = get_http_response($url, 30); | 2154 | list($headers, $content) = get_http_response($url, 30); |
2201 | if (strpos($headers[0], '200 OK') !== false) { | 2155 | if (strpos($headers[0], '200 OK') !== false) { |
2202 | $filepath=$GLOBALS['config']['CACHEDIR'].'/'.$thumbname; | 2156 | $filepath = $cacheDir .'/'.$thumbname; |
2203 | // Save image to cache. | 2157 | // Save image to cache. |
2204 | file_put_contents($filepath, $content); | 2158 | file_put_contents($filepath, $content); |
2205 | if (resizeImage($filepath)) | 2159 | if (resizeImage($filepath)) |
@@ -2214,7 +2168,8 @@ function genThumbnail() | |||
2214 | 2168 | ||
2215 | // Otherwise, return an empty image (8x8 transparent gif) | 2169 | // Otherwise, return an empty image (8x8 transparent gif) |
2216 | $blankgif = base64_decode('R0lGODlhCAAIAIAAAP///////yH5BAEKAAEALAAAAAAIAAgAAAIHjI+py+1dAAA7'); | 2170 | $blankgif = base64_decode('R0lGODlhCAAIAIAAAP///////yH5BAEKAAEALAAAAAAIAAgAAAIHjI+py+1dAAA7'); |
2217 | file_put_contents($GLOBALS['config']['CACHEDIR'].'/'.$blankname,$blankgif); // Also put something in cache so that this URL is not requested twice. | 2171 | // Also put something in cache so that this URL is not requested twice. |
2172 | file_put_contents($cacheDir .'/'. $blankname, $blankgif); | ||
2218 | header('Content-Type: image/gif'); | 2173 | header('Content-Type: image/gif'); |
2219 | echo $blankgif; | 2174 | echo $blankgif; |
2220 | } | 2175 | } |
@@ -2254,6 +2209,8 @@ function resizeImage($filepath) | |||
2254 | 2209 | ||
2255 | if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=genthumbnail')) { genThumbnail(); exit; } // Thumbnail generation/cache does not need the link database. | 2210 | if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=genthumbnail')) { genThumbnail(); exit; } // Thumbnail generation/cache does not need the link database. |
2256 | if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=dailyrss')) { showDailyRSS(); exit; } | 2211 | if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=dailyrss')) { showDailyRSS(); exit; } |
2257 | if (!isset($_SESSION['LINKS_PER_PAGE'])) $_SESSION['LINKS_PER_PAGE']=$GLOBALS['config']['LINKS_PER_PAGE']; | 2212 | if (!isset($_SESSION['LINKS_PER_PAGE'])) { |
2213 | $_SESSION['LINKS_PER_PAGE'] = $conf->get('config.LINKS_PER_PAGE', 20); | ||
2214 | } | ||
2258 | renderPage(); | 2215 | renderPage(); |
2259 | ?> | 2216 | ?> |