|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Koch Framework |
|
5
|
|
|
* Jens-André Koch © 2005 - onwards. |
|
6
|
|
|
* |
|
7
|
|
|
* This file is part of "Koch Framework". |
|
8
|
|
|
* |
|
9
|
|
|
* License: GNU/GPL v2 or any later version, see LICENSE file. |
|
10
|
|
|
* |
|
11
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU General Public License as published by |
|
13
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
14
|
|
|
* (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* This program is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU General Public License |
|
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Koch\Filter\Filters; |
|
26
|
|
|
|
|
27
|
|
|
use Koch\Exception\Exception; |
|
28
|
|
|
use Koch\Filter\FilterInterface; |
|
29
|
|
|
use Koch\Http\HttpRequestInterface; |
|
30
|
|
|
use Koch\Http\HttpResponseInterface; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Koch Framework - Filter performing Startup Checks. |
|
34
|
|
|
* |
|
35
|
|
|
* Purpose: Perform Various Startup Check before running a Koch Framework Module. |
|
36
|
|
|
*/ |
|
37
|
|
|
class StartupChecks implements FilterInterface |
|
38
|
|
|
{ |
|
39
|
|
|
public function executeFilter(HttpRequestInterface $request, HttpResponseInterface $response) |
|
40
|
|
|
{ |
|
41
|
|
|
/* |
|
42
|
|
|
* Deny service, if the system load is too high. |
|
43
|
|
|
*/ |
|
44
|
|
|
if (defined('DEBUG') and DEBUG === false) { |
|
|
|
|
|
|
45
|
|
|
$maxServerLoad = isset(self::$config['load']['max']) ? (float) self::$config['load']['max'] : 80; |
|
46
|
|
|
|
|
47
|
|
|
if (\Koch\Functions\Functions::getServerLoad() > $maxServerLoad) { |
|
48
|
|
|
$response->addHeader('Retry-After', mt_rand(45, 90)); |
|
49
|
|
|
$response->setStatusCode('503'); |
|
50
|
|
|
$response->addContent('HTTP/1.1 503 Server too busy. Please try again later.'); |
|
|
|
|
|
|
51
|
|
|
$response->sendResponse(); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// ensure smarty "tpl_compile" folder exists |
|
56
|
|
View Code Duplication |
if (false === is_dir(APPLICATION_CACHE_PATH . 'tpl_compile') and |
|
|
|
|
|
|
57
|
|
|
(false === @mkdir(APPLICATION_CACHE_PATH . 'tpl_compile', 0755, true))) { |
|
58
|
|
|
throw new Exception('Smarty Template Directories not existant.', 9); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// ensure smarty "cache" folder exists |
|
62
|
|
View Code Duplication |
if (false === is_dir(APPLICATION_CACHE_PATH . 'tpl_cache') and |
|
|
|
|
|
|
63
|
|
|
(false === @mkdir(APPLICATION_CACHE_PATH . 'tpl_cache', 0755, true))) { |
|
64
|
|
|
throw new Exception('Smarty Template Directories not existant.', 9); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// ensure smarty folders are writable |
|
68
|
|
|
if (false === is_writable(APPLICATION_CACHE_PATH . 'tpl_compile') or |
|
|
|
|
|
|
69
|
|
|
(false === is_writable(APPLICATION_CACHE_PATH . 'tpl_cache'))) { |
|
70
|
|
|
// if not, try to set writeable permission on the folders |
|
71
|
|
View Code Duplication |
if ((false === chmod(APPLICATION_CACHE_PATH . 'tpl_compile', 0755)) and |
|
|
|
|
|
|
72
|
|
|
(false === chmod(APPLICATION_CACHE_PATH . 'tpl_cache', 0755))) { |
|
73
|
|
|
throw new Exception('Smarty Template Directories not writable.', 10); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.