Редакция 423 | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | 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 | // Получение и отображение списка дистрибутивов |
423 | alex-w | 84 | // XXX deprecated ? |
393 | alex-w | 85 | public function showDistributionList($name, $heads = "", $info = "", $format = 'html') { |
315 | alex-w | 86 | $query = "SELECT * FROM ".$this->prefix."distribution ORDER BY dist_id ASC"; |
317 | alex-w | 87 | $rq =& $this->db->query($query); |
315 | alex-w | 88 | switch ($format) { |
89 | case 'html': |
||
394 | alex-w | 90 | $show = "<fieldset><legend>".$heads."</legend>\n<select id='".$name."' name='".$name."'>\n"; |
390 | alex-w | 91 | $show .= "<option value=''>".$info."</option>\n"; |
315 | alex-w | 92 | while ($rq->fetchInto($element)) { |
93 | $show .= "<option value='".$this->secure->checkInt($element["dist_id"])."'>".$this->secure->checkStr($element["distname"],1)."</option>\n"; |
||
94 | } |
||
317 | alex-w | 95 | $show .= "</select></fieldset>"; |
315 | alex-w | 96 | break; |
97 | case 'json': |
||
317 | alex-w | 98 | $show = '[{value:"",text:"'.$info.'"}'; |
315 | alex-w | 99 | while ($rq->fetchInto($element)) { |
100 | $show .= ',{value:"'.$this->secure->checkInt($element["dist_id"]).'",text:"'.$this->secure->checkStr($element["distname"],1).'"}'; |
||
101 | } |
||
102 | $show .= ']'; |
||
103 | break; |
||
104 | } |
||
105 | return $show; |
||
106 | } |
||
107 | |||
108 | // Получение названия дистрибутива |
||
368 | alex-w | 109 | public function getDistName($distID) { |
315 | alex-w | 110 | $result = array(); |
111 | $query = "SELECT distname FROM ".$this->prefix."distribution WHERE dist_id='".$this->secure->checkInt($distID)."'"; |
||
112 | $rq =& $this->db->query($query); |
||
113 | if (PEAR::isError($this->db)) { |
||
114 | $result["ERR"] = 1; |
||
115 | $result["ERRINFO"] = $this->db->getMessage(); |
||
116 | } else { |
||
117 | $rq->fetchInto($element); |
||
118 | $result["ERR"] = 0; |
||
119 | $result["DistName"] = $this->secure->checkStr($element["distname"],1); |
||
120 | } |
||
121 | |||
122 | return $result; |
||
123 | } |
||
124 | |||
383 | alex-w | 125 | // Получение названия программы, ее версии и описания |
126 | public function getEngineAttr($attr = 'codename') { |
||
127 | $cname = $this->getOption($attr); |
||
382 | alex-w | 128 | return $this->secure->checkStr($cname["OptValue"],1); |
381 | alex-w | 129 | } |
130 | |||
315 | alex-w | 131 | // Получение и отображение списка версий дистрибутива |
368 | alex-w | 132 | public function showDistVersionsList($name, $distID, $format = 'html') { |
316 | alex-w | 133 | $distname = $this->getDistName($distID); |
315 | alex-w | 134 | $query = "SELECT * FROM ".$this->prefix."version WHERE dist_id='".$this->secure->checkInt($distID)."' ORDER BY version ASC"; |
317 | alex-w | 135 | $rq =& $this->db->query($query); |
315 | alex-w | 136 | switch ($format) { |
137 | case 'html': |
||
394 | alex-w | 138 | $show = "<fieldset><legend>Версии ".$distname["DistName"]."</legend>\n<select id='".$name."' name='".$name."'>\n"; |
139 | $show .= "<option value=''>Выбери версию ".$distname["DistName"]."</option>\n"; |
||
315 | alex-w | 140 | while ($rq->fetchInto($element)) { |
316 | alex-w | 141 | $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 | 142 | } |
317 | alex-w | 143 | $show .= "</select></fieldset>"; |
315 | alex-w | 144 | break; |
145 | case 'json': |
||
317 | alex-w | 146 | $show = '[{value:"",text:"Выбери версию '.$distname["DistName"].'"}'; |
315 | alex-w | 147 | while ($rq->fetchInto($element)) { |
316 | alex-w | 148 | $show .= ',{value:"'.$this->secure->checkInt($element["version_id"]).'",text:"'.$this->secure->checkStr($element["version"],1).' '.$this->secure->checkStr($element["vname"],1).'"}'; |
315 | alex-w | 149 | } |
150 | $show .= ']'; |
||
151 | break; |
||
152 | } |
||
153 | return $show; |
||
154 | } |
||
155 | |||
317 | alex-w | 156 | // Получение и отображение списка секций основного (официального) репозитория |
368 | alex-w | 157 | public function showBranchesList($version, $format = 'html') { |
317 | alex-w | 158 | $query = "SELECT rtype FROM ".$this->prefix."rtype WHERE rtype_id='1'"; |
159 | $rq =& $this->db->query($query); |
||
160 | $rq->fetchInto($types); |
||
161 | $query = "SELECT s.*,t.rtype FROM ".$this->prefix."section s "; |
||
162 | $query .= "JOIN ".$this->prefix."sect2rep l ON s.sect_id=l.sect_id "; |
||
163 | $query .= "JOIN ".$this->prefix."repository r ON r.rep_id=l.rep_id "; |
||
164 | $query .= "JOIN ".$this->prefix."rtype t ON r.rtype_id=t.rtype_id "; |
||
165 | $query .= "WHERE t.rtype_id='1' AND r.version='".$this->secure->checkInt($version)."'"; |
||
166 | $rq =& $this->db->query($query); |
||
167 | switch ($format) { |
||
168 | case 'html': |
||
169 | $show = "<fieldset><legend>".$this->secure->checkStr($types["rtype"],1)."</legend>\n"; |
||
170 | while ($rq->fetchInto($element)) { |
||
320 | alex-w | 171 | $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 | 172 | } |
173 | $show .= "</fieldset>\n"; |
||
174 | break; |
||
175 | case 'json': |
||
176 | //TODO Доделать JSON-вывод списка секций основного репозитория |
||
177 | break; |
||
178 | } |
||
318 | alex-w | 179 | |
180 | return $show; |
||
317 | alex-w | 181 | } |
182 | |||
183 | // Получение и отображение списка репозиториев |
||
368 | alex-w | 184 | public function showRepList($version, $reptype, $format = 'html') { |
406 | alex-w | 185 | $query = "SELECT rtype FROM ".$this->prefix."rtype WHERE rtype_id='".$this->secure->checkInt($reptype)."'"; |
317 | alex-w | 186 | $rq =& $this->db->query($query); |
187 | $rq->fetchInto($types); |
||
188 | $query = "SELECT * FROM ".$this->prefix."repository WHERE version='".$this->secure->checkInt($version)."' AND rtype_id='".$this->secure->checkInt($reptype)."'"; |
||
189 | $rq =& $this->db->query($query); |
||
190 | switch ($format) { |
||
191 | case 'html': |
||
192 | $show = "<fieldset><legend>".$this->secure->checkStr($types["rtype"],1)."</legend>\n"; |
||
193 | while ($rq->fetchInto($types)) { |
||
320 | alex-w | 194 | $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 | 195 | } |
196 | $show .= "</fieldset>\n"; |
||
197 | break; |
||
198 | case 'json': |
||
199 | //TODO Доделать JSON-вывод списка репозиториев |
||
200 | break; |
||
201 | } |
||
318 | alex-w | 202 | |
203 | return $show; |
||
317 | alex-w | 204 | } |
205 | |||
321 | alex-w | 206 | // Добавление поддержки нового apt-дистрибутива |
368 | alex-w | 207 | public function addDistribution($distname, $disttype, $distua = 1, $distlogo = 0) { |
321 | alex-w | 208 | $result = array(); |
209 | $sDName = $this->secure->checkStr($distname); |
||
210 | $sDType = $this->secure->checkInt($disttype); |
||
211 | $sDUAgt = $this->secure->checkStr($distua); |
||
212 | $sDLogo = $this->secure->checkInt($distname); |
||
213 | |||
214 | $query = "INSERT INTO ".$this->prefix."distribution SET distname='".$sDName."', distua='".$sDUAgt."', disttype='".$sDType."', distlogo='".$sDLogo."'"; |
||
215 | $rq =& $this->db->query($query); |
||
216 | if (PEAR::isError($this->db)) { |
||
217 | $result["ERR"] = 1; |
||
218 | $result["ERRINFO"] = $this->db->getMessage(); |
||
219 | } else { |
||
220 | $rq->fetchInto($element); |
||
221 | $result["ERR"] = 0; |
||
222 | } |
||
223 | |||
224 | return $result; |
||
225 | } |
||
226 | |||
356 | alex-w | 227 | // Добавление поддержки новой версии apt-дистрибутива |
368 | alex-w | 228 | public function addDistVersion($distID, $version, $vname = "", $vcodename = "") { |
356 | alex-w | 229 | $result = array(); |
230 | $sDistID = $this->secure->checkStr($distID); |
||
231 | $sDVersion = $this->secure->checkStr($version); |
||
232 | $sDVName = $this->secure->checkStr($vname); |
||
233 | $sDVCName = $this->secure->checkInt($vcodename); |
||
234 | |||
235 | $query = "INSERT INTO ".$this->prefix."version SET dist_id='".$sDistID."', vname='".$sDVName."', version='".$sDVersion."', vcodename='".$sDVCName."'"; |
||
236 | $rq =& $this->db->query($query); |
||
237 | if (PEAR::isError($this->db)) { |
||
238 | $result["ERR"] = 1; |
||
239 | $result["ERRINFO"] = $this->db->getMessage(); |
||
240 | } else { |
||
241 | $rq->fetchInto($element); |
||
242 | $result["ERR"] = 0; |
||
243 | } |
||
244 | |||
245 | return $result; |
||
246 | } |
||
358 | alex-w | 247 | |
329 | alex-w | 248 | // Отображение типа дистрибутива |
368 | alex-w | 249 | public function showDistTypeForm($name = "dtype",$type = 0) { |
329 | alex-w | 250 | $query = "SELECT * FROM ".$this->prefix."dtype"; |
251 | $rq =& $this->db->query($query); |
||
252 | $show = "<select name='".$name."' id='".$name."'>\n"; |
||
253 | while ($rq->fetchInto($element)) { |
||
347 | alex-w | 254 | if ($element["type_id"] == $type) { |
255 | $show .= "<option value='".$element["type_id"]."' selected>".$this->secure->checkStr($element["type"],1)."</option>\n"; |
||
329 | alex-w | 256 | } else { |
347 | alex-w | 257 | $show .= "<option value='".$element["type_id"]."'>".$this->secure->checkStr($element["type"],1)."</option>\n"; |
329 | alex-w | 258 | } |
259 | } |
||
260 | $show .= "</select>"; |
||
261 | |||
262 | return $show; |
||
263 | } |
||
264 | |||
358 | alex-w | 265 | // Отображение формы создания и редактирования apt-дистрибутива |
368 | alex-w | 266 | public function showDistributionForm($distID = 0) { |
329 | alex-w | 267 | $sDistID = $this->secure->checkInt($distID); |
268 | if ($sDistID != 0) { |
||
269 | // Режим редактирования |
||
270 | $query = "SELECT * FROM ".$this->prefix."distribution WHERE dist_id='".$sDistID."'"; |
||
271 | $rq =& $this->db->query($query); |
||
272 | $rq->fetchInto($element); |
||
273 | } |
||
274 | |||
275 | if ($element["distlogo"] == 1) { |
||
380 | alex-w | 276 | $image = "<img src='./img/d/".$this->secure->checkStr($element["distua"],1).".png' width='32' height='32' id='adm-dist-logo' alt='Логотип дистрибутива ".$this->secure->checkStr($element["distname"],1)."' title='Логотип дистрибутива ".$this->secure->checkStr($element["distname"],1)."'>"; |
329 | alex-w | 277 | } else { |
380 | alex-w | 278 | $image = "<img src='./img/d/empty-logo.png' width='32' height='32' id='adm-dist-logo' alt='Логотип дистрибутива' title='Логотип дистрибутива не загружен'>"; |
329 | alex-w | 279 | } |
280 | |||
281 | $show = "<fieldset><legend>Дистрибутив</legend>\n"; |
||
350 | alex-w | 282 | $show .= "<div class='inputbox'><label for='dname'>Название дистрибутива:</label> <input type='text' name='dname' id='dname' value='".$this->secure->checkStr($element["distname"],1)."'></div>\n"; |
283 | $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"; |
||
284 | $show .= "<div class='inputbox'><label for='dtype'>Тип дистрибутива:</label> ".$this->showDistTypeForm("dtype",$element["dtype_id"])."</div>\n"; |
||
348 | alex-w | 285 | $show .= "<div class='inputbox'><table><tr><td class='td-name'>Логотип дистрибутива:</td>\n"; |
329 | alex-w | 286 | $show .= "<td>".$image."</td>\n<td><input type='file' name='distlogo'></td>\n</tr></table>\n</div>\n"; |
350 | alex-w | 287 | $show .= "<div class='inputbox'><input type='submit' value=' Отправить данные '></div>\n</fieldset>\n"; |
329 | alex-w | 288 | |
289 | return $show; |
||
290 | } |
||
291 | |||
358 | alex-w | 292 | // sourses.list |
423 | alex-w | 293 | public function showSourcesList($distID,$versID,$sectIDs,$repIDs) { |
294 | //TODO Написать генератор sources.list |
||
358 | alex-w | 295 | } |
296 | |||
359 | alex-w | 297 | // Проверка пароля (из формы авторизации) |
368 | alex-w | 298 | public function checkSign($word) { |
359 | alex-w | 299 | $result = array(); |
300 | |||
301 | $sHash = $this->secure->encryptStr($word); |
||
302 | $pwd = $this->getOption("passwd"); |
||
303 | if ($sHash == $pwd["OptValue"]) { |
||
304 | $result["ERR"] = 0; |
||
305 | $result["Location"] = "manager.php"; |
||
306 | setcookie($this->cookie, $sHash); |
||
307 | } else { |
||
308 | $result["ERR"] = 1; |
||
309 | $result["ERRINFO"] = "Password not valid"; |
||
368 | alex-w | 310 | $result["Location"] = "manager.php?error=1"; |
359 | alex-w | 311 | } |
312 | |||
313 | return $result; |
||
358 | alex-w | 314 | } |
357 | alex-w | 315 | |
359 | alex-w | 316 | // Проверка пароля (из cookies) |
368 | alex-w | 317 | public function checkCookieSign($hash) { |
359 | alex-w | 318 | $result = array(); |
319 | |||
320 | $pwd = $this->getOption("passwd"); |
||
321 | if ($hash == $pwd["OptValue"]) { |
||
322 | $result["ERR"] = 0; |
||
323 | } else { |
||
324 | $result["ERR"] = 1; |
||
325 | $result["ERRINFO"] = "Hash not valid"; |
||
368 | alex-w | 326 | $result["Location"] = "manager.php"; |
359 | alex-w | 327 | } |
328 | |||
329 | return $result; |
||
330 | } |
||
331 | |||
368 | alex-w | 332 | // Форма ввода пароля |
333 | public function showSigninForm() { |
||
425 | alex-w | 334 | $show = "<div id='regform'>"; |
335 | $show .= "<form action='process.php' method='post'>\n"; |
||
368 | alex-w | 336 | $show .= "<fieldset><legend>Пароль</legend>\n"; |
337 | $show .= "<input type='hidden' name='mode' value='authorize'>\n"; |
||
425 | alex-w | 338 | $show .= "<input type='text' name='word' value=''>\n"; |
368 | alex-w | 339 | $show .= "<input type='submit' value=' Войти '>\n"; |
425 | alex-w | 340 | $show .= "</fieldset>\n</form></div>\n"; |
368 | alex-w | 341 | |
342 | return $show; |
||
343 | } |
||
344 | |||
359 | alex-w | 345 | // Обновление пароля |
368 | alex-w | 346 | public function updatePassword($word1, $word2) { |
359 | alex-w | 347 | $result = array(); |
348 | |||
349 | if ($word1 == $word2) { |
||
350 | $sWord = $this->secure->encryptStr($word1); |
||
351 | $r = $this->setOption("passwd", $sWord); |
||
352 | $result = $r; |
||
353 | } else { |
||
354 | $result["ERR"] = 1; |
||
355 | $result["ERRINFO"] = "Passwords is mismatch"; |
||
356 | } |
||
357 | |||
358 | return $result; |
||
359 | } |
||
360 | |||
390 | alex-w | 361 | |
304 | alex-w | 362 | } |
363 | |||
356 | alex-w | 364 | ?> |