class_Action.inc 5.32 KB
Newer Older
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
<?php
/*
  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
  Copyright (C) 2017-2018  FusionDirectory

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

  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/

/*!
 * \brief Action base class
 */
class Action
{
  protected $name;
  protected $label;
  protected $icon;

30
  /* 0, 1, ?, + or * */
31
32
  protected $targets;

33
34
  protected $acl;

35
36
37
38
39
  /* Booleans */
  protected $inmenu;
  protected $inline;

  protected $callable;
40
  protected $enabledCallable;
41
42
43
44

  protected $minTargets;
  protected $maxTargets;

45
46
  protected $separator = FALSE;

47
  function __construct($name, $label, $icon, $targets, $callable, array $acl = array(), $inmenu = TRUE, $inline = TRUE)
48
49
50
51
52
53
54
55
56
57
58
59
  {
    if ($targets == '0') {
      $inline = FALSE;
    }

    $this->name     = $name;
    $this->label    = $label;
    $this->icon     = $icon;
    $this->targets  = $targets;
    $this->callable = $callable;
    $this->inmenu   = $inmenu;
    $this->inline   = $inline;
60
    $this->acl      = $acl;
61
62
63
64
65
66
67
68
69
70

    switch ($targets) {
      case '0':
        $this->minTargets = 0;
        $this->maxTargets = 0;
        break;
      case '1':
        $this->minTargets = 1;
        $this->maxTargets = 1;
        break;
71
72
73
74
      case '?':
        $this->minTargets = 0;
        $this->maxTargets = 1;
        break;
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
      case '+':
        $this->minTargets = 1;
        $this->maxTargets = FALSE;
        break;
      case '*':
        $this->minTargets = 0;
        $this->maxTargets = FALSE;
        break;
      default:
        throw new Exception('Invalid targets value for action '.$name.': '.$targets);
    }
  }

  function getName()
  {
    return $this->name;
  }

  function getLabel()
  {
    return $this->label;
  }

98
99
100
101
102
  function setSeparator($bool)
  {
    $this->separator = $bool;
  }

103
104
105
106
107
  function setEnableFunction(callable $callable)
  {
    $this->enabledCallable = $callable;
  }

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  function listActions()
  {
    return array($this->name);
  }

  function execute($management, $action)
  {
    if ($this->callable === FALSE) {
      return;
    }
    if (count($action['targets']) < $this->minTargets) {
      throw new Exception('Not enough targets ('.count($action['targets']).') passed for action '.$name);
    }
    if (($this->maxTargets !== FALSE) && (count($action['targets']) > $this->maxTargets)) {
      throw new Exception('Too many targets ('.count($action['targets']).') passed for action '.$name);
    }
    $func = $this->callable;
    if (!is_array($func)) {
      $func = array($management, $func);
    }
    return call_user_func($func, $action);
  }

131
  function fillMenuItems(&$actions)
132
133
  {
    if (!$this->inmenu) {
134
      return;
135
    }
136

137
    $actions[] = array(
138
139
140
      'name'      => $this->name,
      'icon'      => $this->icon,
      'label'     => $this->label,
141
      'enabled'   => $this->isEnabledFor(),
142
      'separator' => $this->separator,
143
    );
144
  }
145

146
  function fillRowClasses(&$classes, ListingEntry $entry)
147
148
149
  {
  }

150
  function renderColumnIcons(ListingEntry $entry)
151
  {
152
153
154
155
    if (!$this->inline) {
      return '';
    }

156
    // Skip the entry completely if there's no permission to execute it
157
    if (!$this->hasPermission($entry->dn, $entry->getTemplatedType(), $entry->isTemplate())) {
158
      return '<img src="images/empty.png" alt=" " class="optional"/>';
159
    }
160

161
162
163
164
    if (!$this->isEnabledFor($entry)) {
      return '<img src="'.htmlentities($this->icon.'&disabled=1', ENT_COMPAT, 'UTF-8').'"'.
              ' title="'.$this->label.'" alt="'.$this->label.'"/>';
    }
165
166

    // Render
167
    return '<input type="image" src="'.htmlentities($this->icon, ENT_COMPAT, 'UTF-8').'"'.
168
            ' title="'.$this->label.'" alt="'.$this->label.'" name="listing_'.$this->name.'_'.$entry->row.'"/>';
169
  }
170

171
172
173
174
175
176
177
178
  function isEnabledFor(ListingEntry $entry = NULL)
  {
    if (isset($this->enabledCallable)) {
      return call_user_func($this->enabledCallable, $this->name, $entry);
    }
    return TRUE;
  }

179
  function hasPermission($dn, $type, $template)
180
181
182
183
184
185
186
187
188
189
190
  {
    global $ui;

    $infos  = objects::infos($type);
    foreach ($this->acl as $acl) {
      if (preg_match('/([a-zA-Z0-9]+):([rwcdm]+)/', $acl, $m)) {
        /* attribute:rw */
        $module   = $ui->getAttributeCategory($type, $m[1]);
        $checkAcl = $ui->get_permissions($dn, $module, $m[1]);
        $acl      = $m[2];
      } else {
191
192
193
194
195
        if ($template) {
          $module = $infos['aclCategory'].'/template';
        } else {
          $module = $infos['aclCategory'].'/'.$infos['mainTab'];
        }
196
197
198
199
200
201
202
203
204
205
206
207
208
209
        $checkAcl = $ui->get_permissions($dn, $module, '0');
      }

      // Split up remaining part of the acl and check rights
      $parts = str_split($acl);
      foreach ($parts as $part) {
        if (strpos($checkAcl, $part) === FALSE) {
          return FALSE;
        }
      }
    }

    return TRUE;
  }
210
}