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.email, main.discussion,
35 history.id, history.discussion, history.nodename, history.nodenote, users.email
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
41 WHERE history.seen = "0" AND locations.id LIKE ?
42 ORDER BY locations.id, history.id
43 LIMIT 100',#Dumps new discussion submits with their senders and corresponding main history and names
44 'delete-hist' => 'DELETE FROM history WHERE id = ?',
45 'mark-checked' => 'UPDATE history SET seen = 1 WHERE id = ?',
46 'delete-item' => 'DELETE FROM locations WHERE id = ?',
47 'set-mainhist' => 'UPDATE locations SET
49 name = ( SELECT nodename FROM history WHERE id = ? ),
50 note = ( SELECT nodenote FROM history WHERE id = ? )
53 'profiledata' => 'SELECT email, xmpp, login, mailgather, xmppgather FROM users WHERE id = ?',
54 'pushprofile' => 'UPDATE users SET xmpp = ?, login = ?, mailgather = ?, xmppgather = ? WHERE id = ?',
55 'setemail' => 'UPDATE users SET email = ?, passwd = ? WHERE id = ?',
56 'notifuser' => 'SELECT location, recursive FROM notifications WHERE user = ? ORDER BY location',
57 'notifdata' => 'SELECT recursive, type, notification FROM notifications WHERE user = ? AND location = ?',
58 'drop-notif' => 'DELETE FROM notifications WHERE user = ? AND location = ?',
59 'new-notif' => 'INSERT INTO notifications (user, location, recursive, type, notification) VALUES (?, ?, ?, ?, ?)',
60 '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 ) )',
61 '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 ) ) )',
62 '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 ) ) )',
64 pending.user, users.email,
65 pending.reason, history.discussion, history.nodename, history.nodenote, history.time,
66 auth.login, history.location, locations.name, locations.note
69 INNER JOIN users ON users.id = pending.user
70 INNER JOIN history ON history.id = pending.history
71 INNER JOIN locations ON history.location = locations.id
72 INNER JOIN users AS auth ON auth.id = history.owner
74 pending.notification = 0
75 AND users.nextmail <= ?
77 pending.user, pending.reason, history.time, history.location',
79 pending.user, users.xmpp,
80 pending.reason, history.discussion, history.nodename, history.nodenote, history.time,
81 auth.login, history.location, locations.name, locations.note
84 INNER JOIN users ON users.id = pending.user
85 INNER JOIN history ON history.id = pending.history
86 INNER JOIN locations ON history.location = locations.id
87 INNER JOIN users AS auth ON auth.id = history.owner
89 pending.notification = 1
90 AND users.nextxmpp <= ?
92 pending.user, pending.reason, history.time, history.location',
93 'dropnotifsxmpp' => 'DELETE FROM pending WHERE notification = 1 AND EXISTS ( SELECT 1 FROM users WHERE users.id = pending.user AND nextxmpp <= ? )',
94 'dropnotifsmail' => 'DELETE FROM pending WHERE notification = 0 AND EXISTS ( SELECT 1 FROM users WHERE users.id = pending.user AND nextmail <= ? )',
95 'time' => 'SELECT NOW()',
96 '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',
97 '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',
98 'hasChildren' => 'SELECT 1 FROM locations WHERE parent = ?'
102 my %sorts = ( 'id' => 1, 'rid' => 1, 'name' => 1, 'rname' => 1 );
104 sub hasChildren( $$ ) {
105 my( $self, $parent ) = @_;
106 return scalar @{$self->query( 'hasChildren', [ $parent ] )};
110 my( $self, $parent, $args, $restrict ) = @_;
112 $q = $args->{'sort'} if( defined( $args->{'sort'} ) && defined( $sorts{$args->{'sort'}} ) );
113 if( defined( $restrict ) && ( $restrict ne "" ) ) {
114 return $self->query( 'nodes-'.$q.'-r', [ $parent, $parent.'/'.$restrict.'%' ] );
116 return $self->query( 'nodes-'.$q, [ $parent ] );
121 my( $self, $id ) = @_;
122 my $result = $self->query( "item", [ $id ] );
123 if( scalar @{$result} ) {
124 return $result->[ 0 ];
131 my( $self, $login ) = @_;
132 my $result = $self->query( 'login', [ $login ] );
133 return scalar @{$result};
137 my( $self, $email ) = @_;
138 my $result = $self->query( 'email', [ $email ] );
139 return scalar @{$result};
142 sub addUser( $$$$ ) {
143 my( $self, $login, $email, $passwd ) = @_;
145 if( ( defined $login ) && ( $login ne '' ) ) {
146 $self->command( 'adduser', [ $login, $email, $passwd ] );
148 $self->command( 'adduser-null', [ $email, $passwd ] );
154 return $self->last();
158 sub getLogInfo( $$ ) {
159 my( $self, $info ) = @_;
161 if( $info =~ /@/ ) {#An email address
162 $data = $self->query( 'loginfomail', [ $info ] );
164 $data = $self->query( 'loginfoname', [ $info ] );
166 if( scalar @{$data} ) {
167 my( $id, $passwd, $logtime, $lastlog, $email ) = @{$data->[ 0 ]};
169 $logstring = "Last logged from $lastlog at $logtime" if( defined $logtime && defined $lastlog );
170 $email = $info if( $info =~ /@/ );
171 return( $id, $passwd, $email, $logstring );
178 my( $self, $id ) = @_;
179 return $self->query( 'rights', [ $id ] );
182 sub setLastLog( $$$ ) {
183 my( $self, $id, $from ) = @_;
184 $self->command( 'setlastlog', [ $from, $id ] );
188 my( $self, $addr ) = @_;
189 return $self->query( 'history', [ $addr ] );
192 sub submitItem( $$$ ) {
193 my( $self, $data, $auth ) = @_;
194 my( $addr ) = ( $data->{'address'} );
195 foreach( @{$addr->addressDeps()} ) {
196 my( $dep, $error ) = @{$_};
197 return ( $error, undef ) unless defined $self->item( $dep->get(), 0 );
199 return( 'exists', undef ) if( defined( $self->item( $addr->get(), 0 ) ) );
201 $self->command( 'newitem', [ $addr->get(), $addr->parent()->get() ] );
202 $self->command( 'newhistory', [ $addr->get(), $auth->{'authid'}, $data->{'discussion'}, $data->{'name'}, $data->{'note'} ] );
206 return( 'internal: '.$@, undef );
208 return( '', $self->last() );
211 sub submitHistory( $$$$ ) {
212 my( $self, $data, $auth, $address ) = @_;
213 if( $data->{'delete'} ) {
214 $self->command( 'newhistory', [ $address->get(), $auth->{'authid'}, $data->{'text'}, '', $data->{'note'} ], 1 );
216 $data->{'name'} = undef if defined $data->{'name'} && $data->{'name'} eq '';
217 $self->command( 'newhistory', [ $address->get(), $auth->{'authid'}, $data->{'text'}, $data->{'name'}, $data->{'note'} ], 1 );
219 return $self->last();
222 sub adminDump( $$ ) {
223 my( $self, $prefix ) = @_;
224 return $self->query( 'admindump', [ "$prefix%" ] );
227 sub deleteHistory( $$ ) {
228 my( $self, $id ) = @_;
229 $self->command( 'delete-hist', [ $id ] );
232 sub markChecked( $$ ) {
233 my( $self, $id ) = @_;
234 $self->command( 'mark-checked', [ $id ] );
237 sub deleteItem( $$ ) {
238 my( $self, $id ) = @_;
239 $self->command( 'delete-item', [ $id ] );
242 sub setMainHistory( $$$ ) {
243 my( $self, $location, $history ) = @_;
244 $self->command( 'set-mainhist', [ $history, $history, $history, $location ] );
247 sub resetInfo( $$ ) {
248 my( $self, $mail ) = @_;
249 my $result = $self->query( 'resetinfo', [ $mail ] );
250 if( scalar @{$result} ) {
251 return ( @{$result->[0]} );
257 sub changePasswd( $$$ ) {
258 my( $self, $id, $passwd ) = @_;
259 $self->command( 'changepasswd', [ $passwd, $id ] );
262 sub profileData( $$ ) {
263 my( $self, $id ) = @_;
265 ( $result{'email'}, $result{'xmpp'}, $result{'login'}, $result{'email_time'}, $result{'xmpp_time'} ) = @{$self->query( 'profiledata', [ $id ] )->[0]};
269 sub setEmail( $$$$ ) {
270 my( $self, $id, $email, $passwd ) = @_;
271 $self->command( 'setemail', [ $email, $passwd, $id ] );
274 sub pushProfile( $$$$$$ ) {
275 my( $self, $id, $login, $xmpp, $mailgather, $xmppgather ) = @_;
276 $self->command( 'pushprofile', [ $xmpp, $login, $mailgather, $xmppgather, $id ] );
279 sub notificationsUser( $$ ) {
280 my( $self, $uid ) = @_;
281 return $self->query( 'notifuser', [ $uid ] );
284 sub getNotifData( $$$ ) {
285 my( $self, $uid, $location ) = @_;
286 my $result = $self->query( 'notifdata', [ $uid, $location ] );
288 my( $recursive, $notification, $way ) = @{$result->[0]};
290 'recursive' => $recursive,
291 'notification' => $notification,
294 return { 'recursive' => 1 };
298 sub submitNotification( $$$$ ) {
299 my( $self, $uid, $location, $data ) = @_;
300 $self->command( 'drop-notif', [ $uid, $location ] );
301 $self->command( 'new-notif', [ $uid, $location, $data->{'recursive'}, $data->{'notification'}, $data->{'way'} ] ) unless( $data->{'notification'} == 3 );
304 sub pushNotifications( $$$$$ ) {
305 my( $self, $location, $history, $priority, $reason ) = @_;
306 $self->command( 'notify', [ $history, 0, $reason, 0, $priority, $location, $location ] );
307 $self->command( 'notify', [ $history, 1, $reason, 1, $priority, $location, $location ] );
308 $self->command( 'newtime-mail', [ $priority, $location, $location ] );
309 $self->command( 'newtime-xmpp', [ $priority, $location, $location ] );
312 sub mailNotifs( $$ ) {
313 my( $self, $time ) = @_;
314 return $self->query( 'mailout', [ $time ] );
317 sub xmppNotifs( $$ ) {
318 my( $self, $time ) = @_;
319 return $self->query( 'xmppout', [ $time ] );
324 return $self->query( 'time', [] )->[0]->[0];
327 sub dropNotifs( $$ ) {
328 my( $self, $time ) = @_;
329 $self->command( 'dropnotifsmail', [ $time ] );
330 $self->command( 'dropnotifsxmpp', [ $time ] );
333 sub searchName( $$$ ) {
334 my( $self, $search, $prefix ) = @_;
335 return $self->query( 'searchlocalname', [ "%$search%", "$prefix/%" ] ) if defined $prefix;
336 return $self->query( 'searchname', [ "%$search%" ] );