Хранилища Subversion ant

Редакция

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