2 # Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
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.
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
13 # GNU General Public License for more details.
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
22 use base 'PciIds::DBQAny';
24 my $adminDumpSql = 'SELECT DISTINCT
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, history.seen
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 LEFT OUTER JOIN history AS test ON test.location = locations.id
33 WHERE test.seen = "0" AND locations.id LIKE ?
34 ORDER BY locations.id, history.id
39 my $node = 'SELECT id, name, note, mainhistory FROM locations WHERE parent = ? ORDER BY ';
40 my $noder = 'SELECT id, name, note, mainhistory FROM locations WHERE parent = ? AND id LIKE ? ORDER BY ';
41 return bless PciIds::DBQAny::new( $dbh, {
42 'nodes-id' => $node.'id',
43 'nodes-name' => $node.'name',
44 'nodes-rid' => $node.'id DESC',
45 'nodes-rname' => $node.'name DESC',
46 'nodes-id-r' => $noder.'id',
47 'nodes-name-r' => $noder.'name',
48 'nodes-rid-r' => $noder.'id DESC',
49 'nodes-rname-r' => $noder.'name DESC',
50 'item' => 'SELECT parent, name, note, mainhistory FROM locations WHERE id = ?',
51 'login' => 'SELECT id FROM users WHERE login = ?',
52 'email' => 'SELECT id FROM users WHERE email = ?',
53 'adduser' => 'INSERT INTO users (login, email, passwd) VALUES(?, ?, ?)',
54 'adduser-null' => 'INSERT users (email, passwd) VALUES(?, ?)',
55 'loginfomail' => 'SELECT id, passwd, logtime, lastlog FROM users WHERE email = ?',
56 'loginfoname' => 'SELECT id, passwd, logtime, lastlog, email FROM users WHERE login = ?',
57 'resetinfo' => 'SELECT id, login, passwd FROM users WHERE email = ?',
58 'changepasswd' => 'UPDATE users SET passwd = ? WHERE id = ?',
59 'setlastlog' => 'UPDATE users SET logtime = now(), lastlog = ? WHERE id = ?',
60 'rights' => 'SELECT rightId FROM rights WHERE userId = ?',
61 'newitem' => 'INSERT INTO locations (id, parent) VALUES(?, ?)',
62 'newhistory' => 'INSERT INTO history (location, owner, discussion, nodename, nodenote) VALUES(?, ?, ?, ?, ?)',
63 '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',
64 'admindump' => "$adminDumpSql 100",#Dumps new discussion submits with their senders and corresponding main history and names
65 'delete-hist' => 'DELETE FROM history WHERE id = ?',
66 'mark-checked' => 'UPDATE history SET seen = 1 WHERE id = ?',
67 'delete-item' => 'DELETE FROM locations WHERE id = ?',
68 'set-mainhist' => 'UPDATE locations SET
70 name = ( SELECT nodename FROM history WHERE id = ? ),
71 note = ( SELECT nodenote FROM history WHERE id = ? )
74 'profiledata' => 'SELECT email, xmpp, login, mailgather, xmppgather FROM users WHERE id = ?',
75 'pushprofile' => 'UPDATE users SET xmpp = ?, login = ?, mailgather = ?, xmppgather = ? WHERE id = ?',
76 'setemail' => 'UPDATE users SET email = ?, passwd = ? WHERE id = ?',
77 'notifuser' => 'SELECT location, `recursive` FROM notifications WHERE user = ? ORDER BY location',
78 'notifdata' => 'SELECT `recursive`, type, notification FROM notifications WHERE user = ? AND location = ?',
79 'drop-notif' => 'DELETE FROM notifications WHERE user = ? AND location = ?',
80 'new-notif' => 'INSERT INTO notifications (user, location, `recursive`, type, notification) VALUES (?, ?, ?, ?, ?)',
81 '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 ) )',
82 '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 ) ) )',
83 '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 ) ) )',
85 pending.user, users.email,
86 pending.reason, history.discussion, history.nodename, history.nodenote, history.time,
87 auth.login, history.location, locations.name, locations.note
90 INNER JOIN users ON users.id = pending.user
91 INNER JOIN history ON history.id = pending.history
92 INNER JOIN locations ON history.location = locations.id
93 INNER JOIN users AS auth ON auth.id = history.owner
95 pending.notification = 0
96 AND users.nextmail <= ?
98 pending.user, pending.reason, history.time, history.location',
100 pending.user, users.xmpp,
101 pending.reason, history.discussion, history.nodename, history.nodenote, history.time,
102 auth.login, history.location, locations.name, locations.note
105 INNER JOIN users ON users.id = pending.user
106 INNER JOIN history ON history.id = pending.history
107 INNER JOIN locations ON history.location = locations.id
108 INNER JOIN users AS auth ON auth.id = history.owner
110 pending.notification = 1
111 AND users.nextxmpp <= ?
112 AND users.xmpp IS NOT NULL
114 pending.user, pending.reason, history.time, history.location',
115 'dropnotifsxmpp' => 'DELETE FROM pending WHERE notification = 1 AND EXISTS ( SELECT 1 FROM users WHERE users.id = pending.user AND nextxmpp <= ? )',
116 'dropnotifsmail' => 'DELETE FROM pending WHERE notification = 0 AND EXISTS ( SELECT 1 FROM users WHERE users.id = pending.user AND nextmail <= ? )',
117 'time' => 'SELECT NOW()',
118 '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',
119 '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',
120 'hasChildren' => 'SELECT DISTINCT 1 FROM locations WHERE parent = ?',
121 'hasMain' => 'SELECT DISTINCT 1 FROM locations WHERE id = ? AND mainhistory IS NOT NULL',
122 'notif-exists' => 'SELECT DISTINCT 1 FROM notifications WHERE user = ? AND ( location = ? OR ( `recursive` = 1 AND type <= 1 AND SUBSTR( ?, 1, LENGTH( location ) ) = location ) )',
123 'itemname' => 'SELECT name FROM locations WHERE id = ?',
124 'admincount' => 'SELECT COUNT( DISTINCT location ) FROM history WHERE seen = 0 AND location LIKE ?'
128 sub hasChildren( $$ ) {
129 my( $self, $parent ) = @_;
130 return scalar @{$self->query( 'hasChildren', [ $parent ] )};
134 my( $self, $id ) = @_;
135 return scalar @{$self->query( 'hasMain', [ $id ] )};
138 my %sorts = ( 'id' => 1, 'rid' => 1, 'name' => 1, 'rname' => 1 );
141 my( $self, $parent, $args, $restrict ) = @_;
143 $q = $args->{'sort'} if( defined( $args->{'sort'} ) && defined( $sorts{$args->{'sort'}} ) );
144 if( defined( $restrict ) && ( $restrict ne "" ) ) {
145 return $self->query( 'nodes-'.$q.'-r', [ $parent, $parent.'/'.$restrict.'%' ] );
147 return $self->query( 'nodes-'.$q, [ $parent ] );
152 my( $self, $id ) = @_;
153 my $result = $self->query( "item", [ $id ] );
154 if( scalar @{$result} ) {
155 return $result->[ 0 ];
162 my( $self, $login ) = @_;
163 my $result = $self->query( 'login', [ $login ] );
164 return scalar @{$result};
168 my( $self, $email ) = @_;
169 my $result = $self->query( 'email', [ $email ] );
170 return scalar @{$result};
173 sub addUser( $$$$ ) {
174 my( $self, $login, $email, $passwd ) = @_;
176 if( ( defined $login ) && ( $login ne '' ) ) {
177 $self->command( 'adduser', [ $login, $email, $passwd ] );
179 $self->command( 'adduser-null', [ $email, $passwd ] );
185 return $self->last();
189 sub getLogInfo( $$ ) {
190 my( $self, $info ) = @_;
192 if( $info =~ /@/ ) {#An email address
193 $data = $self->query( 'loginfomail', [ $info ] );
195 $data = $self->query( 'loginfoname', [ $info ] );
197 if( scalar @{$data} ) {
198 my( $id, $passwd, $logtime, $lastlog, $email ) = @{$data->[ 0 ]};
200 $logstring = "Last logged from $lastlog at $logtime" if( defined $logtime && defined $lastlog );
201 $email = $info if( $info =~ /@/ );
202 return( $id, $passwd, $email, $logstring );
209 my( $self, $id ) = @_;
210 return $self->query( 'rights', [ $id ] );
213 sub setLastLog( $$$ ) {
214 my( $self, $id, $from ) = @_;
215 $self->command( 'setlastlog', [ $from, $id ] );
219 my( $self, $addr ) = @_;
220 return $self->query( 'history', [ $addr ] );
225 return undef unless defined $_;
233 sub submitItem( $$$ ) {
234 my( $self, $data, $auth ) = @_;
235 my( $addr ) = ( $data->{'address'} );
236 foreach( @{$addr->addressDeps()} ) {
237 my( $dep, $error ) = @{$_};
238 return ( $error, undef ) unless defined $self->item( $dep->get(), 0 );
240 return( 'exists', undef ) if( defined( $self->item( $addr->get(), 0 ) ) );
242 $self->command( 'newitem', [ $addr->get(), $addr->parent()->get() ] );
243 $self->command( 'newhistory', [ $addr->get(), $auth->{'authid'}, spaceNorm( $data->{'discussion'} ), spaceNorm( $data->{'name'} ), spaceNorm( $data->{'note'} ) ] );
247 return( 'internal: '.$@, undef );
249 return( '', $self->last() );
252 sub submitHistory( $$$$ ) {
253 my( $self, $data, $auth, $address ) = @_;
254 if( $data->{'delete'} ) {
255 $self->command( 'newhistory', [ $address->get(), $auth->{'authid'}, spaceNorm( $data->{'text'} ), '', '' ], 1 );
257 $data->{'name'} = undef if defined $data->{'name'} && $data->{'name'} eq '';
258 $self->command( 'newhistory', [ $address->get(), $auth->{'authid'}, spaceNorm( $data->{'text'} ), spaceNorm( $data->{'name'} ), spaceNorm( $data->{'note'} ) ], 1 );
260 return $self->last();
263 sub adminDump( $$$ ) {
264 my( $self, $prefix, $limit ) = @_;
266 $limit = int( $limit * 1.2 );
267 my $q = $self->{'dbh'}->prepare( "$adminDumpSql $limit" );
268 $q->execute( "$prefix%" );
269 my @result = @{$q->fetchall_arrayref()};#Copy the array, finish() deletes the content
273 return $self->query( 'admindump', [ "$prefix%" ] );
277 sub deleteHistory( $$ ) {
278 my( $self, $id ) = @_;
279 $self->command( 'delete-hist', [ $id ] );
282 sub markChecked( $$ ) {
283 my( $self, $id ) = @_;
284 $self->command( 'mark-checked', [ $id ] );
287 sub deleteItem( $$ ) {
288 my( $self, $id ) = @_;
289 $self->command( 'delete-item', [ $id ] );
292 sub setMainHistory( $$$ ) {
293 my( $self, $location, $history ) = @_;
294 $self->command( 'set-mainhist', [ $history, $history, $history, $location ] );
297 sub resetInfo( $$ ) {
298 my( $self, $mail ) = @_;
299 my $result = $self->query( 'resetinfo', [ $mail ] );
300 if( scalar @{$result} ) {
301 return ( @{$result->[0]} );
307 sub changePasswd( $$$ ) {
308 my( $self, $id, $passwd ) = @_;
309 $self->command( 'changepasswd', [ $passwd, $id ] );
312 sub profileData( $$ ) {
313 my( $self, $id ) = @_;
315 ( $result{'email'}, $result{'xmpp'}, $result{'login'}, $result{'email_time'}, $result{'xmpp_time'} ) = @{$self->query( 'profiledata', [ $id ] )->[0]};
319 sub setEmail( $$$$ ) {
320 my( $self, $id, $email, $passwd ) = @_;
321 $self->command( 'setemail', [ $email, $passwd, $id ] );
324 sub pushProfile( $$$$$$ ) {
325 my( $self, $id, $login, $xmpp, $mailgather, $xmppgather ) = @_;
326 $self->command( 'pushprofile', [ $xmpp, $login, $mailgather, $xmppgather, $id ] );
329 sub notificationsUser( $$ ) {
330 my( $self, $uid ) = @_;
331 return $self->query( 'notifuser', [ $uid ] );
334 sub getNotifData( $$$ ) {
335 my( $self, $uid, $location ) = @_;
336 my $result = $self->query( 'notifdata', [ $uid, $location ] );
338 my( $recursive, $notification, $way ) = @{$result->[0]};
340 'recursive' => $recursive,
341 'notification' => $notification,
344 return { 'recursive' => 1 };
348 sub submitNotification( $$$$ ) {
349 my( $self, $uid, $location, $data ) = @_;
350 $self->command( 'drop-notif', [ $uid, $location ] );
351 $self->command( 'new-notif', [ $uid, $location, $data->{'recursive'}, $data->{'notification'}, $data->{'way'} ] ) unless( $data->{'notification'} == 3 );
354 sub pushNotifications( $$$$$ ) {
355 my( $self, $location, $history, $priority, $reason ) = @_;
356 $self->command( 'notify', [ $history, 0, $reason, 0, $priority, $location, $location ] );
357 $self->command( 'notify', [ $history, 1, $reason, 1, $priority, $location, $location ] );
358 $self->command( 'newtime-mail', [ $priority, $location, $location ] );
359 $self->command( 'newtime-xmpp', [ $priority, $location, $location ] );
362 sub notifExists( $$$ ) {
363 my( $self, $user, $location ) = @_;
364 return scalar @{$self->query( 'notif-exists', [ $user, $location, $location ] )};
367 sub mailNotifs( $$ ) {
368 my( $self, $time ) = @_;
369 return $self->query( 'mailout', [ $time ] );
372 sub xmppNotifs( $$ ) {
373 my( $self, $time ) = @_;
374 return $self->query( 'xmppout', [ $time ] );
379 return $self->query( 'time', [] )->[0]->[0];
382 sub dropNotifs( $$ ) {
383 my( $self, $time ) = @_;
384 $self->command( 'dropnotifsmail', [ $time ] );
385 $self->command( 'dropnotifsxmpp', [ $time ] );
388 sub searchName( $$$ ) {
389 my( $self, $search, $prefix ) = @_;
390 return $self->query( 'searchlocalname', [ "%$search%", "$prefix/%" ] ) if defined $prefix;
391 return $self->query( 'searchname', [ "%$search%" ] );
395 my( $self, $id ) = @_;
396 my $result = $self->query( 'itemname', [ $id ] )->[0]->[0];
397 return defined $result ? $result : '';
400 sub adminCount( $$ ) {
401 my( $tables, $prefix ) = @_;
402 return $tables->query( 'admincount', [ "$prefix%" ] )->[0]->[0];