PageRenderTime 162ms CodeModel.GetById 86ms RepoModel.GetById 2ms app.codeStats 0ms

/public/codeCore/Classes/php/MooKit.php

https://github.com/IAmCorbin/MooKit
PHP | 208 lines | 100 code | 8 blank | 100 comment | 25 complexity | 03ecb123574c0ea2719ccc54800e27c8 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * contains MooKit Class
  4. * @package MooKit
  5. */
  6. /**
  7. * A Class used to build a new MooKit application
  8. *
  9. * @author Corbin Tarrant
  10. * @copyright March 29th, 2010
  11. * @package MooKit
  12. */
  13. class MooKit {
  14. /** @var string $VERSION MooKit Version */
  15. var $VERSION = '0.1.0';
  16. /** @var array $scriptsPublic Public JavaScripts */
  17. var $scriptsPublic;
  18. /** @var array $scriptsSecure Authorized JavaScripts */
  19. var $scriptsSecure;
  20. /** @var array $stylesPublic Public CSS */
  21. var $stylesPublic;
  22. /** @var array $stylesSecure Authorized CSS */
  23. var $stylesSecure;
  24. /** @var Template $main Main Template */
  25. var $main;
  26. /**
  27. * Constructor
  28. *
  29. * Handles the request and returns requested files if allowed
  30. * @param string $request The application request $_GET['request'] - this is set by .htaccess
  31. * For Reference:
  32. * ## Force connections through index.php for handling
  33. * ## if not already index.php
  34. * RewriteCond %{REQUEST_URI} !/index\.php$
  35. * ## and request has not already been set
  36. * RewriteCond %{QUERY_STRING} !request=
  37. * RewriteRule ^(.+)$ /index.php?request=$1 [L]
  38. */
  39. public function __construct($request) {
  40. //Functions
  41. require_once ROOT_DIR.'codeCore/php/htmLawed1.1.9.1.php';
  42. require_once ROOT_DIR.'codeCore/php/functions.php';
  43. require_once ROOT_DIR.'codeSite/php/functions.php';
  44. //Start Session and regenerate Session ID for security
  45. session_start();
  46. session_regenerate_id();
  47. $_SESSION['SYSNAME'] = 'MooKit';
  48. //Handle Request
  49. new RequestHandler($request);
  50. }
  51. /**
  52. * Application Initialization
  53. *
  54. *@param string $mainTpl The Main Template Location
  55. */
  56. public function INIT($mainTpl='templates/main.tpl.php') {
  57. //Setup main Template
  58. $this->main = new Template($mainTpl);
  59. //initialize arrays
  60. $this->scriptsPublic = array();
  61. $this->scriptsSecure = array();
  62. $this->stylesPublic = array();
  63. $this->stylesSecure = array();
  64. //set core JavaScripts
  65. $this->addScript('codeCore/Classes/js','mootools-1.2.4-core-nc.js');
  66. $this->addScript('codeCore/Classes/js','mootools-1.2.4.4-more.js');
  67. if(DEBUG) $this->addScript('codeCore/js','debug.js'); else array_push($this->scriptsPublic,'<script type="text/javascript"> var DEBUG = false </script>');
  68. $this->addScript('codeCore/js','errorHandler.js');
  69. //load core JavaScript Classes
  70. $this->addScript('codeCore/Classes/js','LightBox.js');
  71. $this->addScript('codeCore/Classes/js','DeepLinker.js');
  72. $this->addScript('codeCore/Classes/js','PaginatingTable.js');
  73. $this->addScript('codeCore/Classes/js','SortingTable.js');
  74. }
  75. /**
  76. * Add a new CSS stylesheet
  77. *
  78. * @param string $dir stylesheet location - directory
  79. * @param string $style stylesheet location - file
  80. * @param bool $secure Secure switch, only allow for authorized users
  81. */
  82. public function addStyle($dir,$style, $secure=NULL) {
  83. //make sure file is .css or .css.php
  84. if( preg_match("/\.css$/",$style) || preg_match("/\.css\.php$/",$style)) {
  85. if($secure) {
  86. if(Security::clearance()) { //secure, use Regex to strip directory location and .css or .css.php
  87. array_push($this->stylesSecure,'<link id="CSS'.preg_replace("/\.css$/","",preg_replace("/\.css\.php$/","",preg_replace("/.+\//","",$style))).'" rel="stylesheet" type="text/css" href="'.$dir.'/'.$style.'" />');
  88. }
  89. } else //public
  90. array_push($this->stylesPublic,'<link rel="stylesheet" type="text/css" href="'.$dir.'/'.$style.'" />');
  91. }
  92. }
  93. /**
  94. * Add a new JavaScript file
  95. *
  96. * @param bool $dir JavaScript location - directory
  97. * @param bool $script JavaScript location - file
  98. * @param bool $secure Secure switch, only allow for authorized users
  99. */
  100. public function addScript($dir, $script, $secure=NULL) {
  101. //make sure file is .js
  102. if( preg_match("/\.js$/",$script) ) {
  103. if($secure) {
  104. if(Security::clearance()) { //secure, use Regex to strip directory location and '.js'
  105. array_push($this->scriptsSecure,'<script type="text/javascript" id="JS'.preg_replace("/\.js$/","",preg_replace("/.+\//","",$script)).'" src="'.$dir.'/'.$script.'"></script>');
  106. }
  107. } else //public
  108. array_push($this->scriptsPublic,'<script type="text/javascript" src="'.$dir.'/'.$script.'"></script>');
  109. }
  110. }
  111. /**
  112. * Cache a php script
  113. */
  114. public function cachePHP($path, $cachefile=NULL) {
  115. if(!$cachefile)
  116. $cachefile = 'cache/'.preg_replace('/\//','_',$path);
  117. //start the output buffer
  118. ob_start();
  119. //output template to buffer
  120. require $path;
  121. // open the cache file for writing
  122. $file = fopen($cachefile, 'w');
  123. // save the contents of the output buffer to the file
  124. fwrite($file, ob_get_contents());
  125. //turn off output buffer
  126. ob_end_clean();
  127. // close the file
  128. fclose($file);
  129. }
  130. /**
  131. * Cache contents of a template to a file
  132. * @param Template $template the template to cache
  133. * @param string $cachefile [path and] filename of cache file
  134. */
  135. public function cacheTpl(&$tpl, $cachefile="cache/cache.htm") {
  136. //start the output buffer
  137. ob_start();
  138. //output template to buffer
  139. echo $tpl;
  140. // open the cache file for writing
  141. $file = fopen($cachefile, 'w');
  142. // save the contents of the output buffer to the file
  143. fwrite($file, ob_get_contents());
  144. //turn off output buffer
  145. ob_end_clean();
  146. // close the file
  147. fclose($file);
  148. }
  149. /**
  150. * Runs the application, outputs to the user's browser
  151. *
  152. * @param bool styles switch styles on/off
  153. * @param bool scripts switch scripts on/off
  154. */
  155. public function RUN($styles=TRUE,$scripts=TRUE) {
  156. if($styles) {
  157. //Grab all public stylesheets - all in style/
  158. foreach(new DirectoryIterator('style') as $style) {
  159. //make sure file is .css or .css.php
  160. if( preg_match("/\.css$/",$style) || preg_match("/\.css\.php$/",$style))
  161. $this->addStyle('style',$style);
  162. }
  163. //if secure, add secure stylesheets - all in style/secure/
  164. if(Security::clearance()) { foreach(new DirectoryIterator('style/secure') as $style) {
  165. //make sure file is .css or .css.php
  166. if( preg_match("/\.css$/",$style) || preg_match("/\.css\.php$/",$style))
  167. $this->addStyle('style/secure',$style,'secure'); }}
  168. //set all styles for main template
  169. $this->main->styles = array_merge($this->stylesPublic, $this->stylesSecure);
  170. }
  171. if($scripts) {
  172. //LOAD codeCore JavaScripts
  173. //Grab all public JavaScripts - all in codeCore/js/
  174. foreach(new DirectoryIterator('codeCore/js') as $script) {
  175. //do not include debug.js here - that is handled in the constructor
  176. if($script != 'debug.js')
  177. //make sure file is .js
  178. if(preg_match("/\.js$/",$script))
  179. $this->addScript('codeCore/js',$script);
  180. }
  181. //LOAD codeSite JavaScripts
  182. //Grab all public JavaScripts - all in codeSite/js/
  183. foreach(new DirectoryIterator('codeSite/js') as $script) {
  184. //make sure file is .js
  185. if(preg_match("/\.js$/",$script))
  186. $this->addScript('codeSite/js',$script);
  187. }
  188. //set all scripts for main template
  189. $this->main->scripts = array_merge($this->scriptsPublic, $this->scriptsSecure);
  190. }
  191. if(DEBUG) /* add debug area */
  192. $this->main->debugTpl = new Template('templates/debug.tpl.php');
  193. else $this->main->debugTpl = null;
  194. //cache page
  195. //$this->cacheTpl($this->main);
  196. //send output gzip encoded to browser
  197. echo $this->main->run(false);
  198. }
  199. }
  200. ?>