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 }else{
276 foreach ($this->timeParams as $value) {
277 $result[$value."Mode"]= "";
278 }
280 $result['CtxInitialProgramMode'] = "";
282 }
284 return $result;
285 }
287 /*Gets Terminal-Server-Login value: enabled/disabled */
288 function getTsLogin ()
289 {
290 $flags= ord(substr($this->ctx['CtxCfgFlags1'], 5, 1));
292 if ($flags & 1) {
293 $result= false;
294 } else {
295 $result= true;
296 }
298 return $result;
299 }
301 /* Sets Terminal-Server-Login value: enabled/disabled */
302 function setTsLogin ($checked)
303 {
304 $flag= substr($this->ctx['CtxCfgFlags1'], 5, 1);
306 if ($checked) {
307 $flag|= 1;
308 } else {
309 $flag&= 0xFE;
310 }
312 $this->ctx['CtxCfgFlags1'][5]= sprintf('%1X', $flag);
313 }
315 /* gets Broken-Connection value: disconnect/reset */
316 function getBrokenConn ()
317 {
318 $flags= ord(substr($this->ctx['CtxCfgFlags1'], 5, 1));
319 if ($flags & 4) {
320 $result= "1";
321 } else {
322 $result= "0";
323 }
325 return $result;
326 }
328 /* sets Broken-Connection value: disconnect/reset */
329 function setBrokenConn ($checked)
330 {
331 $flag= substr($this->ctx['CtxCfgFlags1'], 5, 1);
333 if ($checked) {
334 $flag|= 4;
335 } else {
336 $flag&= 0xFB;
337 }
339 $this->ctx['CtxCfgFlags1'][5]= sprintf('%1X', $flag);
340 }
342 /* gets Reconnection value: from any client/from previous client only */
343 function getReConn ()
344 {
345 $flags= ord(substr($this->ctx['CtxCfgFlags1'], 5, 1));
346 if ($flags & 2) {
347 $result= "1";
348 } else {
349 $result= "0";
350 }
352 return $result;
353 }
355 /* sets Reconnection value: from any client/from previous client only */
356 function setReConn ($checked)
357 {
358 $flag= substr($this->ctx['CtxCfgFlags1'], 5, 1);
360 if ($checked) {
361 $flag|= 2;
362 } else {
363 $flag&= 0xFD;
364 }
366 $this->ctx['CtxCfgFlags1'][5]= sprintf('%1X', $flag);
367 }
369 /* gets Inherit-config-from-client value: enabled/disabled */
370 function getInheritMode ()
371 {
372 if (substr($this->ctx['CtxCfgFlags1'], 6, 1) == "1") {
373 $result= true;
374 } else {
375 $result= false;
376 }
378 return $result;
379 }
381 /* sets Inherit-config-from-client value: enabled/disabled */
382 function setInheritMode ($checked)
383 {
384 if ($checked) {
385 $this->ctx['CtxCfgFlags1'][6]= "1";
386 } else {
387 $this->ctx['CtxCfgFlags1'][6]= "0";
388 }
389 }
391 /* gets shadow value (enum): 0-4
392 0: disabled
393 1: input on, notify on
394 2: input on, notify off
395 3: input off, notify on
396 4: input off, notify off
397 */
398 function getShadow ()
399 {
400 $result= substr($this->ctx['CtxCfgFlags1'], 1, 1);
401 return $result;
402 }
404 /* sets shadow value */
405 function setShadow ($checked, $value)
406 {
407 if ($checked) {
408 $this->ctx['CtxCfgFlags1'][1]= sprintf('%1X', $value);
409 }
410 }
412 /* gets connect-client-drive-at-logon value: enabled/disabled */
413 function getConnectClientDrives ()
414 {
415 $connections= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
416 if ($connections & 8) {
417 $result= true;
418 } else {
419 $result= false;
420 }
422 return $result;
423 }
425 /* sets connect-client-drive-at-logon value: enabled/disabled */
426 function setConnectClientDrives ($checked)
427 {
428 $flag= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
429 if ($checked) {
430 $flag|= 8;
431 } else {
432 $flag&= 0xF7;
433 }
435 $this->ctx['CtxCfgFlags1'][2]= sprintf('%1X', $flag);
436 }
438 /* gets connect-client-printers-at-logon value: enabled/disabled */
439 function getConnectClientPrinters ()
440 {
441 $connections= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
442 if ($connections & 4) {
443 $result= true;
444 } else {
445 $result= false;
446 }
448 return $result;
449 }
451 /* sets connect-client-printers-at-logon value: enabled/disabled */
452 function setConnectClientPrinters ($checked)
453 {
454 $flag= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
456 if ($checked) {
457 $flag|= 4;
458 } else {
459 $flag&= 0xFB;
460 }
462 $this->ctx['CtxCfgFlags1'][2]= sprintf('%1X', $flag);
463 }
465 /* gets set-client-printer-to-default value: enabled/disabled */
466 function getDefaultPrinter ()
467 {
468 $connections= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
469 if ($connections & 2) {
470 $result= true;
471 } else {
472 $result= false;
473 }
475 return $result;
476 }
478 /* sets set-client-printer-to-default value: enabled/disabled */
479 function setDefaultPrinter ($checked)
480 {
481 $flag= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
483 if ($checked) {
484 $flag|= 2;
485 } else {
486 $flag&= 0xFD;
487 }
489 $this->ctx['CtxCfgFlags1'][2]= sprintf('%1X', $flag);
490 }
492 /* SMARTY: gets the checkbox state of "Connection" */
493 function getCtxMaxConnectionTimeF ()
494 {
495 // Connection Time is 0 if disabled
496 if (isset($this->ctx['CtxMaxConnectionTime']) && ($this->ctx['CtxMaxConnectionTime'] != 0)) {
497 $result= true;
498 } else {
499 $result= false;
500 }
502 return $result;
503 }
505 /* SMARTY: sets the checkbox "Connection" to unchecked */
506 function setCtxMaxConnectionTimeF ($checked)
507 {
508 if ($checked) {
509 unset ($this->ctx['CtxMaxConnectionTime']);
510 }
511 }
513 /* SMARTY: gets the checkbox state of "Disconnection" */
514 function getCtxMaxDisconnectionTimeF ()
515 {
516 // Connection Time is 0 if disabled
517 if (isset($this->ctx['CtxMaxDisconnectionTime']) && ($this->ctx['CtxMaxDisconnectionTime'] != 0)) {
518 $result= true;
519 } else {
520 $result= false;
521 }
523 return $result;
524 }
526 /* SMARTY: sets the checkbox "Disconnection" to unchecked */
527 function setCtxMaxDisconnectionTimeF ($checked)
528 {
529 if ($checked) {
530 unset ($this->ctx['CtxMaxDisconnectionTime']);
531 }
532 }
534 /* SMARTY: gets the checkbox state of "Idle" */
535 function getCtxMaxIdleTimeF ()
536 {
537 // Connection Time is 0 if disabled
538 if (isset($this->ctx['CtxMaxIdleTime']) && ($this->ctx['CtxMaxIdleTime'] != 0)) {
539 $result= true;
540 } else {
541 $result= false;
542 }
544 return $result;
545 }
547 /* SMARTY: sets the checkbox "Idle" to unchecked */
548 function setCtxMaxIdleTimeF ($checked)
549 {
550 if ($checked) {
551 unset ($this->ctx['CtxMaxIdleTime']);
552 }
553 }
555 }
557 ?>