PHP7 bug fix
[openemr.git] / library / classes / Controller.class.php
blob0a893e10cbefcd3898680e97976ab7f55f66a714
1 <?php
3 require_once(dirname(__FILE__) . "/../Smarty/Smarty.class.php");
4 require_once(dirname(__FILE__) . "/../formdata.inc.php");
5 if (!defined('SMARTY_DIR')) {
6 define("SMARTY_DIR", dirname(__FILE__) . "/../Smarty/");
10 class Controller extends Smarty {
12 var $_current_action;
13 var $_state;
14 var $_args = array();
16 function __construct() {
17 parent::__construct();
18 $this->template_mod = "general";
19 $this->_current_action = "";
20 $this->_state = true;
21 $this->compile_dir = $GLOBALS['fileroot'] . "/interface/main/calendar/modules/PostCalendar/pntemplates/compiled";
22 $this->compile_check = true;
23 $this->plugins_dir = array(dirname(__FILE__) . "/../Smarty/plugins");
24 $this->assign("PROCESS", "true");
25 $this->assign("HEADER", "<html><head>
26 <?php html_header_show();?></head><body>");
27 $this->assign("FOOTER", "</body></html>");
28 $this->assign("CONTROLLER", "controller.php?");
29 $this->assign("CONTROLLER_THIS", "controller.php?" . $_SERVER['QUERY_STRING']);
30 $this->assign("WEBROOT", $GLOBALS['webroot']);
33 function set_current_action($action) {
34 $this->_current_action = $action;
37 function default_action() {
38 echo "<html><body></body></html>";
41 function process_action() {
42 $this->default_action();
45 function populate_object(&$obj) {
46 if(!is_object($obj)) {
47 $this->function_argument_error();
50 foreach($_POST as $varname => $var) {
51 $varname = preg_replace("/[^A-Za-z0-9_]/","",$varname);
52 $func = "set_" . $varname;
53 if ( (!(strpos("_",$varname) === 0)) && is_callable(array($obj,$func)) ) {
54 //echo "c: $func on w: " . $var . "<br />";
56 //modified 01-2010 by BGM to centralize to formdata.inc.php
57 // have place several debug statements to allow standardized testing over next several months
58 if (!is_array($var)) {
59 //DEBUG LINE - error_log("Controller populate before strip: ".$var, 0);
60 $var = strip_escape_custom($var);
61 //DEBUG LINE - error_log("Controller populate after strip: ".$var, 0);
64 call_user_func_array(array(&$obj,$func),array($var, $_POST));
68 return true;
71 function function_argument_error() {
72 $this->display($GLOBALS['template_dir'] . "error/" . $this->template_mod . "_function_argument.html");
73 exit;
76 function i_once($file) {
77 return include_once($file);
80 function act($qarray) {
82 if (isset($_GET['process'])){
83 unset($_GET['process']);
84 unset($qarray['process']);
85 $_POST['process'] = "true";
87 $args = array_reverse(array_keys($qarray));
88 $c_name = preg_replace("/[^A-Za-z0-9_]/","",array_pop($args));
89 $parts = explode("_",$c_name);
90 $name = "";
92 foreach($parts as $p) {
93 $name .= ucfirst($p);
96 $c_name = $name;
97 $c_action = preg_replace("/[^A-Za-z0-9_]/","",array_pop($args));
98 $args = array_reverse($args);
100 if(!call_user_func(array(Controller,"i_once"),$GLOBALS['fileroot'] ."/controllers/C_" . $c_name . ".class.php")) {
101 echo "Unable to load controller $name\n, please check the first argument supplied in the URL and try again";
102 exit;
105 $obj_name = "C_" . $c_name;
106 $c_obj = new $obj_name();
108 if (empty ($c_action)) {
109 $c_action = "default";
112 $c_obj->_current_action = $c_action;
113 $args_array = array();
115 foreach ($args as $arg) {
116 $arg = preg_replace("/[^A-Za-z0-9_]/","",$arg);
117 //this is a workaround because call user func does funny things with passing args if they have no assigned value
118 //2013-02-10 EMR Direct: workaround modified since "0" is also considered empty;
119 if (empty($qarray[$arg]) && $qarray[$arg]!="0") {
120 //if argument is empty pass null as value and arg as assoc array key
121 $args_array[$arg] = null;
123 else {
124 $args_array[$arg] = $qarray[$arg];
128 $output = "";
129 //print_r($args_array);
130 if (isset($_POST['process']) && ($_POST['process']== "true")) {
132 if (is_callable(array(&$c_obj,$c_action . "_action_process"))) {
133 //echo "ca: " . $c_action . "_action_process";
134 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action_process"),$args_array);
135 if ($c_obj->_state == false) {
136 return $output;
139 //echo "ca: " . $c_action . "_action";
140 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"),$args_array);
143 else {
144 if (is_callable(array(&$c_obj,$c_action . "_action"))) {
145 //echo "ca: " . $c_action . "_action";
146 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"),$args_array);
148 else {
149 echo "The action trying to be performed: " . $c_action ." does not exist controller: ". $name;
154 return $output;
157 function _link($action = "default",$inlining = false) {
158 $url_parts = explode("&",$_SERVER['REQUEST_URI']);
159 $link = array_shift($url_parts);
160 //print_r($url_parts);
162 if (strpos($url_parts[0],"=") === false) {
163 $inline_arg = $url_parts[0];
164 $url_parts[0] = $action;
166 else {
167 array_unshift($url_parts,$action);
169 if ($inlining) {
170 $link .= "&" . $inline_arg;
171 $link .= "&action=" . $url_parts[0];
173 else {
174 $link .= "&" . $url_parts[0];
177 foreach ($this->_args as $arg_name => $arg) {
178 $link .= "&" . $arg_name . "=" . $arg;
180 $link .= "&";
181 return $link;