Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /********************************
- Author: Tymoteusz `Razor Meister` Bartnik
- Contact: [email protected]
- Function: cache_avatars()
- ********************************/
- use Jenssegers\ImageHash\ImageHash;
- use Jenssegers\ImageHash\Implementations\DifferenceHash;
- class cache_icons
- {
- private static $name;
- private static $cfg;
- private static $ip;
- private static $icons_cache;
- static public function construct($event_name)
- {
- global $cfg, $connect;
- self::$cfg = $cfg[$event_name];
- self::$name = $event_name;
- self::$ip = $connect['IP'];
- }
- static private function get_iconId($icon) {
- if($icon < 0)
- {
- $icon = sprintf('%u', $icon & 0xffffffff);
- }
- return $icon;
- }
- static private function update_data($data, $icons_cache)
- {
- global $query_sql, $clients;
- $update = false;
- foreach ($data as $icon_id => $iconData) {
- if ($iconData['action'] == "insert") {
- $result = $query_sql->query("SELECT `icon_id`, `icon_hash` FROM `server_icons` WHERE `icon_id` = '".$icon_id."'");
- if ($result->rowCount() == 0) {
- $update = true;
- file_put_contents(self::$cfg['icons_path'] . $iconData['image_hash'] . ".png", base64_decode($iconData['base_64']));
- $query_sql->exec("INSERT INTO `server_icons`(`icon_id`, `icon_hash`) VALUES ('" . $icon_id . "','" . $iconData['image_hash'] . "')");
- $icons_cache[] = $icon_id;
- }
- }elseif ($iconData['action'] == "delete") {
- $key = array_search($icon_id, $icons_cache);
- $result = $query_sql->query("SELECT `icon_id`, `icon_hash` FROM `server_icons` WHERE `icon_id` = '".$icon_id."'");
- if ($result->rowCount() > 0) {
- $result = $result->fetch(PDO::FETCH_ASSOC);
- $result_2 = $query_sql->query("SELECT `icon_hash` FROM `server_icons` WHERE `icon_hash` = '".$result['icon_hash']."'");
- $update = true;
- $query_sql->exec("DELETE FROM `server_icons` WHERE `icon_id`='".$icon_id."'");
- if ($result_2->rowCount() == 1) {
- unlink(self::$cfg['icons_path'].$result['icon_hash'].".png");
- unset($icons_cache[$key]);
- }
- }
- }
- }
- if ($update) {
- file_put_contents("include/cache/icons.txt", json_encode($icons_cache));
- }
- }
- static public function before_clients()
- {
- global $query, $logs_manager, $all_instances, $event_info;
- $sftp = new \phpseclib\Net\SFTP(self::$ip);
- $hasher = new ImageHash(new DifferenceHash());
- $cache = file_get_contents(self::$cfg['file']);
- $icons_cache = json_decode(($cache == "" ? "[]" : $cache), true);
- $file_list = array();
- if (!$sftp->login(self::$cfg['remote_credentials']['user'], base64_decode(self::$cfg['remote_credentials']['password']))) {
- $logs_manager::write_info("[".self::$name."] - error while connecting to server: ".self::$ip.".");
- echo '> Por favor revisa las credenciales remotas!' . PHP_EOL;
- exit();
- }else{
- $unfiltered_file_list = $sftp->rawlist(self::$cfg['remote_path']);
- $filtered_remote_ids = array_map(function ($val) {
- if ($val !== 0) {
- return self::get_iconId(explode("_", $val)[1]);
- }
- }, array_keys(
- array_filter($unfiltered_file_list, function ($data, $file_name) {
- if ($data['type'] == 1 && substr($file_name, 0, 5) == 'icon_') {
- return true;
- }
- }, ARRAY_FILTER_USE_BOTH)
- ));
- $delete = array_values(array_diff($icons_cache, $filtered_remote_ids));
- $insert = array_values(array_diff($filtered_remote_ids, $icons_cache));
- if (count($delete) > 0) {
- foreach ($delete as $key => $icon_id) {
- $cache_key = array_search($icon_id, $icons_cache);
- $action = "delete";
- $file_list[$icon_id] = compact('action');
- }
- }
- if (count($insert) > 0) {
- foreach ($insert as $key => $file_name) {
- $base_64 = base64_encode($sftp->get(self::$cfg['remote_path']."icon_".$file_name));
- $image_hash = md5($hasher->hash($base_64));
- $icon_id = self::get_iconId($file_name);
- $action = "insert";
- $file_list[$icon_id] = compact('base_64', 'image_hash', 'action');
- }
- }
- }
- self::update_data($file_list, $icons_cache);
- }
- }
- ?>
Advertisement