| Server IP : 188.114.96.2 / Your IP : 104.23.243.201 Web Server : Apache/2.4.59 (Debian) System : Linux EDL-STRETCH 4.19.0-27-amd64 #1 SMP Debian 4.19.316-1 (2024-06-25) x86_64 User : edlftp ( 1002) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /home/nicolasj/www/sms.formationlangues.be/application/models/ |
Upload File : |
<?php
/*********************
*********************
User Registration Model
Creation 02 may 2019
Aernout Guillaume
http://Codes.Solutions
(Most features could be betters,
Google map api should be Lp5uSED)
*********************
*********************/
if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Users_model extends CI_Model
{
public const ENCRYP_SALT = "2a2b2b826766e2fdaab568cccf16435623a8301a316cc507e86d0972741227f4";
public function __construct()
{
parent::__construct(array('no_cache' => 1));
//$this->_ci =& get_instance();
$this->load->model('crud_model');
$this->load->database();
}
public function get_entry(int $id)
{
if ($id <= 0) {
return null;
}
$result = $this->db->get_where('user', ['user_id' => $id])->result();
return isset($result[0]) ? $result[0] : null;
}
public function get_entry_by(array $where)
{
$user = $this->db->get_where('user', $where)->result();
if (!empty($user)) {
return $user[0];
}
return null;
}
public function update_entry(int $id, array $fields): bool
{
if ($id <= 0 || empty($fields)) {
return false;
}
return $this->db->update('user', $fields, ['user_id' => $id]);
}
public function get_users_with_parent_id(): array
{
$query = $this->db->query(
"SELECT u.*, s.parent_id FROM `user` u
LEFT JOIN student s ON u.user_id = s.student_id"
);
$result_users = $query->result();
// Set keys as user_id
$users = [];
foreach ($result_users as $u) {
$users[(int) $u->user_id] = $u;
}
// Add students to parents
foreach ($users as $u) {
if ($u->parent_id && !empty($users[(int) $u->parent_id])) {
if (!empty($users[(int) $u->parent_id]->children)) {
$users[(int) $u->parent_id]->children = [];
}
$users[(int) $u->parent_id]->children[] = $u;
}
}
return $users;
}
/**
* Check if the user have an valid account (fullfilled)
*
* @param stdClass $user
* @return bool
*/
public function fullfilled_user(array $user): bool
{
$valid = !empty($user['first_name']) && !empty($user['last_name']);
return $valid;
}
public function check_email_format($email)
{
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
public function student_exist($id)
{
$query = $this->db->get_where('student', array('student_id' => $id));
$array = $query->result_array();
if (count($array) > 0) {
return true;
}
return false;
}
public function parent_exist($id)
{
$query = $this->db->get_where('parent', array('parent_id' => $id));
$array = $query->result_array();
if (count($array) > 0) {
return true;
}
return false;
}
public function email_exist($mail)
{
$this->db->select('*');
$this->db->from('user');
$this->db->where('email', $mail);
$queryresult = $this->db->get()->result_array();
$queryresult = array_filter($queryresult);
//return $queryresult;
if (!empty($queryresult)) {
return true;
} else {
return false;//email doesnt exist
}
}
public function check_pass($password)
{
// Validate password strength
$uppercase = preg_match('@[A-Z]@', $password);
$lowercase = preg_match('@[a-z]@', $password);
$number = preg_match('@[0-9]@', $password);
$specialChars = preg_match('@[^\w]@', $password);
if (($uppercase || $number || $specialChars) && strlen($password) > 5) {
return true;
} else {
return false;
}
}
public function check_name($name)
{
$notInNames = "![0-9!@#$%^&*()_+={}|\\\\;\"\n\r\t\[\]<>?/]!";
if (!preg_match($notInNames, $name) || strlen($name) < 2) {//preg_match("/^[a-zA-Z ]*$/",$name)
return true;
}
return false;
}
public function check_phone($number)
{
//should use regex by country (symphony lib)
$numbersOnly = preg_replace("[^0-9]", "", $number);
$numberOfDigits = strlen($numbersOnly);
if ($numberOfDigits > 7 && $numberOfDigits < 15) {
return true;
} else {
return false;
}
}
public function check_dob($dob)
{
$minAge = (date('Y') - 3).'/'.date('m/d');
if (strtotime($strDateOfBirth) > strtotime($strSystemMaxDate)) {
return false;
}
return true;
}
public function check_address($line1, $line2, $country, $state, $postcode)
{
//should use google Api
if (strlen($line1) < 3) {
return false;
}
if (strlen($postcode) < 4 || strlen($postcode) > 6) {
return false;
}
return true;
}
public function get_city_by_code($zip)
{
$cities = [];
$filename = "assets/json/zipcode-belgium.json";
$fp = fopen($filename, "r");
$contents = fread($fp, filesize($filename));
$data = json_decode($contents, true);
foreach ($data as $key => $value) {
//return($value['zip']);
if ($value['zip'] == $zip) {
$cities[] = $value['city'];
}
}
return $cities;
}
////////////////////////////////
////////////////////////////////
// Creation funcitons
////////////////////////////////
////////////////////////////////
//final : payment done
//active : profile completion
public function create_user($mail, $pass, $name1, $name2, $phone, $perm, $lang, $final = "1", $active = "1", $paid = "1")
{
$pass = password_hash($pass, PASSWORD_DEFAULT);
if ($final == "3") {
$final = "0";
}
$author = $this->session->userdata('userID');
if ($author == null) {
$author = "shop";
}
$phone = preg_replace("[^0-9]", "", $phone);
$this->db->insert('user', [
'role_id' => $perm,
'email' => $mail,
'password' => $pass,
'first_name' => $name1,
'phone' => $phone,
'last_name' => $name2,
'is_active' => $active,
'is_final' => $final,
'is_paid' => $paid,
'lang' => $lang,
'CreationAuthor' => $author
]);
return $this->db->insert_id();
}
/**
* Create user and teacher account only with email and password.
* Others data must be completed by teacher in first login.
*
* @param string $email
* @param string $password
* @return boolean
*/
public function create_teacher_account(string $email, string $password): bool
{
// Data validation
if (empty($email) || $this->users_model->check_email_format($email) === false) {
return false;
} elseif ($this->users_model->email_exist($email)) {
return false;
} elseif (empty($password) || $this->users_model->check_pass($password) === false) {
return false;
}
// Create user
$user_data = [
'role_id' => 2,
'email' => $email,
'password' => password_hash($password, PASSWORD_DEFAULT),
'is_active' => 1,
'is_final' => 1,
'is_paid' => 1,
'CreationAuthor' => $this->session->userdata('userID') ?? 'shop'
];
if ($this->db->insert('user', $user_data) === false) {
return false;
}
$user = $this->get_entry_by(['email' => $email]);
// Create teacher profile
return $this->db->insert('teacher', [
'teacher_id' => (int) $user->user_id,
'address_id' => null,
'rate' => 999,
'admin_contract' => null,
'admin_state' => 3, // Wrong statut
'admin_public' => null,
'admin_localisation' => null,
'admin_diplome' => null
]);
}
public function create_user_address($l1, $l2, $country, $state, $location, $code)
{
$this->db->insert('address', [
'line_1' => $l1,
'line_2' => $l2,
'location_id' => 4,
'postcode' => $code,
'location' => $location,
'country' => $country
]);
return $this->db->insert_id();
}
public function create_student($id, $addressId, $comment, $dob, $parent)
{
$sql = array(
'student_id' => "$id",
'residential_address_id' => "$addressId",
'admin_comment' => "$comment",
'dob' => "$dob",
'parent_id' => "$parent",
'Archived' => 0
);
//return $this->crud_model->cleanArray($sql);
$this->db->insert('student', $this->crud_model->cleanArray($sql));
$this->db->set(array('dob' => $dob));
$this->db->where('user_id', $id);
$this->db->update('user');
//return $this->db->insert_id();
return $id;
//return true;
}
public function create_group($id, $addressId, $comment, $age, $number)
{
$sql = array(
'group_id' => "$id",
'invoice_address_id' => "$addressId",
'number' => "$number",
'dob' => "$age",
'admin_comment' => "$comment",
'creation' => ""
);
//return $sql;
//$sql = $this->db->get_compiled_insert('group', $sql);
//return $sql;
$this->db->insert('group', $sql);
//$id = $this->db->insert_id();
return true;
}
public function create_parent($id, $addressId, $comment, $dob)
{
$sql = array(
'parent_id' => "$id",
'residential_address_id' => "$addressId",
'admin_comment' => "$comment",
'dob' => "$dob"
);
$this->db->insert('parent', $sql);
//$id = $this->db->insert_id();
return true;
}
public function create_teacher($id, $addressId, $comment, $dob, $rate, $a_c = "0", $a_s = "0", $a_p = "0", $a_l = "0", $a_d = "0")
{
$sql = array(
'teacher_id' => (int) $id,
'address_id' => (int) $addressId,
'admin_comment' => $comment,
'rate' => $rate,
'dob' => $dob,
'admin_contract' => $a_c,
'admin_state' => $a_s,
'admin_public' => $a_p,
'admin_localisation' => $a_l,
'admin_diplome' => $a_d
);
$this->db->insert('teacher', $this->crud_model->cleanArray($sql));
return true;
}
public function create_teacher_lang($id, $lan)
{
$sql = array(
'teacher_id' => (int) $id,
'course_language_id' => (int) $lan
);
$this->db->insert('teacher_course_language', $sql);
return true;
}
////////////////////////////////
////////////////////////////////
// Update funcitons
////////////////////////////////
////////////////////////////////
public function update_user($id, $mail, $pass, $first_name, $last_name, $phone, $lang, $creation = "", $dob = null)
{
//if email changed check email_exist
if (($this->crud_model->get_user_email($id) != $mail) && ($this->email_exist($mail))) {
return false;
}
$user_data = [
'email' => $mail,
'phone' => preg_replace("[^0-9]", "", $phone),
'lang' => $lang,
'CreationDate' => $creation
];
if (!empty($pass)) {
$user_data['password'] = password_hash($pass, PASSWORD_DEFAULT);
}
if (!empty($first_name)) {
$user_data['first_name'] = $first_name;
}
if (!empty($last_name)) {
$user_data['last_name'] = $last_name;
}
if (!empty($dob)) {
$user_data['dob'] = $dob;
}
$this->db->set($this->cleanArray($user_data));
$this->db->where('user_id', $id);
$this->db->update('user');
return true;
}
public function update_user_address($id, $l1, $l2, $country, $state, $location, $code)
{
$addressSql = array(
'line_1' => "$l1",
'line_2' => "$l2",
'location_id' => '4',
'postcode' => "$code",
'location' => "$location",
'country' => "$country"
);//location_id = 4 pour bxl
$this->db->set($addressSql);
$this->db->where('address_id', $id);
$this->db->update('address');
return true;
}
public function add_parent_address_id($id, $parentId)
{
$sql = array("residential_address_id" => $id);
$this->db->set($sql);
$this->db->where('parent_id', $parentId);
$this->db->update('parent');
}
public function add_student_address_id($id, $studentId)
{
$sql = array("residential_address_id" => $id);
$this->db->set($sql);
$this->db->where('student_id', $studentId);
$this->db->update('student');
}
public function update_student($id, $comment, $dob, $parent, $nrn = '', $school = '')
{
if ($parent != "") {
$sql = array(
'admin_comment' => "$comment",
'dob' => "$dob",
'parent_id' => "$parent",
'register_number' => "$nrn",
'school' => "$school"
);
} else {
$sql = array(
'dob' => "$dob",
'register_number' => "$nrn",
'school' => "$school"
);
}
//return $sql;
$this->db->set($sql);
$this->db->where('student_id', $id);
$this->db->update('student');
return true;
}
public function update_group($id, $comment, $status, $dob, $size)
{
$sql = array(
'admin_comment' => "$comment",
'dob' => "$dob",
'number' => "$size"
);
$this->db->set($sql);
$this->db->where('group_id', $id);
$this->db->update('group');
return true;
}
public function update_parent($id, $comment, $dob, $phone_sos = '')
{
if ($comment != "") {
$sql = array(
'admin_comment' => "$comment",
'dob' => "$dob",
'phone_sos' => "$phone_sos"
);
} else {
$sql = array(
'dob' => "$dob",
'phone_sos' => "$phone_sos"
);
}
$this->db->set($this->crud_model->cleanArray($sql));
$this->db->where('parent_id', $id);
$this->db->update('parent');
//return $this->db->last_query();
}
public function update_parent_bis($id, $address, $comment = "", $dob)
{
if ($comment != "") {
$sql = array(
'admin_comment' => "$comment",
'dob' => "$dob"
);
} else {
$sql = array(
'dob' => "$dob"
);
}
$this->db->set($sql);
$this->db->where('parent_id', $id);
$this->db->update('parent');
return true;
}
public function update_teacher($id, $comment, $dob, $rate, $a_c = "0", $a_s = "0", $a_p = "0", $a_l = "0", $a_d = "0")
{
$sql = [
'dob' => $dob,
'admin_contract' => $a_c,
'admin_state' => $a_s,
'admin_public' => $a_p,
'admin_localisation' => $a_l,
'admin_diplome' => $a_d
];
if ($comment !== null) {
$sql['admin_comment'] = $comment;
}
if ($rate !== null) {
$sql['rate'] = $rate;
}
$this->db->set($sql);
$this->db->where('teacher_id', $id);
$this->db->update('teacher');
return true;
}
public function clean_teacher_lang($id)
{
$this->db->where('teacher_id', $id);
$this->db->delete('teacher_course_language');
return true;
}
public function update_teacher_lang($id, $lan)
{
$sql = array(
'teacher_id' => "$id",
'course_language_id' => "$lan"
);
$this->db->insert('teacher_course_language', $sql);
return true;
}
public function log_connection()
{
// $id = 1;
$id = $this->session->userdata('userID');
//var_dump($id);
$sql = array(
'last_connection' => date('Y-m-d')
);
//var_dump($sql);
$this->db->set($sql);
$this->db->where('user_id', $id);
$this->db->update('user');
}
////////////////////////////////
////////////////////////////////
// Info funcitons
////////////////////////////////
////////////////////////////////
public function get_profile_address($userId = "")
{
$userRole;
if ($userId == "") {
$userId = $this->session->userdata('userID');
$userRole = $this->session->userdata('login_type');
} else {
$userRole = $this->crud_model->get_user_role($this->crud_model->get_user_info($userId)[0]['role_id']);
}
switch ($userRole) {
case 'admin':
return "0";
break;
case 'student':
$query = $this->db->get_where('student', array('student_id' => $userId));
$res = $query->result_array();
foreach ($res as $row) {
return $row['residential_address_id'];
}
break;
case 'teacher':
$query = $this->db->get_where('teacher', array('teacher_id' => $userId));
$res = $query->result_array();
foreach ($res as $row) {
return $row['address_id'];
}
break;
case 'parent':
$query = $this->db->get_where('parent', array('parent_id' => $userId));
$res = $query->result_array();
foreach ($res as $row) {
return $row['residential_address_id'];
}
break;
case 'invited':
$query = $this->db->get_where('shop_transaction', array('User_ID' => $userId));
$res = $query->result_array();
foreach ($res as $row) {
return $row['Address_ID'];
}
break;
}
}
public function is_paid($id)
{
$query = $this->db->get_where('user', array('user_id' => $id));
$array = $query->result_array();
if ($array[0]['is_paid'] == 0) {
return false;
} else {
return true;
}
}
////////////////////////////////
////////////////////////////////
// Assign funcitons
////////////////////////////////
////////////////////////////////
public function find_address($id)
{
$query = $this->db->get_where('teacher', array('teacher_id' => $id));
$array = $query->result_array();
if (!empty($array)) {
return $array[0]['address_id'];
}
$query = $this->db->get_where('student', array('student_id' => $id));
$array = $query->result_array();
if (!empty($array)) {
return $array[0]['residential_address_id'];
}
$query = $this->db->get_where('parent', array('parent_id' => $id));
$array = $query->result_array();
if (!empty($array)) {
return $array[0]['residential_address_id'];
}
$query = $this->db->get_where('shop_transaction', array('User_ID' => $id));
$array = $query->result_array();
if (!empty($array)) {
return $array[0]['Address_ID'];
}
return 0;
}
public function find_dob($id)
{
$query = $this->db->get_where('teacher', array('teacher_id' => $id));
$array = $query->result_array();
if (!empty($array)) {
return $array[0]['dob'];
}
$query = $this->db->get_where('student', array('student_id' => $id));
$array = $query->result_array();
if (!empty($array)) {
return $array[0]['dob'];
}
$query = $this->db->get_where('parent', array('parent_id' => $id));
$array = $query->result_array();
if (!empty($array)) {
return $array[0]['dob'];
}
return "";
}
public function checkUserType($id, $role)
{
switch ($role) {
case '2':
//teacher
$query = $this->db->get_where('teacher', array('teacher_id' => $id));
$array = $query->result_array();
if (empty($array)) {
return true;
}
return false;
break;
case '3':
//parent
$query = $this->db->get_where('parent', array('parent_id' => $id));
$array = $query->result_array();
if (empty($array)) {
return true;
}
return false;
break;
case '4':
//student
$query = $this->db->get_where('student', array('student_id' => $id));
$array = $query->result_array();
if (empty($array)) {
return true;
}
return false;
break;
default:
return true;
break;
}
}
public function checkAssigned($si, $ci, $li)
{
$sql = "student_id='$si' AND course_id='$ci' AND lesson_id='$li'";
$this->db->where($sql);
$check = $this->db->get('course_student_new');
$check = $check->result_array();
//return $check;
if (empty($check)) {
return "false";
} else {
return "true";
}
}
public function checkAssignedTeacher($si, $ci, $li)
{
$sql = "teacher_id='$si' AND course_id='$ci' AND lesson_id='$li'";
$this->db->where($sql);
$check = $this->db->get('course_teacher');
$check = $check->result_array();
if (empty($check)) {
return "false";
} else {
return "true";
}
}
////////////////////////////////
////////////////////////////////
// Pass funcitons
////////////////////////////////
////////////////////////////////
public function generate_pass($length)
{
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!/+?.,";
return substr(str_shuffle($chars), 0, $length);
}
public function cleanArray($array)
{
//remove empty fields
foreach ($array as $key => $value) {
if ($value == "") {
unset($array[$key]);
}
}
return $array;
}
/**
* Return uniq user hash (for user filenames, etc)
*
* @param integer $user_id
* @return string
*/
public function uniq_user_hash(int $user_id): string
{
return hash('sha256', $user_id . Users_model::ENCRYP_SALT);
}
}