Code

da52cb304ad45e052debdf21e503bc5bcdbb950e
[gosa.git] / plugins / admin / applications / class_applicationGeneric.inc
1 <?php
2 class application extends plugin
3 {
4   /* CLI vars */
5   var $cli_summary= "Handling of GOsa's application object";
6   var $cli_description= "Some longer text\nfor help";
7   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
9   /* application attributes */
10   var $cn= "";
11   var $description= "";
12   var $base= "";
13   var $gosaApplicationExecute= "";
14   var $gosaApplicationName= "";
15   var $gosaApplicationFlags= "";
16   var $gosaApplicationIcon= "";
17   var $gotoLogonScript  ="";
18   var $iconData;
20   /* Headpage attributes */
21   var $last_sorting= "invalid";
22   var $applications= array();
24   /* attribute list for save action */
25   var $attributes= array("cn", "description", "gosaApplicationExecute", "gosaApplicationName","gosaApplicationIcon",
26       "gosaApplicationFlags","gotoLogonScript");
27   var $objectclasses= array("top", "gosaApplication");
29   var $isReleaseApplikation = false;
31   function application ($config, $dn= NULL)
32   {
33     plugin::plugin ($config, $dn);
35     $tmp = search_config($this->config->data,"faiManagement","CLASS");
36     if(!empty($tmp)) {
37       if(!preg_match("/^ou=apps,/",$_SESSION['appfilter']['release'])){
38         $this->isReleaseApplikation = true;  
39       }
40     }
42     /* Load icon */
43     $ldap= $config->get_ldap_link();
44     if ($dn != 'new'){
45       $this->iconData= $ldap->get_attribute($dn, "gosaApplicationIcon");
46       $this->saved_attributes['gosaApplicationIcon'] = $this->iconData;
47     }
48     if ($this->iconData == ""){
49       $this->set_picture("");
50     }
51     $_SESSION['binary']= $this->iconData;
52     $_SESSION['binarytype']= "image/jpeg";
53     $this->gosaApplicationIcon= $this->iconData;
55     /* This is always an account */
56     $this->is_account= TRUE;
58     if ($this->dn == "new"){
59       if(isset($_SESSION['CurrentMainBase'])){
60         $this->base= $_SESSION['CurrentMainBase'];
61       }else{
62         $ui= get_userinfo();
63         $this->base= dn2base($ui->dn);
64       }
65     } else {
66       $this->base= preg_replace ("/^[^,]+,[^,]+,/", "", $this->dn);
67     }
68   }
71   function generateTemplate(){
72     $str= "# This code is part of GOsa (https://gosa.gonicus.de)\n#\n";
74     $values = array();
75     $names      = array();
76     if($this->parent->by_object['applicationParameters']->is_account){
77       $names = $this->parent->by_object['applicationParameters']->option_name;
78       $values = $this->parent->by_object['applicationParameters']->option_value;
79     }
81     if (count($names)){
82       $str .="# This plugin handles these environment variables:\n";
83     } else {
84       $str .="# This plugin handles no environment variables.\n";
85     }
87     foreach($names as $index => $name){
89       // Fix length
90       for($i = strlen($name) ; $i < 30 ; $i++){
91         $name= $name." ";
92       }
93       if((isset($values[$index]))&&(!empty($values[$index]))){
94         $str.= "# ".$name."\t(e.g. '".$values[$index]."')\n";
95       }else{
96         $str.= "# ".$name."\t("._("no example").")\n";
97       }
98     }
99     $str .= "#\n".
100       "# Don't remove the following tag, it is used for header update.\n".
101       "### END HEADER ###";
103     return($str);
104   }
106   function execute()
107   {
108     /* Call parent execute */
109     plugin::execute();
111     $smarty= get_smarty();
113     /* Do we represent a valid group? */
114     if (!$this->is_account && $this->parent == NULL){
115       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
116         _("This 'dn' is no application.")."</b>";
117       return ($display);
118     }
120     $head = $this->generateTemplate();
121     $this->gotoLogonScript= $this->generateTemplate().preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
123     if((isset($_POST['upLoad']))&&(isset($_FILES['ScriptFile']))){
124       $str = file_get_contents($_FILES['ScriptFile']['tmp_name']);
125       $this->gotoLogonScript = $str;
126     }
128     /* Fill templating stuff */
129     $smarty->assign("cn", $this->cn);
130     $smarty->assign("bases", $this->config->idepartments);
131     if ($this->dn == "new"){
132       $smarty->assign("selectmode", "");
133       $smarty->assign("namemode", "");
134     } else {
135       $smarty->assign("namemode", "readonly");
136       $smarty->assign("selectmode", "disabled");
137     }
139     /* Get random number for pictures */
140     srand((double)microtime()*1000000);
141     $smarty->assign("rand", rand(0, 10000));
143     /* Variables */
144     foreach(array("description", "gosaApplicationExecute", "gosaApplicationName","cn") as $val){
145       $smarty->assign($val, $this->$val);
146       $smarty->assign($val."ACL", chkacl($this->acl, $val));
147     }
149     /* Checkboxes */
150     foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
151           "L" => "place_on_kicker",
152           "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
153       if (preg_match("/$key/", $this->gosaApplicationFlags)){
154         $smarty->assign("$val", "checked");
155       } else {
156         $smarty->assign("$val", "");
157       }
158     }
160     $smarty->assign("isReleaseApplikation" , $this->isReleaseApplikation);
161     $smarty->assign("gotoLogonScript",htmlentities($this->gotoLogonScript, ENT_COMPAT, 'UTF-8'));
162     $smarty->assign("base_select", $this->base);
163     $smarty->assign("gosaApplicationFlagsACL", chkacl($this->acl, "gosaApplicationFlags"));
164     /* Show main page */
165     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
166   }
169   function remove_from_parent()
170   {
171     $ldap= $this->config->get_ldap_link();
172     $ldap->rmDir($this->dn);
174     /* Optionally execute a command after we're done */
175     $this->handle_post_events("remove");
177     /* Delete references to object groups */
178     $ldap->cd ($this->config->current['BASE']);
179     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
180     while ($ldap->fetch()){
181       $og= new ogroup($this->config, $ldap->getDN());
182       unset($og->member[$this->dn]);
183       $og->save ();
184     }
185     $ldap->search ("(&(objectClass=posixGroup)(gosaMemberApplication=".$this->cn."))", array("cn"));
186     while ($attrs= $ldap->fetch()){
187       $ag= new appgroup($this->config, $ldap->getDN());
188       $ag->removeApp($this->cn);
189       $ag->save ();
190     }
192   }
195   /* Save data to object */
196   function save_object()
197   {
198     if($this->isReleaseApplikation){
199       $tmpBase = $this->base;
200     } 
201  
202     if (isset($_POST['cn'])){
204       /* Save attributes */
205       parent::save_object();
207       /* Save application flags */
208       $flag= "";
209       if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
210         $flag.= "G";
211       }
212       if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
213         $flag.= "D";
214       }
215       if (isset($_POST['place_on_kicker']) && $_POST['place_on_kicker'] == 1){
216         $flag.= "L";
217       }
218       if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
219         $flag.= "M";
220       }
221       if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
222         $flag.= "O";
223       }
224       if (chkacl ($this->acl, "gosaApplicationFlags") ==""){
225         $this->gosaApplicationFlags= "[$flag]";
226       }
228       /* Check for picture upload */
229       if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
230         if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
231           print_red (_("The specified picture has not been uploaded correctly."));
232         }
234         if (!function_exists("imagick_blob2image")){
235           /* Get temporary file name for conversation */
236           $fname = tempnam ("/tmp", "GOsa");
238           /* Open file and write out photoData */
239           $fp = fopen ($fname, "w");
240           fwrite ($fp, $_FILES['picture_file']['tmp_name']);
241           fclose ($fp);
243           /* Build conversation query. Filename is generated automatically, so
244              we do not need any special security checks. Exec command and save
245              output. For PHP safe mode, you'll need a configuration which respects
246              image magick as executable... */
247           $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
248           @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
250           /* Read data written by convert */
251           $output= "";
252           $sh= popen($query, 'r');
253           while (!feof($sh)){
254             $output.= fread($sh, 4096);
255           }
256           pclose($sh);
258           unlink($fname);       
259         } else {
261           /* Load the new uploaded Photo */
262           if(!$handle  =  imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
263             gosa_log("Can't Load image");
264           }
266           /* Resizing image to 147x200 and blur */
267           if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
268             gosa_log("imagick_resize failed");
269           }
271           /* Converting image to JPEG */
272           if(!imagick_convert($handle,"PNG")) {
273             gosa_log("Can't Convert to PNG");
274           }
276           if(imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
277             gosa_log("can't write to specified folder");
278           }
280           imagick_free($handle);
281         }
283         /* Activate new picture */
284         $this->set_picture($_FILES['picture_file']['tmp_name']);
285       } 
288       /* Save base, since this is no LDAP attribute */
289       if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
290         $this->base= $_POST['base'];
291       }
292     }
294     if($this->isReleaseApplikation){
295       $this->base = $tmpBase;
296     }
297   }
300   /* Check values */
301   function check()
302   {
303     /* Call common method to give check the hook */
304     $message= plugin::check();
306     if(!preg_match("#^/#",$this->gosaApplicationExecute)){
307       $message[]=(_("Specified execute path must start with '/'."));
308     }
310     /* Permissions for that base? */
311     if ($this->base != ""){
312       $new_dn= "cn=".$this->cn.",ou=apps,".$this->base;
313     } else {
314       $new_dn= $this->dn;
315     }
317     $ui= get_userinfo();
318     $acl= get_permissions ($new_dn, $ui->subtreeACL);
319     $acl= get_module_permission($acl, "application", $new_dn);
320     if (chkacl($acl, "create") != ""){
321       $message[]= _("You have no permissions to create a application on this 'Base'.");
322     }
324     /* All required fields are set? */
325     if ($this->cn == ""){
326       $message[]= _("Required field 'Name' is not filled.");
327     }
329     if(preg_match("/[^a-z0-9]/",$this->cn))     {
330       $message[]=_("Invalid character in application name. Only a-z 0-9 are allowed.");
331     }
333     if ($this->gosaApplicationExecute == ""){
334       $message[]= _("Required field 'Execute' is not filled.");
335     }
337     /* Check for existing application */
338     $ldap= $this->config->get_ldap_link();
339     $ldap->cd($this->config->current["BASE"]);
341     $tmp = search_config($this->config->data,"faiManagement","CLASS");
342     if((!empty($tmp)) && (isset($_SESSION['appfilter']['release']))){
343       $baseDn = str_replace($this->config->current['BASE'],$this->base,$_SESSION['appfilter']['release']);
344       $baseDn = preg_replace("/ou=apps,.*/","ou=apps,".$this->base,$_SESSION['appfilter']['release']);
345       $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))",$baseDn,array("cn"));
346       if($ldap->count()){
347         $attrs = $ldap->fetch();
348         if($this->dn != $attrs['dn']) {
349           $message[]= _("There's already an application with this 'Name'.");
350         }
351       }
352     }else{
353       $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))","ou=apps,".$this->base,array("cn"));
354       if ($ldap->count()){
355         $attrs = $ldap->fetch();
356         if($this->dn != $attrs['dn']) {
357           $message[]= _("There's already an application with this 'Name'.");
358         }
359       }
360     }
361     return $message;
362   }
365   /* Save to LDAP */
366   function save()
367   {
368     plugin::save();
369     $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
371     /* Write back to ldap */
372     $ldap= $this->config->get_ldap_link();
373     $ldap->cat($this->dn);
375     $a= $ldap->fetch();
376     if (count($a)){
377       $ldap->cd($this->dn);
378       $this->cleanup();
379       $ldap->modify ($this->attrs); 
380       $this->handle_post_events("modify");
381     } else {
382       $ldap->cd($this->config->current['BASE']);
383       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
384       $ldap->cd($this->dn);
385       $ldap->add($this->attrs);
386       $this->handle_post_events("add");
387     }
388     show_ldap_error($ldap->get_error());
389   }
391   function set_picture($filename)
392   {
393     if (!is_file($filename)){
394       $filename= "./images/default_icon.png";
395       $this->gosaApplicationIcon= "*removed*";
396     }
398     if (file_exists($filename)){
399       $fd = fopen ($filename, "rb");
400       $this->iconData= fread ($fd, filesize ($filename));
401       $_SESSION['binary']= $this->iconData;
402       $_SESSION['binarytype']= "image/jpeg";
403       $this->gosaApplicationIcon= $this->iconData;
405       fclose ($fd);
406     }
407   }
409   function getCopyDialog()
410   {
411     $vars = array("cn");
413     $str ="<h2>"._("Application settings")."</h2>
414       <table>
415       <tr>
416       <td>".
417       _("Application name"). 
418       "</td>  
419       <td>
420       <input id='gosaApplicationName' name='cn' size='35' maxlength='60' 
421       value='".$this->cn."' 
422       title='"._("Application name to be displayed (i.e. below icons)")."'>                     
423       </td>
424       </tr>
425       </table>";
426     $ret = array();
427     $ret['status'] = "";
428     $ret['string'] = $str;
429     return($ret);
430   }
432   function saveCopyDialog()
433   {
434     if(isset($_POST['cn'])){
435       $this->cn = $_POST['cn'];
436     }
437   }
439 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
440 ?>