Implement url check via AJAX
[infodrom.org/www.zeitungsliste.de] / lib / board.inc
1 <?php
2
3 // Precondition: $body is already sanitised
4 function article_add($topic, $body)
5 {
6   global $_SESSION;
7   global $_SERVER;
8
9   $query = sprintf("INSERT INTO article (topic,uid,status,body,created,remote_addr) ".
10                    "VALUES (%d,%d,%d,'%s',now(),'%s')",
11                    $topic,  $_SESSION['uid'], 1, pg_escape_string($body),
12                    $_SERVER['REMOTE_ADDR']);
13
14   $sth = db_query($query);
15   if ($sth === false)
16     return false;
17
18   return true;
19 }
20
21 function topic_add($zid, $topic, $body)
22 {
23   db_query('BEGIN TRANSACTION');
24
25   $query = sprintf("INSERT INTO topics (zeitung,topic,created,modified) ".
26                    "VALUES (%d,'%s',now(),now())",
27                    $zid, $topic);
28
29   $sth = db_query($query);
30   if ($sth === false) {
31     db_query('ROLLBACK');
32     return false;
33   }
34
35   $id = db_last_id('topics', 'id');
36   if ($id === false) {
37     db_query('ROLLBACK');
38     return false;
39   }
40
41   if (article_add($id, $body) === false) {
42     db_query('ROLLBACK');
43     return false;
44   }
45
46   db_query('COMMIT');
47
48   return $id;
49 }
50
51 function process_topic()
52 {
53   global $cfg;
54   global $zlist;
55   global $_POST;
56   global $_GET;
57
58   if (isset($_GET['zeitung']) && is_numeric($_GET['zeitung']))
59     $zid = $_GET['zeitung'];
60   elseif (isset($_POST['zeitung']) && is_numeric($_POST['zeitung']))
61     $zid = $_POST['zeitung'];
62   else
63     $ret = warning('Keine Zeitung oder Magazin gefunden!');
64
65   if (isset($zid)) {
66     $try = format_newspaper($zid, true);
67     if ($try === false) {
68       $ret .= warning('Keine Zeitung oder Magazin gefunden!');
69       unset($zid);
70     } else
71       $ret .= $try;
72   }
73
74   if (isset($zid)) {
75     if (!isset($_SESSION['uid']))
76       $ret .= warning('Um eine neue Diskussion zu beginnen, müssen Sie angemeldet sein.');
77     else {
78       $replace = array('zeitung' => $zid);
79       if (!isset($_POST['zeitung'])) {
80         $ret .= information('Sie beginnen eine neue Diskussion im Forum.');
81       } else {
82         $replace['topic'] = htmlspecialchars($_POST['topic']);
83         $replace['body'] = string_sanitise($_POST['body']);
84
85         if (strlen($_POST['topic'])) {
86           if (strlen($_POST['body'])) {
87             if (($tid = topic_add($zid, $replace['topic'], $replace['body'])) !== false) {
88               header(sprintf('Location: %stopic/%d.html', $cfg['home'], $tid));
89             } else
90               $ret .= warning('Es ist ein Datenbankfehler aufgetreten.');
91           } else
92             $ret .= warning('Sie haben keinen Text für die Diskussion angegeben!');
93         } else
94           $ret .= warning('Sie haben keinen Titel für die Diskussion angegeben!');
95       }
96
97       $ret .= load_javascript('topic.js') . load_template('topic.html', $replace);
98     }
99   }
100
101   return $ret;
102 }
103
104 // Sideeffect: May set $zlist['redirect']
105 function process_reply()
106 {
107   global $cfg;
108   global $zlist;
109   global $_GET;
110   global $_POST;
111
112   if (isset($_GET['topic']) && is_numeric($_GET['topic']))
113     $topic = $_GET['topic'];
114   elseif (isset($_POST['topic']) && is_numeric($_POST['topic']))
115     $topic = $_POST['topic'];
116   else
117     $ret = warning('Keine passende Diskussion gefunden!');
118
119   if (isset($topic)) {
120     $discussion = format_topic($topic);
121     if (isset($zlist['zid'])) {
122       $ret .= format_newspaper($zlist['zid'], true);
123
124       if (isset($_POST['body'])) {
125         if (article_add($topic, string_sanitise($_POST['body'])) === true) {
126           $ret .= information('Danke für Ihren Beitrag.  Sie werden automatisch zur Diskussion weitergeleitet.');
127           header(sprintf('Location: %stopic/%d.html', $cfg['home'], $topic));
128         } else {
129           $ret .= warning('Ihr Beitrag konnte nicht aufgenommen werden.');
130           $ret .= $discussion;
131           $replace = array('topic' => $topic,
132                            'title' => $zlist['topic'],
133                            'body' => htmlspecialchars($_POST['body']));
134           $ret .= load_javascript('reply.js') . load_template('reply.html', $replace);
135         }
136       } else {
137         $ret .= $discussion;
138         $replace = array('topic' => $topic,
139                          'title' => $zlist['topic']);
140         $ret .= load_javascript('reply.js') . load_template('reply.html', $replace);
141       }
142     } else
143       $ret .= $discussion;
144   }
145
146   return $ret;
147 }
148
149 ?>