]> mj.ucw.cz Git - pciids.git/blob - PciIds/DBQ.pm
2740de554435fde765990c7a18e659325b425bdd
[pciids.git] / PciIds / DBQ.pm
1 #       PciIds web database
2 #       Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
3 #
4 #       This program is free software; you can redistribute it and/or modify
5 #       it under the terms of the GNU General Public License as published by
6 #       he Free Software Foundation; either version 2 of the License, or
7 #       (at your option) any later version.
8 #
9 #       This program is distributed in the hope that it will be useful,
10 #       but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #
13 #       GNU General Public License for more details.
14 #
15 #       You should have received a copy of the GNU General Public License
16 #       along with this program; if not, write to the Free Software
17 #       Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19 package PciIds::DBQ;
20 use strict;
21 use warnings;
22 use base 'PciIds::DBQAny';
23
24 my $adminDumpSql = 'SELECT
25                         locations.id, locations.name, locations.note, locations.mainhistory, musers.email, musers.login, main.discussion, main.time,
26                         history.id, history.discussion, history.nodename, history.nodenote, users.email, users.login, history.time
27                 FROM
28                         locations INNER JOIN history ON history.location = locations.id
29                         LEFT OUTER JOIN users ON history.owner = users.id
30                         LEFT OUTER JOIN history AS main ON locations.mainhistory = main.id
31                         LEFT OUTER JOIN users AS musers ON main.owner = musers.id
32                 WHERE history.seen = "0" AND locations.id LIKE ?
33                 ORDER BY locations.id, history.id
34                 LIMIT ';
35
36 sub new( $ ) {
37         my( $dbh ) = @_;
38         my $node = 'SELECT id, name, note, mainhistory FROM locations WHERE parent = ? ORDER BY ';
39         my $noder = 'SELECT id, name, note, mainhistory FROM locations WHERE parent = ? AND id LIKE ? ORDER BY ';
40         return bless PciIds::DBQAny::new( $dbh, {
41                 'nodes-id' => $node.'id',
42                 'nodes-name' => $node.'name',
43                 'nodes-rid' => $node.'id DESC',
44                 'nodes-rname' => $node.'name DESC',
45                 'nodes-id-r' => $noder.'id',
46                 'nodes-name-r' => $noder.'name',
47                 'nodes-rid-r' => $noder.'id DESC',
48                 'nodes-rname-r' => $noder.'name DESC',
49                 'item' => 'SELECT parent, name, note, mainhistory FROM locations WHERE id = ?',
50                 'login' => 'SELECT id FROM users WHERE login = ?',
51                 'email' => 'SELECT id FROM users WHERE email = ?',
52                 'adduser' => 'INSERT INTO users (login, email, passwd) VALUES(?, ?, ?)',
53                 'adduser-null' => 'INSERT users (email, passwd) VALUES(?, ?)',
54                 'loginfomail' => 'SELECT id, passwd, logtime, lastlog FROM users WHERE email = ?',
55                 'loginfoname' => 'SELECT id, passwd, logtime, lastlog, email FROM users WHERE login = ?',
56                 'resetinfo' => 'SELECT id, login, passwd FROM users WHERE email = ?',
57                 'changepasswd' => 'UPDATE users SET passwd = ? WHERE id = ?',
58                 'setlastlog' => 'UPDATE users SET logtime = now(), lastlog = ? WHERE id = ?',
59                 'rights' => 'SELECT rightId FROM rights WHERE userId = ?',
60                 'newitem' => 'INSERT INTO locations (id, parent) VALUES(?, ?)',
61                 'newhistory' => 'INSERT INTO history (location, owner, discussion, nodename, nodenote) VALUES(?, ?, ?, ?, ?)',
62                 'history' => 'SELECT history.id, history.discussion, history.time, history.nodename, history.nodenote, history.seen, users.login, users.email FROM history LEFT OUTER JOIN users ON history.owner = users.id WHERE history.location = ? ORDER BY history.time',
63                 'admindump' => "$adminDumpSql 100",#Dumps new discussion submits with their senders and corresponding main history and names
64                 'delete-hist' => 'DELETE FROM history WHERE id = ?',
65                 'mark-checked' => 'UPDATE history SET seen = 1 WHERE id = ?',
66                 'delete-item' => 'DELETE FROM locations WHERE id = ?',
67                 'set-mainhist' => 'UPDATE locations SET
68                                 mainhistory = ?,
69                                 name = ( SELECT nodename FROM history WHERE id = ? ),
70                                 note = ( SELECT nodenote FROM history WHERE id = ? )
71                         WHERE
72                                 id = ?',
73                 'profiledata' => 'SELECT email, xmpp, login, mailgather, xmppgather FROM users WHERE id = ?',
74                 'pushprofile' => 'UPDATE users SET xmpp = ?, login = ?, mailgather = ?, xmppgather = ? WHERE id = ?',
75                 'setemail' => 'UPDATE users SET email = ?, passwd = ? WHERE id = ?',
76                 'notifuser' => 'SELECT location, recursive FROM notifications WHERE user = ? ORDER BY location',
77                 'notifdata' => 'SELECT recursive, type, notification FROM notifications WHERE user = ? AND location = ?',
78                 'drop-notif' => 'DELETE FROM notifications WHERE user = ? AND location = ?',
79                 'new-notif' => 'INSERT INTO notifications (user, location, recursive, type, notification) VALUES (?, ?, ?, ?, ?)',
80                 'notify' => 'INSERT INTO pending (user, history, notification, reason) SELECT DISTINCT user, ?, ?, ? FROM notifications WHERE ( notification = 2 OR notification = ? ) AND type <= ? AND ( location = ? OR ( recursive = 1 AND SUBSTR( ?, 1, LENGTH( location ) ) = location ) )',
81                 'newtime-mail' => 'UPDATE users SET nextmail = FROM_UNIXTIME( UNIX_TIMESTAMP( NOW() ) + 60 * mailgather ) WHERE nextmail < NOW() AND EXISTS ( SELECT 1 FROM notifications WHERE ( notification = 0 OR notification = 2 ) AND type <= ? AND ( location = ? OR ( recursive = 1 AND SUBSTR( ?, 1, LENGTH( location ) ) = location ) ) )',
82                 'newtime-xmpp' => 'UPDATE users SET nextxmpp = FROM_UNIXTIME( UNIX_TIMESTAMP( NOW() ) + 60 * xmppgather ) WHERE nextxmpp < NOW() AND EXISTS ( SELECT 1 FROM notifications WHERE ( notification = 1 OR notification = 2 ) AND type <= ? AND ( location = ? OR ( recursive = 1 AND SUBSTR( ?, 1, LENGTH( location ) ) = location ) ) )',
83                 'mailout' => 'SELECT
84                                 pending.user, users.email,
85                                 pending.reason, history.discussion, history.nodename, history.nodenote, history.time,
86                                 auth.login, history.location, locations.name, locations.note
87                         FROM
88                                 pending
89                                 INNER JOIN users ON users.id = pending.user
90                                 INNER JOIN history ON history.id = pending.history
91                                 INNER JOIN locations ON history.location = locations.id
92                                 INNER JOIN users AS auth ON auth.id = history.owner
93                         WHERE
94                                 pending.notification = 0
95                                 AND users.nextmail <= ?
96                         ORDER BY
97                                 pending.user, pending.reason, history.time, history.location',
98                 'xmppout' => 'SELECT
99                                 pending.user, users.xmpp,
100                                 pending.reason, history.discussion, history.nodename, history.nodenote, history.time,
101                                 auth.login, history.location, locations.name, locations.note
102                         FROM
103                                 pending
104                                 INNER JOIN users ON users.id = pending.user
105                                 INNER JOIN history ON history.id = pending.history
106                                 INNER JOIN locations ON history.location = locations.id
107                                 INNER JOIN users AS auth ON auth.id = history.owner
108                         WHERE
109                                 pending.notification = 1
110                                 AND users.nextxmpp <= ?
111                                 AND users.xmpp IS NOT NULL
112                         ORDER BY
113                                 pending.user, pending.reason, history.time, history.location',
114                 'dropnotifsxmpp' => 'DELETE FROM pending WHERE notification = 1 AND EXISTS ( SELECT 1 FROM users WHERE users.id = pending.user AND nextxmpp <= ? )',
115                 'dropnotifsmail' => 'DELETE FROM pending WHERE notification = 0 AND EXISTS ( SELECT 1 FROM users WHERE users.id = pending.user AND nextmail <= ? )',
116                 'time' => 'SELECT NOW()',
117                 'searchname' => 'SELECT l.id, l.name, p.name FROM locations AS l JOIN locations AS p ON l.parent = p.id WHERE l.name LIKE ? ORDER BY LENGTH(l.id), l.id',
118                 'searchlocalname' => 'SELECT l.id, l.name, p.name FROM locations AS l JOIN locations AS p ON l.parent = p.id WHERE l.name LIKE ? AND l.id LIKE ? ORDER BY LENGTH(l.id), l.id',
119                 'hasChildren' => 'SELECT DISTINCT 1 FROM locations WHERE parent = ?',
120                 'hasMain' => 'SELECT DISTINCT 1 FROM locations WHERE id = ? AND mainhistory IS NOT NULL',
121                 'notif-exists' => 'SELECT DISTINCT 1 FROM notifications WHERE user = ? AND ( location = ? OR ( recursive = 1 AND type <= 1 AND SUBSTR( ?, 1, LENGTH( location ) ) = location ) )',
122                 'itemname' => 'SELECT name FROM locations WHERE id = ?',
123                 'admincount' => 'SELECT COUNT( DISTINCT location ) FROM history WHERE seen = 0 AND location LIKE ?'
124         } );
125 }
126
127 sub hasChildren( $$ ) {
128         my( $self, $parent ) = @_;
129         return scalar @{$self->query( 'hasChildren', [ $parent ] )};
130 }
131
132 sub hasMain( $$ ) {
133         my( $self, $id ) = @_;
134         return scalar @{$self->query( 'hasMain', [ $id ] )};
135 }
136
137 my %sorts = ( 'id' => 1, 'rid' => 1, 'name' => 1, 'rname' => 1 );
138
139 sub nodes( $$$$ ) {
140         my( $self, $parent, $args, $restrict ) = @_;
141         my $q = 'id';
142         $q = $args->{'sort'} if( defined( $args->{'sort'} ) && defined( $sorts{$args->{'sort'}} ) );
143         if( defined( $restrict ) && ( $restrict ne "" ) ) {
144                 return $self->query( 'nodes-'.$q.'-r', [ $parent, $parent.'/'.$restrict.'%' ] );
145         } else {
146                 return $self->query( 'nodes-'.$q, [ $parent ] );
147         }
148 }
149
150 sub item( $$ ) {
151         my( $self, $id ) = @_;
152         my $result = $self->query( "item", [ $id ] );
153         if( scalar @{$result} ) {
154                 return $result->[ 0 ];
155         } else {
156                 return undef;
157         }
158 }
159
160 sub hasLogin( $$ ) {
161         my( $self, $login ) = @_;
162         my $result = $self->query( 'login', [ $login ] );
163         return scalar @{$result};
164 }
165
166 sub hasEmail( $$ ) {
167         my( $self, $email ) = @_;
168         my $result = $self->query( 'email', [ $email ] );
169         return scalar @{$result};
170 }
171
172 sub addUser( $$$$ ) {
173         my( $self, $login, $email, $passwd ) = @_;
174         eval {
175                 if( ( defined $login ) && ( $login ne '' ) ) {
176                         $self->command( 'adduser', [ $login, $email, $passwd ] );
177                 } else {
178                         $self->command( 'adduser-null', [ $email, $passwd ] );
179                 }
180         };
181         if( $@ ) {
182                 return 0;
183         } else {
184                 return $self->last();
185         }
186 }
187
188 sub getLogInfo( $$ ) {
189         my( $self, $info ) = @_;
190         my $data;
191         if( $info =~ /@/ ) {#An email address
192                 $data = $self->query( 'loginfomail', [ $info ] );
193         } else {
194                 $data = $self->query( 'loginfoname', [ $info ] );
195         }
196         if( scalar @{$data} ) {
197                 my( $id, $passwd, $logtime, $lastlog, $email ) = @{$data->[ 0 ]};
198                 my $logstring;
199                 $logstring = "Last logged from $lastlog at $logtime" if( defined $logtime && defined $lastlog );
200                 $email = $info if( $info =~ /@/ );
201                 return( $id, $passwd, $email, $logstring );
202         } else {
203                 return undef;
204         }
205 }
206
207 sub rights( $$ ) {
208         my( $self, $id ) = @_;
209         return $self->query( 'rights', [ $id ] );
210 }
211
212 sub setLastLog( $$$ ) {
213         my( $self, $id, $from ) = @_;
214         $self->command( 'setlastlog', [ $from, $id ] );
215 }
216
217 sub history( $$ ) {
218         my( $self, $addr ) = @_;
219         return $self->query( 'history', [ $addr ] );
220 }
221
222 sub spaceNorm( $ ) {
223         $_ = shift;
224         return undef unless defined $_;
225         s/[ \t]+/ /g;
226         s/^\s//;
227         s/\s$//;
228         return $_;
229 }
230
231 sub submitItem( $$$ ) {
232         my( $self, $data, $auth ) = @_;
233         my( $addr ) = ( $data->{'address'} );
234         foreach( @{$addr->addressDeps()} ) {
235                 my( $dep, $error ) = @{$_};
236                 return ( $error, undef ) unless defined $self->item( $dep->get(), 0 );
237         }
238         return( 'exists', undef ) if( defined( $self->item( $addr->get(), 0 ) ) );
239         eval {
240                 $self->command( 'newitem', [ $addr->get(), $addr->parent()->get() ] );
241                 $self->command( 'newhistory', [ $addr->get(), $auth->{'authid'}, spaceNorm( $data->{'discussion'} ), spaceNorm( $data->{'name'} ), spaceNorm( $data->{'note'} ) ] );
242         };
243         if( $@ ) {
244                 $self->rollback();
245                 return( 'internal: '.$@, undef );
246         }
247         return( '', $self->last() );
248 }
249
250 sub submitHistory( $$$$ ) {
251         my( $self, $data, $auth, $address ) = @_;
252         if( $data->{'delete'} ) {
253                 $self->command( 'newhistory', [ $address->get(), $auth->{'authid'}, spaceNorm( $data->{'text'} ), '', '' ], 1 );
254         } else {
255                 $data->{'name'} = undef if defined $data->{'name'} && $data->{'name'} eq '';
256                 $self->command( 'newhistory', [ $address->get(), $auth->{'authid'}, spaceNorm( $data->{'text'} ), spaceNorm( $data->{'name'} ), spaceNorm( $data->{'note'} ) ], 1 );
257         }
258         return $self->last();
259 }
260
261 sub adminDump( $$$ ) {
262         my( $self, $prefix, $limit ) = @_;
263         if( $limit ) {
264                 $limit = int( $limit * 1.2 );
265                 my $q = $self->{'dbh'}->prepare( "$adminDumpSql $limit" );
266                 $q->execute( "$prefix%" );
267                 my @result = @{$q->fetchall_arrayref()};#Copy the array, finish() deletes the content
268                 $q->finish();
269                 return \@result;
270         } else {
271                 return $self->query( 'admindump', [ "$prefix%" ] );
272         }
273 }
274
275 sub deleteHistory( $$ ) {
276         my( $self, $id ) = @_;
277         $self->command( 'delete-hist', [ $id ] );
278 }
279
280 sub markChecked( $$ ) {
281         my( $self, $id ) = @_;
282         $self->command( 'mark-checked', [ $id ] );
283 }
284
285 sub deleteItem( $$ ) {
286         my( $self, $id ) = @_;
287         $self->command( 'delete-item', [ $id ] );
288 }
289
290 sub setMainHistory( $$$ ) {
291         my( $self, $location, $history ) = @_;
292         $self->command( 'set-mainhist', [ $history, $history, $history, $location ] );
293 }
294
295 sub resetInfo( $$ ) {
296         my( $self, $mail ) = @_;
297         my $result = $self->query( 'resetinfo', [ $mail ] );
298         if( scalar @{$result} ) {
299                 return ( @{$result->[0]} );
300         } else {
301                 return undef;
302         }
303 }
304
305 sub changePasswd( $$$ ) {
306         my( $self, $id, $passwd ) = @_;
307         $self->command( 'changepasswd', [ $passwd, $id ] );
308 }
309
310 sub profileData( $$ ) {
311         my( $self, $id ) = @_;
312         my %result;
313         ( $result{'email'}, $result{'xmpp'}, $result{'login'}, $result{'email_time'}, $result{'xmpp_time'} ) = @{$self->query( 'profiledata', [ $id ] )->[0]};
314         return \%result;
315 }
316
317 sub setEmail( $$$$ ) {
318         my( $self, $id, $email, $passwd ) = @_;
319         $self->command( 'setemail', [ $email, $passwd, $id ] );
320 }
321
322 sub pushProfile( $$$$$$ ) {
323         my( $self, $id, $login, $xmpp, $mailgather, $xmppgather ) = @_;
324         $self->command( 'pushprofile', [ $xmpp, $login, $mailgather, $xmppgather, $id ] );
325 }
326
327 sub notificationsUser( $$ ) {
328         my( $self, $uid ) = @_;
329         return $self->query( 'notifuser', [ $uid ] );
330 }
331
332 sub getNotifData( $$$ ) {
333         my( $self, $uid, $location ) = @_;
334         my $result = $self->query( 'notifdata', [ $uid, $location ] );
335         if( @{$result} ) {
336                 my( $recursive, $notification, $way ) = @{$result->[0]};
337                 return {
338                         'recursive' => $recursive,
339                         'notification' => $notification,
340                         'way' => $way };
341         } else {
342                 return { 'recursive' => 1 };
343         }
344 }
345
346 sub submitNotification( $$$$ ) {
347         my( $self, $uid, $location, $data ) = @_;
348         $self->command( 'drop-notif', [ $uid, $location ] );
349         $self->command( 'new-notif', [ $uid, $location, $data->{'recursive'}, $data->{'notification'}, $data->{'way'} ] ) unless( $data->{'notification'} == 3 );
350 }
351
352 sub pushNotifications( $$$$$ ) {
353         my( $self, $location, $history, $priority, $reason ) = @_;
354         $self->command( 'notify', [ $history, 0, $reason, 0, $priority, $location, $location ] );
355         $self->command( 'notify', [ $history, 1, $reason, 1, $priority, $location, $location ] );
356         $self->command( 'newtime-mail', [ $priority, $location, $location ] );
357         $self->command( 'newtime-xmpp', [ $priority, $location, $location ] );
358 }
359
360 sub notifExists( $$$ ) {
361         my( $self, $user, $location ) = @_;
362         return scalar @{$self->query( 'notif-exists', [ $user, $location, $location ] )};
363 }
364
365 sub mailNotifs( $$ ) {
366         my( $self, $time ) = @_;
367         return $self->query( 'mailout', [ $time ] );
368 }
369
370 sub xmppNotifs( $$ ) {
371         my( $self, $time ) = @_;
372         return $self->query( 'xmppout', [ $time ] );
373 }
374
375 sub time( $ ) {
376         my( $self ) = @_;
377         return $self->query( 'time', [] )->[0]->[0];
378 }
379
380 sub dropNotifs( $$ ) {
381         my( $self, $time ) = @_;
382         $self->command( 'dropnotifsmail', [ $time ] );
383         $self->command( 'dropnotifsxmpp', [ $time ] );
384 }
385
386 sub searchName( $$$ ) {
387         my( $self, $search, $prefix ) = @_;
388         return $self->query( 'searchlocalname', [ "%$search%", "$prefix/%" ] ) if defined $prefix;
389         return $self->query( 'searchname', [ "%$search%" ] );
390 }
391
392 sub itemName( $$ ) {
393         my( $self, $id ) = @_;
394         my $result = $self->query( 'itemname', [ $id ] )->[0]->[0];
395         return defined $result ? $result : '';
396 }
397
398 sub adminCount( $$ ) {
399         my( $tables, $prefix ) = @_;
400         return $tables->query( 'admincount', [ "$prefix%" ] )->[0]->[0];
401 }
402
403 1;