211cec67fd5b8e602d20e2d1d09d497023dc7a20
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();
35 var $strings = "";
36 var $type = "";
37 var $dialog =NULL;
39 /* attribute list for save action */
40 var $attributes = array("cn", "description", "l", "labeledURI", "gotoPrinterPPD","macAddress", "ipHostNumber");
41 var $objectclasses = array("top", "gotoPrinter");
43 function printgeneric ($config, $dn= NULL)
44 {
45 plugin::plugin ($config, $dn);
46 $ldap= $this->config->get_ldap_link();
48 /* Set base */
49 if ($this->dn == "new"){
50 $ui= get_userinfo();
51 $this->base= dn2base($ui->dn);
52 $this->cn= "";
53 } else {
54 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
55 }
58 /* In case of gotoWorkstation this tab is calles from workstation plugin
59 * in case of gotoTerminal it is called from a terminal tab
60 * else it is a standalone printer
61 */
62 if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
63 $this->is_terminal = "true";
64 $this->dn = preg_replace("/ou=workstation/","ou=printer",$this->dn);
65 $this->type = "station";
66 }elseif((in_array("gotoTerminal",$this->attrs['objectClass']))){
67 $this->type = "terminal";
68 $this->is_terminal = "true";
69 $this->dn = preg_replace("/ou=terminal/","ou=printer",$this->dn);
70 }else{
71 /* Save dn for later references */
72 $this->orig_dn= $this->dn;
73 }
75 /* If it is no standalone printer
76 */
77 if($this->is_terminal){
78 // Reload plugin with new dn... (ou=printers instead of ou=terminals)
79 plugin::plugin ($this->config, $this->dn);
80 $ldap->cat($this->dn);
81 if(count($ldap->fetch())>0){
82 $this->orig_dn= $this->dn;
83 $this->is_account=true;
84 $this->initially_was_account = true;
85 }else{
86 $this->orig_dn = "new";
87 $this->is_account=false;
88 $this->initially_was_account = false;
89 }
90 }
93 /* Prepare different member types
94 */
95 foreach(array("AddUser" =>"gotoUserPrinter",
96 "AddGroup" =>"gotoGroupPrinter",
97 "AddAdminUser" =>"gotoUserAdminPrinter",
98 "AddAdminGroup" =>"gotoGroupAdminPrinter") as $type => $attr){
100 /* $this->members contains all members */
101 $this->member[$type]=array();
103 unset($this->attrs[$attr]['count']);
105 if(isset($this->attrs[$attr])){
106 foreach($this->attrs[$attr] as $mem){
107 if(preg_match("/Group/",$type)){
108 $ldap->search("(&(objectClass=posixGroup)(cn=".$mem."))",array("cn","description"));
109 $entry = $ldap->fetch();
110 if(isset($entry['description'])){
111 $this->member[$type][$entry['cn'][0]]=$entry;
112 }
113 }else{
114 $ldap->search("(&(objectClass=person)(objectClass=inetOrgPerson)(uid=".$mem."))",array("cn","uid"));
115 $entry = $ldap->fetch();
116 if(isset($entry['uid'])){
117 $this->member[$type][$entry['uid'][0]]=$entry;
118 }
119 }
120 }
121 }
122 }
123 }
125 function execute()
126 {
127 $smarty= get_smarty();
128 $display="";
129 /* Template management.
130 * There are two ways to call this tab.
131 * 1. From a printer Dialog, here we will see the full template, without a toggle state button
132 * 2. As a terminal tab, here we hide the top (name,base,description) of the template.
133 * Toggle Account state will be shown in this case, to disable or enable this tab.
134 *
135 * $this->is_terminal indecates this two different types.
136 */
137 if($this->is_terminal){
138 $smarty->assign("is_terminal","true");
139 }else{
140 $smarty->assign("is_terminal","false");
141 }
143 /* Do we need to flip is_account state? */
144 if (isset($_POST['modify_state'])){
145 $this->is_modified = true;
146 $this->is_account= !$this->is_account;
147 }
149 if($this->is_terminal){
150 if ($this->is_account){
151 if($this->type=="station"){
152 $display= $this->show_header(_("Remove printer extension"),
153 _("This workstation has printer extension enabled. You can disable it by clicking below."));
154 }else{
155 $display= $this->show_header(_("Remove printer extension"),
156 _("This terminal has printer extension enabled. You can disable it by clicking below."));
157 }
158 }else{
159 if($this->type=="station"){
160 $display= $this->show_header(_("Add printer extension"),
161 _("This workstation has printer extension disabled. You can enable it by clicking below."));
162 }else{
163 $display= $this->show_header(_("Add printer extension"),
164 _("This terminal has printer extension disabled. You can enable it by clicking below."));
165 }
166 return ($display);
167 }
168 }
171 /* Do we represent a valid printer? */
172 if (!$this->is_account && $this->parent == NULL){
173 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
174 _("This 'dn' has no printer features.")."</b>";
175 return($display);
176 }
178 /* Fill templating stuff */
179 $smarty->assign("bases", $this->config->idepartments);
180 $smarty->assign("base_select", $this->base);
182 /* Assign attributes */
183 foreach ($this->attributes as $attr){
184 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
185 $smarty->assign("$attr", $this->$attr);
186 }
188 if(isset($_POST['AddUser'])){
189 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddUser");
190 }
191 if(isset($_POST['AddGroup'])){
192 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddGroup");
193 }
194 if(isset($_POST['AddAdminUser'])){
195 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminUser");
196 }
197 if(isset($_POST['AddAdminGroup'])){
198 $this->dialog = new selectUserToPrinterDialog($this->config, get_userinfo(),"AddAdminGroup");
199 }
201 if(isset($_POST['PrinterCancel'])){
202 unset($this->dialog);
203 $this->dialog= NULL;
204 }
206 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
207 if(isset($this->member['AddUser'][$_POST['UserMember']])){
208 unset($this->member['AddUser'][$_POST['UserMember']]);
209 }
210 }
212 if((isset($_POST['DelUser']))&&(isset($_POST['UserMember']))){
213 if(isset($this->member['AddGroup'][$_POST['UserMember']])){
214 unset($this->member['AddGroup'][$_POST['UserMember']]);
215 }
216 }
218 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
219 if(isset($this->member['AddAdminUser'][$_POST['AdminMember']])){
220 unset($this->member['AddAdminUser'][$_POST['AdminMember']]);
221 }
222 }
224 if((isset($_POST['DelAdmin']))&&(isset($_POST['AdminMember']))){
225 if(isset($this->member['AddAdmingroup'][$_POST['AdminMember']])){
226 unset($this->member['AddAdminGroup'][$_POST['AdminMember']]);
227 }
228 }
230 if(isset($_POST['PrinterSave'])){
231 $this->dialog->save_object();
232 if(count($this->dialog->check())){
233 foreach($this->dialog->check() as $msg){
234 print_red($msg);
235 }
236 }else{
237 $new = $this->dialog->save();
238 $data = $new;
239 unset($data['type']);
241 if(preg_match("/User/",$new['type'])){
242 $use = "uid";
243 }else{
244 $use = "cn";
245 }
247 foreach($data as $mem){
248 $this->member[$new['type']][$mem[$use][0]]=$mem;
249 }
250 unset($this->dialog);
251 $this->dialog=NULL;
252 }
253 }
255 if($this->dialog != NULL){
256 $display = $this->dialog->execute();
257 return $display;
258 }
260 /* Don't show Asterisk for non-required attribute ipHostNumber and macAddress */
261 $smarty->assign("staticAddress", "");
262 $smarty->assign("driverInfo", "Not implemented yet");
264 $list=$this->generateList();
265 $userlist = array_merge($list['AddUser'],$list['AddGroup']);
266 $adminlist = array_merge($list['AddAdminUser'],$list['AddAdminGroup']);
268 asort($userlist);
269 asort($adminlist);
271 $smarty->assign("UserMember" ,$this->UserMember);
272 $smarty->assign("UserMembers" ,$userlist);
273 $smarty->assign("UserMemberKeys",array_flip($userlist));
275 $smarty->assign("AdminMember" ,$this->AdminMember);
276 $smarty->assign("AdminMembers" ,$adminlist);
277 $smarty->assign("AdminMemberKeys",array_flip($adminlist));
278 $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
280 return($display.$smarty->fetch (get_template_path('printer.tpl', TRUE)));
281 }
283 function remove_from_parent()
284 {
285 $ldap= $this->config->get_ldap_link();
286 $ldap->rmdir($this->dn);
287 show_ldap_error($ldap->get_error());
288 $this->handle_post_events("remove");
290 /* Delete references to object groups */
291 $ldap->cd ($this->config->current['BASE']);
292 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
293 while ($ldap->fetch()){
294 $og= new ogroup($this->config, $ldap->getDN());
295 unset($og->member[$this->dn]);
296 $og->save ();
297 }
298 }
300 /* Save data to object */
301 function save_object()
302 {
303 plugin::save_object();
305 /* Save base, since this is no LDAP attribute */
306 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
307 $this->base= $_POST['base'];
308 }
309 }
311 /* Check supplied data */
312 function check()
313 {
314 $message= array();
315 $this->dn= "cn=".$this->cn.",ou=printers,ou=systems,".$this->base;
317 /* must: cn */
318 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
319 $message[]= "The required field 'Printer name' is not set.";
320 }
322 $ui= get_userinfo();
323 $acl= get_permissions ($this->dn, $ui->subtreeACL);
324 $acl= get_module_permission($acl, "printer", $this->dn);
325 if (chkacl($acl, "create") != ""){
326 $message[]= _("You have no permissions to create a printer on this 'Base'.");
327 }
329 if (($this->orig_dn != $this->dn)&&(!$this->is_terminal)){
330 $ldap= $this->config->get_ldap_link();
331 $ldap->cd ($this->base);
332 $ldap->search ("(cn=".$this->cn.")", array("cn"));
333 if ($ldap->count() != 0){
334 while ($attrs= $ldap->fetch()){
335 if ($attrs['dn'] != $this->orig_dn){
336 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
337 break;
338 }
339 }
340 }
341 }
343 return ($message);
344 }
347 /* Save to LDAP */
348 function save()
349 {
350 $dn= $this->dn;
351 plugin::save();
353 if((in_array("gotoTerminal",$this->attrs['objectClass']))){
354 $this->dn= preg_replace("/ou=terminal/","ou=printer",$this->dn);
355 }
357 if((in_array("gotoWorkstation",$this->attrs['objectClass']))){
358 $this->dn= preg_replace("/ou=workstation/","ou=printer",$this->dn);
359 }
361 $ldap= $this->config->get_ldap_link();
363 /* We are currently editing a Terminal, so we want to save a seperate printer which cn is the terminla cn
364 */
365 if($this->is_terminal){
367 /* reduce objectClasses to minimun */
368 $this->attrs['objectClass']= $this->objectclasses;
370 /* If a printer with the given dn exists, modify else create new one */
371 $ldap->cat($this->dn);
372 if($ldap->fetch()){
373 $this->orig_dn= $this->dn;
374 }else{
375 $this->orig_dn = "new";
376 }
377 }
379 /* Remove all empty values */
380 if ($this->orig_dn == 'new'){
381 $attrs= array();
382 foreach ($this->attrs as $key => $val){
383 if (is_array($val) && count($val) == 0){
384 continue;
385 }
386 $attrs[$key]= $val;
387 }
388 $this->attrs= $attrs;
389 }
392 /* Append printer user
393 */
394 foreach($this->member['AddUser'] as $mem){
395 $this->attrs['gotoUserPrinter'][]=$mem['uid'][0];
396 }
398 /* Append printer group
399 */
400 foreach($this->member['AddGroup'] as $mem){
401 $this->attrs['gotoGroupPrinter'][]=$mem['cn'][0];
402 }
404 /* Append printer admin user
405 */
406 foreach($this->member['AddAdminUser'] as $mem){
407 $this->attrs['gotoUserAdminPrinter'][]=$mem['uid'][0];
408 }
410 /* Append printer admin group
411 */
412 foreach($this->member['AddAdminGroup'] as $mem){
413 $this->attrs['gotoGroupAdminPrinter'][]=$mem['cn'][0];
414 }
416 /* Write back to ldap */
417 $ldap= $this->config->get_ldap_link();
418 if ($this->orig_dn == 'new'){
419 $ldap->cd($this->config->current['BASE']);
420 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
421 $ldap->cd($this->dn);
422 $ldap->add($this->attrs);
423 $this->handle_post_events("add");
424 } else {
425 if ($this->orig_dn != $this->dn){
426 $this->move($this->orig_dn, $this->dn);
427 }
429 $ldap->cd($this->dn);
430 $ldap->modify($this->attrs);
431 $this->handle_post_events("modify");
432 }
433 show_ldap_error($ldap->get_error());
435 /* Optionally execute a command after we're done */
436 $this->postcreate();
437 }
439 function generateList(){
440 $a_return=array();
441 foreach($this->member as $type => $values){
442 $a_return[$type]=array();
443 foreach($values as $value){
444 if((preg_match("/Group/i",$type))){
445 if(!isset($value['description'])){
446 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0];
447 }else{
448 $a_return[$type][$value['cn'][0]]= _("Group")." : ".$value['cn'][0]." [".$value['description'][0]."]";
449 }
450 }else{
451 $a_return[$type][$value['uid'][0]]=_("User")." : ".$value['cn'][0];
452 }
453 }
454 }
455 return($a_return);
456 }
459 }
463 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
464 ?>