PHP 111
Heatmap.class By siosios on 17th March 2020 01:12:40 AM
  1. <?php
  2. /**
  3.  * Class HeatMap API to generate/add/update/delete content for HeatMaps.
  4.  */
  5. class HeatMap
  6. {
  7.         public $version = "0.5";
  8.         private $db;
  9.  
  10.         /**
  11.          * Stuff we want to fire when we start.
  12.          */
  13.         public function __construct()
  14.         {
  15.                 global $argv;
  16.  
  17.                 $this->db = new DB(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  18.  
  19.                 $this->mapInfo();
  20.                 $this->parseArguments($argv);
  21.         }
  22.  
  23.         /**
  24.          * Function that builds the maplist from db elements.
  25.          */
  26.  
  27.         public function mapInfo()
  28.         {
  29.  
  30.                 $query = "
  31.            SELECT
  32.                g.code,
  33.                hc.code,
  34.                hc.map,
  35.                hc.xoffset,
  36.                hc.yoffset,
  37.                hc.flipx,
  38.                hc.flipy,
  39.                hc.rotate,
  40.                hc.days,
  41.                hc.brush,
  42.                hc.scale,
  43.                hc.font,
  44.                hc.thumbw,
  45.                hc.thumbh,
  46.                hc.cropx1,
  47.                hc.cropx2,
  48.                hc.cropy1,
  49.                hc.cropy2
  50.                         FROM hlstats_codes AS g
  51.                         INNER JOIN hlstats_Heatmap_Config AS hc
  52.                         ON hc.code = g.realcode
  53.                         WHERE 1=1
  54.                         ORDER BY code ASC, code ASC, map ASC";
  55.  
  56.                 $result = $this->db->doQuery($query);
  57.         $mapInfo = [];
  58.  
  59.                 if ($this->db->numRows($result)) {
  60.  
  61.                         while ($row = $this->db->getAssoc($result)) {
  62.  
  63.                                 foreach ($row as $key => $val) {
  64.  
  65.                     $mapInfo[$row['code']][$row['map']][$key] = $val;
  66.                                 }
  67.                         }
  68.  
  69.                         Env::set('mapinfo', $mapInfo);
  70.                 }
  71.         }
  72.  
  73.     /**
  74.      * Print heat dot
  75.      *
  76.      * @param $dst_im
  77.      * @param $src_im
  78.      * @param $dst_x
  79.      * @param $dst_y
  80.      * @param $src_x
  81.      * @param $src_y
  82.      * @param $src_w
  83.      * @param $src_h
  84.      * @param $pct
  85.      * @return bool
  86.      */
  87.     private function printHeatDot($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct)
  88.     {
  89.         if (!isset($pct)) {
  90.             return false;
  91.         }
  92.  
  93.         $pct /= 100;
  94.  
  95.         // Get image width and height
  96.         $w = imagesx($src_im);
  97.         $h = imagesy($src_im);
  98.  
  99.         // Turn alpha blending off
  100.         imagealphablending($src_im, false);
  101.  
  102.         // Find the most opaque pixel in the image (the one with the smallest alpha value)
  103.         $minAlpha = 127;
  104.  
  105.         for ($x = 0; $x < $w; $x++) {
  106.  
  107.             for ($y = 0; $y < $h; $y++) {
  108.  
  109.                 $alpha = ( imagecolorat( $src_im, $x, $y ) >> 24 ) & 0xFF;
  110.  
  111.                 if ($alpha < $minAlpha) {
  112.                     $minAlpha = $alpha;
  113.                 }
  114.             }
  115.         }
  116.  
  117.         //loop through image pixels and modify alpha for each
  118.         for ($x = 0; $x < $w; $x++) {
  119.  
  120.             for ($y = 0; $y < $h; $y++) {
  121.                 //get current alpha value (represents the TANSPARENCY!)
  122.                 $colorXY = imagecolorat($src_im, $x, $y);
  123.                 $alpha = ($colorXY >> 24) & 0xFF;
  124.  
  125.                 //calculate new alpha
  126.                 if($minAlpha !== 127) {
  127.                     $alpha = 127 + 127 * $pct * ($alpha - 127) / (127 - $minAlpha);
  128.                 } else {
  129.                     $alpha += 127 * $pct;
  130.                 }
  131.  
  132.                 //get the color index with new alpha
  133.                 $alphaColorXY = imagecolorallocatealpha($src_im, ($colorXY >> 16) & 0xFF, ($colorXY >> 8) & 0xFF, $colorXY & 0xFF, $alpha);
  134.  
  135.                 //set pixel with the new color + opacity
  136.                 if (!imagesetpixel( $src_im, $x, $y, $alphaColorXY)) {
  137.                     return false;
  138.                 }
  139.             }
  140.         }
  141.  
  142.         // The image copy
  143.         return imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
  144.     }
  145.  
  146.     /**
  147.      * Generate HeatMap
  148.      *
  149.      * @param $code
  150.      * @param $map
  151.      * @param $mode
  152.      * @return bool
  153.      */
  154.     public function generate($code, $map, $mode)
  155.     {
  156.         // generatemap($map, $code, "Total Kills", "HlstatsX:CE")
  157.         $this->buildQuery($code, $map);
  158.  
  159.         $mapinfo = Env::get('mapinfo');
  160.         $map_query = Env::get('map_query');
  161.         $disable_cache = Env::get('disable_cache');
  162.  
  163.         // See if we need to rotate the map or not.
  164.         $rotate = $mapinfo[$code][$map]['rotate'];
  165.         $timestamp = time();
  166.  
  167.         // Fix the last part of your path.
  168.         $path = HLXCE_WEB . "/hlstatsimg/codes/" . $mapinfo[$code][$map]['code'] . "/heatmaps";
  169.         Show::Event("PATH", $path, 3);
  170.  
  171.         // Does the source image exists? else there is no idea to spend resources on it.
  172.         if (!file_exists(dirname(__FILE__) . "/src/" . $mapinfo[$code][$map]['code'] . "/" . $map . ".jpg")) {
  173.  
  174.             Show::Event("FILE", dirname(__FILE__) . "/src/" . $mapinfo[$code][$map]['code'] . "/" . $map . ".jpg doesn't exists", 3);
  175.             return false;
  176.         }
  177.  
  178.         // Check that the dir exists, else try to create it.
  179.         if (!is_dir($path)) {
  180.  
  181.             if (!@mkdir($path)) {
  182.                 Show::Event("PREPARE", "Couln't create outputfolder: $path", 1);
  183.             }
  184.         }
  185.  
  186.         // Check if we have cached info, then we should work from that instead.
  187.         if (is_dir(CACHE_DIR . "/$code")) {
  188.  
  189.             if ($handle = opendir(CACHE_DIR. "/$code")) {
  190.  
  191.                 while (false !== ($file = readdir($handle))) {
  192.  
  193.                     if ($file != "." && $file != ".." && preg_match(str_replace("\$","\\\$","/${map}_(\d+).png/i"), $file, $matches)) {
  194.  
  195.                         $cache_file = CACHE_DIR . "/$code/$file";
  196.                         $oldTimestamp = $matches[1];
  197.  
  198.                         // unless it's over 30 days old cache file, then we delete it and go from 0 again.
  199.                         if (floor((time() - $oldTimestamp) / 86400 > 30)) {
  200.  
  201.                             $obsolite_cache = true;
  202.                             Show::Event("CACHE", "Cache file is obsolite, " . floor((time() - $oldTimestamp) / 86400) . " days old. Generating from scratch", 1);
  203.                         }
  204.  
  205.                         // If we called with --disable-cache we want to clean up and then regen from our start.
  206.                         if ($disable_cache || isset($obsolite_cache)) {
  207.  
  208.                             $disable_cache = true;
  209.  
  210.                             if (file_exists($cache_file)) {
  211.                                 unlink($cache_file);
  212.                             }
  213.  
  214.                         } else {
  215.  
  216.                             Show::Event("CACHE","Found cached file ($file), we will use timestamp $oldTimestamp instead", 1);
  217.                             $find = '/.*AND hef.eventTime >= FROM_UNIXTIME\([0-9]+\).*/i';
  218.                             $replace = '                        AND hef.eventTime > FROM_UNIXTIME(' . $oldTimestamp . ')';
  219.                             $map_query = preg_replace($find, $replace, $map_query);
  220.                         }
  221.                     }
  222.                 }
  223.  
  224.                 closedir($handle);
  225.             }
  226.  
  227.         } else {
  228.  
  229.             if (!@mkdir(CACHE_DIR . "/$code")) {
  230.                 Show::Event("CACHE", "Can't create cache_dir: " . CACHE_DIR . "/$code", 1);
  231.             }
  232.         }
  233.  
  234.         $result = $this->db->doQuery($map_query);
  235.         $num_kills = $this->db->numRows($result);
  236.  
  237.         if (!$num_kills) {
  238.  
  239.             Show::Event("IGNORE", "code: $code, Map: $map, Kills: $num_kills, (to few kills)", 1);
  240.             return false;
  241.         }
  242.  
  243.         $firstData = time();
  244.  
  245.         $img = imagecreatefromjpeg("./src/" . $mapinfo[$code][$map]['code'] . "/" . $map . ".jpg");
  246.         imagealphablending($img, true);
  247.         imagesavealpha($img, true);
  248.  
  249.         if (isset($cache_file) && !$disable_cache) {
  250.             $overlay = imagecreatefrompng($cache_file);
  251.         } else {
  252.             $overlay = imagecreatetruecolor(imagesx($img), imagesy($img));
  253.         }
  254.  
  255.         imagealphablending($overlay, true);
  256.         imagesavealpha($overlay, true);
  257.  
  258.         $brush = imagecreatefrompng("./src/brush_" . $mapinfo[$code][$map]['brush'] . ".png");
  259.         $brushSize = ($mapinfo[$code][$map]['brush'] == "large") ? 33 : 17;
  260.  
  261.         $white = imagecolorallocate($overlay, 255, 255, 255);
  262.         $black = imagecolorallocate($overlay, 0, 0, 0);
  263.  
  264.         imagefill($overlay, 0, 0, $black);
  265.         imagecolortransparent($overlay, $black);
  266.  
  267.         $num_kills = ($num_kills) ? $num_kills : 1;
  268.  
  269.         Show::Event("CREATE", "code: $code, Map: $map, Kills: $num_kills", 1);
  270.         $opacity = intval((500 / $num_kills) * 100);
  271.  
  272.         if ($opacity > 40) {
  273.             $opacity = 40;
  274.         }
  275.  
  276.         if ($opacity < 1) {
  277.             $opacity = 2;
  278.         }
  279.  
  280.         $max_red = 0;
  281.         $i = 0;
  282.  
  283.         while ($row = $this->db->getAssoc($result)) {
  284.  
  285.             if ($row['eventTime'] < $firstData) {
  286.                 $firstData = $row['eventTime'];
  287.             }
  288.  
  289.             if ($mapinfo[$code][$map]['flipx']) {
  290.                 $row['pos_x'] = $row['pos_x'] * -1;
  291.             }
  292.  
  293.             if ($mapinfo[$code][$map]['flipy']) {
  294.                 $row['pos_y'] = $row['pos_y'] * -1;
  295.             }
  296.  
  297.             $x = ($row['pos_x'] + $mapinfo[$code][$map]['xoffset']) / $mapinfo[$code][$map]['scale'];
  298.             $y = ($row['pos_y'] + $mapinfo[$code][$map]['yoffset']) / $mapinfo[$code][$map]['scale'];
  299.  
  300.             $rgb = imagecolorat($overlay, $x, $y);
  301.             $colors = imagecolorsforindex($overlay, $rgb);
  302.  
  303.             if ($colors['red'] > $max_red) $max_red = $colors['red'];
  304.  
  305.             if ($colors['red'] <= 200) {
  306.                 // Rotate the image
  307.                 if ($rotate) {
  308.                     $this->printHeatDot($overlay, $brush, $y - ($brushSize / 2), $x - ($brushSize / 2), 0, 0, $brushSize, $brushSize, $opacity);
  309.                 } else {
  310.                     $this->printHeatDot($overlay, $brush, $x - ($brushSize / 2), $y - ($brushSize / 2), 0, 0, $brushSize, $brushSize, $opacity);
  311.                 }
  312.             }
  313.         }
  314.  
  315.         imagedestroy($brush);
  316.  
  317.         $colorArr = array();
  318.         $colors = array(0, 0, 255);
  319.  
  320.         for ($line = 0; $line < 128; ++$line) {
  321.  
  322.             $colors = array(0, $colors[1] + 2, $colors[2] -2);
  323.             $colorArr[$line] = $colors;
  324.         }
  325.  
  326.         for ($line = 128; $line < 255; ++$line) {
  327.  
  328.             $colors = array($colors[0] + 2, $colors[1] -2, 0);
  329.             $colorArr[$line] = $colors;
  330.         }
  331.  
  332.         for ($x = 0; $x < imagesx($overlay); ++$x) {
  333.  
  334.             for ($y = 0; $y < imagesy($overlay); ++$y) {
  335.  
  336.                 $index = imagecolorat($overlay, $x, $y);
  337.                 $rgb = imagecolorsforindex($overlay, $index);
  338.                 $alpha = ( imagecolorat( $overlay, $x, $y ) >> 24 ) & 0xFF;
  339.  
  340.                 $color = imagecolorallocatealpha($img, $colorArr[$rgb['red']][0], $colorArr[$rgb['red']][1], $colorArr[$rgb['red']][2], 127 - ($rgb['red'] / 2));
  341.  
  342.                 if (!imagesetpixel($img, $x, $y, $color)) {
  343.                     echo ".";
  344.                 }
  345.             }
  346.         }
  347.  
  348.         if ($mapinfo[$code][$map]['cropy2'] > 0 && $mapinfo[$code][$map]['cropy2'] > 0) {
  349.  
  350.             $temp = imagecreatetruecolor($mapinfo[$code][$map]['cropx2'], $mapinfo[$code][$map]['cropy2']);
  351.             imagecopy($temp, $img, 0, 0, $mapinfo[$code][$map]['cropx1'], $mapinfo[$code][$map]['cropy1'], $mapinfo[$code][$map]['cropx2'], $mapinfo[$code][$map]['cropy2']);
  352.             imagedestroy($img);
  353.  
  354.             $img = imagecreatetruecolor(imagesx($temp), imagesy($temp));
  355.             imagecopy($img, $temp, 0, 0, 0, 0, imagesx($temp), imagesy($temp));
  356.             imagedestroy($temp);
  357.         }
  358.  
  359.         if ($mapinfo[$code][$map]['thumbw'] > 0 && $mapinfo[$code][$map]['thumbh'] > 0) {
  360.  
  361.             $thumb = imagecreatetruecolor(imagesx($img) * $mapinfo[$code][$map]['thumbw'], imagesy($img) * $mapinfo[$code][$map]['thumbh']);
  362.             imagecopyresampled($thumb, $img, 0, 0, 0, 0, imagesx($thumb), imagesy($thumb), imagesx($img), imagesy($img));
  363.             imagejpeg($thumb, $path . "/" . $map . "-" . $mode . "-thumb.jpg", 100);
  364.             imagedestroy($thumb);
  365.         }
  366.  
  367.         $img = self::drawHud($img, $map, "HLX:CE", "Total Kills", $num_kills, $firstData);
  368.  
  369.         $return = false;
  370.  
  371.         if (imagejpeg($img, $path . "/" . $map . "-" . $mode . ".jpg", 100)) {
  372.             $return = true;
  373.         }
  374.  
  375.         if (imagepng($overlay, CACHE_DIR . "/$code/${map}_${timestamp}.png", 9)) {
  376.             $return = true;
  377.         }
  378.  
  379.         imagedestroy($overlay);
  380.  
  381.         // Clean upc cache file
  382.         if (isset($cache_file) && file_exists($cache_file)) {
  383.             unlink(CACHE_DIR . "/$code/${map}_${oldTimestamp}.png");
  384.         }
  385.  
  386.         imagedestroy($img);
  387.  
  388.         return $return;
  389.     }
  390.  
  391.     /**
  392.      * Build map SQL query
  393.      *
  394.      * @param $code
  395.      * @param $map
  396.      */
  397.     public function buildQuery($code, $map)
  398.     {
  399.         $mapInfo = Env::get('mapinfo');
  400.         Env::set('code', $code);
  401.  
  402.         $ignore_infected = Env::get('ignore_infected');
  403.         $timeScope = (time() - 60*60*24*$mapInfo[$code][$map]['days']);
  404.  
  405.         $map_query = "
  406.            SELECT
  407.                'frag' AS killtype,
  408.                hef.id,
  409.                hef.map,
  410.                hs.code,    
  411.                hef.eventTime,
  412.                hef.pos_x,
  413.                hef.pos_y
  414.            FROM
  415.                 hlstats_Events_Frags as hef,
  416.                 hlstats_Servers as hs
  417.            WHERE 1=1
  418.            AND hef.map = {$map}
  419.            AND hs.serverId = hef.serverId
  420.            AND hs.code = {$code}
  421.            AND hef.pos_x IS NOT NULL
  422.            AND hef.pos_y IS NOT NULL
  423.            AND hef.eventTime >= FROM_UNIXTIME(' . $timeScope . ')";
  424.  
  425.         if ($ignore_infected) {
  426.             $map_query.= " AND hef.victimRole != 'infected'";
  427.         }
  428.  
  429.         $map_query.= " LIMIT 1000
  430.            UNION ALL
  431.            SELECT
  432.                'teamkill' AS killtype,
  433.                hef.id,
  434.                hef.map,
  435.                hs.code,
  436.                hef.eventTime,
  437.                hef.pos_x,
  438.                hef.pos_y
  439.            FROM
  440.                hlstats_Events_Teamkills as hef,
  441.                hlstats_Servers as hs
  442.            WHERE 1=1
  443.            AND hef.map = {$map}
  444.            AND hs.serverId = hef.serverId
  445.            AND hs.code = {$code}
  446.            AND hef.pos_x IS NOT NULL
  447.            AND hef.pos_y IS NOT NULL
  448.            AND hef.eventTime >= FROM_UNIXTIME({$timeScope})
  449.            LIMIT 1000";
  450.  
  451.         Env::set('map_query', $map_query);
  452.         Show::Event("SQL", $map_query, 3);
  453.     }
  454.  
  455.     /**
  456.      * Draw HUD
  457.      *
  458.      * @param $img
  459.      * @param $map
  460.      * @param $heatmapname
  461.      * @param $method
  462.      * @param $num_kills
  463.      * @param $firstdata
  464.      * @return false|resource
  465.      */
  466.     private function drawHud($img, $map, $heatmapname, $method, $num_kills, $firstdata)
  467.     {
  468.         $mapInfo = Env::get('mapinfo');
  469.         $code = Env::get('code');
  470.  
  471.         // Resize the image according to your  settings
  472.         $img = $this->resize($img);
  473.  
  474.         $hudText = [
  475.             strtoupper($map) . " - " . strtoupper($heatmapname) . " HEATMAP - " . strtoupper($method),
  476.             date("m/d/y", intval(time() - 60*60*24*30)) . " - " . date("m/d/y", time()),
  477.             "Generated: " . date("Y-m-d H:i:s"),
  478.             HUD_URL
  479.         ];
  480.  
  481.         Show::Event("HUD", "Creating Overlay HUD", 2);
  482.  
  483.         $hudX = imagesx($img);
  484.         $hudY = intval(intval($mapInfo[$code][$map]['font'] + 4) * intval(count($hudText) + 1) + 8);
  485.  
  486.         $hud = imagecreatetruecolor(imagesx($img), imagesy($img)) or die ('Cannot Initialize new GD image stream');
  487.         imagesavealpha($hud, true);
  488.  
  489.         $trans_colour = imagecolorallocatealpha($hud, 0, 0, 0, 127);
  490.         $black = imagecolorallocatealpha($hud, 0, 0, 0, 90);
  491.  
  492.         imagefill($hud, 0, 0, $trans_colour);
  493.         imagefilledrectangle($hud, 0, 0, imagesx($img) - 1, imagesy($img) - 1, $black);
  494.  
  495.         $font = "./DejaVuSans.ttf";
  496.  
  497.         // Copy the hud to the top of the image.
  498.         imagecopy($img, $hud, 0, 0, 0, 0, $hudX, $hudY);
  499.  
  500.         //array imagettftext  ( resource $image  , float $size  , float $angle  , int $x  , int $y  , int $color  , string $fontfile  , string $text  )
  501.         $i = 1;
  502.  
  503.         foreach ($hudText as $text) {
  504.  
  505.             imagettftext($img,
  506.                 $mapInfo[$code][$map]['font'],
  507.                 0,
  508.                 10,
  509.                 intval(intval($mapInfo[$code][$map]['font'] + 4) * $i + 8),
  510.                 imagecolorallocate($img, 255, 255, 255),
  511.                 $font,
  512.                 $text
  513.             );
  514.  
  515.             $i++;
  516.         }
  517.  
  518.         imagedestroy($hud);
  519.  
  520.         Show::Event("HUD", "Done...", 2);
  521.  
  522.         return $img;
  523.     }
  524.  
  525.     /**
  526.      * Resize Image
  527.      *
  528.      * @param $img
  529.      * @return false|resource
  530.      */
  531.     private function resize($img)
  532.     {
  533.         switch (OUTPUT_SIZE) {
  534.             case "small":
  535.                 $newWidth = 800;
  536.                 $newHeight = 600;
  537.                 break;
  538.             case "medium":
  539.                 $newWidth = 1024;
  540.                 $newHeight = 768;
  541.                 break;
  542.             case "large":
  543.                 $newWidth = 1280;
  544.                 $newHeight = 1024;
  545.                 // As for now we don't do anything since this is default size
  546.                 return $img;
  547.                 break;
  548.             default:
  549.                 $newWidth = 1024;
  550.                 $newHeight = 768;
  551.         }
  552.  
  553.         Show::Event("RESIZE", "Adjusting Heatmap to current setting: " . OUTPUT_SIZE, 2);
  554.  
  555.         $resized = imagecreatetruecolor($newWidth, $newHeight);
  556.         imagecopyresized($resized, $img, 0, 0, 0, 0, $newWidth, $newHeight, imagesx($img), imagesy($img));
  557.  
  558.         imagedestroy($img);
  559.  
  560.         Show::Event("RESIZE", "Done...", 2);
  561.  
  562.         return $resized;
  563.     }
  564.  
  565.     /**
  566.      * Command line arguments
  567.      *
  568.      * @param $argv
  569.      * @return array
  570.      */
  571.     private function arguments($argv)
  572.     {
  573.         $_ARG = [];
  574.  
  575.         foreach ($argv as $arg) {
  576.  
  577.             if (preg_match('/--[a-zA-Z0-9]*=.*/', $arg)) {
  578.  
  579.                 $str = explode('=', $arg);
  580.                 $arg = '';
  581.                 $key = preg_replace('/--/', '', $str[0]);
  582.  
  583.                 for ( $i = 1; $i < count($str); $i++ ) {
  584.  
  585.                     $arg .= $str[$i];
  586.                 }
  587.  
  588.                 $_ARG[$key] = $arg;
  589.  
  590.             } elseif(preg_match('/-[a-zA-Z0-9]/', $arg)) {
  591.  
  592.                 $arg = preg_replace('/-/', '', $arg);
  593.                 $_ARG[$arg] = 'true';
  594.             }
  595.         }
  596.  
  597.         return $_ARG;
  598.     }
  599.  
  600.     /**
  601.      * Parse command line arguments
  602.      *
  603.      * @param $argv
  604.      */
  605.     private function parseArguments($argv)
  606.     {
  607.         $mapInfo = Env::get('mapinfo');
  608.         $cache = false;
  609.         $args = $this->arguments($argv);
  610.  
  611.         if (isset($args['code'])) {
  612.  
  613.             if (!isset($mapInfo[$args['code']])) {
  614.  
  615.                 Show::Event("ERROR", "code: " . $args['code'] . " doesn't exists, escaping", 1);
  616.                 exit;
  617.             }
  618.  
  619.             if (isset($args['map'])) {
  620.  
  621.                 if (!isset($mapInfo[$args['code']][$args['map']])) {
  622.  
  623.                     Show::Event("ERROR", "code: " . $args['code'] . " Map: " . $args['map'] . " doesn't exists, escaping", 1);
  624.                     exit;
  625.                 }
  626.  
  627.                 $tmp[$args['code']][$args['map']] = $mapInfo[$args['code']][$args['map']];
  628.                 Show::Event("ARGS", "--code=" . $args['code'], 2);
  629.                 Show::Event("ARGS", "--map=" . $args['map'], 2);
  630.  
  631.             } else {
  632.  
  633.                 $tmp[$args['code']] = $mapInfo[$args['code']];
  634.                 Show::Event("ARGS", "--code=" . $args['code'], 2);
  635.             }
  636.  
  637.         } else {
  638.  
  639.             $visible = '';
  640.             $query = "SELECT code FROM ${DB_PREFIX}_codes WHERE hidden='0'";
  641.             $result = $this->db->doQuery($query);
  642.  
  643.             if ($this->db->numRows($result)) {
  644.  
  645.                 while ($row = $this->db->getAssoc($result)) {
  646.  
  647.                     foreach ($row as $key => $val) {
  648.  
  649.                         if (isset($mapInfo[$val])) {
  650.  
  651.                             $visible .= "$val, ";
  652.                             $tmp[$val] = $mapInfo[$val];
  653.                         }
  654.                     }
  655.                 }
  656.             }
  657.  
  658.             Show::Event("codeS", substr($visible, 0, -2), 2);
  659.         }
  660.  
  661.         if (isset($tmp)) {
  662.             $mapInfo = $tmp;
  663.         }
  664.  
  665.         if (isset($args['disablecache'])) {
  666.  
  667.             $cache = true;
  668.             Show::Event("ARGS", "--disable-cache=true", 2);
  669.  
  670.         } else {
  671.  
  672.             $cache = false;
  673.             Show::Event("ARGS", "--disable-cache=false", 2);
  674.         }
  675.  
  676.         if (isset($args['ignoreinfected'])) {
  677.  
  678.             $ignore_infected = true;
  679.             Show::Event("ARGS", "--ignore-infected=true", 2);
  680.  
  681.         } else {
  682.  
  683.             $ignore_infected = false;
  684.             Show::Event("ARGS", "--ignore-infected=false", 2);
  685.         }
  686.  
  687.         Env::set('mapinfo', $mapInfo);
  688.         Env::set('disable_cache', $cache);
  689.         Env::set('ignore_infected', $ignore_infected);
  690.     }
  691. }
  692.  
  693. /**
  694.  * Class DB
  695.  */
  696. class DB
  697. {
  698.         private $link;
  699.  
  700.         public function __construct($host, $user, $pass, $dbName)
  701.         {
  702.                 $this->link = mysqli_connect($host, $user, $pass, $dbName);
  703.                 Show::Event("DB", "Connected to " . DB_NAME . " as " . DB_USER . "@" . DB_HOST, 1);
  704.         }
  705.  
  706.         public function doQuery($query)
  707.         {
  708.                 return mysqli_query($this->link, $query);
  709.         }
  710.  
  711.         public function getAssoc($result)
  712.         {
  713.                 return mysqli_fetch_assoc($result);
  714.         }
  715.  
  716.         public function numRows($result)
  717.         {
  718.                 return mysqli_num_rows($result);
  719.     }
  720. }
  721.  
  722. /**
  723.  * Class Env holds Environmental vars that can be used anywhere in the code
  724.  * For example the database object to perform mysql stuff
  725.  */
  726. class Env
  727. {
  728.     private static $data = [];
  729.  
  730.     /**
  731.      * Add the specified key=>value to the environmental data array
  732.      *
  733.      * @param string $key this is the identifier of the value you are adding
  734.      * @param string|array $value The value to add into the array
  735.      */
  736.     public static function set($key, $value)
  737.     {
  738.         self::$data[$key] = $value;
  739.     }
  740.  
  741.     /**
  742.      * Gets the current value from the data array
  743.      *
  744.      * @param string $key The key to lookup in the array
  745.      * @return mixed null if the key cannot be found, or the value that was stored in the array
  746.      */
  747.     public static function get($key)
  748.     {
  749.         return array_key_exists($key, self::$data) ? self::$data[$key] : null;
  750.     }
  751. }
  752.  
  753. /**
  754.  * Class Show for console output
  755.  */
  756. class Show
  757. {
  758.         public static function Event ($type, $text, $runLevel)
  759.         {
  760.                 if ($runLevel <= DEBUG) {
  761.                         print date("Y-m-d H:i:s") . "\t\t$type: $text\n";
  762.                 }    
  763.         }
  764. }

N/U PasteBin е място за публикуване на код или текст за по-лесно отстраняване на грешки.

Влез или се Регистрай за да редактираш, изтриваш или преглеждаш хронология на твоето публикувано съдържание

Raw Paste

Влез или се Регистрирай за да редактираш или задържиш това съдържание.