Code

83a10850b73d5cb9c8774b0ef5e1fe47cf2ac431
[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;
27     $this->load_csv_data();
28   }
31   function get_object_groups()
32   {
33     $ret = array();
34     $ldap = $this->config->get_ldap_link();
35     $ldap->cd($this->config->current['BASE']);
36     $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
37     while($attrs = $ldap->fetch()){
38       $ret [$attrs['cn'][0]] = $attrs['cn'][0];
39     }
40     return($ret); 
41   }
44   function execute()
45   {
46     if(isset($_POST['export_gotomass_csv'])){
47         $data = "";
48         foreach($this->contents as $val){
49           $data .= $val['MAC'].", ".$val['OG']."\n";
50         }
51         header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
52         header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
53         header("Cache-Control: no-cache");
54         header("Pragma: no-cache");
55         header("Cache-Control: post-check=0, pre-check=0");
56         header("Content-type: text/plain");
57         if (preg_match('/MSIE 5.5/', $_SERVER['HTTP_USER_AGENT']) ||
58             preg_match('/MSIE 6.0/', $_SERVER['HTTP_USER_AGENT'])){
59           header('Content-Disposition: filename="gotomass.csv"');
60         } else {
61           header('Content-Disposition: attachment; filename="gotomass.csv";');
62         }
63         echo $data;
64         exit();
65     }
66    
67     /* Import given file */ 
68     if(isset($_POST['import_gotomass_csv']) && isset($_FILES['mass_file'])){
69       $str = @file_get_contents($_FILES['mass_file']['tmp_name']);
70       if(empty($str)){
71         print_red(_("The uploaded file seams to be empty, import aborted."));
72       }else{
73         $this->load_csv_data($str);
74       }
75     }
77     /* Add a new empty entry to the list */ 
78     if(isset($_POST['add_new_entry'])){
79       $this->contents[] = array("MAC" => "", "OG" => "");
80     }
82     /* Call parent execute */
83     plugin::execute();
84     $smarty= get_smarty();
85     $smarty->assign("ogs", $this->get_object_groups());
86     $smarty->assign("contents", $this->contents);
87     $smarty->assign("launchimage","images/launch.png");
88     return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
89   }
92   function load_csv_data($data = NULL)
93   {
94     if($data == NULL){
95       if(!file_exists($this->file_to_read) || !is_readable($this->file_to_read)){
96         print_red(sprintf(_("Can't locate or read csv storage file '%s'."),$this->file_to_read));
97         return(FALSE);
98       }
100       $fp = @fopen($this->file_to_read,"r");
101       if(!$fp){
102         print_red(sprintf(_("Can't read csv storage file '%s'."),$this->file_to_read));
103         return(FALSE);
104       }
106       $this->contents =array(); 
108       while(!feof($fp)){
109         $str = trim(fgets($fp,512));
111         /* Get mac address */
112         $og = trim(preg_replace("/^[^,;]*(,|;)/","",$str));
113         $mac = preg_replace("/(,|;).*$/","",$str);
115         if(!empty($og) || !empty($mac)){
116           $this->contents[] = array("MAC" => $mac , "OG" => $og);
117         }
118       }
119       fclose($fp);
120     }else{
121       $this->contents =array(); 
122       $rows = split("\n",$data);
123       foreach($rows as $str){
124         
125         /* Get mac address */
126         $og = trim(preg_replace("/^[^,;]*(,|;)/","",$str));
127         $mac = preg_replace("/(,|;).*$/","",$str);
129         if(!empty($og) || !empty($mac)){
130           $this->contents[] = array("MAC" => $mac , "OG" => $og);
131         }
132       }
133     }
134   }
137   function save_csv_data()
138   {
139     if(!file_exists($this->file_to_read) || !is_writeable($this->file_to_read)){
140       print_red(sprintf(_("Can't locate or write csv storage file '%s'."),$this->file_to_read));
141     }else{
142       $fp = @fopen($this->file_to_read,"w");
143       if(!$fp){
144         print_red(sprintf(_("Can't write csv storage file '%s'."),$this->file_to_read));
145       }else{  
146         $data = "";
147         foreach($this->contents as $val){
148           $data .= $val['MAC'].", ".$val['OG']."\n";
149         }
150         fwrite($fp,$data,strlen($data));
151         fclose($fp);
152       }
153     }
154   }
157   function save_object()
158   {
159     if(isset($_POST['gotomasses'])){
161       /* Check for input changes */
162       $ogs = $this->get_object_groups();
163       foreach($this->contents as $id => $data){
164         if(isset($_POST['mac_'.$id])){
165           $this->contents[$id]['MAC'] = $_POST['mac_'.$id];
166         }
167         if(isset($_POST['og_'.$id]) && in_array_ics($_POST['og_'.$id],$ogs)){
168           $this->contents[$id]['OG'] = $_POST['og_'.$id];
169         }
170       }
172       /* check for remove requests */
173       $once = TRUE;
174       foreach($_POST as $name => $value){
175         if(preg_match("/^remove_[0-9]*_(x|y)$/",$name) && $once){
176           $once = FALSE;
177           $id = preg_replace("/^remove_/","",$name);
178           $id = preg_replace("/_(x|y)$/","",$id);
180           if(isset($this->contents[$id])){
181             unset($this->contents[$id]);
182           }
183         }
184       }
186       /* Write back all changes */
187       if(isset($_POST['save_gotomass_changes'])){
188         $this->save_csv_data();
189       }
191       /* Reload data from csv file ? */
192       if(isset($_POST['reload_gotomass_data'])){
193         $this->load_csv_data();
194       }
195     }
196   }
199   function plInfo()
200   {
201     return (array(
202         "plShortName"   => _("Mass machine deployment"),
203         "plDescription" => _("Mass machine deployment addon"),
204         "plSelfModify"  => FALSE,
205         "plDepends"     => array(),
206         "plPriority"    => 0,
207         "plSection"     => array("addon"),
208         "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("Mass machine deployment"))),
209         "plProvidedAcls" => array()
210         ));
211   }
213 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
214 ?>