Code

Readded iso creation
[gosa.git] / gosa-plugins / systems / admin / systems / class_systemManagement.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class systemManagement extends management
24 {
25   var $plHeadline     = "Systems";
26   var $plDescription  = "List of systems";
27   var $plIcon  = "plugins/systems/images/plugin.png";
29   // Tab definition 
30   protected $tabClass = "";
31   protected $tabType = "";
32   protected $aclCategory = "";
33   protected $aclPlugin   = "";
34   protected $objectName   = "";
36   protected $objectInfo = array();
38   protected $opsi = NULL;
40   function __construct($config,$ui)
41   {
42     $this->config = $config;
43     $this->ui = $ui;
45     // Set storage points 
46     $tD = $this->getObjectDefinitions(); 
47     $sP = array();
48     foreach($tD as $entry){
49       if(!empty($entry['ou']))
50         $sP[] = $entry['ou'];
51     }
52     $this->storagePoints = array_unique($sP);
54     // Build filter
55 #    if (session::global_is_set(get_class($this)."_filter")){
56 #      $filter= session::global_get(get_class($this)."_filter");
57 #    } else {
58       $filter = new filter(get_template_path("system-filter.xml", true));
59       $filter->setObjectStorage($this->storagePoints);
60 #    }
61     $this->setFilter($filter);
63     // Build headpage
64     $headpage = new listing(get_template_path("system-list.xml", true));
65     $headpage->setFilter($filter);
66     $filter->setConverter('INCOMING', 'systemManagement::incomingFilterConverter');
68     // Register Daemon Events
69     if(class_available("DaemonEvent") && class_available("gosaSupportDaemon")){
70       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
71       foreach($events['TRIGGERED'] as $name => $data){
72         $this->registerAction("T_".$name,"handleEvent");
73         $this->registerAction("S_".$name,"handleEvent");
74       }
75       $this->registerAction("DaemonEvent_activate","handleEvent");
76     }
77     $this->registerAction("saveEvent","saveEventDialog");
78     $this->registerAction("createISO","createISO");
79     $this->registerAction("initiateISOcreation","initiateISOcreation");
80     $this->registerAction("performIsoCreation","performIsoCreation");
82     // Add copy&paste and snapshot handler.
83     if ($this->config->boolValueIsTrue("main", "copyPaste")){
84       $this->cpHandler = new CopyPasteHandler($this->config);
85     }
86     if($this->config->get_cfg_value("enableSnapshots") == "true"){
87       $this->snapHandler = new SnapshotHandler($this->config);
88     }
90     // Check if we are able to communicate with the GOsa supprot daemon
91     if(class_available("gosaSupportDaemon")){
92       $o = new gosaSupportDaemon();
93       $this->si_active = $o->connect() && class_available("DaemonEvent");
94     }
96     // Check if we are able to communicate with the GOsa supprot daemon
97     if(class_available("opsi")){
98       $this->opsi = new opsi($this->config);
99     }
102     parent::__construct($config, $ui, "systems", $headpage);
103   }
106   function createISO($action,$target)
107   {
108     if(count($target) == 1){
109       $smarty = get_smarty();
110       $this->dn= array_pop($target);
111       set_object_info($this->dn);
112       return ($smarty->fetch(get_template_path('goto/gencd.tpl', TRUE)));
114     }
115   }
116   
117   function initiateISOcreation()
118   {
119     $smarty = get_smarty();
120     $smarty->assign("src", "?plug=".$_GET['plug']."&amp;PerformIsoCreation");
121     return ($smarty->fetch(get_template_path('goto/gencd_frame.tpl', TRUE)));  
122   }
125   function performIsoCreation()
126   {
127     $return_button   = "<form method='get' action='main.php' target='_parent'>
128       <input type='submit' value='"._("Back")."'>
129       <input type='hidden' name='plug' value='".$_GET['plug']."'/>
130       </form>";
132     $dsc   = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
134     /* Get and check command */
135     $command= $this->config->search("workgeneric", "SYSTEMISOHOOK",array('tabs'));
136     if (check_command($command)){
137       @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $command, "Execute");
139       /* Print out html introduction */
140       echo '  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
141         <html>
142         <head>
143         <title></title>
144         <style type="text/css">@import url("themes/default/style.css");</style>
145         <script language="javascript" src="include/focus.js" type="text/javascript"></script>
146         </head>
147         <body style="background: none; margin:4px;" id="body" >
148         <pre>';
150       /* Open process handle and check if it is a valid process */
151       $process= proc_open($command." '".$this->dn."'", $dsc, $pipes);
152       if (is_resource($process)) {
153         fclose($pipes[0]);
155         /* Print out returned lines && write JS to scroll down each line */
156         while (!feof($pipes[1])){
157           $cur_dat = fgets($pipes[1], 1024);
158           echo $cur_dat;
159           echo '<script language="javascript" type="text/javascript">scrollDown2();</script>' ;
160           flush();
161         }
162       }
164       /* Get error string && close streams */
165       $buffer= stream_get_contents($pipes[2]);
167       fclose($pipes[1]);
168       fclose($pipes[2]);
169       echo "</pre>";
171       /* Check return code */
172       $ret= proc_close($process);
173       if ($ret != 0){
174         echo "<h1 style='color:red'>"._("Creating the image failed. Please see the report below.")."</h1>";
175         echo "<pre style='color:red'>$buffer</pre>";
176       }
177       echo $return_button."<br>";
178     } else {
179       $tmp= "<h1 style='color:red'>".sprintf(_("Command '%s', specified for ISO creation doesn't seem to exist."), $command)."</h1>";
180       echo $tmp;
181       echo $return_button."<br>";
182     }
184     /* Scroll down completly */
185     echo '<script language="javascript" type="text/javascript">scrollDown2();</script>' ;
186     echo '</body></html>';
187     flush();
188     exit;
189   }
192   /*! \brief    Handle GOsa-si events
193    *            All events are handled here.
194    */
195   function handleEvent($action="",$target=array(),$all=array())
196   {
197     // Detect whether this event is scheduled or triggered.
198     $triggered = TRUE;
199     if(preg_match("/^S_/",$action)){
200       $triggered = FALSE;
201     }
203     // Detect triggere or scheduled actions 
204     $headpage = $this->getHeadpage();
205     if(preg_match("/^[TS]_/", $action)){
207       // Send special reinstall action for opsi hosts
208       $event = preg_replace("/^[TS]_/","",$action); 
209       if($event == "DaemonEvent_reinstall" && $this->si_active && $this->opsi){
210         foreach($target as $key => $dn){
211           $type = $headpage->getType($dn);
213           // Send Reinstall event for opsi hosts
214           if($type == "FAKE_OC_OpsiHost"){
215             $obj = $headpage->getEntry($dn);
216             $this->opsi->job_opsi_install_client($obj['cn'][0],$obj['macAddress'][0]);
217             unset($target[$key]);
218           }
219         }
220       }
221     } 
223     // Now send remaining FAI/GOsa-si events here.
224     if(count($target) && $this->si_active){
225       $mac= array();
227       // Collect target mac addresses
228       $ldap = $this->config->get_ldap_link();
229       $tD = $this->getObjectDefinitions();
230       $events = DaemonEvent::get_event_types(SYSTEM_EVENT);
231       $o_queue = new gosaSupportDaemon();
232       foreach($target as $dn){
233         $type = $headpage->getType($dn);
234         if($tD[$type]['sendEvents']){
235           $obj = $headpage->getEntry($dn);
236           if(isset($obj['macAddress'][0])){
237             $mac[] = $obj['macAddress'][0];
238           }
239         }
240       }
242       /* Skip installation or update trigerred events,
243        *  if this entry is currently processing.
244        */
245       if($triggered && in_array($event,array("DaemonEvent_reinstall","DaemonEvent_update"))){
246         foreach($mac as $key => $mac_address){
247           foreach($o_queue->get_entries_by_mac(array($mac_address)) as $entry){
248             $entry['STATUS'] = strtoupper($entry['STATUS']);
249             if($entry['STATUS'] == "PROCESSING" &&
250                 isset($events['QUEUED'][$entry['HEADERTAG']]) &&
251                 in_array($events['QUEUED'][$entry['HEADERTAG']],array("DaemonEvent_reinstall","DaemonEvent_update"))){
252               unset($mac[$key]);
254               new log("security","systems/".get_class($this),"",array(),"Skip adding 'DaemonEvent::".$type."' for mac '".$mac_address."', there is already a job in progress.");
255               break;
256             }
257           }
258         }
259       }
261       // Prepare event to be added
262       if(count($mac) && isset($events['BY_CLASS'][$event]) && $this->si_active){
263         $event = $events['BY_CLASS'][$event];
264         $this->dialogObject = new $event['CLASS_NAME']($this->config);
265         $this->dialogObject->add_targets($mac);
267         if($triggered){
268           $this->dialogObject->set_type(TRIGGERED_EVENT);
269           $o_queue->append($this->dialogObject);
270           if($o_queue->is_error()){
271             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
272           }else{
273             $this->closeDialogs();
274           }
275         }else{
276           $this->dialogObject->set_type(SCHEDULED_EVENT);
277         }
278       }
279     }
281     // Handle system activation 
282     if($action == "DaemonEvent_activate"){
283       echo "Aktivieren!";
284     }
285   }
286  
287   
288   function saveEventDialog()
289   {
290     $o_queue = new gosaSupportDaemon();
291     $o_queue->append($this->dialogObject);
292     if($o_queue->is_error()){
293       msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
294     }else{
295       $this->closeDialogs();
296     }
297  
298   }
299  
301   /*! \brief    Update filter part for INCOMING.
302    *            Allows us to search for "systemIncomingRDN".
303    */
304   static function incomingFilterConverter($filter)
305   {
306     $rdn = preg_replace("/^[^=]*=/", "", get_ou('systemIncomingRDN'));
307     $rdn = preg_replace("/,.*$/","",$rdn);
308     return(preg_replace("/%systemIncomingRDN/", $rdn,$filter));
309   }
311  
312   /*! \brief    Queue selected objects to be removed. 
313    *            Checks ACLs, Locks and ask for confirmation.
314    */
315   protected function removeEntryRequested($action="",$target=array(),$all=array())
316   {
317     $disallowed = array();
318     $this->dns = array();
320     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel requested!");
322     // Check permissons for each target
323     $tInfo = $this->getObjectDefinitions();
324     $headpage = $this->getHeadpage();
325     foreach($target as $dn){
326       $type = $headpage->getType($dn);
327       if(!isset($tInfo[$type])){
328         trigger_error("Unknown object type received '".$type."' please update systemManagement::getObjectDefinitions()!");
329       }else{
330         $info = $tInfo[$type];
331         $acl = $this->ui->get_permissions($dn, $info['aclCategory']."/".$info['aclClass']);
332         if(preg_match("/d/",$acl)){
333           $this->dns[] = $dn;
334         }else{
335           $disallowed[] = $dn;
336         }
337       }
338     }
339     if(count($disallowed)){
340       msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
341     }
343     // We've at least one entry to delete.
344     if(count($this->dns)){
346       // check locks
347       if ($user= get_multiple_locks($this->dns)){
348         return(gen_locked_message($user,$this->dns));
349       }
351       // Add locks
352       $dns_names = array();
353       foreach($this->dns as $dn){
354         $dns_names[] =LDAP::fix($dn);
355       }
356       add_lock ($this->dns, $this->ui->dn);
358       // Display confirmation dialog.
359       $smarty = get_smarty();
360       $smarty->assign("info", msgPool::deleteInfo($dns_names,_($this->objectName)));
361       $smarty->assign("multiple", true);
362       return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
363     }
364   }
367   /*! \brief  Object removal was confirmed, now remove the requested entries.
368    *
369    *  @param  String  'action'  The name of the action which was the used as trigger.
370    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
371    *  @param  Array   'all'     A combination of both 'action' and 'target'.
372    */
373   function removeEntryConfirmed($action="",$target=array(),$all=array(),
374       $altTabClass="",$altTabType="",$altAclCategory="")
375   {
376     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel confirmed!");
378     // Check permissons for each target
379     $tInfo = $this->getObjectDefinitions();
380     $headpage = $this->getHeadpage();
381     $disallowed = array();
382     foreach($this->dns as $key => $dn){
383       $type = $headpage->getType($dn);
384       if(!isset($tInfo[$type])){
385         trigger_error("Unknown object type received '".$type."' please update systemManagement::getObjectDefinitions()!");
386       }else{
388         $info = $tInfo[$type];
389         $acl = $this->ui->get_permissions($dn, $info['aclCategory']."/".$info['aclClass']);
390         if(preg_match("/d/",$acl)){
392           // Delete the object
393           $this->dn = $dn;
394           $this->tabObject= new $info['tabClass']($this->config,$this->config->data['TABS'][$info['tabDesc']], 
395               $this->dn, $info['aclCategory'], true, true);
396           $this->tabObject->set_acl_base($this->dn);
397           $this->tabObject->parent = &$this;
398           $this->tabObject->delete ();
400           // Remove the lock for the current object.
401           del_lock($this->dn);
403         }else{
404           $disallowed[] = $dn;
405           new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
406         }
407       }
408     }
409     if(count($disallowed)){
410       msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
411     }
413     // Cleanup
414     $this->remove_lock();
415     $this->closeDialogs();
416   }
419   /*! \brief  Edit the selected system type.
420    *
421    *  @param  String  'action'  The name of the action which was the used as trigger.
422    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
423    *  @param  Array   'all'     A combination of both 'action' and 'target'.
424    */
425   function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
426   {
427     if(count($target) == 1){
428       $tInfo = $this->getObjectDefinitions();
429       $headpage = $this->getHeadpage();
430       $dn = $target[0];
431       $type = $tInfo[$headpage->getType($dn)];
432       return(management::editEntry($action,$target,$all,$type['tabClass'],$type['tabDesc'],$type['aclCategory']));
433     }
434   }
437  /*! \brief  Detects actions/events send by the ui
438    *           and the corresponding targets.
439    */
440   function detectPostActions()
441   {
442     $action= management::detectPostActions();
443     if(isset($_POST['abort_event_dialog']))  $action['action'] = "cancel";
444     if(isset($_POST['save_event_dialog']))  $action['action'] = "saveEvent";
445     if(isset($_POST['cd_create']))  $action['action'] = "initiateISOcreation";
446     if(isset($_GET['PerformIsoCreation']))  $action['action'] = "performIsoCreation";
447     return($action);
448   }
451   /*! \brief   Overridden render method of class mangement.
452    *            this allows us to add a release selection box.
453    */
454   function renderList()
455   {
456     $headpage = $this->getHeadpage();
457     $headpage->update();
459     $tD = $this->getObjectDefinitions();
460     $smarty = get_smarty();
461     foreach($tD as $name => $obj){
462       $smarty->assign("USE_".$name, (empty($obj['TABNAME']) || class_available($obj['TABNAME'])));
463     }
465     $display = $headpage->render();
466     return($this->getHeader().$display);
467   }
470   public function getObjectDefinitions()
471   {
472     $tabs = array(
473         "FAKE_OC_OpsiHost" => array(
474           "ou"          => "",
475           "plugClass"   => "opsiGeneric",
476           "tabClass"    => "opsi_tabs",
477           "tabDesc"     => "OPSITABS",
478           "aclClass"    => "opsiGeneric",
479           "sendEvents"  => TRUE,
480           "aclCategory" => "opsi"),
482         "goServer" => array(
483           "ou"          => get_ou('serverRDN'),
484           "plugClass"   => "servgeneric",
485           "tabClass"    => "servtabs",
486           "tabDesc"     => "SERVTABS",
487           "aclClass"    => "servgeneric",
488           "sendEvents"  => TRUE,
489           "aclCategory" => "server"),
491         "gotoWorkstation" => array(
492           "ou"          => get_ou('workstationRDN'),
493           "plugClass"   => "workgeneric",
494           "tabClass"    => "worktabs",
495           "tabDesc"     => "WORKTABS",
496           "aclClass"    => "workstation",
497           "sendEvents"  => TRUE,
498           "aclCategory" => "workgeneric"),
500         "gotoTerminal" => array(
501             "ou"          => get_ou('terminalRDN'),
502             "plugClass"   => "termgeneric",
503             "tabClass"    => "termtabs",
504             "sendEvents"  => TRUE,
505             "tabDesc"     => "TERMTABS",
506             "aclClass"    => "terminal",
507             "aclCategory" => "termgeneric"),
509         "gotoPrinter" => array(
510             "ou"          => get_ou('printerRDN'),
511             "plugClass"   => "printgeneric",
512             "tabClass"    => "printtabs",
513             "tabDesc"     => "PRINTTABS",
514             "aclClass"    => "printer",
515             "sendEvents"  => FALSE,
516             "aclCategory" => "printgeneric"),
518         "FAKE_OC_NewDevice" => array(
519             "ou"          => get_ou('systemIncomingRDN'),
520             "plugClass"   => "termgeneric",
521             "tabClass"    => "termtabs",
522             "sendEvents"  => TRUE,
523             "tabDesc"     => "TERMTABS",
524             "aclClass"    => "terminal",
525             "aclCategory" => "termgeneric"),
527         "goFonHardware" => array(
528             "ou"          => get_ou('phoneRDN'),
529             "plugClass"   => "phoneGeneric",
530             "tabClass"    => "phonetabs",
531             "tabDesc"     => "PHONETABS",
532             "sendEvents"  => FALSE,
533             "aclClass"    => "phone",
534             "aclCategory" => "phoneGeneric"),
536         "FAKE_OC_winstation" => array(
537             "ou"          => get_winstations_ou(),
538             "plugClass"   => "wingeneric",
539             "sendEvents"  => TRUE,
540             "tabClass"    => "wintabs",
541             "tabDesc"     => "WINTABS",
542             "aclClass"    => "wingeneric",
543             "aclCategory" => "winworkstation"),
545         "ieee802Device" => array(
546             "ou"          => get_ou('componentRDN'),
547             "plugClass"   => "componentGeneric",
548             "sendEvents"  => FALSE,
549             "tabClass"    => "componenttabs",
550             "tabDesc"     => "COMPONENTTABS",
551             "aclClass"    => "componentGeneric",
552             "aclCategory" => "component"),
553         );
555     // Now map some special types
556     $tabs['FAKE_OC_NewWorkstation'] = &$tabs['gotoWorkstation'];
557     $tabs['FAKE_OC_NewTerminal'] = &$tabs['gotoTerminal'];
558     $tabs['FAKE_OC_NewServer'] = &$tabs['gotoWorkstation'];
559     $tabs['gotoWorkstation__IS_BUSY'] = &$tabs['gotoWorkstation'];
560     $tabs['gotoWorkstation__IS_ERROR'] = &$tabs['gotoWorkstation'];
561     $tabs['gotoWorkstation__IS_LOCKED'] = &$tabs['gotoWorkstation'];
562     $tabs['gotoTerminal__IS_BUSY'] = &$tabs['gotoTerminal'];
563     $tabs['gotoTerminal__IS_ERROR'] = &$tabs['gotoTerminal'];
564     $tabs['gotoTerminal__IS_LOCKED'] = &$tabs['gotoTerminal'];
565     $tabs['goServer__IS_BUSY'] = &$tabs['goServer'];
566     $tabs['goServer__IS_ERROR'] = &$tabs['goServer'];
567     $tabs['goServer__IS_LOCKED'] = &$tabs['goServer'];
569     $tabs['FAKE_OC_NewUnknownDevice'] = &$tabs['FAKE_OC_NewDevice'];
571     return($tabs);
572   }
573
574 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
575 ?>