1: <?php
2:
3: class ActiveDirectoryUtils {
4:
5:
6: const UAC_SCRIPT = 1;
7: const UAC_ACCOUNTDISABLE = 2;
8: const UAC_HOMEDIR_REQUIRED = 8;
9: const UAC_LOCKOUT = 16;
10: const UAC_PASSWD_NOTREQD = 32;
11: const UAC_ENCRYPTED_TEXT_PWD_ALLOWED = 128;
12: const UAC_TEMP_DUPLICATE_ACCOUNT = 256;
13: const UAC_NORMAL_ACCOUNT = 512;
14: const UAC_INTERDOMAIN_TRUST_ACCOUNT = 2048;
15: const UAC_WORKSTATION_TRUST_ACCOUNT = 4096;
16: const UAC_SERVER_TRUST_ACCOUNT = 8192;
17: const UAC_DONT_EXPIRE_PASSWORD = 65536;
18: const UAC_MNS_LOGON_ACCOUNT = 131072;
19: const UAC_SMARTCARD_REQUIRED = 262144;
20: const UAC_TRUSTED_FOR_DELEGATION = 524288;
21: const UAC_NOT_DELEGATED = 1048576;
22: const UAC_USE_DES_KEY_ONLY = 2097152;
23: const UAC_DONT_REQ_PREAUTH = 4194304;
24: const UAC_PASSWORD_EXPIRED = 8388608;
25: const UAC_TRUSTED_TO_AUTH_FOR_DELEGATION = 16777216;
26:
27: public static function encodePassword($password) {
28: $newPassword = '';
29: $plainPassword = "\"" . $password . "\"";
30: $len = strlen($plainPassword);
31: for ($i = 0; $i < $len; $i++) {
32: $newPassword .= "{$plainPassword{$i}}\000";
33: }
34: return $newPassword;
35: }
36:
37: private static function _getUserAccountControlFlags() {
38: return array(
39: self::UAC_SCRIPT,
40: self::UAC_ACCOUNTDISABLE,
41: self::UAC_HOMEDIR_REQUIRED,
42: self::UAC_LOCKOUT,
43: self::UAC_PASSWD_NOTREQD,
44: self::UAC_ENCRYPTED_TEXT_PWD_ALLOWED,
45: self::UAC_TEMP_DUPLICATE_ACCOUNT,
46: self::UAC_NORMAL_ACCOUNT,
47: self::UAC_INTERDOMAIN_TRUST_ACCOUNT,
48: self::UAC_WORKSTATION_TRUST_ACCOUNT,
49: self::UAC_SERVER_TRUST_ACCOUNT,
50: self::UAC_DONT_EXPIRE_PASSWORD,
51: self::UAC_MNS_LOGON_ACCOUNT,
52: self::UAC_SMARTCARD_REQUIRED,
53: self::UAC_TRUSTED_FOR_DELEGATION,
54: self::UAC_NOT_DELEGATED,
55: self::UAC_USE_DES_KEY_ONLY,
56: self::UAC_DONT_REQ_PREAUTH,
57: self::UAC_PASSWORD_EXPIRED,
58: self::UAC_TRUSTED_TO_AUTH_FOR_DELEGATION,
59: );
60: }
61:
62: 63: 64: 65: 66: 67:
68: public static function encodeUserAccountControl($options) {
69: if (!is_array($options)) {
70: throw new InvalidArgumentException("options argument is not a array: " . $options);
71: }
72:
73: $value = 0;
74: foreach ($options as $flag => $insert) {
75: if (!in_array($flag, self::_getUserAccountControlFlags())) {
76: throw new InvalidArgumentException("\"$flag\" is not a valid User Account Control flag");
77: }
78:
79: if ($insert) {
80: $value |= $flag;
81: }
82: }
83:
84: return $value;
85: }
86:
87: public static function decodeUserAccountControl($uacValue) {
88: $options = array();
89: foreach (self::_getUserAccountControlFlags() as $flag) {
90: $options[$flag] = ($uacValue & $flag) != 0;
91: }
92: return $options;
93: }
94:
95: }