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 $iconData;
19 /* Headpage attributes */
20 var $last_sorting= "invalid";
21 var $applications= array();
23 /* attribute list for save action */
24 var $attributes= array("cn", "description", "gosaApplicationExecute", "gosaApplicationName",
25 "gosaApplicationFlags");
26 var $objectclasses= array("top", "gosaApplication");
28 function application ($config, $dn= NULL)
29 {
30 plugin::plugin ($config, $dn);
32 /* Load icon */
33 $ldap= $config->get_ldap_link();
34 if ($dn != 'new'){
35 $this->iconData= $ldap->get_attribute($dn, "gosaApplicationIcon");
36 }
37 if ($this->iconData == ""){
38 $this->set_picture("");
39 }
40 $_SESSION['picture']= $this->iconData;
41 $this->gosaApplicationIcon= $this->iconData;
43 /* This is always an account */
44 $this->is_account= TRUE;
46 if ($this->dn == "new"){
47 $ui= get_userinfo();
48 $this->base= dn2base($ui->dn);
49 } else {
50 $this->base= preg_replace ("/^[^,]+,[^,]+,/", "", $this->dn);
51 }
52 }
54 function execute()
55 {
56 /* Do we represent a valid group? */
57 if (!$this->is_account && $this->parent == NULL){
58 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
59 _("This 'dn' is no application.")."</b>";
60 return ($display);
61 }
63 /* Fill templating stuff */
64 $smarty= get_smarty();
65 $smarty->assign("cn", $this->cn);
66 $smarty->assign("bases", $this->config->idepartments);
67 if ($this->dn == "new"){
68 $smarty->assign("selectmode", "");
69 $smarty->assign("namemode", "");
70 } else {
71 $smarty->assign("namemode", "readonly");
72 $smarty->assign("selectmode", "disabled");
73 }
75 /* Get random number for pictures */
76 srand((double)microtime()*1000000);
77 $smarty->assign("rand", rand(0, 10000));
79 /* Variables */
80 foreach(array("description", "gosaApplicationExecute", "gosaApplicationName") as $val){
81 $smarty->assign($val, $this->$val);
82 $smarty->assign($val."ACL", chkacl($this->acl, $val));
83 }
85 /* Checkboxes */
86 foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
87 "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
88 if (preg_match("/$key/", $this->gosaApplicationFlags)){
89 $smarty->assign("$val", "checked");
90 } else {
91 $smarty->assign("$val", "");
92 }
93 }
94 $smarty->assign("base_select", $this->base);
95 $smarty->assign("gosaApplicationFlagsACL", chkacl($this->acl, "gosaApplicationFlags"));
97 /* Show main page */
98 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
99 }
101 function remove_from_parent()
102 {
103 $ldap= $this->config->get_ldap_link();
104 $ldap->rmDir($this->dn);
106 /* Optionally execute a command after we're done */
107 $this->handle_post_events("remove");
109 /* Delete references to object groups */
110 $ldap->cd ($this->config->current['BASE']);
111 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
112 while ($ldap->fetch()){
113 $og= new ogroup($this->config, $ldap->getDN());
114 unset($og->member[$this->dn]);
115 $og->save ();
116 }
118 }
121 /* Save data to object */
122 function save_object()
123 {
124 if (isset($_POST['cn'])){
125 plugin::save_object();
127 /* Save application flags */
128 $flag= "";
129 if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
130 $flag.= "G";
131 }
132 if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
133 $flag.= "D";
134 }
135 if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
136 $flag.= "M";
137 }
138 if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
139 $flag.= "O";
140 }
141 if (chkacl ($this->acl, "gosaApplicationFlags") ==""){
142 $this->gosaApplicationFlags= "[$flag]";
143 }
145 /* Check for picture upload */
146 if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
147 if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
148 print_red (_("The specified picture has not been uploaded correctly."));
149 }
151 if (!function_exists("imagick_blob2image")){
152 /* Get temporary file name for conversation */
153 $fname = tempnam ("/tmp", "GOsa");
155 /* Open file and write out photoData */
156 $fp = fopen ($fname, "w");
157 fwrite ($fp, $_FILES['picture_file']['tmp_name']);
158 fclose ($fp);
160 /* Build conversation query. Filename is generated automatically, so
161 we do not need any special security checks. Exec command and save
162 output. For PHP safe mode, you'll need a configuration which respects
163 image magick as executable... */
164 $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
165 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
167 /* Read data written by convert */
168 $output= "";
169 $sh= popen($query, 'r');
170 while (!feof($sh)){
171 $output.= fread($sh, 4096);
172 }
173 pclose($sh);
175 unlink($fname);
176 } else {
178 /* Load the new uploaded Photo */
179 if(!$handle = imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
180 gosa_log("Can't Load image");
181 }
183 /* Resizing image to 147x200 and blur */
184 if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
185 gosa_log("imagick_resize failed");
186 }
188 /* Converting image to JPEG */
189 if(!imagick_convert($handle,"PNG")) {
190 gosa_log("Can't Convert to PNG");
191 }
193 if(imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
194 gosa_log("can't write to specified folder");
195 }
197 imagick_free($handle);
198 }
200 /* Activate new picture */
201 $this->set_picture($_FILES['picture_file']['tmp_name']);
202 }
205 /* Save base, since this is no LDAP attribute */
206 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
207 $this->base= $_POST['base'];
208 }
209 }
210 }
213 /* Check values */
214 function check()
215 {
216 $message= array();
218 /* Permissions for that base? */
219 if ($this->base != ""){
220 $new_dn= "cn=".$this->cn.",ou=apps,".$this->base;
221 } else {
222 $new_dn= $this->dn;
223 }
225 $ui= get_userinfo();
226 $acl= get_permissions ($new_dn, $ui->subtreeACL);
227 $acl= get_module_permission($acl, "application", $new_dn);
228 if (chkacl($acl, "create") != ""){
229 $message[]= _("You have no permissions to create a application on this 'Base'.");
230 }
232 /* All required fields are set? */
233 if ($this->cn == ""){
234 $message[]= _("Required field 'Name' is not filled.");
235 }
236 if ($this->gosaApplicationExecute == ""){
237 $message[]= _("Required field 'Execute' is not filled.");
238 }
240 /* Check for existing application */
241 $ldap= $this->config->get_ldap_link();
242 $ldap->cd($this->config->current["BASE"]);
243 $ldap->search("(&(objectClass=gosaApplication)(cn=$this->cn))");
244 $ldap->fetch();
245 if ($ldap->count() != 0 && $this->dn == "new"){
246 $message[]= _("There's already an application with this 'Name'.");
247 }
249 return $message;
250 }
253 /* Save to LDAP */
254 function save()
255 {
256 plugin::save();
257 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
259 /* Write back to ldap */
260 $ldap= $this->config->get_ldap_link();
261 $ldap->cat($this->dn);
262 $a= $ldap->fetch();
263 if (count($a)){
264 $ldap->cd($this->dn);
265 $ldap->modify($this->attrs);
266 $this->handle_post_events("modify");
267 } else {
268 $ldap->cd($this->config->current['BASE']);
269 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
270 $ldap->cd($this->dn);
271 $ldap->add($this->attrs);
272 $this->handle_post_events("add");
273 }
274 show_ldap_error($ldap->get_error());
275 }
277 function set_picture($filename)
278 {
279 if (!is_file($filename)){
280 $filename= "./images/default_icon.png";
281 $this->gosaApplicationIcon= "*removed*";
282 }
284 if (file_exists($filename)){
285 $fd = fopen ($filename, "rb");
286 $this->iconData= fread ($fd, filesize ($filename));
287 $_SESSION['picture']= $this->iconData;
288 $this->gosaApplicationIcon= $this->iconData;
290 fclose ($fd);
291 }
292 }
295 }
297 ?>