forked from captivat/LBCMail
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConfigManager.php
155 lines (143 loc) · 4.01 KB
/
ConfigManager.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
<?php
class Alert {
public $email;
public $id;
public $title;
public $suspend = 0;
public $url;
public $interval = 180;
public $time_last_ad = 0;
public $time_updated = 0;
public $price_min = -1;
public $price_max = -1;
public $price_strict = false;
public $cities;
public function fromArray(array $values)
{
foreach ($values AS $key => $value) {
$this->$key = $value;
}
}
public function toArray()
{
return array(
"email" => $this->email,
"id" => $this->id,
"title" => $this->title,
"suspend" => $this->suspend,
"url" => $this->url,
"interval" => $this->interval,
"time_last_ad" => $this->time_last_ad,
"time_updated" => $this->time_updated,
"price_min" => $this->price_min,
"price_max" => $this->price_max,
"price_strict" => $this->price_strict,
"cities" => $this->cities
);
}
}
class ConfigManager
{
protected static $_config;
protected static $_name = "config";
public static function getConfigFile()
{
return dirname(__FILE__)."/configs/".self::$_name.".csv";
}
public static function setConfigName($name)
{
self::$_name = $name;
self::load();
}
public static function load()
{
if (!is_file(self::getConfigFile())) {
return array();
}
$fp = fopen(self::getConfigFile(), "r");
if (!$header = fgetcsv($fp, 0, ",", '"')) {
return array();
}
$nb = count($header);
$config = array();
while (false !== $a = fgetcsv($fp, 0, ",", '"')) {
$alert = new Alert();
for ($i = 0; $i < $nb; $i++) {
if (isset($a[$i])) {
$alert->$header[$i] = $a[$i];
}
}
$config[$alert->id] = $alert;
}
fclose($fp);
self::$_config = $config;
return $config;
}
public static function save()
{
if (!is_array(self::$_config)) {
self::load();
}
$filename = self::getConfigFile();
$setChmod = false;
if (!is_file($filename)) {
$dir = dirname($filename);
if ($dir == $filename) {
throw new Exception("Permission d'écrire sur le fichier de configuration non autorisée.");
}
if (!is_writable($dir)) {
throw new Exception("Permission d'écrire sur le fichier de configuration non autorisée.");
}
$setChmod = true;
}
$fp = fopen($filename, "w");
if (self::$_config && is_array(self::$_config)) {
$alerts = array_values(self::$_config);
$keys = array_keys($alerts[0]->toArray());
fputcsv($fp, $keys, ",", '"');
foreach (self::$_config AS $alert) {
fputcsv($fp, array_values($alert->toArray()), ",", '"');
}
}
fclose($fp);
if ($setChmod) {
chmod($filename, 0777);
}
}
public static function saveAlert(Alert $alert)
{
if (!is_array(self::$_config)) {
self::load();
}
if (empty($alert->id)) {
$alert->id = md5(uniqid());
}
self::$_config[$alert->id] = $alert;
self::save();
}
public static function deleteAlert(Alert $alert)
{
if (!is_array(self::$_config)) {
self::load();
}
unset(self::$_config[$alert->id]);
self::save();
}
public static function getAlerts()
{
if (!is_array(self::$_config)) {
self::load();
}
return self::$_config;
}
public static function getAlertById($id)
{
if (!is_array(self::$_config)) {
self::load();
}
if (isset(self::$_config[$id])) {
return self::$_config[$id];
}
return null;
}
}