So, I have coded a PHP script that will make it able for the users to edit their Post, or comments on a site that runs SociaEngine 4.
So, Is there anything that could get inproved/fixed on this?
<?php
session_start();
$Mysql_Hostname = "192.168.1.110";
$Mysql_Username = "*user";
$Mysql_Password = "************";
$Mysql_Database = "Socialengine";
$Sql = new mysqli($Mysql_Hostname, $Mysql_Username, $Mysql_Password, $Mysql_Database);
if ($Sql->connect_error){die($Sql->connect_error);}
$ID = session_id(); // Get session cookie data / id
$GetSession = $Sql->query("SELECT * FROM `engine4_core_session` WHERE id = '".$ID."'"); // Get the session data for the user.
$Session = $GetSession->fetch_array(); // blah blah...
if($Session['user_id'] == NULL) { die("You need to be logged inn to use this"); } // Check if the user is logged inn
$GetUser = $Sql->query("SELECT * FROM `engine4_users` WHERE `user_id` = '".$Session['user_id']."'"); // Get user data
$User = $GetUser->fetch_array(); // blah...
echo "<p>You are currenty logged inn as: ".$User['displayname']."</p>"; // Give a message that they are logged in
if($_GET['mode'] == "done") { // If they have edited, Then close the window
echo '<script type="text/javascript"> window.close(); </script>';
}
if($_GET['mode'] == "edit") { // If they want to edit a comment...
$CID = $_GET['id']; // Get comment id
$GetComment = $Sql->query("SELECT * FROM `engine4_activity_comments` WHERE `poster_id` = '".$Session['user_id']."' AND `comment_id` = '".$Sql->real_escape_string($CID)."'"); // Get the comment from Mysql with User restriction...
$Comment = $GetComment->fetch_array(); // blah
if($GetComment->num_rows == 0) { die("Not authorized to edit"); } // If the comment wasn't theirs, Then die...
echo <<<HTML
<form action="Session.php?mode=done&type=comment" method="post">
<input type="hidden" name="ID" value="{$Comment['comment_id']}">
<textarea rows="3" cols="45" name="Comment">{$Comment['body']}</textarea>
<p><input type="submit" value="Edit comment"></p>
</form>
HTML;
// Render the edit form
exit; // Stop processing
}
if($_GET['mode'] == "post") { // If they want to edit a post...
$CID = $_GET['id']; // Get post id
$GetPost = $Sql->query("SELECT * FROM `engine4_activity_actions` WHERE `subject_id` = '".$Session['user_id']."' AND `action_id` = '".$Sql->real_escape_string($CID)."'"); // Same as comments, Get with post id and user id
$Post = $GetPost->fetch_array(); // blah
if($GetPost->num_rows == 0) { die("Not authorized to edit"); } // No! Don't try to edit others post's...
echo <<<HTML
<form action="Session.php?mode=done&type=post" method="post">
<input type="hidden" name="ID" value="{$Post['action_id']}">
<textarea rows="3" cols="45" name="Comment">{$Post['body']}</textarea>
<p><input type="submit" value="Edit post"></p>
</form>
HTML;
// Render the post edit form
exit; // and stop
}
if(isset($_POST['Comment'])) { // If the form has been submitted.
if($_GET['type'] == "comment") { // Check if it's a comment or a post
$Verify = $Sql->query("SELECT * FROM `engine4_activity_comments` WHERE `poster_id` = '".$Session['user_id']."' AND `comment_id` = '".$Sql->real_escape_string($_POST['ID'])."'"); // Verify if it's theirs...
if($Verify->num_rows == 1) { // If it's theirs, Contiune
$Update = $Sql->query("UPDATE `Manehatten`.`engine4_activity_comments` SET `body` = '".$Sql->real_escape_string($_POST['Comment'])."'
WHERE `poster_id` = '".$Session['user_id']."' AND `comment_id` = '".$Sql->real_escape_string($_POST['ID'])."'"); // Update the data in mysql
}
}
elseif($_GET['type'] == "post") { // Check if it's a comment or a post
$Verify = $Sql->query("SELECT * FROM `engine4_activity_actions` WHERE `subject_id` = '".$Session['user_id']."' AND `action_id` = '".$Sql->real_escape_string($_POST['ID'])."'"); // Verify if it's theirs...
if($Verify->num_rows == 1) { // If it's theirs, Contiune
$Update = $Sql->query("UPDATE `Manehatten`.`engine4_activity_actions` SET `body` = '".$Sql->real_escape_string($_POST['Comment'])."'
WHERE `subject_id` = '".$Session['user_id']."' AND `action_id` = '".$Sql->real_escape_string($_POST['ID'])."'"); // Update the data..
}
}
}
// This script is only for test, or comment id getting /IGNORE THIS/
echo "<p>Displaying Comments with Limit of 30 </p>";
echo "<hr>";
$GetComments = $Sql->query("SELECT * FROM `engine4_activity_comments` WHERE `poster_id` = '".$Session['user_id']."' ORDER BY `engine4_activity_comments`.`creation_date` DESC LIMIT 0, 30");
while($Data = $GetComments->fetch_assoc()){
echo "<p>".$Data['body'].' | ID: <a href="Session.php?mode=edit&id='.$Data['comment_id'].'">'.$Data['comment_id']."</a> <- Click to edit</p>";
}
I have added comments for what lines do what.
I am just a bit unsure if they can use this and alter their session cookie to gather a valid user cookie.
The script works fine, Just that if there was something that could be improved.