Advertisement
terorama

WP / inc / rss.inc.php

May 29th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.78 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.  class Rss {
  5.  
  6.     private $_db;
  7.     private $lasterror='';
  8.    
  9.     private  $rss_lines;
  10.     private $rss_content;
  11.     private $digests;
  12.     private $digests_elements;
  13.    
  14.     //----------------------------------
  15.     private function get_mysqli() {
  16.    
  17.        global $wpdb;
  18.        
  19.        $wpdb->show_errors();      
  20.        return $wpdb;
  21.     }
  22.  
  23.     //----------------------------------
  24.     private function check_sql_error($result) {
  25.    
  26.          
  27.        if ($result===false) {
  28.           ob_start();
  29.           $this->_db->print_error();
  30.           $err = ob_get_clean();
  31.          
  32.           throw new Exception ('database error: '.$err);
  33.        }
  34.     }
  35.    
  36.  
  37.     //----------------------------------
  38.     public function mk_tables() {
  39.        
  40.        require_once(ABSPATH . '/wp-admin/includes/upgrade.php');
  41.        
  42.        if ($this->_db->get_var("SHOW TABLES LIKE '". $this->digests ."'") == $this->digests)
  43.           return;
  44.        
  45.        $sqls = array (
  46.        
  47.        "create table {$this->rss_lines} (id int(11) not null auto_increment,
  48.        line_name varchar(50), line_url varchar(500), primary key(`id`));",
  49.  
  50.        "create table {$this->rss_content} (id int(11) not null auto_increment,
  51.        line_id int(11),
  52.        title varchar(500), description varchar(1500),
  53.        link varchar(500), pubdate datetime, pubhash varchar(500), primary key(`id`));",
  54.        
  55.        /*"drop table {$this->digests}",
  56.        "drop table {$this->digests_elements}",*/
  57.        
  58.         "create table {$this->digests} (
  59.             digest_id int(11) not null auto_increment,
  60.                
  61.                 digest_title varchar(50),
  62.                 digest_text varchar(500),
  63.                 digest_date datetime,
  64.                      
  65.                  digest_schedule_period int(11),
  66.                  digest_schedule_time varchar(50),
  67.                  digest_is_active int(11),
  68.                  digest_post_type varchar(50),
  69.                  digest_taxonomy varchar(50),
  70.                  digest_category_id int(11),
  71.             primary key(`digest_id`));",
  72.                  
  73.           //-----------------------------
  74.           "create table {$this->digests_elements} (
  75.             element_id int(11) not null auto_increment,
  76.                  digest_id int(11),
  77.                 element_rss_name varchar(20),
  78.                 element_page_order int(11),
  79.                
  80.                  element_request_period int(11),
  81.                  element_template varchar(2000),
  82.                  element_before_template varchar(2000),
  83.                 element_after_template varchar(2000),
  84.             primary key(`element_id`));");
  85.      
  86.       /*$sqls = array (
  87.          "alter table {$this->digests} add column digest_post_type varchar(50);",
  88.          "alter table {$this->digests} add column digest_taxonomy varchar(50);",
  89.          "alter table {$this->digests} add column digest_category_id int(11);");*/
  90.          
  91.        while (list($key,$sq)= each($sqls)) {
  92.          
  93.           $this->_db->query($sq);
  94.           //dbDelta($sq);
  95.          
  96.           }
  97.     }
  98.  
  99.     //----------------------------------
  100.     private function date_rss2mysql4($s) {
  101.    
  102.        //   Thu, 27 Dec 2012 13:35:18 +0000
  103.        
  104.        //   2012-12-27 13:35:18
  105.        
  106.        $ver = phpversion();
  107.        $ver = explode('.',$ver);
  108.        $ver = implode(array_slice($ver,0,2));
  109.  
  110.        if ($ver>=53) {
  111.        
  112.          $date =  DateTime::createFromFormat('D, d M Y H:i:s O',$s);
  113.          $s = $date->format('Y-m-d H:i:s',$tm);
  114.          
  115.        } else {
  116.           $tm = strtotime($s);   
  117.           $s = date('Y-m-d H:i:s',$tm);
  118.        }
  119.        
  120.        return $s;
  121.     }
  122.  
  123.     //----------------------------------
  124.     private function get_rssline_num($name) {
  125.    
  126.        $res = $this->_db->get_var(
  127.           $this->_db->prepare("select min(id) from {$this->rss_lines} where line_name=%s",$name));
  128.            
  129.        $this->check_sql_error($res);
  130.        
  131.        return $res;
  132.     }
  133.    
  134.     //----------------------------------
  135.     private function get_rssline_url($id) {
  136.    
  137.        $res = $this->_db->get_var(
  138.           $this->_db->prepare("select line_url from {$this->rss_lines} where id=%d",$id));
  139.        
  140.        $this->check_sql_error($res);
  141.  
  142.        return $res;
  143.     }
  144.    
  145.  
  146.     //----------------------------------
  147.     private function check_hash($hash) {
  148.    
  149.        $res = $this->_db->get_row(
  150.          $this->_db->prepare('select * from '.$this->rss_content.' where pubhash=%s',$hash), ARRAY_A);
  151.          
  152.        $this->check_sql_error($res);
  153.        
  154.        if ($res!=null) {  
  155.           return true;
  156.        }
  157.        else {
  158.           return false;
  159.        }
  160.      
  161.     }
  162.     //----------------------------------
  163.     private function getNRows($id) {
  164.  
  165.        $res = $this->_db->get_var($this->_db->prepare('select count(*) from '.$this->rss_content.' where line_id=%d',$id));
  166.        $this->check_sql_error($res);
  167.        
  168.        $nrows = $res;
  169.    
  170.        return $nrows;
  171.     }
  172.     //----------------------------------
  173.     public function greeting() {
  174.        return 'ready';
  175.     }
  176.    
  177.     //----------------------------------
  178.     public function show_rsslines() {
  179.    
  180.    
  181.        $res = $this->_db->get_results('select id, line_name from '.$this->rss_lines.' order by id desc');
  182.        $this->check_sql_error($res);
  183.        
  184.        ob_start();
  185.        echo '<form id="lineform"><ul class="twitlines">';
  186.        foreach ($res as $row) {
  187.           echo '<li><input type="radio" name="line_name" value="'.$row->line_name.'" />'.
  188.           '<a href="#" rel="_show_rssline" lnname="'.$row->line_name.'">'.$row->line_name.'</a></li>';
  189.        }
  190.        echo '</ul></form>';    
  191.        
  192.        $s = ob_get_clean();
  193.        return $s;
  194.     }
  195.    
  196.     //----------------------------------
  197.     public function get_rsslines() {
  198.        
  199.        $res = $this->_db->get_col('select line_name from '.$this->rss_lines);
  200.        $this->check_sql_error($res);
  201.        
  202.        return $res;
  203.     }
  204.    
  205.     //----------------------------------
  206.     public function show_controls() {
  207.    
  208.        $s = '<ul class="controls">
  209.        <li><a href="#" rel="_add_rssline_form">add rss feed</a></li>
  210.        <li><a href="#" rel="_edit_rssline_form">edit rss feed</a></li>
  211.        <li><a href="#" rel="_update_rssline">update rss feed</a></li>
  212.        <li><a href="#" rel="_clear_rssline">clear rss feed</a></li>    
  213.        <li><a href="#" rel="_delete_rssline_form">delete rss feed</a></li>
  214.  
  215.        </ul>';
  216.        
  217.        return $s;
  218.     }
  219.    
  220.     //----------------------------------
  221.     public function edit_rssline_form($name='') {
  222.        
  223.  
  224.        if ($name=='') {
  225.           $isedit = false;
  226.          
  227.           $line_name = '';
  228.           $line_url = '';
  229.           $line_id = 0;
  230.        }
  231.        else {
  232.           $isdeit = true;
  233.           $num = $this->get_rssline_num($name);
  234.          
  235.           if ($num===null)
  236.              throw new Exception ('error getting rss line');
  237.          
  238.           $res = $this->_db->get_row($this->_db->prepare('select * from '.$this->rss_lines.' where id=%d',$num));
  239.           $this->check_sql_error($res);
  240.          
  241.           if ($res===null)
  242.              throw new Exception ('error getting rss line info');
  243.    
  244.           $line_name = $res->line_name;
  245.           $line_url = $res->line_url;
  246.           $line_id = $res->id;
  247.        }
  248.        
  249.        ob_start();     
  250.        echo '<form rel="_save_rssline" '.
  251.        'enctype="application/x-www-form-urlencoded">'.
  252.        '<p> input rss feed name: '.
  253.        '<input type="text" name="line_name" value="'.$line_name.'" /></p>'.
  254.        '<p> input rss feed url: '.
  255.        '<input type="text" name="line_url" value="'.$line_url.'" /></p>'.      
  256.        '<input type="hidden" name="line_id" value="'.$line_id,'" />'.
  257.        '<p><input type="submit" name="submit" value="save"/>'.
  258.        '<input type="button" rel="_greeting" value="cancel" /></p></form>';
  259.        
  260.        $s = ob_get_contents();
  261.        ob_end_clean();
  262.        
  263.        return $s;
  264.     }  
  265.    
  266.     //----------------------------------
  267.     public function add_rssline_form() {
  268.    
  269.        return $this->edit_rssline_form();
  270.     }
  271.    
  272.     //----------------------------------
  273.     public function delete_rssline_form($name) {
  274.    
  275.  
  276.        if (!$name)
  277.           throw new Exception('line_name parameter is empty');
  278.          
  279.        $num = $this->get_rssline_num($name);
  280.        if ($num===null)
  281.           throw new Exception ('error getting rss line');
  282.          
  283.        $s = '<form rel="_delete_rssline" '.
  284.        'enctype="application/x-www-form-urlencoded">'.
  285.        '<input type="hidden" name="line_id" value="'.$num.'" />'.
  286.        '<p><input type="submit" name="submit" value="delete"/>'.
  287.        '<input type="button" rel="_greeting" value="cancel"></p></form>';
  288.        
  289.        return $s;
  290.     }
  291.    
  292.     //----------------------------------
  293.     public function save_rssline() {
  294.    
  295.    
  296.        if ($_POST['line_id']==0) {
  297.        
  298.           $rez = $this->_db->insert( $this->rss_lines,
  299.                   array (
  300.                      'line_name'=>$_POST['line_name'],
  301.                      'line_url'=>$_POST['line_url']
  302.                   ),
  303.                   array ('%s','%s'));
  304.  
  305.           $this->check_sql_error($rez);
  306.           $id = $this->_db->insert_id;
  307.        
  308.           return "record with number $id inserted";
  309.          
  310.          
  311.        } else {
  312.          
  313.           $rez = $this->_db->update( $this->rss_lines,
  314.                     array (
  315.                        'line_name'=>$_POST['line_name'],
  316.                        'line_url'=>$_POST['line_url']
  317.                     ),
  318.                     array('id'=> $_POST['line_id']),
  319.                     array('%s','%s'), array('%d'));
  320.  
  321.        
  322.           $this->check_sql_error($rez);
  323.           $affected = $rez;
  324.          
  325.           return " $affected records updated";
  326.        }
  327.        
  328.     }
  329.    
  330.     //----------------------------------
  331.     public function delete_rssline() {
  332.        
  333.        
  334.        $rez = $this->_db->query( $this->_db->prepare(
  335.           'delete from '.$this->rss_lines.' where id=%d',$_POST['line_id']));
  336.            
  337.        $this->check_sql_error($rez);
  338.        $affected = $rez;
  339.        
  340.        return " deleted rows $affected";
  341.        
  342.     }
  343.    
  344.     //----------------------------------
  345.         public function show_rssdate($curdate, $name, $indate=1) {
  346.  
  347.          $num = $this->get_rssline_num($name);
  348.          $npages = ceil($this->getNRows($num)/100);
  349.        
  350.        if ($num===null)
  351.           throw new Exception ('error getting rss line');
  352.          
  353.        if (is_numeric($indate)) {
  354.  
  355.           $rez = $this->_db->get_results(
  356.           'select id, title, description, link,'.
  357.           'DATE_FORMAT(pubdate,\'%d.%m.%Y %H:%i\') pubdate4 from '.$this->rss_content.
  358.           ' where line_id='.$num.' and pubdate<DATE(\''.$curdate.'\') '.
  359.           ' and DATEDIFF(DATE(\''.$curdate.'\'), pubdate)<'.$indate);
  360.        
  361.           }
  362.        else
  363.           $rez = $this->_db->get_results($this->_db->prepare(
  364.           'select id, title, description, link, '.
  365.           'DATE_FORMAT(pubdate,\'%d.%m.%Y  %H:%i\') pubdate4 from '.$this->rss_content.
  366.           ' where line_id=%d and '.
  367.                   'DATE_FORMAT(pubdate,\'%d.%m.%Y\')=%s order by pubdate',$num,$indate));
  368.  
  369.         $this->check_sql_error($rez);
  370.  
  371.        //---------------------------
  372.  
  373.        $out = array();
  374.        foreach ($rez as $elz) {
  375.  
  376.               $el = array();
  377.              
  378.           $el["twitid"]=$elz->id;
  379.           $el["twittitle"]=$elz->title;
  380.           $el["twitdesc"]=$elz->description;
  381.           $el["twitlink"]=$elz->link;
  382.           $el["twitpubdate"]=$elz->pubdate4;
  383.  
  384.               $out[]=$el;        
  385.        }     
  386.  
  387.        return $rez;
  388.            
  389.         }
  390.         //----------------------------------
  391.     public function show_rssline($name) {
  392.    
  393.        $num = $this->get_rssline_num($name);
  394.        $npages = ceil($this->getNRows($num)/100);
  395.        
  396.        if ($num===null)
  397.           throw new Exception ('error getting rss line');
  398.          
  399.        $rez = $this->_db->get_results(
  400.           'select id, title, description, link, '.
  401.           'DATE_FORMAT(pubdate,\'%d.%m.%Y  %H:%i\') pubdate4 from '.$this->rss_content.
  402.           ' where line_id='.$num.' order by pubdate desc');
  403.            
  404.        $this->check_sql_error($rez);
  405.        
  406.        if ($rez===null) {
  407.           echo 'Empty result. Click "update rss feed"';
  408.           return;
  409.        }
  410.          
  411.          
  412.        //---------------------------
  413.        $jz = 0;
  414.        if (isset($_GET['page'])) {
  415.           if ($_GET['page']>1) {         
  416.                  $jz = ($_GET['page']-1)*100;
  417.              }
  418.        }
  419.        //---------------------------
  420.        ob_start();
  421.    
  422.        echo '<ul class="pager">';
  423.        for ($i=0; $i<$npages; $i++) {
  424.           echo '<li><a href="#" rel="_show_rssline" pg="'.($i+1).
  425.           '" lnname="'.$name.'">'.
  426.           ($i+1).'</a></li>';
  427.        }
  428.        echo '</ul>';
  429.        //---------------------------       
  430.        
  431.        $j=0;
  432.        
  433.        echo '<ul class="twitfeed">';
  434.        while (($jz<count($rez)) && ($j<100)) {
  435.           printf ("<li>
  436.               <div class=\"twitid\">%s</div>
  437.               <div class=\"twittitle\">%s</div>
  438.               <div class=\"twitdesc\">%s</div>
  439.               <div class=\"twitlink\"><a href=\"%s\" target=\"_blank\">%s</a></div>
  440.               <div class=\"twitpubdate\">%s</div>
  441.              
  442.              </li>",
  443.           $rez[$jz]->id, $rez[$jz]->title,
  444.           $rez[$jz]->description, $rez[$jz]->link,
  445.           $rez[$jz]->link, $rez[$jz]->pubdate4);
  446.          
  447.           $j++;
  448.           $jz++;
  449.        }
  450.        echo '</ul>';
  451.  
  452.        $s = ob_get_clean();
  453.            
  454.        return $s;
  455.     }
  456.     //----------------------------------------------------------------------
  457.     public function clear_rssline($name) {
  458.    
  459.        $line_id = $this->get_rssline_num($name);
  460.        
  461.        if ($line_id===null)
  462.           throw new Exception ('error getting rss line');
  463.  
  464.        $rez = $this->_db->query($this->_db->prepare('delete from '.$this->rss_content.' where line_id=%d',$line_id));
  465.        $this->check_sql_error($rez);
  466.        
  467.        return 'rss line cleared, '.$rez.' deleted';
  468.     }
  469.    
  470.     //----------------------------------------------------------------------
  471.     private function fpatch($que) {
  472.  
  473.        $ch = curl_init();
  474.        curl_setopt($ch, CURLOPT_URL, $que);
  475.        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  476.        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  477.        curl_setopt($ch, CURLOPT_USERAGENT,
  478.           "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)");
  479.        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  480.        
  481.        $inf = curl_exec($ch);
  482.        
  483.        if (curl_errno($ch)) {
  484.           throw new Exception ('curl exception: '.curl_error($ch));      
  485.        }       
  486.        curl_close($ch);
  487.      
  488.        if (substr($inf,0,1)!='<') {
  489.           $inf = substr($inf,strpos($inf,'<'));  
  490.         }
  491.  
  492.         preg_match_all('/(http(([^<"\'\?])*?)\?)(([^<"\'])*)/ims', $inf, $z,
  493.          PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
  494.  
  495.         $outs = '';
  496.         $start = 0;
  497.         foreach ($z as $el) {
  498.            $outs .= substr($inf, $start, $el[0][1]-$start).
  499.                 $el[1][0].str_replace('&','&amp;',$el[4][0]);
  500.  
  501.          $start = $el[0][1]+strlen($el[0][0]);
  502.  
  503.         }
  504.         $outs .= substr($inf, $start);
  505.         return $outs;
  506.     }
  507.     //----------------------------------------------------------------------
  508.     private function getrss($rssquery,$count) {
  509.  
  510.       $ver=1;
  511.       $rez='';
  512.       $outs = $this->fpatch($rssquery);
  513.       $xml=simplexml_load_string($outs);
  514.    
  515.       $i=1;
  516.       try {
  517.    
  518.          foreach ($xml->children() as $a=>$b) {
  519.            if ($a=='channel') {
  520.                $ver=2;
  521.             }
  522.          }
  523.    
  524.       $out = array();
  525.    
  526.       if ($ver==1) {
  527.             $base = $xml->entry;
  528.             $content = 'content';
  529.             $pub = 'published';
  530.          }
  531.       else {
  532.            $base = $xml->channel->item;
  533.            $content = 'description';
  534.            $pub = 'pubDate';
  535.       }
  536.      
  537.       //--------------------------------------
  538.       foreach ($base as $item) {
  539.      
  540.          $title=$item->title;
  541.          $inf = array();
  542.          $inf['title'] = (string)$item->title;
  543.          $inf['description'] = (string)$item->$content;
  544.  
  545.          $att = 'href';
  546.              $attr = $item->link->attributes()->$att;
  547.  
  548.              if ($attr)
  549.                 $inf['link']= (string)$attr;
  550.              else
  551.                 $inf['link']= (string)$item->link;
  552.  
  553.          $inf['pubdate']=(string)$item->$pub;    
  554.          $out[]=$inf;
  555.  
  556.          $i++;  
  557.          
  558.          if ($i>$count)
  559.              break;
  560.  
  561.      }}
  562.        catch (Exception $e) {
  563.            return $e->getMessage();
  564.            
  565.         }
  566.       return $out;
  567.     }
  568.     //----------------------------------------------------------------------
  569.     private function save_rsss($url, $line_id) {
  570.          
  571.        $out = $this->getrss($url,500);
  572.        
  573.        foreach ($out as $item) {
  574.        
  575.           extract($item);            
  576.           $pubhash =md5($title.$pubdate);  
  577.           if ($this->check_hash($pubhash))
  578.               continue;
  579.              
  580.           $pubdate4 = $this->date_rss2mysql4($pubdate);
  581.           $desc = preg_replace('/style=(["\'])[^\1]*?\1/ims','',$description);
  582.                  
  583.            $rez = $this->_db->insert( $this->rss_content,
  584.                  array(
  585.                      'line_id'=> $line_id,
  586.                      'title'=> $title,
  587.                      'description'=> $desc,
  588.                      'link'=> $link,
  589.                      'pubdate'=> $pubdate4,
  590.                      'pubhash'=> $pubhash),
  591.                      
  592.                      array('%d','%s','%s','%s','%s','%s'));
  593.                
  594.           $this->check_sql_error($rez);  
  595.        }
  596.  
  597.        return 'inserted '.count($out).' rsss : '.$url;
  598.     }
  599.  
  600.     //----------------------------------------------------------------------
  601.     public function update_rssline($name) {
  602.    
  603.        $line_id = $this->get_rssline_num($name);
  604.        if ($line_id===null)
  605.           throw new Exception ('error getting rss line');
  606.        
  607.      
  608.        $url = $this->get_rssline_url($line_id);
  609.        
  610.        return $this->save_rsss($url, $line_id);
  611.     }
  612.  
  613.     //----------------------------------------------------------------------
  614.      public function exceptionHandler($e) {
  615.        
  616.            $this->lasterror .= 'exception: '.$e->getMessage().'<br/><br/>';
  617.         }
  618.        
  619.     //----------------------------------------------------------------------
  620.      public function errorHandler($errno, $errstr, $errfile, $errline) {
  621.        
  622.            $this->lasterror .= "error occured: errno:$errno errstr:$errstr
  623.            errfile:$errfile errline:$errline<br/><br/>";
  624.                
  625.            return true;
  626.         }
  627.     //----------------------------------------------------------------------
  628.     public function __construct() {
  629.        
  630.        //set_exception_handler(array($this,'exceptionHandler'));
  631.        //set_error_handler(array($this, 'errorHandler'));
  632.        
  633.        $this->_db = $this->get_mysqli();
  634.        $this->rss_lines = $this->_db->prefix.'dkrss_rss_lines';
  635.        $this->rss_content = $this->_db->prefix.'dkrss_rss_rsss';
  636.        $this->digests = $this->_db->prefix.'dkrss_digests';
  637.        $this->digests_elements = $this->_db->prefix.'dkrss_digests_elements';
  638.     }
  639.     //----------------------------------------------------------------------
  640.      public function __destruct() {
  641.            
  642.            //echo $this->lasterror;
  643.         }
  644.    
  645.    
  646.    
  647.  }
  648.  
  649.  
  650. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement