2013-05-04 00:26:04 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2013-09-20 16:58:11 +03:00
|
|
|
/**
|
|
|
|
* Generate a hash of password string.
|
|
|
|
*
|
|
|
|
* For user security, all system passwords are stored in hash string into the database. Use
|
2013-09-23 18:42:36 +03:00
|
|
|
* this method to produce the hashed password.
|
2013-09-20 16:58:11 +03:00
|
|
|
*
|
2013-09-23 18:42:36 +03:00
|
|
|
* @param string $salt Salt value for current user. This value is stored on the database and
|
|
|
|
* is used when generating the password hash.
|
2013-09-20 16:58:11 +03:00
|
|
|
* @param string $password Given string password.
|
|
|
|
* @return string Returns the hash string of the given password.
|
|
|
|
*/
|
2013-09-23 18:42:36 +03:00
|
|
|
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; // @task include salt and hash more times.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2013-09-20 16:58:11 +03:00
|
|
|
}
|
|
|
|
|
2013-10-11 18:58:46 +03:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2013-05-11 13:19:18 +03:00
|
|
|
/* End of file general_helper.php */
|
|
|
|
/* Location: ./application/helpers/general_helper.php */
|