Code

Updated posix plugin
[gosa.git] / gosa-core / plugins / personal / samba / class_sambaLogonHours.inc
1 <?php
3 class sambaLogonHours extends plugin
4 {
6   var $sambaLogonHours = "";
7   var $Matrix          = array();
8   var $timezone        = 0; 
9   var $config;
11   function sambaLogonHours (&$config, $dn, $slh)
12   {
13     plugin::plugin($config,$dn);
14     $this->sambaLogonHours = $slh;
16     /* Get default timezone */
17     $zone = get_default_timezone();
18     $this->timezone = $zone['value'];
20     /* Convert to bin */
21     $tmp = '';
22     for($i = 0 ; $i < strlen($slh) ; $i ++){
23       $tmp .= str_pad(base_convert($slh[$i],16,2),4,'0',STR_PAD_LEFT);
24     }
25     $slh = $tmp;
27     /* Set to default if value is empty or seams to be broken */
28     if(strlen($slh) != 168){
29       $slh = str_pad('',168 , "1");
30     }
32     /* Rework string, because it was stored in little endian */
33     $new = '';
34     for($i = 0 ; $i < 21 ; $i ++ ){
35       $part         = strrev(substr($slh, $i * 8, 8));
36       $byte['hi']   = substr($part,0,4);
37       $byte['low']  = substr($part,4,4);
38       $new .= $byte['hi'].$byte['low'];
39     }
41     /* Shift string to match given timezone settings */
42     $shift_by = (168 + $this->timezone) % 168;
43     for($i = 0 ; $i < $shift_by; $i ++){
44       $new = $new[167].$new;
45       $new = substr($new,0,168);
46     }
48     /* Create matrix */ 
49     $this->Matrix = array();
50     for($day = 0 ; $day < 7 ; $day ++ ){
51       for($hour  = 0 ; $hour < 24 ; $hour ++){
52         $bitpos  = ($day * 24 + $hour) % 168;
53         $this->Matrix[$day][$hour] = $new[$bitpos]; 
54       }
55     }
56   }
58   function execute()
59   {
61     $week_days = array();
62     $week_days[0]= _("Sunday");
63     $week_days[1]= _("Monday");
64     $week_days[2]= _("Tuesday");
65     $week_days[3]= _("Wednesday");
66     $week_days[4]= _("Thursday");
67     $week_days[5]= _("Friday");
68     $week_days[6]= _("Saturday");
70     $hours = array();
71     for($i = 0 ; $i <24 ; $i++ ){
72       $hours[$i] = $i ;
73     }
74     $ui =get_userinfo();
76     $smarty = get_smarty();
77     $smarty->assign("sambaLogonHoursACL",$ui->get_permissions($this->acl_base,"user/generic","sambaLogonHours"));
78     $smarty->assign("Matrix",$this->Matrix);
79     $smarty->assign("Days"  ,$week_days);
80     $smarty->assign("Hours" ,$hours);
81     return($smarty->fetch(get_template_path("sambaLogonHours.tpl",TRUE,dirname(__FILE__))));
82   }
84   function save_object()
85   {
86     /* Get userinfo */
87     $ui = get_userinfo();
88   
89     /* Check if dialog was opened and if there were any changes */
90     if(isset($_POST['sambaLogonHoursPosted']) && preg_match("/w/i",$ui->get_permissions($this->acl_base,"user/generic","sambaLogonHours"))){
91       foreach($this->Matrix as $day_key => $days){
92         foreach($days as $hour_key => $hour){
93           if(isset($_POST['day_'.$day_key.'_'.$hour_key])){
94             $this->Matrix[$day_key][$hour_key] = 1;
95           }else{
96             $this->Matrix[$day_key][$hour_key] = 0;
97           }
98         }
99       }
100     }
101   }
102   
103   function save()
104   {
106     /* Convert Matrix to Hex */
107     $slh = "";
108     foreach($this->Matrix as $day_key => $days){
109       foreach($days as $hour_key => $hour){
110         $slh .= $hour;
111       }
112     }
114     /* Shift string to match given timezone settings */
115     $shift_by = (168 + ($this->timezone*(-1))) % 168;
116     for($i = 0 ; $i < $shift_by; $i ++){
117       $slh = $slh[167].$slh;
118       $slh = substr($slh,0,168);
119     }
120  
121     /* Rework string, because it was stored in little endian */
122     $new = '';
123     for($i = 0 ; $i < 21 ; $i ++ ){
124       $part         = strrev(substr($slh, $i * 8, 8));
125       $byte['hi']   = substr($part,0,4);
126       $byte['low']  = substr($part,4,4);
127       $new .= $byte['hi'].$byte['low'];
128     }
130     /* Convert to bin */
131     $tmp = '';
132     for($i = 0 ; $i < 21 ; $i ++){
133       $tmp .= str_pad(base_convert(substr($new,$i*8,8),2,16),2,'0',STR_PAD_LEFT);
134     }
136     /* Create uppercase HEX chars */
137     $ret = strtoupper( $tmp);
138     
139     /* All hours and days selected */
140     if(substr_count($ret,'F') == 42){
141       $ret = "";
142     }
144     return($ret);
145   }
147 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
148 ?>