PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/meagerBase.php

https://github.com/mcgrew/meager
PHP | 547 lines | 194 code | 40 blank | 313 comment | 27 complexity | 38386b9550cec29840ed7f8dc7c2a686 MD5 | raw file
  1. <?php
  2. if ( !defined( '_VALID_' )) define( '_VALID_', true );
  3. session_start( );
  4. /*
  5. Class: Module
  6. This class is not intended to be instantiated directly, but instead will be
  7. instantiated by a ModuleHandler object when a module is registered.
  8. Syntax:
  9. (start code)
  10. new Module( );
  11. (end)
  12. Example:
  13. (start code)
  14. $modules = new ModuleHandler( );
  15. $modules->register( 'test', 'test.php', array( 'option1' => 'yes' ));
  16. $test = $modules->get( 'test' );
  17. (end)
  18. */
  19. Class Module
  20. {
  21. private $options = array( );
  22. private $file;
  23. private $name;
  24. function __construct( $name, $file, $options = array( )) {
  25. global $config;
  26. if ( substr( $file, 0, 1 ) == "/" )
  27. $this->file = $file;
  28. else
  29. $this->file = meager_config( 'doc_root' ).meager_config( 'module_dir' ).$file;
  30. $this->name = $name;
  31. $this->set_opts( $options );
  32. }
  33. /*
  34. Method: load
  35. Print out the output of a module.
  36. */
  37. function load( ) {
  38. global $modules;
  39. global $config;
  40. $mod = $this;
  41. $params = $this; // Joomla module compatibility.
  42. include( $this->file );
  43. }
  44. /*
  45. Method: to_string
  46. Retrive the output of a module.
  47. Returns:
  48. A string containing the output of a module.
  49. */
  50. function to_string( ) {
  51. ob_start( );
  52. $this->load( );
  53. $returnvalue = ob_get_clean( );
  54. return $returnvalue;
  55. }
  56. /*
  57. Method: set_opts
  58. Set options for a module
  59. Arguments:
  60. options - An associative array containing the options to be set.
  61. Returns:
  62. True if the module exists, false otherwise.
  63. */
  64. function set_opts( $options ) {
  65. $this->options = array_merge( $this->options, $options );
  66. }
  67. /*
  68. Method: set_opt
  69. Set a single option for a module.
  70. Arguments:
  71. name - A string containing the name of the option to be set.
  72. value - The value for the option.
  73. */
  74. function set_opt( $name, $value ) {
  75. $this->set_opts( array( $name => $value ));
  76. }
  77. /*
  78. Method: get_opts
  79. Get the options for a module.
  80. Returns:
  81. An associative array containing the options for the module.
  82. */
  83. function get_opts( ) {
  84. return $this->options;
  85. }
  86. /*
  87. Method: get_opt
  88. Get the value for an option
  89. Arguments:
  90. name - A string containing the name of the option.
  91. default - A default value to use if the option is not set.
  92. Returns:
  93. The value of the option if it is set, or the value of $default if
  94. it is not.
  95. */
  96. function get_opt( $name, $default=null ) {
  97. if ( isset( $this->options[ $name ]) && $this->options[ $name ] !== null )
  98. return $this->options[ $name ];
  99. return $default;
  100. }
  101. /*
  102. Method: get
  103. Get the value for an option (Joomla module compatibility)
  104. Arguments:
  105. name - A string containing the name of the option.
  106. default - A default value to use if the option is not set.
  107. Returns:
  108. The value of the option if it is set, or the value of $default if
  109. it is not.
  110. */
  111. function get( $name, $default=null ) {
  112. return $this->get_opt( $name, $default );
  113. }
  114. /*
  115. Method: get_filename
  116. Get the filename registered for a module
  117. Returns:
  118. A string containing the filename for the module.
  119. */
  120. function get_filename( ) {
  121. return $this->file;
  122. }
  123. /*
  124. Method: get_name
  125. Get the name registered for a module
  126. Returns:
  127. A string containing the name for the module.
  128. */
  129. function get_name( ) {
  130. return $this->name;
  131. }
  132. }
  133. /*
  134. Class: ModuleHandler
  135. Syntax:
  136. (start code)
  137. new ModuleHandler( );
  138. (end)
  139. Example:
  140. (start code)
  141. $modules = new ModuleHandler( );
  142. $modules->register( 'test', 'test.php', array( 'option1' => 'yes' ));
  143. (end)
  144. */
  145. Class ModuleHandler
  146. {
  147. private $registry = array( );
  148. function __construct( ) { }
  149. /*
  150. Method: register
  151. Register a module for later use.
  152. Arguments:
  153. name - A string containing the name for the module.
  154. file - A string containing the php filename for the module
  155. options (optional) - An associative array containing any options which the
  156. module might require.
  157. */
  158. function register( $name, $file, $options=array( )) {
  159. $module = new Module( $name, $file );
  160. $module->set_opts( $options );
  161. $this->registry[ $name ] = $module;
  162. return $module;
  163. }
  164. /*
  165. Method: exists
  166. Determine whether a module has been registered.
  167. Arguments:
  168. name - A string containing the name for the module.
  169. Returns:
  170. True if the module exists, false otherwise.
  171. */
  172. function exists( $name ) {
  173. return isset( $this->registry[ $name ]);
  174. }
  175. /*
  176. Method: get
  177. Get a module object that has been registered.
  178. Arguments:
  179. name - A string containing the name for the module.
  180. Returns:
  181. The module object requested.
  182. */
  183. function get( $name ) {
  184. if ( $this->exists( $name ))
  185. return $this->registry[ $name ];
  186. return false;
  187. }
  188. /*
  189. Method: load
  190. Print out the output of a module.
  191. Arguments:
  192. name - A string containing the name for the module.
  193. */
  194. function load( $name ) {
  195. if ( $this->exists( $name )) {
  196. $this->get( $name )->load( );
  197. return true;
  198. }
  199. error_log( meager_current_page( ).": The module '$name' is not registered" );
  200. return false;
  201. }
  202. /*
  203. Method: to_string
  204. Retrieve the output of a module as a string.
  205. Arguments:
  206. name - A string containing the name for the module.
  207. Returns:
  208. A string containing the output of the module.
  209. */
  210. function to_string( $name ) {
  211. if ( $this->exists( $name )) {
  212. return $this->get( $name )->to_string( );
  213. }
  214. }
  215. /*
  216. Method: set_opts
  217. Set options for a module.
  218. Arguments:
  219. name - A string containing the name for the module.
  220. options - An associative array containing the options to be set.
  221. Returns:
  222. True if the module exists, false otherwise.
  223. */
  224. function set_opts( $name, $options ) {
  225. if ( $this->exists( $name )) {
  226. $this->get( $name )->set_opts( $options );
  227. return true;
  228. }
  229. }
  230. /*
  231. Method: set_opt
  232. Set a single option for a module.
  233. Arguments:
  234. name - A string containing the name for the module.
  235. opt_name - A string containing the name of the option to be set.
  236. opt_value - The value for the option.
  237. Returns:
  238. True if the module exists, false otherwise.
  239. */
  240. function set_opt( $name, $opt_name, $opt_value ) {
  241. if ( $this->exists( $name )) {
  242. $this->get( $name )->set_opt( $opt_name, $opt_value );
  243. return true;
  244. }
  245. return false;
  246. }
  247. /*
  248. Method: get_opt
  249. Get the value for an option
  250. Arguments:
  251. name - A string containing the name for the module.
  252. opt_name - A string containing the name of the option.
  253. default - A default value for the option to be returned if one is not set.
  254. Returns:
  255. The option value for the module if it exists, false otherwise.
  256. */
  257. function get_opt( $name, $opt_name, $default=null ) {
  258. if ( $this->exists( $name )) {
  259. return $this->get( $name )->get_opt( $opt_name, $default );
  260. }
  261. return false;
  262. }
  263. /*
  264. Method: get_opts
  265. Get the options for a module.
  266. Arguments:
  267. name - A string containing the name for the module.
  268. Returns:
  269. An associative array containing the options for the module.
  270. */
  271. function get_opts( $name ) {
  272. if ( $this->exists( $name )) {
  273. return $this->get( $name )->get_opts( );
  274. }
  275. return false;
  276. }
  277. /*
  278. Method: get_filename
  279. Get the filename registered for a module
  280. Arguments:
  281. name - A string containing the name for the module.
  282. Returns:
  283. A string containing the filename for the module.
  284. */
  285. function get_filename( $name ) {
  286. if ( $this->exists( $name )) {
  287. return $this->get( $name )->get_filename( );
  288. return false;
  289. }
  290. }
  291. }
  292. $modules = new ModuleHandler( );
  293. /*
  294. Function: meager_config
  295. Gets a configuration setting from the $config array
  296. */
  297. function meager_config( $name, $default=null ) {
  298. global $config;
  299. if ( isset( $config[ $name ]))
  300. return $config[ $name ];
  301. return $default;
  302. }
  303. /*
  304. Function: meager_current_page
  305. Get the filename of the page to be displayed.
  306. Returns:
  307. A string containing the filename.
  308. */
  309. function meager_current_page( ) {
  310. $meager_current_page = @$_REQUEST[ 'page' ];
  311. if ( !file_exists( $meager_current_page )) {
  312. if ( file_exists( meager_config( 'doc_root' ).$meager_current_page.".php" )) $meager_current_page .= '.php';
  313. if ( file_exists( meager_config( 'doc_root' ).$meager_current_page.".html" )) $meager_current_page .= '.html';
  314. } else if ( is_dir( meager_config( 'doc_root' ).$meager_current_page ))
  315. $meager_current_page = meager_find_index_for_dir( $meager_current_page );
  316. return $meager_current_page;
  317. }
  318. /*
  319. Function: meager_find_index_for_dir
  320. Internal function which will return the index file for a directory passed in.
  321. */
  322. function meager_find_index_for_dir( $dir ) {
  323. foreach ( meager_config( 'index_files' ) as $file ) {
  324. if ( file_exists( meager_config( 'doc_root' ).$dir.'/'.$file ) && !is_dir( meager_config( 'doc_root' ).$dir.'/'.$file ))
  325. return $dir.'/'.$file;
  326. }
  327. return false;
  328. }
  329. /*
  330. Function: meager_get_template
  331. Get the template to be used. This can be overridden with the 'template' GET or POST variable
  332. Returns:
  333. The directory name for the template to be used.
  334. */
  335. function meager_get_template( ) {
  336. global $special_templates;
  337. global $config;
  338. if ( isset( $_REQUEST[ 'template' ] ) and is_dir( 'meager/templates/'.($template = str_replace( '/', '', $_REQUEST[ 'template' ]))))
  339. return $_REQUEST[ 'template' ];
  340. foreach( $special_templates as $template => $active )
  341. if ( $active )
  342. return $template;
  343. return $config[ 'template' ];
  344. }
  345. /*
  346. Function: redirect
  347. Sends redirect header to the browser, or embeds a javascript redirect if
  348. headers have already been sent.
  349. */
  350. function redirect( $location ) {
  351. if( $location ) {
  352. if ( headers_sent( )) {
  353. echo "<script type='text/javascript'>";
  354. echo "window.location=\"$location\"";
  355. echo "</script>";
  356. } else {
  357. header( "Location: http://". meager_config('http_host') . $location, true, 307 );
  358. }
  359. }
  360. }
  361. /*
  362. Function: print_array
  363. A debug function - prints an array in a human readable format.
  364. */
  365. function print_array($array)
  366. {
  367. echo "<div style=\"font-family: 'Courier New', Courier, monospace; font-size: 12px;\">\n";
  368. print_array_recursive($array, '');
  369. echo "</div>\n";
  370. }
  371. function print_array_recursive($array, $prefix)
  372. {
  373. foreach ($array as $k=>$v)
  374. {
  375. if (is_array($v))
  376. {
  377. echo $prefix.$k.' =&gt; '."<br>\n";
  378. print_array_recursive($v, $prefix."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
  379. }
  380. else
  381. {
  382. echo $prefix.$k.' =&gt; '.$v."<br>\n";
  383. }
  384. }
  385. }
  386. /**
  387. * A function for safely including a file based on user input.
  388. *
  389. * @param $file The file name to be sanitized.
  390. */
  391. function meager_safe_include( $file ) {
  392. include( "./" . str_replace( '../', '', $file ));
  393. }
  394. ob_start( );
  395. require_once( 'meager/config/configuration.php' );
  396. $meager_current_page = meager_current_page( );
  397. // figure out the http host name (or set this manually if something fails)
  398. $config[ 'http_host' ] = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ?
  399. $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'];
  400. // set the root directory for meager.
  401. $config[ 'http_root' ] = dirname( $_SERVER[ "SCRIPT_NAME" ]);
  402. $config[ 'doc_root' ] = $_SERVER[ 'DOCUMENT_ROOT' ].$config[ 'http_root' ].'/';
  403. require_once( 'meager/config/modules.php' );
  404. foreach ( glob( "meager/include/*.php" ) as $file ) {
  405. require_once( $file );
  406. }
  407. // Register any modules/options contained in these variables.
  408. if ( isset( $module_list ))
  409. foreach( $module_list as $name => $file )
  410. $modules->register( $name, $file );
  411. if ( isset( $module_options ))
  412. foreach( $module_options as $name => $opts )
  413. $modules->set_opts( $name, $opts );
  414. $modules->register( 'content', $config[ 'doc_root' ] ."/". $meager_current_page );
  415. if ( !file_exists( $meager_current_page ))
  416. $modules->register( 'content', $modules->get_filename( 'STATUS_404' ));
  417. include( 'meager/templates/'.meager_get_template( ).'/index.php' );
  418. // tidy up the html if the null template isn't being used and $config[ 'tidy' ] is true
  419. if ( meager_config( 'tidy', false ) && meager_get_template( ) != 'null' ) {
  420. $tidy = tidy_parse_string( ob_get_clean( ));
  421. $tidy->cleanRepair( );
  422. echo tidy_get_output( $tidy );
  423. } else {
  424. ob_end_flush( );
  425. }
  426. ?>