Advertisement
Guest User

Controller

a guest
Dec 5th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.38 KB | None | 0 0
  1. <?php
  2.  
  3. class Controller
  4. {
  5. private $model;
  6.  
  7. public function __construct($model){
  8. $this->model = $model;
  9. }
  10.  
  11. public function clicked() {
  12. $this->model->testing = "Updated";
  13. }
  14.  
  15.  
  16. public function arts(){
  17. $page = isset($_GET['page2']) ? (int)$_GET['page2'] : 1;
  18. $perPage = isset($_GET['per-page']) && $_GET['per-page'] <= 6 ? (int)$_GET['per-page'] : 6;
  19. $nextpage = (int)$_GET['page2'] + 1;
  20. $lastpage = (int)$_GET['page2'] - 1;
  21. $start = ($page > 1) ? ($page * $perPage) - $perPage : 0;
  22. $obj = new \stdClass();
  23. $obj->start = $start;
  24. $obj->pp = $perPage;
  25. /*$output = */$this->model->getArticles(json_encode($obj));
  26. //return $output;
  27.  
  28. }
  29. public function maincon() {
  30. $id = $_GET['id'];
  31. $newid = new \stdClass();
  32. $newid->id = $id;
  33. /*$output =*/ $this->model->mainart(json_encode($newid));
  34. //return $output;
  35. }
  36. public function getuser() {
  37. if(!empty($_SESSION['Username'])){
  38. $login = $_SESSION['Username'];
  39. $user = new \stdClass();
  40. $user->username = $login;
  41. /*$output =*/$this->model->getuser(json_encode($user));
  42. //return $output;
  43. }
  44. }
  45.  
  46.  
  47. public function insertComments(){
  48. $review = htmlspecialchars($_POST['comment']);
  49. $obj = new \stdClass();
  50. $id = $_GET['id'];
  51. $obj->username = $_SESSION['Username'];
  52. $obj->id = $id;
  53. $obj->review = $review;
  54. $this->model->insertComments(json_encode($obj));
  55.  
  56. }
  57.  
  58. public function login(){
  59. $output = '';
  60. if(isset($_POST['button'])){
  61. if(isset($_POST['Username']) && !empty($_POST['Username'])){
  62. $username = $_POST['Username'];
  63. $email = $_POST['Email'];
  64. $password = $_POST['Password'];
  65.  
  66. if(isset($_POST['Password']) && !empty($_POST['Password'])){
  67. $objj->pass = $enc_pass;
  68. $objj->password = $password;
  69. $objj->email = $email;
  70. $objj->username = $username;
  71. $this->model->login(json_encode($objj));
  72. }
  73. else{
  74. $output.= '<div class="alert alert-danger alert-dismissible fade in">';
  75. $output.= '<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>';
  76. $output.= '<i class="fas fa-exclamation-circle"></i> <strong> Error:</strong> Please Enter Password.';
  77. $output.= '</div>';
  78. return $output;
  79.  
  80. }
  81. }
  82.  
  83. else{
  84. $output.= '<div class="alert alert-danger alert-dismissible fade in">';
  85. $output.= '<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>';
  86. $output.= '<i class="fas fa-exclamation-circle"></i> <strong> Error:</strong> Please Enter Username and Password.';
  87. $output.= '</div>';
  88.  
  89. return $output;
  90.  
  91. }
  92. }
  93. }
  94.  
  95. public function reg(){
  96. if(isset($_POST['button'])){
  97. if(isset($_POST['Username']) && isset($_POST['Email']) && isset($_POST['Password'])&& isset($_POST['Fname']) && isset($_POST['Lname']) && isset($_POST['CPassword']) && $_POST['g-recaptcha-response']!=""){
  98. $secret = '6LfEL3QUAAAAAD8ddCdGkR3gdcD9UC8SX03SRZz6';
  99. $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
  100. $responseData = json_decode($verifyResponse);
  101. if($responseData->success){
  102. if(!empty($_POST['Username']) && !empty($_POST['Email']) && !empty($_POST['Password'])&& !empty($_POST['Fname']) && !empty($_POST['Lname']) && !empty($_POST['CPassword'])){
  103. if($_POST['CPassword'] == $_POST['Password']){
  104.  
  105. $re = '/\A(?=[\x20-\x7E]*?[A-Z])(?=[\x20-\x7E]*?[a-z])(?=[\x20-\x7E]*?[0-9])[\x20-\x7E]{6,}\z/';
  106.  
  107. if(preg_match($re, $_POST['Password'])) {
  108.  
  109.  
  110.  
  111.  
  112.  
  113. $errorMessage = 'ERROR: Unallowed character';
  114. $Fname = $_POST['Fname'];
  115. $Lname = $_POST['Lname'];
  116. $username = $_POST['Username'];
  117. $email = $_POST['Email'];
  118. $password = $_POST['Password'];
  119. $ecn_pass = password_hash($_POST['Password'],PASSWORD_DEFAULT,['cost' => 12]);
  120. if(strlen($password) > 8){
  121. /*
  122. function pkcs7_pad($data, $size)
  123. {
  124. $length = $size - strlen($data) % $size;
  125. return $data . str_repeat(chr($length), $length);
  126. }
  127. */
  128.  
  129.  
  130. /*$enc_pass = openssl_encrypt(
  131. pkcs7_pad($password, 16),
  132. 'AES-256-CBC',
  133. $encryption_key,
  134. 0,
  135. $iv
  136. );*/
  137. $obj->pass = $ecn_pass;
  138. $obj->email = $email;
  139. $obj->username = $username;
  140. $obj->fname = $Fname;
  141. $obj->lname = $Lname;
  142. $this->model->reg(json_encode($obj));
  143.  
  144. }
  145. else{
  146. //echo "<h1 style = 'color: red;'>Minimum password length is 8</h1>";
  147. $output2.= '<div class="alert alert-danger">';
  148. $output2.= ' <strong>Error:</strong> Minimum password length is 8.';
  149. $output2.= '</div>';
  150. return $output2;
  151. }
  152. }
  153. else{
  154. //echo "<h1 style = 'color: red;'>at least 8 characters of uppercase,lowercase and numbers</h1>";
  155. $output2.= '<div class="alert alert-danger">';
  156. $output2.= ' <strong>Error:</strong> at least 8 characters of uppercase,lowercase and numbers.';
  157. $output2.= '</div>';
  158. return $output2;
  159. }
  160.  
  161.  
  162. }
  163. else{
  164. //echo "<h1 style = 'color: red;'>Password Confirmation Incorrect</h1>";
  165. $output2.= '<div class="alert alert-danger">';
  166. $output2.= ' <strong>Error:</strong> Password Confirmation Incorrect.';
  167. $output2.= '</div>';
  168. return $output2;
  169. }
  170. }
  171. else{
  172. //echo "<h1 style = 'color: red;'>PLEASE FILL IN ALL FIELDS</h1>";
  173. $output2.='<div class="alert alert-danger">';
  174. $output2.=' <strong>Error:</strong> PLEASE FILL IN ALL FIELDS.';
  175. $output2.='</div>';
  176. return $output2;
  177. }
  178. }
  179. }
  180. else{
  181. $output2.= '<div class="alert alert-danger alert-dismissible fade in">';
  182. $output2.= '<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>';
  183. $output2.= ' <strong>Error:</strong> Please Complete Captcha.';
  184. $output2.= '</div>';
  185. return $output2;
  186. }
  187.  
  188. }
  189. else{
  190. echo "";
  191. }
  192. }
  193.  
  194. public function pagination(){
  195. $page = isset($_GET['page2']) ? (int)$_GET['page2'] : 1;
  196. $perPage = isset($_GET['per-page']) && $_GET['per-page'] <= 6 ? (int)$_GET['per-page'] : 6;
  197. $nextpage = (int)$_GET['page2'] + 1;
  198. $lastpage = (int)$_GET['page2'] - 1;
  199. $start = ($page > 1) ? ($page * $perPage) - $perPage : 0;
  200. $obj = new \stdClass();
  201. $obj->start = $start;
  202. $obj->pp = $perPage;
  203. $getpages = $this->model->getPages(json_encode($obj));
  204. $pages = json_decode($getpages);
  205. if((int)$_GET['page2'] <= 1){
  206. $output .= "<li class='page-item'><a class='page-link' href=#>Previous</a></li>";
  207. }
  208. else{
  209. $output .= "<li class='page-item'><a class='page-link' href=?page2=".$lastpage."&per-page=".$perPage.">Previous</a></li>";
  210. }
  211. for($x = 1; $x <= $pages; $x++ ){
  212. if((int)$_GET['page2'] == $x){
  213. $test = 'active';
  214. $css = "<span class='sr-only'>(current)</span>";
  215. }
  216. else{
  217. $test = 'page-item';
  218. $css = "";
  219.  
  220. }
  221.  
  222. $output .= "<li class=".$test."><a class='page-link' href=?page2=".$x."&per-page=".$perPage.">".$x."".$css."</a></li>";
  223. }
  224. if((int)$_GET['page2'] >= $pages){
  225. $output .= "<li class='page-item'><a class='page-link' href=#>Next</a></li>";
  226. }
  227. else{
  228. $output .= "<li class='page-item'><a class='page-link' href=?page2=".$nextpage."&per-page=".$perPage.">Next</a></li>";
  229. }
  230. return $output;
  231. }
  232.  
  233. public function logout(){
  234. session_destroy();
  235. header("Location: https://mayar.abertay.ac.uk/~1800854/MVC/index.php?page=views&model=getArticles");
  236. die();
  237. }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement