瀏覽代碼

Multiple little fixes, typos and reparation for some future code

ohartl 9 年之前
父節點
當前提交
3964e5331b

+ 27 - 19
include/php/default.inc.php

@@ -3,7 +3,29 @@
 session_start();
 session_regenerate_id();
 
-// Include config
+
+/**
+ * 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';
+	}
+	elseif(file_exists('include/php/classes/'.$class.'.php')){
+		include 'include/php/classes/'.$class.'.php';
+	}
+});
+
+
+/**
+ * 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';
 }
@@ -13,31 +35,17 @@ else{
 
 
 /**
- * @param string $errorMessage
+ * Establish database connection
  */
-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';
-	}
-	elseif(file_exists('include/php/classes/'.$class.'.php')){
-		include 'include/php/classes/'.$class.'.php';
-	}
-});
 
-/* Initialize Authentication (Login User if in session) */
+/**
+ * Initialize Authentication (Login User if in session)
+ */
 Auth::init();
 
-require_once 'include/php/global.inc.php';
 

+ 47 - 23
include/php/global.inc.php

@@ -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)

+ 1 - 1
include/php/pages/private/start.php

@@ -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>

+ 2 - 2
include/php/pages/start.php

@@ -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>
 

+ 2 - 0
include/php/template/header.php

@@ -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>

+ 10 - 1
index.php

@@ -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';