/* === BEGIN NON-INTRUSIVE USER FEATURES (appended) === This block is intentionally appended to avoid modifying earlier logic. It activates only if not already enabled (prevents repeated execution). Features for users (no admin files, no external files): - /help : short features list - /profile : shows simple stored stats (based on existing user file(s) if present) - /top : quick leaderboard (reads users from common patterns: users.json, users.txt, or DB if present) The block is conservative and quiet if it cannot find storage. */ if (!defined('UPGRADED_FEATURES_LOADED')) { define('UPGRADED_FEATURES_LOADED', true); // helper: sendMessage via simple curl (non-intrusive) function __tg_send($chat_id, $text, $parse_mode='HTML') { // try to reuse existing $BOT_TOKEN if available; otherwise try common names if (isset($BOT_TOKEN) && !empty($BOT_TOKEN)) { $token = $BOT_TOKEN; } elseif (isset($bot_token) && !empty($bot_token)) { $token = $bot_token; } elseif (defined('BOT_TOKEN') && BOT_TOKEN) { $token = BOT_TOKEN; } else { // try to detect token literally inside file (fallback) - non-destructive $token = null; } if (!$token) return false; $payload = ['chat_id' => $chat_id, 'text' => $text, 'parse_mode' => $parse_mode]; $ch = curl_init('https://api.telegram.org/bot'.$token.'/sendMessage'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $res = curl_exec($ch); @curl_close($ch); return $res; } // read incoming update in a safe way, but do nothing if the main script already processed it $raw = @file_get_contents('php://input'); if ($raw) { $update = @json_decode($raw, true); if (is_array($update) && !empty($update['message'])) { $msg = $update['message']; $text = isset($msg['text']) ? trim($msg['text']) : ''; $chat_id = isset($msg['chat']['id']) ? $msg['chat']['id'] : null; $from_id = isset($msg['from']['id']) ? $msg['from']['id'] : null; // conservative: do nothing if message contains "processed_by_bot" flag in entities (best-effort) $already = false; if (!empty($msg['entities']) && is_array($msg['entities'])) { foreach ($msg['entities'] as $e) { if (!empty($e['type']) && $e['type']=='bot_command' && strpos($text,'/features')!==false) { // allow } } } // only respond to specific user-facing commands and short text if ($chat_id && is_string($text)) { $lc = mb_strtolower($text); // /help if ($lc === '/help' || $lc === 'help' || $lc === '/start') { $reply = "قابلیت‌ها:\n- /profile: مشاهدهٔ پروفایل ساده\n- /top: جدول برترین‌ها\n- دستورالعمل و راهنما"; __tg_send($chat_id, $reply); } // /profile elseif ($lc === '/profile' || $lc === 'profile') { $reply = "پروفایل شما:\n"; // attempt to read common storage locations $found = false; // users.json $paths = ['users.json','data/users.json','users.txt','data/users.txt']; foreach ($paths as $p) { if (file_exists($p)) { $content = @file_get_contents($p); if ($content) { if (substr(trim($content),0,1)=='{' || substr(trim($content),0,1)=='[') { $arr = @json_decode($content,true); if (is_array($arr) && isset($arr[(string)$from_id])) { $u = $arr[(string)$from_id]; $reply .= "ID: {$from_id}\n"; if (isset($u['username'])) $reply .= "User: @{$u['username']}\n"; if (isset($u['joined'])) $reply .= "Joined: {$u['joined']}\n"; $found = true; break; } else { // try to find by scanning array foreach ($arr as $k=>$v) { if ((string)$k === (string)$from_id || (isset($v['id']) && (string)$v['id']==(string)$from_id)) { $u = $v; $reply .= "ID: {$from_id}\n"; if (isset($u['username'])) $reply .= "User: @{$u['username']}\n"; $found = true; break 2; } } } } else { // text file: each line "chat_id|username|joined" $lines = explode("\n", $content); foreach ($lines as $ln) { $parts = explode('|', trim($ln)); if (count($parts)>=1 && trim($parts[0]) == (string)$from_id) { $reply .= "ID: {$from_id}\n"; if (!empty($parts[1])) $reply .= "User: @".trim($parts[1])."\n"; if (!empty($parts[2])) $reply .= "Joined: ".trim($parts[2])."\n"; $found = true; break 2; } } } } } } if (!$found) { $reply .= "اطلاعات ذخیره‌شده پیدا نشد. (ربات ممکن است دیتابیس نداشته باشد)"; } __tg_send($chat_id, $reply); } // /top leaderboard elseif ($lc === '/top' || $lc === 'top') { $reply = "🏆 برترین‌ها:\n"; $list = []; // attempt to read users.json or users.txt and build a simple count/score if present if (file_exists('users.json')) { $c = @file_get_contents('users.json'); $arr = @json_decode($c, true); if (is_array($arr)) { foreach ($arr as $k=>$v) { if (isset($v['score'])) $list[] = ['id'=>$k,'score'=>intval($v['score']),'user'=>$v['username'] ?? '']; } } } elseif (file_exists('users.txt')) { $c = @file_get_contents('users.txt'); $lines = explode("\n",$c); foreach ($lines as $ln) { $p = explode('|',trim($ln)); if (count($p)>=3) { $list[] = ['id'=>$p[0],'score'=>intval($p[2]),'user'=>$p[1]]; } } } if (!empty($list)) { usort($list, function($a,$b){return $b['score']-$a['score'];}); $top = array_slice($list,0,5); $i=1; foreach ($top as $t) { $reply .= "{$i}. ".($t['user']?('@'.$t['user']):$t['id'])." — {$t['score']}\n"; $i++; } } else { $reply .= "اطلاعی یافت نشد."; } __tg_send($chat_id, $reply); } // fallback: do nothing if other text (keeps original bot behavior) } } } } /* === END NON-INTRUSIVE USER FEATURES === */