Files
hpr_hub/cms/update_show_json.php
T
2026-11-21 15:03:59 +01:00

366 lines
11 KiB
PHP

<?php
require "/home/hpr/php/include.php";
date_default_timezone_set('UTC');
// executed by postshow.bash
// curl --netrc-file $HOME/.netrc --verbose --request POST https://hub.hackerpublicradio.org/cms/update_show_json.php --data-ascii @post_show.json --header "Content-Type: application/json"
logextra( "Starting update_show_json.php");
//Make sure that it is a POST request.
if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0){
throw new Exception('Request method must be POST!');
}
//Make sure that the content type of the POST request has been set to application/json
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if(strcasecmp($contentType, 'application/json') != 0){
throw new Exception('Content type must be: application/json');
}
//Receive the RAW post data.
$content = trim(file_get_contents("php://input"));
//Attempt to decode the incoming RAW post data from JSON.
$decoded_json = json_decode($content, true);
//If json_decode failed, the JSON is invalid.
if(!is_array($decoded_json)){
problem( "Received content contained invalid JSON!" );
}
if ( empty($_SERVER["REMOTE_ADDR"]) ) {
problem("No REMOTE_ADDR");
}
else {
$ip = $_SERVER["REMOTE_ADDR"];
}
logextra( "We have a IP of $ip" );
// Check Episode Number
$provided_ep_num = intval( urldecode( $decoded_json["ep_num"] ) );
if ( !isset( $provided_ep_num ) ) {
problem("ep_num is not set ");
}
if ( empty( $provided_ep_num ) ) {
problem("ep_num is empty ");
}
if ( intval($provided_ep_num) >= 9999 ) {
problem("ep_num is a reserved show 9999");
}
if ( intval($provided_ep_num) === 0 ) {
problem("ep_num is 0");
}
$ep_num = intval($provided_ep_num);
// Check Episode Date
$provided_ep_date = urldecode( $decoded_json["ep_date"] );
if ( !preg_match( "/^\d{4}-\d{2}-\d{2}$/", $provided_ep_date ) ) {
problem("ep_date fails the regex match \"${provided_ep_date}\"");
}
if ( strtotime( $provided_ep_date ) === false ) {
problem("ep_date didn't convert to date \"${provided_ep_date}\"");
}
$ep_date_epoch = strtotime( $provided_ep_date );
$ep_date = $provided_ep_date;
logextra( "ep_date checkes passed: $ep_date, $ep_date_epoch" );
// Check title
$provided_title = urldecode( $decoded_json["title"] );
if ( empty($provided_title) or strlen($provided_title) > 100 ) {
problem("Title length is not OK");
}
logextra( "Title length is OK" );
$title = $provided_title;
// Check Duration
$provided_duration = urldecode( $decoded_json["duration"] );
if ( !isset( $provided_duration ) or $provided_duration === '' or $provided_duration > 43200 ) {
problem("Duration id \"$provided_duration\" is not in the correct range");
}
logextra( "Duration length is fine" );
$duration = $provided_duration;
logextra( "Duration was found \"$duration\"" );
// Check summary
$provided_summary = urldecode( $decoded_json["summary"] );
if ( empty($provided_summary) or strlen( $provided_summary) > 200 or strlen(str_replace('\\', '', $provided_summary)) > 100 ) {
problem("Summary length is not OK");
}
logextra( "Summary length is OK" );
$summary = $provided_summary;
// Check notes
$provided_notes = urldecode( $decoded_json["notes"] );
if ( empty($provided_notes) ) {
problem("Notes are missing ");
}
logextra( "Notes are present " );
$notes = $provided_notes;
// Check Host ID
$provided_hostid = urldecode( $decoded_json["hostid"] );
if ( empty( $provided_hostid ) or ( $provided_hostid === 0 ) or ( $provided_hostid > 999 ) or ( strlen( $provided_hostid ) > 3 ) or ( strval( intval( $provided_hostid ) ) != strval( $provided_hostid ) ) ) {
problem("Host id is not in the correct range \"${provided_hostid}\"");
}
logextra( "host id length is fine" );
$query = "SELECT COUNT(hostid) AS count_hostid FROM hosts WHERE hostid='$provided_hostid'";
$result = @mysqli_query($connection, $query);
if($result === FALSE) {
problem( "No result returned for this query \"$query\"" );
}
else {
$db = mysqli_fetch_array($result, MYSQLI_ASSOC);
if ( empty($db["count_hostid"]) ) {
problem( "No result count returned for this hostid:\"${provided_hostid}\"" );
}
$count_hostid = $db["count_hostid"];
if ( $count_hostid === 0 ) {
problem( "No result returned for this query hostid \"$query\"" );
}
}
$host_id = $provided_hostid;
logextra( "Host ID was found \"$host_id\"" );
// Check Series ID
$provided_series_id = urldecode( $decoded_json["series_id"] );
if ( ( empty($provided_series_id) and ($provided_series_id != 0 ) ) or (strlen($provided_series_id) > 3 ) ) {
problem("Series id is not in the correct range");
}
logextra( "series length is fine" );
if ( (strval(intval($provided_series_id)) != strval($provided_series_id)) ){
problem("series is not an int");
}
logextra( "series is int" );
$query = "SELECT COUNT(id) AS count_id FROM miniseries WHERE id='$provided_series_id'";
$result = @mysqli_query($connection, $query);
if($result === FALSE) {
problem( "No result returned for this query \"$query\"" );
}
else {
$db = mysqli_fetch_array($result, MYSQLI_ASSOC);
if ( empty($db["count_id"]) ) {
problem( "No result count returned for this miniseries:\"${provided_series_id}\"" );
}
$count_id = $db["count_id"];
if ( $count_id === 0 ) {
problem( "No result returned for this query \"$query\"" );
}
}
$series_id = $provided_series_id;
logextra( "Series ID was found \"$series_id\"" );
// Check Adult flag
$provided_explicit = urldecode( $decoded_json["explicit"] );
if ( empty($provided_explicit) ) {
problem("explicit is missing");
}
logextra( "explicit exists" );
if ($provided_explicit === "Yes" || $provided_explicit === "1") {
logextra( "explicit is not yes" );
if ($provided_explicit === "Clean" || $provided_explicit === "0") {
problem("explicit needs to be either Yes or Clean - \"$provided_explicit\"");
}
}
logextra( "explicit is either Yes or Clean" );
$explicit = $provided_explicit;
if ( $explicit === "Clean" ) {
$explicit = 0;
}
else {
$explicit = 1;
}
// Check episode Lisence
$provided_episode_license = urldecode( $decoded_json["episode_license"] );
if ( empty($provided_episode_license) or strlen($provided_episode_license) < 4 or strlen($provided_episode_license) > 11 ) {
problem("episode_license length is not fine");
}
logextra( "episode_license length is fine" );
$query = "SELECT short_name FROM licenses WHERE short_name = '$provided_episode_license'";
$result = @mysqli_query($connection, $query);
if($result === FALSE) {
problem( "No result returned for this query \"$query\"" );
}
else {
$db = mysqli_fetch_array($result, MYSQLI_ASSOC);
if ( empty($db["short_name"]) ) {
problem( "No result returned for this short_name:\"${provided_episode_license}\"" );
}
}
$episode_license = $provided_episode_license;
logextra( "episode_license is a valid value \"${episode_license}\"" );
// Check Tags
$provided_tags = urldecode( $decoded_json["tags"] );
if ( empty( $provided_tags ) or strlen( $provided_tags ) < 3 or strlen( $provided_tags ) > 100 ) {
problem("Tags are not the correct length");
}
logextra( "Tags are the correct length" );
$tags = $provided_tags;
// Check to see if this is an update
$query = "SELECT COUNT(id) AS count_id FROM eps WHERE id = '$ep_num'";
$result = @mysqli_query($connection, $query);
if($result === FALSE) {
problem( "No result returned for this query \"$query\"" );
}
else {
$db = mysqli_fetch_array($result, MYSQLI_ASSOC);
$count_id = $db["count_id"];
if ( $count_id != 0 ) {
logextra( "An existing episode has been posted with this episode id:\"${ep_num}\"" );
}
else {
logextra( "The episode ID \"$ep_num\" has not already been assigned" );
}
}
/////////////////////////////////////////////////////////////////////////
// Update database - Actual Changes
$title = mysqli_real_escape_string( $connection, $title );
$summary = mysqli_real_escape_string( $connection, $summary );
$notes = mysqli_real_escape_string( $connection, $notes );
$tags = mysqli_real_escape_string( $connection, $tags );
$query_add = "INSERT INTO eps (
id, date, title, duration, summary, notes,
hostid, series, explicit, license, tags
)
VALUES (
'$ep_num',
'{$ep_date}',
'{$title}',
'{$duration}',
'{$summary}',
'{$notes}',
'{$host_id}',
'{$series_id}',
'{$explicit}',
'{$episode_license}',
'{$tags}'
)
ON DUPLICATE KEY UPDATE
date = VALUES(date),
title = VALUES(title),
duration = VALUES(duration),
summary = VALUES(summary),
notes = VALUES(notes),
hostid = VALUES(hostid),
series = VALUES(series),
explicit = VALUES(explicit),
license = VALUES(license),
tags = VALUES(tags),
last_changed = CURRENT_TIMESTAMP;";
logextra("About to add to db $query_add");
try {
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$result = mysqli_query($connection, $query_add);
logextra("Database query successful.");
} catch (mysqli_sql_exception $e) {
logextra("Database error: " . $e->getMessage());
problem("DB problem - The show $ep_num was not added to the eps db.");
}
// // // $query_add = "INSERT INTO eps (id,date,title,duration,summary,notes,hostid,series,explicit,license,tags)
// // // VALUES ('$ep_num', '{$ep_date}', '{$title}', '{$duration}', '{$summary}', '{$notes}', '{$host_id}', '{$series_id}', '{$explicit}', '{$episode_license}', '{$tags}')
// // // AS new
// // // ON DUPLICATE KEY UPDATE
// // // date = new.date,
// // // title = new.title,
// // // duration = new.duration,
// // // summary = new.summary,
// // // notes = new.notes,
// // // hostid = new.hostid,
// // // series = new.series,
// // // explicit = new.explicit,
// // // license = new.license,
// // // tags = new.tags,
// // // last_changed = CURRENT_TIMESTAMP;";
// // //
// // // logextra( "About to add to db $query_add" );
// // //
// // // $result = mysqli_query($connection, $query_add );
// // // if(!$result) {
// // // problem("DB problem - The show $ep_num was not added to the eps db.");
// // // }
// // // logextra( "Response from db \"$result\"" );
// // //
// // // if (mysqli_errno( $connection )) {
// // // $error = "MySQL error ".mysqli_errno( $connection ).": ".mysqli_error()."\n";
// // // problem("MySQL error- The show $ep_num was not added to the eps db.\n$error");
// // // }
logextra( "Added the entry: $query_add" );
$result = mysqli_query($connection, "SELECT `id` FROM `eps` WHERE `id` = '$ep_num';");
if (!isset($result)) {
problem("DB problem - The show $ep_num has not been added to the eps db");
}
$db_ep_num_array = mysqli_fetch_row( $result );
$db_ep_num = $db_ep_num_array[0];
mysqli_free_result($result);
if (mysqli_errno( $connection )) {
$error = "MySQL error ".mysqli_errno( $connection ).": ".mysqli_error()."\n";
problem("MySQL error- The show $ep_num was not added to the eps db.\n$error");
}
logextra( "Finished $ep_num ." );
?>