c362ab439c81a5d531b04118f5704a27b786c602
1 <?php
3 class gotomasses extends plugin
4 {
5 /* Definitions */
6 var $plHeadline = "System mass deployment";
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();
24 var $view_logged = FALSE;
25 var $contents_initially_loaded = FALSE;
27 function gotomasses($config, $dn= NULL)
28 {
29 /* Define source file */
30 $this->file_to_read = CONFIG_DIR."/gotomasses_machines";
32 /* Include config object */
33 $this->config= $config;
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 /* Load contents */
53 if(!$this->contents_initially_loaded){
54 $this->load_csv_data();
55 $this->contents_initially_loaded = TRUE;
56 }
58 /* Log view */
59 if(!$this->view_logged){
60 $this->view_logged = TRUE;
61 @log::log("view","gotomasses/".get_class($this),$this->dn);
62 }
64 if(isset($_POST['export_gotomass_csv']) && $this->acl_is_writeable("something")){
65 $data = "";
66 foreach($this->contents as $val){
67 $data .= $val['MAC'].", ".$val['OG']."\n";
68 }
69 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
70 header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
71 header("Cache-Control: no-cache");
72 header("Pragma: no-cache");
73 header("Cache-Control: post-check=0, pre-check=0");
74 header("Content-type: text/plain");
75 if (preg_match('/MSIE 5.5/', $_SERVER['HTTP_USER_AGENT']) ||
76 preg_match('/MSIE 6.0/', $_SERVER['HTTP_USER_AGENT'])){
77 header('Content-Disposition: filename="gotomass.csv"');
78 } else {
79 header('Content-Disposition: attachment; filename="gotomass.csv";');
80 }
81 echo $data;
82 exit();
83 }
85 /* Import given file */
86 if(isset($_POST['import_gotomass_csv']) && isset($_FILES['mass_file'])){
87 if(!$this->acl_is_writeable("something")){
88 print_red(_("Your are not allowed to import csv data into this plugin."));
89 }else{
90 $str = @file_get_contents($_FILES['mass_file']['tmp_name']);
91 if(empty($str)){
92 print_red(_("The uploaded file seams to be empty, import aborted."));
93 }else{
94 $this->load_csv_data($str);
95 }
96 }
97 }
99 /* Add a new empty entry to the list */
100 if(isset($_POST['add_new_entry'])){
101 $this->contents[] = array("MAC" => "", "OG" => "","VALID_MAC" => FALSE);
102 }
104 /* Call parent execute */
105 plugin::execute();
106 $smarty= get_smarty();
107 $smarty->assign("is_writeable",$this->acl_is_writeable("something"));
108 $smarty->assign("is_readable", $this->acl_is_readable("something"));
109 $smarty->assign("contents_modified",$this->contents_modified());
110 $smarty->assign("ogs", $this->get_object_groups());
111 $smarty->assign("contents", $this->contents);
112 $smarty->assign("launchimage","images/launch.png");
113 return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
114 }
117 /* Check if something is modified */
118 function contents_modified($display = FALSE)
119 {
120 $a = $this->contents;
121 $b = $this->contents_backup;
122 if(count($a) != count($b)){
123 if($display){
124 print_a(array_diff_assoc($a,$b));
125 }
126 return(TRUE);
127 }else{
128 foreach($a as $a_key => $a_val){
129 if(count(array_diff($a_val, $b[$a_key]))){
131 if($display){
132 print_a(array_diff($a_val, $b[$a_key]));
133 }
134 return(TRUE);
135 }
136 }
137 }
138 return(FALSE);
139 }
142 function load_csv_data($data = NULL)
143 {
144 $ui = get_userinfo();
146 if(!$this->acl_is_readable("something")){
147 $this->contents =array();
148 print_red(_("Your are not allowed to view contents of this plugin."));
149 return(FALSE);
150 }
152 if($data == NULL){
153 if(!file_exists($this->file_to_read) || !is_readable($this->file_to_read)){
154 print_red(sprintf(_("Can't locate or read csv storage file '%s'."),$this->file_to_read));
155 return(FALSE);
156 }
158 $fp = @fopen($this->file_to_read,"r");
159 if(!$fp){
160 print_red(sprintf(_("Can't read csv storage file '%s'."),$this->file_to_read));
161 return(FALSE);
162 }
164 $this->contents =array();
166 while(!feof($fp)){
167 $str = trim(fgets($fp,512));
169 /* Get mac address */
170 $og = trim(preg_replace("/^[^,;]*(,|;)/","",$str));
171 $mac = preg_replace("/(,|;).*$/","",$str);
173 if(!empty($og) || !empty($mac)){
174 $this->contents[] = array("MAC" => $mac , "OG" => $og,"VALID_MAC" => is_mac($mac));
175 }
176 }
177 fclose($fp);
178 $this->contents_backup = $this->contents;
179 }else{
180 $this->contents =array();
181 $rows = split("\n",$data);
182 foreach($rows as $str){
184 /* Get mac address */
185 $og = trim(preg_replace("/^[^,;]*(,|;)/","",$str));
186 $mac = preg_replace("/(,|;).*$/","",$str);
188 if(!empty($og) || !empty($mac)){
189 $this->contents[] = array("MAC" => $mac , "OG" => $og, "VALID_MAC" => is_mac($mac));
190 }
191 }
192 }
193 }
196 function save_csv_data()
197 {
198 if(!$this->acl_is_writeable("something")){
199 $this->contents =array();
200 print_red(_("Your are not allowed to write the content of this plugin."));
201 return(FALSE);
202 }
204 if(!file_exists($this->file_to_read) || !is_writeable($this->file_to_read)){
205 print_red(sprintf(_("Can't locate or write csv storage file '%s'."),$this->file_to_read));
206 }else{
207 $fp = @fopen($this->file_to_read,"w");
208 if(!$fp){
209 print_red(sprintf(_("Can't write csv storage file '%s'."),$this->file_to_read));
210 }else{
211 $data = "";
212 foreach($this->contents as $val){
213 $data .= $val['MAC'].", ".$val['OG']."\n";
214 }
215 fwrite($fp,$data,strlen($data));
216 fclose($fp);
217 }
218 }
219 }
222 function save_object()
223 {
224 if(isset($_POST['gotomasses'])){
226 /* Check for input changes */
227 $ogs = $this->get_object_groups();
228 foreach($this->contents as $id => $data){
229 if(isset($_POST['mac_'.$id])){
230 $this->contents[$id]['MAC'] = $_POST['mac_'.$id];
231 $this->contents[$id]['VALID_MAC'] = is_mac($_POST['mac_'.$id]);
232 }
233 if(isset($_POST['og_'.$id]) && in_array_ics($_POST['og_'.$id],$ogs)){
234 $this->contents[$id]['OG'] = $_POST['og_'.$id];
235 }
236 }
238 /* check for remove requests */
239 $once = TRUE;
240 foreach($_POST as $name => $value){
241 if(preg_match("/^remove_[0-9]*_(x|y)$/",$name) && $once){
242 $once = FALSE;
243 $id = preg_replace("/^remove_/","",$name);
244 $id = preg_replace("/_(x|y)$/","",$id);
246 if(isset($this->contents[$id])){
247 unset($this->contents[$id]);
248 }
249 }
250 }
252 /* Write back all changes */
253 if(isset($_POST['save_gotomass_changes'])){
254 $this->save_csv_data();
256 /* Call load again, so we will see if everything is fine.
257 * And load_csv_data causes the contents_backup to be updated
258 */
259 $this->load_csv_data();
260 }
262 /* Reload data from csv file ? */
263 if(isset($_POST['reload_gotomass_data'])){
264 $this->load_csv_data();
265 }
266 }
267 }
270 function plInfo()
271 {
272 return (array(
273 "plShortName" => _("System mass deployment"),
274 "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
275 "plSelfModify" => FALSE,
276 "plDepends" => array(),
277 "plPriority" => 0,
278 "plSection" => array("addon"),
279 "plCategory" => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
280 "plProvidedAcls" => array()
281 ));
282 }
283 }
284 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
285 ?>