Advertisement
Guest User

Untitled

a guest
Jul 4th, 2017
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. <?php
  2.  
  3. $object = new TestLogin('https://www.balatarin.com/sessions', 'i886318', 'i886318@mvrht.net');
  4.  
  5. $object->login();
  6.  
  7. $object->getContent('https://www.balatarin.com/users/i886318');
  8.  
  9. function __autoload($class)
  10. {
  11. if(file_exists($class.'.php'))
  12. {
  13. require_once $class.'.php';
  14. }
  15. else
  16. {
  17. die($class. ' class does not exists.');
  18. }
  19. }
  20. die;
  21.  
  22. <?php
  23.  
  24. class TestLogin extends General implements ICurl
  25. {
  26. public function __construct($loginUrl, $username, $password)
  27. {
  28. $this->loginUrl = $loginUrl;
  29. $this->username = $username;
  30. $this->password = $password;
  31.  
  32. parent::__construct();
  33. }
  34.  
  35. public function login()
  36. {
  37. if(!$this->isLogin())
  38. {
  39. $response = curl_exec($this->curl);
  40.  
  41. if(curl_errno($this->curl)) die(curl_error($this->curl));
  42.  
  43. curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, false);
  44. curl_setopt($this->curl, CURLOPT_POST, true);
  45.  
  46. /* params */
  47. $params = array(
  48. 'session[login]' => $this->username,
  49. 'session[password]' => $this->password,
  50. 'session[remember_me]' => '1',
  51. 'commit' => 'ورود',
  52. 'utf8' => '✓',
  53. 'authenticity_token' => '',
  54. );
  55.  
  56. /* create http query */
  57. curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($params));
  58.  
  59. $login = curl_exec($this->curl);
  60.  
  61. $_SESSION['loggedIn'] = ($login === true ? true : false);
  62.  
  63. return $login;
  64. }
  65. else
  66. {
  67. return true;
  68. }
  69. }
  70.  
  71. public function getContent($url)
  72. {
  73. if($this->isLogin())
  74. {
  75.  
  76. curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
  77. curl_setopt($this->curl, CURLOPT_BINARYTRANSFER, true);
  78.  
  79. curl_setopt($this->curl, CURLOPT_URL, $url);
  80.  
  81. $content = curl_exec($this->curl);
  82.  
  83. if (curl_errno($this->curl)) print curl_error($this->curl);
  84. curl_close($this->curl);
  85.  
  86. echo 'start<br />';
  87. echo($content);
  88. echo '<br />end<br />';
  89. }
  90. else
  91. {
  92. return null;
  93. }
  94. }
  95. }
  96.  
  97. <?php
  98. class General
  99. {
  100. protected $curl;
  101.  
  102. protected $loginUrl;
  103.  
  104. protected $username;
  105.  
  106. protected $password;
  107.  
  108. private $cookiePath = __DIR__;
  109.  
  110. private $subClassName;
  111.  
  112. public function __construct()
  113. {
  114. session_start();
  115.  
  116. $this->subClassName = get_called_class();
  117.  
  118. $this->curl = curl_init();
  119.  
  120. //Set the useragent
  121. curl_setopt($this->curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
  122.  
  123. /* set login url */
  124. curl_setopt($this->curl, CURLOPT_URL, $this->loginUrl);
  125.  
  126. /* set cookie */
  127. curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookiePath."\".$this->subClassName.'_cookie.txt');
  128. curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookiePath."\".$this->subClassName.'_cookie.txt');
  129.  
  130. curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
  131.  
  132. // for ssl
  133. //curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  134. }
  135.  
  136. protected function isLogin()
  137. {
  138. return (isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] === true);
  139. }
  140.  
  141. public function logout()
  142. {
  143. $cookiePath = $this->cookiePath.'\'.$this->subClassName.'_cookie.txt';
  144.  
  145. unset($_SESSION['loggedIn']);
  146.  
  147. if(file_exists($cookiePath)) unlink($cookiePath);
  148. }
  149.  
  150. public function __destruct()
  151. {
  152. curl_close($this->curl);
  153. }
  154. }
  155.  
  156. <?php
  157.  
  158. interface ICurl
  159. {
  160. public function login();
  161.  
  162. public function getContent($url);
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement