Code

Sometimes the base wasn't posted correctly
[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;
25   var $module = "msgplug";
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     $res = get_list("(objectClass=gosaAccount)", "users", $this->config->current['BASE'],array('uid', 'cn'),GL_SUBSEARCH);
39     foreach($res as $key => $attrs){
40       $this->users['U:'.$attrs['uid'][0]]= $attrs['cn'][0].' ['.$attrs['uid'][0].']';
41     }
42     ksort($this->users);
44     $res = get_list("(objectClass=posixGroup)", "groups", $this->config->current['BASE'],array('cn','description'));
45     foreach($res as $key => $attrs){
46       $dsc= "";
47       if (isset($attrs['description'][0])){
48         $dsc= $attrs['description'][0];
49       }
50       $this->groups['G:'.$attrs['cn'][0]]= $attrs['cn'][0].' ['.$dsc.']';
51     }
52     ksort($this->users);
55     /* Load templates */
56     if (isset($this->config->current['NOTIFYDIR'])){
57       $dir= $this->config->current['NOTIFYDIR'];
58       if (is_dir($dir) && is_readable($dir)){
60         /* Look for files and build the vacation array */
61         $dh= opendir($dir);
62         while ($file = readdir($dh)){
63           $description= $this->parse_notification("$dir/$file");
64           if ($description != ""){
65             $this->templates["$dir/$file"]= $description;
66           }
67         }
68         closedir($dh);
69       }
71       /* Enable templates if there are some... */
72       if (count($this->templates)){
73         $this->show_templates= true;
74       }
75     }
76   }
79   function execute()
80   {
81     /* Call parent execute */
82     plugin::execute();
84     /* Send message? */
85     if (isset($_POST['send']) && $this->acl_is_writeable("notify")){
87       /* Do we have recipients? */
88       if (count($this->recipients)){
90         /*Permissions ok? */
91         if (!$this->acl_is_writeable('notify')){
92           print_red(_("You have no permissions to send a message!"));
93         } else {
94           $cmd= search_config($this->config->data['MENU'], "msgplug", "NOTIFY_COMMAND");
95           if ($cmd == ""){
96             print_red(_("No NOTIFY_COMMAND definition found in your gosa.conf"));
97           } else {
98             $parameters= base64_encode($this->nmessage) ." ";
99             foreach ($this->recipients as $key => $value){
100               $parameters.= "$key ";
101             }
102             exec ("$cmd $parameters", $dummy, $retval);
103             if ($retval != 0){
104               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
105             }
106             $this->finalized= true;
107           }
108         }
109       } else {
110         print_red(_("Please specify at least one recipient to send a message!"));
111       }
112     }
114     /* Bounce back to the original dialog */
115     if (isset($_POST['continue'])){
116       $this->finalized= false;
117     }
119     /* Add to list? */
120     if (isset($_POST['add']) && isset($_POST['source']) && $this->acl_is_writeable("notify")){
121       foreach ($_POST['source'] as $key){
122         if ($this->target == 'user'){
123           if(isset($this->users[$key])){
124             $this->recipients[$key]= $this->users[$key];
125           }
126         }
127         if ($this->target == 'group'){
128           if(isset($this->groups[$key])){
129             $this->recipients[$key]= $this->groups[$key];
130           }
131         }
132       }
133       ksort($this->recipients);
134     }
136     /* Remove from list? */
137     if (isset($_POST['del']) && isset($_POST['recipient'])){
138       foreach ($_POST['recipient'] as $key){
139           unset($this->recipients[$key]);
140       }
141     }
143     /* Import message? */
144     if (isset($_POST["import_template"]) && isset($this->templates[$_POST["nmessage_template"]])){
145       $contents= "";
146       $lines= file($_POST["nmessage_template"]);
147       foreach ($lines as $line){
148         if (!preg_match('/^DESC:/', $line)){
149           $contents.= $line;
150         }
151       }
153       /* Replace attributes */
154       $ui= get_userinfo();
155       $contents= preg_replace('/%self/', $ui->cn, $contents);
157       /* Save message */
158       $this->nmessage= htmlspecialchars($contents);
159     }
161     $smarty= get_smarty();
163     /* Assign possible target types */
164     $smarty->assign("targets", $this->targets);
165     foreach ($this->attributes as $attr){
166       $smarty->assign($attr, $this->$attr);
167     }
169     /* Generate list */
170     $tmp= array();
171     foreach (array("user" => "users", "group" => "groups") as $field => $arr){
172       if ($this->target == $field){
173         foreach ($this->$arr as $key => $value){
174           if (!isset($this->recipients[$key])){
175             $tmp[$key]= $value;
176           }
177         }
178       }
179     }
180     $smarty->assign('sources', $tmp);
181     $smarty->assign('recipients', $this->recipients);
183     /* Assign ACL */
184     $smarty->assign('nmessageACL', $this->getacl("notify"));
186     /* Handle templates */
187     $smarty->assign('show_templates', $this->show_templates?"true":"false");
188     $smarty->assign('message_templates', $this->templates);
189     $smarty->assign('template', $this->template);
190     $smarty->assign('finished', $this->finalized?"true":"false");
192     /* Show main page */
193     return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
194   }
197   function parse_notification($file)
198   {
199     $desc= "";
201     if (is_file($file)){
202       $fh = fopen($file, "r");
203       $line= fgets($fh, 256);
205       if (!preg_match('/^DESC:/', $line)){
206         print_red (_("No DESC tag in message file:")." $file");
207         return $desc;
208       }
209       fclose ($fh);
211       $desc= trim(preg_replace('/^DESC:\s*/', '', $line));
212     }
214     return $desc;
215   }
217   
218   function save_object()
219   {
220     plugin::save_object();
221     foreach($this->attributes as $attr){
222       if(isset($_POST[$attr])){
223         $this->$attr = $_POST[$attr];
224       }
225     }
226   }
228   
229   /* Return plugin informations for acl handling */
230   function plInfo()
231   {
232     return (array(
233         "plShortName"   => _("Notification"),
234         "plDescription" => _("Notification plugin"),
235         "plSelfModify"  => FALSE,
236         "plDepends"     => array(),
237         "plPriority"    => 89,
238         "plSection"     => array("addon"),
239         "plCategory"    => array("msgplug" => array("objectClass" => "none", "description" => _("Notification plugin"))),
241         "plProvidedAcls" => array(
242             "notify"          => _("Allow sending notifications")
243           )
244         ));
245   }
250 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
251 ?>