00001 <?php 00022 class Controller 00023 { 00029 public $models = null; 00035 public $response = ''; 00041 public $module_name; 00047 public $controller_name; 00053 public $action_name; 00060 public $id; 00066 public $template = 'default'; 00075 public $cache = array('type' => false, 'time' => false); 00081 public $logger; 00087 public $view = null; 00098 public function __construct($module, $controller, $action, $id, $all_parameters, $parameters) { 00099 $this->module_name = $module; 00100 $this->controller_name = $controller; 00101 $this->id = $id; 00102 $this->all_parameters = $all_parameters; 00103 $this->parameters = $parameters; 00104 $this->view = $this->action_name = $action; 00105 } 00112 public function cache($time, $type='view') 00113 { 00114 if($time !== false) { 00115 $this->cache['type'] = $type; 00116 $this->cache['time'] = $time; 00117 } else { 00118 $this->cache['type'] = false; 00119 } 00120 } 00131 public function route_to() 00132 { 00133 $args = func_get_args(); 00134 return call_user_func_array(array('Router', 'route_to'), $args); 00135 } 00136 00143 protected function post($param_name) 00144 { 00148 $param_name = explode('.', $param_name); 00149 if(count($param_name)>1) { 00150 $value = isset($_POST[$param_name[0]][$param_name[1]]) ? $_POST[$param_name[0]][$param_name[1]] : ''; 00151 } else { 00152 $value = isset($_POST[$param_name[0]]) ? $_POST[$param_name[0]] : ''; 00153 } 00154 00158 if(func_num_args()>1){ 00159 $args = func_get_args(); 00160 $args[0] = $value; 00161 return call_user_func_array(array('Filter', 'get'), $args); 00162 } 00163 return $value; 00164 } 00165 00172 protected function get($param_name) 00173 { 00177 $param_name = explode('.', $param_name); 00178 if(count($param_name)>1) { 00179 $value = isset($_GET[$param_name[0]][$param_name[1]]) ? $_GET[$param_name[0]][$param_name[1]] : ''; 00180 } else { 00181 $value = isset($_GET[$param_name[0]]) ? $_GET[$param_name[0]] : ''; 00182 } 00183 00187 if(func_num_args()>1){ 00188 $args = func_get_args(); 00189 $args[0] = $value; 00190 return call_user_func_array(array('Filter', 'get'), $args); 00191 } 00192 return $value; 00193 } 00194 00201 protected function request($param_name) 00202 { 00206 $param_name = explode('.', $param_name); 00207 if(count($param_name)>1) { 00208 $value = isset($_REQUEST[$param_name[0]][$param_name[1]]) ? $_REQUEST[$param_name[0]][$param_name[1]] : ''; 00209 } else { 00210 $value = isset($_REQUEST[$param_name[0]]) ? $_REQUEST[$param_name[0]] : ''; 00211 } 00212 00216 if(func_num_args()>1){ 00217 $args = func_get_args(); 00218 $args[0] = $value; 00219 return call_user_func_array(array('Filter', 'get'), $args); 00220 } 00221 return $value; 00222 } 00223 00230 protected function has_post($s) 00231 { 00232 $success = true; 00233 $args = func_get_args(); 00234 foreach($args as $f) { 00238 $f = explode('.', $f); 00239 if(count($f)>1 && !isset($_POST[$f[0]][$f[1]]) ) { 00240 $success = false; 00241 break; 00242 } elseif(!isset($_POST[$f[0]])) { 00243 $success = false; 00244 break; 00245 } 00246 } 00247 return $success; 00248 } 00249 00256 protected function has_get($s) 00257 { 00258 $success = true; 00259 $args = func_get_args(); 00260 foreach($args as $f) { 00264 $f = explode('.', $f); 00265 if(count($f)>1 && !isset($_GET[$f[0]][$f[1]]) ) { 00266 $success = false; 00267 break; 00268 } elseif(!isset($_GET[$f[0]])) { 00269 $success = false; 00270 break; 00271 } 00272 } 00273 return $success; 00274 } 00275 00282 protected function has_request($s) 00283 { 00284 $success = true; 00285 $args = func_get_args(); 00286 foreach($args as $f) { 00290 $f = explode('.', $f); 00291 if(count($f)>1 && !isset($_REQUEST[$f[0]][$f[1]]) ) { 00292 $success = false; 00293 break; 00294 } elseif(!isset($_REQUEST[$f[0]])) { 00295 $success = false; 00296 break; 00297 } 00298 } 00299 return $success; 00300 } 00301 00308 protected function upload_image($name, $new_name='') 00309 { 00310 if(isset($_FILES[$name])){ 00311 if(!$new_name) { 00312 $new_name = $_FILES[$name]['name']; 00313 } 00314 move_uploaded_file($_FILES[$name]['tmp_name'], htmlspecialchars("public/img/upload/$new_name")); 00315 return urlencode(htmlspecialchars("upload/$new_name")); 00316 } else { 00317 return urlencode($this->request($name)); 00318 } 00319 } 00320 00327 protected function upload_file($name, $dir, $new_name='') 00328 { 00329 if($_FILES[$name]){ 00330 if(!$new_name) { 00331 $new_name = $_FILES[$name]['name']; 00332 } 00333 return move_uploaded_file($_FILES[$name]['tmp_name'], htmlspecialchars("$dir/$new_name")); 00334 } else { 00335 return false; 00336 } 00337 } 00338 00346 protected function redirect($controller, $seconds=0.5) 00347 { 00348 $seconds*=1000; 00349 if(headers_sent()){ 00350 print " 00351 <script type='text/javascript'> 00352 window.setTimeout(\"window.location='".URL_PATH."$controller'\", $seconds); 00353 </script>\n"; 00354 } else { 00355 header('Location: '.URL_PATH."$controller"); 00356 } 00357 } 00358 00365 protected function is_ajax() 00366 { 00367 return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'); 00368 } 00369 00375 protected function set_response($type) 00376 { 00377 $this->response = $type; 00378 } 00379 00386 protected function log($msg, $type=Logger::DEBUG) 00387 { 00388 if(is_array($msg)){ 00389 $msg = print_r($msg, true); 00390 } 00391 if(!$this->logger){ 00392 $this->logger = new Logger($this->controller_name.'.txt'); 00393 } 00394 $this->logger->log($msg, $type); 00395 } 00396 00400 protected function nullify() 00401 { 00402 $args = func_get_args(); 00403 foreach($args as $f) { 00404 $this->$f = null; 00405 } 00406 } 00412 public function render($view){ 00413 $this->view = $view; 00414 } 00420 public function before_filter() 00421 { 00422 } 00428 public function after_filter() 00429 { 00430 } 00436 public function initialize() 00437 { 00438 } 00444 public function finalize() 00445 { 00446 } 00458 protected function set_persistent($var, $value=null) 00459 { 00460 if(func_num_args()>1) { 00461 $_SESSION['KUMBIA_CONTROLLER']["$this->module_name/$this->controller_name"][$var] = $value; 00462 } 00463 } 00475 protected function get_persistent($var) 00476 { 00477 if(isset($_SESSION['KUMBIA_CONTROLLER']["$this->module_name/$this->controller_name"][$var])) { 00478 return $_SESSION['KUMBIA_CONTROLLER']["$this->module_name/$this->controller_name"][$var]; 00479 } 00480 return null; 00481 } 00482 00488 protected function destroy_persistent($var) 00489 { 00490 $args = func_get_args(); 00491 foreach ($args as $var) { 00492 if(isset($_SESSION['KUMBIA_CONTROLLER']["$this->module_name/$this->controller_name"][$var])) { 00493 unset($_SESSION['KUMBIA_CONTROLLER']["$this->module_name/$this->controller_name"][$var]); 00494 } 00495 } 00496 } 00497 }