Редакция 359 | Редакция 380 | К новейшей редакции | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | RSS
Редакция | Автор | № строки | Строка |
---|---|---|---|
304 | alex-w | 1 | <?php |
2 | |||
3 | /** |
||
4 | * |
||
5 | * Codename: ant-ng - generator of sources.list for Debian and |
||
6 | * distributives, based on Debian |
||
7 | * http://alex-w.org.ru/p/antng/ |
||
8 | * |
||
9 | * Copyright (c) 2009 Alexander Wolf |
||
10 | * Dual licensed under the MIT and GNU LGPL licenses. |
||
11 | * http://alex-w.org.ru/p/antng/license |
||
12 | * |
||
13 | */ |
||
14 | |||
15 | class Core { |
||
308 | alex-w | 16 | protected $db = NULL; |
17 | protected $prefix = NULL; |
||
313 | alex-w | 18 | protected $secure = NULL; |
359 | alex-w | 19 | protected $cookie = NULL; |
304 | alex-w | 20 | |
368 | alex-w | 21 | public function __construct($database, $prefix, $secure, $cookie) { |
308 | alex-w | 22 | $this->db = $database; |
23 | $this->prefix = $prefix; |
||
359 | alex-w | 24 | $this->secure = $secure; |
25 | $this->cookie = $cookie; |
||
307 | alex-w | 26 | } |
27 | |||
315 | alex-w | 28 | // Получение данных о настройке |
368 | alex-w | 29 | public function getOption($attr) { |
308 | alex-w | 30 | $result = array(); |
315 | alex-w | 31 | $query = "SELECT optvalue FROM ".$this->prefix."settings WHERE opt='".$this->secure->checkStr($attr)."'"; |
314 | alex-w | 32 | $rq =& $this->db->query($query); |
308 | alex-w | 33 | if ($rq->numRows()!=0) { |
34 | $rq->fetchInto($element); |
||
35 | $result["ERR"] = 0; |
||
36 | $result["OptValue"] = $element["optvalue"]; |
||
37 | } else { |
||
38 | $result["ERR"] = 1; |
||
39 | $result["ERRINFO"] = "Empty result"; |
||
40 | } |
||
41 | return $result; |
||
42 | } |
||
43 | |||
359 | alex-w | 44 | // Установка данных о настройке |
368 | alex-w | 45 | public function setOption($attr, $value) { |
359 | alex-w | 46 | $result = array(); |
47 | |||
48 | if ($attr != "passwd") { |
||
49 | $sValue = $this->secure->checkStr($value); |
||
50 | } else { |
||
51 | $sValue = $value; |
||
52 | } |
||
53 | |||
54 | $query = "UPDATE ".$this->prefix."settings SET optvalue='".$sValue."' WHERE opt='".$attr."'"; |
||
55 | $rq =& $this->db->query($query); |
||
56 | if (PEAR::isError($this->db)) { |
||
57 | $result["ERR"] = 1; |
||
58 | $result["ERRINFO"] = $this->db->getMessage(); |
||
59 | } else { |
||
60 | $result["ERR"] = 0; |
||
61 | } |
||
62 | |||
63 | return $result; |
||
64 | } |
||
65 | |||
66 | // Создание настройки |
||
368 | alex-w | 67 | public function addOption($attr, $value) { |
359 | alex-w | 68 | $result = array(); |
69 | $sValue = $this->secure->checkStr($value); |
||
70 | |||
71 | $query = "INSERT INTO ".$this->prefix."settings SET opt='".$attr."', optvalue='".$sValue."'"; |
||
72 | $rq =& $this->db->query($query); |
||
73 | if (PEAR::isError($this->db)) { |
||
74 | $result["ERR"] = 1; |
||
75 | $result["ERRINFO"] = $this->db->getMessage(); |
||
76 | } else { |
||
77 | $result["ERR"] = 0; |
||
78 | } |
||
79 | |||
80 | return $result; |
||
81 | } |
||
82 | |||
315 | alex-w | 83 | // Получение и отображение списка дистрибутивов |
368 | alex-w | 84 | public function showDistributionList($name, $info = "", $format = 'html') { |
315 | alex-w | 85 | $query = "SELECT * FROM ".$this->prefix."distribution ORDER BY dist_id ASC"; |
317 | alex-w | 86 | $rq =& $this->db->query($query); |
315 | alex-w | 87 | switch ($format) { |
88 | case 'html': |
||
317 | alex-w | 89 | $show = "<fieldset><legend>".$info."</legend>\n<select id='".$name."' name='".$name."'>\n"; |
315 | alex-w | 90 | while ($rq->fetchInto($element)) { |
91 | $show .= "<option value='".$this->secure->checkInt($element["dist_id"])."'>".$this->secure->checkStr($element["distname"],1)."</option>\n"; |
||
92 | } |
||
317 | alex-w | 93 | $show .= "</select></fieldset>"; |
315 | alex-w | 94 | break; |
95 | case 'json': |
||
317 | alex-w | 96 | $show = '[{value:"",text:"'.$info.'"}'; |
315 | alex-w | 97 | while ($rq->fetchInto($element)) { |
98 | $show .= ',{value:"'.$this->secure->checkInt($element["dist_id"]).'",text:"'.$this->secure->checkStr($element["distname"],1).'"}'; |
||
99 | } |
||
100 | $show .= ']'; |
||
101 | break; |
||
102 | } |
||
103 | return $show; |
||
104 | } |
||
105 | |||
106 | // Получение названия дистрибутива |
||
368 | alex-w | 107 | public function getDistName($distID) { |
315 | alex-w | 108 | $result = array(); |
109 | $query = "SELECT distname FROM ".$this->prefix."distribution WHERE dist_id='".$this->secure->checkInt($distID)."'"; |
||
110 | $rq =& $this->db->query($query); |
||
111 | if (PEAR::isError($this->db)) { |
||
112 | $result["ERR"] = 1; |
||
113 | $result["ERRINFO"] = $this->db->getMessage(); |
||
114 | } else { |
||
115 | $rq->fetchInto($element); |
||
116 | $result["ERR"] = 0; |
||
117 | $result["DistName"] = $this->secure->checkStr($element["distname"],1); |
||
118 | } |
||
119 | |||
120 | return $result; |
||
121 | } |
||
122 | |||
123 | // Получение и отображение списка версий дистрибутива |
||
368 | alex-w | 124 | public function showDistVersionsList($name, $distID, $format = 'html') { |
316 | alex-w | 125 | $distname = $this->getDistName($distID); |
315 | alex-w | 126 | $query = "SELECT * FROM ".$this->prefix."version WHERE dist_id='".$this->secure->checkInt($distID)."' ORDER BY version ASC"; |
317 | alex-w | 127 | $rq =& $this->db->query($query); |
315 | alex-w | 128 | switch ($format) { |
129 | case 'html': |
||
317 | alex-w | 130 | $show = "<fieldset><legend>Версии ".$distname["DistName"]."</legend>\n<select id='".$name."' name='".$name."'>\n"; |
315 | alex-w | 131 | while ($rq->fetchInto($element)) { |
316 | alex-w | 132 | $show .= "<option value='".$this->secure->checkInt($element["version_id"])."'>".$this->secure->checkStr($element["version"],1)." ".$this->secure->checkStr($element["vname"],1)."</option>\n"; |
315 | alex-w | 133 | } |
317 | alex-w | 134 | $show .= "</select></fieldset>"; |
315 | alex-w | 135 | break; |
136 | case 'json': |
||
317 | alex-w | 137 | $show = '[{value:"",text:"Выбери версию '.$distname["DistName"].'"}'; |
315 | alex-w | 138 | while ($rq->fetchInto($element)) { |
316 | alex-w | 139 | $show .= ',{value:"'.$this->secure->checkInt($element["version_id"]).'",text:"'.$this->secure->checkStr($element["version"],1).' '.$this->secure->checkStr($element["vname"],1).'"}'; |
315 | alex-w | 140 | } |
141 | $show .= ']'; |
||
142 | break; |
||
143 | } |
||
144 | return $show; |
||
145 | } |
||
146 | |||
317 | alex-w | 147 | // Получение и отображение списка секций основного (официального) репозитория |
368 | alex-w | 148 | public function showBranchesList($version, $format = 'html') { |
317 | alex-w | 149 | $query = "SELECT rtype FROM ".$this->prefix."rtype WHERE rtype_id='1'"; |
150 | $rq =& $this->db->query($query); |
||
151 | $rq->fetchInto($types); |
||
152 | $query = "SELECT s.*,t.rtype FROM ".$this->prefix."section s "; |
||
153 | $query .= "JOIN ".$this->prefix."sect2rep l ON s.sect_id=l.sect_id "; |
||
154 | $query .= "JOIN ".$this->prefix."repository r ON r.rep_id=l.rep_id "; |
||
155 | $query .= "JOIN ".$this->prefix."rtype t ON r.rtype_id=t.rtype_id "; |
||
156 | $query .= "WHERE t.rtype_id='1' AND r.version='".$this->secure->checkInt($version)."'"; |
||
157 | $rq =& $this->db->query($query); |
||
158 | switch ($format) { |
||
159 | case 'html': |
||
160 | $show = "<fieldset><legend>".$this->secure->checkStr($types["rtype"],1)."</legend>\n"; |
||
161 | while ($rq->fetchInto($element)) { |
||
320 | alex-w | 162 | $show .= "<div class='sections'><input type='checkbox' name='section[]' value='".$element["sect_id"]."'> ".$this->secure->checkStr($element["secname"],1)." — ".$this->secure->checkStr($element["secinfo"],1)."</div>\n"; |
317 | alex-w | 163 | } |
164 | $show .= "</fieldset>\n"; |
||
165 | break; |
||
166 | case 'json': |
||
167 | //TODO Доделать JSON-вывод списка секций основного репозитория |
||
168 | break; |
||
169 | } |
||
318 | alex-w | 170 | |
171 | return $show; |
||
317 | alex-w | 172 | } |
173 | |||
174 | // Получение и отображение списка репозиториев |
||
368 | alex-w | 175 | public function showRepList($version, $reptype, $format = 'html') { |
317 | alex-w | 176 | $query = "SELECT rtype FROM ".$this->prefix."rtype WHERE rtype_id='1'"; |
177 | $rq =& $this->db->query($query); |
||
178 | $rq->fetchInto($types); |
||
179 | $query = "SELECT * FROM ".$this->prefix."repository WHERE version='".$this->secure->checkInt($version)."' AND rtype_id='".$this->secure->checkInt($reptype)."'"; |
||
180 | $rq =& $this->db->query($query); |
||
181 | switch ($format) { |
||
182 | case 'html': |
||
183 | $show = "<fieldset><legend>".$this->secure->checkStr($types["rtype"],1)."</legend>\n"; |
||
184 | while ($rq->fetchInto($types)) { |
||
320 | alex-w | 185 | $show .= "<div class='repository'><input type='checkbox' name='repository[]' value='".$element["rep_id"]."'> ".$this->secure->checkStr($element["repname"],1)." — ".$this->secure->checkStr($element["repinfo"],1)."</div>\n"; |
317 | alex-w | 186 | } |
187 | $show .= "</fieldset>\n"; |
||
188 | break; |
||
189 | case 'json': |
||
190 | //TODO Доделать JSON-вывод списка репозиториев |
||
191 | break; |
||
192 | } |
||
318 | alex-w | 193 | |
194 | return $show; |
||
317 | alex-w | 195 | } |
196 | |||
321 | alex-w | 197 | // Добавление поддержки нового apt-дистрибутива |
368 | alex-w | 198 | public function addDistribution($distname, $disttype, $distua = 1, $distlogo = 0) { |
321 | alex-w | 199 | $result = array(); |
200 | $sDName = $this->secure->checkStr($distname); |
||
201 | $sDType = $this->secure->checkInt($disttype); |
||
202 | $sDUAgt = $this->secure->checkStr($distua); |
||
203 | $sDLogo = $this->secure->checkInt($distname); |
||
204 | |||
205 | $query = "INSERT INTO ".$this->prefix."distribution SET distname='".$sDName."', distua='".$sDUAgt."', disttype='".$sDType."', distlogo='".$sDLogo."'"; |
||
206 | $rq =& $this->db->query($query); |
||
207 | if (PEAR::isError($this->db)) { |
||
208 | $result["ERR"] = 1; |
||
209 | $result["ERRINFO"] = $this->db->getMessage(); |
||
210 | } else { |
||
211 | $rq->fetchInto($element); |
||
212 | $result["ERR"] = 0; |
||
213 | } |
||
214 | |||
215 | return $result; |
||
216 | } |
||
217 | |||
356 | alex-w | 218 | // Добавление поддержки новой версии apt-дистрибутива |
368 | alex-w | 219 | public function addDistVersion($distID, $version, $vname = "", $vcodename = "") { |
356 | alex-w | 220 | $result = array(); |
221 | $sDistID = $this->secure->checkStr($distID); |
||
222 | $sDVersion = $this->secure->checkStr($version); |
||
223 | $sDVName = $this->secure->checkStr($vname); |
||
224 | $sDVCName = $this->secure->checkInt($vcodename); |
||
225 | |||
226 | $query = "INSERT INTO ".$this->prefix."version SET dist_id='".$sDistID."', vname='".$sDVName."', version='".$sDVersion."', vcodename='".$sDVCName."'"; |
||
227 | $rq =& $this->db->query($query); |
||
228 | if (PEAR::isError($this->db)) { |
||
229 | $result["ERR"] = 1; |
||
230 | $result["ERRINFO"] = $this->db->getMessage(); |
||
231 | } else { |
||
232 | $rq->fetchInto($element); |
||
233 | $result["ERR"] = 0; |
||
234 | } |
||
235 | |||
236 | return $result; |
||
237 | } |
||
358 | alex-w | 238 | |
329 | alex-w | 239 | // Отображение типа дистрибутива |
368 | alex-w | 240 | public function showDistTypeForm($name = "dtype",$type = 0) { |
329 | alex-w | 241 | $query = "SELECT * FROM ".$this->prefix."dtype"; |
242 | $rq =& $this->db->query($query); |
||
243 | $show = "<select name='".$name."' id='".$name."'>\n"; |
||
244 | while ($rq->fetchInto($element)) { |
||
347 | alex-w | 245 | if ($element["type_id"] == $type) { |
246 | $show .= "<option value='".$element["type_id"]."' selected>".$this->secure->checkStr($element["type"],1)."</option>\n"; |
||
329 | alex-w | 247 | } else { |
347 | alex-w | 248 | $show .= "<option value='".$element["type_id"]."'>".$this->secure->checkStr($element["type"],1)."</option>\n"; |
329 | alex-w | 249 | } |
250 | } |
||
251 | $show .= "</select>"; |
||
252 | |||
253 | return $show; |
||
254 | } |
||
255 | |||
358 | alex-w | 256 | // Отображение формы создания и редактирования apt-дистрибутива |
368 | alex-w | 257 | public function showDistributionForm($distID = 0) { |
329 | alex-w | 258 | $sDistID = $this->secure->checkInt($distID); |
259 | if ($sDistID != 0) { |
||
260 | // Режим редактирования |
||
261 | $query = "SELECT * FROM ".$this->prefix."distribution WHERE dist_id='".$sDistID."'"; |
||
262 | $rq =& $this->db->query($query); |
||
263 | $rq->fetchInto($element); |
||
264 | } |
||
265 | |||
266 | if ($element["distlogo"] == 1) { |
||
267 | $image = "<img src='./img/".$this->secure->checkStr($element["distua"],1).".png' width='32' height='32' id='adm-dist-logo'>"; |
||
268 | } else { |
||
269 | $image = "<img src='./img/empty-logo.png' width='32' height='32' id='adm-dist-logo'>"; |
||
270 | } |
||
271 | |||
272 | $show = "<fieldset><legend>Дистрибутив</legend>\n"; |
||
350 | alex-w | 273 | $show .= "<div class='inputbox'><label for='dname'>Название дистрибутива:</label> <input type='text' name='dname' id='dname' value='".$this->secure->checkStr($element["distname"],1)."'></div>\n"; |
274 | $show .= "<div class='inputbox'><label for='dua'>UA дистрибутива:</label> <input type='text' name='dua' id='dua' value='".$this->secure->checkStr($element["distua"],1)."'></div>\n"; |
||
275 | $show .= "<div class='inputbox'><label for='dtype'>Тип дистрибутива:</label> ".$this->showDistTypeForm("dtype",$element["dtype_id"])."</div>\n"; |
||
348 | alex-w | 276 | $show .= "<div class='inputbox'><table><tr><td class='td-name'>Логотип дистрибутива:</td>\n"; |
329 | alex-w | 277 | $show .= "<td>".$image."</td>\n<td><input type='file' name='distlogo'></td>\n</tr></table>\n</div>\n"; |
350 | alex-w | 278 | $show .= "<div class='inputbox'><input type='submit' value=' Отправить данные '></div>\n</fieldset>\n"; |
329 | alex-w | 279 | |
280 | return $show; |
||
281 | } |
||
282 | |||
358 | alex-w | 283 | // sourses.list |
368 | alex-w | 284 | public function showSourcesList() { |
357 | alex-w | 285 | //TODO Написать генератор sources.list |
358 | alex-w | 286 | } |
287 | |||
359 | alex-w | 288 | // Проверка пароля (из формы авторизации) |
368 | alex-w | 289 | public function checkSign($word) { |
359 | alex-w | 290 | $result = array(); |
291 | |||
292 | $sHash = $this->secure->encryptStr($word); |
||
293 | $pwd = $this->getOption("passwd"); |
||
294 | if ($sHash == $pwd["OptValue"]) { |
||
295 | $result["ERR"] = 0; |
||
296 | $result["Location"] = "manager.php"; |
||
297 | setcookie($this->cookie, $sHash); |
||
298 | } else { |
||
299 | $result["ERR"] = 1; |
||
300 | $result["ERRINFO"] = "Password not valid"; |
||
368 | alex-w | 301 | $result["Location"] = "manager.php?error=1"; |
359 | alex-w | 302 | } |
303 | |||
304 | return $result; |
||
358 | alex-w | 305 | } |
357 | alex-w | 306 | |
359 | alex-w | 307 | // Проверка пароля (из cookies) |
368 | alex-w | 308 | public function checkCookieSign($hash) { |
359 | alex-w | 309 | $result = array(); |
310 | |||
311 | $pwd = $this->getOption("passwd"); |
||
312 | if ($hash == $pwd["OptValue"]) { |
||
313 | $result["ERR"] = 0; |
||
314 | } else { |
||
315 | $result["ERR"] = 1; |
||
316 | $result["ERRINFO"] = "Hash not valid"; |
||
368 | alex-w | 317 | $result["Location"] = "manager.php"; |
359 | alex-w | 318 | } |
319 | |||
320 | return $result; |
||
321 | } |
||
322 | |||
368 | alex-w | 323 | // Форма ввода пароля |
324 | public function showSigninForm() { |
||
325 | $show = "<form action='process.php' method='post'>\n"; |
||
326 | $show .= "<fieldset><legend>Пароль</legend>\n"; |
||
327 | $show .= "<input type='hidden' name='mode' value='authorize'>\n"; |
||
328 | $show .= "<input type='text' name='word' value=''>\n<br />"; |
||
329 | $show .= "<input type='submit' value=' Войти '>\n"; |
||
330 | $show .= "</fieldset>\n</form>\n"; |
||
331 | |||
332 | return $show; |
||
333 | } |
||
334 | |||
359 | alex-w | 335 | // Обновление пароля |
368 | alex-w | 336 | public function updatePassword($word1, $word2) { |
359 | alex-w | 337 | $result = array(); |
338 | |||
339 | if ($word1 == $word2) { |
||
340 | $sWord = $this->secure->encryptStr($word1); |
||
341 | $r = $this->setOption("passwd", $sWord); |
||
342 | $result = $r; |
||
343 | } else { |
||
344 | $result["ERR"] = 1; |
||
345 | $result["ERRINFO"] = "Passwords is mismatch"; |
||
346 | } |
||
347 | |||
348 | return $result; |
||
349 | } |
||
350 | |||
304 | alex-w | 351 | } |
352 | |||
356 | alex-w | 353 | ?> |