Dalexhd

cache icons

Sep 6th, 2018
444
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.23 KB | None | 0 0
  1. <?php
  2.     /********************************
  3.  
  4.     Author: Tymoteusz `Razor Meister` Bartnik
  5.  
  6.     Contact: [email protected]
  7.  
  8.     Function: cache_avatars()
  9.  
  10.     ********************************/
  11. use Jenssegers\ImageHash\ImageHash;
  12. use Jenssegers\ImageHash\Implementations\DifferenceHash;
  13. class cache_icons
  14. {
  15.     private static $name;
  16.     private static $cfg;
  17.     private static $ip;
  18.     private static $icons_cache;
  19.  
  20.     static public function construct($event_name)
  21.     {
  22.         global $cfg, $connect;
  23.         self::$cfg = $cfg[$event_name];
  24.         self::$name = $event_name;
  25.         self::$ip = $connect['IP'];
  26.     }
  27.  
  28.     static private function get_iconId($icon) {
  29.  
  30.         if($icon < 0)
  31.         {
  32.             $icon = sprintf('%u', $icon & 0xffffffff);
  33.         }
  34.         return $icon;
  35.     }
  36.  
  37.     static private function update_data($data, $icons_cache)
  38.     {
  39.         global $query_sql, $clients;
  40.  
  41.         $update = false;
  42.         foreach ($data as $icon_id => $iconData) {
  43.             if ($iconData['action'] == "insert") {
  44.                 $result = $query_sql->query("SELECT `icon_id`, `icon_hash` FROM `server_icons` WHERE `icon_id` = '".$icon_id."'");
  45.                 if ($result->rowCount() == 0) {
  46.                     $update = true;
  47.                     file_put_contents(self::$cfg['icons_path'] . $iconData['image_hash'] . ".png", base64_decode($iconData['base_64']));
  48.                     $query_sql->exec("INSERT INTO `server_icons`(`icon_id`, `icon_hash`) VALUES ('" . $icon_id . "','" . $iconData['image_hash'] . "')");
  49.                     $icons_cache[] = $icon_id;
  50.                 }
  51.             }elseif ($iconData['action'] == "delete") {
  52.                 $key = array_search($icon_id, $icons_cache);
  53.                 $result = $query_sql->query("SELECT `icon_id`, `icon_hash` FROM `server_icons` WHERE `icon_id` = '".$icon_id."'");
  54.                 if ($result->rowCount() > 0) {
  55.                     $result = $result->fetch(PDO::FETCH_ASSOC);
  56.                     $result_2 = $query_sql->query("SELECT `icon_hash` FROM `server_icons` WHERE `icon_hash` = '".$result['icon_hash']."'");
  57.                     $update = true;
  58.                     $query_sql->exec("DELETE FROM `server_icons` WHERE `icon_id`='".$icon_id."'");
  59.                     if ($result_2->rowCount() == 1) {
  60.                         unlink(self::$cfg['icons_path'].$result['icon_hash'].".png");
  61.                         unset($icons_cache[$key]);
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.         if ($update) {
  67.             file_put_contents("include/cache/icons.txt", json_encode($icons_cache));
  68.         }
  69.     }
  70.     static public function before_clients()
  71.     {
  72.         global $query, $logs_manager, $all_instances, $event_info;
  73.  
  74.         $sftp = new \phpseclib\Net\SFTP(self::$ip);
  75.         $hasher = new ImageHash(new DifferenceHash());
  76.  
  77.         $cache = file_get_contents(self::$cfg['file']);
  78.         $icons_cache = json_decode(($cache == "" ? "[]" : $cache), true);
  79.  
  80.         $file_list = array();
  81.         if (!$sftp->login(self::$cfg['remote_credentials']['user'], base64_decode(self::$cfg['remote_credentials']['password']))) {
  82.             $logs_manager::write_info("[".self::$name."] - error while connecting to server: ".self::$ip.".");
  83.             echo '> Por favor revisa las credenciales remotas!' . PHP_EOL;
  84.             exit();
  85.         }else{
  86.             $unfiltered_file_list = $sftp->rawlist(self::$cfg['remote_path']);
  87.             $filtered_remote_ids = array_map(function ($val) {
  88.                 if ($val !== 0) {
  89.                     return self::get_iconId(explode("_", $val)[1]);
  90.                 }
  91.             }, array_keys(
  92.                 array_filter($unfiltered_file_list, function ($data, $file_name) {
  93.                     if ($data['type'] == 1 && substr($file_name, 0, 5) == 'icon_') {
  94.                         return true;
  95.                     }
  96.                 }, ARRAY_FILTER_USE_BOTH)
  97.             ));
  98.  
  99.             $delete = array_values(array_diff($icons_cache, $filtered_remote_ids));
  100.             $insert = array_values(array_diff($filtered_remote_ids, $icons_cache));
  101.  
  102.  
  103.             if (count($delete) > 0) {
  104.                 foreach ($delete as $key => $icon_id) {
  105.                     $cache_key = array_search($icon_id, $icons_cache);
  106.                     $action = "delete";
  107.                     $file_list[$icon_id] = compact('action');
  108.                 }
  109.             }
  110.  
  111.             if (count($insert) > 0) {
  112.                 foreach ($insert as $key => $file_name) {
  113.                     $base_64 = base64_encode($sftp->get(self::$cfg['remote_path']."icon_".$file_name));
  114.                     $image_hash = md5($hasher->hash($base_64));
  115.                     $icon_id = self::get_iconId($file_name);
  116.                     $action = "insert";
  117.                     $file_list[$icon_id] = compact('base_64', 'image_hash', 'action');
  118.                 }
  119.             }
  120.         }
  121.         self::update_data($file_list, $icons_cache);
  122.     }
  123. }
  124. ?>
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment