���ѧۧݧ�ӧ�� �ާ֧ߧ֧էا֧� - ���֧էѧܧ�ڧ��ӧѧ�� - /var/www/dev-kft.petruch.de/classes/user.class.php
���ѧ٧ѧ�
<?php class User{ private $sql = false; public $id = false; public $name = ''; public $firstName = ''; public $rights = false; public $hash = ''; public $email = ''; public $color = ''; public $image = false; public $loggedIn = false; public $reg = false; public $path = 'files/user/'; private $mailer = false; function __construct(){ global $sql; $this->sql = $sql; global $mailer; $this->mailer = $mailer; if(isset($_COOKIE['kft_u']) && $_COOKIE['kft_u'] && !preg_match('/^[\w]+$/', ((base64_decode($_COOKIE['kft_u'])-1328)/128))){ $this->logout(); } } public function showTabletOnly($tabletOnly){ switch($tabletOnly){ case 10: return 'Produktion & Wareneingang'; break; case 30: return 'Nur Wareneingang'; break; case 50: return 'Nur Produktion'; break; default: return 'Unbekannt'; break; } } public function showProfileImage(){ $r = '<div class="profil-image '.((!$this->image?'text':'')).'">'; if($this->image) $r .= '<img src="'.$this->image.'?'.filemtime(DIR.$this->image).'" alt="'.$this->name.'"/>'; else $r .= substr($this->firstName,0,1).substr($this->name,0,1); $r .= '</div>'; return $r; } public function checkIfEmailExists($email){ return $this->sql->select('id,email','user','email = ?',$email,1); } /** * Add User with all data and profile image -> Used in Admin Panel * @param object $p * @param array $f * @return bool */ public function add($p,$f){ $hash = md5(date('YmdHis').mt_rand(0,999999)); if($p->tabletOnly == 100){ if($p->email == '') return false; if($this->checkIfEmailExists($p->email)) return false; $pw = createStr(20, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*()_+'); $password = $this->createHash($pw); $sendMail = true; $rights = 1; $p->tabletOnly = 10; }else{ $sendMail = false; $password = ''; $rights = 0; } $color = generateLightColor(); $userID = $this->sql->insert('user','email,name,firstName,password,active,tabletOnly,pin,color,rights,created,changed,hash', [$p->email,$p->name,$p->firstName,$password,1,$p->tabletOnly,$p->pin,$color,$rights,NOW,NOW,$hash]); if($userID && $f && is_uploaded_file($f->image->tmp_name)){ // Create Dir if not exists if(!is_dir(DIR.$this->path)) mkdir(DIR.$this->path, 0777, true); $imagick = new Imagick($f->image->tmp_name); $imagick->scaleImage(350, 350, true); $imagick->setImageFormat("webp"); $imagick->setImageCompressionQuality(70); $imagick->setCompressionQuality(70); $imagick->setOption('webp:method', '6'); $imagick->writeImage(DIR.$this->path.$userID.'.webp'); $image = $this->path.$userID.'.webp'; $r = $this->sql->update('user','image,imgChanged',[$image,NOW],'id=?',$userID); } if($userID && $sendMail){ $this->sendPwMail($p,$pw); } return $userID; } /** * Change User with all data and profile image -> Used in Admin Panel * @param object $p * @param object $f * @return boolean */ public function change($p,$f){ $sendMail = false; if($p->tabletOnly == 100){ // This is an Admin with full rights if($p->email == '') return false; if($p->emailOld == '' || $p->hasPW == "0"){ $sendMail = true; $pw = createStr(20, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*()_+'); $password = $this->createHash($pw); } if($checkMail = $this->checkIfEmailExists($p->email)){ if($p->userID != $checkMail->id) return false; } $rights = 1; $p->tabletOnly = 10; }else{ $password = ''; $rights = 0; } $r = $this->sql->update('user','firstName,name,email,color,active,tabletOnly,pin,rights,changed',[$p->firstName,$p->name,$p->email,$p->color,$p->active,$p->tabletOnly,$p->pin,$rights,NOW],'id=?',$p->userID); if($r && $f && is_uploaded_file($f->image->tmp_name)){ // Create Dir if not exists if(!is_dir(DIR.$this->path)) mkdir(DIR.$this->path, 0777, true); $imagick = new Imagick($f->image->tmp_name); $imagick->scaleImage(350, 350, true); $imagick->setImageFormat("webp"); $imagick->setImageCompressionQuality(70); $imagick->setCompressionQuality(70); $imagick->setOption('webp:method', '6'); $imagick->writeImage(DIR.$this->path.$this->id.'.webp'); $this->image = $this->path.$this->id.'.webp'; $r = $this->sql->update('user','image,imgChanged',[$this->image,NOW],'id=?',$p->userID); } if($r && $sendMail){ $this->sql->update('user','password=?',[$password],'id=?',$p->userID); $this->sendPwMail($p,$pw); } return $r; } /** * Send Password Mail * @param object $p * @param string $pw * @return boolean */ private function sendPwMail($p,$pw){ $subject = 'Dein Zugang zu '.SITENAME; $message = 'Hallo '.$p->firstName.' '.$p->name.',<br /><br /> wir haben einen Account für dich angelegt.<br /> Dein Benutzername lautet: '.$p->email.'<br /> Dein Passwort lautet: '.$pw.'<br /><br /> <a href="'.HOST.'">Zum Login</a><br /><br /> Viele Grüße<br /> '.SITENAME; return $this->mailer->myMail($p->email,$subject,$message); } /** * Delete User -> Used in Admin Panel */ public function delete($p){ if(!$p->deleteUser) return false; return $this->sql->delete('user','id=? AND id != ?',[$p->userID,$this->id]); } /** * Add User with random password -> Used only for first user in setup * @param object $p * @return boolean */ public function addUser($p){ $hash = md5(date('YmdHis').mt_rand(0,999999)); $pw = createStr(20, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*()_+'); $password = $this->createHash($pw); return $this->sql->insert('user','email,name,firstName,password,active,created,changed,hash',[$p->email,$p->name,$p->firstName,$password,1,NOW,NOW,$hash]); } public function changeMe($p,$f){ $r = $this->sql->update('user','firstName,name,email,color, changed',[$p->firstName,$p->name,$p->email,$p->color,NOW],'id=?',$this->id); if($r && $f && is_uploaded_file($f["image"]->tmp_name)){ // Create Dir if not exists if(!is_dir(DIR.$this->path)) mkdir(DIR.$this->path, 0777, true); $imagick = new Imagick($f["image"]->tmp_name); $imagick->scaleImage(350, 350, true); $imagick->setImageFormat("webp"); $imagick->setImageCompressionQuality(70); $imagick->setCompressionQuality(70); $imagick->setOption('webp:method', '6'); $imagick->writeImage(DIR.$this->path.$this->id.'.webp'); $this->image = $this->path.$this->id.'.webp'; $r = $this->sql->update('user','image,imgChanged',[$this->image,NOW],'id=?',$this->id); } return $r; } public function changeMePassword($p){ if($p->password_new != $p->f_password_new_2) return false; $pwHash = $this->createHash($p->password_old); if(!$this->sql->select("id","user","id = ? AND password = ?",[$this->id,$pwHash],1)) return false; $password = $this->createHash($p->password_new); return $this->sql->update('user','password,changed',[$password,NOW],'id=?',$this->id); } public function getAll(){ return $this->sql->select('id,active,rights,email,name,firstName,pin,tabletOnly,color','user ORDER BY firstName ASC, name ASC'); } public function getUser($userID){ return $this->sql->select('id,active,rights,email,name,firstName,pin,tabletOnly,color,IF(password = "",0,1) AS hasPW','user','id = ?',[$userID],1); } public function setUser(){ if(!isset($_COOKIE["kft_u"],$_COOKIE["kft_p"],$_COOKIE["kft_e"])){ $this->id = false; return false; } $id = (base64_decode($_COOKIE['kft_u'])-1328)/128; $password = $this->decodeRand(base64_decode($_COOKIE['kft_p'])); $email = $this->decodeRand(base64_decode($_COOKIE['kft_e'])); $u = $this->sql->select('id,email,name,firstName,rights,hash,image,color','user','id = ? AND password = ? AND email = ? AND active = 1',[$id,$password,$email],1); if(!$u) return $this->logout(); if($id != $u->id) return $this->logout(); $this->id = $u->id; $this->rights = $u->rights; $this->email = $u->email; $this->name = $u->name; $this->firstName = $u->firstName; $this->hash = $u->hash; $this->loggedIn = true; $this->image = ($u->image)?$u->image:false; $this->color = $u->color; $this->refreshCookies(); } private function encodeRand($str) { mt_srand(SEED); $out = array(); for ($x=0, $l=strlen($str); $x<$l; $x++) $out[$x] = (ord($str[$x]) * 7) + mt_rand(1531, 35987); mt_srand(); return implode('-', $out); } private function decodeRand($str) { mt_srand(SEED); $blocks = explode('-', $str); $out = array(); foreach ($blocks as $block) { $ord = (intval($block) - mt_rand(1531, 35987)) / 7; $out[] = chr($ord); } mt_srand(); return implode('', $out); } public function login($p){ $pwHash = $this->createHash($p->password); $email = $p->email; $u = $this->sql->select("id,rights,password,email","user","email = ?",$email,1); //$this->decodeRand(base64_decode($u->password)); if(!$u){ $this->logout(); return false; } $_u = $this->sql->select("id","user","email = ? AND password = ?",[$email,$pwHash],1); if(!$_u){ $this->logout(); return false; } $UserID = base64_encode(($u->id*128)+1328); $Hash = base64_encode($this->encodeRand($u->password)); $login = base64_encode($this->encodeRand($u->email)); $sess_neu = base64_encode($this->encodeRand(session_id())); setcookie('kft_s', $sess_neu, strtotime("+12 month"), "/"); setcookie('kft_u', $UserID, strtotime("+12 month"), "/"); setcookie('kft_p', $Hash, strtotime("+12 month"), "/"); setcookie('kft_e', $login, strtotime("+12 month"), "/"); setcookie('kft_r', $u->rights, strtotime ("+12 month"), "/"); $_COOKIE['kft_s'] = $sess_neu; $_COOKIE['kft_u'] = $UserID; $_COOKIE['kft_p'] = $Hash; $_COOKIE['kft_e'] = $login; $_COOKIE['kft_r'] = $u->rights; $this->loggedIn = false; $this->setUser(); return 1; } private function refreshCookies(){ setcookie('kft_s', $_COOKIE['kft_s'], strtotime("+12 month"), "/"); setcookie('kft_u', $_COOKIE['kft_u'], strtotime("+12 month"), "/"); setcookie('kft_p', $_COOKIE['kft_p'], strtotime("+12 month"), "/"); setcookie('kft_e', $_COOKIE['kft_e'], strtotime("+12 month"), "/"); setcookie('kft_r', $_COOKIE['kft_r'], strtotime("+12 month"), "/"); } public function logout(){ setcookie('kft_s', "", time() -1, "/"); setcookie("kft_u", "", time() -1, "/"); setcookie("kft_p", "", time() -1, "/"); setcookie("kft_e", "", time() -1, "/"); setcookie("kft_r", "", time() -1, "/"); $_COOKIE['kft_s'] = false; $_COOKIE['kft_u'] = false; $_COOKIE['kft_p'] = false; $_COOKIE['kft_e'] = false; $_COOKIE['kft_r'] = false; unset($_COOKIE['kft_s']); unset($_COOKIE['kft_u']); unset($_COOKIE['kft_p']); unset($_COOKIE['kft_e']); unset($_COOKIE['kft_r']); //youtube: $_SESSION['token'] = false; unset($_SESSION['token']); $this->setUser(); return true; } private function createHash($input){ $hash = hash('whirlpool', $input); return hash('sha256', $hash); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.33 | ���֧ߧ֧�ѧ�ڧ� ����ѧߧڧ��: 0 |
proxy
|
phpinfo
|
���ѧ����ۧܧ�