Code

Updated print_red stuff
[gosa.git] / gosa-plugins / gofon / admin / systems / gofon / class_phoneGeneric.inc
1 <?php
3 class phoneGeneric extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Manage phone 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= TRUE;
14   /* Needed values and lists */
15   var $base             = "";
16   var $cn               = "";
17   var $description      = "";
18   var $orig_dn          = "";
19   var $goFonType        = "";
20   var $goFonDmtfMode    = "";
21   var $goFonHost        = "";
22   var $goFonDefaultIP   = "dynamic";
23   var $goFonQualify     = "";
24   var $goFonAuth        = "";
25   var $goFonSecret      = "";
26   var $goFonInkeys      = "";
27   var $goFonPermit      = array();
28   var $goFonDeny        = array();
29   var $goFonOutkey      = "";
30   var $goFonTrunk       = "";
31   var $goFonAccountCode = "";
32   var $goFonMSN         = "";
33   var $selected_categorie    = 0;
34   var $netConfigDNS;
35   var $view_logged      = FALSE;
37   /* attribute list for save action */
38   var $attributes= array("cn", "description",
39                           "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
40                           "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
41                           "goFonTrunk","goFonAccountCode","goFonMSN","selected_categorie","goFonPermit","goFonDeny" 
42                          );
44   /* this array defines which attributes are schown / saved for the different type of phones */ 
45   var $usedattrs =    array( "0"=>array("cn", "description", 
46                                   "goFonType","goFonDmtfMode","goFonHost","goFonDefaultIP",
47                                   "goFonQualify"),
48                              "1"=>array("cn", "description", 
49                                   "goFonType","goFonHost","goFonDefaultIP",
50                                   "goFonQualify","goFonAuth","goFonSecret","goFonInkeys","goFonOutkey",
51                                   "goFonTrunk","goFonAccountCode","selected_categorie","goFonPermit","goFonDeny"),
52                              "2"=>array("cn", "description", "goFonMSN"));
53   
55   var $objectclasses= array("top", "goFonHardware");
57   function phoneGeneric (&$config, $dn= NULL, $parent= NULL)
58   {
59     plugin::plugin ($config, $dn, $parent);
60     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses, true);
62     /* Set base */
63     if ($this->dn == "new"){
64       $ui= get_userinfo();
65       $this->base= dn2base($ui->dn);
66       $this->cn= "";
67     } else {
68       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
69     }
70   
71     if($this->goFonMSN != ""){
72       $this->selected_categorie = 2;
73     }elseif($this->goFonAccountCode != ""){
74       $this->selected_categorie = 1 ;
75     
76       if(isset($this->attrs['goFonPermit']['count'])){
77         unset ($this->attrs['goFonPermit']['count']);
78         $this->goFonPermit=$this->attrs['goFonPermit'];
79       }  
80      
81       if(isset($this->attrs['goFonDeny']['count'])){
82         unset ($this->attrs['goFonDeny']['count'])   ;
83         $this->goFonDeny=$this->attrs['goFonDeny'];
84       } 
86     } else {
87       $this->selected_categorie = 0;
88     }
90     if($this->goFonDefaultIP!="dynamic"){
91       $this->goFonDefaultIP = "network";
92     }
94     /* Save dn for later references */
95     $this->orig_dn= $this->dn;
96   }
98   function set_acl_base($base)
99   {
100     plugin::set_acl_base($base);
101     $this->netConfigDNS->set_acl_base($base);
102   }
104   function set_acl_category($cat)
105   {
106     plugin::set_acl_category($cat);
107     $this->netConfigDNS->set_acl_category($cat);
108   }
111   function execute()
112   {
113     /* Call parent execute */
114     plugin::execute();
116     if($this->is_account && !$this->view_logged){
117       $this->view_logged = TRUE;
118       new log("view","phone/".get_class($this),$this->dn);
119     }
121     /* Do we represent a valid phone? */
122     if (!$this->is_account && $this->parent === NULL){
123       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
124         _("This 'dn' has no phone features.")."</b>";
125       return($display);
126     }
128     /* Base select dialog */
129     $once = true;
130     foreach($_POST as $name => $value){
131       if(preg_match("/^chooseBase/",$name) && $once){
132         $once = false;
133         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
134         $this->dialog->setCurrentBase($this->base);
135       }
136     }
138     /* Dialog handling */
139     if(is_object($this->dialog)){
140       /* Must be called before save_object */
141       $this->dialog->save_object();
143       if($this->dialog->isClosed()){
144         $this->dialog = false;
145       }elseif($this->dialog->isSelected()){
146         
147         /* A new base was selected, check if it is a valid one */
148         $tmp = $this->get_allowed_bases();
149         if(isset($tmp[$this->dialog->isSelected()])){
150           $this->base = $this->dialog->isSelected();
151         }
153         $this->dialog= false;
154       }else{
155         return($this->dialog->execute());
156       }
157     }
159     /* handle Permit Add*/
160     if(isset($_POST['goFonPermitAdd']) && $this->acl_is_writeable("goFonPermit")){
161       if(isset($_POST['goFonPermitNew'])){
162         if(is_string($this->goFonPermit)){
163           $this->goFonPermit=array();
164         }
165         $new = $_POST['goFonPermitNew'];
166         if(strlen($new)> 1) {
167           $this->goFonPermit[]= $new;
168         }
169       }
170     }
171       
172     /* handle Deny Add*/
173     if(isset($_POST['goFonDenyAdd']) && $this->acl_is_writeable("goFonDeny")){
174       if(isset($_POST['goFonDenyNew'])){
175         if(is_string($this->goFonDeny)){
176           $this->goFonDeny=array();
177         }
178         $new = $_POST['goFonDenyNew'];
179         if(strlen($new)> 1) {
180           $this->goFonDeny[]= $new;
181         }
182       }
183     }
185     /* Handle Permit Deletion*/
186     if(isset($_POST['goFonPermitDel']) && $this->acl_is_writeable("goFonPermit")){
187       if(isset($_POST['goFonPermitS'])){
188         if(is_string($this->goFonPermit)){
189           $this->goFonPermit=array();
190         }
191         $new = $_POST['goFonPermitS'];
192         $tmp = array_flip($this->goFonPermit);
193         unset($tmp[$new]);
194         $this->goFonPermit=array();
195         foreach(array_flip($tmp) as $tm){
196           $this->goFonPermit[]=$tm;
197         }
198       }
199     }
202     /* Handle Permit Deletion*/
203     if(isset($_POST['goFonDenyDel']) && $this->acl_is_writeable("goFonDeny")){
204       if(isset($_POST['goFonDenyS'])){
205         if(is_string($this->goFonDeny)){
206           $this->goFonDeny=array();
207         }
208         $new = $_POST['goFonDenyS'];
209         $tmp = array_flip($this->goFonDeny);
210         unset($tmp[$new]);
211         $this->goFonDeny=array();
212         foreach(array_flip($tmp) as $tm){
213           $this->goFonDeny[]=$tm;
214         }
215       }
216     }
217   
218     /* Fill templating stuff */
219     $smarty= get_smarty();
221     $smarty->assign("bases", $this->get_allowed_bases());
223     $tmp = $this->plInfo();
224     foreach($tmp['plProvidedAcls'] as $name => $translation){
225       $smarty->assign($name."ACL",$this->getacl($name));
226     }
228     $smarty->assign("staticAddress","<font class=\"must\">*</font>");// $this->config->idepartments);
230     /* Create Arrays for samrty select boxes */
231     $smarty->assign("categories",     array("SIP","IAX","CAPI"));
232     $smarty->assign("goFonTypes",     array("peer"      =>"peer"      ,"user"   =>"user"    ,"friend" =>"friend"));
233     $smarty->assign("goFonDmtfModes", array("inband"    =>"inband"    ,"rfc2833"=>"rfc2833" ,"info"   =>"info"));
234     $smarty->assign("goFonAuths",     array("plaintext" =>"plaintext" ,"md5"    =>"md5"     /*,"rsa"    =>"rsa"*/));
235     $smarty->assign("goFonTrunks",    array("yes" =>_("yes") ,"no" => _("no")));
237     /* deativate all fields that are not used by the specified type */
238     foreach($this->attributes as $att){
239       if((!in_array($att,$this->usedattrs[$this->selected_categorie]))){
240         $smarty->assign($att."USED", "disabled" );
241         $smarty->assign($att, "");
242       }else{
243         $smarty->assign($att."USED", "" );
244         $smarty->assign($att, $this->$att);
245       }
246     }
247     
248     $smarty->assign("selected_categorie",$this->selected_categorie);
250     /* Assign attributes */
251     $smarty->assign("base_select", $this->base);
252     $smarty->assign("goFonDefaultIPs",array("dynamic"=>_("dynamic"),"network"=>_("Networksettings")));
253  
254     /* Show main page */
255     $str = $this->netConfigDNS->execute();
256     if(is_object($this->netConfigDNS->dialog)){
257       return($str);
258     }
259     $smarty->assign("netconfig", $str);
260     $smarty->assign("phonesettings", get_template_path("phonesettings.tpl",TRUE,dirname(__FILE__)));
261     return($smarty->fetch (get_template_path('phone.tpl', TRUE,dirname(__FILE__))));
262   }
264   function remove_from_parent()
265   {
266     if($this->acl_is_removeable()){
267       $ldap= $this->config->get_ldap_link();
268       $ldap->cd($this->config->current['BASE']);
270       $ldap->search ("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))", array("uid","cn"));
271       while ($attr =  $ldap->fetch()){
272         msg_dialog::display(_("Error"), sprintf(_("Cannot delete entry because it is still in use by '%s'!"), $attr['cn'][0]), ERROR_DIALOG);
273         return;
274       }
276       $this->netConfigDNS->remove_from_parent();
277       $ldap->rmdir($this->dn);
278   
279       new log("remove","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
281       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system phone/generic with dn '%s' failed!"),$this->dn));
282       $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
284       /* Delete references to object groups */
285       $ldap->cd ($this->config->current['BASE']);
286       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
287       while ($ldap->fetch()){
288         $og= new ogroup($this->config, $ldap->getDN());
289         unset($og->member[$this->dn]);
290         $og->save ();
291       }
292     }
293   }
296   /* Save data to object */
297   function save_object()
298   {
299     /* Create a base backup and reset the
300        base directly after calling plugin::save_object();
301        Base will be set seperatly a few lines below */
302     $base_tmp = $this->base;
303     plugin::save_object();
304     $this->base = $base_tmp;
306     $this->netConfigDNS->save_object();
308     /* Set new base if allowed */
309     $tmp = $this->get_allowed_bases();
310     if(isset($_POST['base'])){
311       if(isset($tmp[$_POST['base']])){
312         $this->base= $_POST['base'];
313       }
314     }
315   }
318   /* Check supplied data */
319   function check()
320   {
321     /* Call common method to give check the hook */
322     $message= plugin::check();
323     $message= array_merge($message, $this->netConfigDNS->check());
325     $this->dn= "cn=".$this->cn.",".get_ou('phoneou').$this->base;
327     /* To check for valid ip*/
328     if($this->netConfigDNS->ipHostNumber == ""){
329        $message[]= _("IP address is not set!");
330     } else {
331       if (!tests::is_ip($this->netConfigDNS->ipHostNumber)){
332         $message[]= _("IP address is not valid!");
333       }
334     }
336     /* Check if given name is a valid host/dns name */
337     if(!tests::is_dns_name($this->cn) ){
338       $message[] = _("Name is not valid!");
339     }
341     if ($this->cn == ""){
342       $message[]= _("Name is not set!");
343     }
344     if ($this->cn == "0"){ 
345       $message[]= _("'0' is a reserved name and cannot be used!");
346     }
348     if ($this->orig_dn != $this->dn){
349       $ldap= $this->config->get_ldap_link();
350       $ldap->cd ($this->base);
351       $ldap->search ("(cn=".$this->cn.")", array("cn"));
352       if ($ldap->count() != 0){
353         while ($attrs= $ldap->fetch()){
355           if(preg_match("/cn=dhcp,/",$attrs['dn'])){
356             continue;
357           }
358   
359           if ($attrs['dn'] != $this->orig_dn){
360             $message[]= _("Name is already in use!");
361             break;
362           }
363         }
364       }
365     }
367     return ($message);
368   }
371   /* Save to LDAP */
372   function save()
373   {
374     plugin::save();
375    
376    
377     /* only to define which attrs to save*/ 
378     $mode = $this->selected_categorie;
380     /* Remove all unwanted attrs */
381     foreach($this->attributes as $att){
382       
383       /* Check all attributes, if they are needed for this type of phone */
384       if(!in_array($att,$this->usedattrs[$mode])){
385         $this->attrs[$att] = array();
386       }
387     }
389     /* unset the categorie*/
390     unset($this->attrs['selected_categorie']);
391     
392     /* Remove all empty values */
393     if ($this->orig_dn == 'new'){
394       $attrs= array();
395       foreach ($this->attrs as $key => $val){
396         if (is_array($val) && count($val) == 0){
397           continue;
398         }
399         $attrs[$key]= $val;
400       }
401       $this->attrs= $attrs;
402     }
404     if($this->goFonDefaultIP!="dynamic"){
405       $this->attrs['goFonDefaultIP'] = $this->netConfigDNS->ipHostNumber;
406     }    
408     $this->attrs = $this->netConfigDNS->getVarsForSaving($this->attrs);
410     /* Write back to ldap */
411     $ldap= $this->config->get_ldap_link();
412     if ($this->orig_dn == 'new'){
413       $ldap->cd($this->config->current['BASE']);
414       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
415       $ldap->cd($this->dn);
416       $ldap->add($this->attrs);
417       new log("create","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
418       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
419     } else {
420       if ($this->orig_dn != $this->dn){
421         $this->move($this->orig_dn, $this->dn);
422       }
424       $ldap->cd($this->dn);
425       $this->cleanup();
426       $ldap->modify ($this->attrs); 
427       new log("modify","phone/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
429       // $user_phone_reload   
430       $ldap->cd ($this->config->current['BASE']); 
431       $user_phone_assignment = $ldap->fetch($ldap->search("(&(objectClass=goFonAccount)(goFonHardware=".$this->cn."))",array("uid")));
432       if($user_phone_assignment){
433         $usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $user_phone_assignment['dn']);
434         $usertab->by_object['phoneAccount']->is_modified = true;
435         $usertab->save();
436         unset($usertab);
437       }
438       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
439     }
440     $this->netConfigDNS->cn = $this->cn;    
441     $this->netConfigDNS->save();
442     show_ldap_error($ldap->get_error(), sprintf(_("Saving of phone/generic with dn '%s' failed!"),$this->dn));
443   }
446   /* Display generic part for server copy & paste */
447   function getCopyDialog()
448   {
449     $vars = array("cn");
450     $smarty = get_smarty();
451     $smarty->assign("cn" ,$this->cn);
452     $smarty->assign("object","phone");
453     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
454     $ret = array();
455     $ret['string'] = $str;
456     $ret['status'] = "";
457     return($ret);
458   }
461   function saveCopyDialog()
462   {
463     if(isset($_POST['cn'])){
464       $this->cn = $_POST['cn'];
465     }
466   }
469   function PrepareForCopyPaste($source)
470   {
471     plugin::PrepareForCopyPaste($source);
472     if(isset($source['macAddress'][0])){
473       $this->netConfigDNS->macAddress = $source['macAddress'][0];
474     }
475     if(isset($source['ipHostNumber'][0])){
476       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
477     }
479     $source_o = new phoneGeneric($this->config, $source['dn']);
480     $this->selected_categorie = $source_o->selected_categorie;
481   }
484   /* Return plugin informations for acl handling
485       #FIXME FAIscript seams to ununsed within this class... */
486   static function plInfo()
487   {
488     return (array(
489           "plShortName"   => _("Generic"),
490           "plDescription" => _("Phone generic"),
491           "plSelfModify"  => FALSE,
492           "plDepends"     => array(),
493           "plPriority"    => 0,
494           "plSection"     => array("administration"),
495           "plCategory"    => array("phone" => array("description"  => _("Phone hardware"),
496                                                     "objectClass"  => "goFonHardware")),
497           "plProvidedAcls"=> array(
498             "cn"                  => _("Name"),
499             "base"                => _("Base"),
500             "description"         => _("Description"),
501             "goFonType"           => _("SIP Mode"),
502             "goFonDmtfMode"       => _("SIP DTMF mode"),
503             "goFonDefaultIP"      => _("SIP Default ip"),
504             "goFonQualify"        => _("SIP Qualify"),
505             "goFonAuth"           => _("IAX authentication type"),
506             "goFonSecret"         => _("IAX secret"),
507             "goFonAccountCode"    => _("IAX account code"),
508             "goFonTrunk"          => _("IAX trunk lines"),
509             "goFonPermit"         => _("IAX permit settings"),
510             "goFonDeny"           => _("IAX deny settings"),
511             "goFonMSN"            => _("CAPI MSN"),
512             "categorie"  => _("Hardware type"))
513           ));
514   }
519 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
520 ?>