Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Rss {
- private $_db;
- private $lasterror='';
- //----------------------------------
- private function get_mysqli() {
- return new mysqli('','','','');
- }
- //----------------------------------
- private function check_sql_error($agent='') {
- if ($agent==='')
- $agent = $this->_db;
- if ($agent->errno!=0) {
- throw new Exception ('database error: '.$agent->error);
- }
- }
- //----------------------------------
- private function check_auth() {
- if (!$this->is_logged())
- throw new Exception('authorization error');
- }
- //----------------------------------
- public function mk_tables() {
- $this->check_auth();
- $sqls = array ('drop table if exists rss_lines',
- 'drop table if exists rss_rsss',
- 'create table rss_lines (id int(11) not null auto_increment,
- line_name varchar(50), line_url varchar(500), primary key(`id`))',
- 'create table rss_rsss (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`))');
- while (list($key,$sq)= each($sqls)) {
- $this->_db->query($sq);
- $this->check_sql_error();
- }
- return 'operation completed';
- }
- //----------------------------------
- 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 is_logged() {
- if (isset($_SESSION['logged_in']))
- return $_SESSION['logged_in'];
- return false;
- }
- //----------------------------------
- private function get_rssline_num($name) {
- $res = $this->_db->query(
- "select min(id) from rss_lines where line_name='".
- $this->_db->real_escape_string($name)."'");
- $this->check_sql_error();
- if ($row = $res->fetch_array(MYSQL_NUM))
- $num = $row[0];
- else
- $num=false;
- $res->close();
- return $num;
- }
- //----------------------------------
- private function get_rssline_url($id) {
- $res = $this->_db->query(
- "select line_url from rss_lines where id=".$id);
- $this->check_sql_error();
- if ($row = $res->fetch_array(MYSQL_NUM))
- $num = $row[0];
- else
- $num=false;
- $res->close();
- return $num;
- }
- //----------------------------------
- private function check_hash($hash) {
- $res = $this->_db->query('select * from rss_rsss where pubhash="'.$hash.'"');
- $this->check_sql_error();
- if ($row = $res->fetch_array(MYSQL_NUM)) {
- $res->close();
- return true;
- }
- else {
- $res->close();
- return false;
- }
- }
- //----------------------------------
- private function getNRows($id) {
- $res = $this->_db->query('select * from rss_rsss where line_id='.
- $this->_db->real_escape_string($id));
- $this->check_sql_error();
- $nrows = $res->num_rows;
- $res->close();
- return $nrows;
- }
- //----------------------------------
- public function greeting() {
- return 'ready';
- }
- //----------------------------------
- public function show_rsslines() {
- $res = $this->_db->query('select id, line_name from rss_lines order by id desc');
- $this->check_sql_error();
- ob_start();
- echo '<form id="lineform"><ul class="twitlines">';
- while ($row = $res->fetch_object()) {
- 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 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='') {
- $this->check_auth();
- if ($name=='') {
- $isedit = false;
- $line_name = '';
- $line_url = '';
- $line_id = 0;
- }
- else {
- $isdeit = true;
- $num = $this->get_rssline_num($name);
- if ($num===false)
- throw new Exception ('error getting rss line');
- $res = $this->_db->query('select * from rss_lines where id='.$num);
- $this->check_sql_error();
- if (!$row = $res->fetch_assoc())
- throw new Exception ('error getting rss line info');
- $res->close();
- $line_name = $row['line_name'];
- $line_url = $row['line_url'];
- $line_id = $row['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) {
- $this->check_auth();
- if (!$name)
- throw new Exception('line_name parameter is empty');
- $num = $this->get_rssline_num($name);
- if ($num===false)
- 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() {
- $this->check_auth();
- if ($_POST['line_id']==0) {
- $stmt = $this->_db->prepare(
- 'insert into rss_lines(line_name, line_url) values(?,?)');
- $stmt->bind_param('ss', $_POST['line_name'], $_POST['line_url']);
- $stmt->execute();
- $this->check_sql_error($stmt);
- $id = $stmt->insert_id;
- $stmt->close();
- return "record with number $id inserted";
- } else {
- $stmt = $this->_db->prepare(
- 'update rss_lines set line_name=?, line_url=? where id=?');
- $stmt->bind_param('ssi', $_POST['line_name'], $_POST['line_url'], $_POST['line_id']);
- $stmt->execute();
- $this->check_sql_error($stmt);
- $affected = $stmt->affected_rows;
- $stmt->close();
- return " $affected records updated";
- }
- }
- //----------------------------------
- public function delete_rssline() {
- $this->check_auth();
- $stmt = $this->_db->prepare(
- 'delete from rss_lines where id=?');
- $stmt->bind_param('i', $_POST['line_id']);
- $stmt->execute();
- $this->check_sql_error($stmt);
- $affected = $stmt->affected_rows;
- $stmt->close();
- return " deleted rows $affected";
- }
- //----------------------------------
- public function show_loginout_form() {
- if (!$this->is_logged()) {
- $frm ='<form rel="_loginout" '.
- 'enctype="application/x-www-form-urlencoded">'.
- '<p>input password <input type="text" name="pass" /></p>'.
- '<p><input type="submit" name="submit" value="login" />'.
- '<input type="button" rel="_greeting" value="cancel"></p></form>';
- }
- else {
- parse_str($_SERVER['QUERY_STRING'], $getvars);
- $action = $getvars['action'];
- $action = 'greeting';
- $frm = '<form rel="_loginout" '.
- 'enctype="application/x-www-form-urlencoded">'.
- '<p>log out, are you sure? '.
- '<input type="submit" name="logout" value="logout" />'.
- '<input type="button" rel="_'.$action.'" value="cancel" />'.
- '</p></form>';
- }
- return $frm;
- }
- //----------------------------------
- public function loginstatus() {
- $s = $this->is_logged() ? 'logged in' : 'logged out';
- $s4 = $this->is_logged() ? 'log out' : 'log in';
- return $s.'<br/><a href="#" rel="_show_loginout_form">'.$s4.'</a>';
- }
- //----------------------------------
- public function loginout() {
- if ($this->is_logged()===false) {
- if ($_POST['pass']=='') {
- $_SESSION['logged_in']=true;
- return 'successfully authorized';
- } else {
- return 'invalid password, '.
- '<a href="#" rel="_show_loginout_form">try again</a>';
- }
- } else {
- if (isset($_SESSION['logged_in']))
- unset($_SESSION['logged_in']);
- }
- }
- //----------------------------------
- public function show_rssdate($name) {
- $num = $this->get_rssline_num($name);
- $npages = ceil($this->getNRows($num)/100);
- if ($num===false)
- throw new Exception ('error getting rss line');
- $stmt = $this->_db->prepare(
- 'select id, title, description, link, '.
- 'DATE_FORMAT(pubdate,\'%d.%m.%Y %H:%i\') from rss_rsss '.
- ' where line_id=? and '.
- 'DATE_FORMAT(pubdate,\'%d.%m.%Y\')=? order by pubdate');
- $this->check_sql_error($stmt);
- $stmt->bind_param('is',$num, $_GET['date']);
- $stmt->execute();
- $stmt->bind_result($id, $title, $description, $link, $pubdate);
- //---------------------------
- $out = array();
- while ($stmt->fetch()) {
- $el = array();
- $el["twitid"]=$id;
- $el["twittitle"]=$title;
- $el["twitdesc"]=$description;
- $el["twitlink"]=$link;
- $el["twitpubdate"]=$pubdate;
- $out[]=$el;
- }
- $stmt->close();
- return json_encode($out);
- }
- //----------------------------------
- public function show_rssline($name) {
- $num = $this->get_rssline_num($name);
- $npages = ceil($this->getNRows($num)/100);
- if ($num===false)
- throw new Exception ('error getting rss line');
- $stmt = $this->_db->prepare(
- 'select id, title, description, link, '.
- 'DATE_FORMAT(pubdate,\'%d.%m.%Y %H:%i\') from rss_rsss '.
- ' where line_id=? order by pubdate desc');
- $stmt->bind_param('i',$num);
- $stmt->execute();
- $stmt->bind_result($id, $title, $description, $link, $pubdate);
- //---------------------------
- if (isset($_GET['page'])) {
- if ($_GET['page']>1) {
- for ($i=0; $i<($_GET['page']-1)*100; $i++)
- $stmt->fetch();
- }
- }
- //---------------------------
- 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 (($stmt->fetch()) && ($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>",
- $id, $title, $description, $link, $link, $pubdate);
- $j++;
- }
- echo '</ul>';
- $s = ob_get_clean();
- $stmt->close();
- return $s;
- }
- //----------------------------------------------------------------------
- public function clear_rssline($name) {
- $line_id = $this->get_rssline_num($name);
- if ($line_id===false)
- throw new Exception ('error getting rss line');
- $this->_db->query('delete from rss_rsss where line_id='.$line_id);
- $this->check_sql_error();
- return 'rss line cleared, '.$this->_db->affected_rows.' 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) {
- $stmt = $this->_db->prepare (
- 'insert into rss_rsss(line_id, title, description, link, pubdate, pubhash)'.
- 'values (?,?,?,?,?,?)');
- $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);
- $stmt->bind_param('isssss', $line_id, $title, $desc, $link, $pubdate4, $pubhash);
- $stmt->execute();
- $this->check_sql_error();
- }
- $stmt->close();
- return 'inserted '.count($out).' rsss : '.$url;
- }
- //----------------------------------------------------------------------
- public function update_rssline($name) {
- $line_id = $this->get_rssline_num($name);
- if ($line_id===false)
- 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() {
- session_start();
- set_exception_handler(array($this,'exceptionHandler'));
- set_error_handler(array($this, 'errorHandler'));
- $this->_db = $this->get_mysqli();
- }
- //----------------------------------
- public function __destruct() {
- $this->_db->close();
- echo $this->lasterror;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement