4 use base 'PciIds::DBQAny';
8 my $node = 'SELECT id, name, note, mainhistory FROM locations WHERE parent = ? ORDER BY ';
9 my $noder = 'SELECT id, name, note, mainhistory FROM locations WHERE parent = ? AND id LIKE ? ORDER BY ';
10 return bless PciIds::DBQAny::new( $dbh, {
11 'nodes-id' => $node.'id',
12 'nodes-name' => $node.'name',
13 'nodes-rid' => $node.'id DESC',
14 'nodes-rname' => $node.'name DESC',
15 'nodes-id-r' => $noder.'id',
16 'nodes-name-r' => $noder.'name',
17 'nodes-rid-r' => $noder.'id DESC',
18 'nodes-rname-r' => $noder.'name DESC',
19 'item' => 'SELECT parent, name, note, mainhistory FROM locations WHERE id = ?',
20 'login' => 'SELECT id FROM users WHERE login = ?',
21 'email' => 'SELECT id FROM users WHERE email = ?',
22 'adduser' => 'INSERT INTO users (login, email, passwd) VALUES(?, ?, ?)',
23 'adduser-null' => 'INSERT users (email, passwd) VALUES(?, ?)',
24 'loginfomail' => 'SELECT id, passwd, logtime, lastlog FROM users WHERE email = ?',
25 'loginfoname' => 'SELECT id, passwd, logtime, lastlog, email FROM users WHERE login = ?',
26 'resetinfo' => 'SELECT id, login, passwd FROM users WHERE email = ?',
27 'changepasswd' => 'UPDATE users SET passwd = ? WHERE id = ?',
28 'setlastlog' => 'UPDATE users SET logtime = now(), lastlog = ? WHERE id = ?',
29 'rights' => 'SELECT rightId FROM rights WHERE userId = ?',
30 'newitem' => 'INSERT INTO locations (id, parent) VALUES(?, ?)',
31 'newhistory' => 'INSERT INTO history (location, owner, discussion, nodename, nodenote) VALUES(?, ?, ?, ?, ?)',
32 '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',
33 'admindump' => 'SELECT
34 locations.id, locations.name, locations.note, locations.mainhistory, musers.login, main.discussion,
35 history.id, history.discussion, history.nodename, history.nodenote, users.login
37 locations INNER JOIN history ON history.location = locations.id
38 LEFT OUTER JOIN users ON history.owner = users.id
39 LEFT OUTER JOIN history AS main ON locations.mainhistory = main.id
40 LEFT OUTER JOIN users AS musers ON main.owner = musers.id WHERE history.seen = "0"
41 ORDER BY locations.id, history.id
42 LIMIT 15',#Dumps new discussion submits with their senders and corresponding main history and names
43 'delete-hist' => 'DELETE FROM history WHERE id = ?',
44 'mark-checked' => 'UPDATE history SET seen = 1 WHERE id = ?',
45 'delete-item' => 'DELETE FROM locations WHERE id = ?',
46 'set-mainhist' => 'UPDATE locations SET
48 name = ( SELECT nodename FROM history WHERE id = ? ),
49 note = ( SELECT nodenote FROM history WHERE id = ? )
52 'profiledata' => 'SELECT email, xmpp, login, mailgather, xmppgather FROM users WHERE id = ?',
53 'pushprofile' => 'UPDATE users SET xmpp = ?, login = ?, mailgather = ?, xmppgather = ? WHERE id = ?',
54 'setemail' => 'UPDATE users SET email = ?, passwd = ? WHERE id = ?',
55 'notifuser' => 'SELECT location, recursive FROM notifications WHERE user = ? ORDER BY location',
56 'notifdata' => 'SELECT recursive, type, notification FROM notifications WHERE user = ? AND location = ?',
57 'drop-notif' => 'DELETE FROM notifications WHERE user = ? AND location = ?',
58 'new-notif' => 'INSERT INTO notifications (user, location, recursive, type, notification) VALUES (?, ?, ?, ?, ?)',
59 'notify' => 'INSERT INTO pending (user, history, notification, reason) SELECT DISTINCT user, ?, ?, ? FROM notifications WHERE ( notification = 2 OR notification = ? ) AND type <= ? AND ( location = ? OR ( SUBSTR( ?, 1, LENGTH( location ) ) = location ) )',
60 '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 ( SUBSTR( ?, 1, LENGTH( location ) ) = location ) ) )',
61 '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 ( SUBSTR( ?, 1, LENGTH( location ) ) = location ) ) )',
63 pending.user, users.email,
64 pending.reason, history.discussion, history.nodename, history.nodenote, history.time,
65 auth.login, history.location, locations.name, locations.note
68 INNER JOIN users ON users.id = pending.user
69 INNER JOIN history ON history.id = pending.history
70 INNER JOIN locations ON history.location = locations.id
71 INNER JOIN users AS auth ON auth.id = history.owner
73 pending.notification = 0
74 AND users.nextmail <= ?
76 pending.user, pending.reason, history.time, history.location',
78 pending.user, users.xmpp,
79 pending.reason, history.discussion, history.nodename, history.nodenote, history.time,
80 auth.login, history.location, locations.name, locations.note
83 INNER JOIN users ON users.id = pending.user
84 INNER JOIN history ON history.id = pending.history
85 INNER JOIN locations ON history.location = locations.id
86 INNER JOIN users AS auth ON auth.id = history.owner
88 pending.notification = 1
89 AND users.nextxmpp <= ?
91 pending.user, pending.reason, history.time, history.location',
92 'dropnotifsxmpp' => 'DELETE FROM pending WHERE notification = 1 AND EXISTS ( SELECT 1 FROM users WHERE users.id = pending.user AND nextxmpp <= ? )',
93 'dropnotifsmail' => 'DELETE FROM pending WHERE notification = 0 AND EXISTS ( SELECT 1 FROM users WHERE users.id = pending.user AND nextmail <= ? )',
94 'time' => 'SELECT NOW()',
95 '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 l.id',
96 '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 l.id'
100 my %sorts = ( 'id' => 1, 'rid' => 1, 'name' => 1, 'rname' => 1 );
103 my( $self, $parent, $args, $restrict ) = @_;
105 $q = $args->{'sort'} if( defined( $args->{'sort'} ) && defined( $sorts{$args->{'sort'}} ) );
106 if( defined( $restrict ) && ( $restrict ne "" ) ) {
107 return $self->query( 'nodes-'.$q.'-r', [ $parent, $parent.'/'.$restrict.'%' ] );
109 return $self->query( 'nodes-'.$q, [ $parent ] );
114 my( $self, $id ) = @_;
115 my $result = $self->query( "item", [ $id ] );
116 if( scalar @{$result} ) {
117 return $result->[ 0 ];
124 my( $self, $login ) = @_;
125 my $result = $self->query( 'login', [ $login ] );
126 return scalar @{$result};
130 my( $self, $email ) = @_;
131 my $result = $self->query( 'email', [ $email ] );
132 return scalar @{$result};
135 sub addUser( $$$$ ) {
136 my( $self, $login, $email, $passwd ) = @_;
138 if( ( defined $login ) && ( $login ne '' ) ) {
139 $self->command( 'adduser', [ $login, $email, $passwd ] );
141 $self->command( 'adduser-null', [ $email, $passwd ] );
147 return $self->last();
151 sub getLogInfo( $$ ) {
152 my( $self, $info ) = @_;
154 if( $info =~ /@/ ) {#An email address
155 $data = $self->query( 'loginfomail', [ $info ] );
157 $data = $self->query( 'loginfoname', [ $info ] );
159 if( scalar @{$data} ) {
160 my( $id, $passwd, $logtime, $lastlog, $email ) = @{$data->[ 0 ]};
162 $logstring = "Last logged from $lastlog at $logtime" if( defined $logtime && defined $lastlog );
163 $email = $info if( $info =~ /@/ );
164 return( $id, $passwd, $email, $logstring );
171 my( $self, $id ) = @_;
172 return $self->query( 'rights', [ $id ] );
175 sub setLastLog( $$$ ) {
176 my( $self, $id, $from ) = @_;
177 $self->command( 'setlastlog', [ $from, $id ] );
181 my( $self, $addr ) = @_;
182 return $self->query( 'history', [ $addr ] );
185 sub submitItem( $$$ ) {
186 my( $self, $data, $auth ) = @_;
187 my( $addr ) = ( $data->{'address'} );
188 foreach( @{$addr->addressDeps()} ) {
189 my( $dep, $error ) = @{$_};
190 return ( $error, undef ) unless defined $self->item( $dep->get(), 0 );
192 return( 'exists', undef ) if( defined( $self->item( $addr->get(), 0 ) ) );
194 $self->command( 'newitem', [ $addr->get(), $addr->parent()->get() ] );
195 $self->command( 'newhistory', [ $addr->get(), $auth->{'authid'}, $data->{'discussion'}, $data->{'name'}, $data->{'note'} ] );
199 return( 'internal: '.$@, undef );
201 return( '', $self->last() );
204 sub submitHistory( $$$$ ) {
205 my( $self, $data, $auth, $address ) = @_;
206 if( $data->{'delete'} ) {
207 $self->command( 'newhistory', [ $address->get(), $auth->{'authid'}, $data->{'text'}, '', $data->{'note'} ], 1 );
209 $data->{'name'} = undef if defined $data->{'name'} && $data->{'name'} eq '';
210 $self->command( 'newhistory', [ $address->get(), $auth->{'authid'}, $data->{'text'}, $data->{'name'}, $data->{'note'} ], 1 );
212 return $self->last();
216 return shift->query( 'admindump', [] );
219 sub deleteHistory( $$ ) {
220 my( $self, $id ) = @_;
221 $self->command( 'delete-hist', [ $id ] );
224 sub markChecked( $$ ) {
225 my( $self, $id ) = @_;
226 $self->command( 'mark-checked', [ $id ] );
229 sub deleteItem( $$ ) {
230 my( $self, $id ) = @_;
231 $self->command( 'delete-item', [ $id ] );
234 sub setMainHistory( $$$ ) {
235 my( $self, $location, $history ) = @_;
236 $self->command( 'set-mainhist', [ $history, $history, $history, $location ] );
239 sub resetInfo( $$ ) {
240 my( $self, $mail ) = @_;
241 my $result = $self->query( 'resetinfo', [ $mail ] );
242 if( scalar @{$result} ) {
243 return ( @{$result->[0]} );
249 sub changePasswd( $$$ ) {
250 my( $self, $id, $passwd ) = @_;
251 $self->command( 'changepasswd', [ $passwd, $id ] );
254 sub profileData( $$ ) {
255 my( $self, $id ) = @_;
257 ( $result{'email'}, $result{'xmpp'}, $result{'login'}, $result{'email_time'}, $result{'xmpp_time'} ) = @{$self->query( 'profiledata', [ $id ] )->[0]};
261 sub setEmail( $$$$ ) {
262 my( $self, $id, $email, $passwd ) = @_;
263 $self->command( 'setemail', [ $email, $passwd, $id ] );
266 sub pushProfile( $$$$$$ ) {
267 my( $self, $id, $login, $xmpp, $mailgather, $xmppgather ) = @_;
268 $self->command( 'pushprofile', [ $xmpp, $login, $mailgather, $xmppgather, $id ] );
271 sub notificationsUser( $$ ) {
272 my( $self, $uid ) = @_;
273 return $self->query( 'notifuser', [ $uid ] );
276 sub getNotifData( $$$ ) {
277 my( $self, $uid, $location ) = @_;
278 my $result = $self->query( 'notifdata', [ $uid, $location ] );
280 my( $recursive, $notification, $way ) = @{$result->[0]};
282 'recursive' => $recursive,
283 'notification' => $notification,
286 return { 'recursive' => 1 };
290 sub submitNotification( $$$$ ) {
291 my( $self, $uid, $location, $data ) = @_;
292 $self->command( 'drop-notif', [ $uid, $location ] );
293 $self->command( 'new-notif', [ $uid, $location, $data->{'recursive'}, $data->{'notification'}, $data->{'way'} ] ) unless( $data->{'notification'} == 3 );
296 sub pushNotifications( $$$$$ ) {
297 my( $self, $location, $history, $priority, $reason ) = @_;
298 $self->command( 'notify', [ $history, 0, $reason, 0, $priority, $location, $location ] );
299 $self->command( 'notify', [ $history, 1, $reason, 1, $priority, $location, $location ] );
300 $self->command( 'newtime-mail', [ $priority, $location, $location ] );
301 $self->command( 'newtime-xmpp', [ $priority, $location, $location ] );
304 sub mailNotifs( $$ ) {
305 my( $self, $time ) = @_;
306 return $self->query( 'mailout', [ $time ] );
309 sub xmppNotifs( $$ ) {
310 my( $self, $time ) = @_;
311 return $self->query( 'xmppout', [ $time ] );
316 return $self->query( 'time', [] )->[0]->[0];
319 sub dropNotifs( $$ ) {
320 my( $self, $time ) = @_;
321 $self->command( 'dropnotifsmail', [ $time ] );
322 $self->command( 'dropnotifsxmpp', [ $time ] );
325 sub searchName( $$$ ) {
326 my( $self, $search, $prefix ) = @_;
327 return $self->query( 'searchlocalname', [ "%$search%", "$prefix/%" ] ) if defined $prefix;
328 return $self->query( 'searchname', [ "%$search%" ] );