1 : <?php
2 :
3 : /**
4 : * PHPDevShell is a RAD Framework aimed at developing administrative applications.
5 : *
6 : * @package PHPDevShell
7 : * @link http://www.phpdevshell.org
8 : * @copyright Copyright (C) 2007 Jason Schoeman, All rights reserved.
9 : * @license GNU/LGPL, see readme/licensed_under_lgpl or http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
10 : * @author Jason Schoeman, Contact: titan [at] phpdevshell [dot] org.
11 : *
12 : * Copyright notice: See readme/notice
13 : * By using PHPDevShell you agree to notice and license, if you dont agree to this notice/license you are not allowed to use PHPDevShell.
14 : *
15 : * This is the main engine.
16 : */
17 :
18 : /**
19 : * Master controller.
20 : */
21 : class PHPDS_controller extends PHPDS_dependant
22 1 : {
23 : protected $_POST;
24 : protected $_GET;
25 :
26 : public function construct()
27 : {
28 : // prevent some funky business
29 8 : unset($_REQUEST['_SESSION']);
30 8 : unset($_POST['_SESSION']);
31 8 : unset($_GET['_SESSION']);
32 :
33 8 : $this->_POST = empty($_POST) ? array() : $_POST;
34 8 : $this->_GET = empty($_GET) ? array() : $_GET;
35 :
36 8 : return parent::construct();
37 : }
38 :
39 : /**
40 : * Return a value from the _POST meta array
41 : *
42 : * @date 20101016 (v1.0) (greg) added
43 : * @version 1.0
44 : * @author greg
45 : *
46 : * @param string|null $key the name of the post variable to fetch; if null, the entire array is returned
47 : * @param mixed|array $default a default value to return when the post variable is not set; when returning the entire array, an array can be given here with default values
48 : * @param integer $options
49 : *
50 : * @return scalar|array the content of the post variable or the whole array, possibly with default value(s)
51 : */
52 : public function POST($key = null, $default = null, $options = 0)
53 : {
54 1 : if (!empty($key)) {
55 1 : return (isset($this->_POST[$key])) ? $this->_POST[$key] : $default;
56 : } else {
57 1 : if (is_array($default)) return array_merge($default, $this->_POST);
58 1 : else return $this->_POST;
59 : }
60 : }
61 :
62 : /**
63 : * Return a value from the _GET meta array
64 : *
65 : * @date 20101016 (v1.0) (greg) added
66 : * @version 1.0
67 : * @author greg
68 : *
69 : * @param string|null $key the name of the get variable to fetch; if null, the entire array is returned
70 : * @param mixed|array $default a default value to return when the get variable is not set; when returning the entire array, an array can be given here with default values
71 : * @param integer $options
72 : *
73 : * @return scalar|array the content of the get variable or the whole array, possibly with default value(s)
74 : */
75 : public function GET($key = null, $default = null, $options = 0)
76 : {
77 1 : if (!empty($key)) {
78 1 : return (isset($this->_GET[$key])) ? $this->_GET[$key] : $default;
79 : } else {
80 1 : if (is_array($default)) return array_merge($default, $this->_GET);
81 1 : else return $this->_GET;
82 : }
83 : }
84 :
85 : /**
86 : * Does security check and runs controller.
87 : *
88 : * @version 1.1
89 : * @author greg <greg@phpdevshell.org>
90 : *
91 : * @date 20110307 (v1.1) (greg) calls via Ajax don't exit anymore but empty the template output instead
92 : *
93 : * @return mixed
94 : */
95 : public function run()
96 : {
97 0 : (is_object($this->security)) ? $this->security->securityIni() : exit('Access Denied!');
98 :
99 0 : $result = false;
100 0 : if (PU_isAJAX ()) {
101 0 : $result = $this->runAJAX();
102 0 : } else {
103 0 : $result = $this->execute();
104 : }
105 0 : return $result;
106 : }
107 :
108 : /**
109 : * Run a controller when called with ajax
110 : *
111 : * @version 1.0
112 : * @since 3.0.5
113 : * @author greg <greg@phpdevshell.org>
114 : *
115 : * @return mixed
116 : */
117 : public function runAJAX()
118 : {
119 0 : $raw_data = false;
120 0 : if (isset($_SERVER["HTTP_X_REMOTE_CALL"])) {
121 0 : $f = 'ajax'.$_SERVER["HTTP_X_REMOTE_CALL"];
122 :
123 0 : if (method_exists($this, $f)) {
124 0 : $raw_data = call_user_func(array($this, $f), $this->_GET);
125 0 : }
126 0 : } else {
127 0 : $raw_data = $this->viaAJAX();
128 : }
129 :
130 0 : return $this->handleResult($raw_data);
131 : }
132 :
133 : /**
134 : * Deal with the controller's output
135 : *
136 : * @version 1.0
137 : * @since 3.0.5
138 : * @author greg <greg@phpdevshell.org>
139 : *
140 : * @param mixed $raw_data
141 : * @return mixed
142 : */
143 : public function handleResult($raw_data)
144 : {
145 3 : $template = $this->template;
146 3 : $encoded_data = PU_isJSON($raw_data);
147 3 : if (false !== $encoded_data) {
148 1 : $template->controller = $encoded_data;
149 1 : $template->templateName = '';
150 1 : $result = true;
151 1 : } else {
152 2 : if (false === $raw_data) { // we consider it's an error
153 2 : $result = false;
154 2 : } elseif (is_null($raw_data)) { // deal with it the usual way (normal template)
155 2 : $result = true;
156 2 : } elseif (true === $raw_data) { // controller handled output
157 2 : $template->controller = '';
158 2 : $template->templateName = '';
159 2 : $result = true;
160 2 : } elseif (is_string($raw_data)) { // bare data, using empty template
161 2 : $template->controller = $raw_data;
162 2 : $template->templateName = '';
163 2 : $result = true;
164 2 : } else{
165 2 : throw new PHPDS_exception(sprintf(___('The return value of controller %d is invalid'), $this->configuration['m']));
166 : }
167 : }
168 3 : return $result;
169 : }
170 :
171 : /**
172 : * This method is meant to be the entry point of your class. Most checks and cleanup should have been done by the time it's executed
173 : *
174 : * @return whatever, if you return "false" output will be truncated
175 : */
176 : public function execute()
177 : {
178 : // Your code here
179 0 : }
180 :
181 : /**
182 : * This method is run if your controller is called in an ajax context
183 : *
184 : * @return mixed, there are 3 cases: "true" (or nothing) the output will be handled by the template the usual way, "false" it's an error, otherwise the result data will be displayed in an empty template
185 : */
186 : public function viaAJAX()
187 : {
188 : // Your code here
189 0 : }
190 : }
|