class_Action.inc 4.86 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
40
41
42
43
  /* Booleans */
  protected $inmenu;
  protected $inline;

  protected $callable;

  protected $minTargets;
  protected $maxTargets;

44
45
  protected $separator = FALSE;

46
  function __construct($name, $label, $icon, $targets, $callable, array $acl = array(), $inmenu = TRUE, $inline = TRUE)
47
48
49
50
51
52
53
54
55
56
57
58
  {
    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;
59
    $this->acl      = $acl;
60
61
62
63
64
65
66
67
68
69

    switch ($targets) {
      case '0':
        $this->minTargets = 0;
        $this->maxTargets = 0;
        break;
      case '1':
        $this->minTargets = 1;
        $this->maxTargets = 1;
        break;
70
71
72
73
      case '?':
        $this->minTargets = 0;
        $this->maxTargets = 1;
        break;
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
      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;
  }

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

102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  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);
  }

125
  function fillMenuItems(&$actions)
126
127
  {
    if (!$this->inmenu) {
128
      return;
129
    }
130
    $actions[] = array(
131
132
133
134
      'name'      => $this->name,
      'icon'      => $this->icon,
      'label'     => $this->label,
      'separator' => $this->separator,
135
    );
136
  }
137

138
  function getRowClasses(ListingEntry $entry)
139
140
141
142
  {
    return array();
  }

143
  function renderColumnIcons(ListingEntry $entry)
144
  {
145
146
147
148
    if (!$this->inline) {
      return '';
    }

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

    // TODO? Skip entry if the pseudo filter does not fit
    // TODO? Skip depending on type

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

162
  function hasPermission($dn, $type, $template)
163
164
165
166
167
168
169
170
171
172
173
  {
    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 {
174
175
176
177
178
        if ($template) {
          $module = $infos['aclCategory'].'/template';
        } else {
          $module = $infos['aclCategory'].'/'.$infos['mainTab'];
        }
179
180
181
182
183
184
185
186
187
188
189
190
191
192
        $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;
  }
193
}