From b0db1289f4ed483881ac4745861feab03ee14128 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Sun, 18 Oct 2015 19:46:16 +0200 Subject: [PATCH] Added comment banners to helpers. --- .../helpers/custom_exceptions_helper.php | 161 ++++++++-------- src/application/helpers/general_helper.php | 174 ++++++++++-------- 2 files changed, 179 insertions(+), 156 deletions(-) diff --git a/src/application/helpers/custom_exceptions_helper.php b/src/application/helpers/custom_exceptions_helper.php index 7c7065fe..015617b6 100644 --- a/src/application/helpers/custom_exceptions_helper.php +++ b/src/application/helpers/custom_exceptions_helper.php @@ -1,75 +1,86 @@ - -
- -
-
-
' . $exc->getTraceAsString() . '
-
-
-
- '; -} - -/** - * Get a javascript object of a given exception. - * - * This method is pretty useful whenever we need to pass an exception object - * to javascript during ajax calls. - * - * @param Exception $exception The given exception object. - * @return string Returns the json encoded object of the exception. - */ -function exceptionToJavaScript($exception) { - return json_encode(array( - 'code' => $exception->getCode(), - 'file' => $exception->getFile(), - 'line' => $exception->getLine(), - 'message' => $exception->getMessage(), - 'previous' => $exception->getPrevious(), - 'trace' => $exception->getTraceAsString() - )); -} - -/* End of file exception_types_helper.php */ -/* Location: ./application/helpers/exception_types_helper.php */ \ No newline at end of file + + * @copyright Copyright (c) 2013 - 2015, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.0.0 + * ---------------------------------------------------------------------------- */ + +/** + * Database Exception Class + */ +class DatabaseException extends Exception {} + +/** + * Validation Exception Class + */ +class ValidationException extends Exception {} + +/** + * Notification Exception Class + */ +class NotificationException extends Exception {} + +/** + * Sync Exception Class + */ +class SyncException extends Exception {} + +/** + * Print an exception to an HTML text. + * + * This method is used to display exceptions in a way that is useful and easy + * for the user to see. It uses the Bootstrap CSS accordion markup to display + * the message and when the user clicks on it the exception trace will be revealed. + * We display only one exceptions at a time because the user needs to be able + * to display the details of each exception seperately. (In contrast with js). + * + * @param Exception $exc The exception to be displayed. + * @return string Returns the html markup of the exception. + */ +function exceptionToHtml($exc) { + return + '
+
+ +
+
+
' . $exc->getTraceAsString() . '
+
+
+
+
'; +} + +/** + * Get a javascript object of a given exception. + * + * This method is pretty useful whenever we need to pass an exception object + * to javascript during ajax calls. + * + * @param Exception $exception The given exception object. + * @return string Returns the json encoded object of the exception. + */ +function exceptionToJavaScript($exception) { + return json_encode(array( + 'code' => $exception->getCode(), + 'file' => $exception->getFile(), + 'line' => $exception->getLine(), + 'message' => $exception->getMessage(), + 'previous' => $exception->getPrevious(), + 'trace' => $exception->getTraceAsString() + )); +} + +/* End of file exception_types_helper.php */ +/* Location: ./application/helpers/exception_types_helper.php */ diff --git a/src/application/helpers/general_helper.php b/src/application/helpers/general_helper.php index 7ccd941b..29546ccd 100644 --- a/src/application/helpers/general_helper.php +++ b/src/application/helpers/general_helper.php @@ -1,81 +1,93 @@ - + * @copyright Copyright (c) 2013 - 2015, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.0.0 + * ---------------------------------------------------------------------------- */ + +/** + * Get date in RFC3339 + * For example used in XML/Atom + * + * @link http://stackoverflow.com/questions/5671433/php-time-to-google-calendar-dates-time-format + * + * @param integer $timestamp + * @return string date in RFC3339 + * @author Boris Korobkov + */ +function date3339($timestamp=0) { + + if (!$timestamp) { + $timestamp = time(); + } + $date = date('Y-m-d\TH:i:s', $timestamp); + + $matches = array(); + if (preg_match('/^([\-+])(\d{2})(\d{2})$/', date('O', $timestamp), $matches)) { + $date .= $matches[1].$matches[2].':'.$matches[3]; + } else { + $date .= 'Z'; + } + return $date; +} + +/** + * Generate a hash of password string. + * + * For user security, all system passwords are stored in hash string into the database. Use + * this method to produce the hashed password. + * + * @param string $salt Salt value for current user. This value is stored on the database and + * is used when generating the password hash. + * @param string $password Given string password. + * @return string Returns the hash string of the given password. + */ +function hash_password($salt, $password) { + $half = (int)(strlen($salt) / 2); + $hash = hash('sha256', substr($salt, 0, $half ) . $password . substr($salt, $half)); + + for ($i = 0; $i < 100000; $i++) { + $hash = hash('sha256', $hash); + } + + return $hash; +} + +/** + * Generate a new password salt. + * + * This method will not check if the salt is unique in database. This must be done + * from the calling procedure. + * + * @return string Returns a salt string. + */ +function generate_salt() { + $max_length = 100; + $salt = hash('sha256', (uniqid(rand(), true))); + return substr($salt, 0, $max_length); +} + +/** + * This method generates a random string. + * + * @param int $length (OPTIONAL = 10) The length of the generated string. + * @return string Returns the randomly generated string. + * @link http://stackoverflow.com/a/4356295/1718162 + */ +function generate_random_string($length = 10) { + $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $random_string = ''; + for ($i = 0; $i < $length; $i++) { + $random_string .= $characters[rand(0, strlen($characters) - 1)]; + } + return $random_string; +} + +/* End of file general_helper.php */ +/* Location: ./application/helpers/general_helper.php */