a8230776f8aad1208e5f1a077a762390f0128261
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();
19 /* Used to detect changes made on the csv content.
20 * Buttons will be disabled and js warnings will be
21 * shown if the content wasn't saved or discarded
22 */
23 var $contents_backup = array();
26 function gotomasses($config, $dn= NULL)
27 {
28 /* Define source file */
29 $this->file_to_read = CONFIG_DIR."/gotomasses_machines";
31 /* Include config object */
32 $this->config= $config;
33 $this->load_csv_data();
34 }
37 function get_object_groups()
38 {
39 $ret = array();
40 $ldap = $this->config->get_ldap_link();
41 $ldap->cd($this->config->current['BASE']);
42 $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
43 while($attrs = $ldap->fetch()){
44 $ret [$attrs['cn'][0]] = $attrs['cn'][0];
45 }
46 return($ret);
47 }
50 function execute()
51 {
52 if(isset($_POST['export_gotomass_csv'])){
53 $data = "";
54 foreach($this->contents as $val){
55 $data .= $val['MAC'].", ".$val['OG']."\n";
56 }
57 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
58 header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
59 header("Cache-Control: no-cache");
60 header("Pragma: no-cache");
61 header("Cache-Control: post-check=0, pre-check=0");
62 header("Content-type: text/plain");
63 if (preg_match('/MSIE 5.5/', $_SERVER['HTTP_USER_AGENT']) ||
64 preg_match('/MSIE 6.0/', $_SERVER['HTTP_USER_AGENT'])){
65 header('Content-Disposition: filename="gotomass.csv"');
66 } else {
67 header('Content-Disposition: attachment; filename="gotomass.csv";');
68 }
69 echo $data;
70 exit();
71 }
73 /* Import given file */
74 if(isset($_POST['import_gotomass_csv']) && isset($_FILES['mass_file'])){
75 $str = @file_get_contents($_FILES['mass_file']['tmp_name']);
76 if(empty($str)){
77 print_red(_("The uploaded file seams to be empty, import aborted."));
78 }else{
79 $this->load_csv_data($str);
80 }
81 }
83 /* Add a new empty entry to the list */
84 if(isset($_POST['add_new_entry'])){
85 $this->contents[] = array("MAC" => "", "OG" => "");
86 }
88 /* Call parent execute */
89 plugin::execute();
90 $smarty= get_smarty();
91 $smarty->assign("contents_modified",$this->contents_modified());
92 $smarty->assign("ogs", $this->get_object_groups());
93 $smarty->assign("contents", $this->contents);
94 $smarty->assign("launchimage","images/launch.png");
95 return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
96 }
99 /* Check if something is modified */
100 function contents_modified($display = FALSE)
101 {
102 $a = $this->contents;
103 $b = $this->contents_backup;
104 if(count($a) != count($b)){
105 if($display){
106 print_a(array_diff_assoc($a,$b));
107 }
108 return(TRUE);
109 }else{
110 foreach($a as $a_key => $a_val){
111 if(count(array_diff($a_val, $b[$a_key]))){
113 if($display){
114 print_a(array_diff($a_val, $b[$a_key]));
115 }
116 return(TRUE);
117 }
118 }
119 }
120 return(FALSE);
121 }
124 function load_csv_data($data = NULL)
125 {
126 if($data == NULL){
127 if(!file_exists($this->file_to_read) || !is_readable($this->file_to_read)){
128 print_red(sprintf(_("Can't locate or read csv storage file '%s'."),$this->file_to_read));
129 return(FALSE);
130 }
132 $fp = @fopen($this->file_to_read,"r");
133 if(!$fp){
134 print_red(sprintf(_("Can't read csv storage file '%s'."),$this->file_to_read));
135 return(FALSE);
136 }
138 $this->contents =array();
140 while(!feof($fp)){
141 $str = trim(fgets($fp,512));
143 /* Get mac address */
144 $og = trim(preg_replace("/^[^,;]*(,|;)/","",$str));
145 $mac = preg_replace("/(,|;).*$/","",$str);
147 if(!empty($og) || !empty($mac)){
148 $this->contents[] = array("MAC" => $mac , "OG" => $og);
149 }
150 }
151 fclose($fp);
152 }else{
153 $this->contents =array();
154 $rows = split("\n",$data);
155 foreach($rows as $str){
157 /* Get mac address */
158 $og = trim(preg_replace("/^[^,;]*(,|;)/","",$str));
159 $mac = preg_replace("/(,|;).*$/","",$str);
161 if(!empty($og) || !empty($mac)){
162 $this->contents[] = array("MAC" => $mac , "OG" => $og);
163 }
164 }
165 }
166 $this->contents_backup = $this->contents;
167 }
170 function save_csv_data()
171 {
172 if(!file_exists($this->file_to_read) || !is_writeable($this->file_to_read)){
173 print_red(sprintf(_("Can't locate or write csv storage file '%s'."),$this->file_to_read));
174 }else{
175 $fp = @fopen($this->file_to_read,"w");
176 if(!$fp){
177 print_red(sprintf(_("Can't write csv storage file '%s'."),$this->file_to_read));
178 }else{
179 $data = "";
180 foreach($this->contents as $val){
181 $data .= $val['MAC'].", ".$val['OG']."\n";
182 }
183 fwrite($fp,$data,strlen($data));
184 fclose($fp);
185 }
186 }
187 }
190 function save_object()
191 {
192 if(isset($_POST['gotomasses'])){
194 /* Check for input changes */
195 $ogs = $this->get_object_groups();
196 foreach($this->contents as $id => $data){
197 if(isset($_POST['mac_'.$id])){
198 $this->contents[$id]['MAC'] = $_POST['mac_'.$id];
199 }
200 if(isset($_POST['og_'.$id]) && in_array_ics($_POST['og_'.$id],$ogs)){
201 $this->contents[$id]['OG'] = $_POST['og_'.$id];
202 }
203 }
205 /* check for remove requests */
206 $once = TRUE;
207 foreach($_POST as $name => $value){
208 if(preg_match("/^remove_[0-9]*_(x|y)$/",$name) && $once){
209 $once = FALSE;
210 $id = preg_replace("/^remove_/","",$name);
211 $id = preg_replace("/_(x|y)$/","",$id);
213 if(isset($this->contents[$id])){
214 unset($this->contents[$id]);
215 }
216 }
217 }
219 /* Write back all changes */
220 if(isset($_POST['save_gotomass_changes'])){
221 $this->save_csv_data();
223 /* Call load again, so we will see if everything is fine.
224 * And load_csv_data causes the contents_backup to be updated
225 */
226 $this->load_csv_data();
227 }
229 /* Reload data from csv file ? */
230 if(isset($_POST['reload_gotomass_data'])){
231 $this->load_csv_data();
232 }
233 }
234 }
237 function plInfo()
238 {
239 return (array(
240 "plShortName" => _("Mass machine deployment"),
241 "plDescription" => _("Mass machine deployment addon"),
242 "plSelfModify" => FALSE,
243 "plDepends" => array(),
244 "plPriority" => 0,
245 "plSection" => array("addon"),
246 "plCategory" => array("gotomasses" => array("objectClass" => "none", "description" => _("Mass machine deployment"))),
247 "plProvidedAcls" => array()
248 ));
249 }
250 }
251 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
252 ?>