Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PHP - Thread Class and Validation Add validation that checks: $title and $messag

ID: 3780725 • Letter: P

Question

PHP - Thread Class and Validation

Add validation that checks:

$title and $message both have a minimum length of 3.

title has a maximum length of 32 characters.

$message has a maximum length of 52 characters.

If any of the three validation rules fail, print a status of 432 and an error message of what is wrong with the user input. The JSON response should look like:

If the user submitted a successful post, save the record using the provided Thread class. Set the timeposted set to when the item was submitted. Make the thread visible as soon as its submitted. Print a status code of 200. The JSON response should look like:

Add your code to newthread.php. Test your script by running:

ALSO convert all urls in the $message to HTML links and store the new result as message_html using the provided Thread class.

HINT: You'll need to modify the provided Thread class.

-------------------------------

newthread.php

------------------------

Thread.php

-------------------------------

Show changes added to newthread.php and Thread.php. Thank you

Explanation / Answer

newthread.php

<?php


require_once(__DIR__ . '/init.php');
header('Content-Type: application/json');

if ($argc != 3)
{
    throw new Exception('Invalid number of arguments provided');
}

$title = $argv[1];
$message = $argv[2];

$thread = new Thread();

$err_Short_Title = array('status' => 432, 'error' => 'title field is too short');
$err_Long_Title = array('status' => 432, 'error' => 'title field is too long');

$err_Short_Msg = array('status' => 432, 'error' => 'message field is too short');
$err_Long_Msg = array('status' => 432, 'error' => 'message field is too long');

$success = array('status' => 200);


/* Extract URLs from Message */


$url = '~(?:(https?)://([^s<]+)|(www.[^s<]+?.[^s<]+))(?<![.,:])~i';
$message_html = preg_replace($url, '<a href="$0"</a>', $message);


//If Title and Message fit Req.
   //Echo Success and Create New Thread

if (strlen($title) >= 3 && strlen($title) <= 32 && strlen($message) >= 3 && strlen($message) <= 52) {
   //title, message, isvisible, timeposted
   $thread->newThread($title, $message, $message_html, true, date('D M j G:i:s Y'));
   echo json_encode($success) . " ";
} else {
   //Check Title Length
   if (strlen($title) < 3) {
       echo json_encode($err_Short_Title) . " ";
   } else if (strlen($title) > 32) {
       echo json_encode($err_Long_Title) . " ";
   }
   //Check Message Length
   if (strlen($message) < 3) {
       echo json_encode($err_Short_Msg) . " ";
   } else if (strlen($message) > 52) {
       echo json_encode($err_Long_Msg) . " ";
   }
}

threadlist.php

<?php

require_once(__DIR__ . '/init.php');

header('Content-Type: application/json');


$thread = new Thread();
$bannedWords = 'banana apple mongoose pear duck';

// Actual implementation below

$success = array('status' => 200);
$threads = $thread->getRecentThreads(3, true);

//Reference: http://php.net/manual/en/function.explode.php
$bannedWords_array = explode (" ", $bannedWords);

//Reference: http://www.w3schools.com/php/showphp.asp?filename=demo_loop_foreach
foreach ($threads as $thread) {
   foreach ($thread as $value) {
       $newphrase = str_replace($bannedWords_array, '****', $value);
       echo json_encode($newphrase) . " ";
   }
}

//If prints everything out, post success 200.
echo json_encode($success) . " ";