forked from Blount/Cheky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.php
211 lines (188 loc) · 6.61 KB
/
bootstrap.php
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
<?php
define("DOCUMENT_ROOT", __DIR__);
define("DS", DIRECTORY_SEPARATOR);
// Define application environment
defined("APPLICATION_ENV")
|| define("APPLICATION_ENV", (getenv("APPLICATION_ENV") ? getenv("APPLICATION_ENV") : "production"));
// Define application environment
define("APPLICATION_VERSION", require DOCUMENT_ROOT."/version.php");
set_include_path(
__DIR__."/lib".PATH_SEPARATOR.get_include_path()
);
require_once "Http/Client/Curl.php";
require_once "Config/Lite.php";
require_once "Log4php/Logger.php";
class Bootstrap
{
/**
* @var Config_Lite
*/
protected $_config;
/**
* @var HttpClientCurl
*/
protected $_client;
protected function initAutoload()
{
spl_autoload_register(function ($className) {
$filename = ltrim(str_replace("\\", "/", $className), "/");
if (false !== strpos($filename, "App/")) {
$filename = __DIR__."/app/models/".
str_replace("App/", "", $filename).".php";
} else {
$filename = __DIR__."/lib/".$filename.".php";
}
if (is_file($filename)) {
require_once $filename;
}
});
}
protected function initPHPConfig()
{
mb_internal_encoding("UTF-8");
if (function_exists("date_default_timezone_set")) {
date_default_timezone_set("Europe/Paris");
}
if (APPLICATION_ENV == "development") {
ini_set("display_errors", true);
ini_set("error_reporting", -1);
}
}
protected function initLogger()
{
Logger::configure(array(
"rootLogger" => array(
"appenders" => array("default"),
"level" => APPLICATION_ENV == "development"?"debug":"info"
),
"appenders" => array(
"default" => array(
"class" => "LoggerAppenderRollingFile",
"layout" => array(
"class" => "LoggerLayoutPattern",
"params" => array(
"conversionPattern" => "%date %-5level %msg%n"
)
),
"params" => array(
"file" => DOCUMENT_ROOT."/var/log/info.log",
"maxFileSize" => "3MB",
"maxBackupIndex" => 5,
"append" => true
)
)
)
));
}
protected function initConfig()
{
// valeurs par défaut.
$this->_config->set("proxy", "ip", "");
$this->_config->set("proxy", "port", "");
$this->_config->set("proxy", "user", "");
$this->_config->set("proxy", "password", "");
$this->_config->set("http", "user_agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.6) Gecko/20100628 Ubuntu/10.04 (lucid) Firefox/3.6.6");
$this->_config->set("general", "check_start", 7);
$this->_config->set("general", "check_end", 24);
$this->_config->set("general", "version", 0);
$this->_config->set("general", "baseurl", "");
$this->_config->set("storage", "type", "files");
// lit la configuration du fichier.
try {
$this->_config->read();
} catch (Config_Lite_Exception_Runtime $e) {
return;
}
if (isset($_SERVER["HTTP_HOST"])) {
$current_base_url = $this->_config->get("general", "baseurl", "");
$base_url = "http";
if (!empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] != "off") {
$base_url .= "s";
}
$base_url .= "://".$_SERVER["HTTP_HOST"]."/";
if (!empty($_SERVER["REQUEST_URI"])) {
$request_uri = trim($_SERVER["REQUEST_URI"], "/");
if (false !== $pos = strpos($request_uri, "?")) {
$request_uri = mb_substr($request_uri, 0, $pos);
}
if ($request_uri) {
$base_url .= trim($request_uri, "/")."/";
}
}
if ($base_url != $current_base_url) {
$this->_config->set("general", "baseurl", $base_url);
$this->_config->save();
}
}
}
public function getClient()
{
if (!$this->_client) {
$client = new HttpClientCurl();
$proxy = $this->_config->get("proxy", null, array());
if (!empty($proxy["ip"])) {
$client->setProxyIp($proxy["ip"]);
if (!empty($proxy["port"])) {
$client->setProxyPort($proxy["port"]);
}
}
if ($userAgent = $this->_config->get("http", "user_agent", "")) {
$client->setUserAgent($userAgent);
}
}
return $client;
}
public function __construct()
{
}
public function bootstrap(Config_Lite $config)
{
$this->_config = $config;
foreach (get_class_methods($this) AS $method) {
if (0 === strpos($method, "init")) {
$this->$method();
}
}
}
}
$config = new Config_Lite(DOCUMENT_ROOT."/var/config.ini");
$bootstrap = new Bootstrap();
$bootstrap->bootstrap($config);
$userAuthed = null;
// initialise le client HTTP.
$client = $bootstrap->getClient();
// si stockage en base de données, on initialise la connexion
if ("db" == $config->get("storage", "type", "files")) {
$options = array_merge(array(
"host" => "",
"user" => "",
"password" => "",
"dbname" => ""
), $config->get("storage", "options"));
$dbConnection = new mysqli($options["host"], $options["user"],
$options["password"], $options["dbname"]);
unset($options);
}
### Fonctions ###
/**
* Test la connexion HTTP.
* @param HttpClientAbstract $client
* @return string|boolean
*/
function testHTTPConnection(HttpClientAbstract $client) {
// teste la connexion
$client->setDownloadBody(false);
if (false === $client->request("http://www.google.fr")) {
return "Connexion vers http://www.google.fr échouée: cet hébergement ne semble pas accepter les connexions distantes.";
}
if (200 != $client->getRespondCode()) {
return "Code HTTP différent de 200.";
}
if (false === $client->request("http://www.leboncoin.fr")) {
return "Connexion vers http://www.leboncoin.fr échouée: cet hébergement (ou le proxy) semble blacklisté par Leboncoin";
}
if (200 != $client->getRespondCode()) {
return "Code HTTP différent de 200.";
}
return true;
}