Хранилища Subversion ant

Редакция

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