Multiple little fixes, typos and reparation for some future code

This commit is contained in:
ohartl 2016-02-19 15:08:38 +01:00
parent 32d0216a83
commit 3964e5331b
6 changed files with 93 additions and 50 deletions

View file

@ -3,30 +3,10 @@
session_start();
session_regenerate_id();
// Include config
if(file_exists('config/config.inc.php')){
require_once 'config/config.inc.php';
}
else{
require_once 'config/config.inc.php.example';
}
/**
* @param string $errorMessage
* Register automatic loading for dependency injection
*/
function dbError($errorMessage){
die('There was an error running the query ['.$errorMessage.']');
}
// Establish database connection
$db = new mysqli(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
// register automatic loading for dependency injection
spl_autoload_register(function($class){
if(file_exists('include/php/models/'.$class.'.php')){
include 'include/php/models/'.$class.'.php';
@ -36,8 +16,36 @@ spl_autoload_register(function($class){
}
});
/* Initialize Authentication (Login User if in session) */
Auth::init();
/**
* Load some global accessible functions
*/
require_once 'include/php/global.inc.php';
/**
* Require config
*/
if(file_exists('config/config.inc.php')){
require_once 'config/config.inc.php';
}
else{
require_once 'config/config.inc.php.example';
}
/**
* Establish database connection
*/
$db = new mysqli(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
/**
* Initialize Authentication (Login User if in session)
*/
Auth::init();

View file

@ -1,15 +1,27 @@
<?php
/*
* Message manager
* Types of notifications:
* success
* fail
* info
/**
* @param string $errorMessage
* @param null|string $sql
*/
function dbError($errorMessage, $sql = null)
{
die('There was an error running the query ['.$errorMessage.']'.(!is_null($sql)?' with statement "'.$sql.'"':''));
}
/**
* Holds all messages
* @var array
*/
$MESSAGES = array();
/**
* Add a new message
* @param string $type Supported types: success, fail, info
* @param string $message
*/
function add_message($type, $message)
{
global $MESSAGES;
@ -20,6 +32,9 @@ function add_message($type, $message)
$MESSAGES[] = $newmessage;
}
/**
* Print all messages
*/
function output_messages()
{
global $MESSAGES;
@ -33,8 +48,10 @@ function output_messages()
}
/*
/**
* Add message to logfile
*
* @param string $text
*/
function writeLog($text)
{
@ -53,22 +70,20 @@ function writeLog($text)
/**
* Generate full url
*
* @param string $url
*
* @return string
*/
function url($url)
{
$base = FRONTEND_BASE_PATH;
if (substr($base, -1) === '/') {
$base = substr($base, 0, -1);
}
if (strlen($url) > 0 && $url[0] === '/') {
$url = substr($url, 1);
}
return $base.'/'.$url;
return sprintf('%s/%s', rtrim(BASE_URL, '/'), trim($url));
}
/**
* Redirect user to an url
*
* @param string $url
*/
function redirect($url)
@ -87,22 +102,25 @@ function redirect($url)
*/
function stringToEmails($input)
{
$separators = array(',', ';', "\r\n", "\r", "\n", '|', ':');
$list = explode(
'|',
str_replace(
array(',', ';', "\r\n", "\r", "\n", '|', ':'),
'|',
$input
)
);
$list = explode('|', str_replace($separators, '|', $input));
foreach($list as $i => &$email){
if(empty($email)){
unset($list[$i]);
}
else{
$email = trim($email);
}
}
return array_values(
array_map(
'strtolower',
array_unique(
array_unique(
array_map(
'formatEmail',
$list
)
)
@ -119,6 +137,10 @@ function stringToEmails($input)
*/
function emailsToString($list, $glue = ',')
{
if(is_string($list)){
return $list;
}
return implode($glue, $list);
}
@ -126,6 +148,7 @@ function emailsToString($list, $glue = ',')
* Format single email address
*
* @param string $input
*
* @return string
*/
function formatEmail($input)
@ -138,6 +161,7 @@ function formatEmail($input)
*
* @param string|array $input
* @param string $glue
*
* @return string
*/
function formatEmails($input, $glue)

View file

@ -5,5 +5,5 @@
</p>
<div class="buttons buttons-horizontal button-large">
<a class="button" href="<?php echo url('private/changepass'); ?>">Change e-mail account password</a>
<a class="button" href="<?php echo url('private/changepass'); ?>">Change your password</a>
</div>

View file

@ -1,13 +1,13 @@
<?php
if(Auth::isLoggedIn()){
redirect("private/");
redirect("private");
}
?>
<h1>WebMUM</h1>
<p>
WebMUM is an easy to use webinterface for managing user accounts on your mailserver's MySQL user backend.<br/>
WebMUM is an easy to use web interface for managing user accounts on your e-mail server with a MySQL user backend.<br/>
Users of your server can log in here to change their passwords.
</p>

View file

@ -3,6 +3,7 @@
<head>
<title>WebMUM</title>
<link rel=stylesheet href="<?php echo url('include/css/style.css'); ?>" type="text/css" media=screen>
<?php if(defined('MIN_PASS_LENGTH')): ?>
<script type="text/javascript">
function generatePassword() {
var length = <?php echo MIN_PASS_LENGTH + 1; ?>,
@ -14,6 +15,7 @@
return retVal;
}
</script>
<?php endif; ?>
</head>
<body>

View file

@ -21,8 +21,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
define("BACKEND_BASE_PATH", preg_replace("#index.php#", "", $_SERVER['SCRIPT_FILENAME']));
function getBaseUrl(){
$sec = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") ? 's' : '';
$host = $_SERVER["SERVER_NAME"].($_SERVER["SERVER_PORT"] != "80" ? ':'.$_SERVER["SERVER_PORT"] : '');
return sprintf("http%s://%s%s", $sec, $host, str_replace('index.php', '', $_SERVER["DOCUMENT_URI"]));
}
define("BASE_URL", getBaseUrl());
require_once 'include/php/default.inc.php';
require_once 'include/php/template/header.php';