Example #1
0
 /**
  * Function used to draw a menu
  *
  * @param string $name The identifier of the menu
  */
 static function Draw($name)
 {
     $menu = Cache::Read('menus/' . $name);
     if (!$menu) {
         if (Database::Exists(LWC::QUERY_MENU, [':name', $name])) {
             $menu = Database::Data();
             if (Database::Fetch(LWC::QUERY_MENU_ITEMS, [':menu', $menu['id'], PDO::PARAM_INT])) {
                 foreach (Database::Data() as $item) {
                     if (Database::Fetch(LWC::QUERY_MENU_SUBITEMS, [[':parent', $item['id'], PDO::PARAM_INT], [':menu', $menu['id'], PDO::PARAM_INT]])) {
                         foreach (Database::Data() as $subitem) {
                             $item['subitems'][] = $subitem;
                         }
                     }
                     $menu['items'][] = $item;
                 }
             }
             Cache::Write('menus/' . $name, $menu);
         } else {
             return;
         }
     }
     if (session('rank') >= $menu['minrank'] && session('rank') <= $menu['maxrank']) {
         Template::Load('menu/navbar', $menu);
     }
 }
Example #2
0
 /**
  * This constructor will prepare this form
  *
  * Will read the requested form from the databse or from a cached file.
  *
  * @param string $name The name of the form to read
  * @param array $data This data will be used to fill out the form (specify 'field_name' => 'value')
  */
 function __construct($name = null, $data = array())
 {
     if (!is_null($name)) {
         $form = Cache::Read('forms/' . $name);
         // Get form from cache...
         if (!$form) {
             if (Database::Exists(LWC::QUERY_FORM, [':name', $name])) {
                 $form = Database::Data();
                 // Get the form
                 $fields = array();
                 if (Database::Fetch(LWC::QUERY_FORM_ITEMS, [':id', $form['id'], PDO::PARAM_INT])) {
                     foreach (Database::Data() as $row) {
                         if (isset($row['properties'])) {
                             $properties = explode('&', $row['properties']);
                             // ...and split them like HTTP parameters
                             foreach ($properties as $property) {
                                 $pair = explode('=', $property);
                                 // Explode each of the properties by =
                                 if (sizeof($pair) == 1) {
                                     $row[$pair[0]] = true;
                                 } else {
                                     if (sizeof($pair) == 2) {
                                         $row[$pair[0]] = $pair[1];
                                     }
                                 }
                                 // Otherwise we will set the proper value
                             }
                         }
                         unset($row['properties']);
                         // We unset the properties field after it has been parsed
                         $fields[$row['identifier']] = $row;
                         // The row is now prepared and can be used as field
                     }
                 } else {
                     Lightwork::Log('Form has no fields: ' . $form['identifier'], Lightwork::LOG_WARN);
                 }
                 $form['fields'] = $fields;
                 // Assign this fields to this form
             } else {
                 Lightwork::Log('Form does not exist: ' . $form['identifier'], Lightwork::LOG_WARN);
             }
         }
         Cache::Write('forms/' . $name, $form);
         // Write this form to the cache
         $this->form = $form;
         // Set the local form
         $this->data = $data;
         // Set the local form data from parameters
     }
 }
Example #3
0
 /**
  * Function used initialize the resource handler
  *
  * Will load all active stylesheets and javascripts from the database/cache into this class.
  */
 static function Initialize()
 {
     self::$stylesheets = Cache::Read('resources/stylesheets');
     // Try to load the stylesheets from cache
     if (!self::$stylesheets) {
         if (Database::Fetch(LWC::QUERY_RESOURCES_STYLES)) {
             foreach (Database::Data() as $stylesheet) {
                 self::$stylesheets[$stylesheet['name']] = $stylesheet;
                 // Add the stylesheet to the array
                 if (!is_file(CSS_DIR . $stylesheet['path'] . '.css')) {
                     if (!$stylesheet['source']) {
                         // ...and report so if no souce is available.
                         Lightwork::Log('Resource missing: ' . $stylesheet['name'], Lightwork::LOG_WARN);
                     } else {
                         self::MakeStylesheet($stylesheet['name'], STYLES);
                     }
                     // ...otherwise simply make it.
                 }
             }
             Cache::Write('resources/stylesheets', self::$stylesheets);
             // Write into cache
         }
     }
     self::$scripts = Cache::Read('resources/scripts');
     // Attempt to read scripts from database
     if (!self::$scripts) {
         if (Database::Fetch(LWC::QUERY_RESOURCES_SCRIPTS)) {
             foreach (Database::Data() as $script) {
                 self::$scripts[$script['name']] = $script;
                 // Add script to array
                 if (!is_file(JS_DIR . $script['path'] . '.js')) {
                     if (!$script['source']) {
                         Lightwork::Log('Resource missing: ' . $script['name'], Lightwork::LOG_WARN);
                     } else {
                         self::MakeScript($script['name'], STYLES);
                     }
                     // ...or compile it when a source is available.
                 }
             }
             Cache::Write('resources/scripts', self::$scripts);
             // Save to cache
         }
     }
     self::$initialized = true;
     // Initialized successfully
     return true;
 }
 /**
  * Function will determine which language should be used
  *
  * Allows comparing available languages with client languages, will use fallback if necessary.
  *
  * @return string Returns country code of language to use.
  */
 static function IdentifyLanguage()
 {
     self::$languages = Cache::Read('languages');
     if (!self::$languages) {
         if (Database::Fetch(LWC::QUERY_LANGUAGES)) {
             foreach (Database::Data() as $language) {
                 self::$languages[] = $language['code'];
             }
             Cache::Write('languages', self::$languages);
         } else {
             self::$languages = array(LANGUAGE);
         }
     }
     if (isset($_GET['lang'])) {
         if (in_array($_GET['lang'], self::$languages)) {
             cookie('language', $_GET['lang']);
             return $_GET['lang'];
         }
     } else {
         if (!cookieexists('language')) {
             $requested = self::ParseLanguages();
             foreach ($requested as $language) {
                 if (in_array($language, self::$languages)) {
                     cookie('language', $language);
                     return $language;
                 }
             }
         } else {
             if (in_array(cookie('language'), self::$languages)) {
                 return cookie('language');
             }
         }
     }
     return LANGUAGE;
 }
Example #5
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
 }