Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $db = mysql_connect('127.0.0.1', 'user', 'pass');
- if ($db === false){
- die ('Error connecting to database');
- }
- mysql_select_db('link_db', $db) || die('Error selecting database');
- $method = $_SERVER['REQUEST_METHOD'];
- if (( ($method == 'GET') && (!isset($_GET['changes']))) ||
- ($method == 'POST') && (!isset($_GET['add']))){
- // Site for browsers
- ?>
- <html>
- <head>
- <title>Link node test</title>
- </head>
- <body>
- <?php
- $q = mysql_query('select count(*) from links');
- $r = mysql_fetch_array($q);
- if ($r[0] == 0){
- echo 'Nothing here...';
- }
- else{
- echo $r[0] . ' links';
- }
- ?>
- </body>
- </html>
- <?php
- mysql_close($db);
- exit(0);
- }
- else if ($method == 'GET'){ // GET /<interface>?changes...
- header("Content-type: text/plain");
- $since = 0;
- if (isset($_GET['since'])){
- $since = intval($_GET['since']);
- }
- /* Links added */
- $q = mysql_query('select title, url, UNIX_TIMESTAMP(sent_on) as usent_on, sent_by, signature'.
- ' from links where sent_on >= FROM_UNIXTIME('.$since.')',
- $db);
- if ($q == false){
- die("Error querying database ".mysql_error($db));
- }
- //echo mysql_num_rows($q)."\n";
- while (($r = mysql_fetch_assoc($q))){
- echo "ADD\n";
- echo "title:". $r['title']. "\n";
- echo "url:". $r['url']. "\n";
- echo "sent_on:". $r['usent_on']. "\n";
- if (isset($r['sent_by'])){
- echo "sent_by:". $r['sent_by']. "\n";
- }
- else{
- echo "sent_by:none\n";
- }
- if (isset($r['signature'])){
- echo "signature:". $r['signature'] ."\n";
- }
- else{
- echo "signature:none\n";
- }
- echo ".\n";
- }
- }
- else if($method == 'POST'){
- // Clean parameters
- $timestamp = intval($_POST['timestamp']);
- $signature = mysql_real_escape_string($_POST['signature']);
- $link = mysql_real_escape_string($_POST['link']);
- $sender = mysql_real_escape_string($_POST['sender']);
- $title = mysql_real_escape_string($_POST['title']);
- if ($sender == 'none'){
- $sender = 'NULL';
- }
- else{
- $sender = "'$sender'";
- }
- if ($signature == 'none'){
- $signature = 'NULL';
- }
- else{
- $signature = "'$signature'";
- }
- // TODO: Check signature, etc, etc...
- $query = 'insert into links(title, url, sent_on, sent_by, signature) values'.
- "('$title', '$link', FROM_UNIXTIME('$timestamp'), $sender, $signature);";
- $q = mysql_query($query, $db);
- if ($q == false){
- die("Error updating database ".mysql_error($db));
- }
- else{
- echo "OK";
- }
- }
- mysql_close($db);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement