diff --git a/application/config/autoload.php b/application/config/autoload.php index 530b91b..e49b21d 100755 --- a/application/config/autoload.php +++ b/application/config/autoload.php @@ -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'); /* diff --git a/application/helpers/hash_helper.php b/application/helpers/hash_helper.php deleted file mode 100644 index fb40d24..0000000 --- a/application/helpers/hash_helper.php +++ /dev/null @@ -1,45 +0,0 @@ - - */ - -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 */ diff --git a/application/models/experiment.php b/application/models/experiment.php index ca2c7fa..034c4cc 100644 --- a/application/models/experiment.php +++ b/application/models/experiment.php @@ -24,16 +24,26 @@ /** * Experiments are used to store different variations of the same project. * + * @package ScattPort + * @subpackage Models * @author Karsten Heiken * @author Eike Foken */ 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)) { diff --git a/application/models/group.php b/application/models/group.php index 51cdccd..ac35ec6 100644 --- a/application/models/group.php +++ b/application/models/group.php @@ -1,6 +1,6 @@ + * 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(); } diff --git a/application/models/job.php b/application/models/job.php index 8048917..78b7dbf 100644 --- a/application/models/job.php +++ b/application/models/job.php @@ -22,25 +22,35 @@ */ /** + * Model for jobs. + * + * @package ScattPort + * @subpackage Models * @author Karsten Heiken + * @author Eike Foken */ 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); diff --git a/application/models/parameter.php b/application/models/parameter.php index d902bcb..9593988 100644 --- a/application/models/parameter.php +++ b/application/models/parameter.php @@ -26,6 +26,8 @@ * * Each program has many parameters used for configuration of experiments. * + * @package ScattPort + * @subpackage Models * @author Eike Foken */ 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 diff --git a/application/models/program.php b/application/models/program.php index 4914bae..49728b8 100644 --- a/application/models/program.php +++ b/application/models/program.php @@ -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)); diff --git a/application/models/project.php b/application/models/project.php index 222d9ab..7452c21 100644 --- a/application/models/project.php +++ b/application/models/project.php @@ -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; } } diff --git a/application/models/setting.php b/application/models/setting.php index c5a4fd4..a09541a 100644 --- a/application/models/setting.php +++ b/application/models/setting.php @@ -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; } /** diff --git a/application/models/share.php b/application/models/share.php index 3b7db1e..2e9a1af 100644 --- a/application/models/share.php +++ b/application/models/share.php @@ -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; } diff --git a/application/models/user.php b/application/models/user.php index c5dc56c..2714a15 100644 --- a/application/models/user.php +++ b/application/models/user.php @@ -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. *