Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //--------------------------------------------
- function get_mysqli() {
- return new mysqli('','','','');
- }
- //--------------------------------------------
- function mkbase() {
- $sqls = array ('drop table if exists twit',
- 'drop table if exists twits',
- 'create table twit(id int(11) not null auto_increment,
- title varchar(500), description varchar(500),
- link varchar(500), pubdate datetime, primary key(`id`) )');
- $msq = get_mysqli();
- while (list($key,$sq) = each($sqls)) {
- echo 'executing sql: '.$sq.'<br/><br/>';
- $msq->query($sq);
- if ($msq->errno!=0) {
- echo 'error '.$msq->error.'<br/><br/>';
- } else
- echo 'executed successfully<br/><br/>';
- }
- $msq->close();
- }
- //--------------------------------------------
- //--------------------------------------------
- function get_twits($since) {
- global $context;
- global $baseaddr;
- echo $since.'<br/>';
- $addr = sprintf($baseaddr, $since);
- if ($since==0)
- $addr = substr($addr, 0, strrpos($addr,'&'));
- echo $addr.'<br/><br/>';
- //$s = file_get_contents($addr, false, $context);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $addr);
- 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);
- $s = curl_exec($ch);
- if (curl_errno($ch))
- throw new Exception('curl error: '.curl_error($ch));
- echo '<br/><br/>'.$s.'<br/><br/>';
- curl_close($ch);
- return $s;
- }
- //--------------------------------------------
- function parse_xml($inf) {
- $tz = array();
- $xml = simplexml_load_string($inf);
- foreach ($xml->channel->item as $item) {
- $itout = array();
- $itout['title'] = (string)$item->title;
- $itout['description'] = (string)$item->description;
- $itout['link'] = (string)$item->link;
- $itout['pubdate'] = (string)$item->pubDate;
- array_push ($tz, $itout);
- }
- return $tz;
- }
- //--------------------------------------------
- function savetodb($inf) {
- $msq = get_mysqli();
- $stmt = $msq->prepare('insert into twit(title,description,link,pubdate) '.
- 'values(?,?,?,?)');
- for ($i=0; $i<count($inf); $i++) {
- extract($inf[$i]);
- $stmt->bind_param('sssd', $title, $description, $link, $pubdate);
- $stmt->execute();
- if ($stmt->errno!=0)
- echo 'error '.$stmt->error.' while inserting twit: '.$title.'<br/><br/>';
- else
- echo 'inserted '.$stmt->insert_id.' twit: '.$link.'<br/>';
- }
- $stmt->close();
- $msq->close();
- }
- //--------------------------------------------
- function showTwits() {
- $msq = get_mysqli();
- $result = $msq->query('select * from twit order by link desc');
- if ($msq->errno!=0)
- throw new Exception('mysqli error: '.$msq->error);
- echo '<table cellspacing="0" cellpadding="0" border="1" bordercolor="#eee">';
- while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
- echo '<tr><td>'.
- $row['title'].'</td><td>'.$row['link'].'</td><td>'.'</td></tr>';
- }
- echo '</table>';
- $result->close();
- $msq->close();
- }
- //--------------------------------------------
- function clearTwits() {
- session_start();
- if (isset($_SESSION['nstart']))
- unset ($_SESSION['nstart']);
- session_commit();
- $msq = get_mysqli();
- $msq->query('delete from twit');
- if ($msq->errno!=0) {
- throw new Exception('mysqli error: '.$msq->error);
- }
- echo 'twit cleared, '.$msq->affected_rows.' deleted';
- $msq->close();
- }
- //--------------------------------------------
- function getStart() {
- $msq = get_mysqli();
- $result = $msq->query ('select link from twit where id=(select max(id) from twit)');
- if ($msq->errno!=0) {
- echo 'mysql error: '.$msq->error.'<br/><br/>';
- throw new Exception('db error');
- }
- if ($row= $result->fetch_array(MYSQLI_NUM))
- $rez = substr(strrchr($row[0],'/'),1);
- else
- $rez = 0;
- $result->close();
- $msq->close();
- return $rez;
- }
- //--------------------------------------------
- function processTwits() {
- session_start();
- $reload = <<<STR
- <script type="text/javascript">
- el= document.createElement('DIV');
- document.body.appendChild(el);
- el.num = 1;
- el.zz = setInterval( function () {
- el.innerHTML = 'reload after '+el.num+'s';
- el.num--;
- if (el.num == 0) {
- clearInterval(el.zz);
- location.replace(location.href);
- }
- }, 1000);
- </script>
- STR;
- if (isset($_SESSION['nstart'])) {
- $nstart = $_SESSION['nstart'];
- } else {
- $nstart = getStart();
- }
- echo 'starting position: '.$nstart.'<br/><br/>';
- $inf = get_twits($nstart);
- $inf4 = parse_xml($inf);
- savetodb($inf4);
- $link = $inf4[count($inf4)-1]['link'];
- $nstart = substr(strrchr($link,'/'),1);
- echo $nstart;
- $_SESSION['nstart']= $nstart;
- echo $reload;
- }
- //--------------------------------------------
- $baseaddr = 'https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=drunktwi&count=200&max_id=%s';
- $opts = array (
- 'http'=> array(
- 'method'=>'GET',
- 'header'=> "User-Agent: Mozilla/5.0\r\n"));
- $context = stream_context_create($opts);
- if (isset($_GET['mkbase'])) {
- mkbase();
- exit();
- }
- if (isset($_GET['gettwits'])) {
- processTwits();
- exit();
- }
- if (isset($_GET['showtwits'])) {
- showTwits();
- exit();
- }
- if (isset($_GET['cleartwits'])) {
- clearTwits();
- exit();
- }
- if (isset($_GET['clearsess'])) {
- session_start();
- if (isset($_SESSION['nstart']))
- unset ($_SESSION['nstart']);
- session_commit();
- }
- if (isset($_GET['query'])) {
- ?>
- <form action="<? echo $_SERVER["PHP_SELF"]."?query4";?>" method="POST" enctype="application/x-www-form-urlencoded">
- <?php
- echo '<'.'textarea rows="30" cols="30" name="sqgo">'.
- 'zz'.
- '<'.'/textarea>';
- ?>
- <p><input type="text" name="pass"/>
- <p><input type="submit" name="submit" value="go" /></p>
- </form>
- <?php
- exit();
- }
- if (isset($_GET['query4'])) {
- if ($_POST['pass']!='') {
- echo 'inf';
- exit();
- }
- $msq = get_mysqli();
- $result = $msq->query($_POST['sqgo']);
- echo '<table cellspacing="0" cellpadding="0" border="1" bordercolor="#999">';
- $flds = $result->fetch_fields();
- echo '<tr>';
- foreach ($flds as $fld) {
- echo '<td>'.$fld->name.'</td>';
- }
- echo '</tr>';
- while ($row = $result->fetch_array(MYSQLI_NUM)) {
- echo '<tr>';
- foreach ($row as $vall) {
- echo '<td>'.$vall.'</td>';
- }
- echo '</tr>';
- }
- echo '</table>';
- $result->close();
- $msq->close();
- exit();
- }
- session_start();
- ?>
- <html>
- <head>
- <style type="text/css">
- </style>
- </head>
- <body>
- <?php
- echo '<pre>'.print_r($_SESSION,true).'</pre>'; ?>
- <ul><li><a href="<?php echo $_SERVER["PHP_SELF"]."?mkbase";?>">create database</a></li>
- <li><a href="<?php echo $_SERVER["PHP_SELF"]."?gettwits";?>">retrieve twits</a></li>
- <li><a href="<?php echo $_SERVER["PHP_SELF"]."?showtwits";?>">show twits</a></li>
- <li><a href="<?php echo $_SERVER["PHP_SELF"]."?clearsess";?>">clear sess</a></li>
- <li><a href="<?php echo $_SERVER["PHP_SELF"]."?query";?>">query</a></li>
- <li><a href="<?php echo $_SERVER["PHP_SELF"]."?cleartwits";?>">delete twits</a></li></ul>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement