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 }
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) {
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 }
155 return ($result);
156 }
158 /* Setup parameter given by paramName to MungedDial-Format */
159 function munge($paramName, $paramValue, $isString)
160 {
161 $result= "";
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);
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 }
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();
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]);
211 /* Reposition ctxField on start of parameter name, read parameter name */
212 $ctxField= substr($ctxField, 6);
213 $ctxParmName= sambaMungedDial::to8bit(substr($ctxField, 0, $ctxParmNameLength));
215 /* Reposition ctxField on start of parameter */
216 $ctxField= substr($ctxField, $ctxParmNameLength);
217 $ctxParm= substr($ctxField, 0, $ctxParmLength);
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);
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 } else {
266 $result[$value."Mode"]= "";
267 }
268 }
270 if (substr($this->ctx['CtxCfgFlags1'], 6, 1) == "1") {
271 $result['CtxInitialProgramMode'] = "disabled";
272 } else {
273 $result['CtxInitialProgramMode'] = "";
274 }
275 }
277 return $result;
278 }
280 /*Gets Terminal-Server-Login value: enabled/disabled */
281 function getTsLogin ()
282 {
283 $flags= ord(substr($this->ctx['CtxCfgFlags1'], 5, 1));
285 if ($flags & 1) {
286 $result= false;
287 } else {
288 $result= true;
289 }
291 return $result;
292 }
294 /* Sets Terminal-Server-Login value: enabled/disabled */
295 function setTsLogin ($checked)
296 {
297 $flag= substr($this->ctx['CtxCfgFlags1'], 5, 1);
299 if ($checked) {
300 $flag|= 1;
301 } else {
302 $flag&= 0xFE;
303 }
305 $this->ctx['CtxCfgFlags1'][5]= sprintf('%1X', $flag);
306 }
308 /* gets Broken-Connection value: disconnect/reset */
309 function getBrokenConn ()
310 {
311 $flags= ord(substr($this->ctx['CtxCfgFlags1'], 5, 1));
312 if ($flags & 4) {
313 $result= "1";
314 } else {
315 $result= "0";
316 }
318 return $result;
319 }
321 /* sets Broken-Connection value: disconnect/reset */
322 function setBrokenConn ($checked)
323 {
324 $flag= substr($this->ctx['CtxCfgFlags1'], 5, 1);
326 if ($checked) {
327 $flag|= 4;
328 } else {
329 $flag&= 0xFB;
330 }
332 $this->ctx['CtxCfgFlags1'][5]= sprintf('%1X', $flag);
333 }
335 /* gets Reconnection value: from any client/from previous client only */
336 function getReConn ()
337 {
338 $flags= ord(substr($this->ctx['CtxCfgFlags1'], 5, 1));
339 if ($flags & 2) {
340 $result= "1";
341 } else {
342 $result= "0";
343 }
345 return $result;
346 }
348 /* sets Reconnection value: from any client/from previous client only */
349 function setReConn ($checked)
350 {
351 $flag= substr($this->ctx['CtxCfgFlags1'], 5, 1);
353 if ($checked) {
354 $flag|= 2;
355 } else {
356 $flag&= 0xFD;
357 }
359 $this->ctx['CtxCfgFlags1'][5]= sprintf('%1X', $flag);
360 }
362 /* gets Inherit-config-from-client value: enabled/disabled */
363 function getInheritMode ()
364 {
365 if (substr($this->ctx['CtxCfgFlags1'], 6, 1) == "1") {
366 $result= true;
367 } else {
368 $result= false;
369 }
371 return $result;
372 }
374 /* sets Inherit-config-from-client value: enabled/disabled */
375 function setInheritMode ($checked)
376 {
377 if ($checked) {
378 $this->ctx['CtxCfgFlags1'][6]= "1";
379 } else {
380 $this->ctx['CtxCfgFlags1'][6]= "0";
381 }
382 }
384 /* gets shadow value (enum): 0-4
385 0: disabled
386 1: input on, notify on
387 2: input on, notify off
388 3: input off, notify on
389 4: input off, notify off
390 */
391 function getShadow ()
392 {
393 $result= substr($this->ctx['CtxCfgFlags1'], 1, 1);
394 return $result;
395 }
397 /* sets shadow value */
398 function setShadow ($checked, $value)
399 {
400 if ($checked) {
401 $this->ctx['CtxCfgFlags1'][1]= sprintf('%1X', $value);
402 }
403 }
405 /* gets connect-client-drive-at-logon value: enabled/disabled */
406 function getConnectClientDrives ()
407 {
408 $connections= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
409 if ($connections & 8) {
410 $result= true;
411 } else {
412 $result= false;
413 }
415 return $result;
416 }
418 /* sets connect-client-drive-at-logon value: enabled/disabled */
419 function setConnectClientDrives ($checked)
420 {
421 $flag= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
422 if ($checked) {
423 $flag|= 8;
424 } else {
425 $flag&= 0xF7;
426 }
428 $this->ctx['CtxCfgFlags1'][2]= sprintf('%1X', $flag);
429 }
431 /* gets connect-client-printers-at-logon value: enabled/disabled */
432 function getConnectClientPrinters ()
433 {
434 $connections= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
435 if ($connections & 4) {
436 $result= true;
437 } else {
438 $result= false;
439 }
441 return $result;
442 }
444 /* sets connect-client-printers-at-logon value: enabled/disabled */
445 function setConnectClientPrinters ($checked)
446 {
447 $flag= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
449 if ($checked) {
450 $flag|= 4;
451 } else {
452 $flag&= 0xFB;
453 }
455 $this->ctx['CtxCfgFlags1'][2]= sprintf('%1X', $flag);
456 }
458 /* gets set-client-printer-to-default value: enabled/disabled */
459 function getDefaultPrinter ()
460 {
461 $connections= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
462 if ($connections & 2) {
463 $result= true;
464 } else {
465 $result= false;
466 }
468 return $result;
469 }
471 /* sets set-client-printer-to-default value: enabled/disabled */
472 function setDefaultPrinter ($checked)
473 {
474 $flag= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
476 if ($checked) {
477 $flag|= 2;
478 } else {
479 $flag&= 0xFD;
480 }
482 $this->ctx['CtxCfgFlags1'][2]= sprintf('%1X', $flag);
483 }
485 /* SMARTY: gets the checkbox state of "Connection" */
486 function getCtxMaxConnectionTimeF ()
487 {
488 // Connection Time is 0 if disabled
489 if (isset($this->ctx['CtxMaxConnectionTime']) && ($this->ctx['CtxMaxConnectionTime'] != 0)) {
490 $result= true;
491 } else {
492 $result= false;
493 }
495 return $result;
496 }
498 /* SMARTY: sets the checkbox "Connection" to unchecked */
499 function setCtxMaxConnectionTimeF ($checked)
500 {
501 if ($checked) {
502 unset ($this->ctx['CtxMaxConnectionTime']);
503 }
504 }
506 /* SMARTY: gets the checkbox state of "Disconnection" */
507 function getCtxMaxDisconnectionTimeF ()
508 {
509 // Connection Time is 0 if disabled
510 if (isset($this->ctx['CtxMaxDisconnectionTime']) && ($this->ctx['CtxMaxDisconnectionTime'] != 0)) {
511 $result= true;
512 } else {
513 $result= false;
514 }
516 return $result;
517 }
519 /* SMARTY: sets the checkbox "Disconnection" to unchecked */
520 function setCtxMaxDisconnectionTimeF ($checked)
521 {
522 if ($checked) {
523 unset ($this->ctx['CtxMaxDisconnectionTime']);
524 }
525 }
527 /* SMARTY: gets the checkbox state of "Idle" */
528 function getCtxMaxIdleTimeF ()
529 {
530 // Connection Time is 0 if disabled
531 if (isset($this->ctx['CtxMaxIdleTime']) && ($this->ctx['CtxMaxIdleTime'] != 0)) {
532 $result= true;
533 } else {
534 $result= false;
535 }
537 return $result;
538 }
540 /* SMARTY: sets the checkbox "Idle" to unchecked */
541 function setCtxMaxIdleTimeF ($checked)
542 {
543 if ($checked) {
544 unset ($this->ctx['CtxMaxIdleTime']);
545 }
546 }
548 }
550 ?>