117aa379252699a08db0270f4e689ce3deb0e490
1 <?php
3 class glpiAccount extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary= "Manage server basic objects";
7 var $cli_description= "Some longer text\nfor help";
8 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* attribute list for save action */
11 var $ignore_account= TRUE;
12 var $attributes= array("ID","name","contact",
13 "tech_num","comments","date_mod","os","location","domain","network",
14 "model","type","is_template","FK_glpi_enterprise","deleted");
16 var $ID ;
17 var $name ="";
18 var $contact ="";
20 var $tech_num ="";
21 var $comments ="";
23 var $date_mod ="";
24 var $os =0;
25 var $location =0;
26 var $domain =0;
27 var $network =0;
29 var $model =0;
30 var $type =0;
31 var $is_template =0;
32 var $FK_glpi_enterprise =0;
33 var $deleted ="N";
35 var $objectclasses= array("whatever");
37 var $initialy_was_account = false;
39 var $edit_type =false;
40 var $edit_os =false;
42 var $data;
43 var $handle = NULL;
45 var $cur_dialog = NULL;
47 var $orig_dn;
48 var $ui;
50 /* Contructor
51 Sets default values and checks if we already have an existing glpi account
52 */
53 function glpiAccount ($config, $dn= NULL)
54 {
55 plugin::plugin ($config, $dn);
56 $this->ui= get_userinfo();
58 if(!isset($this->config->data['SERVERS']['GLPI'])){
59 return;
60 }
62 $this->data = $this->config->data['SERVERS']['GLPI'];
64 if(!is_callable("mysql_connect")){
65 return;
66 }
68 $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
70 if(!$this->handle->is_connected){
71 return;
72 }
74 if($this->handle->is_account($this->dn)){
75 $this->is_account = true;
76 $tmp = ($this->handle->getComputerInformations($this->dn));
78 foreach(array("tech_num","os","FK_glpi_enterprise","type","comments") as $attr){
79 $this->$attr = $tmp[0][$attr];
80 }
82 }else{
83 $this->is_account = false;
84 }
86 $this->name = $this->dn;
87 $this->orig_dn = $this->dn;
88 $this->initialy_was_account = $this->is_account;
89 }
91 function execute()
92 {
93 /* Call parent execute */
94 plugin::execute();
96 /* Fill templating stuff */
97 $smarty= get_smarty();
98 $display= "";
100 // #fixme
101 // Check if mysql a.s.o. is available.
104 $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
105 $users = ($this->handle->getUsers());
107 $ldap= $this->config->get_ldap_link();
109 if(isset($_POST['AddDevice'])){
110 $this->dialog =true;
111 $this->cur_dialog = new glpiDeviceManagement($this->config,$this->dn);
112 }
115 /* System type management
116 */
117 if(isset($_POST['edit_type'])){
118 $this->dialog = true;
119 $this->edit_type=true;
120 }
122 if(isset($_POST['close_edit_type'])){
123 $this->edit_type=false;
124 $this->dialog = false;
125 }
127 if((isset($_POST['add_type']))&&(!empty($_POST['type_string']))){
128 $this->handle->addSystemType($_POST['type_string']);
129 }
131 if((isset($_POST['del_type']))&&(!empty($_POST['select_type']))){
132 $this->handle->removeSystemType_byID($_POST['select_type']);
133 }
135 if((isset($_POST['rename_type']))&&(!empty($_POST['select_type']))&&(!empty($_POST['type_string']))){
136 $this->handle->updateSystemType($_POST['type_string'],$_POST['select_type']);
137 }
139 if($this->edit_type){
140 $smarty->assign("SystemTypes", $this->handle->getSystemTypes());
141 $smarty->assign("SystemTypeKeys", array_flip($this->handle->getSystemTypes()));
143 $display= $smarty->fetch(get_template_path('glpi_edit_type.tpl', TRUE));
144 return($display);
145 }
147 /* ENDE, Systemtype management.
148 */
150 /* System os management
151 */
152 if(isset($_POST['edit_os'])){
153 $this->dialog = true;
154 $this->edit_os=true;
155 }
157 if(isset($_POST['close_edit_os'])){
158 $this->edit_os=false;
159 $this->dialog = false;
160 }
162 if((isset($_POST['add_os']))&&(!empty($_POST['is_string']))){
163 $this->handle->addOS($_POST['is_string']);
164 }
166 if((isset($_POST['del_os']))&&(!empty($_POST['select_os']))){
167 $this->handle->removeOS_byID($_POST['select_os']);
168 }
170 if((isset($_POST['rename_os']))&&(!empty($_POST['select_os']))&&(!empty($_POST['is_string']))){
171 $this->handle->updateOS($_POST['is_string'],$_POST['select_os']);
172 }
174 if($this->edit_os){
175 $smarty->assign("OSKeys", array_flip($this->handle->getOSTypes()));
176 $smarty->assign("OSs", $this->handle->getOSTypes());
178 $display= $smarty->fetch(get_template_path('glpi_edit_os.tpl', TRUE));
179 return($display);
180 }
182 /* ENDE, os management.
183 */
185 if(isset($_POST['edit_manufacturer'])){
186 $this->cur_dialog = new glpiManufacturer($this->config,$this->dn);
187 $this->dialog = true;
188 }
190 if(isset($_POST['close_edit_manufacturer'])){
191 $this->dialog = false;
192 $this->cur_dialog = false;
193 }
195 if(isset($_POST['SelectUserCancel'])){
196 $this->dialog = false;
197 $this->cur_dialog = false;
198 }
200 if(isset($_POST['SelectTechPerson'])){
201 $this->cur_dialog= new glpiSelectUser($this->config,$this->dn);
202 }
204 /* Technical responsible person selected*/
205 if(isset($_GET['act'])&&($_GET['act']=="user_tech_num")){
207 /* Get posted id */
208 $id = base64_decode($_GET['id']);
210 /* Check if user is already created in glpi database */
211 if(!in_array($id,$users)){
213 /* If this user doesn't exists in glpi db, we must create him */
214 $atr = $ldap->fetch($ldap->cat($id));
215 $tmp = array();
216 $use = array( "cn" =>"name",
217 "mail" =>"email",
218 "telephoneNumber" =>"phone");
220 /* Create array */
221 foreach($use as $gosa => $glpi){
222 if(isset($atr[$gosa])){
223 $tmp[$glpi]= $atr[$gosa][0];
224 }
225 }
227 /* Add this user */
228 $this->handle->addUser($tmp,$id);
229 }
231 /* Re-read users */
232 $users = ($this->handle->getUsers());
234 /* Get user */
235 $tmp = array_flip($users);
236 $id=$tmp[$id];
238 /* Use user id, close dialog */
239 $this->tech_num = $id;
240 $this->cur_dialog = false;
241 $this->dialog= false;
242 }
244 if($this->cur_dialog){
245 $this->cur_dialog->save_object();
246 $this->dialog=true;
247 $this->cur_dialog->parent = &$this;
248 return($this->cur_dialog->execute());
249 }
251 /* Assign smarty defaults */
252 foreach(array("SystemTypes","SystemTypeKeys","Manufacturers","OSs","TechnicalResponsibles","InstalledDevices","Attachments") as $attr){
253 $smarty->assign($attr,array());
254 $smarty->assign($attr."ACL"," disabled ");
255 }
257 foreach(array("type","FK_glpi_enterprise","os","tech_num") as $attr){
258 $smarty->assign($attr,"");
259 $smarty->assign($attr."ACL"," disabled ");
260 }
262 /* Perform some checks
263 * arround the database
264 */
265 if(!isset($this->config->data['SERVERS']['GLPI'])){
266 print_red(_("There is no valid configuration for glpi database available."));
267 return($smarty->fetch(get_template_path('glpi.tpl', TRUE)));
268 }
269 $this->data = $this->config->data['SERVERS']['GLPI'];
271 if(!is_callable("mysql_connect")){
272 print_red(_("The php mysql extension is missing. Can't perform any queries."));
273 return($smarty->fetch(get_template_path('glpi.tpl', TRUE)));
274 }
276 $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
278 if(!$this->handle->is_connected){
279 print_red(_("Can't connect to specified database."));
280 return($smarty->fetch(get_template_path('glpi.tpl', TRUE)));
281 }
283 /* If we reach this this
284 * the database connection seams to ok
285 */
287 /* Do we need to flip is_account state? */
288 if (isset($_POST['modify_state'])){
289 $this->is_account= !$this->is_account;
290 }
292 /* Show tab dialog headers */
293 if ($this->is_account){
294 $display= $this->show_header(_("Remove inventory service"),
295 _("This server has inventory features enabled. You can disable them by clicking below."));
296 } else {
297 $display= $this->show_header(_("Add inventory service"),
298 _("This server has inventory features disabled. You can enable them by clicking below."));
299 return ($display);
300 }
303 foreach($this->attributes as $attr){
304 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
305 }
308 $smarty->assign("SystemTypeKeys", array_flip($this->handle->getSystemTypes()));
309 $smarty->assign("SystemTypes", $this->handle->getSystemTypes());
310 $smarty->assign("type", $this->type);
312 $smarty->assign("OSKeys", array_flip($this->handle->getOSTypes()));
313 $smarty->assign("OSs", $this->handle->getOSTypes());
314 $smarty->assign("os", $this->os);
316 $smarty->assign("ManufacturerKeys", array_flip($this->handle->getEnterprisesTypes()));
317 $smarty->assign("Manufacturers", $this->handle->getEnterprisesTypes());
318 $smarty->assign("FK_glpi_enterprise", $this->FK_glpi_enterprise);
320 if(isset($users[$this->tech_num])){
321 $tr = $ldap->fetch($ldap->cat($users[$this->tech_num]));
322 $str = "";
323 if(isset($tr['givenName'][0])){
324 $str .= $tr['givenName'][0]." ";
325 }
327 if(isset($tr['sn'][0])){
328 $str .= $tr['sn'][0]." ";
329 }
331 if(isset($tr['uid'][0])){
332 $str .= "[".$tr['uid'][0]."]";
333 }
336 $smarty->assign("tech_num", $str);
337 }else{
338 $smarty->assign("tech_num", _("N/A"));
339 }
340 $smarty->assign("comments", $this->comments);
342 $display.= $smarty->fetch(get_template_path('glpi.tpl', TRUE));
343 return($display);
344 }
346 function remove_from_parent()
347 {
348 }
351 /* Save data to object */
352 function save_object()
353 {
354 plugin::save_object();
355 foreach($this->attributes as $attrs){
356 if(isset($_POST[$attrs])){
357 $this->$attrs = $_POST[$attrs];
358 }
359 }
360 }
363 /* Check supplied data */
364 function check()
365 {
366 $message= array();
368 // if($this->TechnicalResponsible == ""){
369 // $message[] = _("Please select a technical responsible person for this entry.");
370 // }
372 return ($message);
373 }
375 /* Save to LDAP */
376 function save()
377 {
378 $attrs = array();
379 $this->date_mod = date("Y-m-d H:i:s");
380 foreach($this->attributes as $attr){
381 $attrs[$attr] = $this->$attr;
382 }
383 $attrs['name'] = $this->dn;
384 unset($attrs['ID']);
385 $this->handle = new glpiDB($this->data['SERVER'],$this->data['LOGIN'],$this->data['PASSWORD'],$this->data['DB']);
386 if($this->initialy_was_account&&$this->is_account){
387 $this->handle->updateComputerInformations($attrs,$this->dn);
388 }elseif($this->is_account){
389 $this->handle->addComputerInformations($attrs,$this->dn);
390 }
391 }
393 }
395 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
396 ?>