PHP File Manager
Editing File: Robot.php
<?php /* * Copyright (C) Wayne Purton-Smith - All Rights Reserved * Unauthorized copying of this file or removing this paragraph, via any medium is strictly prohibited * Proprietary and confidential * Written by Wayne Purton-Smith <waynepurtonsmith@hotmail.co.uk> February 2014 */ class Robot extends CI_Model { private $mode; private $token; private $_url; private $_session; private $_status_code; private $_response; private $_info; private $_errors = array(); private $_doc; private $_session_log = array(); public function mode($mode = NULL) { $this->mode = $mode; $this->_session_log[] = 'Mode set to "' . $this->mode . '"'; return $this; } public function setToken($token = NULL) { $this->token = $token; return $this; } public function login($user = '', $password = '') { if($this->_session) { if($this->mode == 'johnlewis') { curl_setopt($this->_session, CURLOPT_USERPWD, $user . ':' . $password); $this->_session_log[] = 'John Lewis user and password set'; } } return $this; } public function init() { if($this->_session === NULL) { $this->_session = curl_init(); $random = generate_unique_identifier(20); curl_setopt($this->_session, CURLOPT_COOKIEJAR, APPPATH . 'cache/cookies/request-cookie-' . $random); curl_setopt($this->_session, CURLOPT_COOKIEFILE, APPPATH . 'cache/cookies/request-' . $random); $this->_session_log[] = 'cURL session initiated'; } return $this; } public function setUrl($url = '') { $this->_url = $url; $this->_session_log[] = 'URL set to "' . $this->_url . '"'; return $this; } public function getUrl() { return $this->_url; } public function isOK() { return (stripos($this->_response, '<html>') !== false); } public function getContents() { return $this->_response; } public function getInfo() { return $this->_info; } public function getErrors() { return $this->_errors; } public function getLog() { return $this->_session_log; } public function status() { return $this->_status_code; } public function scrape($post_data = array()) { $this->_response = NULL; if(!filter_var($this->_url, FILTER_VALIDATE_URL)) { $this->_session_log[] = 'URL was invalid'; return false; } curl_setopt($this->_session, CURLOPT_URL, $this->_url); curl_setopt($this->_session, CURLOPT_CAINFO, APPPATH . 'third_party/cacert.pem'); //curl_setopt($this->_session, CURLOPT_SSL_VERIFYPEER, false); //curl_setopt($this->_session, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($this->_session, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0'); curl_setopt($this->_session, CURLOPT_FOLLOWLOCATION, true); curl_setopt($this->_session, CURLOPT_RETURNTRANSFER, true); curl_setopt($this->_session, CURLOPT_TIMEOUT, 40); if(count($post_data)) { curl_setopt($this->_session, CURLOPT_POST, true); curl_setopt($this->_session, CURLOPT_POSTFIELDS, $post_data); } $this->_status_code = curl_getinfo($this->_session, CURLINFO_HTTP_CODE); $this->_info = curl_getinfo($this->_session); $this->_response = curl_exec($this->_session); $this->_errors[] = (curl_error($this->_session)) ? curl_error($this->_session) : 'Request OK'; $this->_session_log[] = 'Sending request'; return $this; } public function parseAsHtml() { require_once('application/third_party/shdp/simple_html_dom.php'); $this->_doc = new simple_html_dom(); $this->_doc->clear(); return $this->_doc->load($this->_response); } public function parseAsJson() { return json_decode($this->_response); } public function clear() { if($this->_doc) { $this->_doc->clear(); } return $this; } public function saveContents() { $this->db->query($this->db->insert_string('crawler_responses', array ( 'date' => time(), 'url' => $this->getUrl(), 'body' => $this->getContents(), 'debug' => json_encode(array ( 'session' => $this->getLog(), 'info' => $this->getInfo(), 'errors' => $this->getErrors() )) ))); return ($this->db->affected_rows()) ? $this->db->insert_id() : false; } public function destroySession() { curl_close($this->_session); if(file_exists($this->cookie_file)) { unlink($this->cookie_file); } if(file_exists($this->request_file)) { unlink($this->request_file); } $this->_session = NULL; $this->_errors = array(); } }
Cancel