1<?php
2// qrequest.php -- HotCRP helper class for request objects (no warnings)
3// Copyright (c) 2006-2018 Eddie Kohler; see LICENSE.
4
5class Qrequest implements ArrayAccess, IteratorAggregate, Countable, JsonSerializable {
6    // NB see also count()
7    private $____method;
8    private $____a = [];
9    private $____files = [];
10    private $____x = [];
11    private $____post_ok = false;
12    private $____post_empty = false;
13    function __construct($method, $data = null) {
14        $this->____method = $method;
15        if ($data)
16            foreach ((array) $data as $k => $v)
17                $this->$k = $v;
18    }
19    function method() {
20        return $this->____method;
21    }
22    function offsetExists($offset) {
23        return property_exists($this, $offset);
24    }
25    function& offsetGet($offset) {
26        $x = null;
27        if (property_exists($this, $offset))
28            $x =& $this->$offset;
29        return $x;
30    }
31    function offsetSet($offset, $value) {
32        $this->$offset = $value;
33        unset($this->____a[$offset]);
34    }
35    function offsetUnset($offset) {
36        unset($this->$offset);
37    }
38    function getIterator() {
39        return new ArrayIterator($this->make_array());
40    }
41    function __set($name, $value) {
42        $this->$name = $value;
43        unset($this->____a[$name]);
44    }
45    function& __get($name) {
46        $x = null;
47        if (property_exists($this, $name))
48            $x =& $this->$name;
49        return $x;
50    }
51    function __isset($name) {
52        return isset($this->$name);
53    }
54    function __unset($name) {
55        unset($this->$name);
56    }
57    function get($name, $default = null) {
58        if (property_exists($this, $name))
59            $default = $this->$name;
60        return $default;
61    }
62    function get_a($name, $default = null) {
63        if (property_exists($this, $name)) {
64            $default = $this->$name;
65            if ($default === "__array__" && isset($this->____a[$name]))
66                $default = $this->____a[$name];
67        }
68        return $default;
69    }
70    function allow_a(/* ... */) {
71        foreach (func_get_args() as $name) {
72            if (property_exists($this, $name)
73                && $this->$name === "__array__"
74                && isset($this->____a[$name])) {
75                $this->$name = $this->____a[$name];
76                unset($this->____a[$name]);
77            }
78        }
79    }
80    function set_req($name, $value) {
81        if (is_array($value)) {
82            $this->$name = "__array__";
83            $this->____a[$name] = $value;
84        } else {
85            $this->$name = $value;
86        }
87    }
88    function count() {
89        return count(get_object_vars($this)) - 6;
90    }
91    function jsonSerialize() {
92        return $this->make_array();
93    }
94    function make_array() {
95        $d = [];
96        foreach (get_object_vars($this) as $k => $v)
97            if (substr($k, 0, 4) !== "____")
98                $d[$k] = $v;
99        return $d;
100    }
101    function keys() {
102        $d = [];
103        foreach (array_keys(get_object_vars($this)) as $k)
104            if (substr($k, 0, 4) !== "____")
105                $d[] = $k;
106        return $d;
107    }
108    function make_object() {
109        return (object) $this->make_array();
110    }
111    function contains($key) {
112        return property_exists($this, $key);
113    }
114    function set_file($name, $finfo) {
115        $this->____files[$name] = $finfo;
116    }
117    function has_files() {
118        return !empty($this->____files);
119    }
120    function has_file($name) {
121        return isset($this->____files[$name]);
122    }
123    function file($name) {
124        $f = null;
125        if (array_key_exists($name, $this->____files))
126            $f = $this->____files[$name];
127        return $f;
128    }
129    function file_filename($name) {
130        $fn = false;
131        if (array_key_exists($name, $this->____files))
132            $fn = $this->____files[$name]["name"];
133        return $fn;
134    }
135    function file_contents($name) {
136        $data = false;
137        if (array_key_exists($name, $this->____files))
138            $data = @file_get_contents($this->____files[$name]["tmp_name"]);
139        return $data;
140    }
141    function files() {
142        return $this->____files;
143    }
144    function set_attachment($name, $x) {
145        $this->____x[$name] = $x;
146    }
147    function has_attachments() {
148        return !empty($this->____x);
149    }
150    function has_attachment($name) {
151        return isset($this->____x[$name]);
152    }
153    function attachment($name) {
154        $x = null;
155        if (array_key_exists($name, $this->____x))
156            $x = $this->____x[$name];
157        return $x;
158    }
159    function attachments() {
160        return $this->____x;
161    }
162    function approve_post() {
163        $this->____post_ok = true;
164    }
165    function post_ok() {
166        return $this->____post_ok;
167    }
168    function set_post_empty() {
169        $this->____post_empty = true;
170    }
171    function post_empty() {
172        return $this->____post_empty;
173    }
174}
175