aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ManaTools/Shared/GUI/Dialog.pm
blob: 760953609ce937b87c004495a1cf84f57133e37a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# vim: set et ts=4 sw=4:
package ManaTools::Shared::GUI::Dialog;
#============================================================= -*-perl-*-

=head1 NAME

ManaTools::Shared::GUI::Dialog - Class to manage a yui YDumbTab properly

=head1 SYNOPSIS

use ManaTools::Shared::GUI::Dialog;

my $dlg = ManaTools::Shared::GUI::Dialog->new(
    dialogType => ManaTools::Shared::GUI::Dialog->mainDialog, ## or popupDialog
    title => "New Title",
    icon => $icon,
    optFields => [
        ManaTools::Shared::GUI::Dialog->TimeField,
        ManaTools::Shared::GUI::Dialog->DateField,
        ManaTools::Shared::GUI::Dialog->TabField,
    ],
    buttons => [
        ManaTools::Shared::GUI::Dialog->cancelButton,
        ManaTools::Shared::GUI::Dialog->okButton,
    ],
    event_timeout => 0, # event timeout in msec during the waitForEvent()
    layout => sub { my $self = shift; my $layoutstart = shift; my $dlg = $self->dialog(); my $info = $self->info(); ... $self->addWidget('button1', $button, sub{...}, $backendItem1); },
    restoreValues => sub { my $self = shift; $info = {}; ...; return $info },
    result => sub { my $self = shift; ... },
);

my $result = $dlg->call();

## you should call this from the Module, and not directly from here...

=head1 DESCRIPTION

This class wraps the most common dialog functionality


=head1 SUPPORT

You can find documentation for this module with the perldoc command:

perldoc ManaTools::Shared::GUI::Dialog

=head1 AUTHOR

Maarten Vanraes <alien@rmail.be>

=head1 COPYRIGHT and LICENSE

Copyright (C) 2015, Maarten Vanraes.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2, as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA

=head1 FUNCTIONS

=cut


use Moose;
with 'ManaTools::Shared::GUI::EventHandlerRole';

use diagnostics;
use utf8;

use Moose::Util::TypeConstraints;
use MooseX::ClassAttribute;

use yui;
use ManaTools::Shared::GUI::Event;
#=============================================================

=head2 new

=head3 INPUT

    hash ref containing
        module:             the parent ManaTools::Module
        dialogType:         ManaTools::Shared::GUI::Dialog->mainDialog, or popupDialog
        title:              a title
        icon:               an icon
        layout:             a callback that builds the layout of the dialog
        restoreValues:      an optional callback that restore the values to an $info HashRef
        event_timeout:      an optional and rw timeout in msec during the waitForEvent()
                            needs a ManaTools::Shared::GUI::Event to manage the $yui::YEvent::TimeoutEvent
        buttons:            an optional hashref containing
                                ManaTools::Shared::GUI::Dialog->cancelButton and/or
                                ManaTools::Shared::GUI::Dialog->okButton
        result:             an optional callback for returning a result (mostly for popupDialogs)

=head3 DESCRIPTION

    new is inherited from Moose, to create a Dialog object

=cut

#=============================================================

has 'module' => (
    is => 'ro',
    isa => 'ManaTools::Module',
    required => 1,
);

has 'factory' => (
    is => 'ro',
    isa => 'Maybe[yui::YWidgetFactory]',
    lazy => 1,
    init_arg => undef,
    default => sub {
       return yui::YUI::widgetFactory;
    },
);

has 'optFactory' => (
    is => 'ro',
    isa => 'Maybe[yui::YOptionalWidgetFactory]',
    lazy => 1,
    init_arg => undef,
    default => sub {
        return yui::YUI::optionalWidgetFactory;
    },
);

has 'mgaExternalFactory' => (
    is => 'ro',
    isa => 'Maybe[yui::YMGAWidgetFactory]',
    lazy => 1,
    init_arg => undef,
     builder => '_MGAFactoryInitialize'
);

sub _MGAFactoryInitialize {
    my $self = shift();

    $self->factory();  # just to be sure default factory is initialized first

    my $mageiaPlugin = "mga";
    my $mgaFactory   = yui::YExternalWidgets::externalWidgetFactory($mageiaPlugin);
    return yui::YMGAWidgetFactory::getYMGAWidgetFactory($mgaFactory);
}

has 'dialog' => (
    is => 'rw',
    isa => 'Maybe[yui::YDialog]',
    lazy => 1,
    init_arg => undef,
    default => sub {
        return undef;
    },
);

subtype 'TimeoutType'
    => as Int
    => where {($_ >= 0)};

has 'event_timeout' => (
    is => 'rw',
    isa => 'TimeoutType',
    lazy => 1,
    default => 0,
);

subtype 'DialogType'
    => as Int
    => where {($_ > 0 && $_<=2)};

has 'dialogType' => (
    is => 'ro',
    isa => 'DialogType',
    required => 1,
);

class_has 'mainDialog' => (
    is => 'ro',
    isa => 'DialogType',
    init_arg => undef,
    default => sub {return 1;},
);

class_has 'popupDialog' => (
    is => 'ro',
    isa => 'DialogType',
    init_arg => undef,
    default => sub {return 2;},
);

has 'title' => (
    is => 'ro',
    isa => 'Str',
    required => 1,
);

has 'icon' => (
    is => 'ro',
    isa => 'Str',
    required => 1,
);

class_has 'cancelButton' => (
    is => 'ro',
    init_arg => undef,
    default => sub {return 'cancelButton';},
);

class_has 'okButton' => (
    is => 'ro',
    init_arg => undef,
    default => sub {return 'okButton';},
);

class_has 'closeButton' => (
    is => 'ro',
    init_arg => undef,
    default => sub {return 'closeButton';},
);

class_has 'resetButton' => (
    is => 'ro',
    init_arg => undef,
    default => sub {return 'resetButton';},
);

class_has 'aboutButton' => (
    is => 'ro',
    init_arg => undef,
    default => sub {return 'aboutButton';},
);

has 'buttons' => (
    is => 'ro',
    isa => 'HashRef[CodeRef]',
    lazy => 1,
    default => sub {
        return {};
    },
);

class_has 'DateField' => (
    is => 'ro',
    isa => 'Int',
    init_arg => undef,
    default => sub {return 1;},
);
class_has 'TimeField' => (
    is => 'ro',
    isa => 'Int',
    init_arg => undef,
    default => sub {return 2;},
);
class_has 'TabField' => (
    is => 'ro',
    isa => 'Int',
    init_arg => undef,
    default => sub {return 3;},
);


has 'optFields' => (
    is => 'ro',
    isa => 'ArrayRef[Int]',
    lazy => 1,
    default => sub {
        return [];
    },
);

has 'info' => (
    is => 'rw',
    isa => 'HashRef',
    default => sub {
        return {};
    }
);

has 'layout' => (
    is => 'ro',
    isa => 'CodeRef',
    required => 1,
);

has 'restoreValues' => (
    is => 'ro',
    isa => 'Maybe[CodeRef]',
    lazy => 1,
    default => sub {
        return undef;
    },
);

has 'result' => (
    is => 'ro',
    isa => 'Maybe[CodeRef]',
    lazy => 1,
    default => sub {
        return undef;
    }
);

sub loc {
    my $self = shift;
    return $self->module->loc(@_);
}

#=============================================================

=head2 checkFields

=head3 INPUT

    $self: this object

=head3 OUTPUT

    returns true or false depending on success

=head3 DESCRIPTION

    helper function to check if the optional factory has the optFields

=cut

#=============================================================
sub checkFields {
    my $self = shift;
    my $fields = $self->optFields();
    my $optFactory = $self->optFactory();
    for my $field (@{$fields}) {
        return 0 if ($field == ManaTools::Shared::GUI::Dialog->TimeField && !$optFactory->hasTimeField());
        return 0 if ($field == ManaTools::Shared::GUI::Dialog->DateField && !$optFactory->hasDateField());
        return 0 if ($field == ManaTools::Shared::GUI::Dialog->TabField && !$optFactory->hasDumbTab());
    }
    return 1;
}

#=============================================================

=head2 getButton

=head3 INPUT

    $self: this object
    $buttonType: a buttonType

=head3 OUTPUT

    returns the CodeRef associated with the buttonType, or undef

=head3 DESCRIPTION

    gets the CodeRef associated with a buttontype

=cut

#=============================================================
sub getButton {
    my $self = shift;
    my $buttonType = shift;
    my $buttons = $self->buttons;

    return $buttons->{$buttonType} if (defined($buttons->{$buttonType}));
    return undef;
}

#=============================================================

=head2 addButtons

=head3 INPUT

    $self: this object
    $ylayout: a layout widget to base the buttons on

=head3 DESCRIPTION

    add buttons to the layout according to $self->buttons()

=cut

#=============================================================
sub addButtons {
    my $self = shift;
    my $layout = shift;
    my $buttons = $self->buttons;
    return if scalar(keys %{$buttons}) == 0;
    my $factory = $self->factory();

    ### buttons on the last line
    $factory->createVSpacing($layout, 1.0);
    my $buttonbox = $factory->createHBox($layout);

    ## Left side
    my $align = $factory->createLeft($buttonbox);
    my $hbox = $factory->createHBox($align);

    $self->addWidget('aboutButton',
                     $factory->createPushButton($hbox, $self->loc->N("&About")),
                     $self->getButton(ManaTools::Shared::GUI::Dialog->aboutButton)) if $self->getButton(ManaTools::Shared::GUI::Dialog->aboutButton);
    $self->addWidget('resetButton', $factory->createPushButton($hbox, $self->loc->N("&Reset")), $self->getButton(ManaTools::Shared::GUI::Dialog->resetButton)) if $self->getButton(ManaTools::Shared::GUI::Dialog->resetButton);

    ## Right side
    $align = $factory->createRight($buttonbox);
    $hbox = $factory->createHBox($align);
    $self->addWidget('cancelButton', $factory->createPushButton($hbox, $self->loc->N("&Cancel")), $self->getButton(ManaTools::Shared::GUI::Dialog->cancelButton)) if $self->getButton(ManaTools::Shared::GUI::Dialog->cancelButton);
    $self->addWidget('okButton', $factory->createPushButton($hbox, $self->loc->N("&Ok")), $self->getButton(ManaTools::Shared::GUI::Dialog->okButton)) if $self->getButton(ManaTools::Shared::GUI::Dialog->okButton);
    $self->addWidget('closeButton', $factory->createPushButton($hbox, $self->loc->N("&Close")), $self->getButton(ManaTools::Shared::GUI::Dialog->closeButton)) if $self->getButton(ManaTools::Shared::GUI::Dialog->closeButton);
    $factory->createHSpacing($hbox, 1.0);

    ## no changes by default
    my $ydialog = $self->dialog();
    if (defined($self->widget('closeButton'))) {
        $ydialog->setDefaultButton($self->widget('closeButton'));
    }
    elsif (defined($self->widget('cancelButton'))) {
        $ydialog->setDefaultButton($self->widget('cancelButton'));
    }
    elsif (defined($self->widget('okButton'))) {
        $ydialog->setDefaultButton($self->widget('okButton'));
    }
}

#=============================================================

=head2 call

=head3 INPUT

    $self: this object

=head3 OUTPUT

    the result from the dialog

=head3 DESCRIPTION

    Creates a dialog (either popup or main) and handles it's events and resturns the proper result.

=cut

#=============================================================
sub call {
    my $self = shift;
    my $oldAppTitle = yui::YUI::app()->applicationTitle();

    ## set new title to get it in dialog
    yui::YUI::app()->setApplicationTitle($self->title());
    ## set icon if not already set by external launcher
    yui::YUI::app()->setApplicationIcon($self->icon());

    ## required fields
    die "required widgets missing from YOptionalFactory" if (!$self->checkFields());

    ## restore values
    my $restoreValues = $self->restoreValues();
    $self->info($restoreValues->($self)) if defined($restoreValues);

    ## create the dialog
    my $factory = $self->factory();
    $self->dialog($factory->createMainDialog()) if ($self->dialogType() == ManaTools::Shared::GUI::Dialog->mainDialog);
    $self->dialog($factory->createPopupDialog()) if ($self->dialogType() == ManaTools::Shared::GUI::Dialog->popupDialog);
    my $ydialog = $self->dialog();
    my $layoutstart = $ydialog;
    my $vbox = undef;
    if (scalar(keys %{$self->buttons()}) > 0) {
        # we have buttons, so, we need a vbox + hbox basic layout to start with
        $vbox = $factory->createVBox($ydialog);
        $layoutstart = $factory->createHBox($vbox);
    }
    ## if layout returns a YWidget, we can define buttons on it
    $self->addButtons($vbox) if defined($vbox);

    ## build the whole layout
    my $layout = $self->layout->($self, $layoutstart);


    ## add a cancelEvent
    ManaTools::Shared::GUI::Event->new(name => 'cancelEvent', eventHandler => $self, eventType => $yui::YEvent::CancelEvent, event => sub { return 0; });

    # main loop
    while(1) {
        my $yevent = $ydialog->waitForEvent($self->event_timeout);
        last if (!$self->processEvents($yevent));
    }

    my $resultcb = $self->result();
    my $result = 1;
    $result = $resultcb->($self) if defined ($resultcb);

    # end dialog
    $ydialog->destroy();

    #restore old application title
    yui::YUI::app()->setApplicationTitle($oldAppTitle) if $oldAppTitle;
    return $result;
}

no Moose;
__PACKAGE__->meta->make_immutable;


1;
class="hl opt">(); foreach ($hold_ary as $user_id => $forum_ary) { foreach ($forum_ary as $forum_id => $auth_option_ary) { foreach ($auth_option_ary as $auth_option => $auth_setting) { if ($auth_setting) { $auth_ary[$forum_id][$auth_option][] = $user_id; } } } } return $auth_ary; } /** * Cache data to user_permissions row */ function acl_cache(&$userdata) { global $db; // Empty user_permissions $userdata['user_permissions'] = ''; $hold_ary = $this->acl_raw_data_single_user($userdata['user_id']); // Key 0 in $hold_ary are global options, all others are forum_ids // If this user is founder we're going to force fill the admin options ... if ($userdata['user_type'] == USER_FOUNDER) { foreach ($this->acl_options['global'] as $opt => $id) { if (strpos($opt, 'a_') === 0) { $hold_ary[0][$this->acl_options['id'][$opt]] = ACL_YES; } } } $hold_str = $this->build_bitstring($hold_ary); if ($hold_str) { $userdata['user_permissions'] = $hold_str; $sql = 'UPDATE ' . USERS_TABLE . " SET user_permissions = '" . $db->sql_escape($userdata['user_permissions']) . "', user_perm_from = 0 WHERE user_id = " . $userdata['user_id']; $db->sql_query($sql); } return; } /** * Build bitstring from permission set */ function build_bitstring(&$hold_ary) { $hold_str = ''; if (sizeof($hold_ary)) { ksort($hold_ary); $last_f = 0; foreach ($hold_ary as $f => $auth_ary) { $ary_key = (!$f) ? 'global' : 'local'; $bitstring = array(); foreach ($this->acl_options[$ary_key] as $opt => $id) { if (isset($auth_ary[$this->acl_options['id'][$opt]])) { $bitstring[$id] = $auth_ary[$this->acl_options['id'][$opt]]; $option_key = substr($opt, 0, strpos($opt, '_') + 1); // If one option is allowed, the global permission for this option has to be allowed too // example: if the user has the a_ permission this means he has one or more a_* permissions if ($auth_ary[$this->acl_options['id'][$opt]] == ACL_YES && (!isset($bitstring[$this->acl_options[$ary_key][$option_key]]) || $bitstring[$this->acl_options[$ary_key][$option_key]] == ACL_NEVER)) { $bitstring[$this->acl_options[$ary_key][$option_key]] = ACL_YES; } } else { $bitstring[$id] = ACL_NEVER; } } // Now this bitstring defines the permission setting for the current forum $f (or global setting) $bitstring = implode('', $bitstring); // The line number indicates the id, therefore we have to add empty lines for those ids not present $hold_str .= str_repeat("\n", $f - $last_f); // Convert bitstring for storage - we do not use binary/bytes because PHP's string functions are not fully binary safe for ($i = 0, $bit_length = strlen($bitstring); $i < $bit_length; $i += 31) { $hold_str .= str_pad(base_convert(str_pad(substr($bitstring, $i, 31), 31, 0, STR_PAD_RIGHT), 2, 36), 6, 0, STR_PAD_LEFT); } $last_f = $f; } unset($bitstring); $hold_str = rtrim($hold_str); } return $hold_str; } /** * Clear one or all users cached permission settings */ function acl_clear_prefetch($user_id = false) { global $db, $cache; // Rebuild options cache $cache->destroy('_role_cache'); $sql = 'SELECT * FROM ' . ACL_ROLES_DATA_TABLE . ' ORDER BY role_id ASC'; $result = $db->sql_query($sql); $this->role_cache = array(); while ($row = $db->sql_fetchrow($result)) { $this->role_cache[$row['role_id']][$row['auth_option_id']] = (int) $row['auth_setting']; } $db->sql_freeresult($result); foreach ($this->role_cache as $role_id => $role_options) { $this->role_cache[$role_id] = serialize($role_options); } $cache->put('_role_cache', $this->role_cache); // Now empty user permissions $where_sql = ''; if ($user_id !== false) { $user_id = (!is_array($user_id)) ? $user_id = array((int) $user_id) : array_map('intval', $user_id); $where_sql = ' WHERE ' . $db->sql_in_set('user_id', $user_id); } $sql = 'UPDATE ' . USERS_TABLE . " SET user_permissions = '', user_perm_from = 0 $where_sql"; $db->sql_query($sql); return; } /** * Get assigned roles */ function acl_role_data($user_type, $role_type, $ug_id = false, $forum_id = false) { global $db; $roles = array(); $sql_id = ($user_type == 'user') ? 'user_id' : 'group_id'; $sql_ug = ($ug_id !== false) ? ((!is_array($ug_id)) ? "AND a.$sql_id = $ug_id" : 'AND ' . $db->sql_in_set("a.$sql_id", $ug_id)) : ''; $sql_forum = ($forum_id !== false) ? ((!is_array($forum_id)) ? "AND a.forum_id = $forum_id" : 'AND ' . $db->sql_in_set('a.forum_id', $forum_id)) : ''; // Grab assigned roles... $sql = 'SELECT a.auth_role_id, a.' . $sql_id . ', a.forum_id FROM ' . (($user_type == 'user') ? ACL_USERS_TABLE : ACL_GROUPS_TABLE) . ' a, ' . ACL_ROLES_TABLE . " r WHERE a.auth_role_id = r.role_id AND r.role_type = '" . $db->sql_escape($role_type) . "' $sql_ug $sql_forum ORDER BY r.role_order ASC"; $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { $roles[$row[$sql_id]][$row['forum_id']] = $row['auth_role_id']; } $db->sql_freeresult($result); return $roles; } /** * Get raw acl data based on user/option/forum */ function acl_raw_data($user_id = false, $opts = false, $forum_id = false) { global $db; $sql_user = ($user_id !== false) ? ((!is_array($user_id)) ? 'user_id = ' . (int) $user_id : $db->sql_in_set('user_id', array_map('intval', $user_id))) : ''; $sql_forum = ($forum_id !== false) ? ((!is_array($forum_id)) ? 'AND a.forum_id = ' . (int) $forum_id : 'AND ' . $db->sql_in_set('a.forum_id', array_map('intval', $forum_id))) : ''; $sql_opts = $sql_opts_select = $sql_opts_from = ''; $hold_ary = array(); if ($opts !== false) { $sql_opts_select = ', ao.auth_option'; $sql_opts_from = ', ' . ACL_OPTIONS_TABLE . ' ao'; $this->build_auth_option_statement('ao.auth_option', $opts, $sql_opts); } $sql_ary = array(); // Grab non-role settings - user-specific $sql_ary[] = 'SELECT a.user_id, a.forum_id, a.auth_setting, a.auth_option_id' . $sql_opts_select . ' FROM ' . ACL_USERS_TABLE . ' a' . $sql_opts_from . ' WHERE a.auth_role_id = 0 ' . (($sql_opts_from) ? 'AND a.auth_option_id = ao.auth_option_id ' : '') . (($sql_user) ? 'AND a.' . $sql_user : '') . " $sql_forum $sql_opts"; // Now the role settings - user-specific $sql_ary[] = 'SELECT a.user_id, a.forum_id, r.auth_option_id, r.auth_setting, r.auth_option_id' . $sql_opts_select . ' FROM ' . ACL_USERS_TABLE . ' a, ' . ACL_ROLES_DATA_TABLE . ' r' . $sql_opts_from . ' WHERE a.auth_role_id = r.role_id ' . (($sql_opts_from) ? 'AND r.auth_option_id = ao.auth_option_id ' : '') . (($sql_user) ? 'AND a.' . $sql_user : '') . " $sql_forum $sql_opts"; foreach ($sql_ary as $sql) { $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { $option = ($sql_opts_select) ? $row['auth_option'] : $this->acl_options['option'][$row['auth_option_id']]; $hold_ary[$row['user_id']][$row['forum_id']][$option] = $row['auth_setting']; } $db->sql_freeresult($result); } $sql_ary = array(); // Now grab group settings - non-role specific... $sql_ary[] = 'SELECT ug.user_id, a.forum_id, a.auth_setting, a.auth_option_id' . $sql_opts_select . ' FROM ' . ACL_GROUPS_TABLE . ' a, ' . USER_GROUP_TABLE . ' ug, ' . GROUPS_TABLE . ' g' . $sql_opts_from . ' WHERE a.auth_role_id = 0 ' . (($sql_opts_from) ? 'AND a.auth_option_id = ao.auth_option_id ' : '') . ' AND a.group_id = ug.group_id AND g.group_id = ug.group_id AND ug.user_pending = 0 AND NOT (ug.group_leader = 1 AND g.group_skip_auth = 1) ' . (($sql_user) ? 'AND ug.' . $sql_user : '') . " $sql_forum $sql_opts"; // Now grab group settings - role specific... $sql_ary[] = 'SELECT ug.user_id, a.forum_id, r.auth_setting, r.auth_option_id' . $sql_opts_select . ' FROM ' . ACL_GROUPS_TABLE . ' a, ' . USER_GROUP_TABLE . ' ug, ' . GROUPS_TABLE . ' g, ' . ACL_ROLES_DATA_TABLE . ' r' . $sql_opts_from . ' WHERE a.auth_role_id = r.role_id ' . (($sql_opts_from) ? 'AND r.auth_option_id = ao.auth_option_id ' : '') . ' AND a.group_id = ug.group_id AND g.group_id = ug.group_id AND ug.user_pending = 0 AND NOT (ug.group_leader = 1 AND g.group_skip_auth = 1) ' . (($sql_user) ? 'AND ug.' . $sql_user : '') . " $sql_forum $sql_opts"; foreach ($sql_ary as $sql) { $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { $option = ($sql_opts_select) ? $row['auth_option'] : $this->acl_options['option'][$row['auth_option_id']]; if (!isset($hold_ary[$row['user_id']][$row['forum_id']][$option]) || (isset($hold_ary[$row['user_id']][$row['forum_id']][$option]) && $hold_ary[$row['user_id']][$row['forum_id']][$option] != ACL_NEVER)) { $hold_ary[$row['user_id']][$row['forum_id']][$option] = $row['auth_setting']; // If we detect ACL_NEVER, we will unset the flag option (within building the bitstring it is correctly set again) if ($row['auth_setting'] == ACL_NEVER) { $flag = substr($option, 0, strpos($option, '_') + 1); if (isset($hold_ary[$row['user_id']][$row['forum_id']][$flag]) && $hold_ary[$row['user_id']][$row['forum_id']][$flag] == ACL_YES) { unset($hold_ary[$row['user_id']][$row['forum_id']][$flag]); /* if (in_array(ACL_YES, $hold_ary[$row['user_id']][$row['forum_id']])) { $hold_ary[$row['user_id']][$row['forum_id']][$flag] = ACL_YES; } */ } } } } $db->sql_freeresult($result); } return $hold_ary; } /** * Get raw user based permission settings */ function acl_user_raw_data($user_id = false, $opts = false, $forum_id = false) { global $db; $sql_user = ($user_id !== false) ? ((!is_array($user_id)) ? 'user_id = ' . (int) $user_id : $db->sql_in_set('user_id', array_map('intval', $user_id))) : ''; $sql_forum = ($forum_id !== false) ? ((!is_array($forum_id)) ? 'AND a.forum_id = ' . (int) $forum_id : 'AND ' . $db->sql_in_set('a.forum_id', array_map('intval', $forum_id))) : ''; $sql_opts = ''; $hold_ary = $sql_ary = array(); if ($opts !== false) { $this->build_auth_option_statement('ao.auth_option', $opts, $sql_opts); } // Grab user settings - non-role specific... $sql_ary[] = 'SELECT a.user_id, a.forum_id, a.auth_setting, a.auth_option_id, ao.auth_option FROM ' . ACL_USERS_TABLE . ' a, ' . ACL_OPTIONS_TABLE . ' ao WHERE a.auth_role_id = 0 AND a.auth_option_id = ao.auth_option_id ' . (($sql_user) ? 'AND a.' . $sql_user : '') . " $sql_forum $sql_opts ORDER BY a.forum_id, ao.auth_option"; // Now the role settings - user-specific $sql_ary[] = 'SELECT a.user_id, a.forum_id, r.auth_option_id, r.auth_setting, r.auth_option_id, ao.auth_option FROM ' . ACL_USERS_TABLE . ' a, ' . ACL_ROLES_DATA_TABLE . ' r, ' . ACL_OPTIONS_TABLE . ' ao WHERE a.auth_role_id = r.role_id AND r.auth_option_id = ao.auth_option_id ' . (($sql_user) ? 'AND a.' . $sql_user : '') . " $sql_forum $sql_opts ORDER BY a.forum_id, ao.auth_option"; foreach ($sql_ary as $sql) { $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { $hold_ary[$row['user_id']][$row['forum_id']][$row['auth_option']] = $row['auth_setting']; } $db->sql_freeresult($result); } return $hold_ary; } /** * Get raw group based permission settings */ function acl_group_raw_data($group_id = false, $opts = false, $forum_id = false) { global $db; $sql_group = ($group_id !== false) ? ((!is_array($group_id)) ? 'group_id = ' . (int) $group_id : $db->sql_in_set('group_id', array_map('intval', $group_id))) : ''; $sql_forum = ($forum_id !== false) ? ((!is_array($forum_id)) ? 'AND a.forum_id = ' . (int) $forum_id : 'AND ' . $db->sql_in_set('a.forum_id', array_map('intval', $forum_id))) : ''; $sql_opts = ''; $hold_ary = $sql_ary = array(); if ($opts !== false) { $this->build_auth_option_statement('ao.auth_option', $opts, $sql_opts); } // Grab group settings - non-role specific... $sql_ary[] = 'SELECT a.group_id, a.forum_id, a.auth_setting, a.auth_option_id, ao.auth_option FROM ' . ACL_GROUPS_TABLE . ' a, ' . ACL_OPTIONS_TABLE . ' ao WHERE a.auth_role_id = 0 AND a.auth_option_id = ao.auth_option_id ' . (($sql_group) ? 'AND a.' . $sql_group : '') . " $sql_forum $sql_opts ORDER BY a.forum_id, ao.auth_option"; // Now grab group settings - role specific... $sql_ary[] = 'SELECT a.group_id, a.forum_id, r.auth_setting, r.auth_option_id, ao.auth_option FROM ' . ACL_GROUPS_TABLE . ' a, ' . ACL_ROLES_DATA_TABLE . ' r, ' . ACL_OPTIONS_TABLE . ' ao WHERE a.auth_role_id = r.role_id AND r.auth_option_id = ao.auth_option_id ' . (($sql_group) ? 'AND a.' . $sql_group : '') . " $sql_forum $sql_opts ORDER BY a.forum_id, ao.auth_option"; foreach ($sql_ary as $sql) { $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { $hold_ary[$row['group_id']][$row['forum_id']][$row['auth_option']] = $row['auth_setting']; } $db->sql_freeresult($result); } return $hold_ary; } /** * Get raw acl data based on user for caching user_permissions * This function returns the same data as acl_raw_data(), but without the user id as the first key within the array. */ function acl_raw_data_single_user($user_id) { global $db, $cache; // Check if the role-cache is there if (($this->role_cache = $cache->get('_role_cache')) === false) { $this->role_cache = array(); // We pre-fetch roles $sql = 'SELECT * FROM ' . ACL_ROLES_DATA_TABLE . ' ORDER BY role_id ASC'; $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { $this->role_cache[$row['role_id']][$row['auth_option_id']] = (int) $row['auth_setting']; } $db->sql_freeresult($result); foreach ($this->role_cache as $role_id => $role_options) { $this->role_cache[$role_id] = serialize($role_options); } $cache->put('_role_cache', $this->role_cache); } $hold_ary = array(); // Grab user-specific permission settings $sql = 'SELECT forum_id, auth_option_id, auth_role_id, auth_setting FROM ' . ACL_USERS_TABLE . ' WHERE user_id = ' . $user_id; $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { // If a role is assigned, assign all options included within this role. Else, only set this one option. if ($row['auth_role_id']) { $hold_ary[$row['forum_id']] = (empty($hold_ary[$row['forum_id']])) ? unserialize($this->role_cache[$row['auth_role_id']]) : $hold_ary[$row['forum_id']] + unserialize($this->role_cache[$row['auth_role_id']]); } else { $hold_ary[$row['forum_id']][$row['auth_option_id']] = $row['auth_setting']; } } $db->sql_freeresult($result); // Now grab group-specific permission settings $sql = 'SELECT a.forum_id, a.auth_option_id, a.auth_role_id, a.auth_setting FROM ' . ACL_GROUPS_TABLE . ' a, ' . USER_GROUP_TABLE . ' ug, ' . GROUPS_TABLE . ' g WHERE a.group_id = ug.group_id AND g.group_id = ug.group_id AND ug.user_pending = 0 AND NOT (ug.group_leader = 1 AND g.group_skip_auth = 1) AND ug.user_id = ' . $user_id; $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { if (!$row['auth_role_id']) { $this->_set_group_hold_ary($hold_ary[$row['forum_id']], $row['auth_option_id'], $row['auth_setting']); } else if (!empty($this->role_cache[$row['auth_role_id']])) { foreach (unserialize($this->role_cache[$row['auth_role_id']]) as $option_id => $setting) { $this->_set_group_hold_ary($hold_ary[$row['forum_id']], $option_id, $setting); } } } $db->sql_freeresult($result); return $hold_ary; } /** * Private function snippet for setting a specific piece of the hold_ary */ function _set_group_hold_ary(&$hold_ary, $option_id, $setting) { if (!isset($hold_ary[$option_id]) || (isset($hold_ary[$option_id]) && $hold_ary[$option_id] != ACL_NEVER)) { $hold_ary[$option_id] = $setting; // If we detect ACL_NEVER, we will unset the flag option (within building the bitstring it is correctly set again) if ($setting == ACL_NEVER) { $flag = substr($this->acl_options['option'][$option_id], 0, strpos($this->acl_options['option'][$option_id], '_') + 1); $flag = (int) $this->acl_options['id'][$flag]; if (isset($hold_ary[$flag]) && $hold_ary[$flag] == ACL_YES) { unset($hold_ary[$flag]); /* This is uncommented, because i suspect this being slightly wrong due to mixed permission classes being possible if (in_array(ACL_YES, $hold_ary)) { $hold_ary[$flag] = ACL_YES; }*/ } } } } /** * Authentication plug-ins is largely down to Sergey Kanareykin, our thanks to him. */ function login($username, $password, $autologin = false, $viewonline = 1, $admin = 0) { global $config, $db, $user, $phpbb_root_path, $phpEx; $method = trim(basename($config['auth_method'])); include_once($phpbb_root_path . 'includes/auth/auth_' . $method . '.' . $phpEx); $method = 'login_' . $method; if (function_exists($method)) { $login = $method($username, $password); // If the auth module wants us to create an empty profile do so and then treat the status as LOGIN_SUCCESS if ($login['status'] == LOGIN_SUCCESS_CREATE_PROFILE) { // we are going to use the user_add function so include functions_user.php if it wasn't defined yet if (!function_exists('user_add')) { include($phpbb_root_path . 'includes/functions_user.' . $phpEx); } user_add($login['user_row'], (isset($login['cp_data'])) ? $login['cp_data'] : false); $sql = 'SELECT user_id, username, user_password, user_passchg, user_email, user_type FROM ' . USERS_TABLE . " WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($username)) . "'"; $result = $db->sql_query($sql); $row = $db->sql_fetchrow($result); $db->sql_freeresult($result); if (!$row) { return array( 'status' => LOGIN_ERROR_EXTERNAL_AUTH, 'error_msg' => 'AUTH_NO_PROFILE_CREATED', 'user_row' => array('user_id' => ANONYMOUS), ); } $login = array( 'status' => LOGIN_SUCCESS, 'error_msg' => false, 'user_row' => $row, ); } // If login succeeded, we will log the user in... else we pass the login array through... if ($login['status'] == LOGIN_SUCCESS) { $old_session_id = $user->session_id; if ($admin) { global $SID, $_SID; $cookie_expire = time() - 31536000; $user->set_cookie('u', '', $cookie_expire); $user->set_cookie('sid', '', $cookie_expire); unset($cookie_expire); $SID = '?sid='; $user->session_id = $_SID = ''; } $result = $user->session_create($login['user_row']['user_id'], $admin, $autologin, $viewonline); // Successful session creation if ($result === true) { // If admin re-authentication we remove the old session entry because a new one has been created... if ($admin) { // the login array is used because the user ids do not differ for re-authentication $sql = 'DELETE FROM ' . SESSIONS_TABLE . " WHERE session_id = '" . $db->sql_escape($old_session_id) . "' AND session_user_id = {$login['user_row']['user_id']}"; $db->sql_query($sql); } return array( 'status' => LOGIN_SUCCESS, 'error_msg' => false, 'user_row' => $login['user_row'], ); } return array( 'status' => LOGIN_BREAK, 'error_msg' => $result, 'user_row' => $login['user_row'], ); } return $login; } trigger_error('Authentication method not found', E_USER_ERROR); } /** * Fill auth_option statement for later querying based on the supplied options */ function build_auth_option_statement($key, $auth_options, &$sql_opts) { global $db; if (!is_array($auth_options)) { if (strpos($auth_options, '%') !== false) { $sql_opts = "AND $key " . $db->sql_like_expression(str_replace('%', $db->any_char, $auth_options)); } else { $sql_opts = "AND $key = '" . $db->sql_escape($auth_options) . "'"; } } else { $is_like_expression = false; foreach ($auth_options as $option) { if (strpos($option, '%') !== false) { $is_like_expression = true; } } if (!$is_like_expression) { $sql_opts = 'AND ' . $db->sql_in_set($key, $auth_options); } else { $sql = array(); foreach ($auth_options as $option) { if (strpos($option, '%') !== false) { $sql[] = $key . ' ' . $db->sql_like_expression(str_replace('%', $db->any_char, $option)); } else { $sql[] = $key . " = '" . $db->sql_escape($option) . "'"; } } $sql_opts = 'AND (' . implode(' OR ', $sql) . ')'; } } } } ?>