Advertisement
WriterofDestiny

writer function

Feb 1st, 2025 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.38 KB | Source Code | 0 0
  1. class Functions{
  2.     public $file;
  3.     public function __construct($file = "cookie.txt"){
  4.         $this->file = $file;
  5.     }
  6.     public function curl($url, $header, $method = "GET", $follow = 1, $request = null) {
  7.         $data = ["data" => null, "info" => null, "error" => null, "header" => null];
  8.         $link = parse_url($url, PHP_URL_HOST);
  9.         $retryCount = 0;
  10.         while ($retryCount < 5) {
  11.             $ch = curl_init();
  12.             $options = [
  13.                 CURLOPT_URL => $url,
  14.                 CURLOPT_RETURNTRANSFER => true,
  15.                 CURLOPT_FOLLOWLOCATION => $follow,
  16.                 CURLOPT_COOKIEJAR => getcwd()."/{$this->file}",
  17.                 CURLOPT_COOKIEFILE => getcwd()."/{$this->file}",
  18.                 CURLOPT_HTTPHEADER => $header,
  19.                 CURLOPT_SSL_VERIFYPEER => false,
  20.                 CURLOPT_SSL_VERIFYHOST => false,
  21.                 CURLOPT_HEADER => true,
  22.                 CURLOPT_TIMEOUT => 60,
  23.                 CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_3,
  24.                 CURLOPT_SSL_CIPHER_LIST => 'TLS_AES_128_GCM_SHA256:ECDHE-RSA-AES128-GCM-SHA256'
  25.             ];
  26.             if (strtoupper($method) === "POST") {
  27.                 $options[CURLOPT_POST] = true;
  28.                 $options[CURLOPT_POSTFIELDS] = $request;
  29.             }
  30.             curl_setopt_array($ch, $options);
  31.             $res = curl_exec($ch);
  32.             $curl_error = curl_errno($ch);
  33.             if ($curl_error) {
  34.                 $data["error"] = curl_error($ch);
  35.             } else {
  36.                 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  37.                 $data["data"] = substr($res, $header_size);
  38.                 $data["header"] = substr($res, 0, $header_size);
  39.                 $data["info"] = curl_getinfo($ch);
  40.             }
  41.             curl_close($ch);
  42.             if (!$curl_error && !empty($data['info']['url'])) {
  43.                 return $data;
  44.             }
  45.             if ($curl_error) {
  46.                 switch ($curl_error) {
  47.                     case CURLE_COULDNT_CONNECT:
  48.                         break;
  49.                     case CURLE_TOO_MANY_REDIRECTS:
  50.                     case CURLE_OPERATION_TIMEDOUT:
  51.                         return null;
  52.                     case CURLE_HTTP_RETURNED_ERROR:
  53.                         $http_code = $data["info"]["http_code"];
  54.                         switch ($http_code) {
  55.                             case 500: $this->timer("{$link} HTTP CODE: 500 (Internal Server Error)", 10);
  56.                                 break;
  57.                             case 502: $this->timer("{$link} HTTP CODE: 502 (Bad Gateway)", 10);
  58.                                 break;
  59.                             case 503: $this->timer("{$link} HTTP CODE: 503 (Service Unavailable)", 10);
  60.                                 break;
  61.                             case 521: $this->timer("{$link} HTTP CODE: 521 (Web Server Is Down)", 10);
  62.                                 break;
  63.                             case 522: $this->timer("{$link} HTTP CODE: 522 (Connection Timed Out)", 10);
  64.                                 break;
  65.                             default: return false;
  66.                         }
  67.                         break;
  68.                     default: return false;
  69.                 }
  70.             }
  71.             $retryCount++;
  72.         }
  73.         return $data;
  74.     }
  75.     public function timer($text, $seconds) {
  76.         $colors = [
  77.             "g" => "\033[1;30m", "w" => "\033[1;37m",
  78.             "r" => "\033[1;31m", "gr" => "\033[1;32m",
  79.             "y" => "\033[1;33m", "p" => "\033[1;35m", "l" => "\033[1;36m"
  80.         ];
  81.    
  82.         if ($seconds <= 0) {
  83.             return;
  84.         }
  85.         $dot = 1;
  86.         $colorKeys = array_keys($colors);
  87.         $colorIndex = 0;
  88.         for ($x = $seconds; $x > 0; $x--) {
  89.             $colorCode = $colors[$colorKeys[$colorIndex]];
  90.             $colorIndex = ($colorIndex + 1) % count($colorKeys);
  91.             if ($x < 60) {
  92.                 $timeDisplay = "$x seconds";
  93.             } elseif ($x < 3600) {
  94.                 $timeDisplay = gmdate("i:s", $x);
  95.             } else {
  96.                 $timeDisplay = gmdate("H:i:s", $x);
  97.             }
  98.             $progress = str_repeat("•", $dot);
  99.             $message = "{$colors['w']}{$text} {$timeDisplay} {$colorCode}{$progress}\033[0m";
  100.             echo $message;
  101.             $dot = ($dot % 5) + 1;
  102.             sleep(1);
  103.             echo "\r" . str_repeat(" ", strlen($message)+5) . "\r";
  104.         }
  105.     }
  106.    
  107.     public function get($link, $header = [], $follow = 0, $return = "data"){
  108.         $response = $this->curl($link, $header, "GET", $follow);
  109.         if($return == "all"){
  110.             return $response;
  111.         }
  112.         return $response[$return];
  113.     }
  114.     public function post($link, $header = [], $request = null, $follow = 1, $return = "data") {
  115.         $response = $this->curl($link, $header, "POST", $follow, $request);
  116.         if($return == "all"){
  117.             return $response;
  118.         }
  119.         return $response[$return];
  120.     }
  121.     public function getConfig($filePath = "config.json") {
  122.         if (!file_exists($filePath)) {
  123.             return [];
  124.         }
  125.         $config = file_get_contents($filePath);
  126.         if ($config === false) {
  127.             throw new RuntimeException("Failed to read configuration file: {$filePath}");
  128.         }
  129.         $decoded = json_decode($config, true);
  130.         if (json_last_error() !== JSON_ERROR_NONE) {
  131.             throw new RuntimeException("Invalid JSON format in configuration file: {$filePath}");
  132.         }
  133.         return $decoded;
  134.     }
  135.     public function saveConfig($filePath, $config = "config.json") {
  136.         $data = json_encode($config, JSON_PRETTY_PRINT);
  137.         if ($data === false) {
  138.             throw new RuntimeException("Failed to encode configuration data.");
  139.         }
  140.  
  141.         if (file_put_contents($filePath, $data) === false) {
  142.             throw new RuntimeException("Failed to write configuration file: {$filePath}");
  143.         }
  144.     }
  145.     public function remove($filename, $filePath = "config.json") {
  146.         $config = $this->getConfig($filePath);
  147.         if (isset($config[$filename])) {
  148.             unset($config[$filename]);
  149.             $this->saveConfig($filePath, $config);
  150.             echo "\033[1;32mKey '$filename' removed successfully from {$filePath}.\033[0m\n";
  151.         } else {
  152.             echo "\033[1;31mError: Key '$filename' not found in {$filePath}.\033[0m\n";
  153.         }
  154.     }
  155.     public function save($filename, $filePath = "config.json", $defaultValue = null) {
  156.         $config = $this->getConfig($filePath);
  157.         if (isset($config[$filename]) && !empty($config[$filename])) {
  158.             return $config[$filename];
  159.         }
  160.         $data = readline("\033[1;37mInput $filename: \033[0m");
  161.         if (empty($data)) {
  162.             $data = $defaultValue ?? null;
  163.         }
  164.         if ($data !== null) {
  165.             $config[$filename] = $data;
  166.             $this->saveConfig($filePath, $config);
  167.             echo "\033[1;32mKey '$filename' saved successfully to {$filePath}.\033[0m\n";
  168.         }
  169.         return $config[$filename] ?? null;
  170.     }
  171.     public function silentSave($filename, $data, $filePath = "config.json") {
  172.         if (empty($data)) {
  173.             throw new InvalidArgumentException("Data cannot be empty.");
  174.         }
  175.         $config = $this->getConfig($filePath);
  176.         $config[$filename] = $data;
  177.         $this->saveConfig($filePath, $config);
  178.         return $config[$filename];
  179.     }
  180.  
  181.     public function getSave($filename, $filePath = "config.json", $fallback = null) {
  182.         $config = $this->getConfig($filePath);
  183.         return $config[$filename] ?? $fallback;
  184.     }
  185.     public function getStr($string, $start, $end, $num = 1){
  186.       $str = explode($start, $string);
  187.       $str = explode($end, $str[$num]);
  188.       return $str[0];
  189.     }
  190. }
  191.  
  192. class Color{
  193.     public static $res = "\033[0m";
  194.     public static $bb = "\033[1;30m";
  195.     public static $bw = "\033[1;37m";
  196.     public static $br = "\033[1;31m";
  197.     public static $bg = "\033[1;32m";
  198.     public static $by = "\033[1;33m";
  199.     public static $bp = "\033[1;35m";
  200.     public static $bc = "\033[1;36m";
  201.     public static $g = "\033[1;32m";
  202.     public static $r = "\033[1;31m";
  203.     public static function banner($msg, $author){
  204.         echo Color::$bg .  "────────────────────────────────────────────────\n" . Color::$res;
  205.         echo Color::$bg .  " [".Color::$bw."  Author  ".Color::$bg."]".Color::$br." $author\n" . Color::$res;
  206.         echo Color::$bg .  " [".Color::$bw."   Note   ".Color::$bg."]".Color::$bw." This script is FREE to use!\n" . Color::$res;
  207.         echo Color::$bg .  " [".Color::$bw." Messages ".Color::$bg."]".Color::$bw." $msg\n" . Color::$res;
  208.         echo Color::$bg .  " [".Color::$bw." Support  ".Color::$bg."]".Color::$bw." Having issues? Contact me ".Color::$r."@scpwhite\n" . Color::$res;
  209.         echo Color::$bg .  " [".Color::$bw." TG Group ".Color::$bg."]".Color::$bw." https://t.me/appwebscript\n" . Color::$res;
  210.         echo Color::$bg .  " [".Color::$bw."TG Channel".Color::$bg."]".Color::$bw." https://t.me/appwebscripts\n" . Color::$res;
  211.         echo Color::$bg .  "────────────────────────────────────────────────\n" . Color::$res;
  212.            
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement