Code

Updated filtering again
[gosa.git] / plugins / admin / applications / class_applicationGeneric.inc
1 <?php
2 class application extends plugin
3 {
4   /* application attributes */
5   var $cn= "";
6   var $description= "";
7   var $base= "";
8   var $gosaApplicationExecute= "";
9   var $gosaApplicationName= "";
10   var $gosaApplicationFlags= "";
11   var $gosaApplicationIcon= "";
12   var $gotoLogonScript  ="";
13   var $iconData;
15   /* Headpage attributes */
16   var $last_sorting= "invalid";
17   var $applications= array();
19   /* attribute list for save action */
20   var $attributes= array("cn", "description", "gosaApplicationExecute", "gosaApplicationName","gosaApplicationIcon",
21       "gosaApplicationFlags","gotoLogonScript");
22   var $objectclasses= array("top", "gosaApplication");
24   var $isReleaseApplikation = false;
26   function application ($config, $dn= NULL, $parent= NULL)
27   {
28     plugin::plugin ($config, $dn, $parent);
30     $tmp = search_config($this->config->data,"faiManagement","CLASS");
31     if(!empty($tmp)) {
32       if(!preg_match("/^ou=apps,/",$_SESSION['appfilter']['release'])){
33         $this->isReleaseApplikation = true;  
34       }
35     }
37     /* Load icon */
38     $ldap= $config->get_ldap_link();
39     if ($dn != 'new'){
40       $this->iconData= $ldap->get_attribute($dn, "gosaApplicationIcon");
41       $this->saved_attributes['gosaApplicationIcon'] = $this->iconData;
42     }
43     if ($this->iconData == ""){
44       $this->set_picture("");
45     }
46     $_SESSION['binary']= $this->iconData;
47     $_SESSION['binarytype']= "image/jpeg";
48     $this->gosaApplicationIcon= $this->iconData;
50     /* This is always an account */
51     $this->is_account= TRUE;
53     if ($this->dn == "new"){
54       if(isset($_SESSION['CurrentMainBase'])){
55         $this->base= $_SESSION['CurrentMainBase'];
56       }else{
57         $ui= get_userinfo();
58         $this->base= dn2base($ui->dn);
59       }
60     } else {
61       $this->base= preg_replace ("/^[^,]+,[^,]+,/", "", $this->dn);
62     }
63   }
66   function generateTemplate(){
67     $str= "# This code is part of GOsa (https://gosa.gonicus.de)\n#\n";
69     $values = array();
70     $names      = array();
71     if($this->parent->by_object['applicationParameters']->is_account){
72       $names = $this->parent->by_object['applicationParameters']->option_name;
73       $values = $this->parent->by_object['applicationParameters']->option_value;
74     }
76     if (count($names)){
77       $str .="# This plugin handles these environment variables:\n";
78     } else {
79       $str .="# This plugin handles no environment variables.\n";
80     }
82     foreach($names as $index => $name){
84       // Fix length
85       for($i = strlen($name) ; $i < 30 ; $i++){
86         $name= $name." ";
87       }
88       if((isset($values[$index]))&&(!empty($values[$index]))){
89         $str.= "# ".$name."\t(e.g. '".$values[$index]."')\n";
90       }else{
91         $str.= "# ".$name."\t("._("no example").")\n";
92       }
93     }
94     $str .= "#\n".
95       "# Don't remove the following tag, it is used for header update.\n".
96       "### END HEADER ###";
98     return($str);
99   }
101   function execute()
102   {
103     /* Call parent execute */
104     plugin::execute();
106     $smarty= get_smarty();
108     if(isset($_POST['download'])){
109       header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
110       header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
111       header("Cache-Control: no-cache");
112       header("Pragma: no-cache");
113       header("Cache-Control: post-check=0, pre-check=0");
114       header("Content-type: application/octet-stream");
115       header( "Content-disposition: attachment; filename=".$this->cn.".gotoLogonScript" );
116       echo $this->gotoLogonScript;
117       exit();
118     }
120     /* Do we represent a valid group? */
121     if (!$this->is_account && $this->parent == NULL){
122       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
123         _("This 'dn' is no application.")."</b>";
124       return ($display);
125     }
127     $head = $this->generateTemplate();
128     $this->gotoLogonScript= $this->generateTemplate().preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
130     if((isset($_POST['upLoad']))&&(isset($_FILES['ScriptFile'])) && chkacl($this->acl,"gotoLogonScript") == ""){
131       $str = file_get_contents($_FILES['ScriptFile']['tmp_name']);
132       $this->gotoLogonScript = $str;
133     }
135     /* Fill templating stuff */
136     $smarty->assign("cn", $this->cn);
137     $smarty->assign("bases", $this->config->idepartments);
138     if ($this->dn == "new"){
139       $smarty->assign("selectmode", "");
140       $smarty->assign("namemode", "");
141     } else {
142       $smarty->assign("namemode", "readonly");
143       $smarty->assign("selectmode", "disabled");
144     }
145     
146     /* Base select dialog */
147     $once = true;
148     foreach($_POST as $name => $value){
149       if(preg_match("/^chooseBase/",$name) && $once && chkacl($this->acl,"base") == ""){
150         $once = false;
151         $this->dialog = new baseSelectDialog($this->config);
152         $this->dialog->setCurrentBase($this->base);
153       }
154     }
156     /* Dialog handling */
157     if(is_object($this->dialog)){
158       /* Must be called before save_object */
159       $this->dialog->save_object();
161       if($this->dialog->isClosed()){
162         $this->dialog = false;
163       }elseif($this->dialog->isSelected()){
164         $this->base = $this->dialog->isSelected();
165         $this->dialog= false;
166       }else{
167         return($this->dialog->execute());
168       }
169     }
171     /* Get random number for pictures */
172     srand((double)microtime()*1000000);
173     $smarty->assign("rand", rand(0, 10000));
175     /* Variables */
176     foreach(array("description", "gosaApplicationExecute", "gosaApplicationName","cn","gotoLogonScript","gosaApplicationIcon") as $val){
177       $smarty->assign($val, $this->$val);
178       $smarty->assign($val."ACL", chkacl($this->acl, $val));
179     }
180     $smarty->assign("baseACL", chkacl($this->acl,"base"));
182     /* Checkboxes */
183     foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
184           "L" => "place_on_kicker",
185           "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
186       if (preg_match("/$key/", $this->gosaApplicationFlags)){
187         $smarty->assign("$val", "checked");
188       } else {
189         $smarty->assign("$val", "");
190       }
191     }
193     $smarty->assign("isReleaseApplikation" , $this->isReleaseApplikation);
194     $smarty->assign("gotoLogonScript",htmlentities($this->gotoLogonScript, ENT_COMPAT, 'UTF-8'));
195     $smarty->assign("base_select", $this->base);
196     $smarty->assign("gosaApplicationFlagsACL", chkacl($this->acl, "gosaApplicationFlags"));
197     /* Show main page */
198     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
199   }
202   function remove_from_parent()
203   {
204     $ldap= $this->config->get_ldap_link();
205     $ldap->rmDir($this->dn);
206     show_ldap_error($ldap->get_error(), _("Removing application failed"));
208     /* Optionally execute a command after we're done */
209     $this->handle_post_events("remove");
211     /* Delete references to object groups */
212     $ldap->cd ($this->config->current['BASE']);
213     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
214     while ($ldap->fetch()){
215       $og= new ogroup($this->config, $ldap->getDN());
216       unset($og->member[$this->dn]);
217       $og->save ();
218       show_ldap_error($ldap->get_error(), sprintf(_("Removing application from objectgroup '%s' failed"), $og->dn));
219     }
220     $ldap->search ("(&(objectClass=posixGroup)(gosaMemberApplication=".$this->cn."))", array("cn"));
221     while ($attrs= $ldap->fetch()){
222       $ag= new appgroup($this->config, $ldap->getDN());
223       $ag->removeApp($this->cn);
224       $ag->save ();
225       show_ldap_error($ldap->get_error(), sprintf(_("Removing application from group '%s' failed"), $ag->dn));
226     }
228   }
231   /* Save data to object */
232   function save_object()
233   {
234     if($this->isReleaseApplikation){
235       $tmpBase = $this->base;
236     } 
237  
238     if (isset($_POST['cn'])){
240       /* Save attributes */
241       parent::save_object();
243       /* Save application flags */
244       $flag= "";
245       if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
246         $flag.= "G";
247       }
248       if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
249         $flag.= "D";
250       }
251       if (isset($_POST['place_on_kicker']) && $_POST['place_on_kicker'] == 1){
252         $flag.= "L";
253       }
254       if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
255         $flag.= "M";
256       }
257       if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
258         $flag.= "O";
259       }
260       if (chkacl ($this->acl, "gosaApplicationFlags") ==""){
261         $this->gosaApplicationFlags= "[$flag]";
262       }
264       /* Check for picture upload */
265       if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != "" && chkacl($this->acl,"gosaApplicationIcon") == ""){
266         if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
267           print_red (_("The specified picture has not been uploaded correctly."));
268         }
270         if (!function_exists("imagick_blob2image")){
271           /* Get temporary file name for conversation */
272           $fname = tempnam ("/tmp", "GOsa");
274           /* Open file and write out photoData */
275           $fp = fopen ($fname, "w");
276           fwrite ($fp, $_FILES['picture_file']['tmp_name']);
277           fclose ($fp);
279           /* Build conversation query. Filename is generated automatically, so
280              we do not need any special security checks. Exec command and save
281              output. For PHP safe mode, you'll need a configuration which respects
282              image magick as executable... */
283           $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
284           @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
286           /* Read data written by convert */
287           $output= "";
288           $sh= popen($query, 'r');
289           while (!feof($sh)){
290             $output.= fread($sh, 4096);
291           }
292           pclose($sh);
294           unlink($fname);       
295         } else {
297           /* Load the new uploaded Photo */
298           if(!$handle  =  imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
299             gosa_log("Can't Load image");
300           }
302           /* Resizing image to 147x200 and blur */
303           if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
304             gosa_log("imagick_resize failed");
305           }
307           /* Converting image to JPEG */
308           if(!imagick_convert($handle,"PNG")) {
309             gosa_log("Can't Convert to PNG");
310           }
312           if(imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
313             gosa_log("can't write to specified folder");
314           }
316           imagick_free($handle);
317         }
319         /* Activate new picture */
320         $this->set_picture($_FILES['picture_file']['tmp_name']);
321       } 
324       /* Save base, since this is no LDAP attribute */
325       if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
326         $this->base= $_POST['base'];
327       }
328     }
330     if($this->isReleaseApplikation){
331       $this->base = $tmpBase;
332     }
333   }
336   /* Check values */
337   function check()
338   {
339     /* Call common method to give check the hook */
340     $message= plugin::check();
342     if(!preg_match("#^/#",$this->gosaApplicationExecute)){
343       $message[]=(_("Specified execute path must start with '/'."));
344     }
346     /* Permissions for that base? */
347     if ($this->base != ""){
348       $new_dn= "cn=".$this->cn.",ou=apps,".$this->base;
349     } else {
350       $new_dn= $this->dn;
351     }
353     $ui= get_userinfo();
354     $acl= get_permissions ($new_dn, $ui->subtreeACL);
355     $acl= get_module_permission($acl, "application", $new_dn);
356     if (chkacl($acl, "create") != ""){
357       $message[]= _("You have no permissions to create a application on this 'Base'.");
358     }
360     /* All required fields are set? */
361     if ($this->cn == ""){
362       $message[]= _("Required field 'Name' is not filled.");
363     }
365     if(preg_match("/[^a-z0-9]/",$this->cn))     {
366       $message[]=_("Invalid character in application name. Only a-z 0-9 are allowed.");
367     }
369     if ($this->gosaApplicationExecute == ""){
370       $message[]= _("Required field 'Execute' is not filled.");
371     }
373     /* Check for existing application */
374     $ldap= $this->config->get_ldap_link();
375     $ldap->cd($this->config->current["BASE"]);
377     $tmp = search_config($this->config->data,"faiManagement","CLASS");
378     if((!empty($tmp)) && (isset($_SESSION['appfilter']['release']))){
379       $baseDn = str_replace($this->config->current['BASE'],$this->base,$_SESSION['appfilter']['release']);
380       $baseDn = preg_replace("/ou=apps,.*/","ou=apps,".$this->base,$_SESSION['appfilter']['release']);
381       $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))",$baseDn,array("cn"));
382       if($ldap->count()){
383         $attrs = $ldap->fetch();
384         if($this->dn != $attrs['dn']) {
385           $message[]= _("There's already an application with this 'Name'.");
386         }
387       }
388     }else{
389       $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))","ou=apps,".$this->base,array("cn"));
390       if ($ldap->count()){
391         $attrs = $ldap->fetch();
392         if($this->dn != $attrs['dn']) {
393           $message[]= _("There's already an application with this 'Name'.");
394         }
395       }
396     }
397     return $message;
398   }
401   /* Save to LDAP */
402   function save()
403   {
404     /* Get application script without header part, to check if we must save the script itself */
405     $script = preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
407     plugin::save();
408     $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
410     /* Write back to ldap */
411     $ldap= $this->config->get_ldap_link();
412     $ldap->cat($this->dn, array('dn'));
414     $a= $ldap->fetch();
415     if (count($a)){
416       
417       /* Remove gotoLogonScript if it is empty */
418       if(empty($script))    {
419         $this->attrs['gotoLogonScript'] = array();
420       }
422       $ldap->cd($this->dn);
423       $this->cleanup();
424       $ldap->modify ($this->attrs); 
425       $this->handle_post_events("modify");
426     } else {
428       /* Remove gotoLogonScript if it is empty */
429       if(empty($script))    {
430         unset($this->attrs['gotoLogonScript']);
431       }
433       $ldap->cd($this->config->current['BASE']);
434       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
435       $ldap->cd($this->dn);
436       $ldap->add($this->attrs);
437       $this->handle_post_events("add");
438     }
439     show_ldap_error($ldap->get_error(), _("Saving application failed"));
440   }
442   function set_picture($filename)
443   {
444     if (!is_file($filename)){
445       $filename= "./images/default_icon.png";
446       $this->gosaApplicationIcon= "*removed*";
447     }
449     if (file_exists($filename)){
450       $fd = fopen ($filename, "rb");
451       $this->iconData= fread ($fd, filesize ($filename));
452       $_SESSION['binary']= $this->iconData;
453       $_SESSION['binarytype']= "image/jpeg";
454       $this->gosaApplicationIcon= $this->iconData;
456       fclose ($fd);
457     }
458   }
460   function getCopyDialog()
461   {
462     $vars = array("cn");
464     $str ="<h2>"._("Application settings")."</h2>
465       <table>
466       <tr>
467       <td>".
468       _("Application name"). 
469       "</td>  
470       <td>
471       <input id='gosaApplicationName' name='cn' size='35' maxlength='60' 
472       value='".$this->cn."' 
473       title='"._("Application name to be displayed (i.e. below icons)")."'>                     
474       </td>
475       </tr>
476       </table>";
477     $ret = array();
478     $ret['status'] = "";
479     $ret['string'] = $str;
480     return($ret);
481   }
483   function saveCopyDialog()
484   {
485     if(isset($_POST['cn'])){
486       $this->cn = $_POST['cn'];
487     }
489     $tmp = search_config($this->config->data,"faiManagement","CLASS");
490     if(!empty($tmp)) {
491       if(!preg_match("/^ou=apps,/",$_SESSION['appfilter']['release'])){
492         $this->isReleaseApplikation = true;  
493       }
494     }
496     if($_SESSION['CurrentMainBase']){
497       $this->base= $_SESSION['CurrentMainBase'];
498     }
499     
500   }
502 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
503 ?>