1: <?php
2: /**
3: * Pinoco: makes existing static web site dynamic transparently.
4: * Copyright 2010-2012, Hisateru Tanaka <tanakahisateru@gmail.com>
5: *
6: * Licensed under The MIT License
7: * Redistributions of files must retain the above copyright notice.
8: *
9: * PHP Version 5
10: *
11: * @author Hisateru Tanaka <tanakahisateru@gmail.com>
12: * @copyright Copyright 2010-2012, Hisateru Tanaka <tanakahisateru@gmail.com>
13: * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
14: * @package Pinoco
15: */
16:
17: /**
18: * Global variables wrapper for HTTP request.
19: *
20: * @package Pinoco
21: * @property-read Pinoco_Vars $server wraps $_SERVER
22: * @property-read Pinoco_Vars $get wraps $_GET
23: * @property-read Pinoco_Vars $post wraps $_POST
24: * @property-read Pinoco_Vars $cookie wraps $_COOKIE
25: * @property-read Pinoco_Vars $files wraps $_FILES
26: * @property-read Pinoco_Vars $session wraps $_SESSION
27: * @property-read Pinoco_Vars $env wraps $_ENV
28: * @property-read string $method alias to $_SERVER['REQUEST_METHOD']
29: */
30: class Pinoco_HttpRequestVars extends Pinoco_DynamicVars
31: {
32: private $_pinoco;
33:
34: private $_server;
35: private $_get;
36: private $_post;
37: private $_cookie;
38: private $_files;
39: private $_session;
40: private $_env;
41:
42: /**
43: * Constructor
44: *
45: * @param Pinoco $pinoco
46: */
47: public function __construct($pinoco)
48: {
49: parent::__construct();
50: $this->_pinoco = $pinoco;
51: }
52:
53: public function get_server()
54: {
55: if (!isset($this->_server)) {
56: if ($this->_pinoco->testing) {
57: $this->_server = empty($_SERVER) ? (new Pinoco_Vars()) : Pinoco_Vars::fromArray($_SERVER);
58: }
59: else {
60: $this->_server = Pinoco_Vars::wrap($_SERVER);
61: }
62: }
63: return $this->_server;
64: }
65:
66: public function get_get()
67: {
68: if (!isset($this->_get)) {
69: if ($this->_pinoco->testing) {
70: $this->_get = empty($_GET) ? (new Pinoco_Vars()) : Pinoco_Vars::fromArray($_GET);
71: }
72: else {
73: $this->_get = Pinoco_Vars::wrap($_GET);
74: }
75: }
76: return $this->_get;
77: }
78:
79: public function get_post()
80: {
81: if (!isset($this->_post)) {
82: if ($this->_pinoco->testing) {
83: $this->_post = empty($_POST) ? (new Pinoco_Vars()) : Pinoco_Vars::fromArray($_POST);
84: }
85: else {
86: $this->_post = Pinoco_Vars::wrap($_POST);
87: }
88: }
89: return $this->_post;
90: }
91:
92: public function get_cookie()
93: {
94: if (!isset($this->_cookie)) {
95: if ($this->_pinoco->testing) {
96: $this->_cookie = empty($_COOKIE) ? (new Pinoco_Vars()) : Pinoco_Vars::fromArray($_COOKIE);
97: }
98: else {
99: $this->_cookie = Pinoco_Vars::wrap($_COOKIE);
100: }
101: }
102: return $this->_cookie;
103: }
104:
105: public function get_files()
106: {
107: if (!isset($this->_files)) {
108: if ($this->_pinoco->testing) {
109: $this->_files = empty($_FILES) ? (new Pinoco_Vars()) : Pinoco_Vars::fromArray($_FILES);
110: }
111: else {
112: $this->_files = Pinoco_Vars::wrap($_FILES);
113: }
114: }
115: return $this->_files;
116: }
117:
118: public function get_session()
119: {
120: if (!isset($this->_session)) {
121: if ($this->_pinoco->testing) {
122: // fake cookie header
123: $this->_pinoco->setcookie(session_name(), '0');
124: $this->_session = empty($_SESSION) ? (new Pinoco_Vars()) : Pinoco_Vars::wrap($_SESSION);
125: }
126: elseif (session_status() === PHP_SESSION_DISABLED) {
127: $this->_session = new Pinoco_Vars();
128: }
129: else {
130: if (session_status() !== PHP_SESSION_ACTIVE) {
131: session_start();
132: }
133: // fake cookie header
134: $this->_pinoco->sent_headers->push(
135: 'Set-Cookie: ' . urlencode(session_name()) . '=' . urlencode(session_id())
136: );
137: $this->_session = Pinoco_Vars::wrap($_SESSION);
138: }
139: }
140: return $this->_session;
141: }
142:
143: public function get_env()
144: {
145: if (!isset($this->_env)) {
146: if ($this->_pinoco->testing) {
147: $this->_env = empty($_ENV) ? (new Pinoco_Vars()) : Pinoco_Vars::fromArray($_ENV);
148: }
149: else {
150: $this->_env = Pinoco_Vars::wrap($_ENV);
151: }
152: }
153: return $this->_env;
154: }
155:
156: public function get_method()
157: {
158: return $this->server->get('REQUEST_METHOD', null);
159: }
160:
161: public function set_method($method)
162: {
163: $this->server->set('REQUEST_METHOD', $method);
164: }
165:
166: public function isHead()
167: {
168: return $this->method == 'HEAD';
169: }
170:
171: public function isGet()
172: {
173: return $this->method == 'GET';
174: }
175:
176: public function isPost()
177: {
178: return $this->method == 'POST';
179: }
180:
181: public function isPut()
182: {
183: return $this->method == 'PUT';
184: }
185:
186: public function isDelete()
187: {
188: return $this->method == 'DELETE';
189: }
190: }
191:
192: