Skip to content

Commit

Permalink
translate procedure inot oop
Browse files Browse the repository at this point in the history
  • Loading branch information
duythien committed Jan 23, 2014
1 parent 138aeb9 commit 17bb8d8
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 214 deletions.
107 changes: 53 additions & 54 deletions admin/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,63 @@
session_start();


function logIn() {
if (!isset($_SESSION['username'])) {
if (!isset($_SESSION['login'])) {
$_SESSION['login'] = TRUE;
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'You must enter a valid login and password';
echo '<p><a href="?action=logOut">Try again</a></p>';
exit;
} else {
$user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
$password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
$result = authenticate($user, $password);
if ($result == 0) {
$_SESSION['username'] = $user;
} else {
session_unset($_SESSION['login']);
errMes($result);
echo '<p><a href="">Try again</a></p>';
class Auth{

public function logIn() {
if (!isset($_SESSION['username'])) {
if (!isset($_SESSION['login'])) {
$_SESSION['login'] = TRUE;
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'You must enter a valid login and password';
echo '<p><a href="?action=logOut">Try again</a></p>';
exit;
} else {
$user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
$password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
$result = $this->authenticate($user, $password);
if ($result == 0) {
$_SESSION['username'] = $user;
} else {
session_unset($_SESSION['login']);
$this->errMes($result);
echo '<p><a href="">Try again</a></p>';
exit;
};
};
};
};
}

function authenticate($user, $password) {

}
}

if (($user == UserAuth)&&($password == PasswordAuth)){
return 0;
}else{
return 1;
public function authenticate($user, $password) {

if (($user == UserAuth)&&($password == PasswordAuth)){
return 0;
}else{
return 1;
}
}
}

function errMes($errno) {
switch ($errno) {
case 0:
break;
case 1:
echo 'The username or password you entered is incorrect';
break;
default:
echo 'Unknown error';
};
}
public function errMes($errno) {
switch ($errno) {
case 0:
break;
case 1:
echo 'The username or password you entered is incorrect';
break;
default:
echo 'Unknown error';
};
}

function logOut() {
session_destroy();
if (isset($_SESSION['username'])) {
session_unset($_SESSION['username']);
echo "You've successfully logged out<br>";
} else {
header("Location: ?action=logIn", TRUE, 301);
};
if (isset($_SESSION['login'])) { session_unset($_SESSION['login']); };
exit;
public function logOut() {
session_destroy();
if (isset($_SESSION['username'])) {
session_unset($_SESSION['username']);
echo "You've successfully logged out<br>";
} else {
header("Location: ?action=logIn", TRUE, 301);
};
if (isset($_SESSION['login'])) { session_unset($_SESSION['login']); };
exit;
}
}


?>
25 changes: 12 additions & 13 deletions admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
require_once '../vendor/markdown/Markdown.inc.php';

use Michelf\MarkdownExtra,
Michelf\Markdown,
Blog\Functions;

Michelf\Markdown;

$url_action = (empty($_REQUEST['action'])) ? 'logIn' : $_REQUEST['action'];

if (isset($url_action)) {
if (is_callable($url_action)) {
call_user_func($url_action);
$action = new Auth;
if (is_callable(array($action,$url_action))) {
call_user_func(array($action,$url_action));
} else {
echo 'Function does not exist, request terminated';
}
Expand All @@ -36,11 +35,11 @@
$data['status'] = 'Please fill out both inputs.';
}else {
// then create a new row in the table
$conn->posts->insert($article);
$db->create('posts',$article);
$data['status'] = 'Row has successfully been inserted.';
}
}
view('admin/create', $data);
$layout->view('admin/create', $data);
break;
case 'edit':
$id = $_REQUEST['id'];
Expand All @@ -57,28 +56,28 @@
$data['status'] = 'Please fill out both inputs.';
}else {
// then create a new row in the table
$conn->posts->update(array('_id' => new MongoId($id)), $article);
$db->update($id,'posts',$article);
$data['status'] = 'Row has successfully been update.';
}
}
view('admin/edit',array(
'article' => Functions\getById($id,'posts',$conn),
$layout->view('admin/edit',array(
'article' => $db->getById($id,'posts'),
'status' => $data['status']
));
break;
case 'delete':
$id = $_GET['id'];
$status = Functions\delete($id,'posts',$conn);
$status = $db->delete($id,'posts');
if ($status ==TRUE ) {
header("Location:index");
}
break;
default:
$currentPage = (isset($_GET['page'])) ? (int) $_GET['page'] : 1; //current page number
$data = Functions\get($currentPage,'posts',$conn);
$data = $db->get($currentPage,'posts');


view('admin/dashboard',array(
$layout->view('admin/dashboard',array(
'currentPage' => $data[0],
'totalPages' => $data[1],
'cursor' => $data[2],
Expand Down
13 changes: 8 additions & 5 deletions app.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php
include 'config.php';
include 'layout.php';
include 'functions.php';
include 'db.php';

use Blog\Functions;
use Blog\DB,
Blog\Layout;

// Connect to the db
$conn = Functions\connect($config);
if ( !$conn ) die('Problem connecting to the database see file config.php.');
// Constructor to the db
$db = new DB\DB($config);

// Constructor to layout view
$layout = new Layout\Layout();
35 changes: 14 additions & 21 deletions comment.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
<?php

require 'app.php';
use Blog\Functions;

$id = $_POST['article_id'];
$postCollection = $conn->posts;
$post = $postCollection->findOne(array('_id' => new MongoId($id)));
if ($_SERVER['REQUEST_METHOD'] === 'POST' ) {

if (isset($post['comments'])) {
$comments = $post['comments'];
}else{
$comments = array();
}
$id = $_POST['article_id'];
$comment = array(
'name' => $_POST['fName'],
'email' => $_POST['fEmail'],
'comment' => $_POST['fComment'],
'posted_at' => new MongoDate()
);
$status = $db->commentId($id,'posts',$comment);

$comment = array(
'name' => $_POST['fName'],
'email' => $_POST['fEmail'],
'comment' => $_POST['fComment'],
'posted_at' => new MongoDate()
);

array_push($comments, $comment);
if ($status == TRUE) {
header('Location: single.php?id='.$id);
}


$postCollection->update(
array('_id' => new MongoId($id)),
array('$set' => array('comments' => $comments)));
header('Location: single.php?id='.$id);
}
Loading

0 comments on commit 17bb8d8

Please sign in to comment.