aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Web/HTMLHeaders.php
blob: f9bc3043bc76bae6b4eca10749999b5fd6aeee10 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php

namespace Zotlabs\Web;

class HTMLHeaders {

	private $in_progress = [];
	private $parsed = [];


	function __construct($headers) {

		$lines = explode("\n",str_replace("\r",'',$headers));
		if($lines) {
			foreach($lines as $line) {
				if(preg_match('/^\s+/',$line,$matches) && trim($line)) {
					if($this->in_progress['k']) {
						$this->in_progress['v'] .= ' ' . ltrim($line);
						continue;
					}
				}
				else {
					if($this->in_progress['k']) {
						$this->parsed[] = [ $this->in_progress['k'] => $this->in_progress['v'] ];
						$this->in_progress = [];
					}

					$this->in_progress['k'] = strtolower(substr($line,0,strpos($line,':')));
					$this->in_progress['v'] = ltrim(substr($line,strpos($line,':') + 1));
				}

			}
			if($this->in_progress['k']) {
				$this->parsed[] = [ $this->in_progress['k'] => $this->in_progress['v'] ];
				$this->in_progress = [];
			}
		}
	}

	function fetch() {
		return $this->parsed;
	}
}