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");
31 /* This is the old header, it is needed to automatically convert old mungedDials to new ones */
32 define ("FILEHEADER_OLD",
33 "6d000800200020002000200020002000".
34 "20002000200020002000200020002000".
35 "20002000200020002000200064000100".
36 "20002000200020002000200020002000".
37 "20002000200020002000200020002000".
38 "20002000200020002000200020002000".
39 "50001000");
41 class sambaMungedDial
42 {
43 /* Terminal server variables (samba3) */
44 var $ctx= array(
45 'CtxCfgPresent' => '551e0bb0',
46 'CtxCfgFlags1' => '00e00010',
47 'CtxCallback' => '',
48 'CtxShadow' => '01000000',
49 'CtxMaxConnectionTime' => '',
50 'CtxMaxDisconnectionTime' => '',
51 'CtxMaxIdleTime' => '',
52 'CtxKeyboardLayout' => '',
53 'CtxMinEncryptionLevel' => '00',
54 'CtxWorkDirectory' => '',
55 'CtxNWLogonServer' => '',
56 'CtxWFHomeDir' => '',
57 'CtxWFHomeDirDrive' => '',
58 'CtxWFProfilePath' => '',
59 'CtxInitialProgram' => '',
60 'CtxCallbackNumber' => '');
62 /* attribute list for save action */
63 var $ctxattributes= array("CtxCfgPresent", "CtxCfgFlags1", "CtxCallback",
64 "CtxShadow", "CtxMaxConnectionTime", "CtxMaxDisconnectionTime",
65 "CtxMaxIdleTime", "CtxKeyboardLayout", "CtxMinEncryptionLevel",
66 "CtxWorkDirectory", "CtxNWLogonServer", "CtxWFHomeDir",
67 "CtxWFHomeDirDrive", "CtxWFProfilePath", "CtxInitialProgram",
68 "CtxCallbackNumber");
70 /* These parameters are treated as strings and get a trailing zero */
71 var $stringParams= array( "CtxWorkDirectory", "CtxNWLogonServer",
72 "CtxWFHomeDir", "CtxWFHomeDirDrive",
73 "CtxWFProfilePath", "CtxInitialProgram", "CtxCallbackNumber");
75 /* These parameters are treated as time values and get converted */
76 var $timeParams= array("CtxMaxConnectionTime", "CtxMaxDisconnectionTime", "CtxMaxIdleTime");
78 var $old_behavior= false;
80 function strhex($string)
81 {
82 $hex="";
84 for ($i=0; $i<strlen($string); $i++) {
85 $hex.= dechex(ord($string[$i]));
86 }
88 return ($hex);
89 }
91 function hexstr($hex)
92 {
93 $string="";
95 for ($i=0; $i<strlen($hex)-1; $i+=2) {
96 $string.= chr(hexdec($hex[$i].$hex[$i+1]));
97 }
99 return ($string);
100 }
102 function endian($src)
103 {
104 return (substr($src, 2, 2).substr($src, 0, 2));
105 }
107 function genTime ($minutes)
108 {
109 $usec= (int) ($minutes * 60 * 1000);
110 $src= sprintf('%04x%04x', $usec & 0x0FFFF, ($usec & 0x0FFFF0000) >> 16);
111 return (sambaMungedDial::endian(substr($src, 0, 4)).sambaMungedDial::endian(substr($src, 4, 4)));
112 }
114 function readTime ($time)
115 {
116 $lo= substr($time, 0, 4);
117 $hi= substr($time, 4, 4);
119 $usecs= (hexdec(substr($lo, 2, 2)) * 256 + hexdec(substr($lo, 0, 2))) +
120 (hexdec(substr($hi, 2 ,2)) * 256 + hexdec(substr($hi, 0, 2))) * 256 * 256;
122 return ((int)($usecs / (60 * 1000)));
123 }
125 function to8bit($string)
126 {
127 $result= "";
129 /* Strip zeros */
130 for ($i= 0; $i<strlen($string); $i++){
131 if ($string[$i] != chr(0)){
132 $result.= $string[$i];
133 }
134 }
136 return ($result);
137 }
139 function is_samba_path($path)
140 {
141 if ($path == ""){
142 return (TRUE);
143 }
145 if (!preg_match('/^[a-z0-9%\\\\_.:+-\\\\$]+$/i', $path)){
146 return (FALSE);
147 }
149 return preg_match ("/\\\\.+$/", $path);
150 }
152 /* Encode full MungedDial-String */
153 function encode_munged ($params)
154 {
155 /* Walk through the parameters and convert them */
156 $result= sambaMungedDial::hexstr(FILEHEADER);
158 // CHANGED: We need to insert the number of attributes right after FILEHEADER.
159 $counter= 0;
160 $result_tmp= "";
161 foreach ($params as $paramName => $paramValue) {
162 /* String parameter? */
163 if (in_array($paramName, $this->stringParams)){
164 $isString= TRUE;
165 $paramValue= sambaMungedDial::strhex($paramValue.chr(0).chr(0));
166 } else {
167 $isString= FALSE;
168 }
170 /* Time parameter? */
171 if (in_array($paramName, $this->timeParams)){
172 $paramValue= sambaMungedDial::genTime($paramValue);
173 }
175 $result_tmp.= sambaMungedDial::munge($paramName, $paramValue, $isString);
176 $counter++;
177 }
179 // First add the number of attributes
180 $result.= sambaMungedDial::hexstr(sprintf("%02x00", $counter));
182 // Then the usual stuff
183 $result.= $result_tmp;
185 return ($result);
186 }
188 /* Setup parameter given by paramName to MungedDial-Format */
189 function munge($paramName, $paramValue, $isString)
190 {
191 $result= "";
193 /* Encode paramName to UTF-16 */
194 if (function_exists("recode")){
195 $utfName= recode("ISO8859-15..UTF-16", $paramName);
196 } else {
197 $utfName= iconv("ISO8859-15", "UTF-16BE", $paramName);
198 }
200 /* Set parameter length, high and low byte */
201 $paramLen= strlen($utfName);
202 $result.= chr($paramLen & 0x0FF);
203 $result.= chr(($paramLen & 0x0FF00) >> 8);
205 /* String parameters have additional trailing bytes */
206 $valueLen= strlen($paramValue);
207 $result.= chr($valueLen & 0x0FF);
208 $result.= chr(($valueLen & 0x0FF00) >> 8);
210 /* Length fields have a trailing '01' appended by the UTF-16 converted name */
211 $result.= chr(1);
212 $result.= $utfName;
214 /* Parameter is padded with '00' */
215 $result.= chr(0);
216 $result.= $paramValue;
218 /* Append a trailing '00' to string parameters */
219 if ($isString && (strlen($paramValue) & 1)){
220 $result.= chr(0);
221 }
223 return ($result);
224 }
226 /* Takes a base64-encoded MungedDial-String and returns an array of included parameters and values */
227 function decode_munged($munge)
228 {
229 $result= array();
231 /*
232 * Remove base64 encoding and skip FILEHEADER.
233 * The '4' is added, because the FILEHEADER has been stripped by 4 chars.
234 * This is the number of attributes following - we don't need this at read time, only when writing.
235 */
236 if(substr(base64_decode($munge),0,2)=="6d") {
237 $this->old_behavior=true;
238 }
240 $ctxField="";
241 if($this->old_behavior==true) {
242 $ctxField= substr(base64_decode($munge), (strlen(FILEHEADER_OLD)) / 2);
243 } else {
244 $ctxField= substr(base64_decode($munge), (strlen(FILEHEADER)+4) / 2);
245 }
247 /* Decode parameters */
248 while ($ctxField!=""){
250 /* Read value lengths */
251 $ctxParmNameLength= ord($ctxField[0]) + 16 * ord($ctxField[1]);
252 $ctxParmLength= ord($ctxField[2]) + 16 * ord($ctxField[3]);
254 /* Reposition ctxField on start of parameter name, read parameter name */
255 $ctxField= substr($ctxField, 6);
256 $ctxParmName= sambaMungedDial::to8bit(substr($ctxField, 0, $ctxParmNameLength));
258 /* Reposition ctxField on start of parameter */
259 $ctxField= substr($ctxField, $ctxParmNameLength);
260 $ctxParm= substr($ctxField, 0, $ctxParmLength);
262 /* If string parameter, convert */
263 if (in_array($ctxParmName, $this->stringParams)){
264 $ctxParm= sambaMungedDial::hexstr($ctxParm);
265 }
266 /* If time parameter, convert */
267 if (in_array($ctxParmName, $this->timeParams)){
268 $ctxParm= sambaMungedDial::readTime($ctxParm);
269 }
271 /* Assign in result array */
272 $result[$ctxParmName]= trim($ctxParm);
274 /* Reposition ctxField on end of parameter and continue */
275 $ctxField= substr($ctxField, $ctxParmLength);
276 }
278 return ($result);
279 }
281 /* function takes a base64-encoded sambaMungedDial */
282 function load ($mungedDial)
283 {
284 $this->ctx= $this->decode_munged($mungedDial);
285 }
287 /* Returns ready-to-run mungedDialString to be filled into ldap */
288 function getMunged ()
289 {
290 // Do extra check for valid timeParams (they must be set to 0 if disabled)
291 foreach($this->timeParams as $value) {
292 if(!isset($this->ctx[$value])) {
293 $this->ctx[$value]= 0;
294 }
295 }
296 $result= base64_encode($this->encode_munged($this->ctx));
298 return $result;
299 }
301 /* Returns array of flags, which can be set on-demand with activated java-script */
302 function getOnDemandFlags ()
303 {
304 $result= array();
305 if ($_SESSION["js"]){
306 foreach ($this->timeParams as $value) {
307 if (!isset($this->ctx[$value]) || (isset($this->ctx[$value]) && $this->ctx[$value] == 0)) {
308 $result[$value."Mode"]= "disabled";
309 } else {
310 $result[$value."Mode"]= "";
311 }
312 }
314 if (substr($this->ctx['CtxCfgFlags1'], 6, 1) == "1") {
315 $result['CtxInitialProgramMode'] = "disabled";
316 } else {
317 $result['CtxInitialProgramMode'] = "";
318 }
319 }else{
320 foreach ($this->timeParams as $value) {
321 $result[$value."Mode"]= "";
322 }
324 $result['CtxInitialProgramMode'] = "";
326 }
328 return $result;
329 }
331 /*Gets Terminal-Server-Login value: enabled/disabled */
332 function getTsLogin ()
333 {
334 $flags= ord(substr($this->ctx['CtxCfgFlags1'], 5, 1));
336 if ($flags & 1) {
337 $result= false;
338 } else {
339 $result= true;
340 }
342 return $result;
343 }
345 /* Sets Terminal-Server-Login value: enabled/disabled */
346 function setTsLogin ($checked)
347 {
348 $flag= substr($this->ctx['CtxCfgFlags1'], 5, 1);
350 if ($checked) {
351 $flag|= 1;
352 } else {
353 $flag&= 0xFE;
354 }
356 $this->ctx['CtxCfgFlags1'][5]= sprintf('%1x', $flag);
357 }
359 /* gets Broken-Connection value: disconnect/reset */
360 function getBrokenConn ()
361 {
362 $flags= ord(substr($this->ctx['CtxCfgFlags1'], 5, 1));
363 if ($flags & 4) {
364 $result= "1";
365 } else {
366 $result= "0";
367 }
369 return $result;
370 }
372 /* sets Broken-Connection value: disconnect/reset */
373 function setBrokenConn ($checked)
374 {
375 $flag= substr($this->ctx['CtxCfgFlags1'], 5, 1);
377 if ($checked) {
378 $flag|= 4;
379 } else {
380 $flag&= 0xFB;
381 }
383 $this->ctx['CtxCfgFlags1'][5]= sprintf('%1x', $flag);
384 }
386 /* gets Reconnection value: from any client/from previous client only */
387 function getReConn ()
388 {
389 $flags= ord(substr($this->ctx['CtxCfgFlags1'], 5, 1));
390 if ($flags & 2) {
391 $result= "1";
392 } else {
393 $result= "0";
394 }
396 return $result;
397 }
399 /* sets Reconnection value: from any client/from previous client only */
400 function setReConn ($checked)
401 {
402 $flag= substr($this->ctx['CtxCfgFlags1'], 5, 1);
404 if ($checked) {
405 $flag|= 2;
406 } else {
407 $flag&= 0xFD;
408 }
410 $this->ctx['CtxCfgFlags1'][5]= sprintf('%1x', $flag);
411 }
413 /* gets Inherit-config-from-client value: enabled/disabled */
414 function getInheritMode ()
415 {
416 if (substr($this->ctx['CtxCfgFlags1'], 6, 1) == "1") {
417 $result= true;
418 } else {
419 $result= false;
420 }
422 return $result;
423 }
425 /* sets Inherit-config-from-client value: enabled/disabled */
426 function setInheritMode ($checked)
427 {
428 if ($checked) {
429 $this->ctx['CtxCfgFlags1'][6]= "1";
430 } else {
431 $this->ctx['CtxCfgFlags1'][6]= "0";
432 }
433 }
435 /* gets shadow value (enum): 0-4
436 0: disabled
437 1: input on, notify on
438 2: input on, notify off
439 3: input off, notify on
440 4: input off, notify off
441 */
442 function getShadow ()
443 {
444 if($this->old_behavior==true) {
445 $result= substr($this->ctx['CtxCfgFlags1'], 1, 1);
446 } else {
447 $result= substr($this->ctx['CtxShadow'], 1, 1);
448 }
449 return $result;
450 }
452 /* sets shadow value */
453 function setShadow ($checked, $value)
454 {
455 if ($checked) {
456 if($this->old_behavior==true) {
457 // We need to reset the old setting
458 $this->ctx['CtxCfgFlags1'][1]= sprintf('%1X', $value);
459 }
460 $this->ctx['CtxShadow'][1]= sprintf('%1x', $value);
461 }
462 }
464 /* gets connect-client-drive-at-logon value: enabled/disabled */
465 function getConnectClientDrives ()
466 {
467 $connections= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
468 if ($connections & 8) {
469 $result= true;
470 } else {
471 $result= false;
472 }
474 return $result;
475 }
477 /* sets connect-client-drive-at-logon value: enabled/disabled */
478 function setConnectClientDrives ($checked)
479 {
480 $flag= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
481 if ($checked) {
482 $flag|= 8;
483 } else {
484 $flag&= 0xF7;
485 }
487 $this->ctx['CtxCfgFlags1'][2]= sprintf('%1x', $flag);
488 }
490 /* gets connect-client-printers-at-logon value: enabled/disabled */
491 function getConnectClientPrinters ()
492 {
493 $connections= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
494 if ($connections & 4) {
495 $result= true;
496 } else {
497 $result= false;
498 }
500 return $result;
501 }
503 /* sets connect-client-printers-at-logon value: enabled/disabled */
504 function setConnectClientPrinters ($checked)
505 {
506 $flag= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
508 if ($checked) {
509 $flag|= 4;
510 } else {
511 $flag&= 0xFB;
512 }
514 $this->ctx['CtxCfgFlags1'][2]= sprintf('%1x', $flag);
515 }
517 /* gets set-client-printer-to-default value: enabled/disabled */
518 function getDefaultPrinter ()
519 {
520 $connections= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
521 if ($connections & 2) {
522 $result= true;
523 } else {
524 $result= false;
525 }
527 return $result;
528 }
530 /* sets set-client-printer-to-default value: enabled/disabled */
531 function setDefaultPrinter ($checked)
532 {
533 $flag= hexdec(substr($this->ctx['CtxCfgFlags1'], 2, 1));
535 if ($checked) {
536 $flag|= 2;
537 } else {
538 $flag&= 0xFD;
539 }
541 $this->ctx['CtxCfgFlags1'][2]= sprintf('%1x', $flag);
542 }
544 /* SMARTY: gets the checkbox state of "Connection" */
545 function getCtxMaxConnectionTimeF ()
546 {
547 // Connection Time is 0 if disabled
548 if (isset($this->ctx['CtxMaxConnectionTime']) && ($this->ctx['CtxMaxConnectionTime'] != 0)) {
549 $result= true;
550 } else {
551 $result= false;
552 }
554 return $result;
555 }
557 /* SMARTY: sets the checkbox "Connection" to unchecked */
558 function setCtxMaxConnectionTimeF ($checked)
559 {
560 if ($checked) {
561 unset ($this->ctx['CtxMaxConnectionTime']);
562 }
563 }
565 /* SMARTY: gets the checkbox state of "Disconnection" */
566 function getCtxMaxDisconnectionTimeF ()
567 {
568 // Connection Time is 0 if disabled
569 if (isset($this->ctx['CtxMaxDisconnectionTime']) && ($this->ctx['CtxMaxDisconnectionTime'] != 0)) {
570 $result= true;
571 } else {
572 $result= false;
573 }
575 return $result;
576 }
578 /* SMARTY: sets the checkbox "Disconnection" to unchecked */
579 function setCtxMaxDisconnectionTimeF ($checked)
580 {
581 if ($checked) {
582 unset ($this->ctx['CtxMaxDisconnectionTime']);
583 }
584 }
586 /* SMARTY: gets the checkbox state of "Idle" */
587 function getCtxMaxIdleTimeF ()
588 {
589 // Connection Time is 0 if disabled
590 if (isset($this->ctx['CtxMaxIdleTime']) && ($this->ctx['CtxMaxIdleTime'] != 0)) {
591 $result= true;
592 } else {
593 $result= false;
594 }
596 return $result;
597 }
599 /* SMARTY: sets the checkbox "Idle" to unchecked */
600 function setCtxMaxIdleTimeF ($checked)
601 {
602 if ($checked) {
603 unset ($this->ctx['CtxMaxIdleTime']);
604 }
605 }
606 }
608 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
609 ?>