public function register()
 {
     if (is_post()) {
         $this->loadHelper('Validator');
         if (captcha()) {
             $data = ['email' => validate('email', 'email'), 'username' => validate('required', 'username'), 'password' => password_hash(validate('required', 'register_token'), PASSWORD_BCRYPT), 'token' => str_rand(40)];
             if (validator($data)) {
                 if ($this->user->checkExistUser($data['email'])) {
                     $data2 = ['firstname' => validate('required', 'firstname'), 'lastname' => validate('required', 'lastname'), 'nickname' => validate('required', 'nickname'), 'major' => validate('required', 'major')];
                     if (validator($data2)) {
                         $this->user->createUser($data, $data2);
                         $validate = $this->user->validate($data['email'], $_POST['register_token']);
                         if (!empty($validate)) {
                             $_SESSION['auth'] = $validate;
                             $_SESSION['user'] = $this->user->getDetail($validate['id']);
                             cache_forgot('user.members.' . user('major'));
                             cache_forgot('user.get.members.' . user('major'));
                         }
                     }
                 }
             }
         }
     }
     return redirect('');
 }
 public function p($action = null, $id = null)
 {
     switch ($action) {
         case 'news':
             if (is_post()) {
                 $gbn = new \app\models\GbNews();
                 $data = array('title' => $_POST['title'], 'content' => $_POST['content']);
                 if ($_POST['active'] === 'on') {
                     $data['active'] = 1;
                 } else {
                     $data['active'] = 0;
                 }
                 if (!empty($_POST['action'])) {
                     $data['id'] = $_POST['id'];
                     $gbn->update($data);
                     cache_forgot('home.gbnews');
                     cache_forgot('p.gbn.' . $data['id']);
                 } else {
                     $gbn->create($data);
                     cache_forgot('home.gbnews');
                 }
                 return redirect('admin/p/news');
             } else {
                 $gbn = new \app\models\GbNews();
                 $gbn = $gbn->all();
                 $data = empty($gbn) ? array() : $gbn;
                 return $this->view('admin/gbnews', compact('data'));
             }
             break;
         case 'write':
             if (!empty($id)) {
                 $id = intval($id);
                 $news = new \app\models\GbNews();
                 $news = $news->getNews($id);
                 if (empty($news)) {
                     return $this->view('errors/404');
                 }
                 return $this->view('admin/gbnews_write', compact('news'));
             }
             return $this->view('admin/gbnews_write');
             break;
         case 'destroy':
             echo $_POST['id'];
             if (is_post()) {
                 $news = new \app\models\GbNews();
                 $news->remove($_POST['id']);
             }
             break;
         default:
             return redirect('admin');
             break;
     }
 }
 public function run()
 {
     $data = cache_remember('user.inactive.' . auth('id'), function () {
         $user = new \app\models\User();
         return $user->getActiveData(auth('id'));
     });
     if ($data['active'] === '1') {
         $_SESSION['auth']['active'] = '1';
         cache_forgot('user.inactive.' . auth('id'));
         echo '<div class="alert alert-success">คุณได้รับการยืนยันแล้วว่า ข้อมูลถูกต้องและเป็นสมาชิกของสาขานี้ กรุณา reload page</div>';
     }
 }
 private function update($_filename, $_data)
 {
     if (file_exists($this->user_path . $_filename)) {
         if (copy($this->user_path . $_filename, $this->user_path . $_filename . '.bak')) {
             $file_get = file_get_contents($this->user_path . $_filename);
             $arr = json_decode($file_get, true);
             if (is_null($arr['lists'])) {
                 $arr['lists'] = array();
             }
             if (empty($arr['lists'][$this->current_month])) {
                 $arr['lists'][$this->current_month] = array();
             }
             $result = array_merge($arr['lists'][$this->current_month], $_data);
             $arr['lists'][$this->current_month] = $result;
             ksort($arr['lists']);
             ksort($arr['lists'][$this->current_month]);
             $op = @fopen($this->user_path . $_filename, 'w');
             if (flock($op, LOCK_EX)) {
                 fwrite($op, json_encode($arr));
                 fflush($op);
                 flock($op, LOCK_UN);
             }
             fclose($op);
             cache_forgot('ledger.index.' . (string) auth('id'));
         }
         return true;
     }
     return false;
 }
 private function eCover()
 {
     if (!empty($_FILES['cover'])) {
         $img_w = 900;
         $img_h = 200;
         $jpeg_quality = 90;
         $dst_r = ImageCreateTrueColor($img_w, $img_h);
         $src = $_FILES['cover']['tmp_name'];
         $getSize = getimagesize($src);
         $img_r = $this->checkIMG($getSize['mime'], $src);
         $dst_ratio = $getSize['0'] / $getSize['1'];
         $img_ratio = $img_w / $img_h;
         if ($dst_ratio >= $img_ratio) {
             $dst_h = $getSize['1'];
             $dst_w = $dst_h / $img_ratio;
             $dst_x = ($getSize['0'] - $dst_w) / 2;
             $dst_y = 0;
         } else {
             $dst_w = $getSize['0'];
             $dst_h = $dst_w / $img_ratio;
             $dst_x = 0;
             $dst_y = ($getSize['1'] - $dst_h) / 2;
         }
         imagecopyresampled($dst_r, $img_r, 0, 0, $dst_x, $dst_y, $img_w, $img_h, $dst_w, $dst_h);
         imagejpeg($dst_r, APP_PATH . 'contents/users/covers/' . auth('id') . '.jpg', $jpeg_quality);
         imagedestroy($dst_r);
         imagedestroy($img_r);
         if (empty(user('cover'))) {
             $user = new User();
             $user->setCover(auth('id'), auth('id') . '.jpg');
             $_SESSION['user']['cover'] = auth('id') . '.jpg';
             cache_forgot('user.i.' . auth('id'));
         }
     }
 }
 private function eNews($id = null)
 {
     if (is_post()) {
         $this->loadHelper('Validator');
         $data = array('title' => strip_tags(validate('required', 'title')), 'content' => validate('required', 'content'), 'updated_at' => date('Y-m-d H:i:s'));
         $id = validate('required', 'token');
         if (validator($data) && !is_null($id)) {
             $id = base64_decode($id);
             if (!preg_match('/^[0-9]+$/', $id)) {
                 exit('401');
             }
             $news = new \app\models\News();
             if ($_POST['c_readIn'] === 'on') {
                 $data['readIn'] = null;
             }
             if ($news->updateNews($data, $id)) {
                 cache_forgot('p.n.' . $id);
                 cache_forgot('p.getJson.news.' . user('major'));
             }
             return redirect('');
         }
     } else {
         if (preg_match('/^[0-9]+$/', $id)) {
             $data = cache_remember('p.n.' . $id, function () use($id) {
                 $news = new \app\models\News();
                 $data = $news->getNews($id);
                 if (!empty($data)) {
                     return $data;
                 }
                 return false;
             });
             if (!is_null($data) && $data['major_id'] === user('major')) {
                 return $this->view('editNews', compact('data'));
             }
         }
         return $this->view('errors/404');
     }
 }