Code

Completed migration
[gosa.git] / include / class_sambaMungedDial.inc
1 <?php
2 /*
3   This code is part of GOsa (https://gosa.gonicus.de)
4   Copyright (C) 2004  Cajus Pollmeier
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2 of the License, or
9   (at your option) any later version.
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
22 /* File header is treated as constant. It cannot be defined inside the class if $PHP_VERSION < 5 */
23 define ("FILEHEADER",   "6d00080020002000200020002000200020002000200020002000200020".
24                         "0020002000200020002000200020006400010020002000200020002000".
25                         "2000200020002000200020002000200020002000200020002000200020".
26                         "00200020002000200050001000");
28 class sambaMungedDial
29 {
30   /* Terminal server variables (samba3) */
31   var $ctx= array('CtxCfgPresent' =>          '551e0bb0',
32                         'CtxCfgFlags1'=>            '00e00010',
33                         'CtxCallback'=>             '0000000e',
34                         'CtxShadow'=>               '01000000',
35                         'CtxKeyboardLayout' =>      '00000000',
36                         'CtxMinEncryptionLevel' =>  '',
37                         'CtxWorkDirectory' =>       '',
38                         'CtxNWLogonServer' =>       '',  
39                         'CtxWFHomeDir' =>           '',
40                         'CtxWFHomeDirDrive' =>      '',
41                         'CtxWFProfilePath' =>       '',
42                         'CtxInitialProgram' =>      '',
43                         'CtxCallbackNumber' =>      '');
45   /* attribute list for save action */
46   var $ctxattributes= array("CtxCfgPresent", "CtxCfgFlags1", "CtxCallback",
47         "CtxMaxConnectionTime", "CtxMaxDisconnectionTime",
48         "CtxMaxIdleTime", "CtxKeyboardLayout", "CtxMinEncryptionLevel",
49         "CtxWorkDirectory", "CtxNWLogonServer", "CtxWFHomeDir",
50         "CtxWFHomeDirDrive", "CtxWFProfilePath", "CtxInitialProgram",
51         "CtxCallbackNumber");   
53   /* These parameters are treated as strings and get a trailing zero */
54   var $stringParams= array( "CtxWorkDirectory", "CtxNWLogonServer", 
55                         "CtxWFHomeDir", "CtxWFHomeDirDrive", 
56                         "CtxWFProfilePath", "CtxInitialProgram", "CtxCallbackNumber");
58   /* These parameters are treated as time values and get converted */
59   var $timeParams= array("CtxMaxConnectionTime", "CtxMaxDisconnectionTime", "CtxMaxIdleTime");
62   function strhex($string)
63   {
64         $hex="";
65         for ($i=0; $i<strlen($string); $i++) {
66                 $hex.= dechex(ord($string[$i]));
67   }
68         
69         return ($hex);
70   }
72   function hexstr($hex)
73   {
74         $string="";
75         for ($i=0; $i<strlen($hex)-1; $i+=2) {
76                 $string.= chr(hexdec($hex[$i].$hex[$i+1]));
77         }
79         return ($string);
80   }
82   function endian($src)
83   {
84         return (substr($src, 2, 2).substr($src, 0, 2));
85   }
87   function genTime ($minutes)
88   {
89         $usec= (int) ($minutes * 60 * 1000);
90         $src=  sprintf('%04X%04X', $usec & 0x0FFFF, ($usec & 0x0FFFF0000) >> 16);
91         return (sambaMungedDial::endian(substr($src, 0, 4)).sambaMungedDial::endian(substr($src, 4, 4)));
92   }
94   function readTime ($time)
95   {
96         $lo= substr($time, 0, 4);
97         $hi= substr($time, 4, 4);
99         $usecs= (hexdec(substr($lo, 2, 2)) * 256 + hexdec(substr($lo, 0, 2))) +
100                 (hexdec(substr($hi, 2 ,2)) * 256 + hexdec(substr($hi, 0, 2))) * 256 * 256;
102         return ((int)($usecs / (60 * 1000)));
103   }
105   function to8bit($string)
106   {
107         $result= "";
109         /* Strip zeros */
110         for ($i= 0; $i<strlen($string); $i++){
111                 if ($string[$i] != chr(0)){
112                         $result.= $string[$i];
113                 }
114         }
116         return ($result);
117   }
119   function is_samba_path($path)
120   {
121         if ($path == ""){
122                 return (TRUE);
123         }
125         if (!preg_match('/^[a-z0-9%\\\\_.:+-\\\\$]+$/i', $path)){
126                 return (FALSE);
127         }
129         return preg_match ("/\\\\.+$/", $path);
130   }
132   /* Encode full MungedDial-String */
133   function encode_munged ($params)
134   {
135         /* Walk through the parameters and convert them */
136         $result= sambaMungedDial::hexstr(FILEHEADER);
137         foreach ($params as $paramName => $paramValue) {
138                 
139                 /* String parameter? */
140                 if (in_array($paramName, $this->stringParams)){
141                         $isString= TRUE;
142                         $paramValue= sambaMungedDial::strhex($paramValue.chr(0).chr(0));
143                 } else {
144                         $isString= FALSE;
145                 }
147                 /* Time parameter? */
148                 if (in_array($paramName, $this->timeParams)){
149                         $paramValue= sambaMungedDial::genTime($paramValue);
150                 }
152                 $result.= sambaMungedDial::munge($paramName, $paramValue, $isString);
153         }
154         
155         return ($result);
156   }
158   /* Setup parameter given by paramName to MungedDial-Format */
159   function munge($paramName, $paramValue, $isString) 
160   {
161         $result= "";
162         
163         /* Encode paramName to UTF-16 */
164         if (function_exists("recode")){
165                 $utfName= recode("ISO8859-15..UTF-16", $paramName);
166         } else {
167                 $utfName= iconv("ISO8859-15", "UTF-16BE", $paramName);
168         }
170         /* Set parameter length, high and low byte */
171         $paramLen= strlen($utfName);
172         $result.= chr($paramLen & 0x0FF);
173         $result.= chr(($paramLen & 0x0FF00) >> 8);
175         /* String parameters have additional trailing bytes */
176         $valueLen= strlen($paramValue);
177         $result.= chr($valueLen & 0x0FF);
178         $result.= chr(($valueLen & 0x0FF00) >> 8);
179         
180         /* Length fields have a trailing '01' appended by the UTF-16 converted name */
181         $result.= chr(1);
182         $result.= $utfName;
184         /* Parameter is padded with '00' */
185         $result.= chr(0);
186         $result.= $paramValue;
188         /* Append a trailing '00' to string parameters */
189         if ($isString && (strlen($paramValue) & 1)){
190                 $result.= chr(0);
191         }
192         
193         return ($result);
194   }
196   /* Takes a base64-encoded MungedDial-String and returns an array of included parameters and values */
197   function decode_munged($munge)
198   {
199         $result= array();
200                 
201         /* Remove base64 encoding and skip FILEHEADER */
202         $ctxField= substr(base64_decode($munge), strlen(FILEHEADER) / 2);
204         /* Decode parameters */
205         while ($ctxField!=""){
207                 /* Read value lengths */
208                 $ctxParmNameLength= ord($ctxField[0]) + 16 * ord($ctxField[1]);
209                 $ctxParmLength= ord($ctxField[2]) + 16 * ord($ctxField[3]);
210                                 
211                 /* Reposition ctxField on start of parameter name, read parameter name */
212                 $ctxField= substr($ctxField, 6);
213                 $ctxParmName= sambaMungedDial::to8bit(substr($ctxField, 0, $ctxParmNameLength));
214                                 
215                 /* Reposition ctxField on start of parameter */
216                 $ctxField= substr($ctxField, $ctxParmNameLength);
217                 $ctxParm= substr($ctxField, 0, $ctxParmLength);
218                                 
219                 /* If string parameter, convert */
220                 if (in_array($ctxParmName, $this->stringParams)){
221                                 $ctxParm= sambaMungedDial::hexstr($ctxParm);
222                 }
223                 /* If time parameter, convert */
224                 if (in_array($ctxParmName, $this->timeParams)){
225                         $ctxParm= sambaMungedDial::readTime($ctxParm);
226                 }
228                 /* Assign in result array */
229                 $result[$ctxParmName]= trim($ctxParm);
230                                 
231                 /* Reposition ctxField on end of parameter and continue */
232                 $ctxField= substr($ctxField, $ctxParmLength);
233         }
235         return ($result);
236   }
238   /* function takes a base64-encoded sambaMungedDial */
239   function load ($mungedDial)
240   {
241         $this->ctx= $this->decode_munged($mungedDial);
242   }
244   /* Returns ready-to-run mungedDialString to be filled into ldap */
245   function getMunged ()
246   {
247         // Do extra check for valid timeParams (they must be set to 0 if disabled)
248         foreach($this->timeParams as $value) {
249                 if(!isset($this->ctx[$value])) {
250                         $this->ctx[$value]= 0;
251                 }
252         }
253         $result= base64_encode($this->encode_munged($this->ctx));
254         return $result;
255   }
257   /* Returns array of flags, which can be set on-demand with activated java-script */
258   function getOnDemandFlags ()
259   {
260         $result= array();
261         if ($_SESSION["js"]){
262                 foreach ($this->timeParams as $value) {
263                         if (!isset($this->ctx[$value]) || (isset($this->ctx[$value]) && $this->ctx[$value] == 0)) {
264                                 $result[$value."Mode"]= "disabled";
265                         }
266                 }
267                 
268                 if (substr($this->ctx['CtxCfgFlags1'], 6, 1) == "1") {
269                         $result['CtxInitialProgramMode'] = "disabled";
270                 }
271         }
273         return $result;
274   }
276   /*Gets Terminal-Server-Login value: enabled/disabled */
277   function getTsLogin ()
278   {
279         $flags= ord(substr($this->ctx['CtxCfgFlags1'], 5, 1));
280         
281         if ($flags & 1) {
282                 $result= false;
283         } else {
284                 $result= true;
285   }
287         return $result;
288   }
290   /* Sets Terminal-Server-Login value: enabled/disabled */
291   function setTsLogin ($checked)
292   {
293         $flag= substr($this->ctx['CtxCfgFlags1'], 5, 1);
294         
295         if ($checked) {
296                 $flag|= 1;
297         } else {
298                 $flag&= 0xFE;
299         }
300         
301         $this->ctx['CtxCfgFlags1'][5]= sprintf('%1X', $flag);
302   }
304   /* gets Broken-Connection value: disconnect/reset */
305   function getBrokenConn ()
306   {
307         $flags= ord(substr($this->ctx['CtxCfgFlags1'], 5, 1));
308         if ($flags & 4) {
309                 $result= "1";
310         } else {
311                 $result= "0";
312         }
313         
314         return $result;
315   }
317   /* sets Broken-Connection value: disconnect/reset */
318   function setBrokenConn ($checked) 
319   {
320         $flag= substr($this->ctx['CtxCfgFlags1'], 5, 1);
321         
322         if ($checked) {
323                 $flag|= 4;
324         } else {
325                 $flag&= 0xFB;
326         }
327         
328         $this->ctx['CtxCfgFlags1'][5]= sprintf('%1X', $flag);
329   }
331   /* gets Reconnection value: from any client/from previous client only */
332   function getReConn ()
333   {
334         $flags= ord(substr($this->ctx['CtxCfgFlags1'], 5, 1));
335         if ($flags & 2) {
336                 $result= "1";
337         } else {
338                 $result= "0";   
339         }
340         
341         return $result;
342   }
344   /* sets Reconnection value: from any client/from previous client only */
345   function setReConn ($checked)
346   {
347         $flag= substr($this->ctx['CtxCfgFlags1'], 5, 1);
348         
349         if ($checked) {
350                 $flag|= 2;
351         } else {
352                 $flag&= 0xFD;
353         }
354         
355         $this->ctx['CtxCfgFlags1'][5]= sprintf('%1X', $flag);
356   }
358   /* gets Inherit-config-from-client value: enabled/disabled */
359   function getInheritMode ()
360   {
361         if (substr($this->ctx['CtxCfgFlags1'], 6, 1) == "1") {
362                 $result= true;
363         } else {
364                 $result= false;
365         }
367         return $result;
368   }
370   /* sets Inherit-config-from-client value: enabled/disabled */
371   function setInheritMode ($checked)
372   {
373         if ($checked) {
374                 $this->ctx['CtxCfgFlags1'][6]= "1";
375         } else {
376                 $this->ctx['CtxCfgFlags1'][6]= "0";
377         }
378   }
380   /* gets shadow value (enum): 0-4
381         0: disabled
382         1: input on, notify on
383         2: input on, notify off
384         3: input off, notify on
385         4: input off, notify off 
386   */
387   function getShadow ()
388   {
389         $result= substr($this->ctx['CtxCfgFlags1'], 1, 1);
390         return $result;
391   }
393   /* sets shadow value */
394   function setShadow ($checked, $value)
395   {
396         if ($checked) {
397                 $this->ctx['CtxCfgFlags1'][1]= sprintf('%1X', $value);
398         }
399   }
401   /* gets connect-client-drive-at-logon value: enabled/disabled */
402   function getConnectClientDrives ()
403   {
404         $connections= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
405         if ($connections & 8) {
406                 $result= true;
407         } else {
408                 $result= false;
409         }
411         return $result;
412   }
414   /* sets connect-client-drive-at-logon value: enabled/disabled */
415   function setConnectClientDrives ($checked)
416   {
417         $flag= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
418         if ($checked) {
419                 $flag|= 8;
420         } else {
421                 $flag&= 0xF7;
422         }
424         $this->ctx['CtxCfgFlags1'][2]= sprintf('%1X', $flag);
425   }
427   /* gets connect-client-printers-at-logon value: enabled/disabled */
428   function getConnectClientPrinters ()
429   {
430         $connections= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
431         if ($connections & 4) {
432                 $result= true;
433         } else {
434                 $result= false;
435         }
436         
437         return $result;
438   }
440   /* sets connect-client-printers-at-logon value: enabled/disabled */
441   function setConnectClientPrinters ($checked)
442   {
443         $flag= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
444         
445         if ($checked) {
446                 $flag|= 4;
447         } else {
448                 $flag&= 0xFB;
449         }
450         
451         $this->ctx['CtxCfgFlags1'][2]= sprintf('%1X', $flag);
452   }
454   /* gets set-client-printer-to-default value: enabled/disabled */
455   function getDefaultPrinter ()
456   {
457         $connections= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
458         if ($connections & 2) {
459                 $result= true;
460         } else {
461                 $result= false;
462         }
464         return $result;
465   }
467   /* sets set-client-printer-to-default value: enabled/disabled */
468   function setDefaultPrinter ($checked)
469   {
470         $flag= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
471         
472         if ($checked) {
473                 $flag|= 2;
474         } else {
475                 $flag&= 0xFD;
476         }
477         
478         $this->ctx['CtxCfgFlags1'][2]= sprintf('%1X', $flag);
479   }
481   /* SMARTY: gets the checkbox state of "Connection" */
482   function getCtxMaxConnectionTimeF ()
483   {
484         // Connection Time is 0 if disabled
485         if (isset($this->ctx['CtxMaxConnectionTime']) && ($this->ctx['CtxMaxConnectionTime'] != 0)) {
486                 $result= true;
487         } else {
488                 $result= false;
489         }
491         return $result;
492   }
494   /* SMARTY: sets the checkbox "Connection" to unchecked */
495   function setCtxMaxConnectionTimeF ($checked)
496   {
497         if ($checked) {
498                 unset ($this->ctx['CtxMaxConnectionTime']);
499         }
500   }
502   /* SMARTY: gets the checkbox state of "Disconnection" */
503   function getCtxMaxDisconnectionTimeF ()
504   {
505         // Connection Time is 0 if disabled
506         if (isset($this->ctx['CtxMaxDisconnectionTime']) && ($this->ctx['CtxMaxDisconnectionTime'] != 0)) {
507                 $result= true;
508         } else {
509                 $result= false;
510         }
512         return $result;
513   }
515   /* SMARTY: sets the checkbox "Disconnection" to unchecked */
516   function setCtxMaxDisconnectionTimeF ($checked)
517   {
518         if ($checked) {
519                 unset ($this->ctx['CtxMaxDisconnectionTime']);
520         } 
521   }
523   /* SMARTY: gets the checkbox state of "Idle" */
524   function getCtxMaxIdleTimeF ()
525   {
526         // Connection Time is 0 if disabled
527         if (isset($this->ctx['CtxMaxIdleTime']) && ($this->ctx['CtxMaxIdleTime'] != 0)) {
528                 $result= true;
529         } else {
530                 $result= false;
531         }
533         return $result;
534   }
536   /* SMARTY: sets the checkbox "Idle" to unchecked */
537   function setCtxMaxIdleTimeF ($checked)
538   {
539         if ($checked) {
540                 unset ($this->ctx['CtxMaxIdleTime']);
541         }
542   }
546 ?>