<?php
define
('S2SNVERSION','0.1.1');
$url='';
$title='';
$msgerror='';
if (
$_SERVER['REQUEST_METHOD'] == 'POST') {
    
$url=$_POST['url'];
    
$title=$_POST['title'];
    
$snurl=$_POST['snurl'];
    if(empty(
$snurl)) {
        
$msgerror="Not a valid URL";
    } else {
        if (
$db = new SQLite3('knownsn.db')) {
            
            
$qry sprintf("SELECT id FROM statusnet where url = '%s' "$snurl);
            
$result = @$db->querySingle($qry);
        
            if(!
$result) {
                
$ok checkSN($snurl);
                if(!
$ok) {
                    
$msgerror="Not a valid URL";
                } else {
                    
$updHit sprintf("INSERT INTO statusnet (url,hits) VALUES ('%s',0)"$snurl);
                    @
$db->exec($updHit);
                    
$instanceid $db->lastInsertRowID();
                    
$status=$title " " $url;
                    
redirect($instanceid,$status);
                }
            } else {
                
$status=$title " " $url;
                
redirect($result,$status);
            }
        }
        
    }
} else {
    
$url= isset($_GET['url']) ? $_GET['url'] : '';
    
$title=isset($_GET['title']) ? $_GET['title'] : '';
    if( (isset(
$_COOKIE['share2sn']) || isset($_GET['snid'])) && !empty($url) ) {
        
$instanceid = (isset($_GET['snid'])) ? $_GET['snid'] : $_COOKIE['share2sn'];
        
$status=$title " " $url;
        
$ret redirect($instanceid,$status);
        if(!
$ret) {
            
$msgerror='Hum.. looks like I can\'t redirect..';
            if(isset(
$_GET['clearcookie'])) {
                        
setcookie('share2sn'""time()-3600);
                        unset(
$_COOKIE['share2sn']);
                }
        }
    }
    if (isset(
$_GET['source'])) {
            
showSource(isset($_GET['highlight']));
        }

    if(isset(
$_GET['clearcookie'])) {
        
setcookie('share2sn'""time()-3600);
        unset(
$_COOKIE['share2sn']);
    }
    
}

function 
checkSN($url) {
    
$ret file_get_contents($url."/api/statusnet/version.json");
    if (
false === $ret) return false;
        return 
json_decode($ret);
}

function 
showSource($highlight false) {
        if (
$highlight) {
            echo 
'<div id="source">';
            
highlight_file(__FILE__);
            echo 
'</div>';
        } else {
            
header('Content-Type: text/plain; charset=UTF-8');
            
readfile(__FILE__);
            
        }
    die();
    }

function 
redirect($sninstance$status) {
    
    if (
$db = new SQLite3('knownsn.db')) {
        
$qry sprintf('SELECT url FROM statusnet where id = %d '$sninstance);
        
$result = @$db->querySingle($qry);
        
        if(
$result) {
            
$updHit sprintf('UPDATE statusnet SET hits = hits+1 WHERE id = %d '$sninstance);
            @
$db->exec($updHit);
            
$link $result.'/notice/new?status_textarea='.urlencode($status);
            
header('HTTP/1.1 303 See Other');
            
header("Location: $link");
            
setcookie('share2sn',$sninstance,time()+60*60*24*30*12*5);
            die();
        } else {
            
$msgerror"Failed to fetch statusnet URL for id "$sninstance;
        }
    }
    return 
false;
}

?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
  <title>share to statusnet</title>
  <style>
body {
  background-color: #666;
  margin: 0;
  padding: 0;
  font-family: verdana, arial, helvetica, sans-serif;
}
a {
  text-decoration: none;
  color: #666;
}
a:VISITED {
  color: #666;
}
#content {
  margin: 0 auto;
  width: 980px;
  padding: 20px 20px 0 20px;
  background-color: #fff;
  -webkit-border-bottom-right-radius: 25px;
  -webkit-border-bottom-left-radius: 25px;
  -moz-border-radius-bottomright: 25px;
  -moz-border-radius-bottomleft: 25px;
  border-bottom-right-radius: 25px;
  border-bottom-left-radius: 25px;
  background-color: #EDEDED;
}
#snsites {
  float: left;
  width: 300px;
  border-right-color: #666;
  border-right-width: 1px;
  border-right-style: solid;
  min-height: 300px;
}
#snform {
  float: left;
  margin-left: 10px;
  width: 650px;
  
}
#sharetext {
  border-width: 1px;
  border-color: #666;
  border-style: solid;
  background-color: #FFF;
  padding: 5px;
}
#footer {
  margin-top: 30px;
  padding-top: 30px;
  padding-bottom: 30px;
  text-align: center;
  width: 100%;
  border-top-color: #666;
  border-top-width: 1px;
  border-top-style: solid;
}
.clear {
  clear: both;
}
  </style>
  <meta name="viewport" content="width=1100"/>
 </head>
 <body>
 <div id="content"> 
    <h2>share to statusnet</h2>
<?php
    
if(empty($url)) {
        echo 
'<p>Hi! To use this service, just call this page with 2 parameters: url (required) and title (optional)</p>';
        echo 
'<p>for example: <a href="?url='.urlencode('http://macno.org/share2sn/').'&title='.urlencode('share to sn').'">Share this!</a>';

        if(isset(
$_COOKIE['share2sn'])) {
            if (
$db = new SQLite3('knownsn.db')) {
                
$qry sprintf('SELECT url FROM statusnet where id = %d '$_COOKIE['share2sn']);
                
$result = @$db->querySingle($qry);
        
                if(
$result) {
                    echo 
'<p>You are actually sharing to ' $result ' <a href="?clearcookie">reset it?</a></p>';
                }
            }
        }
    } else {
        if (
$db = new SQLite3('knownsn.db')) {
            echo 
"<div id=\"snsites\">\n";
            echo 
"<h3>StatusNet instances</h3>";
            echo 
"<ul>";
            
$q = @$db->query('SELECT id, url FROM statusnet order by hits desc');
                if (
$q === false) {
                    
$db->exec('CREATE TABLE statusnet (id INTEGER PRIMARY KEY, url TEXT, hits INTEGER); INSERT INTO statusnet (id,url,hits) VALUES (1,\'http://identi.ca\',1000);');
                    echo 
'<li><a href="?snid=1&url='.$url.'&title='.$title.'">identi.ca</a></li>' ;
                } else {
                while (
$entry $q->fetchArray()) {
                    echo 
'<li><a href="?snid='.$entry['id'].'&url='.urlencode($url).'&title='.urlencode($title).'">'.$entry['url'].'</a></li>' ;
                }
            }
            echo 
'</ul></div>';
            echo 
'<div id="snform">';
            echo 
'<div id="status"><h4>Sharing</h4><div id="sharetext">' $title " " $url '</div></div>';
            echo 
'<h3>Add new StatusNet instance</h3>';
            if(!empty(
$msgerror)) {
                echo 
'<p>'.$msgerror.'</p>';
            }
            echo 
'<form method="POST">';
            echo 
'<input type="text" name="snurl" size="30" id="snurl" />';
            echo 
'<input type="hidden" name="url" value="'.$url.'" />';
            echo 
'<input type="hidden" name="title" value="'.$title.'" />';
            echo 
'<input type="submit" name="Add" value="Add" />';
            echo 
'</form>';
            echo 
'</div>';
        } else {
            echo(
'<p class="error">Failed to open db</p>');

        }
    }
?>
  <div class="clear"></div>
  <div id="footer">
    share2sn <?php echo(S2SNVERSION);?> by <a href="http://stream.macno.org/macno">@macno</a> Check out <a href="?source">source</a> (<a href="?source&highlight">highlighted</a>)
  </div>
  </div>
 </body>
</html>