Code

108022afb0baafcc15c38200a29151d38423db3f
[gosa.git] / plugins / addons / gotomasses / class_gotomasses.inc
1 <?php
3 class gotomasses extends plugin
4 {
5   /* Definitions */
6   var $plHeadline     = "Mass machine";
7   var $plDescription  = "This does something";
9   /* attribute list for save action */
10   var $attributes= array();
11   var $objectclasses= array();
13   /* Source file that contains the csv data */
14   var $file_to_read = "Undefined"; #Set in constructor 
16   /* Parsed csv content */
17   var $contents = array();
20   function gotomasses($config, $dn= NULL)
21   {
22     /* Define source file */
23     $this->file_to_read = CONFIG_DIR."/gotomasses_machines";
24   
25     /* Include config object */
26     $this->config= $config;
28     $this->load_csv_data();
29   }
32   function get_object_groups()
33   {
34     $ret = array();
35     $ldap = $this->config->get_ldap_link();
36     $ldap->cd($this->config->current['BASE']);
37     $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
38     while($attrs = $ldap->fetch()){
39       $ret [$attrs['cn'][0]] = $attrs['cn'][0];
40     }
41     return($ret); 
42   }
45   function execute()
46   {
47     if(isset($_POST['export_gotomass_csv'])){
48         $data = "";
49         foreach($this->contents as $val){
50           $data .= $val['MAC'].", ".$val['OG']."\n";
51         }
52         header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
53         header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
54         header("Cache-Control: no-cache");
55         header("Pragma: no-cache");
56         header("Cache-Control: post-check=0, pre-check=0");
57         header("Content-type: text/plain");
58         if (preg_match('/MSIE 5.5/', $_SERVER['HTTP_USER_AGENT']) ||
59             preg_match('/MSIE 6.0/', $_SERVER['HTTP_USER_AGENT'])){
60           header('Content-Disposition: filename="gotomass.csv"');
61         } else {
62           header('Content-Disposition: attachment; filename="gotomass.csv";');
63         }
64         echo $data;
65         exit();
66     }
67    
68     /* Import given file */ 
69     if(isset($_POST['import_gotomass_csv']) && isset($_FILES['mass_file'])){
70       $str = @file_get_contents($_FILES['mass_file']['tmp_name']);
71       if(empty($str)){
72         print_red(_("Uploaded file seams to be empty, import aborted."));
73       }else{
74         $this->load_csv_data($str);
75       }
76     }
77  
78     if(isset($_POST['add_new_entry'])){
79       $this->contents[] = array("MAC" => "", "OG" => "");
80     }
81  
83     /* Call parent execute */
84     plugin::execute();
85     $smarty= get_smarty();
86     $smarty->assign("ogs", $this->get_object_groups());
87     $smarty->assign("contents", $this->contents);
88     $smarty->assign("launchimage","images/launch.png");
89     return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
90   }
93   function load_csv_data($data = NULL)
94   {
96     if($data == NULL){
97       if(!file_exists($this->file_to_read) || !is_readable($this->file_to_read)){
98         print_red(sprintf(_("Can't locate or read goto masses csv storage file '%s'."),$this->file_to_read));
99         return(FALSE);
100       }
102       $fp = @fopen($this->file_to_read,"r");
103       if(!$fp){
104         print_red(sprintf(_("Can't read goto masses csv storage file '%s'."),$this->file_to_read));
105         return(FALSE);
106       }
108       $this->contents =array(); 
110       while(!feof($fp)){
111         $str = trim(fgets($fp,512));
113         /* Get mac address */
114         $og = trim(preg_replace("/^[^,;]*(,|;)/","",$str));
115         $mac = preg_replace("/(,|;).*$/","",$str);
117         if(!empty($og) || !empty($mac)){
118           $this->contents[] = array("MAC" => $mac , "OG" => $og);
119         }
120       }
121       fclose($fp);
122     }else{
123       $this->contents =array(); 
124       $rows = split("\n",$data);
125       foreach($rows as $str){
126         
127         /* Get mac address */
128         $og = trim(preg_replace("/^[^,;]*(,|;)/","",$str));
129         $mac = preg_replace("/(,|;).*$/","",$str);
131         if(!empty($og) || !empty($mac)){
132           $this->contents[] = array("MAC" => $mac , "OG" => $og);
133         }
134       }
135     }
136   }
139   function save_csv_data()
140   {
141     if(!file_exists($this->file_to_read) || !is_writeable($this->file_to_read)){
142       print_red(sprintf(_("Can't locate or write goto masses csv storage file '%s'."),$this->file_to_read));
143     }else{
144       
145       $fp = @fopen($this->file_to_read,"w");
146         
147       if(!$fp){
148         print_red(sprintf(_("Can't write goto masses csv storage file '%s'."),$this->file_to_read));
149       }else{  
150         $data = "";
151         foreach($this->contents as $val){
152           $data .= $val['MAC'].", ".$val['OG']."\n";
153         }
154         fwrite($fp,$data,strlen($data));
155         fclose($fp);
156       }
157     }
158   }
161   function save_object()
162   {
163     if(isset($_POST['gotomasses'])){
165       /* Check for input changes */
166       $ogs = $this->get_object_groups();
167       foreach($this->contents as $id => $data){
168         if(isset($_POST['mac_'.$id])){
169           $this->contents[$id]['MAC'] = $_POST['mac_'.$id];
170         }
171         if(isset($_POST['og_'.$id]) && in_array_ics($_POST['og_'.$id],$ogs)){
172           $this->contents[$id]['OG'] = $_POST['og_'.$id];
173         }
174       }
176       /* check for remove requests */
177       $once = TRUE;
178       foreach($_POST as $name => $value){
179         if(preg_match("/^remove_[0-9]*_(x|y)$/",$name) && $once){
180           $once = FALSE;
181           $id = preg_replace("/^remove_/","",$name);
182           $id = preg_replace("/_(x|y)$/","",$id);
184           if(isset($this->contents[$id])){
185             unset($this->contents[$id]);
186           }
187         }
188       }
190       /* Write back all changes */
191       if(isset($_POST['save_gotomass_changes'])){
192         $this->save_csv_data();
193       }
195       /* Reload data from csv file ? */
196       if(isset($_POST['reload_gotomass_data'])){
197         $this->load_csv_data();
198       }
199     }
200   }
203   function plInfo()
204   {
205     return (array(
206         "plShortName"   => _("Mass machine deployment"),
207         "plDescription" => _("Mass machine deployment addon"),
208         "plSelfModify"  => FALSE,
209         "plDepends"     => array(),
210         "plPriority"    => 0,
211         "plSection"     => array("addon"),
212         "plCategory"    => array("mass_machines" => array("objectClass" => "none", "description" => _("Mass machine deployment"))),
213         "plProvidedAcls" => array()
214         ));
215   }
217 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
218 ?>