Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Controller
- {
- private $model;
- public function __construct($model){
- $this->model = $model;
- }
- public function clicked() {
- $this->model->testing = "Updated";
- }
- public function arts(){
- $page = isset($_GET['page2']) ? (int)$_GET['page2'] : 1;
- $perPage = isset($_GET['per-page']) && $_GET['per-page'] <= 6 ? (int)$_GET['per-page'] : 6;
- $nextpage = (int)$_GET['page2'] + 1;
- $lastpage = (int)$_GET['page2'] - 1;
- $start = ($page > 1) ? ($page * $perPage) - $perPage : 0;
- $obj = new \stdClass();
- $obj->start = $start;
- $obj->pp = $perPage;
- /*$output = */$this->model->getArticles(json_encode($obj));
- //return $output;
- }
- public function maincon() {
- $id = $_GET['id'];
- $newid = new \stdClass();
- $newid->id = $id;
- /*$output =*/ $this->model->mainart(json_encode($newid));
- //return $output;
- }
- public function getuser() {
- if(!empty($_SESSION['Username'])){
- $login = $_SESSION['Username'];
- $user = new \stdClass();
- $user->username = $login;
- /*$output =*/$this->model->getuser(json_encode($user));
- //return $output;
- }
- }
- public function insertComments(){
- $review = htmlspecialchars($_POST['comment']);
- $obj = new \stdClass();
- $id = $_GET['id'];
- $obj->username = $_SESSION['Username'];
- $obj->id = $id;
- $obj->review = $review;
- $this->model->insertComments(json_encode($obj));
- }
- public function login(){
- $output = '';
- if(isset($_POST['button'])){
- if(isset($_POST['Username']) && !empty($_POST['Username'])){
- $username = $_POST['Username'];
- $email = $_POST['Email'];
- $password = $_POST['Password'];
- if(isset($_POST['Password']) && !empty($_POST['Password'])){
- $objj->pass = $enc_pass;
- $objj->password = $password;
- $objj->email = $email;
- $objj->username = $username;
- $this->model->login(json_encode($objj));
- }
- else{
- $output.= '<div class="alert alert-danger alert-dismissible fade in">';
- $output.= '<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>';
- $output.= '<i class="fas fa-exclamation-circle"></i> <strong> Error:</strong> Please Enter Password.';
- $output.= '</div>';
- return $output;
- }
- }
- else{
- $output.= '<div class="alert alert-danger alert-dismissible fade in">';
- $output.= '<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>';
- $output.= '<i class="fas fa-exclamation-circle"></i> <strong> Error:</strong> Please Enter Username and Password.';
- $output.= '</div>';
- return $output;
- }
- }
- }
- public function reg(){
- if(isset($_POST['button'])){
- if(isset($_POST['Username']) && isset($_POST['Email']) && isset($_POST['Password'])&& isset($_POST['Fname']) && isset($_POST['Lname']) && isset($_POST['CPassword']) && $_POST['g-recaptcha-response']!=""){
- $secret = '6LfEL3QUAAAAAD8ddCdGkR3gdcD9UC8SX03SRZz6';
- $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
- $responseData = json_decode($verifyResponse);
- if($responseData->success){
- if(!empty($_POST['Username']) && !empty($_POST['Email']) && !empty($_POST['Password'])&& !empty($_POST['Fname']) && !empty($_POST['Lname']) && !empty($_POST['CPassword'])){
- if($_POST['CPassword'] == $_POST['Password']){
- $re = '/\A(?=[\x20-\x7E]*?[A-Z])(?=[\x20-\x7E]*?[a-z])(?=[\x20-\x7E]*?[0-9])[\x20-\x7E]{6,}\z/';
- if(preg_match($re, $_POST['Password'])) {
- $errorMessage = 'ERROR: Unallowed character';
- $Fname = $_POST['Fname'];
- $Lname = $_POST['Lname'];
- $username = $_POST['Username'];
- $email = $_POST['Email'];
- $password = $_POST['Password'];
- $ecn_pass = password_hash($_POST['Password'],PASSWORD_DEFAULT,['cost' => 12]);
- if(strlen($password) > 8){
- /*
- function pkcs7_pad($data, $size)
- {
- $length = $size - strlen($data) % $size;
- return $data . str_repeat(chr($length), $length);
- }
- */
- /*$enc_pass = openssl_encrypt(
- pkcs7_pad($password, 16),
- 'AES-256-CBC',
- $encryption_key,
- 0,
- $iv
- );*/
- $obj->pass = $ecn_pass;
- $obj->email = $email;
- $obj->username = $username;
- $obj->fname = $Fname;
- $obj->lname = $Lname;
- $this->model->reg(json_encode($obj));
- }
- else{
- //echo "<h1 style = 'color: red;'>Minimum password length is 8</h1>";
- $output2.= '<div class="alert alert-danger">';
- $output2.= ' <strong>Error:</strong> Minimum password length is 8.';
- $output2.= '</div>';
- return $output2;
- }
- }
- else{
- //echo "<h1 style = 'color: red;'>at least 8 characters of uppercase,lowercase and numbers</h1>";
- $output2.= '<div class="alert alert-danger">';
- $output2.= ' <strong>Error:</strong> at least 8 characters of uppercase,lowercase and numbers.';
- $output2.= '</div>';
- return $output2;
- }
- }
- else{
- //echo "<h1 style = 'color: red;'>Password Confirmation Incorrect</h1>";
- $output2.= '<div class="alert alert-danger">';
- $output2.= ' <strong>Error:</strong> Password Confirmation Incorrect.';
- $output2.= '</div>';
- return $output2;
- }
- }
- else{
- //echo "<h1 style = 'color: red;'>PLEASE FILL IN ALL FIELDS</h1>";
- $output2.='<div class="alert alert-danger">';
- $output2.=' <strong>Error:</strong> PLEASE FILL IN ALL FIELDS.';
- $output2.='</div>';
- return $output2;
- }
- }
- }
- else{
- $output2.= '<div class="alert alert-danger alert-dismissible fade in">';
- $output2.= '<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>';
- $output2.= ' <strong>Error:</strong> Please Complete Captcha.';
- $output2.= '</div>';
- return $output2;
- }
- }
- else{
- echo "";
- }
- }
- public function pagination(){
- $page = isset($_GET['page2']) ? (int)$_GET['page2'] : 1;
- $perPage = isset($_GET['per-page']) && $_GET['per-page'] <= 6 ? (int)$_GET['per-page'] : 6;
- $nextpage = (int)$_GET['page2'] + 1;
- $lastpage = (int)$_GET['page2'] - 1;
- $start = ($page > 1) ? ($page * $perPage) - $perPage : 0;
- $obj = new \stdClass();
- $obj->start = $start;
- $obj->pp = $perPage;
- $getpages = $this->model->getPages(json_encode($obj));
- $pages = json_decode($getpages);
- if((int)$_GET['page2'] <= 1){
- $output .= "<li class='page-item'><a class='page-link' href=#>Previous</a></li>";
- }
- else{
- $output .= "<li class='page-item'><a class='page-link' href=?page2=".$lastpage."&per-page=".$perPage.">Previous</a></li>";
- }
- for($x = 1; $x <= $pages; $x++ ){
- if((int)$_GET['page2'] == $x){
- $test = 'active';
- $css = "<span class='sr-only'>(current)</span>";
- }
- else{
- $test = 'page-item';
- $css = "";
- }
- $output .= "<li class=".$test."><a class='page-link' href=?page2=".$x."&per-page=".$perPage.">".$x."".$css."</a></li>";
- }
- if((int)$_GET['page2'] >= $pages){
- $output .= "<li class='page-item'><a class='page-link' href=#>Next</a></li>";
- }
- else{
- $output .= "<li class='page-item'><a class='page-link' href=?page2=".$nextpage."&per-page=".$perPage.">Next</a></li>";
- }
- return $output;
- }
- public function logout(){
- session_destroy();
- header("Location: https://mayar.abertay.ac.uk/~1800854/MVC/index.php?page=views&model=getArticles");
- die();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement