Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Rss {
- private $_db;
- private $lasterror='';
- private $rss_lines;
- private $rss_content;
- private $digests;
- private $digests_elements;
- //----------------------------------
- private function get_mysqli() {
- global $wpdb;
- $wpdb->show_errors();
- return $wpdb;
- }
- //----------------------------------
- private function check_sql_error($result) {
- if ($result===false) {
- ob_start();
- $this->_db->print_error();
- $err = ob_get_clean();
- throw new Exception ('database error: '.$err);
- }
- }
- //----------------------------------
- public function mk_tables() {
- require_once(ABSPATH . '/wp-admin/includes/upgrade.php');
- if ($this->_db->get_var("SHOW TABLES LIKE '". $this->digests ."'") == $this->digests)
- return;
- $sqls = array (
- "create table {$this->rss_lines} (id int(11) not null auto_increment,
- line_name varchar(50), line_url varchar(500), primary key(`id`));",
- "create table {$this->rss_content} (id int(11) not null auto_increment,
- line_id int(11),
- title varchar(500), description varchar(1500),
- link varchar(500), pubdate datetime, pubhash varchar(500), primary key(`id`));",
- /*"drop table {$this->digests}",
- "drop table {$this->digests_elements}",*/
- "create table {$this->digests} (
- digest_id int(11) not null auto_increment,
- digest_title varchar(50),
- digest_text varchar(500),
- digest_date datetime,
- digest_schedule_period int(11),
- digest_schedule_time varchar(50),
- digest_is_active int(11),
- digest_post_type varchar(50),
- digest_taxonomy varchar(50),
- digest_category_id int(11),
- primary key(`digest_id`));",
- //-----------------------------
- "create table {$this->digests_elements} (
- element_id int(11) not null auto_increment,
- digest_id int(11),
- element_rss_name varchar(20),
- element_page_order int(11),
- element_request_period int(11),
- element_template varchar(2000),
- element_before_template varchar(2000),
- element_after_template varchar(2000),
- primary key(`element_id`));");
- /*$sqls = array (
- "alter table {$this->digests} add column digest_post_type varchar(50);",
- "alter table {$this->digests} add column digest_taxonomy varchar(50);",
- "alter table {$this->digests} add column digest_category_id int(11);");*/
- while (list($key,$sq)= each($sqls)) {
- $this->_db->query($sq);
- //dbDelta($sq);
- }
- }
- //----------------------------------
- private function date_rss2mysql4($s) {
- // Thu, 27 Dec 2012 13:35:18 +0000
- // 2012-12-27 13:35:18
- $ver = phpversion();
- $ver = explode('.',$ver);
- $ver = implode(array_slice($ver,0,2));
- if ($ver>=53) {
- $date = DateTime::createFromFormat('D, d M Y H:i:s O',$s);
- $s = $date->format('Y-m-d H:i:s',$tm);
- } else {
- $tm = strtotime($s);
- $s = date('Y-m-d H:i:s',$tm);
- }
- return $s;
- }
- //----------------------------------
- private function get_rssline_num($name) {
- $res = $this->_db->get_var(
- $this->_db->prepare("select min(id) from {$this->rss_lines} where line_name=%s",$name));
- $this->check_sql_error($res);
- return $res;
- }
- //----------------------------------
- private function get_rssline_url($id) {
- $res = $this->_db->get_var(
- $this->_db->prepare("select line_url from {$this->rss_lines} where id=%d",$id));
- $this->check_sql_error($res);
- return $res;
- }
- //----------------------------------
- private function check_hash($hash) {
- $res = $this->_db->get_row(
- $this->_db->prepare('select * from '.$this->rss_content.' where pubhash=%s',$hash), ARRAY_A);
- $this->check_sql_error($res);
- if ($res!=null) {
- return true;
- }
- else {
- return false;
- }
- }
- //----------------------------------
- private function getNRows($id) {
- $res = $this->_db->get_var($this->_db->prepare('select count(*) from '.$this->rss_content.' where line_id=%d',$id));
- $this->check_sql_error($res);
- $nrows = $res;
- return $nrows;
- }
- //----------------------------------
- public function greeting() {
- return 'ready';
- }
- //----------------------------------
- public function show_rsslines() {
- $res = $this->_db->get_results('select id, line_name from '.$this->rss_lines.' order by id desc');
- $this->check_sql_error($res);
- ob_start();
- echo '<form id="lineform"><ul class="twitlines">';
- foreach ($res as $row) {
- echo '<li><input type="radio" name="line_name" value="'.$row->line_name.'" />'.
- '<a href="#" rel="_show_rssline" lnname="'.$row->line_name.'">'.$row->line_name.'</a></li>';
- }
- echo '</ul></form>';
- $s = ob_get_clean();
- return $s;
- }
- //----------------------------------
- public function get_rsslines() {
- $res = $this->_db->get_col('select line_name from '.$this->rss_lines);
- $this->check_sql_error($res);
- return $res;
- }
- //----------------------------------
- public function show_controls() {
- $s = '<ul class="controls">
- <li><a href="#" rel="_add_rssline_form">add rss feed</a></li>
- <li><a href="#" rel="_edit_rssline_form">edit rss feed</a></li>
- <li><a href="#" rel="_update_rssline">update rss feed</a></li>
- <li><a href="#" rel="_clear_rssline">clear rss feed</a></li>
- <li><a href="#" rel="_delete_rssline_form">delete rss feed</a></li>
- </ul>';
- return $s;
- }
- //----------------------------------
- public function edit_rssline_form($name='') {
- if ($name=='') {
- $isedit = false;
- $line_name = '';
- $line_url = '';
- $line_id = 0;
- }
- else {
- $isdeit = true;
- $num = $this->get_rssline_num($name);
- if ($num===null)
- throw new Exception ('error getting rss line');
- $res = $this->_db->get_row($this->_db->prepare('select * from '.$this->rss_lines.' where id=%d',$num));
- $this->check_sql_error($res);
- if ($res===null)
- throw new Exception ('error getting rss line info');
- $line_name = $res->line_name;
- $line_url = $res->line_url;
- $line_id = $res->id;
- }
- ob_start();
- echo '<form rel="_save_rssline" '.
- 'enctype="application/x-www-form-urlencoded">'.
- '<p> input rss feed name: '.
- '<input type="text" name="line_name" value="'.$line_name.'" /></p>'.
- '<p> input rss feed url: '.
- '<input type="text" name="line_url" value="'.$line_url.'" /></p>'.
- '<input type="hidden" name="line_id" value="'.$line_id,'" />'.
- '<p><input type="submit" name="submit" value="save"/>'.
- '<input type="button" rel="_greeting" value="cancel" /></p></form>';
- $s = ob_get_contents();
- ob_end_clean();
- return $s;
- }
- //----------------------------------
- public function add_rssline_form() {
- return $this->edit_rssline_form();
- }
- //----------------------------------
- public function delete_rssline_form($name) {
- if (!$name)
- throw new Exception('line_name parameter is empty');
- $num = $this->get_rssline_num($name);
- if ($num===null)
- throw new Exception ('error getting rss line');
- $s = '<form rel="_delete_rssline" '.
- 'enctype="application/x-www-form-urlencoded">'.
- '<input type="hidden" name="line_id" value="'.$num.'" />'.
- '<p><input type="submit" name="submit" value="delete"/>'.
- '<input type="button" rel="_greeting" value="cancel"></p></form>';
- return $s;
- }
- //----------------------------------
- public function save_rssline() {
- if ($_POST['line_id']==0) {
- $rez = $this->_db->insert( $this->rss_lines,
- array (
- 'line_name'=>$_POST['line_name'],
- 'line_url'=>$_POST['line_url']
- ),
- array ('%s','%s'));
- $this->check_sql_error($rez);
- $id = $this->_db->insert_id;
- return "record with number $id inserted";
- } else {
- $rez = $this->_db->update( $this->rss_lines,
- array (
- 'line_name'=>$_POST['line_name'],
- 'line_url'=>$_POST['line_url']
- ),
- array('id'=> $_POST['line_id']),
- array('%s','%s'), array('%d'));
- $this->check_sql_error($rez);
- $affected = $rez;
- return " $affected records updated";
- }
- }
- //----------------------------------
- public function delete_rssline() {
- $rez = $this->_db->query( $this->_db->prepare(
- 'delete from '.$this->rss_lines.' where id=%d',$_POST['line_id']));
- $this->check_sql_error($rez);
- $affected = $rez;
- return " deleted rows $affected";
- }
- //----------------------------------
- public function show_rssdate($curdate, $name, $indate=1) {
- $num = $this->get_rssline_num($name);
- $npages = ceil($this->getNRows($num)/100);
- if ($num===null)
- throw new Exception ('error getting rss line');
- if (is_numeric($indate)) {
- $rez = $this->_db->get_results(
- 'select id, title, description, link,'.
- 'DATE_FORMAT(pubdate,\'%d.%m.%Y %H:%i\') pubdate4 from '.$this->rss_content.
- ' where line_id='.$num.' and pubdate<DATE(\''.$curdate.'\') '.
- ' and DATEDIFF(DATE(\''.$curdate.'\'), pubdate)<'.$indate);
- }
- else
- $rez = $this->_db->get_results($this->_db->prepare(
- 'select id, title, description, link, '.
- 'DATE_FORMAT(pubdate,\'%d.%m.%Y %H:%i\') pubdate4 from '.$this->rss_content.
- ' where line_id=%d and '.
- 'DATE_FORMAT(pubdate,\'%d.%m.%Y\')=%s order by pubdate',$num,$indate));
- $this->check_sql_error($rez);
- //---------------------------
- $out = array();
- foreach ($rez as $elz) {
- $el = array();
- $el["twitid"]=$elz->id;
- $el["twittitle"]=$elz->title;
- $el["twitdesc"]=$elz->description;
- $el["twitlink"]=$elz->link;
- $el["twitpubdate"]=$elz->pubdate4;
- $out[]=$el;
- }
- return $rez;
- }
- //----------------------------------
- public function show_rssline($name) {
- $num = $this->get_rssline_num($name);
- $npages = ceil($this->getNRows($num)/100);
- if ($num===null)
- throw new Exception ('error getting rss line');
- $rez = $this->_db->get_results(
- 'select id, title, description, link, '.
- 'DATE_FORMAT(pubdate,\'%d.%m.%Y %H:%i\') pubdate4 from '.$this->rss_content.
- ' where line_id='.$num.' order by pubdate desc');
- $this->check_sql_error($rez);
- if ($rez===null) {
- echo 'Empty result. Click "update rss feed"';
- return;
- }
- //---------------------------
- $jz = 0;
- if (isset($_GET['page'])) {
- if ($_GET['page']>1) {
- $jz = ($_GET['page']-1)*100;
- }
- }
- //---------------------------
- ob_start();
- echo '<ul class="pager">';
- for ($i=0; $i<$npages; $i++) {
- echo '<li><a href="#" rel="_show_rssline" pg="'.($i+1).
- '" lnname="'.$name.'">'.
- ($i+1).'</a></li>';
- }
- echo '</ul>';
- //---------------------------
- $j=0;
- echo '<ul class="twitfeed">';
- while (($jz<count($rez)) && ($j<100)) {
- printf ("<li>
- <div class=\"twitid\">%s</div>
- <div class=\"twittitle\">%s</div>
- <div class=\"twitdesc\">%s</div>
- <div class=\"twitlink\"><a href=\"%s\" target=\"_blank\">%s</a></div>
- <div class=\"twitpubdate\">%s</div>
- </li>",
- $rez[$jz]->id, $rez[$jz]->title,
- $rez[$jz]->description, $rez[$jz]->link,
- $rez[$jz]->link, $rez[$jz]->pubdate4);
- $j++;
- $jz++;
- }
- echo '</ul>';
- $s = ob_get_clean();
- return $s;
- }
- //----------------------------------------------------------------------
- public function clear_rssline($name) {
- $line_id = $this->get_rssline_num($name);
- if ($line_id===null)
- throw new Exception ('error getting rss line');
- $rez = $this->_db->query($this->_db->prepare('delete from '.$this->rss_content.' where line_id=%d',$line_id));
- $this->check_sql_error($rez);
- return 'rss line cleared, '.$rez.' deleted';
- }
- //----------------------------------------------------------------------
- private function fpatch($que) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $que);
- curl_setopt($ch, CURLOPT_TIMEOUT, 10);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_USERAGENT,
- "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $inf = curl_exec($ch);
- if (curl_errno($ch)) {
- throw new Exception ('curl exception: '.curl_error($ch));
- }
- curl_close($ch);
- if (substr($inf,0,1)!='<') {
- $inf = substr($inf,strpos($inf,'<'));
- }
- preg_match_all('/(http(([^<"\'\?])*?)\?)(([^<"\'])*)/ims', $inf, $z,
- PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
- $outs = '';
- $start = 0;
- foreach ($z as $el) {
- $outs .= substr($inf, $start, $el[0][1]-$start).
- $el[1][0].str_replace('&','&',$el[4][0]);
- $start = $el[0][1]+strlen($el[0][0]);
- }
- $outs .= substr($inf, $start);
- return $outs;
- }
- //----------------------------------------------------------------------
- private function getrss($rssquery,$count) {
- $ver=1;
- $rez='';
- $outs = $this->fpatch($rssquery);
- $xml=simplexml_load_string($outs);
- $i=1;
- try {
- foreach ($xml->children() as $a=>$b) {
- if ($a=='channel') {
- $ver=2;
- }
- }
- $out = array();
- if ($ver==1) {
- $base = $xml->entry;
- $content = 'content';
- $pub = 'published';
- }
- else {
- $base = $xml->channel->item;
- $content = 'description';
- $pub = 'pubDate';
- }
- //--------------------------------------
- foreach ($base as $item) {
- $title=$item->title;
- $inf = array();
- $inf['title'] = (string)$item->title;
- $inf['description'] = (string)$item->$content;
- $att = 'href';
- $attr = $item->link->attributes()->$att;
- if ($attr)
- $inf['link']= (string)$attr;
- else
- $inf['link']= (string)$item->link;
- $inf['pubdate']=(string)$item->$pub;
- $out[]=$inf;
- $i++;
- if ($i>$count)
- break;
- }}
- catch (Exception $e) {
- return $e->getMessage();
- }
- return $out;
- }
- //----------------------------------------------------------------------
- private function save_rsss($url, $line_id) {
- $out = $this->getrss($url,500);
- foreach ($out as $item) {
- extract($item);
- $pubhash =md5($title.$pubdate);
- if ($this->check_hash($pubhash))
- continue;
- $pubdate4 = $this->date_rss2mysql4($pubdate);
- $desc = preg_replace('/style=(["\'])[^\1]*?\1/ims','',$description);
- $rez = $this->_db->insert( $this->rss_content,
- array(
- 'line_id'=> $line_id,
- 'title'=> $title,
- 'description'=> $desc,
- 'link'=> $link,
- 'pubdate'=> $pubdate4,
- 'pubhash'=> $pubhash),
- array('%d','%s','%s','%s','%s','%s'));
- $this->check_sql_error($rez);
- }
- return 'inserted '.count($out).' rsss : '.$url;
- }
- //----------------------------------------------------------------------
- public function update_rssline($name) {
- $line_id = $this->get_rssline_num($name);
- if ($line_id===null)
- throw new Exception ('error getting rss line');
- $url = $this->get_rssline_url($line_id);
- return $this->save_rsss($url, $line_id);
- }
- //----------------------------------------------------------------------
- public function exceptionHandler($e) {
- $this->lasterror .= 'exception: '.$e->getMessage().'<br/><br/>';
- }
- //----------------------------------------------------------------------
- public function errorHandler($errno, $errstr, $errfile, $errline) {
- $this->lasterror .= "error occured: errno:$errno errstr:$errstr
- errfile:$errfile errline:$errline<br/><br/>";
- return true;
- }
- //----------------------------------------------------------------------
- public function __construct() {
- //set_exception_handler(array($this,'exceptionHandler'));
- //set_error_handler(array($this, 'errorHandler'));
- $this->_db = $this->get_mysqli();
- $this->rss_lines = $this->_db->prefix.'dkrss_rss_lines';
- $this->rss_content = $this->_db->prefix.'dkrss_rss_rsss';
- $this->digests = $this->_db->prefix.'dkrss_digests';
- $this->digests_elements = $this->_db->prefix.'dkrss_digests_elements';
- }
- //----------------------------------------------------------------------
- public function __destruct() {
- //echo $this->lasterror;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement