Edu0221220

Untitled

Jul 15th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // app/vendor/mgp25/instagram-php/src/Client.php
  2.  
  3. class Client
  4. {
  5.     ...
  6.     ...
  7.     ...
  8.  
  9.     public function setToken($value)
  10.     {
  11.         $cookie = $this->getCookie('csrftoken', 'i.instagram.com');
  12.         if ($cookie === null) {
  13.             return false;
  14.         }
  15.  
  16.         $cookie->setValue($value);
  17.  
  18.         return true;
  19.     }
  20.  
  21.     ...
  22.     ...
  23.     ...
  24. }
  25.  
  26. // app/vecdor/mgp25/instagram-php/src/Instagram.php
  27.  
  28. class Instagram implements ExperimentsInterface
  29. {
  30.  
  31.     ...
  32.     ...
  33.     ...
  34.  
  35.     public function getToken() {
  36.         return $this->client->getToken();
  37.     }
  38.  
  39.     public function checkpointSelectChoice($api_path, $choice) {
  40.         return $this->request($api_path)
  41.             ->setNeedsAuth(false)
  42.             ->addPost('choice', $choice)
  43.             ->getDecodedResponse();
  44.     }
  45.  
  46.     public function checkpointLogin($username, $password, $token, $account_id, $api_path, $security_code, $appRefreshInterval = 1800) {
  47.         $this->_setUser($username, $password);
  48.         $this->_sendPreLoginFlow();
  49.  
  50.         $this->client->setToken($token);
  51.  
  52.         $response = $this->request($api_path)
  53.             ->setNeedsAuth(false)
  54.             ->addPost('security_code', $security_code)
  55.             ->getResponse(new Response\LoginResponse());
  56.  
  57.         $this->_updateLoginState($response);
  58.         $this->_sendLoginFlow(true, $appRefreshInterval);
  59.  
  60.         return $response;
  61.     }
  62.  
  63.     ...
  64.     ...
  65.     ...
  66.  
  67. }
  68.  
  69. // app/controllers/AccountController.php
  70.  
  71. class AccountController extends Controller
  72. {
  73.  
  74.     ...
  75.     ...
  76.     ...
  77.  
  78.     private function save()
  79.     {
  80.  
  81.         ...
  82.         ...
  83.         ...
  84.  
  85.         try {
  86.             $login_resp = $Instagram->login($username, $password);
  87.  
  88.             if ($login_resp !== null && $login_resp->isTwoFactorRequired()) {
  89.                 $this->resp->result = 2;
  90.                 $this->resp->twofa_required = true;
  91.                 $this->resp->msg = __(
  92.                     "Enter the code sent to your number ending in %s",
  93.                     $login_resp->getTwoFactorInfo()->getObfuscatedPhoneNumber());
  94.                 $this->resp->identifier = $login_resp->getTwoFactorInfo()->getTwoFactorIdentifier();
  95.  
  96.                 $_SESSION["2FA_".$this->resp->identifier] = [
  97.                     "username" => $username,
  98.                     "passhash" => $passhash,
  99.                     "proxy" => $proxy
  100.                 ];
  101.             } else if ($login_resp) {
  102.                 $logged_in = true;
  103.             }
  104.         } catch (InstagramAPI\Exception\CheckpointRequiredException $e) {
  105.             if ($e->hasResponse() && $e->getResponse()->isCheckpointUrl()) {
  106.                 $url = $e->getResponse()->getCheckpointUrl();
  107.                 if (strpos($url, "dismiss") === false) {
  108.                     $this->resp->msg = __("debug 1");
  109.                 }
  110.                 else {
  111.                     $this->resp->msg = __("debug 2");
  112.                 }
  113.             }
  114.             else {
  115.                 $this->resp->msg = __("Oops! Something went wrong. Please try again later!");
  116.             }
  117.         } catch (InstagramAPI\Exception\ChallengeRequiredException $e) {
  118.             if ($e->hasResponse() && $e->getResponse()->isChallenge()) {
  119.                 try {
  120.                     $api_path = ltrim($e->getResponse()->getChallenge()->getApiPath(), "/");
  121.  
  122.                     // email
  123.                     $checkpoint_resp = $Instagram->checkpointSelectChoice($api_path, $choice = 1);
  124.                     if ($checkpoint_resp["status"] != "ok") {
  125.                         // phone
  126.                         $checkpoint_resp = $Instagram->checkpointSelectChoice($api_path, $choice = 0);
  127.                     }
  128.  
  129.                     if ($checkpoint_resp["status"] == "ok") {
  130.                         $this->resp->result = 2;
  131.                         $this->resp->checkpoint_required = true;
  132.                         $this->resp->identifier = uniqid();
  133.  
  134.                         if ($choice) {
  135.                             $this->resp->msg = __(
  136.                                 "Enter the 6-digit code sent to the email address %s",
  137.                                 $checkpoint_resp["step_data"]["contact_point"]);
  138.                         }
  139.                         else {
  140.                             $this->resp->msg = __(
  141.                                 "Enter the code sent to your number ending in %s",
  142.                                 $checkpoint_resp["step_data"]["contact_point"]);
  143.                         }
  144.  
  145.                         $_SESSION["CHECKPOINT_".$this->resp->identifier] = [
  146.                             "token" => $Instagram->getToken(),
  147.                             "user_id" => $checkpoint_resp["user_id"],
  148.                             "api_path" => $api_path,
  149.                             "username" => $username,
  150.                             "password" => $password,
  151.                             "passhash" => $passhash,
  152.                             "proxy" => $proxy,
  153.                             "is_system_proxy" => $is_system_proxy
  154.                         ];
  155.                     }
  156.                     else {
  157.                         $this->resp->msg = __("Oops! Something went wrong. Please try again later!");
  158.                     }
  159.                 } catch (InstagramAPI\Exception\InstagramException $e) {
  160.                     if ($e->hasResponse()) {
  161.                         $msg = $e->getResponse()->getMessage();
  162.                     } else {
  163.                         $msg = explode(":", $e->getMessage(), 2);
  164.                         $msg = end($msg);
  165.                     }
  166.                     $this->resp->msg = $msg;
  167.                 } catch (\Exception $e) {
  168.                     $this->resp->msg = __("Oops! Something went wrong. Please try again later!");
  169.                 }
  170.             }
  171.             else {
  172.                 $this->resp->msg = __("Oops! Something went wrong. Please try again later!");
  173.             }
  174.         } catch (InstagramAPI\Exception\AccountDisabledException $e) {
  175.             $this->resp->msg = __(
  176.                 "Your account has been disabled for violating Instagram terms. <a href='%s'>Click here</a> to learn how you may be able to restore your account.",
  177.                 "https://help.instagram.com/366993040048856");
  178.         } catch (InstagramAPI\Exception\SentryBlockException $e) {
  179.             $this->resp->msg = __("Your account has been banned from Instagram API for spam behaviour or otherwise abusing.");
  180.         } catch (InstagramAPI\Exception\IncorrectPasswordException $e) {
  181.             $this->resp->msg = __("The password you entered is incorrect. Please try again.");
  182.         } catch (InstagramAPI\Exception\InvalidUserException $e) {
  183.             $this->resp->msg = __("The username you entered doesn't appear to belong to an account. Please check your username and try again.");
  184.         } catch (InstagramAPI\Exception\InstagramException $e) {
  185.             if ($e->hasResponse()) {
  186.                 $msg = $e->getResponse()->getMessage();
  187.             } else {
  188.                 $msg = explode(":", $e->getMessage(), 2);
  189.                 $msg = end($msg);
  190.             }
  191.             $this->resp->msg = $msg;
  192.         } catch (\Exception $e) {
  193.             $this->resp->msg = __("Oops! Something went wrong. Please try again later!");
  194.         }
  195.  
  196.         ...
  197.         ...
  198.         ...
  199.  
  200.     }
  201.  
  202.     ...
  203.     ...
  204.     ...
  205.  
  206.     protected function checkpoint()
  207.     {
  208.         $this->resp->result = 0;
  209.  
  210.         $AuthUser = $this->getVariable("AuthUser");
  211.         $Account = $this->getVariable("Account");
  212.  
  213.         // Check if this is new or not
  214.         $is_new = !$Account->isAvailable();
  215.  
  216.         $security_code = Input::post("checkpoint-security-code");
  217.         $checkpointid = Input::post("checkpointid");
  218.  
  219.         if (!isset(
  220.             $_SESSION["CHECKPOINT_".$checkpointid]["token"],
  221.             $_SESSION["CHECKPOINT_".$checkpointid]["user_id"],
  222.             $_SESSION["CHECKPOINT_".$checkpointid]["api_path"],
  223.             $_SESSION["CHECKPOINT_".$checkpointid]["username"],
  224.             $_SESSION["CHECKPOINT_".$checkpointid]["password"],
  225.             $_SESSION["CHECKPOINT_".$checkpointid]["passhash"]))
  226.         {
  227.             $this->resp->msg = __("Oops! Something went wrong. Please try again later!");
  228.             $this->resp->error_code = "account_invalid_identifier";
  229.             $this->jsonecho();
  230.         }
  231.  
  232.         $token = $_SESSION["CHECKPOINT_".$checkpointid]["token"];
  233.         $user_id = $_SESSION["CHECKPOINT_".$checkpointid]["user_id"];
  234.         $api_path = $_SESSION["CHECKPOINT_".$checkpointid]["api_path"];
  235.         $username = $_SESSION["CHECKPOINT_".$checkpointid]["username"];
  236.         $password = $_SESSION["CHECKPOINT_".$checkpointid]["password"];
  237.         $passhash = $_SESSION["CHECKPOINT_".$checkpointid]["passhash"];
  238.         $proxy = empty($_SESSION["CHECKPOINT_".$checkpointid]["proxy"])
  239.                ? null : $_SESSION["CHECKPOINT_".$checkpointid]["proxy"];
  240.         $is_system_proxy = $_SESSION["CHECKPOINT_".$checkpointid]["is_system_proxy"];
  241.  
  242.         // Setup Instagram Client
  243.         // Allow web usage
  244.         // Since mentioned risks has been consider internally by Nextpost,
  245.         // setting this property value to the true is not risky as it's name
  246.         \InstagramAPI\Instagram::$allowDangerousWebUsageAtMyOwnRisk = true;
  247.        
  248.         $storageConfig = [
  249.             "storage" => "file",
  250.             "basefolder" => SESSIONS_PATH."/".$AuthUser->get("id")."/",
  251.         ];
  252.  
  253.         $Instagram = new \InstagramAPI\Instagram(false, false, $storageConfig);
  254.         $Instagram->setVerifySSL(SSL_ENABLED);
  255.  
  256.         if ($proxy) {
  257.             $Instagram->setProxy($proxy);
  258.         }
  259.  
  260.         try {
  261.             $login_resp = $Instagram->checkpointLogin($username, $password, $token, $user_id, $api_path, $security_code);
  262.             if ($login_resp->getStatus() == "ok") {
  263.                 $Account->set("user_id", $AuthUser->get("id"))
  264.                         ->set("password", $passhash)
  265.                         ->set("proxy", $proxy ? $proxy : "")
  266.                         ->set("login_required", 0)
  267.                         ->set("instagram_id", $login_resp->getLoggedInUser()->getPk())
  268.                         ->set("username", $login_resp->getLoggedInUser()->getUsername())
  269.                         ->set("login_required", 0)
  270.                         ->save();
  271.  
  272.                 // Update proxy use count
  273.                 if ($proxy && $is_system_proxy == true) {
  274.                     $Proxy = Controller::model("Proxy", $proxy);
  275.                     if ($Proxy->isAvailable()) {
  276.                         $Proxy->set("use_count", $Proxy->get("use_count") + 1)
  277.                             ->save();
  278.                     }
  279.                 }
  280.  
  281.                 $this->resp->result = 1;
  282.                 if ($is_new) {
  283.                     $this->resp->redirect = APPURL."/accounts";
  284.                 } else {
  285.                     $this->resp->changes_saved = true;
  286.                     $this->resp->msg = __("Changes saved!");
  287.                 }
  288.             }
  289.             else {
  290.                 $this->resp->msg = __("Please check the code sent to you and try again.");
  291.             }
  292.         } catch (InstagramAPI\Exception\InstagramException $e) {
  293.             if ($e->hasResponse()) {
  294.                 $msg = $e->getResponse()->getMessage();
  295.             } else {
  296.                 $msg = explode(":", $e->getMessage(), 2);
  297.                 $msg = end($msg);
  298.             }
  299.             $this->resp->msg = $msg;
  300.         } catch (\Exception $e) {
  301.             $this->resp->msg = __("Oops! Something went wrong. Please try again later!");
  302.         }
  303.  
  304.         $this->jsonecho();
  305.     }
  306.  
  307.     ...
  308.     ...
  309.     ...
  310.  
  311. }
Add Comment
Please, Sign In to add comment