File: class_dashBoardSystems.inc

package info (click to toggle)
fusiondirectory 1.0.19-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 32,060 kB
  • sloc: php: 47,469; pascal: 2,993; perl: 2,431; xml: 1,822; sh: 136; makefile: 19
file content (209 lines) | stat: -rw-r--r-- 7,740 bytes parent folder | download
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
<?php
/*
  This code is part of FusionDirectory (http://www.fusiondirectory.org)
  Copyright (C) 2010 Antoine Gallavardin
  Copyright (C) 2011-2016 FusionDirectory project

  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.
*/

class dashboardSystems extends simplePlugin
{
  var $default_start_computer_id = 0;

  static function plInfo()
  {
    return array(
      'plShortName'   => _('Systems'),
      'plDescription' => _('Statistics and information about systems'),
      'plObjectType'  => array('dashboard'),

      'plProvidedAcls'  => array()
    );
  }

  static function getAttributesInfo()
  {
    return array(
      'stats' => array(
        'name'  => _('Statistics'),
        'attrs' => array(new FakeAttribute('stats')),
        'template' => get_template_path('systems_stats.tpl', TRUE, dirname(__FILE__)),
      ),
      'pc_ids' => array(
        'name'  => _('Computer name to use by unit'),
        'attrs' => array(new FakeAttribute('pc_ids')),
        'template' => get_template_path('systems_pcids.tpl', TRUE, dirname(__FILE__)),
      ),
    );
  }

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

    $this->stats = array(
      'systems' => $this->systems_stats(),
      'argonaut' => $this->argonaut_stats(),
    );
    $this->pc_ids = $this->computer_ids_rules();
  }

  function systems_stats ()
  {
    global $config;
    $ldap = $config->get_ldap_link();
    $ldap->cd($config->current['BASE']);

    /* Statistics */
    $stats = array(
      array('name' => _('Workstations'),
            'filter' => 'objectClass=gotoWorkstation',
            'img' => 'geticon.php?context=devices&icon=computer&size=16'),
      array('name' => _('Servers'),
            'filter' => 'objectClass=goServer',
            'img' => 'geticon.php?context=devices&icon=server&size=16'),
      array('name' => _('Windows Workstations'),
            'filter' => '(&(objectClass=sambaSamAccount)(uid=*$))',
            'img' => 'geticon.php?context=devices&icon=computer-windows&size=16'),
      array('name' => _('Terminals'),
            'filter' => 'objectClass=gotoTerminal',
            'img' => 'geticon.php?context=devices&icon=terminal&size=16'),
      array('name' => _('Printers'),
            'filter' => 'objectClass=fdPrinter',
            'img' => 'geticon.php?context=devices&icon=printer&size=16'),
      array('name' => _('Phones'),
            'filter' => 'objectClass=fdPhone',
            'img' => 'geticon.php?context=devices&icon=telephone&size=16'),
      array('name' => _('Components'),
            'filter' => 'objectClass=ieee802Device',
            'img' => 'geticon.php?context=devices&icon=network-device&size=16'),
      array('name' => _('Mobile phones'),
            'filter' => 'objectClass=fdMobilePhone',
            'img' => 'geticon.php?context=devices&icon=phone&size=16'),
    );

    $ldap->cd($config->current['BASE']);
    foreach ($stats as &$stat) {
      $ldap->search($stat['filter'], array("cn"));
      $stat['nb'] = $ldap->count();
    }
    unset($stat);

    return $stats;
  }

  function argonaut_stats ()
  {
    global $config;
    $ldap = $config->get_ldap_link();
    $ldap->cd($config->current['BASE']);
    $ldap->search("(objectClass=argonautServer)", array('cn','ipHostNumber','argonautProtocol','argonautPort'));
    $nb_argonaut_server = $ldap->count();
    $argonaut_server = array();
    if ($nb_argonaut_server == 1) {
      $attrs = $ldap->fetch();
      foreach (array('cn','ipHostNumber','argonautProtocol','argonautPort') as $key) {
        $argonaut_server[$key] = $attrs[$key][0];
      }
      $argonaut_server['link'] = objects::link($attrs['dn'], 'server', 'service_serviceArgonaut', $attrs);
    }
    $ldap->search("(objectClass=argonautClient)", array('cn'));
    $nb_argonaut_clients = $ldap->count();

    return array(
      'nb_servers'  => $nb_argonaut_server,
      'server'      => $argonaut_server,
      'nb_clients'  => $nb_argonaut_clients,
    );
  }

  function computer_ids_rules ()
  {
    global $config;
    $ldap = $config->get_ldap_link();
    $ldap->cd($config->current['BASE']);
    /* 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 = "";
    $computer_ids_rules = $config->get_cfg_value('dashboardPrefix', array('PC'));
    if (!is_array($computer_ids_rules)) {
      $computer_ids_rules = array($computer_ids_rules);
    }

    /* $get_cfg_value return the OU like ou=computer .. */
    /* FIXME use get_ou */
    $winstation_ou  = mb_substr($config->get_cfg_value('sambaMachineAccountRDN'), 3);
    $nb_digits      = $config->get_cfg_value('dashboardNumberOfDigit', 3);

    /* running all the table */
    foreach ($computer_ids_rules as $rule) {
      /* aray initialization*/
      $array_complete_list  = array();
      $array_real_list      = array();
      $unused_computer_ids  = array();
      /* 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 */
      for ($d = $start_id;$d < pow(10, $nb_digits_suffix);$d++) {
        /* 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 */
      $request = "(&(|(objectClass=sambaSamAccount)(objectClass=GOhard))(cn=$prefix*)(|(ou:dn:=$winstation_ou)(ou:dn:=systems)))";
      $ldap->search($request, array("cn"));
      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;
  }
}
?>