This repository has been archived by the owner on Apr 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathpost.php
92 lines (91 loc) · 3.53 KB
/
post.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
define ("dir", "https://ziki.hng.tech/" );
//session_start();
include 'includes/config.php';
require_once 'config.php';
require_once 'functions/Post.php';
extract($_SESSION);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//$title = isset($_POST['title']) ? trim($_POST['title']) : null;
$body = isset($_POST['body']) ? trim($_POST['body']) : null;
//$file = $_FILES['image'];
// $name ="Ogundiji Bolade Adio";
// $email = "kamponistullar@gmail.com";
$user = $email;
$db_json = file_get_contents("posts.json");
$newPost = new Post();
$newPost->setUserId($user);
$newPost->setStoryBody($body);
//$newPost->setStoryTitle($title);
//$newPost->setStoryImage($target_file);
if ($newPost->savePost($db_json, $name, $img)) {
$response = array('error' => false, 'message' => 'post published successfully');
} else {
$response = array('error' => true, 'message' => 'error occured while posting');
}
/*
$new_file_name = date('dmYHis').str_replace(" ", "", basename($_FILES['image']['name']));
$image_type = strtolower(pathinfo($new_file_name, PATHINFO_EXTENSION));
if ($image_type != "jpg" && $image_type != "png" && $image_type != "jpeg") {
$response['error'] = true;
$response['message'] = 'Please make sure you uploading your image';
}
else{
$target_file = SITE_ROOT.'/uploads/'.$new_file_name;
if ($file['error'] === 0) {
$upload = move_uploaded_file($file['tmp_name'], $target_file);
if ($upload) {
$db_json = file_get_contents("posts.json");
$newPost = new Post();
$newPost->setUserId($user);
$newPost->setStoryBody($body);
$newPost->setStoryTitle($title);
$newPost->setStoryImage($target_file);
if ($newPost->savePost($db_json)) {
$response = array('error' => false, 'message' => 'post published successfully');
} else {
$response = array('error' => true, 'message' => 'error occured while posting');
}
}
else{
$response['error'] = true;
$response['message'] = 'Error while uploading image';
}
}
else{
$response['error'] = true;
$response['message'] = 'Error, please select an image';
}
}*/
//header("Location: https://ziki.hng.tech/timeline.php");
header('Location: http://localhost:8000/timeline.php');
}
else {
$data = file_get_contents("posts.json");
$posts = json_decode($data, true);
$getAllPosts = Post::fetchAllPosts($posts);
$posts = array();
if (!empty($getAllPosts)) {
foreach ($getAllPosts as $blog) {
$postId = $blog->getId();
//$postTitle = $blog->getStoryTitle();
$postBody = $blog->getStoryBody();
//$postPic = $blog->getStoryImage();
$markdownLink = $blog->getMarkdownUrl();
$authPic = $blog->getAuthorPic();
$postTimestamp = $blog->getTimePosted();
$post['id'] = $postId;
$post['author_image'] = $authPic;
$post['markdown_url'] = $markdownLink;
$post['post_timestamp'] = date("jS F, Y", strtotime($postTimestamp));
array_push($posts, $post);
}
$result['error'] = false;
$result['result'] = $posts;
}
else{
$result['error'] = true;
$result['message'] = 'internal server error';
}
echo(json_encode($result));
}