easyappointments/src/application/helpers/general_helper.php

101 lines
2.8 KiB
PHP
Raw Normal View History

<?php defined('BASEPATH') OR exit('No direct script access allowed');
2015-10-18 20:46:16 +03:00
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2018, Alex Tselegidis
2015-10-18 20:46:16 +03:00
* @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)
{
2015-10-18 20:46:16 +03:00
if ( ! $timestamp)
{
2015-10-18 20:46:16 +03:00
$timestamp = time();
}
$date = date('Y-m-d\TH:i:s', $timestamp);
$matches = [];
if (preg_match('/^([\-+])(\d{2})(\d{2})$/', date('O', $timestamp), $matches))
{
$date .= $matches[1] . $matches[2] . ':' . $matches[3];
2018-01-23 12:08:37 +03:00
}
else
{
2015-10-18 20:46:16 +03:00
$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)
{
2015-10-18 20:46:16 +03:00
$half = (int)(strlen($salt) / 2);
$hash = hash('sha256', substr($salt, 0, $half) . $password . substr($salt, $half));
2015-10-18 20:46:16 +03:00
for ($i = 0; $i < 100000; $i++)
{
2015-10-18 20:46:16 +03:00
$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()
{
2015-10-18 20:46:16 +03:00
$max_length = 100;
$salt = hash('sha256', (uniqid(rand(), TRUE)));
2015-10-18 20:46:16 +03:00
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)
{
2015-10-18 20:46:16 +03:00
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$random_string = '';
for ($i = 0; $i < $length; $i++)
{
2015-10-18 20:46:16 +03:00
$random_string .= $characters[rand(0, strlen($characters) - 1)];
}
return $random_string;
}