Code

Updated trunk, introduced gosa-core
[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";
26   var $view_logged = FALSE;
28   function msgplug (&$config, $dn= NULL)
29   {
30     /* Include config object */
31     $this->config= &$config;
32     $ui= get_userinfo();
33     $tag= $ui->gosaUnitTag;
35     /* Preset values */
36     $this->targets= array("user" => _("Users"), "group" => _("Groups"));
37     asort($this->targets);
39     $res = get_list("(objectClass=gosaAccount)", "users", $this->config->current['BASE'],array('uid', 'cn'),GL_SUBSEARCH);
40     foreach($res as $key => $attrs){
41       $this->users['U:'.$attrs['uid'][0]]= $attrs['cn'][0].' ['.$attrs['uid'][0].']';
42     }
43     ksort($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     /* Log view */
86     if(!$this->view_logged){
87       $this->view_logged = TRUE;
88       new log("view","msgplug/".get_class($this),$this->dn);
89     }
91     /* Send message? */
92     if (isset($_POST['send']) && $this->acl_is_writeable("notify")){
94       /* Do we have recipients? */
95       if (count($this->recipients)){
97         /*Permissions ok? */
98         if (!$this->acl_is_writeable('notify')){
99           print_red(_("You have no permissions to send a message!"));
100         } else {
101           $cmd= $this->config->search("msgplug", "NOTIFY_COMMAND",array('menu'));
102           if ($cmd == ""){
103             print_red(_("No NOTIFY_COMMAND definition found in your gosa.conf"));
104           } else {
105             $parameters= base64_encode($this->nmessage) ." ";
106             foreach ($this->recipients as $key => $value){
107               $parameters.= "$key ";
108             }
109             exec ("$cmd $parameters", $dummy, $retval);
110             if ($retval != 0){
111               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
112             }
113             $this->finalized= true;
114           }
115         }
116       } else {
117         print_red(_("Please specify at least one recipient to send a message!"));
118       }
119     }
121     /* Bounce back to the original dialog */
122     if (isset($_POST['continue'])){
123       $this->finalized= false;
124     }
126     /* Add to list? */
127     if (isset($_POST['add']) && isset($_POST['source']) && $this->acl_is_writeable("notify")){
128       foreach ($_POST['source'] as $key){
129         if ($this->target == 'user'){
130           if(isset($this->users[$key])){
131             $this->recipients[$key]= $this->users[$key];
132           }
133         }
134         if ($this->target == 'group'){
135           if(isset($this->groups[$key])){
136             $this->recipients[$key]= $this->groups[$key];
137           }
138         }
139       }
140       ksort($this->recipients);
141     }
143     /* Remove from list? */
144     if (isset($_POST['del']) && isset($_POST['recipient'])){
145       foreach ($_POST['recipient'] as $key){
146           unset($this->recipients[$key]);
147       }
148     }
150     /* Import message? */
151     if (isset($_POST["import_template"]) && isset($this->templates[$_POST["nmessage_template"]])){
152       $contents= "";
153       $lines= file($_POST["nmessage_template"]);
154       foreach ($lines as $line){
155         if (!preg_match('/^DESC:/', $line)){
156           $contents.= $line;
157         }
158       }
160       /* Replace attributes */
161       $ui= get_userinfo();
162       $contents= preg_replace('/%self/', $ui->cn, $contents);
164       /* Save message */
165       $this->nmessage= htmlspecialchars($contents);
166     }
168     $smarty= get_smarty();
170     /* Assign possible target types */
171     $smarty->assign("targets", $this->targets);
172     foreach ($this->attributes as $attr){
173       $smarty->assign($attr, $this->$attr);
174     }
176     /* Generate list */
177     $tmp= array();
178     foreach (array("user" => "users", "group" => "groups") as $field => $arr){
179       if ($this->target == $field){
180         foreach ($this->$arr as $key => $value){
181           if (!isset($this->recipients[$key])){
182             $tmp[$key]= $value;
183           }
184         }
185       }
186     }
187     $smarty->assign('sources', $tmp);
188     $smarty->assign('recipients', $this->recipients);
190     /* Assign ACL */
191     $smarty->assign('nmessageACL', $this->getacl("notify"));
193     /* Handle templates */
194     $smarty->assign('show_templates', $this->show_templates?"true":"false");
195     $smarty->assign('message_templates', $this->templates);
196     $smarty->assign('template', $this->template);
197     $smarty->assign('finished', $this->finalized?"true":"false");
199     /* Show main page */
200     return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
201   }
204   function parse_notification($file)
205   {
206     $desc= "";
208     if (is_file($file)){
209       $fh = fopen($file, "r");
210       $line= fgets($fh, 256);
212       if (!preg_match('/^DESC:/', $line)){
213         print_red (_("No DESC tag in message file:")." $file");
214         return $desc;
215       }
216       fclose ($fh);
218       $desc= trim(preg_replace('/^DESC:\s*/', '', $line));
219     }
221     return $desc;
222   }
224   
225   function save_object()
226   {
227     plugin::save_object();
228     foreach($this->attributes as $attr){
229       if(isset($_POST[$attr])){
230         $this->$attr = $_POST[$attr];
231       }
232     }
233   }
235   
236   /* Return plugin informations for acl handling */
237   static function plInfo()
238   {
239     return (array(
240         "plShortName"   => _("Notification"),
241         "plDescription" => _("Notification plugin"),
242         "plSelfModify"  => FALSE,
243         "plDepends"     => array(),
244         "plPriority"    => 89,
245         "plSection"     => array("addon"),
246         "plCategory"    => array("msgplug" => array("objectClass" => "none", "description" => _("Notification plugin"))),
248         "plProvidedAcls" => array(
249             "notify"          => _("Allow sending notifications")
250           )
251         ));
252   }
257 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
258 ?>