]> mj.ucw.cz Git - pciids.git/blob - PciIds/DBQ.pm
Always jump to list
[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         } );
123 }
124
125 sub hasChildren( $$ ) {
126         my( $self, $parent ) = @_;
127         return scalar @{$self->query( 'hasChildren', [ $parent ] )};
128 }
129
130 sub hasMain( $$ ) {
131         my( $self, $id ) = @_;
132         return scalar @{$self->query( 'hasMain', [ $id ] )};
133 }
134
135 my %sorts = ( 'id' => 1, 'rid' => 1, 'name' => 1, 'rname' => 1 );
136
137 sub nodes( $$$$ ) {
138         my( $self, $parent, $args, $restrict ) = @_;
139         my $q = 'id';
140         $q = $args->{'sort'} if( defined( $args->{'sort'} ) && defined( $sorts{$args->{'sort'}} ) );
141         if( defined( $restrict ) && ( $restrict ne "" ) ) {
142                 return $self->query( 'nodes-'.$q.'-r', [ $parent, $parent.'/'.$restrict.'%' ] );
143         } else {
144                 return $self->query( 'nodes-'.$q, [ $parent ] );
145         }
146 }
147
148 sub item( $$ ) {
149         my( $self, $id ) = @_;
150         my $result = $self->query( "item", [ $id ] );
151         if( scalar @{$result} ) {
152                 return $result->[ 0 ];
153         } else {
154                 return undef;
155         }
156 }
157
158 sub hasLogin( $$ ) {
159         my( $self, $login ) = @_;
160         my $result = $self->query( 'login', [ $login ] );
161         return scalar @{$result};
162 }
163
164 sub hasEmail( $$ ) {
165         my( $self, $email ) = @_;
166         my $result = $self->query( 'email', [ $email ] );
167         return scalar @{$result};
168 }
169
170 sub addUser( $$$$ ) {
171         my( $self, $login, $email, $passwd ) = @_;
172         eval {
173                 if( ( defined $login ) && ( $login ne '' ) ) {
174                         $self->command( 'adduser', [ $login, $email, $passwd ] );
175                 } else {
176                         $self->command( 'adduser-null', [ $email, $passwd ] );
177                 }
178         };
179         if( $@ ) {
180                 return 0;
181         } else {
182                 return $self->last();
183         }
184 }
185
186 sub getLogInfo( $$ ) {
187         my( $self, $info ) = @_;
188         my $data;
189         if( $info =~ /@/ ) {#An email address
190                 $data = $self->query( 'loginfomail', [ $info ] );
191         } else {
192                 $data = $self->query( 'loginfoname', [ $info ] );
193         }
194         if( scalar @{$data} ) {
195                 my( $id, $passwd, $logtime, $lastlog, $email ) = @{$data->[ 0 ]};
196                 my $logstring;
197                 $logstring = "Last logged from $lastlog at $logtime" if( defined $logtime && defined $lastlog );
198                 $email = $info if( $info =~ /@/ );
199                 return( $id, $passwd, $email, $logstring );
200         } else {
201                 return undef;
202         }
203 }
204
205 sub rights( $$ ) {
206         my( $self, $id ) = @_;
207         return $self->query( 'rights', [ $id ] );
208 }
209
210 sub setLastLog( $$$ ) {
211         my( $self, $id, $from ) = @_;
212         $self->command( 'setlastlog', [ $from, $id ] );
213 }
214
215 sub history( $$ ) {
216         my( $self, $addr ) = @_;
217         return $self->query( 'history', [ $addr ] );
218 }
219
220 sub submitItem( $$$ ) {
221         my( $self, $data, $auth ) = @_;
222         my( $addr ) = ( $data->{'address'} );
223         foreach( @{$addr->addressDeps()} ) {
224                 my( $dep, $error ) = @{$_};
225                 return ( $error, undef ) unless defined $self->item( $dep->get(), 0 );
226         }
227         return( 'exists', undef ) if( defined( $self->item( $addr->get(), 0 ) ) );
228         eval {
229                 $self->command( 'newitem', [ $addr->get(), $addr->parent()->get() ] );
230                 $self->command( 'newhistory', [ $addr->get(), $auth->{'authid'}, $data->{'discussion'}, $data->{'name'}, $data->{'note'} ] );
231         };
232         if( $@ ) {
233                 $self->rollback();
234                 return( 'internal: '.$@, undef );
235         }
236         return( '', $self->last() );
237 }
238
239 sub submitHistory( $$$$ ) {
240         my( $self, $data, $auth, $address ) = @_;
241         if( $data->{'delete'} ) {
242                 $self->command( 'newhistory', [ $address->get(), $auth->{'authid'}, $data->{'text'}, '', $data->{'note'} ], 1 );
243         } else {
244                 $data->{'name'} = undef if defined $data->{'name'} && $data->{'name'} eq '';
245                 $self->command( 'newhistory', [ $address->get(), $auth->{'authid'}, $data->{'text'}, $data->{'name'}, $data->{'note'} ], 1 );
246         }
247         return $self->last();
248 }
249
250 sub adminDump( $$$ ) {
251         my( $self, $prefix, $limit ) = @_;
252         if( $limit ) {
253                 $limit = int( $limit * 1.2 );
254                 my $q = $self->{'dbh'}->prepare( "$adminDumpSql $limit" );
255                 $q->execute( "$prefix%" );
256                 my @result = @{$q->fetchall_arrayref()};#Copy the array, finish() deletes the content
257                 $q->finish();
258                 return \@result;
259         } else {
260                 return $self->query( 'admindump', [ "$prefix%" ] );
261         }
262 }
263
264 sub deleteHistory( $$ ) {
265         my( $self, $id ) = @_;
266         $self->command( 'delete-hist', [ $id ] );
267 }
268
269 sub markChecked( $$ ) {
270         my( $self, $id ) = @_;
271         $self->command( 'mark-checked', [ $id ] );
272 }
273
274 sub deleteItem( $$ ) {
275         my( $self, $id ) = @_;
276         $self->command( 'delete-item', [ $id ] );
277 }
278
279 sub setMainHistory( $$$ ) {
280         my( $self, $location, $history ) = @_;
281         $self->command( 'set-mainhist', [ $history, $history, $history, $location ] );
282 }
283
284 sub resetInfo( $$ ) {
285         my( $self, $mail ) = @_;
286         my $result = $self->query( 'resetinfo', [ $mail ] );
287         if( scalar @{$result} ) {
288                 return ( @{$result->[0]} );
289         } else {
290                 return undef;
291         }
292 }
293
294 sub changePasswd( $$$ ) {
295         my( $self, $id, $passwd ) = @_;
296         $self->command( 'changepasswd', [ $passwd, $id ] );
297 }
298
299 sub profileData( $$ ) {
300         my( $self, $id ) = @_;
301         my %result;
302         ( $result{'email'}, $result{'xmpp'}, $result{'login'}, $result{'email_time'}, $result{'xmpp_time'} ) = @{$self->query( 'profiledata', [ $id ] )->[0]};
303         return \%result;
304 }
305
306 sub setEmail( $$$$ ) {
307         my( $self, $id, $email, $passwd ) = @_;
308         $self->command( 'setemail', [ $email, $passwd, $id ] );
309 }
310
311 sub pushProfile( $$$$$$ ) {
312         my( $self, $id, $login, $xmpp, $mailgather, $xmppgather ) = @_;
313         $self->command( 'pushprofile', [ $xmpp, $login, $mailgather, $xmppgather, $id ] );
314 }
315
316 sub notificationsUser( $$ ) {
317         my( $self, $uid ) = @_;
318         return $self->query( 'notifuser', [ $uid ] );
319 }
320
321 sub getNotifData( $$$ ) {
322         my( $self, $uid, $location ) = @_;
323         my $result = $self->query( 'notifdata', [ $uid, $location ] );
324         if( @{$result} ) {
325                 my( $recursive, $notification, $way ) = @{$result->[0]};
326                 return {
327                         'recursive' => $recursive,
328                         'notification' => $notification,
329                         'way' => $way };
330         } else {
331                 return { 'recursive' => 1 };
332         }
333 }
334
335 sub submitNotification( $$$$ ) {
336         my( $self, $uid, $location, $data ) = @_;
337         $self->command( 'drop-notif', [ $uid, $location ] );
338         $self->command( 'new-notif', [ $uid, $location, $data->{'recursive'}, $data->{'notification'}, $data->{'way'} ] ) unless( $data->{'notification'} == 3 );
339 }
340
341 sub pushNotifications( $$$$$ ) {
342         my( $self, $location, $history, $priority, $reason ) = @_;
343         $self->command( 'notify', [ $history, 0, $reason, 0, $priority, $location, $location ] );
344         $self->command( 'notify', [ $history, 1, $reason, 1, $priority, $location, $location ] );
345         $self->command( 'newtime-mail', [ $priority, $location, $location ] );
346         $self->command( 'newtime-xmpp', [ $priority, $location, $location ] );
347 }
348
349 sub notifExists( $$$ ) {
350         my( $self, $user, $location ) = @_;
351         return scalar @{$self->query( 'notif-exists', [ $user, $location, $location ] )};
352 }
353
354 sub mailNotifs( $$ ) {
355         my( $self, $time ) = @_;
356         return $self->query( 'mailout', [ $time ] );
357 }
358
359 sub xmppNotifs( $$ ) {
360         my( $self, $time ) = @_;
361         return $self->query( 'xmppout', [ $time ] );
362 }
363
364 sub time( $ ) {
365         my( $self ) = @_;
366         return $self->query( 'time', [] )->[0]->[0];
367 }
368
369 sub dropNotifs( $$ ) {
370         my( $self, $time ) = @_;
371         $self->command( 'dropnotifsmail', [ $time ] );
372         $self->command( 'dropnotifsxmpp', [ $time ] );
373 }
374
375 sub searchName( $$$ ) {
376         my( $self, $search, $prefix ) = @_;
377         return $self->query( 'searchlocalname', [ "%$search%", "$prefix/%" ] ) if defined $prefix;
378         return $self->query( 'searchname', [ "%$search%" ] );
379 }
380
381 1;