芝麻web文件管理V1.00
编辑当前文件:/home/pulsehostuk9/public_html/teafund.pulsehost.co.uk/includes/notify.php
true, CURLOPT_POSTFIELDS => $json, CURLOPT_HTTPHEADER => ['Content-Type: application/json'], CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 8, ]); $res = curl_exec($ch); $errno = curl_errno($ch); $http = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($errno) { slack_set_last_error('curl_error:' . $errno . ' ' . curl_error($ch)); curl_close($ch); return false; } curl_close($ch); if ($http < 200 || $http >= 300) { slack_set_last_error('http_' . $http); return false; } return true; } // Fallback $ctx = stream_context_create([ 'http' => [ 'method' => 'POST', 'header' => "Content-Type: application/json\r\n", 'content' => $json, 'timeout' => 8, 'ignore_errors' => true, ] ]); $res = @file_get_contents(SLACK_WEBHOOK_URL, false, $ctx); if ($res === false) { slack_set_last_error('fopen_failed'); return false; } // Try to read status line if (isset($http_response_header[0]) && strpos($http_response_header[0], '200') === false && strpos($http_response_header[0], '2') !== 0) { slack_set_last_error('http_unknown'); return false; } return true; } function slack_escape(string $s): string { return strtr($s, ['&'=>'&','<'=>'<','>'=>'>']); } function slack_notify_stock_new(array $alert, ?string $fundName = null): bool { $item = $alert['item'] ?? ''; $note = trim((string)($alert['note'] ?? '')); $fund = $fundName ? " *Fund:* " . slack_escape($fundName) . "\n" : ''; $text = ":bell: *Low stock alert* — " . slack_escape($item); if ($note) $text .= " — _" . slack_escape($note) . "_"; $blocks = [ ['type'=>'header','text'=>['type'=>'plain_text','text'=>'Low stock alert']], ['type'=>'section','text'=>['type'=>'mrkdwn','text'=>"*Item:* " . slack_escape($item) . "\n" . $fund . ($note ? "*Note:* " . slack_escape($note) : '')]], ['type'=>'context','elements'=>[['type'=>'mrkdwn','text'=>'ID: ' . (int)($alert['id'] ?? 0) . ' • ' . date('Y-m-d H:i')]]], ]; return slack_post(['text'=>$text,'blocks'=>$blocks]); } function slack_notify_stock_update(array $alert, string $action, ?string $fundName = null): bool { $verb = $action === 'resolve' ? 'Resolved' : 'Acknowledged'; $item = $alert['item'] ?? ''; $note = trim((string)($alert['note'] ?? '')); $fund = $fundName ? " *Fund:* " . slack_escape($fundName) . "\n" : ''; $text = ($action === 'resolve' ? ':white_check_mark:' : ':warning:') . " *$verb:* " . slack_escape($item); if ($note) $text .= " — _" . slack_escape($note) . "_"; $blocks = [ ['type'=>'section','text'=>['type'=>'mrkdwn','text'=>"*$verb*\n*Item:* " . slack_escape($item) . "\n" . $fund . ($note ? "*Note:* " . slack_escape($note) : '')]], ['type'=>'context','elements'=>[['type'=>'mrkdwn','text'=>date('Y-m-d H:i')]]], ]; return slack_post(['text'=>$text,'blocks'=>$blocks]); }