示例#1
0
 /**
  * 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);
         }
     }
 }
示例#2
0
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
}
示例#3
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
     }
 }
示例#4
0
/**
 * File used to handle all incoming requests
 *
 * This script will differentiate between the different request types and call the corresponding handler.
 *
 * @author Perfler Dominik <*****@*****.**>
 * @copyright 2014-2015 Perfler Dominik
 */
require_once dirname(__FILE__) . '/bootstrap.php';
// Bootstrap this request (if not done already)
if (Lightwork::Type() == Lightwork::REQUEST_PAGE) {
    require LIB_DIR . 'pagehandler.php';
} else {
    if (Lightwork::Type() == Lightwork::REQUEST_SCRIPT) {
        require LIB_DIR . 'scripthandler.php';
    } else {
        if (Lightwork::Type() == Lightwork::REQUEST_FILE) {
            require LIB_DIR . 'filehandler.php';
        } else {
            if (Lightwork::Type() == Lightwork::REQUEST_CRONJOB) {
                require LIB_DIR . 'cronjobhandler.php';
            } else {
                if (Lightwork::Type() == Lightwork::REQUEST_SITEMAP) {
                    require LIB_DIR . 'sitemaphandler.php';
                }
            }
        }
    }
}
// This is a sitemap request
示例#5
0
    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...
示例#6
0
 /**
  * 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;
     }
 }
示例#7
0
 /**
  * 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;
     }
 }
示例#8
0
                } else {
                    if (!$form->checkFields()) {
                        // Check the actual data according to form settings (will do server-side validation of fields)
                        Lightwork::Done('error', 'SCRIPT_BAD_REQUEST');
                    }
                }
            }
            if (is_file(SCRIPTS_DIR . $script['path'] . '.php')) {
                ob_start();
                require SCRIPTS_DIR . $script['path'] . '.php';
                // Require the script if it exists
            } else {
                Lightwork::Done('error', 'SCRIPT_VANISHED');
            }
            // Script should be there according to database but isn't
            Lightwork::Done('error', 'SCRIPT_UNDONE');
            // The script didn't call done function after executing
        }
    }
}
/**
 * Simple function used to assign a value to a key for the response
 *
 * This function will assign the given value to the key which will later be used for sending a response to the client.
 *
 * @param string $key The key to use
 * @param mixed $value The value to assign to the given key
 */
function set($key, $value)
{
    global $_RESPONSE;
/**
 * 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;
}
示例#10
0
                                $read = 8192;
                            }
                            // Still plenty of bytes left - use default 8192 as chunk size
                            echo fread($stream, $read);
                            // Read the bytes from the stream
                            $done += $read;
                            // Add read bytes to done bytes
                            flush();
                            // Flush this chunk to the browser to allow an instant download start
                        }
                        if ($file['buffer']) {
                            $file['limit'] = $file['limit'] / 8;
                            // Since we enabled a buffering boost earlier we need to "reset" it now
                            $file['buffer'] = false;
                            // Disable buffering for the next read
                        }
                        sleep(1);
                        // Sleep one second to meet kB/s limitation
                    } else {
                        echo fread($stream, 8192);
                        // No download limit is applied, keep reading in small chunks till the end
                        flush();
                    }
                }
            } else {
                Lightwork::Done(404, 'ERROR_FILE_VANISHED');
            }
            // File exists in database but not on disk
        }
    }
}
示例#11
0
<base href="<?php 
echo WEB_ROOT;
?>
" />
<?php 
foreach (ResourceHandler::GetStylesheets() as $stylesheet) {
    echo '<link rel="stylesheet" href="css/' . $stylesheet['path'] . '.css" />';
}
foreach (ResourceHandler::GetScripts() as $script) {
    echo '<script type="text/javascript" src="js/' . $script['path'] . '.js"></script>';
}
?>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

<link rel="shortcut icon" type="image/x-icon" href="img/icons/favicon.ico" />

<meta name="application-name" content="Lightwork" />
<meta name="msapplication-TileColor" content="#ffffff" />
<meta name="msapplication-square70x70logo" content="img/tiles/small.png" />
<meta name="msapplication-square150x150logo" content="img/tiles/medium.png" />
<meta name="msapplication-wide310x150logo" content="img/tiles/wide.png" />
<meta name="msapplication-square310x310logo" content="img/tiles/large.png" />

<title data-page="<?php 
echo PAGE_TITLE;
?>
"><?php 
echo Lightwork::Title();
?>
</title>
示例#12
0
<?php

/**
 * File will be called whenever sitemap.xml is requested from the server
 *
 * Will return all pages which are not hidden and accessible by user.
 *
 * @author Perfler Dominik <*****@*****.**>
 * @copyright 2014-2015 Perfler Dominik
 * @license MIT
 */
header("Content-Type: text/xml");
// This is a xml file
$xml = new SimpleXMLElement('<xml/>');
// Create a new xml file
foreach (Lightwork::Pages() as $page) {
    if (!$page['hidden']) {
        if (0 >= $page['minrank'] && 0 <= $page['maxrank']) {
            if ($page['key'] == 'index') {
                // Rename index to nothing
                $page['key'] = '';
            }
            $url = $xml->addChild('url');
            // Add the "url" child
            $url->addChild('loc', URL . urlencode($page['key']));
            // Add the "loc" child to the "url" and assign the page to its value
            $url->addChild('priority', '1.000');
            // Set the priority to 1.000 (hardcoded for now)
        }
    }
}
示例#13
0
 /**
  * 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;
 }
示例#14
0
                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';
        } else {
            if ($page['type'] == 'handler' && is_file(PAGES_DIR . $page['path'] . DIRECTORY_SEPARATOR . 'handler.php')) {
示例#15
0
/**
 * 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
}
示例#16
0
<?php

$page = Lightwork::Page();
foreach ($_PARAMS['items'] as $item) {
    if ($item['alignment'] == $_PARAMS['alignment']) {
        if (session('rank') >= $item['minrank'] && session('rank') <= $item['maxrank']) {
            if (isset($page['key']) && $item['key'] == $page['key']) {
                $item['active'] = true;
            } else {
                $item['active'] = false;
            }
            if ($item['type'] == 'link') {
                $item = at($item, ['name']);
                Template::Draw('menu/link', $item);
            } else {
                if ($item['type'] == 'label') {
                    $item = at($item, ['name'], 'tt');
                    Template::Draw('menu/label', $item);
                } else {
                    if ($item['type'] == 'dropdown') {
                        $item['active'] = false;
                        if (isset($item['subitems'])) {
                            foreach ($item['subitems'] as $key => $subitem) {
                                if (isset($page['key']) && $subitem['key'] == $page['key']) {
                                    $item['active'] = true;
                                    $item['subitems'][$key]['active'] = true;
                                } else {
                                    $item['subitems'][$key]['active'] = false;
                                }
                                $item['subitems'][$key] = at($item['subitems'][$key], ['name']);
                            }
示例#17
0
}
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>
示例#18
0
 /**
  * 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]]);
 }
示例#19
0
 /**
  * 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);
     }
 }
示例#20
0
 /**
  * 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);
         }
     }
 }
示例#21
0
 /**
  * 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);
 }