class_dashBoardSystems.inc 8 KB
Newer Older
1
2
<?php
/*
3
4
5
  This code is part of FusionDirectory (http://www.fusiondirectory.org)
  Copyright (C) 2010 Antoine Gallavardin
  Copyright (C) 2011-2016 FusionDirectory project
6

7
8
9
10
  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.
11

12
13
14
15
  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.
16

17
18
19
20
  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.
*/
21
22
23

class dashboardSystems extends simplePlugin
{
24
  /* default values */
25
26
  var $default_start_computer_id = 0;

27
  static function plInfo (): array
28
  {
29
    return [
30
      'plShortName'   => _('Systems'),
31
      'plDescription' => _('Statistics and information about systems'),
32
      'plObjectType'  => ['dashboard'],
33
      'plPriority'    => 20,
34

35
36
      'plProvidedAcls'  => []
    ];
37
38
  }

39
  static function getAttributesInfo (): array
40
  {
41
42
    return [
      'stats' => [
43
        'name'  => _('Statistics'),
44
        'attrs' => [new FakeAttribute('stats')],
45
        'template' => get_template_path('systems_stats.tpl', TRUE, dirname(__FILE__)),
46
47
      ],
      'pc_ids' => [
48
        'name'  => _('Computer name to use by unit'),
49
        'attrs' => [new FakeAttribute('pc_ids')],
50
        'template' => get_template_path('systems_pcids.tpl', TRUE, dirname(__FILE__)),
51
52
      ],
    ];
53
54
55
  }

  function __construct ($dn = NULL, $object = NULL, $parent = NULL, $mainTab = FALSE)
56
  {
57
    parent::__construct($dn, $object, $parent, $mainTab);
58

59
    $this->stats = [
60
61
      'systems' => $this->systems_stats(),
      'argonaut' => $this->argonaut_stats(),
62
    ];
63
64
65
    $this->pc_ids = $this->computer_ids_rules();
  }

Côme Chilliet's avatar
Côme Chilliet committed
66
  protected function systems_stats ()
67
  {
68
69
    global $config;
    $ldap = $config->get_ldap_link();
70
71

    /* Statistics */
72
73
    $stats = [
      ['name'  => _('Workstations'),
74
75
            'type'  => 'workstation',
            'img'   => 'geticon.php?context=devices&icon=computer&size=16'
76
77
      ],
      ['name'  => _('Servers'),
78
79
            'type'  => 'server',
            'img'   => 'geticon.php?context=devices&icon=server&size=16'
80
81
      ],
      ['name'    => _('Windows Workstations'),
82
83
            'type'    => 'workstation',
            'filter'  => '(objectClass=sambaSamAccount)',
84
            'img'     => 'geticon.php?context=devices&icon=computer-windows&size=16'
85
86
      ],
      ['name'  => _('Terminals'),
87
88
            'type'  => 'terminal',
            'img'   => 'geticon.php?context=devices&icon=terminal&size=16'
89
90
      ],
      ['name'  => _('Printers'),
91
92
            'type'  => 'printer',
            'img'   => 'geticon.php?context=devices&icon=printer&size=16'
93
94
      ],
      ['name'  => _('Phones'),
95
96
            'type'  => 'phone',
            'img'   => 'geticon.php?context=devices&icon=telephone&size=16'
97
98
      ],
      ['name'  => _('Components'),
99
100
            'type'  => 'component',
            'img'   => 'geticon.php?context=devices&icon=network-device&size=16'
101
102
      ],
      ['name'  => _('Mobile phones'),
103
            'type'  => 'mobilePhone',
104
105
            'img'   => 'geticon.php?context=devices&icon=phone&size=16'],
    ];
106

107
108
109
110
111
112
113
114
115
116
117
118
119
    foreach ($stats as $key => &$stat) {
      try {
        $stat['nb'] = count(objects::ls($stat['type'], NULL, NULL, (isset($stat['filter']) ? $stat['filter'] : ''), TRUE));
        if ($stat['nb'] == 0) {
          unset($stats[$key]);
        }
      } catch (FusionDirectoryException $e) {
        unset($stats[$key]);
        msg_dialog::display(
          _('LDAP error'),
          sprintf(_('Statistics for type "%s" could not be computed because of the following error: %s'), $stat['type'], $e->getMessage()),
          ERROR_DIALOG
        );
120
      }
121
122
123
124
125
126
    }
    unset($stat);

    return $stats;
  }

Côme Chilliet's avatar
Côme Chilliet committed
127
  protected function argonaut_stats ()
128
  {
129
130
    global $config, $ui;

Côme Chilliet's avatar
Côme Chilliet committed
131
132
133
134
    if (!class_available('argonautServer')) {
      return FALSE;
    }

135
136
    $argonaut_servers = objects::ls(
      'server',
137
      ['cn' => 'raw','ipHostNumber' => 'raw','argonautProtocol' => 'raw','argonautPort' => 'raw'],
138
139
140
141
142
      NULL,
      '(objectClass=argonautServer)',
      TRUE
    );
    $nb_argonaut_server = count($argonaut_servers);
143
    $argonaut_server    = [];
144
    if ($nb_argonaut_server == 1) {
145
      $attrs = reset($argonaut_servers);
146
      foreach (['cn','ipHostNumber','argonautProtocol','argonautPort'] as $key) {
147
148
        $argonaut_server[$key] = $attrs[$key][0];
      }
149
      $argonaut_server['link'] = objects::link(key($argonaut_servers), 'server', 'service_argonautServer', $attrs);
150
151
152
153
154
155
156
    }

    if (strpos($ui->get_permissions($config->current['BASE'], 'server/argonautClient', ''), 'r') === FALSE) {
      $nb_argonaut_clients = 0;
    } else {
      $ldap = $config->get_ldap_link();
      $ldap->cd($config->current['BASE']);
157
      $ldap->search('(objectClass=argonautClient)', ['cn']);
158
      $nb_argonaut_clients = $ldap->count();
159
160
    }

161
    return [
162
163
164
      'nb_servers'  => $nb_argonaut_server,
      'server'      => $argonaut_server,
      'nb_clients'  => $nb_argonaut_clients,
165
    ];
166
167
  }

Côme Chilliet's avatar
Côme Chilliet committed
168
  protected function computer_ids_rules ()
169
  {
170
171
172
    global $config;
    $ldap = $config->get_ldap_link();
    $ldap->cd($config->current['BASE']);
173
174
175
176
177
178
179
180
181
182
183
184
    /* Begin of code for selecting next computer IDs
    * Global variable is board_next_computer_ids_rule
    * syntax is :
    * 7 : nb total of digit
    * LYP3 : prefix to use
    * each prefix is followed by the first item flagged by an equal sign
    * ex : "7;LYP0=3;LYP1=6;LYP2;LYP3"
    * if to item is doned it's 0 by default
    *
    */

    $output_next_computer_ids = "";
185
    $computer_ids_rules = $config->get_cfg_value('dashboardPrefix', ['PC']);
186
    if (!is_array($computer_ids_rules)) {
187
      $computer_ids_rules = [$computer_ids_rules];
188
189
    }

190
    $nb_digits = $config->get_cfg_value('dashboardNumberOfDigit', 3);
191
192
193
194

    /* running all the table */
    foreach ($computer_ids_rules as $rule) {
      /* aray initialization*/
195
196
197
      $array_complete_list  = [];
      $array_real_list      = [];
      $unused_computer_ids  = [];
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
      /* get computer ids configuration */
      $config_ids = explode("=", $rule);
      /* fist is is the prefix */
      $prefix = $config_ids[0];
      /* second (if specified) is the first item */
      if (!isset($config_ids[1])) {
        $start_id = $this->default_start_computer_id;
      } else {
        $start_id = $config_ids[1];
      }

      $output_next_computer_ids = $output_next_computer_ids.'<tr><th style="padding:4px;border:1px solid #BBB;">'.$prefix."</th>";
      $nb_digits_suffix         = $nb_digits - strlen($prefix);

      /* generation of list of suffixe */
213
      for ($d = $start_id;$d < (10 ** $nb_digits_suffix);$d++) {
214
215
216
217
218
        /* padding : 34 on 4 digit become : 0034*/
        $array_complete_list[] = str_pad($d, $nb_digits_suffix, "0", STR_PAD_LEFT);
      }

      /* request of all computer beginning by the prefix */
219
      $request = '(&(cn='.$prefix.'*)(ou:dn:=systems))';
220
      $ldap->search($request, ["cn"]);
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
      while ($attrs = $ldap->fetch()) {
        /* if a computer is a windows host, we have to delete the $ at the end */
        $computer_id        = str_replace("$", "", $attrs["cn"][0]);
        $array_real_list[]  = substr($computer_id, -$nb_digits_suffix);
      }
      /* make dfference between real and complete list */
      $unused_computer_ids = array_diff($array_complete_list, $array_real_list);
      asort($unused_computer_ids);
      $unused_computer_ids = array_values($unused_computer_ids);
      /* we take the 5 first */

      for ($r = 0;$r <= 5;$r++) {
        $output_next_computer_ids .= '<td style="padding:4px;border-bottom:1px solid #BBB;">'.$prefix.$unused_computer_ids[$r]."</td>";
      }
      $output_next_computer_ids .= "</tr>";
    }

    return $output_next_computer_ids;
  }
}