| Server IP : 104.21.21.239 / Your IP : 216.73.216.11 Web Server : Apache/2.4.68 (Amazon Linux) OpenSSL/3.5.5 System : Linux ip-172-31-69-123.ec2.internal 6.1.176-223.369.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Jul 24 13:34:27 UTC 2026 x86_64 User : ec2-user ( 1000) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/banners/public_html/bway/oldfeeds/ |
Upload File : |
<?php
/**
* CRM Google News Feed Processor
* Converted from crmfeed.cfm
*
* PART 1: Fetches Google News RSS for the oldest-processed CRM company and inserts new articles.
* PART 2: Updates unread feed counts for specific users.
*/
require_once __DIR__ . '/mysql_tls.php';
set_time_limit(500);
date_default_timezone_set('America/New_York');
// Database credentials
$db_host_read = 'amazonaurora.cluster-ro-cemzxojvmybt.us-east-1.rds.amazonaws.com';
$db_host_write = 'amazonaurora.cluster-cemzxojvmybt.us-east-1.rds.amazonaws.com';
$db_user = 'admin';
$db_pass = 'xxatN6Lb8Kbwb9MiU1At';
$db_name = 'amazonrds';
// Connect to read replica
$conn_read = oldfeeds_mysqli_connect($db_host_read, $db_user, $db_pass, $db_name);
if (!$conn_read) {
die("Read DB connection failed: " . mysqli_connect_error());
}
mysqli_set_charset($conn_read, 'utf8mb4');
// Connect to write master
$conn_write = oldfeeds_mysqli_connect($db_host_write, $db_user, $db_pass, $db_name);
if (!$conn_write) {
die("Write DB connection failed: " . mysqli_connect_error());
}
mysqli_set_charset($conn_write, 'utf8mb4');
// ============================================================
// PART 1: CRM Google News Feed Processor
// ============================================================
try {
// Get company data with region name in single query
$sql = "SELECT b.company, b.altcompany, b.regionid, b.id, b.typeid, r.name as regionname
FROM bwwcrm b
INNER JOIN regions r ON b.regionid = r.id
WHERE b.company <> ''
AND b.googlenews <> '1999-01-01'
ORDER BY b.googlenews ASC
LIMIT 0,1";
$result = mysqli_query($conn_read, $sql);
if ($result && mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
// Use altcompany if filled in, otherwise use company
$companyName = (strlen(trim($row['altcompany'])) > 0) ? trim($row['altcompany']) : trim($row['company']);
// DEBUG: Show company data
echo "<h3>Processing Company: " . htmlspecialchars($companyName) . " (Region: " . htmlspecialchars($row['regionname']) . ")</h3>\n";
// Build search query
$feedme = $companyName . '+' . $row['regionname'];
$feedme = str_ireplace(' ', '+', $feedme);
$feedme = str_ireplace('&', '+', $feedme);
if ($row['typeid'] == 24 || stripos($feedme, 'college') !== false || stripos($feedme, 'university') !== false) {
$feedme .= '+theatre';
}
$feedurl = "https://news.google.com/rss/search?q=" . $feedme . "+when:30d";
// DEBUG: Show search URL
echo "<p><strong>Search URL:</strong> " . htmlspecialchars($feedurl) . "</p>\n";
// Fetch RSS feed via curl
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $feedurl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => 'UTF-8',
]);
$rssContent = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode == 200 && $rssContent !== false) {
echo "<p><strong>RSS Feed Status:</strong> Successfully fetched (" . strlen($rssContent) . " characters)</p>\n";
// Parse XML
libxml_use_internal_errors(true);
$rss = simplexml_load_string($rssContent);
if ($rss !== false) {
// Find items
$items = $rss->xpath("//*[local-name()='item']");
// DEBUG: Show item count
echo "<p><strong>Found " . count($items) . " RSS items to process</strong></p>\n";
if (count($items) > 0) {
foreach ($items as $item) {
$titleNodes = $item->xpath(".//*[local-name()='title']");
$descNodes = $item->xpath(".//*[local-name()='description']");
$linkNodes = $item->xpath(".//*[local-name()='link']");
if (count($titleNodes) > 0 && count($linkNodes) > 0) {
$itemTitle = (string)$titleNodes[0];
$itemLink = (string)$linkNodes[0];
$itemDescription = '';
if (count($descNodes) > 0) {
try {
$itemDescription = (string)$descNodes[0];
} catch (Exception $e) {
$itemDescription = '';
}
}
// Strip non-ASCII characters
$chardecodeb = preg_replace('/[^\x20-\x7E]/', '', $itemTitle);
if (stripos($chardecodeb, 'broadwayworld') === false) {
// Check if title exists
$escapedTitle = mysqli_real_escape_string($conn_read, $chardecodeb);
$checkSql = "SELECT COUNT(*) as counter FROM bwwcrmfeeds WHERE title = '$escapedTitle'";
$checkResult = mysqli_query($conn_read, $checkSql);
$checkRow = mysqli_fetch_assoc($checkResult);
if ($checkRow['counter'] == 0) {
// Strip non-ASCII from description
$newsumnew = preg_replace('/[^\x20-\x7E]/', '', $itemDescription);
$now = date('Y-m-d H:i:s');
$escapedContent = mysqli_real_escape_string($conn_write, $newsumnew);
$escapedTitleW = mysqli_real_escape_string($conn_write, $chardecodeb);
$escapedLink = mysqli_real_escape_string($conn_write, $itemLink);
$escapedFeedname = mysqli_real_escape_string($conn_write, $companyName);
$insertSql = "INSERT INTO bwwcrmfeeds(publisheddate, content, title, link, feedname)
VALUES ('$now', '$escapedContent', '$escapedTitleW', '$escapedLink', '$escapedFeedname')";
mysqli_query($conn_write, $insertSql);
// DEBUG: Show inserted item
echo "<p style=\"color: green;\">Inserted: " . htmlspecialchars($chardecodeb) . "</p>\n";
} else {
// DEBUG: Show duplicate item
echo "<p style=\"color: orange;\">Duplicate: " . htmlspecialchars($chardecodeb) . "</p>\n";
}
}
}
}
}
}
}
// Update company record
$now = date('Y-m-d H:i:s');
$escapedCompany = mysqli_real_escape_string($conn_write, $row['company']);
$escapedCompanyName = mysqli_real_escape_string($conn_write, $companyName);
$companyId = intval($row['id']);
$updateSql = "UPDATE amazonrds.bwwcrm
SET googlenews = '$now',
lastbwwstory = '$now'
WHERE (company = '$escapedCompany'
OR (altcompany <> '' AND altcompany = '$escapedCompanyName'))
OR id = $companyId";
mysqli_query($conn_write, $updateSql);
}
} catch (Exception $e) {
// Log error but don't display sensitive information
$errorMsg = "Error processing feed: " . $e->getMessage();
}
// ============================================================
// PART 2: User Feed Count Updater (runs every time -- the CF
// condition 0-59 is always true)
// ============================================================
$currentMinutes = (int)date('i');
if ($currentMinutes >= 0 && $currentMinutes <= 59) {
// DEBUG: Show user count update
echo "<h3>Updating User Feed Counts (Current time: " . date('h:i:s A') . ")</h3>\n";
// Update Miles (enteredid=14497)
$sql1 = "UPDATE bwwcrmfeedcounts
SET unread = (
SELECT COUNT(*)
FROM (
SELECT f.id
FROM (
SELECT company
FROM amazonrds.bwwcrm
WHERE enteredid = 14497
AND googlenews NOT IN ('1999-01-01', '2001-01-01')
AND company <> ''
) c
INNER JOIN amazonrds.bwwcrmfeeds f ON f.feedname = c.company AND f.processed = 0
UNION DISTINCT
SELECT f.id
FROM (
SELECT altcompany
FROM amazonrds.bwwcrm
WHERE enteredid = 14497
AND googlenews NOT IN ('1999-01-01', '2001-01-01')
AND altcompany <> ''
) c
INNER JOIN amazonrds.bwwcrmfeeds f ON f.feedname = c.altcompany AND f.processed = 0
) x
)
WHERE name = 'Miles'";
// Update Alex (enteredid=1520033)
$sql2 = "UPDATE bwwcrmfeedcounts
SET unread = (
SELECT COUNT(*)
FROM (
SELECT f.id
FROM (
SELECT company
FROM amazonrds.bwwcrm
WHERE enteredid = 1520033
AND googlenews NOT IN ('1999-01-01', '2001-01-01')
AND company <> ''
) c
INNER JOIN amazonrds.bwwcrmfeeds f ON f.feedname = c.company AND f.processed = 0
UNION DISTINCT
SELECT f.id
FROM (
SELECT altcompany
FROM amazonrds.bwwcrm
WHERE enteredid = 1520033
AND googlenews NOT IN ('1999-01-01', '2001-01-01')
AND altcompany <> ''
) c
INNER JOIN amazonrds.bwwcrmfeeds f ON f.feedname = c.altcompany AND f.processed = 0
) x
)
WHERE name = 'Alex'";
// Update Dianna (enteredid=1854039)
$sql3 = "UPDATE bwwcrmfeedcounts
SET unread = (
SELECT COUNT(*)
FROM (
SELECT f.id
FROM (
SELECT company
FROM amazonrds.bwwcrm
WHERE enteredid = 1854039
AND googlenews NOT IN ('1999-01-01', '2001-01-01')
AND company <> ''
) c
INNER JOIN amazonrds.bwwcrmfeeds f ON f.feedname = c.company AND f.processed = 0
UNION DISTINCT
SELECT f.id
FROM (
SELECT altcompany
FROM amazonrds.bwwcrm
WHERE enteredid = 1854039
AND googlenews NOT IN ('1999-01-01', '2001-01-01')
AND altcompany <> ''
) c
INNER JOIN amazonrds.bwwcrmfeeds f ON f.feedname = c.altcompany AND f.processed = 0
) x
)
WHERE name = 'Dianna'";
mysqli_query($conn_write, $sql1);
mysqli_query($conn_write, $sql2);
mysqli_query($conn_write, $sql3);
// DEBUG: Show completion
echo "<p style=\"color: blue;\"><strong>User feed counts updated successfully</strong></p>\n";
} else {
// DEBUG: Show time check
echo "<p>User count update skipped (Current minutes: $currentMinutes - not between 15-20)</p>\n";
}
// Close connections
mysqli_close($conn_read);
mysqli_close($conn_write);