Example #1
0
 /**
  * Function to get the "heart" beating
  *
  * After all the nasty PHP stuff is done (sessions, database, error handling, configuration, ...) we can finally get started with Lightwork...
  *
  * @param mixed $status If set together with $message an error page will be forced to display.
  * @param string $message If set together with $status an error page will be forced to display.
  */
 static function Initialize($status = null, $message = null)
 {
     if (isset($_GET['page']) || $status != null) {
         header('Content-Type: text/html; charset=utf-8');
         // Twice is better than once - set the charset and content-type
         header('Content-Language: ' . Translation::Language());
         // Set the content-language to the current translation language
         self::$pages = Cache::Read('pages');
         // Try to read all pages from cache
         if (!self::$pages) {
             if (Database::Fetch(LWC::QUERY_PAGES)) {
                 self::$pages = Database::Data();
                 // We got the pages, saving them...
                 Cache::Write('pages', self::$pages);
                 // ...and caching them.
             }
         }
         ResourceHandler::Initialize();
         // Initialize the resource handler - essentially it loads all stylesheets and javascripts
         if (DEBUG_MAKEALL) {
             ResourceHandler::MakeAll(STYLES);
         }
         // Incase it is required we will compile & minify all stylesheets and scripts (might make requests slow - debugging only)
         if ($status == null) {
             if (!empty($_GET['page'])) {
                 self::HandlePage($_GET['page']);
             } else {
                 self::HandlePage();
             }
             // Page parameter is empty or not set - page handler will use index by default
             unset($_GET['page']);
             // Unset the original page parameter from $_GET array...
             unset($_REQUEST['page']);
             // ...and $_REQUEST array.
         } else {
             self::SetError($status, $message);
         }
         // Since we need to force an error we are doing so now.
         self::$request = self::REQUEST_PAGE;
         // Set the request type
         Lightwork::Log(sprintf('This is a page request. (%s)', self::$page['key']), Lightwork::LOG_DEBUG);
         // Log this successfully handled request
     } else {
         if (isset($_GET['script'])) {
             self::$scripts = Cache::Read('scripts');
             // Attempt to read enabled scripts from cache
             if (!self::$scripts) {
                 if (Database::Fetch(LWC::QUERY_SCRIPTS)) {
                     self::$scripts = Database::Data();
                     // We got the scripts, lets save them...
                     Cache::Write('scripts', self::$scripts);
                     // ...and cache them.
                 }
             }
             if (!empty($_GET['script'])) {
                 self::HandleScript($_GET['script']);
             } else {
                 self::SetError('error', 'ERROR_BAD_REQUEST');
             }
             //There is no default so we fail...
             unset($_GET['script']);
             // Unset the original script from $_GET...
             unset($_REQUEST['script']);
             // ...and $_REQUEST.
             self::$request = self::REQUEST_SCRIPT;
             // Set this request as a script request
             Lightwork::Log(sprintf('This is a script request. (%s)', self::$script['key']), Lightwork::LOG_DEBUG);
             // Log this successful script handling
         } else {
             if (isset($_GET['file'])) {
                 self::$files = Cache::Read('files');
                 // Attempt to read available files from cache
                 if (!self::$files) {
                     if (Database::Fetch(LWC::QUERY_FILES)) {
                         self::$files = Database::Data();
                         // We got the files, saving them for now...
                         Cache::Write('files', self::$files);
                         // ...and caching them for later.
                     }
                 }
                 // Grab file from parameters, no default
                 if (!empty($_GET['file'])) {
                     self::HandleFile($_GET['file']);
                 }
                 unset($_GET['file']);
                 // Unset the original file from $_GET...
                 unset($_REQUEST['file']);
                 // ...and $_REQUEST.
                 self::$request = self::REQUEST_FILE;
                 // Mark this request as a file request
                 Lightwork::Log(sprintf('This is a file request. (%s)', self::$file['path']), Lightwork::LOG_DEBUG);
                 // Log this successful file handling
             } else {
                 if (isset($_GET['cronjob'])) {
                     self::$cronjobs = Cache::Read('cronjobs');
                     // Attempt to read available cronjobs from cache
                     if (!self::$cronjobs) {
                         if (Database::Fetch(LWC::QUERY_CRONJOBS)) {
                             self::$cronjobs = Database::Data();
                             // We got the cronjobs, saving them...
                             Cache::Write('cronjobs', self::$cronjobs);
                             // ...and caching them.
                         } else {
                             self::$cronjobs = array();
                         }
                         // Return no cronjobs if we couldn't get any cronjobs
                     }
                     self::HandleCronjob($_GET['cronjob']);
                     // Handle a single cronjob if necessary
                     self::$request = self::REQUEST_CRONJOB;
                     // Mark this request as a cronjob request
                     Lightwork::Log('This is a cronjob request.', Lightwork::LOG_DEBUG);
                     // Log this successful cronjob handling
                 } else {
                     if (isset($_GET['sitemap'])) {
                         // Load pages
                         self::$pages = Cache::Read('pages');
                         // Since this is the sitemap we still need to load the pages from cache...
                         if (!self::$pages) {
                             if (Database::Fetch(LWC::QUERY_PAGES)) {
                                 self::$pages = Database::Data();
                                 // ...or database.
                                 Cache::Write('pages', self::$pages);
                             }
                         }
                         self::$request = self::REQUEST_SITEMAP;
                         Lightwork::Log('This is a sitemap request.', Lightwork::LOG_DEBUG);
                     } else {
                         Lightwork::Log('This is an invalid request!', Lightwork::LOG_DEBUG);
                         // We will output a simple log message if we received an invalid request...
                         echo 'Lightwork was not set up properly! (invalid rewrite rules)';
                         // ...and output an error message.
                         die;
                     }
                 }
             }
         }
     }
     self::$initialized = true;
     // Initialization is done
 }