Advertisement
Guest User

Untitled

a guest
Jul 5th, 2016
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.64 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: bathan
  5.  * Date: 4/11/16
  6.  * Time: 3:32 PM
  7.  *
  8.  * FTP
  9.  * v1f9wS)4^fGa9)g
  10.  * marketrealist@totisoft.com.ar
  11.  *
  12.  */
  13.  
  14. class MR_Feed_Monitor
  15. {
  16.  
  17.     private $_notify_email_address = 'miguel@marketrealist.com';
  18.     private $_send_email = false;
  19.  
  20.     /**
  21.      * Feeds class instance property
  22.      *
  23.      * @var array
  24.      */
  25.     var $feeds = array();
  26.    
  27.     /**
  28.      * Default feeds
  29.      *
  30.      * @var array
  31.      */
  32.     private $_default_feeds = [
  33.         'yahoo-finance' => [
  34.             'url'=>'http://marketrealist.com/feed-yahoo-finance/', //-- Very Important to have slashes both at beggining and end
  35.             'username'=>'yahoofinancefeed',
  36.             'password'=>'ED7XaJBiy4HKPMKQqKFPWfW'
  37.         ]
  38.     ];
  39.  
  40.     /**
  41.      * @param array $feeds
  42.      */
  43.     public function __construct( $feeds = array() )
  44.     {
  45.         if(empty($feeds)) {
  46.             $feeds = $this->_default_feeds;
  47.         }
  48.         $this->feeds = $feeds;
  49.     }
  50.  
  51.     public function set_notify_email($e) {
  52.         $this->_notify_email_address = $e;
  53.     }
  54.     public function set_send_emails($s) {
  55.         $this->_send_email = $s;
  56.     }
  57.  
  58.     /**
  59.      * Set class instance feeds
  60.      *
  61.      * @param $feeds
  62.      */
  63.     public function set_feeds( $feeds )
  64.     {
  65.         $this->feeds = $feeds;
  66.     }
  67.  
  68.     /**
  69.      *
  70.      * Runs the Checks for the feeds
  71.      */
  72.     public function run_check()
  73.     {
  74.         foreach ($this->feeds as $feed_name=>$feed) {
  75.             $feed_url = $feed['url'];
  76.             $feed_username = $feed['username'];
  77.             $feed_password = $feed['password'];
  78.  
  79.             $feed_contents = $this->get_feed_contents($feed_url,$feed_username,$feed_password);
  80.  
  81.             libxml_use_internal_errors(true);
  82.  
  83.             $doc = simplexml_load_string($feed_contents);
  84.  
  85.             $error_array = [];
  86.             if (!$doc) {
  87.                 $errors = libxml_get_errors();
  88.  
  89.                 foreach ($errors as $error) {
  90.                     $error_array[] = json_encode($error,JSON_PRETTY_PRINT);
  91.                 }
  92.  
  93.                 libxml_clear_errors();
  94.  
  95.                 //-- Ok Something is wrong, we need to send an email notification.
  96.                 if($this->_send_email) {
  97.                     $this->send_alert_email($this->_notify_email_address,$feed_name,$feed_url, $error_array);
  98.                 }
  99.             }
  100.         }
  101.  
  102.     }
  103.  
  104.     public function get_feed_contents($feedUrl,$feed_username,$feed_password)
  105.     {
  106.  
  107.         $feed_content = null;
  108.  
  109.         try {
  110.             $process = curl_init($feedUrl);
  111.  
  112.             curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
  113.             //curl_setopt($process, CURLOPT_HEADER, 1);
  114.             curl_setopt($process, CURLOPT_USERPWD, $feed_username . ":" . $feed_password);
  115.             curl_setopt($process, CURLOPT_TIMEOUT, 300);
  116.             curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
  117.             $feed_content = curl_exec($process);
  118.  
  119.         } catch (Exception $e) {
  120.             throw $e;
  121.         }
  122.  
  123.         return $feed_content;
  124.     }
  125.  
  126.     private function send_alert_email($email,$feed_name,$feed_url,$errors) {
  127.  
  128.         $subject = $feed_name.' feed in trouble';
  129.         $body = "<h2> Problem with ".$feed_name."</h2><h3>$feed_url</h3><pre>".print_r($errors,true)."</pre>";
  130.  
  131.         $headers  = "From: MR Feeds Monitor <mrfm@marketrealist.com>\r\n";
  132.         $headers .= "X-Mailer: TotiEmailer\n";
  133.         $headers .= 'MIME-Version: 1.0' . "\n";
  134.         $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  135.  
  136.         $email_sent = mail($email,$subject, $body, $headers);
  137.  
  138.         echo $body ;
  139.  
  140.         return $email_sent;
  141.     }
  142. }
  143.  
  144. $send_emails = isset($_GET['send_email']);
  145.  
  146. $x = new MR_Feed_Monitor();
  147.  
  148. $x->set_send_emails($send_emails);
  149.  
  150. if(isset($_GET['notify_email_address'])) {
  151.     $x->set_notify_email($_GET['notify_email_address']);
  152. }
  153.  
  154. $get_parameters = ['feed_name','feed_url','feed_username','feed_password'];
  155.  
  156. $use_get_params = true;
  157. foreach($get_parameters as $param) {
  158.     if(!isset($_GET[$param])) {
  159.         $use_get_params = false;
  160.     }
  161. }
  162.  
  163. if($use_get_params) {
  164.     $feed_name = $_GET['feed_name'];
  165.     $feed_url = $_GET['feed_url'];
  166.     $feed_username = $_GET['feed_username'];
  167.     $feed_password = $_GET['feed_password'];
  168.  
  169.     $default_feeds = [
  170.         $feed_name => [
  171.             'url'=>$feed_url, //-- Very Important to have slashes both at beggining and end
  172.             'username'=>$feed_username,
  173.             'password'=>$feed_password
  174.         ]
  175.     ];
  176.  
  177.     $x->set_feeds($default_feeds);
  178. }
  179.  
  180.  
  181. $x->run_check();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement