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