Code

Fixed msgs for applications / departments / fai
[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     }
138     
139     /* Base select dialog */
140     $once = true;
141     foreach($_POST as $name => $value){
142       if(preg_match("/^chooseBase/",$name) && $once){
143         $once = false;
144         $this->dialog = new baseSelectDialog($this->config,$this);
145         $this->dialog->setCurrentBase($this->base);
146       }
147     }
149     /* Dialog handling */
150     if(is_object($this->dialog)){
151       /* Must be called before save_object */
152       $this->dialog->save_object();
154       if($this->dialog->isClosed()){
155         $this->dialog = false;
156       }elseif($this->dialog->isSelected()){
157         $this->base = $this->dialog->isSelected();
158         $this->dialog= false;
159       }else{
160         return($this->dialog->execute());
161       }
162     }
164     /* Get random number for pictures */
165     srand((double)microtime()*1000000);
166     $smarty->assign("rand", rand(0, 10000));
168     /* Variables */
169     foreach(array("description", "gosaApplicationExecute", "gosaApplicationName","cn") as $val){
170       $smarty->assign($val, $this->$val);
171       $smarty->assign($val."ACL", chkacl($this->acl, $val));
172     }
173     $smarty->assign("baseACL", chkacl($this->acl,"base"));
175     /* Checkboxes */
176     foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
177           "L" => "place_on_kicker",
178           "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
179       if (preg_match("/$key/", $this->gosaApplicationFlags)){
180         $smarty->assign("$val", "checked");
181       } else {
182         $smarty->assign("$val", "");
183       }
184     }
186     $smarty->assign("isReleaseApplikation" , $this->isReleaseApplikation);
187     $smarty->assign("gotoLogonScript",htmlentities($this->gotoLogonScript, ENT_COMPAT, 'UTF-8'));
188     $smarty->assign("base_select", $this->base);
189     $smarty->assign("gosaApplicationFlagsACL", chkacl($this->acl, "gosaApplicationFlags"));
190     /* Show main page */
191     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
192   }
195   function remove_from_parent()
196   {
197     $ldap= $this->config->get_ldap_link();
198     $ldap->rmDir($this->dn);
199     show_ldap_error($ldap->get_error(), sprintf(_("Removing of applciation with dn '%s' failed."),$this->dn));
201     /* Optionally execute a command after we're done */
202     $this->handle_post_events("remove");
204     /* Delete references to object groups */
205     $ldap->cd ($this->config->current['BASE']);
206     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
207     while ($ldap->fetch()){
208       $og= new ogroup($this->config, $ldap->getDN());
209       unset($og->member[$this->dn]);
210       $og->save ();
211       show_ldap_error($ldap->get_error(), sprintf(_("Removing application from objectgroup '%s' failed"), $og->dn));
212     }
213     $ldap->search ("(&(objectClass=posixGroup)(gosaMemberApplication=".$this->cn."))", array("cn"));
214     while ($attrs= $ldap->fetch()){
215       $ag= new appgroup($this->config, $ldap->getDN());
216       $ag->removeApp($this->cn);
217       $ag->save ();
218       show_ldap_error($ldap->get_error(), sprintf(_("Removing application from group '%s' failed"), $ag->dn));
219     }
221   }
224   /* Save data to object */
225   function save_object()
226   {
227     if($this->isReleaseApplikation){
228       $tmpBase = $this->base;
229     } 
230  
231     if (isset($_POST['cn'])){
233       /* Save attributes */
234       parent::save_object();
236       /* Save application flags */
237       $flag= "";
238       if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
239         $flag.= "G";
240       }
241       if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
242         $flag.= "D";
243       }
244       if (isset($_POST['place_on_kicker']) && $_POST['place_on_kicker'] == 1){
245         $flag.= "L";
246       }
247       if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
248         $flag.= "M";
249       }
250       if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
251         $flag.= "O";
252       }
253       if (chkacl ($this->acl, "gosaApplicationFlags") ==""){
254         $this->gosaApplicationFlags= "[$flag]";
255       }
257       /* Check for picture upload */
258       if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
259         if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
260           print_red (_("The specified picture has not been uploaded correctly."));
261         }
263         if (!function_exists("imagick_blob2image")){
264           /* Get temporary file name for conversation */
265           $fname = tempnam ("/tmp", "GOsa");
267           /* Open file and write out photoData */
268           $fp = fopen ($fname, "w");
269           fwrite ($fp, $_FILES['picture_file']['tmp_name']);
270           fclose ($fp);
272           /* Build conversation query. Filename is generated automatically, so
273              we do not need any special security checks. Exec command and save
274              output. For PHP safe mode, you'll need a configuration which respects
275              image magick as executable... */
276           $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
277           @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
279           /* Read data written by convert */
280           $output= "";
281           $sh= popen($query, 'r');
282           while (!feof($sh)){
283             $output.= fread($sh, 4096);
284           }
285           pclose($sh);
287           unlink($fname);       
288         } else {
290           /* Load the new uploaded Photo */
291           if(!$handle  =  imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
292             gosa_log("Can't Load image");
293           }
295           /* Resizing image to 147x200 and blur */
296           if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
297             gosa_log("imagick_resize failed");
298           }
300           /* Converting image to JPEG */
301           if(!imagick_convert($handle,"PNG")) {
302             gosa_log("Can't Convert to PNG");
303           }
305           if(imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
306             gosa_log("can't write to specified folder");
307           }
309           imagick_free($handle);
310         }
312         /* Activate new picture */
313         $this->set_picture($_FILES['picture_file']['tmp_name']);
314       } 
317       /* Save base, since this is no LDAP attribute */
318       if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
319         $this->base= $_POST['base'];
320       }
321     }
323     if($this->isReleaseApplikation){
324       $this->base = $tmpBase;
325     }
326   }
329   /* Check values */
330   function check()
331   {
332     /* Call common method to give check the hook */
333     $message= plugin::check();
335     if(!preg_match("#^/#",$this->gosaApplicationExecute)){
336       $message[]=(_("Specified execute path must start with '/'."));
337     }
339     /* Permissions for that base? */
340     if ($this->base != ""){
341       $new_dn= "cn=".$this->cn.",ou=apps,".$this->base;
342     } else {
343       $new_dn= $this->dn;
344     }
346     $ui= get_userinfo();
347     $acl= get_permissions ($new_dn, $ui->subtreeACL);
348     $acl= get_module_permission($acl, "application", $new_dn);
349     if (chkacl($acl, "create") != ""){
350       $message[]= _("You have no permissions to create a application on this 'Base'.");
351     }
353     /* All required fields are set? */
354     if ($this->cn == ""){
355       $message[]= _("Required field 'Name' is not filled.");
356     }
358     if(preg_match("/[^a-z0-9]/",$this->cn))     {
359       $message[]=_("Invalid character in application name. Only a-z 0-9 are allowed.");
360     }
362     if ($this->gosaApplicationExecute == ""){
363       $message[]= _("Required field 'Execute' is not filled.");
364     }
366     /* Check for existing application */
367     $ldap= $this->config->get_ldap_link();
368     $ldap->cd($this->config->current["BASE"]);
370     $tmp = search_config($this->config->data,"faiManagement","CLASS");
371     if((!empty($tmp)) && (isset($_SESSION['appfilter']['release']))){
372       $baseDn = str_replace($this->config->current['BASE'],$this->base,$_SESSION['appfilter']['release']);
373       $baseDn = preg_replace("/ou=apps,.*/","ou=apps,".$this->base,$_SESSION['appfilter']['release']);
374       $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))",$baseDn,array("cn"));
375       if($ldap->count()){
376         $attrs = $ldap->fetch();
377         if($this->dn != $attrs['dn']) {
378           $message[]= _("There's already an application with this 'Name'.");
379         }
380       }
381     }else{
382       $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))","ou=apps,".$this->base,array("cn"));
383       if ($ldap->count()){
384         $attrs = $ldap->fetch();
385         if($this->dn != $attrs['dn']) {
386           $message[]= _("There's already an application with this 'Name'.");
387         }
388       }
389     }
390     return $message;
391   }
394   /* Save to LDAP */
395   function save()
396   {
397     plugin::save();
398     $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
400     /* Write back to ldap */
401     $ldap= $this->config->get_ldap_link();
402     $ldap->cat($this->dn, array('dn'));
404     $a= $ldap->fetch();
405     if (count($a)){
406       $ldap->cd($this->dn);
407       $this->cleanup();
408       $ldap->modify ($this->attrs); 
409       $this->handle_post_events("modify");
410     } else {
411       $ldap->cd($this->config->current['BASE']);
412       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
413       $ldap->cd($this->dn);
414       $ldap->add($this->attrs);
415       $this->handle_post_events("add");
416     }
417     show_ldap_error($ldap->get_error(), sprintf(_("Saving of application with dn '%s' failed."),$this->dn));
418   }
420   function set_picture($filename)
421   {
422     if (!is_file($filename)){
423       $filename= "./images/default_icon.png";
424       $this->gosaApplicationIcon= "*removed*";
425     }
427     if (file_exists($filename)){
428       $fd = fopen ($filename, "rb");
429       $this->iconData= fread ($fd, filesize ($filename));
430       $_SESSION['binary']= $this->iconData;
431       $_SESSION['binarytype']= "image/jpeg";
432       $this->gosaApplicationIcon= $this->iconData;
434       fclose ($fd);
435     }
436   }
438   function getCopyDialog()
439   {
440     $vars = array("cn");
442     $str ="<h2>"._("Application settings")."</h2>
443       <table>
444       <tr>
445       <td>".
446       _("Application name"). 
447       "</td>  
448       <td>
449       <input id='gosaApplicationName' name='cn' size='35' maxlength='60' 
450       value='".$this->cn."' 
451       title='"._("Application name to be displayed (i.e. below icons)")."'>                     
452       </td>
453       </tr>
454       </table>";
455     $ret = array();
456     $ret['status'] = "";
457     $ret['string'] = $str;
458     return($ret);
459   }
461   function saveCopyDialog()
462   {
463     if(isset($_POST['cn'])){
464       $this->cn = $_POST['cn'];
465     }
466   }
468 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
469 ?>