1<?php
2
3/**
4 * This file is part of the FreeDSx LDAP package.
5 *
6 * (c) Chad Sikorra <Chad.Sikorra@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace FreeDSx\Ldap\Control\Ad;
13
14use FreeDSx\Asn1\Asn1;
15use FreeDSx\Asn1\Exception\EncoderException;
16use FreeDSx\Asn1\Exception\PartialPduException;
17use FreeDSx\Asn1\Type\AbstractType;
18use FreeDSx\Asn1\Type\IntegerType;
19use FreeDSx\Asn1\Type\OctetStringType;
20use FreeDSx\Asn1\Type\SequenceType;
21use FreeDSx\Ldap\Control\Control;
22use FreeDSx\Ldap\Exception\ProtocolException;
23
24/**
25 * Represents a DirSync Request. Defined in MS-ADTS 3.1.1.3.4.1.3. The control value request definition is:
26 *
27 *  DirSyncRequestValue ::= SEQUENCE {
28 *      Flags       INTEGER
29 *      MaxBytes    INTEGER
30 *      Cookie      OCTET STRING
31 *  }
32 *
33 * @see https://msdn.microsoft.com/en-us/library/cc223347.aspx
34 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
35 */
36class DirSyncRequestControl extends Control
37{
38    /**
39     * If this flag is present, the client can only view objects and attributes that are otherwise accessible to the client.
40     * If this flag is not present, the server checks if the client has access rights to read the changes in the NC.
41     */
42    public const FLAG_OBJECT_SECURITY = 0x00000001;
43
44    /**
45     * The server returns parent objects before child objects.
46     */
47    public const FLAG_ANCESTORS_FIRST_ORDER = 0x00000800;
48
49    /**
50     * This flag can optionally be passed to the DC, but it has no effect.
51     */
52    public const FLAG_PUBLIC_DATA_ONLY = 0x00002000;
53
54    /**
55     * If this flag is not present, all of the values, up to a server-specified limit, in a multivalued attribute are
56     * returned when any value changes. If this flag is present, only the changed values are returned, provided the
57     * attribute is a forward link value.
58     *
59     * Note: This flag needs to be encoded as a negative, due to how AD interprets the flags value.
60     */
61    public const FLAG_INCREMENTAL_VALUES = -0x80000000;
62
63    /**
64     * @var int
65     */
66    protected $flags;
67
68    /**
69     * @var int
70     */
71    protected $maxBytes;
72
73    /**
74     * @var string
75     */
76    protected $cookie;
77
78    /**
79     * @param int $flags
80     * @param int $maxBytes
81     * @param string $cookie
82     */
83    public function __construct(int $flags = self::FLAG_INCREMENTAL_VALUES, string $cookie = '', int $maxBytes = 2147483647)
84    {
85        $this->flags = $flags;
86        $this->maxBytes = $maxBytes;
87        $this->cookie = $cookie;
88        parent::__construct(self::OID_DIR_SYNC, true);
89    }
90
91    /**
92     * @return int
93     */
94    public function getFlags(): int
95    {
96        return $this->flags;
97    }
98
99    /**
100     * @param int $flags
101     * @return $this
102     */
103    public function setFlags(int $flags)
104    {
105        $this->flags = $flags;
106
107        return $this;
108    }
109
110    /**
111     * @return int
112     */
113    public function getMaxBytes(): int
114    {
115        return $this->maxBytes;
116    }
117
118    /**
119     * @param int $maxBytes
120     * @return $this
121     */
122    public function setMaxBytes(int $maxBytes)
123    {
124        $this->maxBytes = $maxBytes;
125
126        return $this;
127    }
128
129    /**
130     * @return string
131     */
132    public function getCookie(): string
133    {
134        return $this->cookie;
135    }
136
137    /**
138     * @param string $cookie
139     * @return $this
140     */
141    public function setCookie(string $cookie)
142    {
143        $this->cookie = $cookie;
144
145        return $this;
146    }
147
148    /**
149     * {@inheritDoc}
150     * @return Control
151     * @throws EncoderException
152     * @throws PartialPduException
153     */
154    public static function fromAsn1(AbstractType $type)
155    {
156        $request = self::decodeEncodedValue($type);
157        if (!$request instanceof SequenceType) {
158            throw new ProtocolException('A DirSyncRequest control value must be a sequence type with 3 children.');
159        }
160        $flags = $request->getChild(0);
161        $cookie = $request->getChild(2);
162        $maxBytes = $request->getChild(1);
163        if (!$flags instanceof IntegerType) {
164            throw new ProtocolException('A DirSyncRequest control value sequence 0 must be an integer type.');
165        }
166        if (!$maxBytes instanceof IntegerType) {
167            throw new ProtocolException('A DirSyncRequest control value sequence 1 must be an integer type.');
168        }
169        if (!$cookie instanceof OctetStringType) {
170            throw new ProtocolException('A DirSyncRequest control value sequence 2 must be an octet string type.');
171        }
172
173        /** @var SequenceType $request */
174        $control = new self(
175            $flags->getValue(),
176            $cookie->getValue(),
177            $maxBytes->getValue()
178        );
179
180        return self::mergeControlData($control, $type);
181    }
182
183    /**
184     * {@inheritdoc}
185     */
186    public function toAsn1(): AbstractType
187    {
188        $this->controlValue = Asn1::sequence(
189            Asn1::integer($this->flags),
190            Asn1::integer($this->maxBytes),
191            Asn1::octetString($this->cookie)
192        );
193
194        return parent::toAsn1();
195    }
196}
197