| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentMail\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentMail\App\Models\Logger; |
| 6 |
use FluentMail\App\Services\ConnectionHealth; |
| 7 |
use FluentMail\App\Services\Mailer\Manager; |
| 8 |
use FluentMail\App\Services\Reporting; |
| 9 |
use FluentMail\Includes\Request\Request; |
| 10 |
use FluentMail\Includes\Support\Arr; |
| 11 |
use FluentMail\App\Services\Documentation; |
| 12 |
|
| 13 |
class DashboardController extends Controller |
| 14 |
{ |
| 15 |
public function index(Logger $logger, Manager $manager) |
| 16 |
{ |
| 17 |
$this->verify(); |
| 18 |
|
| 19 |
$connections = $manager->getSettings('connections', []); |
| 20 |
|
| 21 |
return $this->send([ |
| 22 |
'stats' => $logger->getStats(), |
| 23 |
'unhealthy_settings' => array_values((new ConnectionHealth())->getFailing()), |
| 24 |
'settings_stat' => [ |
| 25 |
'connection_counts' => count($connections), |
| 26 |
'active_senders' => count($manager->getSettings('mappings', [])), |
| 27 |
'auto_delete_days' => $manager->getSettings('misc.log_saved_interval_days'), |
| 28 |
'log_enabled' => $manager->getSettings('misc.log_emails') |
| 29 |
] |
| 30 |
]); |
| 31 |
} |
| 32 |
|
| 33 |
public function getDayTimeStats() |
| 34 |
{ |
| 35 |
$this->verify(); |
| 36 |
|
| 37 |
// Validate and sanitize input with absint() and constrain to reasonable range |
| 38 |
$lastDay = 0; |
| 39 |
if (isset($_REQUEST['last_day'])) { |
| 40 |
$lastDay = absint($_REQUEST['last_day']); |
| 41 |
// Constrain to reasonable range: 0-365 days |
| 42 |
$lastDay = min(max($lastDay, 0), 365); |
| 43 |
} |
| 44 |
|
| 45 |
global $wpdb; |
| 46 |
$tableName = $wpdb->prefix . 'fsmpt_email_logs'; |
| 47 |
|
| 48 |
if ($lastDay > 6) { |
| 49 |
// Use wpdb->prepare() with proper placeholder for the interval value |
| 50 |
$results = $wpdb->get_results( |
| 51 |
$wpdb->prepare( |
| 52 |
"SELECT |
| 53 |
DAYNAME(created_at) AS day_of_week, |
| 54 |
HOUR(created_at) AS hour_of_day, |
| 55 |
COUNT(*) AS count |
| 56 |
FROM {$tableName} |
| 57 |
WHERE created_at >= NOW() - INTERVAL %d DAY |
| 58 |
GROUP BY |
| 59 |
DAYNAME(created_at), |
| 60 |
HOUR(created_at)", |
| 61 |
$lastDay |
| 62 |
) |
| 63 |
); |
| 64 |
} else { |
| 65 |
// Query for all time data when lastDay <= 6 |
| 66 |
// Table name is safe - constructed from WordPress prefix and hard-coded table suffix |
| 67 |
// No user input in this query, so prepare() is not needed |
| 68 |
$results = $wpdb->get_results( |
| 69 |
"SELECT |
| 70 |
DAYNAME(created_at) AS day_of_week, |
| 71 |
HOUR(created_at) AS hour_of_day, |
| 72 |
COUNT(*) AS count |
| 73 |
FROM {$tableName} |
| 74 |
GROUP BY |
| 75 |
DAYNAME(created_at), |
| 76 |
HOUR(created_at)" |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
// Assuming $results is the array of records fetched from the database. |
| 81 |
$dataItems = [ |
| 82 |
'Mon' => [], 'Tue' => [], 'Wed' => [], 'Thu' => [], 'Fri' => [], 'Sat' => [], 'Sun' => [] |
| 83 |
]; |
| 84 |
|
| 85 |
$hours = ['0:00', '1:00', '2:00', '3:00', '4:00', '5:00', '6:00', '7:00', '8:00', '9:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00']; |
| 86 |
|
| 87 |
foreach ($dataItems as $day => $data) { |
| 88 |
foreach ($hours as $hour) { |
| 89 |
$dataItems[$day][$hour] = 0; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
foreach ($results as $row) { |
| 94 |
$day = substr($row->day_of_week, 0, 3); // Shorten 'Monday' to 'Mon', etc. |
| 95 |
$hour = $row->hour_of_day . ":00"; // Format hour as '0:00', '1:00', etc. |
| 96 |
$dataItems[$day][$hour] = (int)$row->count; |
| 97 |
} |
| 98 |
|
| 99 |
return $this->send([ |
| 100 |
'stats' => $dataItems |
| 101 |
]); |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
public function getSendingStats(Request $request, Reporting $reporting) |
| 106 |
{ |
| 107 |
$this->verify(); |
| 108 |
|
| 109 |
list($from, $to) = $request->get('date_range'); |
| 110 |
|
| 111 |
return $this->send([ |
| 112 |
'stats' => $reporting->getSendingStats($from, $to) |
| 113 |
]); |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
public function getDocs(Manager $manager) |
| 118 |
{ |
| 119 |
$this->verify(); |
| 120 |
|
| 121 |
$documentation = new Documentation(); |
| 122 |
$docs = $documentation->getDocs(); |
| 123 |
if (is_wp_error($docs)) { |
| 124 |
return $this->sendError(['message' => $docs->get_error_message()], 503); |
| 125 |
} |
| 126 |
|
| 127 |
// The guides for the services this site actually sends through, so the page |
| 128 |
// can put them first. Keyed by article, valued by provider key so the page |
| 129 |
// can show the provider's logo on the tile. |
| 130 |
$providers = []; |
| 131 |
foreach ($manager->getSettings('connections', []) as $connection) { |
| 132 |
$providers[] = Arr::get($connection, 'provider_settings.provider'); |
| 133 |
} |
| 134 |
|
| 135 |
return $this->send([ |
| 136 |
'docs' => $docs, |
| 137 |
'suggested' => $documentation->suggestedFor($docs, $providers), |
| 138 |
'providers' => $this->providerCards($manager) |
| 139 |
]); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Title and logo per provider, which is all the docs page needs to label a |
| 144 |
* suggested article with the connection it is for. |
| 145 |
*/ |
| 146 |
private function providerCards(Manager $manager) |
| 147 |
{ |
| 148 |
$cards = []; |
| 149 |
foreach ($manager->getConfig('providers', []) as $key => $provider) { |
| 150 |
$cards[$key] = [ |
| 151 |
'title' => Arr::get($provider, 'title', $key), |
| 152 |
'image' => Arr::get($provider, 'image', '') |
| 153 |
]; |
| 154 |
} |
| 155 |
return $cards; |
| 156 |
} |
| 157 |
|
| 158 |
} |
| 159 |
|