Remove hash helper and use existing string helper instead

This commit is contained in:
Eike Foken
2011-09-21 01:08:43 +02:00
parent 11ee2107f6
commit 4439fa9dfe
11 changed files with 113 additions and 140 deletions

View File

@@ -64,7 +64,7 @@ $autoload['libraries'] = array('session', 'lang_detect', 'database', 'access', '
| $autoload['helper'] = array('url', 'file');
*/
$autoload['helper'] = array('date', 'url', 'form', 'language', 'hash', 'asset', 'text');
$autoload['helper'] = array('date', 'url', 'form', 'language', 'string', 'asset', 'text');
/*

View File

@@ -1,45 +0,0 @@
<?php defined('BASEPATH') || exit('No direct script access allowed');
/*
* Copyright (c) 2011 Karsten Heiken, Eike Foken
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* Helper for generating hash values.
*
* @package ScattPort
* @subpackage Helpers
* @author Karsten Heiken <karsten@disposed.de>
*/
if (!function_exists('random_hash')) {
/**
* Generates a pseudo-random SHA1-hash.
*
* @param integer $len
* @return integer
*/
function random_hash($len = 40) {
return substr(sha1(rand(1,1000).now().rand(1001,2000)), 0, $len);
}
}
/* End of file MY_date_helper.php */
/* Location: ./application/helpers/MY_date_helper.php */

View File

@@ -24,16 +24,26 @@
/**
* Experiments are used to store different variations of the same project.
*
* @package ScattPort
* @subpackage Models
* @author Karsten Heiken <karsten@disposed.de>
* @author Eike Foken <kontakt@eikefoken.de>
*/
class Experiment extends CI_Model {
/**
* Calls the parent constructor.
*/
public function __construct() {
parent::__construct();
}
/**
* Creates a new experiment.
*
* @param array $data The data of the new experiment
* @return boolean Returns TRUE if the insert was successful.
* @return boolean Returns the ID of the created experiment, or FALSE if the
* insert was unsuccessful.
*/
public function create($data) {
if (!isset($data['project_id'])) {
@@ -41,7 +51,7 @@ class Experiment extends CI_Model {
}
do { // generate unique hash
$data['id'] = random_hash();
$data['id'] = random_string('sha1', 40);
} while ($this->db->where('id', $data['id'])->from('experiments')->count_all_results() > 0);
if ($this->db->insert('experiments', $data)) {

View File

@@ -1,6 +1,6 @@
<?php defined('BASEPATH') || exit('No direct script access allowed');
/*
* Copyright (c) 2011 Eike Foken <kontakt@eikefoken.de>
* Copyright (c) 2011 Karsten Heiken, Eike Foken
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -49,12 +49,12 @@ class Group extends CI_Model {
}
/**
* Gets a specific group.
* Gets a specific group by it's ID.
*
* @param string $id
* @return array
*/
public function getByID($id) {
public function getById($id) {
return $this->db->get_where('groups', array('id' => $id))->row_array();
}

View File

@@ -22,25 +22,35 @@
*/
/**
* Model for jobs.
*
* @package ScattPort
* @subpackage Models
* @author Karsten Heiken <karsten@disposed.de>
* @author Eike Foken <kontakt@eikefoken.de>
*/
class Job extends CI_Model {
/**
* Calls the parent constructor.
*/
public function __construct() {
parent::__construct();
}
/**
* Creates a new job.
*
* @param array $data The data of the new job
* @return boolean Returns TRUE if the insert was successful.
* @return mixed Returns the ID of the created job, or FALSE if the insert
* was unsuccessful.
*/
public function create($data) {
$this->load->helper('date');
$this->load->helper('hash');
do { // generate unique hash
$data['id'] = random_hash();
$data['id'] = random_string('sha1', 40);
} while ($this->db->where('id', $data['id'])->from('jobs')->count_all_results() > 0);
$data['created_at'] = date('Y-m-d H:i:s', now());
$data['created_at'] = mysql_now();
$this->db->insert('jobs', $data);

View File

@@ -26,6 +26,8 @@
*
* Each program has many parameters used for configuration of experiments.
*
* @package ScattPort
* @subpackage Models
* @author Eike Foken <kontakt@eikefoken.de>
*/
class Parameter extends CI_Model {
@@ -52,7 +54,7 @@ class Parameter extends CI_Model {
*/
public function getAll($programId) {
return $this->db->order_by('sort_number ASC')
->get_where('parameters', array('program_id' => $programId))->result_array();
->get_where('parameters', array('program_id' => $programId))->result_array();
}
/**
@@ -79,14 +81,12 @@ class Parameter extends CI_Model {
* the insert was unsuccessful.
*/
public function create($data) {
$this->load->helper('hash');
if (!isset($data['program_id'])) {
return false;
}
do { // generate unique hash
$data['id'] = random_hash('16');
$data['id'] = random_string('sha1', 16);
} while ($this->db->where('id', $data['id'])->from('parameters')->count_all_results() > 0);
// put new parameter to the end

View File

@@ -40,14 +40,11 @@ class Program extends CI_Model {
* Creates a new program.
*
* @param string $name The name of the new program
* @return string|boolean Returns the ID of the new program, or FALSE if
* the insert was unsuccessful.
* @return mixed Returns the ID of the created program, or FALSE if the insert was unsuccessful.
*/
public function create($name) {
$this->load->helper('hash');
do { // generate unique hash
$id = random_hash('16');
$id = random_string('sha1', 8);
} while ($this->db->where('id', $id)->from('programs')->count_all_results() > 0);
$this->db->insert('programs', array('id' => $id, 'name' => $name));

View File

@@ -182,26 +182,25 @@ class Project extends CI_Model {
}
/**
* Create a new project.
* Creates a new project.
*
* @param array $data array with "name" and "description"
* @param array $data An array with "name" and "description"
* @return mixed Returns the ID of the created project, or FALSE if the
* insert was unsuccessful.
*/
public function create($data) {
$this->load->helper(array('hash', 'date'));
$data['owner'] = $this->session->userdata('user_id');
$data['created'] = mysql_now();
$data['last_access'] = mysql_now();
do {
$data['id'] = random_hash();
do { // generate unique hash
$data['id'] = random_string('sha1', 40);
} while ($this->db->where('id', $data['id'])->from('projects')->count_all_results() > 0);
if ($this->db->insert('projects', $data)) {
return $data['id'];
} else {
return FALSE;
return false;
}
}

View File

@@ -60,17 +60,18 @@ class Setting extends CI_Model {
/**
* Creates a new settings entry.
*
* @param array $data
* @return boolean Returns TRUE on success.
* @param array $data An array with "name" and "value"
* @return boolean Returns the ID of the created settings, or FALSE if the
* insert was unsuccessful.
*/
public function create($data = array()) {
do { // generate unique hash
$data['id'] = random_hash();
$data['id'] = random_string('sha1', 40);
} while ($this->db->where('id', $data['id'])->from('settings')->count_all_results() > 0);
$this->db->insert('settings', $data);
return $this->db->affected_rows() == 1;
return ($this->db->affected_rows() > 0) ? $data['id'] : false;
}
/**

View File

@@ -97,20 +97,19 @@ class Share extends CI_Model {
/**
* Creates a share.
*
* @param array $data
* @return boolean
* @param array $data An array with project and user ID to share
* @return boolean Returns TRUE on success.
*/
public function create($data) {
if (!isset($data['project_id']) || !isset($data['user_id'])) {
return false;
}
$this->db->query('REPLACE INTO `shares` (`project_id`, `user_id`, `can_edit`) VALUES ('
. $this->db->escape($data['project_id']) . ', '
$this->db->query('REPLACE INTO `shares` (`project_id`, `user_id`, `can_edit`)'
. ' VALUES (' . $this->db->escape($data['project_id']) . ', '
. $this->db->escape($data['user_id']) . ', '
. $this->db->escape($data['can_edit']) . ')');
//$this->db->insert('shares', $data);
return $this->db->affected_rows() == 1;
}

View File

@@ -56,7 +56,6 @@ class User extends CI_Model {
parent::__construct();
$this->load->config('auth', true);
$this->load->helper('cookie');
$this->load->helper('date');
$this->storeSalt = $this->config->item('store_salt', 'auth');
$this->saltLength = $this->config->item('salt_length', 'auth');
@@ -120,6 +119,32 @@ class User extends CI_Model {
return substr(sha1(uniqid(rand(), true)), 0, $this->saltLength);
}
/**
* Checks entered usernames.
*
* @param string $username
* @return boolean
*/
private function checkUsername($username = '') {
if (empty($username)) {
return false;
}
return $this->db->where('username', $username)->count_all_results('users') > 0;
}
/**
* Checks entered emails.
*
* @param string $email
* @return boolean
*/
private function checkEmail($email = '') {
if (empty($email)) {
return false;
}
return $this->db->where('email', $email)->count_all_results('users') > 0;
}
/**
* Changes the password of the given user.
*
@@ -130,7 +155,7 @@ class User extends CI_Model {
*/
public function changePassword($username, $old, $new) {
$query = $this->db->select('password, salt')
->where('username', $username)->limit(1)->get('users');
->where('username', $username)->limit(1)->get('users');
$result = $query->row();
@@ -149,32 +174,6 @@ class User extends CI_Model {
return false;
}
/**
* Checks entered usernames.
*
* @param string $username
* @return boolean
*/
public function checkUsername($username = '') {
if (empty($username)) {
return false;
}
return $this->db->where('username', $username)->count_all_results('users') > 0;
}
/**
* Checks entered emails.
*
* @param string $email
* @return boolean
*/
public function checkEmail($email = '') {
if (empty($email)) {
return false;
}
return $this->db->where('email', $email)->count_all_results('users') > 0;
}
/**
* Inserts a forgotten password key.
*
@@ -258,11 +257,11 @@ class User extends CI_Model {
* @param string $email
* @param array $additionalData
* @param string $groupName
* @return boolean
* @return mixed Returns the ID of the new user, or FALSE if the
* registration was unsuccessful.
*/
public function register($username, $password, $email, $additionalData = array(), $groupName = '') {
if ($this->checkUsername($username)) {
$this->access->setError('account_creation_duplicate_username');
return false;
}
@@ -280,7 +279,6 @@ class User extends CI_Model {
// users table
$data = array(
'id' => random_hash(16),
'username' => $username,
'password' => $password,
'email' => $email,
@@ -288,6 +286,10 @@ class User extends CI_Model {
'last_login' => now(),
);
do { // generate unique hash
$data['id'] = random_string('sha1', 16);
} while ($this->db->where('id', $data['id'])->count_all_results('users') > 0);
if ($this->storeSalt) {
$data['salt'] = $salt;
}
@@ -374,21 +376,6 @@ class User extends CI_Model {
return $this->get()->result_array();
}
/**
* Returns the number of users.
*
* @param mixed $group
* @return integer The number of users
*/
public function count($group = false) {
if (is_string($group)) {
$this->db->where('groups.name', $group);
} else if (is_array($group)) {
$this->db->where_in('groups.name', $group);
}
return $this->db->from('users')->count_all_results();
}
/**
* Gets a user by ID.
*
@@ -465,6 +452,21 @@ class User extends CI_Model {
return $query->row_array();
}
/**
* Returns the number of users.
*
* @param mixed $group
* @return integer The number of users
*/
public function count($group = false) {
if (is_string($group)) {
$this->db->where('groups.name', $group);
} else if (is_array($group)) {
$this->db->where_in('groups.name', $group);
}
return $this->db->from('users')->count_all_results();
}
/**
* Updates a user.
*
@@ -506,17 +508,6 @@ class User extends CI_Model {
return $this->db->affected_rows() > 0;
}
/**
* Deletes a specified user.
*
* @param string $id
* @return boolean Returns TRUE if the deletion was successful.
*/
public function delete($id) {
$this->db->delete('users', array('id' => $id));
return $this->db->affected_rows() > 0;
}
/**
* Updates a users last login time.
*
@@ -528,6 +519,17 @@ class User extends CI_Model {
return $this->db->affected_rows() == 1;
}
/**
* Deletes a specified user.
*
* @param string $id
* @return boolean Returns TRUE if the deletion was successful.
*/
public function delete($id) {
$this->db->delete('users', array('id' => $id));
return $this->db->affected_rows() > 0;
}
/**
* Logs a remembed user in.
*