Skip to content

Commit b56ea18

Browse files
committed
first commit
0 parents  commit b56ea18

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+22194
-0
lines changed

README

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
How to create blog use mongodb and php

admin/auth.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
2+
<?php
3+
session_start();
4+
5+
6+
function logIn() {
7+
if (!isset($_SESSION['username'])) {
8+
if (!isset($_SESSION['login'])) {
9+
$_SESSION['login'] = TRUE;
10+
header('WWW-Authenticate: Basic realm="My Realm"');
11+
header('HTTP/1.0 401 Unauthorized');
12+
echo 'You must enter a valid login and password';
13+
echo '<p><a href="?action=logOut">Try again</a></p>';
14+
exit;
15+
} else {
16+
$user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
17+
$password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
18+
$result = authenticate($user, $password);
19+
if ($result == 0) {
20+
$_SESSION['username'] = $user;
21+
} else {
22+
session_unset($_SESSION['login']);
23+
errMes($result);
24+
echo '<p><a href="">Try again</a></p>';
25+
exit;
26+
};
27+
};
28+
};
29+
}
30+
31+
function authenticate($user, $password) {
32+
33+
34+
if (($user == UserAuth)&&($password == PasswordAuth)){
35+
return 0;
36+
}else{
37+
return 1;
38+
}
39+
}
40+
41+
function errMes($errno) {
42+
switch ($errno) {
43+
case 0:
44+
break;
45+
case 1:
46+
echo 'The username or password you entered is incorrect';
47+
break;
48+
default:
49+
echo 'Unknown error';
50+
};
51+
}
52+
53+
function logOut() {
54+
session_destroy();
55+
if (isset($_SESSION['username'])) {
56+
session_unset($_SESSION['username']);
57+
echo "You've successfully logged out<br>";
58+
} else {
59+
header("Location: ?action=logIn", TRUE, 301);
60+
};
61+
if (isset($_SESSION['login'])) { session_unset($_SESSION['login']); };
62+
exit;
63+
}
64+
65+
66+
?>

admin/delete.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
$id = $_GET['id'];

admin/index.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
require_once 'auth.php';
3+
4+
require '../app.php';
5+
use \Michelf\MarkdownExtra,
6+
\Michelf\Markdown;
7+
require_once '../vendor/markdown/Markdown.inc.php';
8+
9+
10+
$url_action = (empty($_REQUEST['action'])) ? 'logIn' : $_REQUEST['action'];
11+
12+
if (isset($url_action)) {
13+
if (is_callable($url_action)) {
14+
call_user_func($url_action);
15+
} else {
16+
echo 'Function does not exist, request terminated';
17+
}
18+
}
19+
20+
if (is_array($_SESSION) &&$_SESSION['username'] ==UserAuth) {
21+
$data = array();
22+
23+
if (isset($_GET['status'])&& $_GET['status']=='create') {
24+
if ($_SERVER['REQUEST_METHOD'] === 'POST' ) {
25+
$article = array();
26+
$article['title'] = $_POST['title'];
27+
$article['content'] = Markdown::defaultTransform($_POST['content']);
28+
29+
$article['saved_at'] = new MongoDate();
30+
31+
if ( empty($article['title']) || empty($article['content']) ) {
32+
$data['status'] = 'Please fill out both inputs.';
33+
}else {
34+
// then create a new row in the table
35+
$conn->posts->insert($article);
36+
$data['status'] = 'Row has successfully been inserted.';
37+
}
38+
}
39+
view('admin/create', $data);
40+
}elseif(isset($_GET['status'])&& $_GET['status']=='edit'){
41+
$id = $_REQUEST['id'];
42+
$data['status'] =null;
43+
44+
if ($_SERVER['REQUEST_METHOD'] === 'POST' ) {
45+
$article = array();
46+
$article['title'] = $_POST['title'];
47+
$article['content'] = Markdown::defaultTransform($_POST['content']);
48+
$article['saved_at'] = new MongoDate();
49+
50+
if ( empty($article['title']) || empty($article['content']) ) {
51+
$data['status'] = 'Please fill out both inputs.';
52+
}else {
53+
// then create a new row in the table
54+
$conn->posts->update(array('_id' => new MongoId($id)), $article);
55+
$data['status'] = 'Row has successfully been update.';
56+
}
57+
}
58+
//var_dump(Blog\Functions\getById($id,'posts',$conn));
59+
60+
view('admin/edit',array(
61+
'article' => Blog\Functions\getById($id,'posts',$conn),
62+
'status' => $data['status']
63+
));
64+
65+
}
66+
else{
67+
$currentPage = (isset($_GET['page'])) ? (int) $_GET['page'] : 1; //current page number
68+
$data = Blog\Functions\get($currentPage,'posts',$conn);
69+
70+
71+
view('admin/dashboard',array(
72+
'currentPage' => $data[0],
73+
'totalPages' => $data[1],
74+
'cursor' => $data[2],
75+
76+
));
77+
78+
}
79+
}

app.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
include 'config.php';
3+
include 'layout.php';
4+
include 'functions.php';
5+
6+
use Blog\Functions;
7+
8+
// Connect to the db
9+
$conn = Functions\connect($config);
10+
if ( !$conn ) die('Problem connecting to the database see file config.php.');

comment.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
require 'app.php';
4+
use Blog\Functions;
5+
6+
$id = $_POST['article_id'];
7+
$postCollection = $conn->posts;
8+
$post = $postCollection->findOne(array('_id' => new MongoId($id)));
9+
10+
if (isset($post['comments'])) {
11+
$comments = $post['comments'];
12+
}else{
13+
$comments = array();
14+
}
15+
16+
$comment = array(
17+
'name' => $_POST['fName'],
18+
'email' => $_POST['fEmail'],
19+
'comment' => $_POST['fComment'],
20+
'posted_at' => new MongoDate()
21+
);
22+
23+
array_push($comments, $comment);
24+
25+
$postCollection->update(
26+
array('_id' => new MongoId($id)),
27+
array('$set' => array('comments' => $comments)));
28+
header('Location: single.php?id='.$id);

config.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
define('URL', 'http://duythien.dev/sitepoint/blog-mongodb');
3+
define('UserAuth', 'duythien');
4+
define('PasswordAuth', 'duythien');
5+
6+
$config = array(
7+
'username' => 'duythien',
8+
'password' => 'qazwsx2013@',
9+
'dbname' => 'blog',
10+
//'cn' => sprintf('mongodb://%s:%d/%s', $hosts, $port,$database),
11+
'connection_string'=> sprintf('mongodb://%s:%d/%s','127.0.0.1','27017','blog')
12+
);
13+
/*define('URL', 'http://phpmongodb-duythien.rhcloud.com');
14+
define('UserAuth', 'duythien');
15+
define('PasswordAuth', 'duythien');
16+
17+
$config = array(
18+
'username' => 'admin',
19+
'password' => 'MHZKADzU6ylD',
20+
'dbname' => 'phpmongodb',
21+
//'cn' => sprintf('mongodb://%s:%d/%s', $hosts, $port,$database),
22+
'connection_string'=> sprintf('mongodb://%s:%d/%s','127.5.62.2','27017','phpmongodb')
23+
);*/

functions.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php namespace Blog\Functions;
2+
3+
4+
function connect($config)
5+
{
6+
try{
7+
if ( !class_exists('Mongo')){
8+
echo ("The MongoDB PECL extension has not been installed or enabled");
9+
return false;
10+
}
11+
$connection= new \Mongo($config['connection_string'],array('username'=>$config['username'],'password'=>$config['password']));
12+
$dbname = $connection->selectDB($config['dbname']);
13+
return $dbname;
14+
}catch(Exception $e) {
15+
return false;
16+
}
17+
}
18+
19+
20+
/*function get($collection,$dbname)
21+
{
22+
$table = $dbname->selectCollection($collection);
23+
//$table = $dbname->$collection;
24+
$stmt = $table->find();
25+
26+
return ($stmt->count() > 0) ? $stmt : false;
27+
}*/
28+
29+
function getById($id,$collection,$dbname)
30+
{
31+
$table = $dbname->selectCollection($collection);
32+
$cursor = $table->find(array('_id' => new \MongoId($id)));
33+
$article = $cursor->getNext();
34+
35+
if (!$article ){
36+
return $oh = "Sory article not exits";
37+
}
38+
return $article;
39+
}
40+
function get($page,$collection,$dbname)
41+
{
42+
43+
$currentPage = $page;
44+
$articlesPerPage = 5;
45+
46+
//number of article to skip from beginning
47+
$skip = ($currentPage - 1) * $articlesPerPage;
48+
49+
$table = $dbname->selectCollection($collection);
50+
51+
$cursor = $table->find();
52+
//total number of articles in database
53+
$totalArticles = $cursor->count();
54+
//total number of pages to display
55+
$totalPages = (int) ceil($totalArticles / $articlesPerPage);
56+
57+
$cursor->sort(array('saved_at' => -1))->skip($skip)->limit($articlesPerPage);
58+
//$cursor = iterator_to_array($cursor);
59+
$data=array($currentPage,$totalPages,$cursor);
60+
61+
return $data;
62+
}
63+
function commentId($id,$collection,$dbname)
64+
{
65+
$table = $dbname->selectCollection($collection);
66+
$cursor = $table->findOne(array('_id' => new \MongoId($id)));
67+
$article = $cursor->getNext();
68+
69+
if (!$article ){
70+
return $oh = "Sory article not exits";
71+
}
72+
return $article;
73+
}

index.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
error_reporting(E_ALL);
3+
ini_set('display_errors', 1);
4+
5+
require 'app.php';
6+
use Blog\Functions;
7+
8+
try {
9+
10+
// Fetch all the posts
11+
/*$posts = Functions\get('posts', $conn);
12+
13+
view('index', array(
14+
'cursor' => $posts,
15+
'name' => 'Duy Thien'
16+
));*/
17+
$currentPage = (isset($_GET['page'])) ? (int) $_GET['page'] : 1; //current page number
18+
$data = Functions\get($currentPage,'posts',$conn);
19+
20+
21+
view('index',array(
22+
'currentPage' => $data[0],
23+
'totalPages' => $data[1],
24+
'cursor' => $data[2],
25+
'name' => 'Duy Thien'
26+
27+
));
28+
29+
30+
} catch (Exception $e) {
31+
echo 'Caught exception: ', $e->getMessage(), "\n";
32+
}

layout.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
function view($path, $data = null)
4+
{
5+
if ( $data ) {
6+
extract($data);
7+
}
8+
9+
$path .= '.view.php';
10+
11+
include "views/layout.php";
12+
}
13+
/*function view_admin($path,$data=null){
14+
if ($data ) {
15+
$currentPage = $data[0];
16+
$totalPages =$data[1];
17+
$cursor = $data[2];
18+
extract($data);
19+
}
20+
21+
22+
///$cursor =$data;
23+
$path .= '.view.php';
24+
25+
include "views/layout.php";
26+
27+
}
28+
*/

0 commit comments

Comments
 (0)