PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/library/SHARED.php

http://mymvc.codeplex.com
PHP | 174 lines | 90 code | 40 blank | 44 comment | 11 complexity | 370685a30217fcf400e6aa744941c7ed MD5 | raw file
  1. <?php
  2. /**
  3. * functions for the MVC
  4. *
  5. * @author Jason Torgrimson
  6. * @copyright Advanced Technology Solutions, Inc..
  7. * @since July 01, 2010
  8. * @package SchoolSite
  9. */
  10. /**
  11. * Check if environment is development and display errors
  12. */
  13. function setReporting(){
  14. if (DEVELOPMENT_ENVIRONMENT == true){
  15. error_reporting(E_ALL);
  16. ini_set('display_errors','On');
  17. }else{
  18. error_reporting(E_ALL);
  19. ini_set('display_errors','Off');
  20. ini_set('log_errors', 'On');
  21. ini_set('error_log', ROOT_PATH . '/config/logs/error.log');
  22. }
  23. }
  24. /**
  25. * Check for Magic Quotes and remove them
  26. */
  27. function stripSlashesDeep($value){
  28. $value = is_array($value) ? array_map('stripSlashesDeep', $value) : stripslashes($value);
  29. return $value;
  30. }
  31. /**
  32. * Check for Magic Quotes and remove them
  33. */
  34. function removeMagicQuotes(){
  35. if(get_magic_quotes_gpc()){
  36. $_GET = stripSlashesDeep($_GET );
  37. $_POST = stripSlashesDeep($_POST );
  38. $_COOKIE = stripSlashesDeep($_COOKIE);
  39. }
  40. }
  41. /**
  42. * Check register globals and remove them
  43. */
  44. function unregisterGlobals(){
  45. if (ini_get('register_globals')) {
  46. $array = array('_SESSION', '_POST', '_GET', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES');
  47. foreach ($array as $value) {
  48. foreach ($GLOBALS[$value] as $key => $var) {
  49. if ($var === $GLOBALS[$key]) {
  50. unset($GLOBALS[$key]);
  51. }
  52. }
  53. }
  54. }
  55. }
  56. /**
  57. * Main Call Function for MVC
  58. */
  59. function callHook(){
  60. global $url;
  61. // place url in array
  62. $urlArray = array();
  63. $urlArray = explode("/",$url);
  64. // get controller and action if they exsist in URL
  65. if(count($urlArray) > 0 &&
  66. $urlArray[0] != ''){
  67. $controller = array_shift($urlArray);
  68. if(count($urlArray) > 0 &&
  69. $urlArray[0] != ''){
  70. $action = array_shift($urlArray);
  71. }else{
  72. $action = 'index';
  73. }
  74. }else{
  75. $controller = 'index';
  76. $action = 'index';
  77. }
  78. // save the left over array as query stream
  79. $queryString = $urlArray;
  80. $controllerName = $controller;
  81. $controller = ucwords($controller);
  82. $model = rtrim($controller, 's');
  83. $controller .= 'Controller';
  84. $dispatch = new $controller($model,$controllerName,$action);
  85. if((int)method_exists($controller, $action)){
  86. call_user_func_array(array($dispatch,$action),$queryString);
  87. }else{
  88. /* Error Generation Code Here */
  89. }
  90. }
  91. /**
  92. * Autoload any classes that are required
  93. *
  94. * @param string $className The class name that the autoloader automatically tries to load
  95. */
  96. function __autoload($className) {
  97. if(file_exists(ROOT_PATH . '/library/' . strtoupper($className) . '.php')){
  98. require_once(ROOT_PATH . '/library/' . strtoupper($className) . '.php');
  99. }elseif(file_exists(ROOT_PATH . '/library/' . strtolower($className) . '.php')){
  100. require_once(ROOT_PATH . '/library/' . strtolower($className) . '.php');
  101. }elseif(file_exists(ROOT_PATH . '/application/controllers/' . strtolower($className) . '.php')){
  102. require_once(ROOT_PATH . '/application/controllers/' . strtolower($className) . '.php');
  103. }elseif(file_exists(ROOT_PATH . '/application/models/' . strtolower($className) . '.php')){
  104. require_once(ROOT_PATH . '/application/models/' . strtolower($className) . '.php');
  105. }else{
  106. /* Error Generation Code Here */
  107. }
  108. }
  109. // prepare system for launch
  110. setReporting();
  111. removeMagicQuotes();
  112. unregisterGlobals();
  113. // create variable registry
  114. $registry = array();
  115. // get pointer to db and configure it
  116. $registry['db'] = new db;
  117. $registry['db']->database_driver(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  118. // get pointer for data validation
  119. $registry['data_validation'] = new data_validation;
  120. // get pointer for misc functions object
  121. $registry['functions'] = new functions;
  122. // get pointer for auth
  123. $registry['auth'] = new authentication;
  124. // get pointer for file management
  125. $registry['file_management'] = new file_management;
  126. // get pointer for module
  127. $registry['module'] = new module;
  128. // get pointer for navigation
  129. $registry['navigation'] = new navigation;
  130. // get pointer for security image
  131. $registry['security_image'] = new security_image;
  132. // launch mvc
  133. callHook();
  134. ?>