1 <?php
3 class goto_task extends plugin
4 {
5 /* Definitions */
6 var $plHeadline = "System mass deployment";
7 var $plDescription = "This does something";
9 var $parent = NULL;
11 var $Minute = "*";
12 var $Hour = "*";
13 var $Day = "*";
14 var $Month = "*";
15 var $Weekday = "*";
16 var $Action = "install";
17 var $Comment = "";
18 var $OGroup = "keep_current";
19 var $OGroups = array();
20 var $Target = array();
21 var $Initial_Target = array();
22 var $Actions = array();
23 var $new = FALSE;
24 var $attributes = array("OGroup","Minute","Hour","Day","Month","Weekday","Action","Comment","Target","Initial_Target");
27 function goto_task($config,$parent,$data = array())
28 {
29 /* Set parent object */
30 $this->parent = $parent;
31 $this->acl = $parent->acl;
33 /* Intialize plugin */
34 $this->config = $config;
35 if(count($data)){
37 $tmp = $this->get_array_values();
38 foreach($this->attributes as $attr){
39 if(!isset($data[$attr])){
40 trigger_error("Missing parameter: '".$attr."' for goto_task contruction.");
41 }else{
42 $this->$attr = $data[$attr];
43 }
44 }
45 }else{
46 $this->new = TRUE;
47 }
49 /* Create ogroup select list */
50 $this->OGroups = array("\"\"" => "["._("Keep current")."]");
51 $this->OGroups = array_merge($this->OGroups,$this->parent->get_object_groups());
53 /* Prepare list of available actions */
54 $this->Actions = $this->parent->get_actions();
55 }
58 function execute()
59 {
60 /********
61 * Handle Posts
62 ********/
64 /* Handle Target options */
65 foreach($_POST as $name => $value){
66 if(preg_match("/^remove_/",$name)){
67 $value = preg_replace("/^remove_([0-9]*)_(x|y)$/i","\\1",$name);
68 if(isset($this->Target[$value]) && $this->Action != "initial_install"){
69 unset($this->Target[$value]);
70 }elseif(isset($this->Initial_Target[$value]) && $this->Action == "initial_install"){
71 unset($this->Initial_Target[$value]);
72 }
73 }
74 }
76 if($this->Action != "initial_install"){
78 /* Add target */
79 if(isset($_POST['add_target']) && !empty($_POST['target_text'])){
80 $target = get_post("target_text");
81 if($this->is_valid_target($target) && !in_array($target,$this->Target)){
82 $this->Target[] = $target;
83 }
84 }
85 }else{
87 /* Add target */
88 if(isset($_POST['add_target']) && !empty($_POST['task_MAC'])){
89 $MAC = $_POST['task_MAC'];
90 $NAME= "";
91 $IP = "";
92 if(isset($_POST['task_Name'])){
93 $NAME = $_POST['task_Name'];
94 }
95 if(isset($_POST['task_IP']) && is_ip($_POST['task_IP'])){
96 $IP = $_POST['task_IP'];
97 }
98 if(is_mac($MAC)){
99 $this->Initial_Target[] = array("MAC"=>$MAC,"IP"=>$IP,"NAME"=>$NAME);
100 }
101 }
102 }
104 /* Add via csv */
105 if(isset($_FILES['import_file'])){
106 $file = $_FILES['import_file']['tmp_name'];
107 if(file_exists($file) && is_readable($file)){
108 $str ="";
109 $fp = fopen($file,"r");
110 while(!feof($fp)){
111 $line = fgets($fp,512);
112 $tmp = preg_split("/(,|;)/",$line);
114 $MAC = $IP = $NAME = "";
115 if(isset($tmp[0])){
116 $MAC = $tmp[0];
117 }
118 if(isset($tmp[1])){
119 $IP = $tmp[1];
120 }
121 if(isset($tmp[2])){
122 $NAME = $tmp[2];
123 }
124 if(is_mac($MAC)){
125 $this->Initial_Target[] = array("MAC"=>$MAC,"IP"=>$IP,"NAME"=>$NAME);
126 }
127 }
128 }
129 }
131 /********
132 * Add target from list
133 ********/
135 /* If add from list is was requsted, display this list */
136 if(isset($_POST['add_from_list'])){
137 $this->dialog = new target_list($this->config,$this->Target);
138 }
140 /* Save selected objects as target */
141 if(isset($_POST['SaveMultiSelectWindow'])){
142 $this->dialog->save_object();
143 $ret = $this->dialog->save();
144 foreach($ret as $entry){
145 $this->Target[] = $entry['cn'][0];
146 }
147 $this->dialog = NULL;
148 }
150 /* Cancel object listing */
151 if(isset($_POST['CloseMultiSelectWindow'])){
152 $this->dialog = NULL;
153 }
155 /* Display object Listing */
156 if($this->dialog){
157 $this->dialog->save_object();
158 return($this->dialog->execute());
159 }
161 /********
162 * Display this plugin
163 ********/
165 $divlist = new divlist("goto_task");
166 $divlist->SetHeight("160");
167 $divlist->SetEntriesPerPage(0);
169 if(!preg_match("/disable/i",chkacl($this->parent->acl,"remove"))){
170 $field_del = array("string" => "<input type='image' src='images/edittrash.png' name='remove_%KEY%'>" ,
171 "attach" => "style='width:44px;border-right:0px;'");
172 }else{
173 $field_del = array("string" => "",
174 "attach" => "style='width:44px;border-right:0px;'");
175 }
177 /* Add entries to divlist */
178 if($this->Action == "initial_install"){
179 $divlist->SetHeader(array(
180 array("string" => _("Target systems")),
181 array("string" => _("IP") , "attach" => "style='width:90px;'"),
182 array("string" => _("Name") , "attach" => "style='width:150px;'"),
183 array("string" => _("Actions") , "attach" => "style='width:44px;border-right:0px;text-align:right;'")));
184 foreach($this->Initial_Target as $key => $target){
185 $field1 = array("string" => $target['MAC'] );
186 $field2 = array("string" => $target['IP'] ,"attach" => "style='width:90px;'");
187 $field3 = array("string" => $target['NAME'] ,"attach" => "style='width:150px;'");
188 $divlist->AddEntry(array($field1,$field2,$field3,preg_replace("/%KEY%/",$key,$field_del)));
189 }
190 }else{
191 $divlist->SetHeader(array(
192 array("string" => "Target", "attach" => "style=''"),
193 array("string" => "Actions" , "attach" => "style='width:44px;border-right:0px;text-align:right;'")));
194 foreach($this->Target as $key => $target){
195 $field1 = array("string" => $target);
196 $divlist->AddEntry(array($field1,preg_replace("/%KEY%/",$key,$field_del)));
197 }
198 }
200 $smarty = get_smarty();
201 foreach($this->attributes as $attr){
202 $smarty->assign($attr."ACL", chkacl($this->parent->acl,$attr));
203 $smarty->assign($attr,$this->$attr);
204 }
206 $tmp = $this->get_array_values();
207 $smarty->assign("JS" , $_SESSION['js']);
208 $smarty->assign("Minutes" , $tmp['Minute']);
209 $smarty->assign("Hours" , $tmp['Hour']);
210 $smarty->assign("Days" , $tmp['Day']);
211 $smarty->assign("Months" , $tmp['Month']);
212 $smarty->assign("Weekdays", $tmp['Weekday']);
213 $smarty->assign("OGroups" , $this->OGroups);
214 $smarty->assign("Actions" , $this->Actions);
215 $smarty->assign("Target_list" , $divlist->DrawList());
216 $smarty->assign("new" , $this->new);
217 return ($smarty->fetch (get_template_path('goto_task.tpl', TRUE)));
218 }
221 /* check given values */
222 function check()
223 {
224 $message = plugin::check();
225 $tmp = array(
226 "OGroup" => _("Object group") ,"Minute" => _("Minute"),
227 "Hour" => _("Hour") ,"Day" => _("Day"),
228 "Month" => _("Month") ,"Weekday"=> _("Week day"),
229 "Action" => _("Action") ,"Comment"=> _("Description"));
231 foreach($tmp as $name => $desc){
232 if(empty($this->$name)){
233 $message[] = sprintf(_("The given value for attribute '%s' is invalid."),$desc);
234 }
235 }
236 if(count($this->Target) == 0 && $this->Action != "initial_install"){
237 $message[] = sprintf(_("You must specify at least one target"));
238 }
239 if(count($this->Initial_Target) == 0 && $this->Action == "initial_install"){
240 $message[] = sprintf(_("You must specify at least one target"));
241 }
242 return($message);
243 }
246 function save_object()
247 {
248 if(isset($_POST['goto_task_posted'])){
249 plugin::save_object();
250 foreach($this->attributes as $attr){
251 if(in_array($attr,array("Initial_Target","Target"))){
252 continue;
253 }
254 if(isset($_POST[$attr])){
255 $this->$attr = get_post($attr);
256 }
257 }
258 }
259 }
262 /* Check if given target is vald.
263 * It must either be a valid MAC address or an existing object group
264 */
265 function is_valid_target($str)
266 {
267 if(is_mac($str)){
268 return(TRUE);
269 }else{
270 $ldap = $this->config->get_ldap_link();
271 $ldap->cd($this->config->current['BASE']);
272 $ldap->search("(&(objectClassgosaGroupOfNames)(cn=".$str."))",array("cn"));
273 if($ldap->count()){
274 return(TRUE);
275 }
276 }
277 }
279 function save()
280 {
281 $tmp = array();
282 foreach($this->attributes as $attr){
283 $tmp[$attr] = $this->$attr;
284 }
285 return($tmp);
286 }
289 /* Return values for listboxes.
290 */
291 function get_array_values()
292 {
293 $ret = array();
295 /* Create minute array */
296 $Minute = array( "*" => "*",
297 "*/1" => "*/1",
298 "*/3" => "*/3",
299 "*/5" => "*/5",
300 "*/10" => "*/10",
301 "*/15" => "*/15",
302 "*/30" => "*/30",
303 "*/45" => "*/45",
304 "*/60" => "*/60");
305 for($i = 0; $i < 60 ; $i ++){
306 $Minute[$i] = $i;
307 }
309 /* Create hour array */
310 $Hour = array( "*" => "*");
311 for($i = 1 ; $i < 24 ; $i ++ ){
312 $Hour["*/".$i] = "*/".$i;
313 }
314 for($i = 0 ; $i < 24 ; $i ++ ){
315 $Hour[$i] = $i;
316 }
318 /* Create hour array */
319 $Day = array( "*" => "*");
320 for($i = 1 ; $i < 32 ; $i ++ ){
321 $Day["*/".$i] = "*/".$i;
322 }
323 for($i = 1 ; $i < 32 ; $i ++ ){
324 $Day[$i] = $i;
325 }
327 /* Create month array */
328 $Month = array( "*" => "*");
329 for($i = 1 ; $i <= 12 ; $i ++ ){
330 $Month["*/".$i] = "*/".$i;
331 }
332 for($i = 1 ; $i <= 12 ; $i ++ ){
333 $Month[$i] = $i;
334 }
336 /* Create week day array */
337 $Weekday = array( "*" => "*");
338 for($i = 1 ; $i <= 7 ; $i ++ ){
339 $Weekday["*/".$i] = "*/".$i;
340 }
341 for($i = 0 ; $i <= 7 ; $i ++ ){
342 $Weekday[$i] = $i;
343 }
345 foreach(array("Minute","Weekday","Hour","Day","Month") as $var){
346 $ret[$var] = $$var;
347 }
348 return($ret);
349 }
350 }
351 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
352 ?>