/** * Function used to check the integrity * * This function will loop through all folders/files and check if their permissions are okay. * This function is not finished yet. * * @todo */ static function Check() { foreach (self::$folders as $folder => $permissions) { if (is_dir(DIR . $folder)) { if ($permissions == 'r') { if (!is_readable(DIR . $folder)) { Lightwork::Log($folder . ' is not readable.', Lightwork::LOG_ERROR); } if (is_writeable(DIR . $folder)) { Lightwork::Log($folder . ' is writeable but should be read only.', Lightwork::LOG_WARN); } } else { if ($permissions == 'w') { if (!is_readable(DIR . $folder)) { Lightwork::Log($folder . ' is not readable.', Lightwork::LOG_ERROR); } if (!is_writeable(DIR . $folder)) { Lightwork::Log($folder . ' is not writeable.', Lightwork::LOG_ERROR); } } } } else { Lightwork::Log('Folder does not exist: ' . $folder, Lightwork::LOG_ERROR); } } }
function execute($cronjob) { ob_start(); // Start output caching require CRONJOB_DIR . $cronjob['path'] . '.php'; // Require cronjob ob_end_clean(); // Omit cached output Lightwork::Log('Cronjob executed: ' . $cronjob['name']); // Log success Database::Query(LWC::QUERY_CRONJOB_UPDATE, [':id', $cronjob['id'], PDO::PARAM_INT]); // Update timestamp in database }
/** * 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 } }
/** * Works like Template::Draw() but for dynamic files * * Some things like loops or database queries are not possible using the regular HTML templating. * You can use this function to include php files in the template directory instead. * * @param string $name The name of the template * @param array $_PARAMS Multiple parameters to give to the template */ static function Load($name, $_PARAMS = array()) { if (is_file(TEMPLATE_DIR . $name . '.php')) { include TEMPLATE_DIR . $name . '.php'; } else { if (is_file(INTERNAL_TEMPLATE_DIR . $name . '.php')) { include INTERNAL_TEMPLATE_DIR . $name . '.php'; } else { Lightwork::Log('Tried to load missing template: ' . $name, Lightwork::LOG_DEBUG); } } }
/** * Function used to run the garbage collector * * This function is called by PHP at regular intervals to clean up unused sessions. * The lifetime of a session can be set in the configuration file. * * @param string $lifetime The lifetime. Anything lower than the lifetime will be removed from the database permanently. */ public function gc($lifetime) { if (DEBUG) { Lightwork::Log('Running session garbage collector...', Lightwork::LOG_DEBUG); } return Database::Query(LWC::QUERY_SESSION_GC, [[':lifetime', time() - intval($lifetime), PDO::PARAM_INT]]); }
/** * Short function to get translation * * Acts as a _() alternative. Will also log missing translations. * * @param string $key The key to translate (example: HELLO_WORLD) * * @return string Will return proper translation, hard-coded value or given key. */ function t($key) { if (!empty($key)) { $translation = Translation::Get($key); if (!is_null($translation)) { return $translation; } else { if ($key == 'PAGE_TITLE') { return PAGE_TITLE; } else { if ($key == 'YEAR') { return date('Y'); } else { if ($key == 'USERNAME' && LOGGEDIN) { return session('username'); } else { Lightwork::Log(sprintf('Missing translation key: %s (%s)', $key, Translation::Language()), Lightwork::LOG_DEBUG); } } } } } return $key; }
/** * Lightwork shutdown function * * This function gets executed everytime a script finishes in order to clean stuff up and log some final debug messages. * * @param float $start The start microtime of this request */ function shutdown($start) { $error = error_get_last(); // Get the last error that occured... if (!empty($error)) { Lightwork::Log(sprintf('Shutdown with error: [%s] %s in %s at %s (php.log may contain more info)', $error['type'], $error['message'], $error['file'], $error['line']), Lightwork::LOG_ERROR); } // ...and log it - it might proof useful if (METHOD != 'CLI') { Lightwork::Log('Request ended with status ' . http_response_code(), Lightwork::LOG_DEBUG); } // Log the status the request ended with if this is not a CLI request if (LOG_DATABASE) { Lightwork::Log(Database::Queries() . ' queries were executed for this request.', Lightwork::LOG_DEBUG); } // Log how many queries were executed if (LOG_INCLUDES) { $history = 'Used files:'; $files = get_included_files(); // Get all files that were included... foreach ($files as $file) { $history .= ' > ' . basename($file, '.php'); // ...and log them - this might be useful for debugging and performance optimization } Lightwork::Log('A total of ' . count($files) . ' files were used for this request.', Lightwork::LOG_DEBUG); // Log the amount of files included... Lightwork::Log($history, Lightwork::LOG_DEBUG); // ...and the actual history of files } if (LOG_MEMORYUSAGE) { // Measure memory usage and memory limit Lightwork::Log('Memory usage: ' . number_format(memory_get_usage() / 1024 / 1024, 2) . 'MB/' . ini_get('memory_limit') . 'B (peak: ' . number_format(memory_get_peak_usage() / 1024 / 1024, 2) . ')', Lightwork::LOG_DEBUG); } if (LOG_PERFORMANCE) { // Measure the time that passed since initialization and script shutdown Lightwork::Log('Execution time: ' . number_format(microtime(true) - $start, 3) . 's', Lightwork::LOG_DEBUG); } // If wanted, log execution time Lightwork::Flush(); // Flush log file to disk }
ini_set('session.gc_maxlifetime', SESSION_TIMEOUT); // Set the session timeout ini_set('session.hash_function', 'sha512'); // Increase session security by using a longer hash (SHA-512 vs MD5) session_start(); // Start this session } if (METHOD != 'CLI') { if (cookieexists('session')) { // Check if user already has a session define('FIRSTVISIT', false); } else { define('FIRSTVISIT', true); } // Otherwise this is a new visitor if (sessionexists('loggedin')) { define('LOGGEDIN', true); Lightwork::Log('User is logged in.', Lightwork::LOG_DEBUG); } else { define('LOGGEDIN', false); Lightwork::Log('User is not logged in.', Lightwork::LOG_DEBUG); } } if (!MULTILANGUAGE) { Translation::Initialize(LANGUAGE); } else { Translation::Initialize(Translation::IdentifyLanguage()); } // Multi language needed, try to identify the necessary language for this request Lightwork::Initialize(); // Basic initialization is done. Lets get the Framework started...
/** * Function used to "describe" a table * * This function will create an array of all columns in the given table. * * @param string $table The name of the table to describe. * * @return bool Whether or not the operation succeeded. */ function describe($table) { if ($this->connected) { $this->prepare('SELECT * FROM ' . $table . ' LIMIT 1'); // Select a single row from the given table $this->execute(); // Execute this statement without fetching any data $this->columns = $this->statement->columnCount(); // Count columns $description = array(); for ($i = 0; $i < $this->columns; $i++) { $description[$i] = $this->statement->getColumnMeta($i)['name']; // Get the name of each column in this table and store it in order } $this->description = $description; // Save table description return true; } else { Lightwork::Log('Tried to describe a table while not connected. Please call connect() first.', Lightwork::LOG_ERROR); return false; } }
/** * Function to log a user out * * Will simply destroy the session */ static function Logout() { $_SESSION = array(); session_destroy(); Lightwork::Log('User logged out.', Lightwork::LOG_DEBUG); }
/** * Function used to clear cache * * Will loop through cache directory and clear all files on its way. */ static function Clear() { if (CACHE) { $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(CACHE_DIR, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD); foreach ($iterator as $file) { if ($file->isFile()) { unlink($file); } } Lightwork::Log('Cache was cleared.', Lightwork::LOG_INFO); } }
} if (!is_null($_PARAMS['name'])) { echo '<span class="menu-description">' . $_PARAMS['name'] . '</span>'; } ?> <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <?php if (isset($_PARAMS['subitems'])) { foreach ($_PARAMS['subitems'] as $subitem) { if (session('rank') >= $subitem['minrank'] && session('rank') <= $subitem['maxrank']) { if ($subitem['type'] == 'link') { Template::Draw('menu/subitem', $subitem); } else { if ($subitem['type'] == 'divider') { Template::Draw('menu/divider'); } else { if ($subitem['type'] == 'section') { Template::Draw('menu/section', $subitem); } else { Lightwork::Log('Tried to use incompatible menu type in dropdown: ' . $subitem['type'], Lightwork::LOG_WARN); } } } } } } ?> </ul> </li>
/** * Function used to make a javascript * * Will use the integrated JavaScript minifier to minify the given script. * The script will be moved to web directory automatically. * * @param string $name The name of the javascript file to minify and move. * @param string $styles The styles to use. (example: "default" or "default flat" or "dark flat") * * @todo Styles are not used yet. * * @return bool Will return false if class was not initialized. */ static function MakeScript($name, $styles = 'default') { if (isset(self::$scripts[$name])) { $script = self::$scripts[$name]; // Get the script if ($script['source'] && is_file(JS_SRC_DIR . $script['path'] . '.js')) { require_once THIRD_LIB_DIR . 'jshrink/minifier.php'; // Require the javascript minifier $minified = \JShrink\Minifier::minify(file_get_contents(JS_SRC_DIR . $script['path'] . '.js')); // Minify the script if (!empty($minified)) { if (!is_file(JS_DIR . $script['path'] . '.js') || md5_file(JS_DIR . $script['path'] . '.js') != md5($minified)) { if (file_put_contents(JS_DIR . $script['path'] . '.js', $minified) === false) { // ...and only replace it if necessary. return false; } else { Lightwork::Log('Resource was made: ' . $name, Lightwork::LOG_INFO); // Log this event return true; } } else { Lightwork::Log('Resource already made: ' . $name, Lightwork::LOG_INFO); // No need to minify this script return true; } } } } Lightwork::Log('Could not make resource: ' . $name, Lightwork::LOG_WARN); // Something went wrong. return false; }
$success = embed($page); // Try to embed this page if (!$page['partial']) { echo '</div>'; } // Close the open container div (if this is no partial site) if ($success) { if ($page['cache']) { $content = ob_get_flush(); // The page was created successfully and we can flush it Cache::Write('pages/' . Translation::Language() . '/' . $page['id'], $content, $page['cacheduration'], true); // Write the successfully embeded page to cache } } else { ob_end_clean(); Lightwork::Log('Page is missing: ' . $page['path'] . '.', Lightwork::LOG_WARN); // Page is missing, log this Lightwork::Done(404, 'ERROR_PAGE_VANISHED'); // Page did not embed successfully - it is supposed to be there but isn't } } } } } function embed($page) { if ($page['type'] == 'static' && is_file(PAGES_DIR . $page['path'] . '.html')) { readfile(PAGES_DIR . $page['path'] . '.html'); } else { if ($page['type'] == 'dynamic' && is_file(PAGES_DIR . $page['path'] . '.php')) { require PAGES_DIR . $page['path'] . '.php';
/** * Function used to send an email * * @todo */ static function Send($to, $subject, $body, $from = null, $name = null, $reply = null, $cc = null, $bcc = null, $attachments = array()) { require_once THIRD_LIB_DIR . 'phpmailer/PHPMailerAutoload.php'; $credentials = json_decode(file_get_contents(CONFIG_DIR . 'mail.conf'), true); $mail = new PHPMailer(); if ($credentials['type'] == 'smtp') { $mail->isSMTP(); } $mail->Host = $credentials['host']; $mail->SMTPAuth = $credentials['auth']; $mail->Username = $credentials['username']; $mail->Password = $credentials['password']; $mail->SMTPSecure = $credentials['encryption']; $mail->Port = $credentials['port']; if (is_null($from)) { $mail->From = $credentials['from']; } else { $mail->From = $from; } if (is_null($name)) { $mail->FromName = $credentials['name']; } else { $mail->FromName = $name; } if (is_array($to)) { foreach ($to as $address) { if (is_array($address) && count($address) == 2) { $mail->addAddress($address[0], $address[1]); } else { $mail->addAddress($address); } } } else { $mail->addAddress($to); } if (!is_null($reply)) { if (is_array($reply)) { foreach ($reply as $address) { if (is_array($address) && count($address) == 2) { $mail->addReplyTo($address[0], $address[1]); } else { $mail->addReplyTo($address); } } } else { $mail->addReplyTo($reply); } } if (!is_null($cc)) { if (is_array($cc)) { foreach ($cc as $address) { if (is_array($address) && count($address) == 2) { $mail->addCC($address[0], $address[1]); } else { $mail->addCC($address); } } } else { $mail->addCC($cc); } } if (!is_null($bcc)) { if (is_array($bcc)) { foreach ($bcc as $address) { if (is_array($address) & count($address) == 2) { $mail->addBCC($address[0], $address[1]); } else { $mail->addBCC($address); } } } else { $mail->addBCC($bcc); } } if (!is_null($attachments)) { if (is_array($attachments)) { foreach ($attachments as $attachment) { $mail->addAttachment($attachment); } } else { $mail->addAttachment($attachments); } } $mail->isHTML(true); $mail->Subject = $subject; $mail->Body = $body; if ($mail->send()) { return true; } else { Lightwork::Log('Mail handler failed: ' . $mail->ErrorInfo, Lightwork::LOG_ERROR); return false; } }