ad4d3f163d12a2ab4f854b0e4da17aa328e1f2bf
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",
26 "gosaApplicationFlags","gotoLogonScript");
27 var $objectclasses= array("top", "gosaApplication");
29 function application ($config, $dn= NULL)
30 {
31 plugin::plugin ($config, $dn);
33 /* Load icon */
34 $ldap= $config->get_ldap_link();
35 if ($dn != 'new'){
36 $this->iconData= $ldap->get_attribute($dn, "gosaApplicationIcon");
37 }
38 if ($this->iconData == ""){
39 $this->set_picture("");
40 }
41 $_SESSION['binary']= $this->iconData;
42 $_SESSION['binarytype']= "image/jpeg";
43 $this->gosaApplicationIcon= $this->iconData;
45 /* This is always an account */
46 $this->is_account= TRUE;
48 if ($this->dn == "new"){
49 if(isset($_SESSION['appfilter']['depselect'])){
50 $this->base=$_SESSION['appfilter']['depselect'];
51 }else{
52 $ui= get_userinfo();
53 $this->base= dn2base($ui->dn);
54 }
55 } else {
56 $this->base= preg_replace ("/^[^,]+,[^,]+,/", "", $this->dn);
57 }
58 }
61 function generateTempalte(){
62 $str= "# This code is part of GOsa (https://gosa.gonicus.de)\n#\n";
64 $names = $this->parent->by_object['applicationParameters']->option_name;
65 $values = $this->parent->by_object['applicationParameters']->option_value;
67 if (count($names)){
68 $str .="# This plugin handles these environment variables:\n";
69 } else {
70 $str .="# This plugin handles no environment variables.\n";
71 }
73 foreach($names as $index => $name){
75 // Fix length
76 for($i = strlen($name) ; $i < 30 ; $i++){
77 $name= $name." ";
78 }
79 if((isset($values[$index]))&&(!empty($values[$index]))){
80 $str.= "# ".$name."\t(e.g. '".$values[$index]."')\n";
81 }else{
82 $str.= "# ".$name."\t("._("no example").")\n";
83 }
84 }
85 $str .= "#\n".
86 "# Don't remove the following tag, it is used for header update.\n".
87 "### END HEADER ###";
89 return($str);
90 }
92 function execute()
93 {
94 /* Do we represent a valid group? */
95 if (!$this->is_account && $this->parent == NULL){
96 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
97 _("This 'dn' is no application.")."</b>";
98 return ($display);
99 }
101 # if(isset($_POST['ScriptTemplate'])){
102 $head = $this->generateTempalte();
103 $scr = $this->gotoLogonScript;
105 if(preg_match("/### END HEADER ###/",$scr)){
106 $pos = strrpos($scr,"### END HEADER ###");
107 $pos = $pos + strlen("### END HEADER ###");
108 $scr=substr($scr,$pos,(strlen($scr)));
109 }
111 $this->gotoLogonScript = $head.$scr;
112 # }
114 if((isset($_POST['upLoad']))&&(isset($_FILES['ScriptFile']))){
115 $str = file_get_contents($_FILES['ScriptFile']['tmp_name']);
116 $this->gotoLogonScript = $str;
117 }
119 /* Fill templating stuff */
120 $smarty= get_smarty();
121 $smarty->assign("cn", $this->cn);
122 $smarty->assign("bases", $this->config->idepartments);
123 if ($this->dn == "new"){
124 $smarty->assign("selectmode", "");
125 $smarty->assign("namemode", "");
126 } else {
127 $smarty->assign("namemode", "readonly");
128 $smarty->assign("selectmode", "disabled");
129 }
131 /* Get random number for pictures */
132 srand((double)microtime()*1000000);
133 $smarty->assign("rand", rand(0, 10000));
135 /* Variables */
136 foreach(array("description", "gosaApplicationExecute", "gosaApplicationName") as $val){
137 $smarty->assign($val, $this->$val);
138 $smarty->assign($val."ACL", chkacl($this->acl, $val));
139 }
141 /* Checkboxes */
142 foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
143 "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
144 if (preg_match("/$key/", $this->gosaApplicationFlags)){
145 $smarty->assign("$val", "checked");
146 } else {
147 $smarty->assign("$val", "");
148 }
149 }
150 $smarty->assign("gotoLogonScript",$this->gotoLogonScript);
151 $smarty->assign("base_select", $this->base);
152 $smarty->assign("gosaApplicationFlagsACL", chkacl($this->acl, "gosaApplicationFlags"));
154 /* Show main page */
155 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
156 }
159 function remove_from_parent()
160 {
161 $ldap= $this->config->get_ldap_link();
162 $ldap->rmDir($this->dn);
164 /* Optionally execute a command after we're done */
165 $this->handle_post_events("remove");
167 /* Delete references to object groups */
168 $ldap->cd ($this->config->current['BASE']);
169 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
170 while ($ldap->fetch()){
171 $og= new ogroup($this->config, $ldap->getDN());
172 unset($og->member[$this->dn]);
173 $og->save ();
174 }
175 $ldap->search ("(&(objectClass=posixGroup)(gosaMemberApplication=".$this->cn."))", array("cn"));
176 while ($attrs= $ldap->fetch()){
177 $ag= new appgroup($this->config, $ldap->getDN());
178 $ag->removeApp($this->cn);
179 $ag->save ();
180 }
182 }
185 /* Save data to object */
186 function save_object()
187 {
188 if (isset($_POST['cn'])){
190 plugin::save_object();
192 /* Save application flags */
193 $flag= "";
194 if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
195 $flag.= "G";
196 }
197 if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
198 $flag.= "D";
199 }
200 if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
201 $flag.= "M";
202 }
203 if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
204 $flag.= "O";
205 }
206 if (chkacl ($this->acl, "gosaApplicationFlags") ==""){
207 $this->gosaApplicationFlags= "[$flag]";
208 }
210 /* Check for picture upload */
211 if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
212 if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
213 print_red (_("The specified picture has not been uploaded correctly."));
214 }
216 if (!function_exists("imagick_blob2image")){
217 /* Get temporary file name for conversation */
218 $fname = tempnam ("/tmp", "GOsa");
220 /* Open file and write out photoData */
221 $fp = fopen ($fname, "w");
222 fwrite ($fp, $_FILES['picture_file']['tmp_name']);
223 fclose ($fp);
225 /* Build conversation query. Filename is generated automatically, so
226 we do not need any special security checks. Exec command and save
227 output. For PHP safe mode, you'll need a configuration which respects
228 image magick as executable... */
229 $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
230 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
232 /* Read data written by convert */
233 $output= "";
234 $sh= popen($query, 'r');
235 while (!feof($sh)){
236 $output.= fread($sh, 4096);
237 }
238 pclose($sh);
240 unlink($fname);
241 } else {
243 /* Load the new uploaded Photo */
244 if(!$handle = imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
245 gosa_log("Can't Load image");
246 }
248 /* Resizing image to 147x200 and blur */
249 if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
250 gosa_log("imagick_resize failed");
251 }
253 /* Converting image to JPEG */
254 if(!imagick_convert($handle,"PNG")) {
255 gosa_log("Can't Convert to PNG");
256 }
258 if(imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
259 gosa_log("can't write to specified folder");
260 }
262 imagick_free($handle);
263 }
265 /* Activate new picture */
266 $this->set_picture($_FILES['picture_file']['tmp_name']);
267 }
270 /* Save base, since this is no LDAP attribute */
271 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
272 $this->base= $_POST['base'];
273 }
274 }
275 }
278 /* Check values */
279 function check()
280 {
281 $message= array();
283 /* Permissions for that base? */
284 if ($this->base != ""){
285 $new_dn= "cn=".$this->cn.",ou=apps,".$this->base;
286 } else {
287 $new_dn= $this->dn;
288 }
290 $ui= get_userinfo();
291 $acl= get_permissions ($new_dn, $ui->subtreeACL);
292 $acl= get_module_permission($acl, "application", $new_dn);
293 if (chkacl($acl, "create") != ""){
294 $message[]= _("You have no permissions to create a application on this 'Base'.");
295 }
297 /* All required fields are set? */
298 if ($this->cn == ""){
299 $message[]= _("Required field 'Name' is not filled.");
300 }
302 if(preg_match("/[^a-z0-9]/",$this->cn)) {
303 $message[]=_("Invalid character in application name. Only a-z 0-9 are allowed.");
304 }
306 if ($this->gosaApplicationExecute == ""){
307 $message[]= _("Required field 'Execute' is not filled.");
308 }
310 /* Check for existing application */
311 $ldap= $this->config->get_ldap_link();
312 $ldap->cd($this->config->current["BASE"]);
313 $ldap->search("(&(objectClass=gosaApplication)(cn=$this->cn))");
314 $ldap->fetch();
315 if ($ldap->count() != 0 && $this->dn == "new"){
316 $message[]= _("There's already an application with this 'Name'.");
317 }
319 return $message;
320 }
323 /* Save to LDAP */
324 function save()
325 {
326 plugin::save();
327 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
329 /* Write back to ldap */
330 $ldap= $this->config->get_ldap_link();
331 $ldap->cat($this->dn);
332 $a= $ldap->fetch();
333 if (count($a)){
334 $ldap->cd($this->dn);
335 $ldap->modify($this->attrs);
336 $this->handle_post_events("modify");
337 } else {
338 $ldap->cd($this->config->current['BASE']);
339 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
340 $ldap->cd($this->dn);
341 $ldap->add($this->attrs);
342 $this->handle_post_events("add");
343 }
344 show_ldap_error($ldap->get_error());
345 }
347 function set_picture($filename)
348 {
349 if (!is_file($filename)){
350 $filename= "./images/default_icon.png";
351 $this->gosaApplicationIcon= "*removed*";
352 }
354 if (file_exists($filename)){
355 $fd = fopen ($filename, "rb");
356 $this->iconData= fread ($fd, filesize ($filename));
357 $_SESSION['binary']= $this->iconData;
358 $_SESSION['binarytype']= "image/jpeg";
359 $this->gosaApplicationIcon= $this->iconData;
361 fclose ($fd);
362 }
363 }
366 }
368 ?>