Code

Updated workStartab, to handle empty release configurations
[gosa.git] / plugins / addons / notifications / class_msgplug.inc
1 <?php
3 class msgplug extends plugin
4 {
5   /* Definitions */
6   var $plHeadline= "Notifications";
7   var $plDescription= "This does something";
9   /* attribute list for save action */
10   var $attributes= array("target", "nmessage");
11   var $objectclasses= array();
13   /* Helpers */
14   var $target= "group";
15   var $nmessage= "";
17   var $targets= array();
18   var $users= array();
19   var $groups= array();
20   var $recipients= array();
21   var $show_templates= false;
22   var $templates= array();
23   var $template= "";
24   var $finalized= false;
27   function msgplug ($config, $dn= NULL)
28   {
29     /* Include config object */
30     $this->config= $config;
31     $ui= get_userinfo();
32     $tag= $ui->gosaUnitTag;
34     /* Preset values */
35     $this->targets= array("user" => _("Users"), "group" => _("Groups"));
36     asort($this->targets);
38     /* Users */
39     $ldap= $config->get_ldap_link();
40     $ldap->cd($config->current['BASE']);
41     if ($tag == ""){
42       $ldap->search('(objectClass=gosaAccount)', array('uid', 'cn'));
43     } else {
44       $ldap->search('(&(objectClass=gosaAccount)(gosaUnitTag='.$tag.'))', array('uid', 'cn'));
45     }
46     while ($attrs= $ldap->fetch()){
47       $this->users['U:'.$attrs['uid'][0]]= $attrs['cn'][0].' ['.$attrs['uid'][0].']';
48     }
49     ksort($this->users);
51     /* Groups */
52     $ldap->cd($config->current['BASE']);
53     if ($tag == ""){
54       $ldap->search('(objectClass=posixGroup)', array('cn', 'description'));
55     } else {
56       $ldap->search('(&(objectClass=posixGroup)(gosaUnitTag='.$tag.'))', array('cn', 'description'));
57     }
58     while ($attrs= $ldap->fetch()){
59       $dsc= "";
60       if (isset($attrs['description'][0])){
61         $dsc= $attrs['description'][0];
62       }
63       $this->groups['G:'.$attrs['cn'][0]]= $attrs['cn'][0].' ['.$dsc.']';
64     }
65     ksort($this->groups);
67     /* Load templates */
68     if (isset($this->config->current['NOTIFYDIR'])){
69       $dir= $this->config->current['NOTIFYDIR'];
70       if (is_dir($dir) && is_readable($dir)){
72         /* Look for files and build the vacation array */
73         $dh= opendir($dir);
74         while ($file = readdir($dh)){
75           $description= $this->parse_notification("$dir/$file");
76           if ($description != ""){
77             $this->templates["$dir/$file"]= $description;
78           }
79         }
80         closedir($dh);
81       }
83       /* Enable templates if there are some... */
84       if (count($this->templates)){
85         $this->show_templates= true;
86       }
87     }
88   }
91   function execute()
92   {
93     /* Call parent execute */
94     plugin::execute();
96     /* Send message? */
97     if (isset($_POST['send'])){
99       /* Do we have recipients? */
100       if (count($this->recipients)){
102         /*Permissions ok? */
103         if (chkacl($this->acl, 'notify') != ""){
104           print_red(_("You have no permissions to send a message!"));
105         } else {
106           $cmd= search_config($this->config->data['MENU'], "msgplug", "NOTIFY_COMMAND");
107           if ($cmd == ""){
108             print_red(_("No NOTIFY_COMMAND definition found in your gosa.conf"));
109           } else {
110             $parameters= base64_encode($this->nmessage) ." ";
111             foreach ($this->recipients as $key => $value){
112               $parameters.= "$key ";
113             }
114             exec ("$cmd $parameters", $dummy, $retval);
115             if ($retval != 0){
116               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
117             }
118             $this->finalized= true;
119           }
120         }
121       } else {
122         print_red(_("Please specify at least one recipient to send a message!"));
123       }
124     }
126     /* Bounce back to the original dialog */
127     if (isset($_POST['continue'])){
128       $this->finalized= false;
129     }
131     /* Add to list? */
132     if (isset($_POST['add']) && isset($_POST['source'])){
133       foreach ($_POST['source'] as $key){
134         if ($this->target == 'user'){
135           $this->recipients[$key]= $this->users[$key];
136         }
137         if ($this->target == 'group'){
138           $this->recipients[$key]= $this->groups[$key];
139         }
140       }
141       ksort($this->recipients);
142     }
144     /* Remove from list? */
145     if (isset($_POST['del']) && isset($_POST['recipient'])){
146       foreach ($_POST['recipient'] as $key){
147           unset($this->recipients[$key]);
148       }
149     }
151     /* Import message? */
152     if (isset($_POST["import_template"]) && isset($this->templates[$_POST["nmessage_template"]])){
153       $contents= "";
154       $lines= file($_POST["nmessage_template"]);
155       foreach ($lines as $line){
156         if (!preg_match('/^DESC:/', $line)){
157           $contents.= $line;
158         }
159       }
161       /* Replace attributes */
162       $ui= get_userinfo();
163       $contents= preg_replace('/%self/', $ui->cn, $contents);
165       /* Save message */
166       $this->nmessage= htmlspecialchars($contents);
167     }
169     $smarty= get_smarty();
171     /* Assign possible target types */
172     $smarty->assign("targets", $this->targets);
173     foreach ($this->attributes as $attr){
174       $smarty->assign($attr, $this->$attr);
175     }
177     /* Generate list */
178     $tmp= array();
179     foreach (array("user" => "users", "group" => "groups") as $field => $arr){
180       if ($this->target == $field){
181         foreach ($this->$arr as $key => $value){
182           if (!isset($this->recipients[$key])){
183             $tmp[$key]= $value;
184           }
185         }
186       }
187     }
188     $smarty->assign('sources', $tmp);
189     $smarty->assign('recipients', $this->recipients);
191     /* Assign ACL */
192     $smarty->assign('nmessageACL', chkacl($this->acl, "notify"));
194     /* Handle templates */
195     $smarty->assign('show_templates', $this->show_templates?"true":"false");
196     $smarty->assign('message_templates', $this->templates);
197     $smarty->assign('template', $this->template);
198     $smarty->assign('finished', $this->finalized?"true":"false");
200     /* Show main page */
201     return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
202   }
205   function parse_notification($file)
206   {
207     $desc= "";
209     if (is_file($file)){
210       $fh = fopen($file, "r");
211       $line= fgets($fh, 256);
213       if (!preg_match('/^DESC:/', $line)){
214         print_red (_("No DESC tag in message file:")." $file");
215         return $desc;
216       }
217       fclose ($fh);
219       $desc= trim(preg_replace('/^DESC:\s*/', '', $line));
220     }
222     return $desc;
223   }
228 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
229 ?>