-
Notifications
You must be signed in to change notification settings - Fork 0
/
updatePosts.php
76 lines (76 loc) · 2.12 KB
/
updatePosts.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
<?php
session_start();
$path = 'phpseclib';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
include_once('Crypt/RSA.php');
header('Content-type: application/json');
class Post {
public $user;
public $title;
public $msg;
public $tim;
public $pid;
}
function check(){
$i;
$array = array();
$str = file_get_contents('posts.txt');
$array = json_decode($str);
for($i=0;$i<sizeof($array);$i++){
if($array[$i]->user == $_SESSION["login"] && $array[$i]->pid == $_POST['postID']){
return true;
}
}
return false;
}
$array = array();
$filename = 'posts.txt';
$pst = new Post();
if($_POST["postID"] == -1 ){
$pst->user = $_SESSION["login"];
$pst->title = $_POST["postTitle"];
$pst->msg = $_POST["postDesc"];
$pst->tim = time();
//$myfile = fopen($filename,"w") or die ("Unable to open file!");
if (!file_exists($filename) || ($bar = file_get_contents($filename))=== '') {
$pst->pid=0;
array_push($array,$pst);
}
else{
// ($bar = file_get_contents($filename));
$array = json_decode($bar);
$pst->pid = sizeof($array);
array_push($array,$pst);
}
$str = json_encode($array);
file_put_contents($filename, $str);
// header('Location: viewPosts.php');
echo json_encode($array);
}
else if($_POST["postID"] == -2){
$bar = file_get_contents($filename);
echo $bar;
}
else{
$bar = file_get_contents($filename);
$array = json_decode($bar);
if($_SESSION["login"]=== 'admin'){
array_splice($array,$_POST["postID"],1);
$str = json_encode($array);
file_put_contents($filename, $str);
}else{
$pst->user = $_SESSION["login"];
$pst->title = $array[$_POST["postID"]]->title;
$pst->msg = $_POST["postDesc"];
$pst->tim = time();
if(check()){
array_splice($array,$_POST["postID"],1);
$pst->pid = sizeof($array)-1;
array_push($array,$pst);
$str = json_encode($array);
file_put_contents($filename, $str);
}
}
echo $str;
}
?>