File: //home/awaldron/_phpassport/api.pennhillspassport.com/v1/news.php
<?php
//**********************************************************************************************************//
// Method: POST
// Endpoint: news/list
// Params: i, size
function post_v1_news_list($args, $body, $uid, $is_admin, $db)
{
$rules = [
'i' => 'req',
'size' => 'req',
];
validate_body($body, $rules);
if (empty($body['chIds'])) {
$body['chIds'] = [];
}
if (empty($body['feed'])) {
$body['feed'] = 1;
}
if (empty($body['unapproved'])) {
$body['unapproved'] = 0;
}
if (empty($body['q'])) {
$body['q'] = '';
}
$channel_count = count($body['chIds']);
$search = trim($body['q']);
$has_search = strlen($search) > 3;
if ($channel_count === 0 && !$is_admin) {
$data['records'] = array();
$data['channels'] = [];
$data['pager'] = [
'i' => 0,
'j' => $body['size'],
'size' => 0,
'total' => 0,
'q' => $body['q'],
];
response(200, ['success' => true, 'data' => $data]);
}
else {
try {
$sql = "SELECT n.id, n.image, n.headline, n.subhead, n.author, n.date_published AS datePublished, n.views, n.shares, n.pushed, n.approved, n.ext_url as extUrl,
c.name, c.slug, c.avatar, u.fname, u.lname
FROM news as n, channels as c, users as u
WHERE ".($body['unapproved'] === 0 ? " n.approved=1 AND " : "")." n.channel_id=c.id AND n.user_id = u.user_id AND n.feed".($body['feed']===-1?'>':'=').$body['feed']." ".
(count($body['chIds']) > 0 ? "AND n.channel_id IN (".implode(',',$body['chIds']).")" : "") .
($has_search ? " AND (LOWER(n.headline) LIKE '%" . addslashes(strtolower($search)) . "%'
OR LOWER(n.body) LIKE '%" . addslashes(strtolower($search)) . "%'
OR LOWER(c.name) LIKE '%" . addslashes(strtolower($search)) . "%'
OR LOWER(n.author) LIKE '%" . addslashes(strtolower($search)) . "%'
OR LOWER(n.subhead) LIKE '%" . addslashes(strtolower($search)) . "%')" : "").
" ORDER BY n.date_published DESC LIMIT " . $body['i'] . ", " . $body['size'];
$res = qSet($db, $sql);
$total = qVal($db, "SELECT COUNT(id) FROM news WHERE approved=1");
$data['records'] = $res;
$data['channels'] = [];
$data['pager'] = [
'i' => $body['i'],
'j' => $body['i'] + $body['size'],
'size' => $body['size'],
'total' => $total,
'q' => $body['q'],
];
response(200, ['success' => true, 'data' => $data]);
} catch (Exception $e) {
response(200, ['success' => false, 'errors' => [$e->getMessage()]]);
}
}
}
//**********************************************************************************************************//
// Method: POST
// Endpoint: news/image
// Params: i, size
function post_v1_news_image($args, $body, $uid, $is_admin, $db)
{
$upload_dir = '/home/awaldron/_phpassport/pennhillspassport.com/news/';
//$upload_dir = 'uploads/';
$server_url = 'https://pennhillspassport.com/news';
try {
if ($_FILES['image']) {
$image_name = $_FILES["image"]["name"];
$image_temp_name = $_FILES["image"]["tmp_name"];
$error = $_FILES["image"]["error"];
$file_ext = pathinfo($image_name, PATHINFO_EXTENSION);
$file_name_str = pathinfo($image_name, PATHINFO_FILENAME);
if ($error > 0) {
response(200, ['success' => false, 'files' => $_FILES, 'errors' => ["File payload contained an error"]]);
} else {
$new_name = rand(1000, 1000000) . "-" . slugify($file_name_str) . '.' . $file_ext;
$upload_name = $upload_dir . $new_name;
if (move_uploaded_file($image_temp_name, $upload_name)) {
resize_crop_image($upload_name, $upload_name, 1000, 500);
response(200, ['success' => true, "message" => "File uploaded successfully", "url" => $server_url . "/" . strtolower($new_name), 'files' => $_FILES]);
} else {
response(200, ['success' => false, 'files' => $_FILES, 'errors' => ["Error uploading file"]]);
}
}
} else {
response(200, ['success' => false, 'files' => $_FILES, 'errors' => ["No file sent."]]);
}
} catch (Exception $e) {
response(200, ['success' => false, 'files' => $_FILES, 'errors' => [$e->getMessage()]]);
}
}
//**********************************************************************************************************//
// Method: GET
// Endpoint: story
// Params: id
function get_v1_news_story($args, $body, $uid, $is_admin, $db)
{
try {
$res = qRow($db, "SELECT n.id, n.image, n.headline, n.subhead, n.body, n.author, n.feed, n.date_published as datePublished, n.views,
n.shares, n.ext_url as extUrl, c.id as channel_id, c.name, c.slug, c.avatar, u.fname, u.lname
FROM news as n, channels as c, users as u
WHERE n.id=" . $args[0] . " AND n.channel_id=c.id");
response(200, ['success' => true, 'data' => $res]);
} catch (Exception $e) {
response(200, ['success' => false, 'errors' => [$e->getMessage()]]);
}
}
//**********************************************************************************************************//
// Method: GET
// Endpoint: stock
// Params: id
function get_v1_news_stock($args, $body, $uid, $is_admin, $db)
{
try {
$res = qSet($db, "SELECT * FROM news_images ORDER BY rank ASC, url ASC");
response(200, ['success' => true, 'data' => $res]);
} catch (Exception $e) {
response(200, ['success' => false, 'errors' => [$e->getMessage()]]);
}
}
//**********************************************************************************************************//
// Method: DELETE
// Endpoint: story
// Params: id
function delete_v1_news_story($args, $body, $uid, $is_admin, $db)
{
try {
// CHECK IF USER IS ADMIN OF CHANNEL
$story_channel_id = qVal($db, "SELECT channel_id FROM news WHERE id=" . $args[0]);
if (!$story_channel_id) {
response(200, ['success' => false, 'errors' => ['Story ID not found.']]);
}
$is_admin = qVal($db, "SELECT id FROM channel_admins WHERE user_id=" . $uid . " AND channel_id=" . $story_channel_id);
if ($is_admin) {
$db->query("UPDATE news SET approved=0 WHERE id=" . $args[0]);
response(200, ['success' => true]);
} else {
response(200, ['success' => false, 'errors' => ['User not authorized to delete this news story.']]);
}
} catch (Exception $e) {
response(200, ['success' => false, 'errors' => [$e->getMessage()]]);
}
}
//**********************************************************************************************************//
// Method: POST
// Endpoint: news/story
// Params:
function post_v1_news_story($args, $body, $uid, $admin_level, $db)
{
$rules = [
'userId' => 'req',
'channelId' => 'req',
'image' => 'req',
'headline' => 'req',
'body' => 'req',
'feed' => 'req',
];
validate_body($body, $rules);
// CHECK IF USER IS ADMIN OF CHANNEL
$is_admin = qVal($db, "SELECT id FROM channel_admins WHERE user_id=" . $uid . " AND channel_id=" . $body['channelId']);
if ($is_admin) {
try {
$ins = "INSERT INTO news (`user_id`, `channel_id`, `image`, `headline`, `subhead`, `body`, `author`, `date_published`, `date_modified`, `feed`, `ext_url`, `approved`, `pushed`) VALUES (";
$ins .= $body['userId'] . ', ';
$ins .= $body['channelId'] . ', ';
$ins .= "'" . $body['image'] . "', ";
$ins .= "'" . addslashes($body['headline']) . "', ";
$ins .= "'" . addslashes($body['subhead']) . "', ";
$ins .= "'" . addslashes($body['body']) . "', ";
$ins .= "'" . addslashes($body['author']) . "', ";
$ins .= "'" . iso_date() . "', ";
$ins .= "'" . iso_date() . "', ";
$ins .= "'" . $body['feed'] . "', ";
$ins .= "'" . $body['extUrl'] . "', ";
//$ins .= $admin_level > 0 ? "1" : "0";
$ins .= "1, 0)"; // approved, pushed
$db->query($ins);
// GET NEWS ID
$new_id = qVal($db, "SELECT MAX(id) FROM news");
response(200, ['success' => true, 'id' => $new_id]);
} catch (Exception $e) {
response(200, ['success' => false, 'errors' => [$e->getMessage()]]);
}
} else {
response(200, ['success' => false, 'uid'=>$uid, 'channelId'=>$body['channelId'], 'errors' => ['User cannot post to this channel.']]);
}
}
//**********************************************************************************************************//
// Method: PUT
// Endpoint: news/story
// Params: i, size
function put_v1_news_story($args, $body, $uid, $admin_level, $db)
{
$rules = [
'id' => 'req',
'userId' => 'req',
'channelId' => 'req',
'image' => 'req',
'datePublished' => 'req',
'headline' => 'req',
'body' => 'req',
'feed' => 'req',
];
validate_body($body, $rules);
if ($admin_level >= 100 || $uid === $body['userId']) {
try {
$upd = "UPDATE news SET ";
$upd .= 'user_id=' . $body['userId'] . ', ';
$upd .= 'channel_id=' . $body['channelId'] . ', ';
$upd .= "image='" . $body['image'] . "', ";
$upd .= "headline='" . addslashes($body['headline']) . "', ";
$upd .= "subhead='" . addslashes($body['subhead']) . "', ";
$upd .= "author='" . addslashes($body['author']) . "', ";
$upd .= "body='" . addslashes($body['body']) . "', ";
$upd .= "date_published='" . $body['datePublished'] . "', ";
$upd .= "feed='" . $body['feed'] . "', ";
$upd .= "ext_url='" . $body['extUrl'] . "' ";
$upd .= "WHERE id=" . $body['id'];
$db->query($upd);
response(200, ['success' => true]);
} catch (Exception $e) {
response(200, ['success' => false, 'errors' => [$e->getMessage()]]);
}
} else {
response(200, ['success' => false, 'errors' => ['Unauthorized']]);
}
}
//**********************************************************************************************************//
// Method: PUT
// Endpoint: news/viewsup
// Params: i, size
function put_v1_news_viewsup($args, $body, $uid, $is_admin, $db)
{
$rules = [
'id' => 'req',
];
$body['id'] = $args[0];
validate_body($body, $rules);
try {
$upd = "UPDATE news SET views = views + 1 WHERE id=" . $body['id'];
$db->query($upd);
response(200, ['success' => true]);
} catch (Exception $e) {
response(200, ['success' => false, 'errors' => [$e->getMessage()]]);
}
}
//**********************************************************************************************************//
// Method: PUT
// Endpoint: news/viewsup
// Params: i, size
function put_v1_news_sharesup($args, $body, $uid, $is_admin, $db)
{
$rules = [
'id' => 'req',
];
$body['id'] = $args[0];
validate_body($body, $rules);
try {
$upd = "UPDATE news SET shares = shares + 1 WHERE id=" . $body['id'];
$db->query($upd);
response(200, ['success' => true]);
} catch (Exception $e) {
response(200, ['success' => false, 'errors' => [$e->getMessage()]]);
}
}
//**********************************************************************************************************//
// Method: GET
// Endpoint: news/push
// Params: i, size
function get_v1_news_push($args, $body, $uid, $is_admin, $db)
{
$rules = [
'id' => 'req',
];
$body['id'] = $args[0];
validate_body($body, $rules);
$story = qRow($db, "SELECT * FROM news WHERE id=" . $body['id'] . " AND approved=1 AND pushed=0");
$ctr = 0;
if ($story !== false) {
sendPush($story['headline'], $story['subhead'], 'news/detaildeep/' . $body['id']);
$db->query("UPDATE news SET pushed=1 WHERE id=" . $body['id']);
$ctr++;
}
response(200, ['success' => true, 'pushes' => $ctr ]);
}