1
<?php
2
/**
3
 *   File functions:
4
 *   Library with players texts
5
 *
6
 *   @name                 : library.php
7
 *   @copyright            : (C) 2004,2005,2006, 2007 Orodlin Team based on Gamers-Fusion ver 2.5
8
 *   @author               : thindil <thindil@users.sourceforge.net>
9
 *   @author               : Zamareth <zamareth@gmail.com>
10
 *   @version              : 1.3
11
 *   @since                : 24.08.2007
12
 *
13
 */
14
15
//
16
//
17
//       This program is free software; you can redistribute it and/or modify
18
//   it under the terms of the GNU General Public License as published by
19
//   the Free Software Foundation; either version 2 of the License, or
20
//   (at your option) any later version.
21
//
22
//   This program is distributed in the hope that it will be useful,
23
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
24
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
//   GNU General Public License for more details.
26
//
27
//   You should have received a copy of the GNU General Public License
28
//   along with this program; if not, write to the Free Software
29
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30
//
31
// $Id: library.php 566 2006-09-13 09:31:08Z thindil $
32
33
$title = 'Biblioteka';
34
require_once('includes/head.php');
35
36
/**
37
* Get the localization for game
38
*/
39
require_once('languages/'.$player -> lang.'/library.php');
40
41
if ($player -> location != 'Altara' && $player -> location != 'Ardulith')
42
{
43
    error (ERROR);
44
}
45
46
$strQuery = 'lang=\''.$player -> lang.'\'';
47
if ($player -> lang != $player -> seclang)
48
{
49
    $strQuery .= ' OR lang=\''.$player -> seclang.'\'';
50
}
51
52
function polishgrammar($intNumber)
53
{
54
    if( $intNumber == 1)
55
    {
56
        return TEXT1;
57
    }
58
    if( $intNumber > 4 && $intNumber < 22)
59
    {
60
        return TEXT3;
61
    }
62
    if( ($intNumber % 10 > 1) && ($intNumber % 10 < 5))
63
    {
64
        return TEXT2;
65
    }
66
    return TEXT3;
67
}
68
/**
69
* Main menu
70
*/
71
if (!isset($_GET['step']))
72
{
73
    $oldFetchMode = $db -> SetFetchMode(ADODB_FETCH_NUM);
74
    $arrTextNot = $db -> GetRow('SELECT count(id) FROM `library` WHERE `added`=\'N\' AND '.$strQuery);
75
    $arrTales = $db -> GetRow('SELECT count(id) FROM `library` WHERE `type`=\'tale\' AND `added`=\'Y\' AND '.$strQuery);
76
    $arrPoetry = $db -> GetRow('SELECT count(id) FROM `library` WHERE `type`=\'poetry\' AND `added`=\'Y\' AND '.$strQuery);
77
    $db -> SetFetchMode($oldFetchMode);
78
    $strWait = WAIT1;
79
    if($arrTextNot[0] > 1 && $arrTextNot[0] < 5 || $arrTextNot[0] > 20 & ($arrTextNot[0] % 10 > 1 && $arrTextNot[0] % 10< 5) )
80
    {
81
        $strWait = WAIT2;
82
    }
83
    $intTextIn = $arrTales[0] + $arrPoetry[0];
84
    $smarty -> assign('TextNot', $arrTextNot[0].' '.polishgrammar($arrTextNot[0]));
85
    $smarty -> assign_by_ref ('TextIn', $intTextIn);
86
    $smarty -> assign_by_ref ('AmountTales', $arrTales[0]);
87
    $smarty -> assign_by_ref ('AmountPoetry', $arrPoetry[0]);
88
    $smarty -> assign('Welcome3', polishgrammar($intTextIn).', '.WELCOME3.' '.$strWait);
89
    $smarty -> assign('Tales_Text', polishgrammar($arrTales[0]));
90
    $smarty -> assign('Poetry_Text', polishgrammar($arrPoetry[0]));
91
    unset($arrTales, $arrPoetry, $arrTextNot);
92
}
93
/**
94
* Add text to library (simple user)
95
*/
96
if (isset($_GET['step']) && $_GET['step'] == 'add')
97
{
98
    $path = 'languages/';
99
    $dir = opendir($path);
100
    $arrLanguage = array();
101
    while ($file = readdir($dir))
102
    {
103
        if (preg_match("/\b[a-z]{2}\b/", $file))
104
        {
105
            $arrLanguage[] = $file;
106
        }
107
    }
108
    closedir($dir);
109
    $arrType = array(T_TYPE1, T_TYPE2);
110
    $smarty -> assign_by_ref ('Llang', $arrLanguage);
111
112
    if (isset($_GET['step2']))
113
    {
114
        if (!in_array($_POST['type'], $arrType))
115
        {
116
            error(ERROR);
117
        }
118
        if (empty($_POST['ttitle']) || empty($_POST['body']))
119
        {
120
            error(EMPTY_FIELDS);
121
        }
122
        $strType = $_POST['type'] == T_TYPE1 ? 'tale' : 'poetry';
123
        require_once('includes/bbcode.php');
124
        $_POST['body'] = censorship($_POST['body']);
125
        $_POST['body'] = bbcodetohtml($_POST['body']);
126
        $_POST['ttitle'] = censorship($_POST['ttitle']);
127
        $strTitle = $db -> qstr($_POST['ttitle'], get_magic_quotes_gpc());
128
        $strBody = $db -> qstr($_POST['body'], get_magic_quotes_gpc());
129
        $db -> Execute('INSERT INTO `library` (`title`, `body`, `type`, `lang`, `author`, `author_id`) VALUES('.$strTitle.', '.$strBody.', \''.$strType.'\', \''.$_POST['lang'].'\', \''.$player -> user.'\', '.$player -> id.')');
130
        error(YOU_ADD);
131
    }
132
}
133
134
/**
135
* Add text to library (librarian)
136
*/
137
if (isset($_GET['step']) && $_GET['step'] == 'addtext')
138
{
139
    if ($player -> rank != 'Bibliotekarz' && $player -> rank != 'Admin')
140
    {
141
        error(NO_PERM);
142
    }
143
    $oldFetchMode = $db -> SetFetchMode(ADODB_FETCH_NUM);
144
    $arrText = $db -> GetAll('SELECT `id`, `title`, `author`, `author_id` FROM `library` WHERE added=\'N\' AND '.$strQuery);
145
    $db -> SetFetchMode($oldFetchMode);
146
    $smarty -> assign_by_ref ('TextList', $arrText);
147
    unset($arrText);
148
    /**
149
    * Modify text
150
    */
151
    if (isset($_GET['action']) && $_GET['action'] == 'modify')
152
    {
153
        if (!preg_match("/^[1-9][0-9]*$/", $_GET['text']))
154
        {
155
            error(ERROR);
156
        }
157
        $oldFetchMode = $db -> SetFetchMode(ADODB_FETCH_NUM);
158
        $arrText = $db -> GetRow('SELECT `id`, `title`, `body`, `type` FROM `library` WHERE `id`='.$_GET['text']);
159
        $db -> SetFetchMode($oldFetchMode);
160
        require_once('includes/bbcode.php');
161
        $arrText[2] = htmltobbcode($arrText[2]);
162
        $smarty -> assign_by_ref ('TextToModify', $arrText);
163
        if (isset($_POST['tid']))
164
        {
165
            if (!preg_match("/^[1-9][0-9]*$/", $_POST['tid']))
166
            {
167
                error(ERROR);
168
            }
169
            if (empty($_POST['ttitle']) || empty($_POST['body']))
170
            {
171
                error(EMPTY_FIELDS);
172
            }
173
            $strType = $_POST['type'] == T_TYPE1 ? 'tale' : 'poetry';
174
            $_POST['body'] = bbcodetohtml($_POST['body']);
175
            $strTitle = $db -> qstr($_POST['ttitle'], get_magic_quotes_gpc());
176
            $strBody = $db -> qstr($_POST['body'], get_magic_quotes_gpc());
177
            $db -> Execute('UPDATE `library` SET `title`='.$strTitle.', `body`='.$strBody.', `type`=\''.$strType.'\' WHERE `id`='.$_POST['tid']);
178
            error (MODIFIED);
179
        }
180
    }
181
    /**
182
    * Add or delete texts in library
183
    */
184
    if (isset($_GET['action']) && ($_GET['action'] == 'add' || $_GET['action'] == 'delete'))
185
    {
186
        if (!preg_match("/^[1-9][0-9]*$/", $_GET['text']))
187
        {
188
            error(ERROR);
189
        }
190
        $arrText = $db -> GetRow('SELECT `id` FROM `library` WHERE `id`='.$_GET['text']);
191
        if (!$arrText['id'])
192
        {
193
            error(NO_TEXT);
194
        }
195
        if ($_GET['action'] == 'add')
196
        {
197
            $db -> Execute('UPDATE `library` SET `added`=\'Y\' WHERE `id`='.$_GET['text']);
198
            error(ADDED);
199
        }
200
            else
201
        {
202
            $db -> Execute('DELETE FROM `library` WHERE `id`='.$_GET['text']);
203
            error(DELETED);
204
        }
205
    }
206
}
207
208
/**
209
* Display texts in library
210
*/
211
if (isset($_GET['step']) && ($_GET['step'] == 'tales' || $_GET['step'] == 'poetry'))
212
{
213
    if ($_GET['step'] == 'tales')
214
    {
215
        $strType = 'tale';
216
        $strInfo = T_INFO1;
217
    }
218
    else
219
    {
220
        $strType = 'poetry';
221
        $strInfo = T_INFO2;
222
    }
223
    $oldFetchMode = $db -> SetFetchMode(ADODB_FETCH_NUM);
224
    $arrAmount = $db -> GetRow('SELECT count(id) FROM `library` WHERE `type`=\''.$strType.'\' AND `added`=\'Y\' AND '.$strQuery);
225
    $db -> SetFetchMode($oldFetchMode);
226
    $smarty -> assign_by_ref ('TextAmount', $arrAmount[0]);
227
    $smarty -> assign_by_ref ('TextType', $strInfo);
228
    unset($arrAmount);
229
    /**
230
    * Display sorted texts
231
    */
232
    if (isset($_POST['sort']) && $_POST['sort'] != 'author')
233
    {
234
        $arrSort = array('title', 'id');
235
        if (!in_array($_POST['sort'], $arrSort))
236
        {
237
            error(ERROR);
238
        }
239
        $oldFetchMode = $db -> SetFetchMode(ADODB_FETCH_NUM);
240
        $arrList = $db -> GetAll('SELECT `id`, `title`, `author`, `author_id` FROM `library` WHERE `added`=\'Y\' AND `type`=\''.$strType.'\' AND '.$strQuery.' ORDER BY '.$_POST['sort'].' DESC') or error($db -> ErrorMsg());
241
        for($i = 0, $k = count($arrList); $i < $k; $i++)
242
        {
243
            $arrCommentAmount = $db -> GetRow('SELECT count(id) FROM `lib_comments` WHERE `textid`='.$arrList[$i][0]);
244
            $arrComments[] = $arrCommentAmount[0];
245
        }
246
        $db -> SetFetchMode($oldFetchMode);
247
        $smarty -> assign_by_ref ('TextList', $arrList);
248
        $smarty -> assign_by_ref ('Comments', $arrComments);
249
        unset($arrList, $arrComments);
250
    }
251
    /**
252
    * Display authors list
253
    */
254
    if (!isset($_GET['author']) && (!isset($_POST['sort']) || (isset($_POST['sort']) && $_POST['sort'] == 'author')))
255
    {
256
        $oldFetchMode = $db -> SetFetchMode(ADODB_FETCH_NUM);
257
        $arrAuthor = $db -> GetAll('SELECT DISTINCT `author`, `author_id`, count(id) FROM `library` WHERE `added`=\'Y\' AND `type`=\''.$strType.'\' AND '.$strQuery.' GROUP BY `author_id`') or error($db -> ErrorMsg());
258
        $db -> SetFetchMode($oldFetchMode);
259
        $smarty -> assign_by_ref ('Tauthor', $arrAuthor);
260
        unset($arrAuthor);
261
    }
262
    /**
263
    * Display texts selected author
264
    */
265
    if (isset($_GET['author']))
266
    {
267
        if (!preg_match("/^[1-9][0-9]*$/", $_GET['author']))
268
        {
269
            error(ERROR);
270
        }
271
        $oldFetchMode = $db -> SetFetchMode(ADODB_FETCH_NUM);
272
        $arrAuthor = $db -> GetAll('SELECT DISTINCT `author`, `author_id` FROM `library` WHERE `author_id`='.$_GET['author']) or ($db -> ErrorMsg());
273
        for ($i = 0, $k = count($arrAuthor); $i < $k; $i++)
274
        {
275
            $arrTexts[] = $arrList = $db -> GetAll('SELECT `id`, `title` FROM `library` WHERE `added`=\'Y\' AND `type`=\''.$strType.'\' AND `author`=\''.$arrAuthor[$i][0].'\' AND '.$strQuery.' ORDER BY `id` DESC') or error($db -> ErrorMsg());
276
            for ($m = 0, $n = count($arrList); $m < $n; $m++)
277
            {
278
                $arrComments[] = $arrAmount = $db -> GetRow('SELECT count(id) FROM `lib_comments` WHERE `textid`='.$arrList[$m][0]);
279
            }
280
        }
281
        $db -> SetFetchMode($oldFetchMode);
282
        $smarty -> assign_by_ref ('AuthorList', $arrAuthor);
283
        $smarty -> assign_by_ref ('TextsList', $arrTexts);
284
        $smarty -> assign_by_ref ('CommentsAmount', $arrComments);
285
        unset($arrAuthors, $arrTexts, $arrComments);
286
    }
287
    /**
288
    * Display selected text
289
    */
290
    if (isset($_GET['text']))
291
    {
292
        if (!preg_match("/^[1-9][0-9]*$/", $_GET['text']))
293
        {
294
            error(ERROR);
295
        }
296
        $oldFetchMode = $db -> SetFetchMode(ADODB_FETCH_NUM);
297
        $arrText = $db -> GetRow('SELECT `id`, `title`, `body`, `author`, `author_id` FROM `library` WHERE id='.$_GET['text']);
298
        $db -> SetFetchMode($oldFetchMode);
299
        if (!$arrText[0])
300
        {
301
            error(NO_TEXT);
302
        }
303
        $smarty -> assign_by_ref ('TextData', $arrText);
304
        unset($arrText);
305
    }
306
}
307
308
/**
309
* Comments to text
310
*/
311
if (isset($_GET['step']) && $_GET['step'] == 'comments')
312
{
313
    $smarty -> assign('Amount', '');
314
315
    require_once('includes/comments.php');
316
    /**
317
    * Display comments
318
    */
319
    if (!isset($_GET['action']))
320
    {
321
        displaycomments($_GET['text'], 'library', 'lib_comments', 'textid');
322
        $smarty -> assign(array('Tauthor' => $arrAuthor,
323
            'Tbody' => $arrBody,
324
            'Amount' => $i,
325
            'Cid' => $arrId,
326
            'Tdate' => $arrDate));
327
    }
328
329
    /**
330
    * Add comment
331
    */
332
    if (isset($_GET['action']) && $_GET['action'] == 'add')
333
    {
334
        addcomments($_POST['tid'], 'lib_comments', 'textid');
335
        $smarty -> assign('Message', '<p>'.BACK_TO.' <a hfef="library.php?step=tales&text='.$_POST['tid'].'">'.A_TEXT.''.I_OR.' <a hfef="library.php?step=comments&text='.$_POST['tid'].'">'.ACOMMENTS.'</a>.</p>');
336
        $smarty -> display('error1.tpl');
337
    }
338
339
    /**
340
    * Delete comment
341
    */
342
    if (isset($_GET['action']) && $_GET['action'] == 'delete')
343
    {
344
        deletecomments('lib_comments');
345
    }
346
}
347
348
/**
349
* Initialization of variable
350
*/
351
if (!isset($_GET['step']))
352
{
353
    $_GET['step'] = '';
354
}
355
if (!isset($_GET['action']))
356
{
357
    $_GET['action'] = '';
358
}
359
if (!isset($_GET['text']))
360
{
361
    $_GET['text'] = '';
362
}
363
if (!isset($_GET['author']))
364
{
365
    $_GET['author'] = '';
366
}
367
if (!isset($_POST['sort']))
368
{
369
    $_POST['sort'] = '';
370
}
371
372
/**
373
* Assign variables to template and display page
374
*/
375
$smarty -> assign_by_ref ('Rank', $player -> rank);
376
$smarty -> assign_by_ref ('Step', $_GET['step']);
377
$smarty -> assign_by_ref ('Action', $_GET['action']);
378
$smarty -> assign_by_ref ('Text', $_GET['text']);
379
$smarty -> assign_by_ref ('Author', $_GET['author']);
380
$smarty -> assign_by_ref ('Sort', $_POST['sort']);
381
$smarty -> display ('library.tpl');
382
383
require_once('includes/foot.php');
384
?>