Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
duythien committed Jan 21, 2014
0 parents commit b56ea18
Show file tree
Hide file tree
Showing 55 changed files with 22,194 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

How to create blog use mongodb and php
66 changes: 66 additions & 0 deletions admin/auth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

<?php
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>';
exit;
};
};
};
}

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';
};
}

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;
}


?>
2 changes: 2 additions & 0 deletions admin/delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
$id = $_GET['id'];
79 changes: 79 additions & 0 deletions admin/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
require_once 'auth.php';

require '../app.php';
use \Michelf\MarkdownExtra,
\Michelf\Markdown;
require_once '../vendor/markdown/Markdown.inc.php';


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

if (isset($url_action)) {
if (is_callable($url_action)) {
call_user_func($url_action);
} else {
echo 'Function does not exist, request terminated';
}
}

if (is_array($_SESSION) &&$_SESSION['username'] ==UserAuth) {
$data = array();

if (isset($_GET['status'])&& $_GET['status']=='create') {
if ($_SERVER['REQUEST_METHOD'] === 'POST' ) {
$article = array();
$article['title'] = $_POST['title'];
$article['content'] = Markdown::defaultTransform($_POST['content']);

$article['saved_at'] = new MongoDate();

if ( empty($article['title']) || empty($article['content']) ) {
$data['status'] = 'Please fill out both inputs.';
}else {
// then create a new row in the table
$conn->posts->insert($article);
$data['status'] = 'Row has successfully been inserted.';
}
}
view('admin/create', $data);
}elseif(isset($_GET['status'])&& $_GET['status']=='edit'){
$id = $_REQUEST['id'];
$data['status'] =null;

if ($_SERVER['REQUEST_METHOD'] === 'POST' ) {
$article = array();
$article['title'] = $_POST['title'];
$article['content'] = Markdown::defaultTransform($_POST['content']);
$article['saved_at'] = new MongoDate();

if ( empty($article['title']) || empty($article['content']) ) {
$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);
$data['status'] = 'Row has successfully been update.';
}
}
//var_dump(Blog\Functions\getById($id,'posts',$conn));

view('admin/edit',array(
'article' => Blog\Functions\getById($id,'posts',$conn),
'status' => $data['status']
));

}
else{
$currentPage = (isset($_GET['page'])) ? (int) $_GET['page'] : 1; //current page number
$data = Blog\Functions\get($currentPage,'posts',$conn);


view('admin/dashboard',array(
'currentPage' => $data[0],
'totalPages' => $data[1],
'cursor' => $data[2],

));

}
}
10 changes: 10 additions & 0 deletions app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
include 'config.php';
include 'layout.php';
include 'functions.php';

use Blog\Functions;

// Connect to the db
$conn = Functions\connect($config);
if ( !$conn ) die('Problem connecting to the database see file config.php.');
28 changes: 28 additions & 0 deletions comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

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

if (isset($post['comments'])) {
$comments = $post['comments'];
}else{
$comments = array();
}

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

array_push($comments, $comment);

$postCollection->update(
array('_id' => new MongoId($id)),
array('$set' => array('comments' => $comments)));
header('Location: single.php?id='.$id);
23 changes: 23 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
define('URL', 'http://duythien.dev/sitepoint/blog-mongodb');
define('UserAuth', 'duythien');
define('PasswordAuth', 'duythien');

$config = array(
'username' => 'duythien',
'password' => 'qazwsx2013@',
'dbname' => 'blog',
//'cn' => sprintf('mongodb://%s:%d/%s', $hosts, $port,$database),
'connection_string'=> sprintf('mongodb://%s:%d/%s','127.0.0.1','27017','blog')
);
/*define('URL', 'http://phpmongodb-duythien.rhcloud.com');
define('UserAuth', 'duythien');
define('PasswordAuth', 'duythien');
$config = array(
'username' => 'admin',
'password' => 'MHZKADzU6ylD',
'dbname' => 'phpmongodb',
//'cn' => sprintf('mongodb://%s:%d/%s', $hosts, $port,$database),
'connection_string'=> sprintf('mongodb://%s:%d/%s','127.5.62.2','27017','phpmongodb')
);*/
73 changes: 73 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php namespace Blog\Functions;


function connect($config)
{
try{
if ( !class_exists('Mongo')){
echo ("The MongoDB PECL extension has not been installed or enabled");
return false;
}
$connection= new \Mongo($config['connection_string'],array('username'=>$config['username'],'password'=>$config['password']));
$dbname = $connection->selectDB($config['dbname']);
return $dbname;
}catch(Exception $e) {
return false;
}
}


/*function get($collection,$dbname)
{
$table = $dbname->selectCollection($collection);
//$table = $dbname->$collection;
$stmt = $table->find();
return ($stmt->count() > 0) ? $stmt : false;
}*/

function getById($id,$collection,$dbname)
{
$table = $dbname->selectCollection($collection);
$cursor = $table->find(array('_id' => new \MongoId($id)));
$article = $cursor->getNext();

if (!$article ){
return $oh = "Sory article not exits";
}
return $article;
}
function get($page,$collection,$dbname)
{

$currentPage = $page;
$articlesPerPage = 5;

//number of article to skip from beginning
$skip = ($currentPage - 1) * $articlesPerPage;

$table = $dbname->selectCollection($collection);

$cursor = $table->find();
//total number of articles in database
$totalArticles = $cursor->count();
//total number of pages to display
$totalPages = (int) ceil($totalArticles / $articlesPerPage);

$cursor->sort(array('saved_at' => -1))->skip($skip)->limit($articlesPerPage);
//$cursor = iterator_to_array($cursor);
$data=array($currentPage,$totalPages,$cursor);

return $data;
}
function commentId($id,$collection,$dbname)
{
$table = $dbname->selectCollection($collection);
$cursor = $table->findOne(array('_id' => new \MongoId($id)));
$article = $cursor->getNext();

if (!$article ){
return $oh = "Sory article not exits";
}
return $article;
}
32 changes: 32 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

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

try {

// Fetch all the posts
/*$posts = Functions\get('posts', $conn);
view('index', array(
'cursor' => $posts,
'name' => 'Duy Thien'
));*/
$currentPage = (isset($_GET['page'])) ? (int) $_GET['page'] : 1; //current page number
$data = Functions\get($currentPage,'posts',$conn);


view('index',array(
'currentPage' => $data[0],
'totalPages' => $data[1],
'cursor' => $data[2],
'name' => 'Duy Thien'

));


} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
28 changes: 28 additions & 0 deletions layout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

function view($path, $data = null)
{
if ( $data ) {
extract($data);
}

$path .= '.view.php';

include "views/layout.php";
}
/*function view_admin($path,$data=null){
if ($data ) {
$currentPage = $data[0];
$totalPages =$data[1];
$cursor = $data[2];
extract($data);
}
///$cursor =$data;
$path .= '.view.php';
include "views/layout.php";
}
*/
Loading

0 comments on commit b56ea18

Please sign in to comment.