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",
24 "20002000200020002000200020002000".
25 "20002000200020002000200020002000".
26 "20002000200020002000200020002000".
27 "20002000200020002000200020002000".
28 "20002000200020002000200020002000".
29 "20002000200020002000200020002000".
30 "5000");
32 class sambaMungedDial
33 {
34 /* Terminal server variables (samba3) */
35 var $ctx= array(
36 'CtxCfgPresent' => '551e0bb0',
37 'CtxCfgFlags1' => '00e00010',
38 'CtxCallback' => '',
39 'CtxShadow' => '01000000',
40 'CtxMaxConnectionTime' => '',
41 'CtxMaxDisconnectionTime' => '',
42 'CtxMaxIdleTime' => '',
43 'CtxKeyboardLayout' => '',
44 'CtxMinEncryptionLevel' => '00',
45 'CtxWorkDirectory' => '',
46 'CtxNWLogonServer' => '',
47 'CtxWFHomeDir' => '',
48 'CtxWFHomeDirDrive' => '',
49 'CtxWFProfilePath' => '',
50 'CtxInitialProgram' => '',
51 'CtxCallbackNumber' => '');
53 /* attribute list for save action */
54 var $ctxattributes= array("CtxCfgPresent", "CtxCfgFlags1", "CtxCallback",
55 "CtxShadow", "CtxMaxConnectionTime", "CtxMaxDisconnectionTime",
56 "CtxMaxIdleTime", "CtxKeyboardLayout", "CtxMinEncryptionLevel",
57 "CtxWorkDirectory", "CtxNWLogonServer", "CtxWFHomeDir",
58 "CtxWFHomeDirDrive", "CtxWFProfilePath", "CtxInitialProgram",
59 "CtxCallbackNumber");
61 /* These parameters are treated as strings and get a trailing zero */
62 var $stringParams= array( "CtxWorkDirectory", "CtxNWLogonServer",
63 "CtxWFHomeDir", "CtxWFHomeDirDrive",
64 "CtxWFProfilePath", "CtxInitialProgram", "CtxCallbackNumber");
66 /* These parameters are treated as time values and get converted */
67 var $timeParams= array("CtxMaxConnectionTime", "CtxMaxDisconnectionTime", "CtxMaxIdleTime");
70 function strhex($string)
71 {
72 $hex="";
74 for ($i=0; $i<strlen($string); $i++) {
75 $hex.= dechex(ord($string[$i]));
76 }
78 return ($hex);
79 }
81 function hexstr($hex)
82 {
83 $string="";
85 for ($i=0; $i<strlen($hex)-1; $i+=2) {
86 $string.= chr(hexdec($hex[$i].$hex[$i+1]));
87 }
89 return ($string);
90 }
92 function endian($src)
93 {
94 return (substr($src, 2, 2).substr($src, 0, 2));
95 }
97 function genTime ($minutes)
98 {
99 $usec= (int) ($minutes * 60 * 1000);
100 $src= sprintf('%04x%04x', $usec & 0x0FFFF, ($usec & 0x0FFFF0000) >> 16);
101 return (sambaMungedDial::endian(substr($src, 0, 4)).sambaMungedDial::endian(substr($src, 4, 4)));
102 }
104 function readTime ($time)
105 {
106 $lo= substr($time, 0, 4);
107 $hi= substr($time, 4, 4);
109 $usecs= (hexdec(substr($lo, 2, 2)) * 256 + hexdec(substr($lo, 0, 2))) +
110 (hexdec(substr($hi, 2 ,2)) * 256 + hexdec(substr($hi, 0, 2))) * 256 * 256;
112 return ((int)($usecs / (60 * 1000)));
113 }
115 function to8bit($string)
116 {
117 $result= "";
119 /* Strip zeros */
120 for ($i= 0; $i<strlen($string); $i++){
121 if ($string[$i] != chr(0)){
122 $result.= $string[$i];
123 }
124 }
126 return ($result);
127 }
129 function is_samba_path($path)
130 {
131 if ($path == ""){
132 return (TRUE);
133 }
135 if (!preg_match('/^[a-z0-9%\\\\_.:+-\\\\$]+$/i', $path)){
136 return (FALSE);
137 }
139 return preg_match ("/\\\\.+$/", $path);
140 }
142 /* Encode full MungedDial-String */
143 function encode_munged ($params)
144 {
145 /* Walk through the parameters and convert them */
146 $result= sambaMungedDial::hexstr(FILEHEADER);
148 // CHANGED: We need to insert the number of attributes right after FILEHEADER.
149 $counter= 0;
150 $result_tmp= "";
151 foreach ($params as $paramName => $paramValue) {
152 /* String parameter? */
153 if (in_array($paramName, $this->stringParams)){
154 $isString= TRUE;
155 $paramValue= sambaMungedDial::strhex($paramValue.chr(0).chr(0));
156 } else {
157 $isString= FALSE;
158 }
160 /* Time parameter? */
161 if (in_array($paramName, $this->timeParams)){
162 $paramValue= sambaMungedDial::genTime($paramValue);
163 }
165 $result_tmp.= sambaMungedDial::munge($paramName, $paramValue, $isString);
166 $counter++;
167 }
169 // First add the number of attributes
170 $result.= sambaMungedDial::hexstr(sprintf("%02x00", $counter));
172 // Then the usual stuff
173 $result.= $result_tmp;
175 return ($result);
176 }
178 /* Setup parameter given by paramName to MungedDial-Format */
179 function munge($paramName, $paramValue, $isString)
180 {
181 $result= "";
183 /* Encode paramName to UTF-16 */
184 if (function_exists("recode")){
185 $utfName= recode("ISO8859-15..UTF-16", $paramName);
186 } else {
187 $utfName= iconv("ISO8859-15", "UTF-16BE", $paramName);
188 }
190 /* Set parameter length, high and low byte */
191 $paramLen= strlen($utfName);
192 $result.= chr($paramLen & 0x0FF);
193 $result.= chr(($paramLen & 0x0FF00) >> 8);
195 /* String parameters have additional trailing bytes */
196 $valueLen= strlen($paramValue);
197 $result.= chr($valueLen & 0x0FF);
198 $result.= chr(($valueLen & 0x0FF00) >> 8);
200 /* Length fields have a trailing '01' appended by the UTF-16 converted name */
201 $result.= chr(1);
202 $result.= $utfName;
204 /* Parameter is padded with '00' */
205 $result.= chr(0);
206 $result.= $paramValue;
208 /* Append a trailing '00' to string parameters */
209 if ($isString && (strlen($paramValue) & 1)){
210 $result.= chr(0);
211 }
213 return ($result);
214 }
216 /* Takes a base64-encoded MungedDial-String and returns an array of included parameters and values */
217 function decode_munged($munge)
218 {
219 $result= array();
221 /*
222 * Remove base64 encoding and skip FILEHEADER.
223 * The '4' is added, because the FILEHEADER has been stripped by 4 chars.
224 * These is the number of attributes follow - we don't need this now - only when writing.
225 */
226 $ctxField= substr(base64_decode($munge), (strlen(FILEHEADER)+4) / 2);
228 /* Decode parameters */
229 while ($ctxField!=""){
231 /* Read value lengths */
232 $ctxParmNameLength= ord($ctxField[0]) + 16 * ord($ctxField[1]);
233 $ctxParmLength= ord($ctxField[2]) + 16 * ord($ctxField[3]);
235 /* Reposition ctxField on start of parameter name, read parameter name */
236 $ctxField= substr($ctxField, 6);
237 $ctxParmName= sambaMungedDial::to8bit(substr($ctxField, 0, $ctxParmNameLength));
239 /* Reposition ctxField on start of parameter */
240 $ctxField= substr($ctxField, $ctxParmNameLength);
241 $ctxParm= substr($ctxField, 0, $ctxParmLength);
243 /* If string parameter, convert */
244 if (in_array($ctxParmName, $this->stringParams)){
245 $ctxParm= sambaMungedDial::hexstr($ctxParm);
246 }
247 /* If time parameter, convert */
248 if (in_array($ctxParmName, $this->timeParams)){
249 $ctxParm= sambaMungedDial::readTime($ctxParm);
250 }
252 /* Assign in result array */
253 $result[$ctxParmName]= trim($ctxParm);
255 /* Reposition ctxField on end of parameter and continue */
256 $ctxField= substr($ctxField, $ctxParmLength);
257 }
259 return ($result);
260 }
262 /* function takes a base64-encoded sambaMungedDial */
263 function load ($mungedDial)
264 {
265 $this->ctx= $this->decode_munged($mungedDial);
266 }
268 /* Returns ready-to-run mungedDialString to be filled into ldap */
269 function getMunged ()
270 {
271 // Do extra check for valid timeParams (they must be set to 0 if disabled)
272 foreach($this->timeParams as $value) {
273 if(!isset($this->ctx[$value])) {
274 $this->ctx[$value]= 0;
275 }
276 }
277 $result= base64_encode($this->encode_munged($this->ctx));
279 return $result;
280 }
282 /* Returns array of flags, which can be set on-demand with activated java-script */
283 function getOnDemandFlags ()
284 {
285 $result= array();
286 if ($_SESSION["js"]){
287 foreach ($this->timeParams as $value) {
288 if (!isset($this->ctx[$value]) || (isset($this->ctx[$value]) && $this->ctx[$value] == 0)) {
289 $result[$value."Mode"]= "disabled";
290 } else {
291 $result[$value."Mode"]= "";
292 }
293 }
295 if (substr($this->ctx['CtxCfgFlags1'], 6, 1) == "1") {
296 $result['CtxInitialProgramMode'] = "disabled";
297 } else {
298 $result['CtxInitialProgramMode'] = "";
299 }
300 }else{
301 foreach ($this->timeParams as $value) {
302 $result[$value."Mode"]= "";
303 }
305 $result['CtxInitialProgramMode'] = "";
307 }
309 return $result;
310 }
312 /*Gets Terminal-Server-Login value: enabled/disabled */
313 function getTsLogin ()
314 {
315 $flags= ord(substr($this->ctx['CtxCfgFlags1'], 5, 1));
317 if ($flags & 1) {
318 $result= false;
319 } else {
320 $result= true;
321 }
323 return $result;
324 }
326 /* Sets Terminal-Server-Login value: enabled/disabled */
327 function setTsLogin ($checked)
328 {
329 $flag= substr($this->ctx['CtxCfgFlags1'], 5, 1);
331 if ($checked) {
332 $flag|= 1;
333 } else {
334 $flag&= 0xFE;
335 }
337 $this->ctx['CtxCfgFlags1'][5]= sprintf('%1x', $flag);
338 }
340 /* gets Broken-Connection value: disconnect/reset */
341 function getBrokenConn ()
342 {
343 $flags= ord(substr($this->ctx['CtxCfgFlags1'], 5, 1));
344 if ($flags & 4) {
345 $result= "1";
346 } else {
347 $result= "0";
348 }
350 return $result;
351 }
353 /* sets Broken-Connection value: disconnect/reset */
354 function setBrokenConn ($checked)
355 {
356 $flag= substr($this->ctx['CtxCfgFlags1'], 5, 1);
358 if ($checked) {
359 $flag|= 4;
360 } else {
361 $flag&= 0xFB;
362 }
364 $this->ctx['CtxCfgFlags1'][5]= sprintf('%1x', $flag);
365 }
367 /* gets Reconnection value: from any client/from previous client only */
368 function getReConn ()
369 {
370 $flags= ord(substr($this->ctx['CtxCfgFlags1'], 5, 1));
371 if ($flags & 2) {
372 $result= "1";
373 } else {
374 $result= "0";
375 }
377 return $result;
378 }
380 /* sets Reconnection value: from any client/from previous client only */
381 function setReConn ($checked)
382 {
383 $flag= substr($this->ctx['CtxCfgFlags1'], 5, 1);
385 if ($checked) {
386 $flag|= 2;
387 } else {
388 $flag&= 0xFD;
389 }
391 $this->ctx['CtxCfgFlags1'][5]= sprintf('%1x', $flag);
392 }
394 /* gets Inherit-config-from-client value: enabled/disabled */
395 function getInheritMode ()
396 {
397 if (substr($this->ctx['CtxCfgFlags1'], 6, 1) == "1") {
398 $result= true;
399 } else {
400 $result= false;
401 }
403 return $result;
404 }
406 /* sets Inherit-config-from-client value: enabled/disabled */
407 function setInheritMode ($checked)
408 {
409 if ($checked) {
410 $this->ctx['CtxCfgFlags1'][6]= "1";
411 } else {
412 $this->ctx['CtxCfgFlags1'][6]= "0";
413 }
414 }
416 /* gets shadow value (enum): 0-4
417 0: disabled
418 1: input on, notify on
419 2: input on, notify off
420 3: input off, notify on
421 4: input off, notify off
422 */
423 function getShadow ()
424 {
425 $result= substr($this->ctx['CtxShadow'], 1, 1);
426 return $result;
427 }
429 /* sets shadow value */
430 function setShadow ($checked, $value)
431 {
432 if ($checked) {
433 $this->ctx['CtxShadow'][1]= sprintf('%1x', $value);
434 }
435 }
437 /* gets connect-client-drive-at-logon value: enabled/disabled */
438 function getConnectClientDrives ()
439 {
440 $connections= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
441 if ($connections & 8) {
442 $result= true;
443 } else {
444 $result= false;
445 }
447 return $result;
448 }
450 /* sets connect-client-drive-at-logon value: enabled/disabled */
451 function setConnectClientDrives ($checked)
452 {
453 $flag= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
454 if ($checked) {
455 $flag|= 8;
456 } else {
457 $flag&= 0xF7;
458 }
460 $this->ctx['CtxCfgFlags1'][2]= sprintf('%1x', $flag);
461 }
463 /* gets connect-client-printers-at-logon value: enabled/disabled */
464 function getConnectClientPrinters ()
465 {
466 $connections= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
467 if ($connections & 4) {
468 $result= true;
469 } else {
470 $result= false;
471 }
473 return $result;
474 }
476 /* sets connect-client-printers-at-logon value: enabled/disabled */
477 function setConnectClientPrinters ($checked)
478 {
479 $flag= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
481 if ($checked) {
482 $flag|= 4;
483 } else {
484 $flag&= 0xFB;
485 }
487 $this->ctx['CtxCfgFlags1'][2]= sprintf('%1x', $flag);
488 }
490 /* gets set-client-printer-to-default value: enabled/disabled */
491 function getDefaultPrinter ()
492 {
493 $connections= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
494 if ($connections & 2) {
495 $result= true;
496 } else {
497 $result= false;
498 }
500 return $result;
501 }
503 /* sets set-client-printer-to-default value: enabled/disabled */
504 function setDefaultPrinter ($checked)
505 {
506 $flag= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
508 if ($checked) {
509 $flag|= 2;
510 } else {
511 $flag&= 0xFD;
512 }
514 $this->ctx['CtxCfgFlags1'][2]= sprintf('%1x', $flag);
515 }
517 /* SMARTY: gets the checkbox state of "Connection" */
518 function getCtxMaxConnectionTimeF ()
519 {
520 // Connection Time is 0 if disabled
521 if (isset($this->ctx['CtxMaxConnectionTime']) && ($this->ctx['CtxMaxConnectionTime'] != 0)) {
522 $result= true;
523 } else {
524 $result= false;
525 }
527 return $result;
528 }
530 /* SMARTY: sets the checkbox "Connection" to unchecked */
531 function setCtxMaxConnectionTimeF ($checked)
532 {
533 if ($checked) {
534 unset ($this->ctx['CtxMaxConnectionTime']);
535 }
536 }
538 /* SMARTY: gets the checkbox state of "Disconnection" */
539 function getCtxMaxDisconnectionTimeF ()
540 {
541 // Connection Time is 0 if disabled
542 if (isset($this->ctx['CtxMaxDisconnectionTime']) && ($this->ctx['CtxMaxDisconnectionTime'] != 0)) {
543 $result= true;
544 } else {
545 $result= false;
546 }
548 return $result;
549 }
551 /* SMARTY: sets the checkbox "Disconnection" to unchecked */
552 function setCtxMaxDisconnectionTimeF ($checked)
553 {
554 if ($checked) {
555 unset ($this->ctx['CtxMaxDisconnectionTime']);
556 }
557 }
559 /* SMARTY: gets the checkbox state of "Idle" */
560 function getCtxMaxIdleTimeF ()
561 {
562 // Connection Time is 0 if disabled
563 if (isset($this->ctx['CtxMaxIdleTime']) && ($this->ctx['CtxMaxIdleTime'] != 0)) {
564 $result= true;
565 } else {
566 $result= false;
567 }
569 return $result;
570 }
572 /* SMARTY: sets the checkbox "Idle" to unchecked */
573 function setCtxMaxIdleTimeF ($checked)
574 {
575 if ($checked) {
576 unset ($this->ctx['CtxMaxIdleTime']);
577 }
578 }
579 }
581 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
582 ?>