Advertisement
Guest User

Untitled

a guest
Jul 31st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.14 KB | None | 0 0
  1. class Lightbulb {
  2.  
  3. private static $instance;
  4. private static $model;
  5. private static $controller;
  6. public $mobile;
  7. public $module_check;
  8. public $module_class;
  9.  
  10. public $date = array();
  11. public $title;
  12.  
  13.  
  14. private function __construct(){
  15. include './system/config/constants.php';
  16.  
  17. $this->check_mobile();
  18.  
  19.  
  20. }
  21.  
  22. /*
  23. * This is called to load in all the assets
  24. * We call this function to avoid having duplicate
  25. * classes when calling the "get_instance" function
  26. *
  27. */
  28. public function loadAssets(){
  29. $config = $this->loadConfig();
  30. $this->routes();
  31.  
  32. }
  33.  
  34.  
  35. /*
  36. * Checks for a default module to load.
  37. * If there is one, then see if that was the path
  38. * specified.
  39. *
  40. *
  41. */
  42.  
  43. public function check_modules(){
  44. require CORE_PATH.'Module.php';
  45.  
  46.  
  47. $this->module_class = new Module();
  48. $module = $this->module_class;
  49.  
  50. $check = $module->mod_scan();
  51.  
  52.  
  53. if($check == false){
  54. $this->module_check = false;
  55. return $this->module_check;
  56. }else if($check != false){
  57. /*
  58. * If there are modules, check to see if there is a default
  59. * specified to load.
  60. * This returns the name of the module that needs to be load
  61. */
  62.  
  63. $db = $this->load_db();
  64.  
  65. $module = $db->where('value','setting', "name='default module");
  66.  
  67. //check to see if the path was what was specified.
  68. $page = $_GET['page'];
  69.  
  70. if($page == $module){
  71. return $module;
  72. }
  73.  
  74. }
  75. }
  76. /*
  77. * This is the singleton function that
  78. * allows the instance of Lightbulb to
  79. * be called anywhere in the framework.
  80. *
  81. */
  82. public static function get_instance(){
  83. if(!isset(self::$instance)){
  84. $c = __CLASS__;
  85. self::$instance = new $c;
  86. }
  87. return self::$instance;
  88. }
  89.  
  90.  
  91. /*
  92. * Loads core config file
  93. */
  94. public function loadConfig(){
  95.  
  96. if(file_exists(CONFIG_PATH.'/config.php')){
  97. require_once CONFIG_PATH.'/config.php';
  98.  
  99. }else{
  100.  
  101.  
  102. error_log("Couldn't find core config file!\n",3,"system/errors/framework-errors.log");
  103.  
  104. }
  105.  
  106.  
  107. }
  108.  
  109.  
  110.  
  111.  
  112. /*
  113. * Takes care of routing.
  114. * - First finds routes file(should be in app/config/routes.php)
  115. * - if found, build out the other classes following routing rules,
  116. * if not, then build out the other classes like normal.
  117. */
  118. public function routes(){
  119. /*
  120. * checking to see if theres a routes file
  121. */
  122. if(file_exists(CONFIG_PATH.'/routes.php')){
  123. require_once CONFIG_PATH.'/routes.php';
  124.  
  125. /*
  126. * If we're using routes
  127. */
  128. if($useRoutes == True){
  129.  
  130. $this->construct(true);
  131.  
  132. }else if($useRoutes == false){
  133. $this->construct(false);
  134. }
  135. }else{
  136. echo "fail";// require_once './site/system/errors/404.php';
  137. // error_log("Couldn't find core config file!\n",3,"./site/system/errors/framework-errors.log");
  138. }
  139. }
  140.  
  141. /*
  142. * Loads and constructs all the classes,
  143. * functions, etc...
  144. *
  145. */
  146. public function construct($routes){
  147. /*
  148. * start the chief Controller and Moduel classes.
  149. */
  150. require_once 'Controller.php';
  151.  
  152.  
  153. $controller = new Controller();
  154.  
  155.  
  156.  
  157. //load all the files from the controller folder
  158. $controllers = scandir(CONTROLLER_PATH,1);
  159.  
  160. //load route file
  161. include CONFIG_PATH.'routes.php';
  162.  
  163. //do we use routes?($useRoutes is defined in routes.php)
  164. if($useRoutes == false){
  165.  
  166. if(isset($_GET['page'])){
  167. $page = $_GET['page'];
  168.  
  169. $loader = $controller->load($page);
  170. }else{
  171. $page = "home";
  172. $loader = $controller->load($page);
  173. }
  174.  
  175. }else if($useRoutes == true){
  176. //first we need to find out how many rules there are
  177. $ruleCount = count($routes);
  178.  
  179. //load the route rules into a useable variable
  180. foreach($routes as $input => $output){
  181.  
  182.  
  183.  
  184.  
  185.  
  186. /*
  187. * Switch statement to take care of all the possible routing cases.
  188. *
  189. * If we only have one rule, its only the rule specifying the
  190. * default controller.
  191. *
  192. * So we handle that interaction, as well as any other URL perameters
  193. * that might have been passed in.
  194. *
  195. * Otherwise, we need to then account for all the rules and route
  196. * accordingly
  197. * --------------------------------------------------------------------
  198. * URL format:
  199. * (root)/index.php?page=(page name)&method=(method name)&var=(variable name)
  200. *
  201. * URL is re-written to look like
  202. * (root)/(page name)/(method name)/(variable name)
  203. *
  204. * $_GET is still useable if necessary.
  205. *
  206. *
  207. */
  208. switch ($ruleCount){
  209. case ($ruleCount == 1):
  210.  
  211. //first see if page variable is set
  212. if((!isset($_GET['page']))){
  213. //if not just load default controller
  214. $controller->load($routes['default']);
  215. }else{
  216. //otherwise start checking for the other variables too
  217. if((!isset($_GET['method']))&&(!isset($_GET['var']))){
  218. //if the method or var variables aren't set, then load the requested page
  219.  
  220. $page = $_GET['page'];
  221. $controller->load($page);
  222.  
  223. }else if((isset($_GET['method']))||(isset($_GET['var']))){
  224.  
  225. //if they are set, load page and method, also do last check for var
  226. $page = $_GET['page'];
  227. $method = $_GET['method'];
  228.  
  229. if(isset($_GET['var'])){
  230. $var = $_GET['var'];
  231. }
  232.  
  233. //load everything
  234. $controller->load($page,$method,$var);
  235. }
  236.  
  237. }
  238. break;
  239.  
  240. case($ruleCount>1):
  241.  
  242. $page = $_GET['page'];
  243. $method = $_GET['method'];
  244. $var = $_GET['var'];
  245.  
  246. /*
  247. * If we have a match with a rule,
  248. * and there isn't a method, load the rule its replacing.
  249. */
  250. if(($page == $input)&&($method == "")){
  251. $controller->load($output);
  252. }else if($method != ""){
  253.  
  254. //first check for a variable
  255. if($var != ""){
  256.  
  257. $controller->load($output,$method,$var);
  258. }else{
  259.  
  260. $controller->load($ouput,$method);
  261. }
  262.  
  263.  
  264.  
  265. }else{
  266. error_log("Potential rule mismatch in routing\n",3,"./system/errors/framework-errors.log");
  267.  
  268. }
  269.  
  270. break;
  271.  
  272.  
  273. }
  274.  
  275.  
  276.  
  277. }
  278. }
  279. }//end of function
  280.  
  281.  
  282.  
  283.  
  284.  
  285. /*
  286. * Loads libraries
  287. */
  288. public function load_library($library){
  289. require LIBRARY_PATH.$library.'.php';
  290. $class = $this->startClass(new $library());
  291.  
  292. return $class;
  293.  
  294. }
  295.  
  296. public function load_module($name){
  297.  
  298. $path = "./system/app/modules/$name/$name.php";
  299. if(file_exists($path)){
  300. require $path;
  301. $class = $this->startClass(new $name());
  302. return $class;
  303. }else{
  304. echo "module doesn't exist";
  305.  
  306. }
  307. }
  308.  
  309. /*
  310. * Function helps to automatically create new instance of
  311. * libraries called by the "loadClass" function.
  312. */
  313. public function startClass($class){
  314. return $class;
  315. }
  316.  
  317. /*
  318. * Load models
  319. */
  320. public function load_model($name){
  321. require 'Model.php';
  322.  
  323.  
  324. $object;
  325. $path = MODEL_PATH.$name.'.php';
  326.  
  327. if((file_exists($path))&&(!class_exists($name))){
  328. require $path;
  329.  
  330. $object = new $name();
  331. return $object;
  332. }else{
  333. error_log("Sorry, could not locate model $name");
  334. }
  335.  
  336. }
  337.  
  338.  
  339.  
  340. /*
  341. * Database interaction function
  342. * Again, MySQL is the assumed default.
  343. * Loads default values for host, username and password.
  344. *
  345. * For flexibility in selecting multiple db's,
  346. * it loads default database specified in Config, but if you pass in a name,
  347. * it'll load that database instead
  348. */
  349.  
  350. public function load_db($dbName=null){
  351. global $database;
  352. include CONFIG_PATH.'config.php';
  353.  
  354.  
  355. $dbUser = $config['database']['dbUser'];
  356. $dbHost = $config['database']['dbHost'];
  357. $dbPass = $config['database']['dbPass'];
  358.  
  359. //if there isn't a DB name name stated, run with the default name.
  360. if($dbName == ""){
  361. $db = $config['database']['dbName'];
  362. }else{
  363. $db = $dbName;
  364. }
  365.  
  366. $database = new DataFetch($dbUser, $dbPass, $dbHost, $db);
  367.  
  368. if(get_class ($database) != ""){
  369. return $database;
  370. echo "instance exists";
  371. }else{
  372. $database = new DataFetch($dbUser, $dbPass, $dbHost, $db);
  373.  
  374. return $database;
  375. echo "needed to start a new instance";
  376. }
  377.  
  378.  
  379. }
  380.  
  381. /*
  382. * Mobile device check:
  383. * gonna see if user is using a mobile device
  384. */
  385.  
  386. public function check_mobile(){
  387. require './system/config/user-agents.php';
  388. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  389.  
  390. $count;
  391. for($i = 0;$i= 1){
  392. $this->mobile = "yes";
  393.  
  394. }else{
  395. $this->mobile = "no";
  396. }
  397.  
  398. }
  399. public function test(){
  400. echo "testing";
  401. }
  402. }
  403. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement