3394cc566927478f8cb511d7f67e99b3be0423f5
1 <?php
3 class printgeneric extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary= "Manage terminal base objects";
7 var $cli_description= "Some longer text\nfor help";
8 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* Generic terminal attributes */
11 var $interfaces= array();
12 var $ignore_account= FALSE;
14 /* Needed values and lists */
15 var $base = "";
16 var $cn = "";
17 var $macAddress = "";
18 var $ipHostNumber = "";
19 var $l = "";
20 var $description = "";
21 var $labeledURI = "";
22 var $gotoPrinterPPD = "";
23 var $orig_dn = "";
24 var $is_terminal = false;
26 var $UserMember ="";
27 var $UserMembers =array();
28 var $UserMemberKeys =array();
30 var $AdminMember ="";
31 var $AdminMembers =array();
32 var $AdminMemberKeys =array();
34 var $member =array();
36 var $dialog =NULL;
38 /* attribute list for save action */
39 var $attributes = array("cn", "description", "l", "labeledURI", "gotoPrinterPPD","macAddress", "ipHostNumber");
40 var $objectclasses = array("top", "gotoPrinter");
42 function printgeneric ($config, $dn= NULL)
43 {
44 plugin::plugin ($config, $dn);
45 $ldap= $this->config->get_ldap_link();
47 /* Set base */
48 if ($this->dn == "new"){
49 $ui= get_userinfo();
50 $this->base= dn2base($ui->dn);
51 $this->cn= "";
52 } else {
53 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
54 }
57 if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
58 $this->is_terminal = "true";
59 $this->dn = preg_replace("/ou=workstation/","ou=printer",$this->dn);
61 // Reload plugin with new dn... (ou=printers instead of ou=terminals)
62 plugin::plugin ($this->config, $this->dn);
63 $ldap->cat($this->dn);
64 if(count($ldap->fetch())>0){
65 $this->orig_dn= $this->dn;
66 $this->is_account=true;
67 $this->initially_was_account = true;
68 }else{
69 $this->orig_dn = "new";
70 $this->is_account=false;
71 $this->initially_was_account = false;
72 }
73 }elseif((in_array("gotoTerminal",$this->attrs['objectClass']))){
74 $this->is_terminal = "true";
75 $this->dn = preg_replace("/ou=terminal/","ou=printer",$this->dn);
77 // Reload plugin with new dn... (ou=printers instead of ou=terminals)
78 plugin::plugin ($this->config, $this->dn);
79 $ldap->cat($this->dn);
80 if(count($ldap->fetch())>0){
81 $this->orig_dn= $this->dn;
82 $this->is_account=true;
83 $this->initially_was_account = true;
84 }else{
85 $this->orig_dn = "new";
86 $this->is_account=false;
87 $this->initially_was_account = false;
88 }
90 }else{
92 /* Save dn for later references */
93 $this->orig_dn= $this->dn;
94 }
96 foreach(array("AddUser" =>"gotoUserPrinter",
97 "AddGroup" =>"gotoGroupPrinter",
98 "AddAdminUser" =>"gotoUserAdminPrinter",
99 "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
101 $this->member[$type]=array();
102 unset($this->attrs[$attr]['count']);
103 if(isset($this->attrs[$attr]))
104 foreach($this->attrs[$attr] as $mem){
105 if(preg_match("/Group/",$type)){
106 $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
107 $entry = $ldap->fetch();
108 if(isset($entry['description'])){
109 $this->member[$type][$entry['cn'][0]]=$entry;
110 }
111 }else{
112 $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
113 $entry = $ldap->fetch();
114 if(isset($entry['uid'])){
115 $this->member[$type][$entry['uid'][0]]=$entry;
116 }
117 }
118 }
119 }
121 }
123 function execute()
124 {
125 print_a($this);
126 $smarty= get_smarty();
127 $display="";
128 /* Template management.
129 * There are two ways to call this tab.
130 * 1. From a printer Dialog, here we will see the full template, without a toggle state button
131 * 2. As a terminal tab, here we hide the top (name,base,description) of the template.
132 * Toggle Account state will be shown in this case, to disable or enable this tab.
133 *
134 * $this->is_terminal indecates this two different types.
135 */
136 if($this->is_terminal){
137 $smarty->assign("is_terminal","true");
138 }else{
139 $smarty->assign("is_terminal","false");
140 }
142 /* Do we need to flip is_account state? */
143 if (isset($_POST['modify_state'])){
144 $this->is_modified = true;
145 $this->is_account= !$this->is_account;
146 }
148 if($this->is_terminal){
149 if ($this->is_account){
150 $display= $this->show_header(_("Remove environment extension"),
151 _("This server has environment extension enabled. You can disable it by clicking below."));
152 }else{
153 $display= $this->show_header(_("Add environment extension"),
154 _("This server has environment extension disabled. You can enable it by clicking below."));
155 return ($display);
156 }
157 }
160 /* Do we represent a valid printer? */
161 if (!$this->is_account && $this->parent == NULL){
162 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
163 _("This 'dn' has no printer features.")."</b>";
164 return($display);
165 }
167 /* Fill templating stuff */
168 $smarty->assign("bases", $this->config->idepartments);
169 $smarty->assign("base_select", $this->base);
171 /* Assign attributes */
172 foreach ($this->attributes as $attr){
173 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
174 $smarty->assign("$attr", $this->$attr);
175 }
177 if(isset($_POST['AddUser'])){
178 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
179 }
180 if(isset($_POST['AddGroup'])){
181 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
182 }
183 if(isset($_POST['AddAdminUser'])){
184 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
185 }
186 if(isset($_POST['AddAdminGroup'])){
187 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
188 }
190 if(isset($_POST['PrinterCancel'])){
191 unset($this->dialog);
192 $this->dialog= NULL;
193 }
195 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
196 if(isset($this->member['AddUser'][$_POST['UserMember']])){
197 unset($this->member['AddUser'][$_POST['UserMember']]);
198 }
199 }
201 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
202 if(isset($this->member['AddGroup'][$_POST['UserMember']])){
203 unset($this->member['AddGroup'][$_POST['UserMember']]);
204 }
205 }
207 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
208 if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
209 unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
210 }
211 }
213 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
214 if(isset($this->member['AddAdmingroup'][$_POST['AdminMember']])){
215 unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
216 }
217 }
219 if(isset($_POST['PrinterSave'])){
220 $this->dialog->save_object();
221 if(count($this->dialog->check())){
222 foreach($this->dialog->check() as $msg){
223 print_red($msg);
224 }
225 }else{
226 $new = $this->dialog->save();
227 $data = $new;
228 unset($data['type']);
230 if(preg_match("/User/",$new['type'])){
231 $use = "uid";
232 }else{
233 $use = "cn";
234 }
236 foreach($data as $mem){
237 $this->member[$new['type']][$mem[$use][0]]=$mem;
238 }
239 unset($this->dialog);
240 $this->dialog=NULL;
241 }
242 }
244 if($this->dialog != NULL){
245 $display = $this->dialog->execute();
246 return $display;
247 }
249 /* Don't show Asterisk for non-required attribute ipHostNumber and macAddress */
250 $smarty->assign("staticAddress", "");
251 $smarty->assign("driverInfo", "Not implemented yet");
253 $list=$this->generateList();
254 $userlist = array_merge($list['AddUser'],$list['AddGroup']);
255 $adminlist = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
257 $smarty->assign("UserMember" ,$this->UserMember);
258 $smarty->assign("UserMembers" ,$userlist);
259 $smarty->assign("UserMemberKeys",array_flip($userlist));
261 $smarty->assign("AdminMember" ,$this->AdminMember);
262 $smarty->assign("AdminMembers" ,$adminlist);
263 $smarty->assign("AdminMemberKeys",array_flip($adminlist));
264 $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
267 return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
268 }
270 function remove_from_parent()
271 {
272 $ldap= $this->config->get_ldap_link();
273 $ldap->rmdir($this->dn);
274 show_ldap_error($ldap->get_error());
275 $this->handle_post_events("remove");
277 /* Delete references to object groups */
278 $ldap->cd ($this->config->current['BASE']);
279 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
280 while ($ldap->fetch()){
281 $og= new ogroup($this->config, $ldap->getDN());
282 unset($og->member[$this->dn]);
283 $og->save ();
284 }
285 }
287 /* Save data to object */
288 function save_object()
289 {
290 plugin::save_object();
292 /* Save base, since this is no LDAP attribute */
293 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
294 $this->base= $_POST['base'];
295 }
296 }
298 /* Check supplied data */
299 function check()
300 {
301 $message= array();
302 $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
304 /* must: cn */
305 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
306 $message[]= "The required field 'Printer name' is not set.";
307 }
309 $ui= get_userinfo();
310 $acl= get_permissions ($this->dn, $ui->subtreeACL);
311 $acl= get_module_permission($acl, "printer", $this->dn);
312 if (chkacl($acl, "create") != ""){
313 $message[]= _("You have no permissions to create a printer on this 'Base'.");
314 }
316 if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
317 $ldap= $this->config->get_ldap_link();
318 $ldap->cd ($this->base);
319 $ldap->search ("(cn=".$this->cn.")", array("cn"));
320 if ($ldap->count() != 0){
321 while ($attrs= $ldap->fetch()){
322 if ($attrs['dn'] != $this->orig_dn){
323 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
324 break;
325 }
326 }
327 }
328 }
330 return ($message);
331 }
334 /* Save to LDAP */
335 function save()
336 {
337 $dn= $this->dn;
338 plugin::save();
340 if((in_array("gotoTerminal",$this->attrs['objectClass']))){
341 $this->dn= preg_replace("/ou=terminal/","ou=printer",$this->dn);
342 }
344 if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
345 $this->dn= preg_replace("/ou=workstation/","ou=printer",$this->dn);
346 }
348 $ldap= $this->config->get_ldap_link();
350 /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn
351 */
352 if($this->is_terminal){
354 /* reduce objectClasses to minimun */
355 $this->attrs['objectClass']= $this->objectclasses;
357 /* If a printer with the given dn exists, modify else create new one */
358 $ldap->cat($this->dn);
359 if($ldap->fetch()){
360 $this->orig_dn= $this->dn;
361 }else{
362 $this->orig_dn = "new";
363 }
364 }
366 /* Remove all empty values */
367 if ($this->orig_dn == 'new'){
368 $attrs= array();
369 foreach ($this->attrs as $key => $val){
370 if (is_array($val) && count($val) == 0){
371 continue;
372 }
373 $attrs[$key]= $val;
374 }
375 $this->attrs= $attrs;
376 }
379 /* Append printer user
380 */
381 foreach($this->member['AddUser'] as $mem){
382 $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
383 }
385 /* Append printer group
386 */
387 foreach($this->member['AddGroup'] as $mem){
388 $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
389 }
391 /* Append printer admin user
392 */
393 foreach($this->member['AddAdminUser'] as $mem){
394 $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
395 }
397 /* Append printer admin group
398 */
399 foreach($this->member['AddAdminGroup'] as $mem){
400 $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
401 }
403 /* Write back to ldap */
404 $ldap= $this->config->get_ldap_link();
405 if ($this->orig_dn == 'new'){
406 $ldap->cd($this->config->current['BASE']);
407 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
408 $ldap->cd($this->dn);
409 $ldap->add($this->attrs);
410 $this->handle_post_events("add");
411 } else {
412 if ($this->orig_dn != $this->dn){
413 $this->move($this->orig_dn, $this->dn);
414 }
416 $ldap->cd($this->dn);
417 $ldap->modify($this->attrs);
418 $this->handle_post_events("modify");
419 }
420 show_ldap_error($ldap->get_error());
422 /* Optionally execute a command after we're done */
423 $this->postcreate();
424 }
426 function generateList(){
427 $a_return=array();
428 foreach($this->member as $type => $values){
429 $a_return[$type]=array();
430 foreach($values as $value){
431 if((preg_match("/Group/i",$type))){
432 if(!isset($value['description'])){
433 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
434 }else{
435 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
436 }
437 }else{
438 $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
439 }
440 }
441 }
442 return($a_return);
443 }
446 }
450 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
451 ?>