Guest User

Untitled

a guest
Dec 17th, 2016
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.40 KB | None | 0 0
  1. <?php
  2.  
  3. class Scrape
  4. {
  5. public $cookies = 'cookies.txt';
  6. private $user = null;
  7. private $pass = null;
  8.  
  9. /*Data generated from cURL*/
  10. public $content = null;
  11. public $response = null;
  12.  
  13. /* Links */
  14. private $url = array(
  15.                     'login'     => 'http://www.westernconnect.ca/site/UserLogin',
  16.                     'submit'    => 'http://www.westernconnect.ca/site/UserLogin'
  17.                     );
  18.  
  19. /* Fields */
  20. public $data = array();
  21.  
  22. public function __construct ($user, $pass)
  23. {
  24.  
  25.     $this->user = $user;
  26.     $this->pass = $pass;        
  27.  
  28. }
  29.  
  30. public function login()
  31. {
  32.  
  33.             $this->cURL($this->url['login']);
  34.             //print_r($this->content);
  35.  
  36.             if($form = $this->getFormFields($this->content, 'lmainLogonForm'))
  37.             {
  38.  
  39.                 $form['USERNAME'] = $this->user;
  40.                 $form['Password'] =$this->pass;
  41.                 //echo "<pre>".print_r($form,true);exit;
  42.                 $this->cURL($this->url['submit'], $form);
  43.                 echo $this->content;exit;
  44.             }
  45.           // echo $this->content;exit;    
  46. }
  47.  
  48. /* Scan for form */
  49. private function getFormFields($data, $id)
  50. {
  51.         if (preg_match('/(<form.*?name=.?'.$id.'.*?<\/form>)/is', $data, $matches)) {
  52.             $inputs = $this->getInputs($matches[1]);
  53.             return $inputs;
  54.         } else {
  55.             return false;
  56.         }
  57.  
  58. }
  59.  
  60. /* Get Inputs in form */
  61. private function getInputs($form)
  62. {
  63.     $inputs = array();
  64.  
  65.     $elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);
  66.  
  67.     if ($elements > 0) {
  68.         for($i = 0; $i < $elements; $i++) {
  69.             $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);
  70.  
  71.             if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
  72.                 $name  = $name[1];
  73.                 $value = '';
  74.  
  75.                 if (preg_match('/value=(?:["\'])?([^"\']*)/i', $el, $value)) {
  76.                     $value = $value[1];
  77.                 }
  78.  
  79.                 $inputs[$name] = $value;
  80.             }
  81.         }
  82.     }
  83.  
  84.     return $inputs;
  85. }
  86.  
  87. /* Perform curl function to specific URL provided */
  88. public function cURL($url, $post = false)
  89. {
  90.     $ch = curl_init();
  91.     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  92.     curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36");
  93.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  94.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  95.     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  96.     curl_setopt($ch, CURLOPT_VERBOSE, 1);
  97.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  98.     curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookies);
  99.     curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookies);
  100.     curl_setopt($ch, CURLOPT_HEADER, 0);  
  101.     curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  102.     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
  103.     curl_setopt($ch, CURLOPT_TIMEOUT, 120);
  104.  
  105.     if($post)   //if post is needed
  106.     {
  107.         curl_setopt($ch, CURLOPT_POST, 1);
  108.         curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
  109.     }
  110.  
  111.     curl_setopt($ch, CURLOPT_URL, $url);
  112.     $this->content = curl_exec($ch);
  113.     $this->response = curl_getinfo( $ch );
  114.     $this->url['last_url'] = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  115.     curl_close($ch);
  116. }
  117. }
  118.  
  119.  
  120. $sc = new Scrape('yli2384@uwo.ca','911colour');
  121.  
  122. $sc->login();
  123.  
  124.  
  125. ?>
Add Comment
Please, Sign In to add comment