6 public static $options = array(
11 'title' => 'Website URL',
17 'title' => 'Browser width',
24 'title' => 'Browser height',
34 'title' => 'Screenshot width',
41 'title' => 'Screenshot height',
48 'title' => 'Screenshot format',
50 'type' => array('png', 'jpg', 'pdf'),
53 'title' => 'Screenshot mode',
54 'default' => 'screen',
55 'type' => array('screen', 'page'),
58 'title' => 'Maximum age for a screenshot',
66 'atimestamp' => array(
67 'title' => 'Timestamp the request has been generated',
72 'title' => 'Access token (user name)',
76 'asignature' => array(
77 'title' => 'Access signature',
83 public $values = array();
92 * Parses an array of options, validates them and writes them into
95 * @param array $arValues Array of options, e.g. $_GET
97 public function parse($arValues)
99 foreach (static::$options as $name => $arOption) {
100 $this->values[$name] = $arOption['default'];
101 if (!isset($arValues[$name])) {
102 if (isset($arOption['required'])) {
103 throw new \InvalidArgumentException(
104 $name . ' parameter missing'
110 if ($arOption['type'] == 'url') {
111 $this->values[$name] = $this->validateUrl($arValues[$name]);
112 } else if ($arOption['type'] == 'int') {
113 $this->values[$name] = $this->validateInt(
114 $arValues[$name], $arOption['min'], $arOption['max']
116 } else if (gettype($arOption['type']) == 'array') {
117 $this->values[$name] = $this->validateArray(
118 $arValues[$name], $arOption['type']
120 } else if ($arOption['type'] == 'age') {
121 $this->values[$name] = $this->clamp(
122 static::validateAge($arValues[$name]),
123 $arOption['min'], null,
126 } else if ($arOption['type'] != 'skip') {
127 throw new \InvalidArgumentException(
128 'Unsupported option type: ' . $arOption['type']
131 unset($arValues[$name]);
134 if (count($arValues) > 0) {
135 throw new \InvalidArgumentException(
136 'Unsupported parameter: ' . implode(', ', array_keys($arValues))
140 $this->calcPageSize();
143 protected function calcPageSize()
145 if ($this->values['swidth'] === null) {
146 $this->values['swidth'] = $this->values['bwidth'];
148 if ($this->values['smode'] == 'page') {
152 if ($this->values['sheight'] !== null) {
153 $this->values['bheight'] = intval(
154 $this->values['bwidth'] / $this->values['swidth']
155 * $this->values['sheight']
157 } else if ($this->values['bheight'] !== null) {
158 $this->values['sheight'] = intval(
159 $this->values['swidth'] / $this->values['bwidth']
160 * $this->values['bheight']
163 //no height set. use 4:3
164 $this->values['sheight'] = $this->values['swidth'] / 4 * 3;
165 $this->values['bheight'] = $this->values['bwidth'] / 4 * 3;
169 protected function clamp($value, $min, $max, $silent = false)
171 if ($min !== null && $value < $min) {
175 throw new \InvalidArgumentException(
176 'Value must be at least ' . $min
180 if ($max !== null && $value > $max) {
184 throw new \InvalidArgumentException(
185 'Value may be up to ' . $min
193 * Validates an age is numeric. If it is not numeric, it's interpreted as
194 * a ISO 8601 duration specification.
196 * @param string $value Age in seconds
198 * @return integer Age in seconds
200 * @throws \InvalidArgumentException
201 * @link http://en.wikipedia.org/wiki/Iso8601#Durations
203 public static function validateAge($value)
205 if (!is_numeric($value)) {
206 //convert short notation to seconds
207 $value = 'P' . ltrim(strtoupper($value), 'P');
209 $interval = new \DateInterval($value);
210 } catch (\Exception $e) {
211 throw new \InvalidArgumentException(
212 'Invalid age: ' . $value
219 ) + $interval->h * 3600
226 protected function validateArray($value, $options)
228 if (array_search($value, $options) === false) {
229 throw new \InvalidArgumentException(
230 'Invalid value ' . $value . '.'
231 . ' Allowed: ' . implode(', ', $options)
237 protected function validateInt($value, $min, $max)
239 if (!is_numeric($value)) {
240 throw new \InvalidArgumentException(
241 'Value must be a number'
244 $value = (int) $value;
245 return $this->clamp($value, $min, $max);
248 protected function validateUrl($url)
250 $parts = parse_url($url);
251 if ($parts === false) {
252 throw new \InvalidArgumentException('Invalid URL');
254 if (!isset($parts['scheme'])) {
255 throw new \InvalidArgumentException('URL scheme missing');
257 if (!isset($parts['host'])) {
258 throw new \InvalidArgumentException('URL host missing');
263 public function setConfig(Config $config)
265 $this->config = $config;
266 static::$options['smaxage']['default'] = $this->config->screenshotMaxAge;
267 static::$options['smaxage']['min'] = $this->config->screenshotMinAge;