-
Notifications
You must be signed in to change notification settings - Fork 3
/
newcomment.php
175 lines (157 loc) · 6.19 KB
/
newcomment.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
session_start();
//Set default values for head & load it
$title = "Create Post | SocialDomayn";
$stylesheet = "jodel.css";
include 'functions/header.php';
//Load all requred functions & config
require 'functions/apicalls.php';
$config = require('config.php');
require 'functions/jodelmeta.php';
require 'functions/class.upload.php';
$apiroot = $config->apiUrl;
$uploaddir = $config->image_upload_dir;
//get session info & post to show comments from
$userid = $_SESSION['userid'];
$post = $_GET['comment'];
//get color of post
$colorOfPost = getColorOfPost($post);
$colid = $colorOfPost->colid;
$colorname = $colorOfPost->name;
$colorhex = $colorOfPost->hex;
//user is not logged in
if(!isset($_SESSION['userid'])) {
die('You need to <a href="login.php">login</a> first');
}
//no comment is selected
if(!isset($_GET['comment'])){
die('You need to select a post');
}
//Get userdata
$callurl = $apiroot . "jodlers?transform=1&filter=jodlerID,eq," . $userid;
$userjson = getCall($callurl);
$user = json_decode($userjson, true);
foreach($user['jodlers'] as $jodler){
//get karma and account state
$karma = $jodler['karma'];
$accstate = $jodler['account_state'];
}
//set user data in session values
$_SESSION['karma'] = $karma;
$_SESSION['acctype'] = $accstate;
//user wants to post a comment
if(isset($_GET['post'])){
//get ID of post to post comment to
$jodel = $_GET['comment'];
//encode special chars to avoid injection
$comment = htmlspecialchars($_POST['comment'], ENT_QUOTES);
$comment = trim(preg_replace('/\s\s+/', ' ', $comment));
//set color as local value
$color = $_POST['color'];
$colorhex = $_POST['colhex'];
if(isset($_FILES["imageFile"]) && $_FILES['imageFile']['name'] != ""){
$epoch = time();
$filename = $epoch . "-" . $_FILES['imageFile']['name'];
$withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename);
//encode is done by upload class
$comment = $_POST['comment'];
$handle = new upload($_FILES['imageFile']);
if ($handle->uploaded) {
$handle->file_new_name_body = $withoutExt;
$handle->image_resize = true;
$handle->image_y = 300;
$handle->file_safe_name = true;
$handle->allowed = array('image/*');
$handle->image_text = $comment;
$handle->image_text_background = $colorhex;
$handle->image_text_x = 1;
$handle->image_text_y = rand(1, 299);
$handle->image_ratio_x = true;
//$handle->file_auto_rename = true;
$handle->process($uploaddir);
if ($handle->processed) {
echo 'image resized';
$handle->clean();
} else {
echo 'error : ' . $handle->error;
}
}
//save image location to DB
$callurl = $apiroot . "images";
$postfields = "{\n \"path\": \"$filename\" \n}";
$imageID = postCall($callurl, $postfields);
}
//get data from original post
$callurl = $apiroot . "jodels?transform=1&filter=jodelID,eq," . $jodel;
$orgpostjson = getCall($callurl);
$orgpost = json_decode($orgpostjson, true);
foreach($orgpost['jodels'] as $theop){
//get number of comments of original post
$comments_cnt = $theop['comments_cnt'];
$author = $theop['jodlerIDFK'];
$score = $theop['score'];
}
//incerase number of comments of OP
$comments_cnt++;
$score = $score + $config->postmeta['get_comment'];
//insert new comment in DB, $postfields as JSON with all data
if($imageID !== null){
$postfields = "{\n\t\"jodlerIDFK\": \"$userid\",\n\t\"colorIDFK\": \"$color\",\n\t\"jodelIDFK\": \"$jodel\",\n\t\"imageIDFK\": \"$imageID\",\n\t\"comment\": \"$comment\"\n\n}";
} else {
$postfields = "{\n\t\"jodlerIDFK\": \"$userid\",\n\t\"colorIDFK\": \"$color\",\n\t\"jodelIDFK\": \"$jodel\",\n\t\"comment\": \"$comment\"\n\n}";
}
$callurl = $apiroot . "comments";
$posted = postCall($callurl, $postfields);
//update comment count of OP in DB
$callurl = $apiroot . "jodels/" . $jodel;
$postfields = "{\n\t\"comments_cnt\": \"$comments_cnt\",\n\t\"score\": \"$score\"\n\n}";
$cmntupdated = putCall($callurl, $postfields);
//update the authors karma for creating a comment
$karma = $karma + $config->karma_calc['post_comment'];
$postfields = "{\n \n \"karma\": \"$karma\"\n\n}";
$callurl = $apiroot . "jodlers/" . $userid;
$karmaupdated = putCall($callurl, $postfields);
//get info about the author of the OP
$callurl = $apiroot . "jodlers?transform=1&filter=jodlerID,eq," . $author;
$authorjson = getCall($callurl);
$authorarray = json_decode($authorjson, true);
foreach($authorarray['jodlers'] as $user){
$karmaOfUser = $user['karma'];
}
//incerase karma of author
$karmaOfUser = $karmaOfUser + $config->karma_calc['get_comment'];
$postfields = "{\n \n \"karma\": \"$karmaOfUser\"\n\n}";
$callurl = $apiroot . "jodlers/" . $author;
$authorkarmaupdated = putCall($callurl, $postfields);
//redirect to post overview
header('Location: ' . $config->baseUrl . 'comments.php?showcomment=' . $jodel . '#' . $posted);
}
?>
<div id="top"></div>
<ul class="nav justify-content-center">
<li class="nav-item">
<a class="nav-link" href="comments.php?showcomment=<?php echo $post;?>"><i class="fa fa-chevron-left" aria-hidden="true"></i></a>
</li>
<li class="nav-item">
<a class="nav-link" href="javascript:window.location.reload();"><i class="fa fa-refresh" aria-hidden="true"></i></a>
</li>
<li class="nav-item">
<a class="nav-link" href="user.php"><i class="fa fa-user" aria-hidden="true"></i><?php echo $_SESSION['karma'];?></a>
</li>
</ul>
<div class="test"></div>
<!-- end main menu -->
<form action="?post=1&comment=<?php echo $post;?>" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="comment">Enter your message</label>
<textarea class="form-control" rows="10" name="comment" placeholder="Your post" style="color:white;background-color:#<?php echo $colorhex;?>"></textarea>
</div>
<!-- save the color in a hidden field -->
<input type="hidden" name="color" value="<?php echo $colid;?>">
<input type="hidden" name="colhex" value="<?php echo $colorhex;?>">
<input type="file" name="imageFile" id="imageFile">
<button type="submit" class="btn btn-warning">Submit</button>
</form>
<!-- end post form -->
<?php
include 'functions/footer.php';