PageRenderTime 36ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/library/classes/Controller.class.php

https://github.com/iempires/openemr
PHP | 181 lines | 136 code | 33 blank | 12 comment | 21 complexity | 8c59bd3a8620db25f85d9fe8b5e74e4f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. require_once(dirname(__FILE__) . "/../Smarty.class.php");
  3. require_once(dirname(__FILE__) . "/../formdata.inc.php");
  4. define("SMARTY_DIR", dirname(__FILE__) . "/../");
  5. class Controller extends Smarty {
  6. var $_current_action;
  7. var $_state;
  8. var $_args = array();
  9. function Controller() {
  10. parent::Smarty();
  11. $this->template_mod = "general";
  12. $this->_current_action = "";
  13. $this->_state = true;
  14. $this->compile_dir = $GLOBALS['fileroot'] . "/interface/main/calendar/modules/PostCalendar/pntemplates/compiled";
  15. $this->compile_check = true;
  16. $this->assign("PROCESS", "true");
  17. $this->assign("HEADER", "<html><head>
  18. <? html_header_show();?></head><body>");
  19. $this->assign("FOOTER", "</body></html>");
  20. $this->assign("CONTROLLER", "controller.php?");
  21. $this->assign("CONTROLLER_THIS", "controller.php?" . $_SERVER['QUERY_STRING']);
  22. $this->assign("WEBROOT", $GLOBALS['webroot']);
  23. }
  24. function set_current_action($action) {
  25. $this->_current_action = $action;
  26. }
  27. function default_action() {
  28. echo "<html><body></body></html>";
  29. }
  30. function process_action() {
  31. $this->default_action();
  32. }
  33. function populate_object(&$obj) {
  34. if(!is_object($obj)) {
  35. $this->function_argument_error();
  36. }
  37. foreach($_POST as $varname => $var) {
  38. $varname = preg_replace("/[^A-Za-z0-9_]/","",$varname);
  39. $func = "set_" . $varname;
  40. if ( (!(strpos("_",$varname) === 0)) && is_callable(array($obj,$func)) ) {
  41. //echo "c: $func on w: " . $var . "<br />";
  42. //modified 01-2010 by BGM to centralize to formdata.inc.php
  43. // have place several debug statements to allow standardized testing over next several months
  44. if (!is_array($var)) {
  45. //DEBUG LINE - error_log("Controller populate before strip: ".$var, 0);
  46. $var = strip_escape_custom($var);
  47. //DEBUG LINE - error_log("Controller populate after strip: ".$var, 0);
  48. }
  49. call_user_func_array(array(&$obj,$func),array($var, $_POST));
  50. }
  51. }
  52. return true;
  53. }
  54. function function_argument_error() {
  55. $this->display($GLOBALS['template_dir'] . "error/" . $this->template_mod . "_function_argument.html");
  56. exit;
  57. }
  58. function i_once($file) {
  59. return include_once($file);
  60. }
  61. function act($qarray) {
  62. if (isset($_GET['process'])){
  63. unset($_GET['process']);
  64. unset($qarray['process']);
  65. $_POST['process'] = "true";
  66. }
  67. $args = array_reverse(array_keys($qarray));
  68. $c_name = preg_replace("/[^A-Za-z0-9_]/","",array_pop($args));
  69. $parts = split("_",$c_name);
  70. $name = "";
  71. foreach($parts as $p) {
  72. $name .= ucfirst($p);
  73. }
  74. $c_name = $name;
  75. $c_action = preg_replace("/[^A-Za-z0-9_]/","",array_pop($args));
  76. $args = array_reverse($args);
  77. if(!@call_user_func(array(Controller,"i_once"),$GLOBALS['fileroot'] ."/controllers/C_" . $c_name . ".class.php")) {
  78. echo "Unable to load controller $name\n, please check the first argument supplied in the URL and try again";
  79. exit;
  80. }
  81. $obj_name = "C_" . $c_name;
  82. $c_obj = new $obj_name();
  83. if (empty ($c_action)) {
  84. $c_action = "default";
  85. }
  86. $c_obj->_current_action = $c_action;
  87. $args_array = array();
  88. foreach ($args as $arg) {
  89. $arg = preg_replace("/[^A-Za-z0-9_]/","",$arg);
  90. //this is a workaround because call user func does funny things with passing args if they have no assigned value
  91. if (empty($qarray[$arg])) {
  92. //if argument is empty pass null as value and arg as assoc array key
  93. $args_array[$arg] = null;
  94. }
  95. else {
  96. $args_array[$arg] = $qarray[$arg];
  97. }
  98. }
  99. $output = "";
  100. //print_r($args_array);
  101. if ($_POST['process'] == "true") {
  102. if (is_callable(array(&$c_obj,$c_action . "_action_process"))) {
  103. //echo "ca: " . $c_action . "_action_process";
  104. $output .= call_user_func_array(array(&$c_obj,$c_action . "_action_process"),$args_array);
  105. if ($c_obj->_state == false) {
  106. return $output;
  107. }
  108. }
  109. //echo "ca: " . $c_action . "_action";
  110. $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"),$args_array);
  111. }
  112. else {
  113. if (is_callable(array(&$c_obj,$c_action . "_action"))) {
  114. //echo "ca: " . $c_action . "_action";
  115. $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"),$args_array);
  116. }
  117. else {
  118. echo "The action trying to be performed: " . $c_action ." does not exist controller: ". $name;
  119. }
  120. }
  121. return $output;
  122. }
  123. function _link($action = "default",$inlining = false) {
  124. $url_parts = split("&",$_SERVER['REQUEST_URI']);
  125. $link = array_shift($url_parts);
  126. //print_r($url_parts);
  127. if (strpos($url_parts[0],"=") === false) {
  128. $inline_arg = $url_parts[0];
  129. $url_parts[0] = $action;
  130. }
  131. else {
  132. array_unshift($url_parts,$action);
  133. }
  134. if ($inlining) {
  135. $link .= "&" . $inline_arg;
  136. $link .= "&action=" . $url_parts[0];
  137. }
  138. else {
  139. $link .= "&" . $url_parts[0];
  140. }
  141. foreach ($this->_args as $arg_name => $arg) {
  142. $link .= "&" . $arg_name . "=" . $arg;
  143. }
  144. $link .= "&";
  145. return $link;
  146. }
  147. }
  148. ?>