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 Timezone to be able to shift to the correct beginning */
17 if(isset($this->config->current['TIMEZONE'])){
18 $this->timezone = $this->config->current['TIMEZONE'];
19 $tz = $this->config->current['TIMEZONE'];
20 $tz_a = timezones_array($tz);
22 if(!count($tz_a)){
23 $this->timezone = 0;
24 print_red(sprintf(_("The configured timezone '%s' seems not to valid."),$tz));
25 }else{
26 $this->timezone = $tz_a['value'] / (60*60);
27 }
28 }
30 /* Convert to bin */
31 $tmp = '';
32 for($i = 0 ; $i < strlen($slh) ; $i ++){
33 $tmp .= str_pad(base_convert($slh[$i],16,2),4,'0',STR_PAD_LEFT);
34 }
35 $slh = $tmp;
37 /* Set to default if value is empty or seams to be broken */
38 if(strlen($slh) != 168){
39 $slh = str_pad('',168 , "1");
40 }
42 /* Rework string, because it was stored in little endian */
43 $new = '';
44 for($i = 0 ; $i < 21 ; $i ++ ){
45 $part = strrev(substr($slh, $i * 8, 8));
46 $byte['hi'] = substr($part,0,4);
47 $byte['low'] = substr($part,4,4);
48 $new .= $byte['hi'].$byte['low'];
49 }
51 /* Shift string to match given timezone settings */
52 $shift_by = (168 + $this->timezone) % 168;
53 for($i = 0 ; $i < $shift_by; $i ++){
54 $new = $new[167].$new;
55 $new = substr($new,0,168);
56 }
58 /* Create matrix */
59 $this->Matrix = array();
60 for($day = 0 ; $day < 7 ; $day ++ ){
61 for($hour = 0 ; $hour < 24 ; $hour ++){
62 $bitpos = ($day * 24 + $hour) % 168;
63 $this->Matrix[$day][$hour] = $new[$bitpos];
64 }
65 }
66 }
68 function execute()
69 {
70 $week_days = array();
71 $week_days[0]= _("Sunday");
72 $week_days[1]= _("Monday");
73 $week_days[2]= _("Tuesday");
74 $week_days[3]= _("Wednesday");
75 $week_days[4]= _("Thursday");
76 $week_days[5]= _("Friday");
77 $week_days[6]= _("Saturday");
79 $hours = array();
80 for($i = 0 ; $i <24 ; $i++ ){
81 $hours[$i] = $i ;
82 }
83 $ui =get_userinfo();
85 $smarty = get_smarty();
86 $smarty->assign("sambaLogonHoursACL",$ui->get_permissions($this->acl_base,"user/generic","sambaLogonHours"));
87 $smarty->assign("Matrix",$this->Matrix);
88 $smarty->assign("Days" ,$week_days);
89 $smarty->assign("Hours" ,$hours);
90 return($smarty->fetch(get_template_path("sambaLogonHours.tpl",TRUE,dirname(__FILE__))));
91 }
93 function save_object()
94 {
95 /* Get userinfo */
96 $ui = get_userinfo();
98 /* Check if dialog was opened and if there were any changes */
99 if(isset($_POST['sambaLogonHoursPosted']) && preg_match("/w/i",$ui->get_permissions($this->acl_base,"user/generic","sambaLogonHours"))){
100 foreach($this->Matrix as $day_key => $days){
101 foreach($days as $hour_key => $hour){
102 if(isset($_POST['day_'.$day_key.'_'.$hour_key])){
103 $this->Matrix[$day_key][$hour_key] = 1;
104 }else{
105 $this->Matrix[$day_key][$hour_key] = 0;
106 }
107 }
108 }
109 }
110 }
112 function save()
113 {
115 /* Convert Matrix to Hex */
116 $slh = "";
117 foreach($this->Matrix as $day_key => $days){
118 foreach($days as $hour_key => $hour){
119 $slh .= $hour;
120 }
121 }
123 /* Shift string to match given timezone settings */
124 $shift_by = (168 + ($this->timezone*(-1))) % 168;
125 for($i = 0 ; $i < $shift_by; $i ++){
126 $slh = $slh[167].$slh;
127 $slh = substr($slh,0,168);
128 }
130 /* Rework string, because it was stored in little endian */
131 $new = '';
132 for($i = 0 ; $i < 21 ; $i ++ ){
133 $part = strrev(substr($slh, $i * 8, 8));
134 $byte['hi'] = substr($part,0,4);
135 $byte['low'] = substr($part,4,4);
136 $new .= $byte['hi'].$byte['low'];
137 }
139 /* Convert to bin */
140 $tmp = '';
141 for($i = 0 ; $i < 21 ; $i ++){
142 $tmp .= str_pad(base_convert(substr($new,$i*8,8),2,16),2,'0',STR_PAD_LEFT);
143 }
145 /* Create uppercase HEX chars */
146 $ret = strtoupper( $tmp);
148 /* All hours and days selected */
149 if(substr_count($ret,'F') == 42){
150 $ret = "";
151 }
153 return($ret);
154 }
155 }
156 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
157 ?>