Code

cc5527e6c72b9a88e59241d0ff0c651253ac3599
[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(_("The uploaded file seams to be empty, import aborted."));
73       }else{
74         $this->load_csv_data($str);
75       }
76     }
78     /* Add a new empty entry to the list */ 
79     if(isset($_POST['add_new_entry'])){
80       $this->contents[] = array("MAC" => "", "OG" => "");
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   {
95     if($data == NULL){
96       if(!file_exists($this->file_to_read) || !is_readable($this->file_to_read)){
97         print_red(sprintf(_("Can't locate or read goto masses csv storage file '%s'."),$this->file_to_read));
98         return(FALSE);
99       }
101       $fp = @fopen($this->file_to_read,"r");
102       if(!$fp){
103         print_red(sprintf(_("Can't read goto masses csv storage file '%s'."),$this->file_to_read));
104         return(FALSE);
105       }
107       $this->contents =array(); 
109       while(!feof($fp)){
110         $str = trim(fgets($fp,512));
112         /* Get mac address */
113         $og = trim(preg_replace("/^[^,;]*(,|;)/","",$str));
114         $mac = preg_replace("/(,|;).*$/","",$str);
116         if(!empty($og) || !empty($mac)){
117           $this->contents[] = array("MAC" => $mac , "OG" => $og);
118         }
119       }
120       fclose($fp);
121     }else{
122       $this->contents =array(); 
123       $rows = split("\n",$data);
124       foreach($rows as $str){
125         
126         /* Get mac address */
127         $og = trim(preg_replace("/^[^,;]*(,|;)/","",$str));
128         $mac = preg_replace("/(,|;).*$/","",$str);
130         if(!empty($og) || !empty($mac)){
131           $this->contents[] = array("MAC" => $mac , "OG" => $og);
132         }
133       }
134     }
135   }
138   function save_csv_data()
139   {
140     if(!file_exists($this->file_to_read) || !is_writeable($this->file_to_read)){
141       print_red(sprintf(_("Can't locate or write goto masses csv storage file '%s'."),$this->file_to_read));
142     }else{
143       
144       $fp = @fopen($this->file_to_read,"w");
145         
146       if(!$fp){
147         print_red(sprintf(_("Can't write goto masses csv storage file '%s'."),$this->file_to_read));
148       }else{  
149         $data = "";
150         foreach($this->contents as $val){
151           $data .= $val['MAC'].", ".$val['OG']."\n";
152         }
153         fwrite($fp,$data,strlen($data));
154         fclose($fp);
155       }
156     }
157   }
160   function save_object()
161   {
162     if(isset($_POST['gotomasses'])){
164       /* Check for input changes */
165       $ogs = $this->get_object_groups();
166       foreach($this->contents as $id => $data){
167         if(isset($_POST['mac_'.$id])){
168           $this->contents[$id]['MAC'] = $_POST['mac_'.$id];
169         }
170         if(isset($_POST['og_'.$id]) && in_array_ics($_POST['og_'.$id],$ogs)){
171           $this->contents[$id]['OG'] = $_POST['og_'.$id];
172         }
173       }
175       /* check for remove requests */
176       $once = TRUE;
177       foreach($_POST as $name => $value){
178         if(preg_match("/^remove_[0-9]*_(x|y)$/",$name) && $once){
179           $once = FALSE;
180           $id = preg_replace("/^remove_/","",$name);
181           $id = preg_replace("/_(x|y)$/","",$id);
183           if(isset($this->contents[$id])){
184             unset($this->contents[$id]);
185           }
186         }
187       }
189       /* Write back all changes */
190       if(isset($_POST['save_gotomass_changes'])){
191         $this->save_csv_data();
192       }
194       /* Reload data from csv file ? */
195       if(isset($_POST['reload_gotomass_data'])){
196         $this->load_csv_data();
197       }
198     }
199   }
202   function plInfo()
203   {
204     return (array(
205         "plShortName"   => _("Mass machine deployment"),
206         "plDescription" => _("Mass machine deployment addon"),
207         "plSelfModify"  => FALSE,
208         "plDepends"     => array(),
209         "plPriority"    => 0,
210         "plSection"     => array("addon"),
211         "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("Mass machine deployment"))),
212         "plProvidedAcls" => array()
213         ));
214   }
216 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
217 ?>