Code

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