Advertisement
Guest User

Untitled

a guest
Mar 24th, 2012
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.86 KB | None | 0 0
  1. <?php
  2.  
  3. $db = mysql_connect('127.0.0.1', 'user', 'pass');
  4.  
  5. if ($db === false){
  6.     die ('Error connecting to database');
  7. }
  8.  
  9. mysql_select_db('link_db', $db) || die('Error selecting database');
  10.  
  11. $method = $_SERVER['REQUEST_METHOD'];
  12. if (( ($method == 'GET')  && (!isset($_GET['changes']))) ||
  13.       ($method == 'POST') && (!isset($_GET['add']))){
  14.     // Site for browsers
  15. ?>
  16. <html>
  17.     <head>
  18.         <title>Link node test</title>
  19.     </head>
  20. <body>
  21. <?php
  22.     $q = mysql_query('select count(*) from links');
  23.  
  24.     $r = mysql_fetch_array($q);
  25.    
  26.     if ($r[0] == 0){
  27.         echo 'Nothing here...';
  28.     }
  29.     else{
  30.         echo $r[0] . ' links';
  31.     }
  32.    
  33. ?>
  34. </body>
  35. </html>
  36.  
  37. <?php
  38.     mysql_close($db);
  39.     exit(0);
  40. }
  41. else if ($method == 'GET'){ // GET /<interface>?changes...
  42.     header("Content-type: text/plain");
  43.  
  44.     $since = 0;
  45.     if (isset($_GET['since'])){
  46.         $since = intval($_GET['since']);
  47.     }
  48.  
  49.     /* Links added */
  50.     $q = mysql_query('select title, url, UNIX_TIMESTAMP(sent_on) as usent_on, sent_by, signature'.
  51.                      ' from links where sent_on >= FROM_UNIXTIME('.$since.')',
  52.                       $db);
  53.    
  54.     if ($q == false){
  55.         die("Error querying database ".mysql_error($db));
  56.     }
  57.    
  58.     //echo mysql_num_rows($q)."\n";
  59.     while (($r = mysql_fetch_assoc($q))){
  60.         echo "ADD\n";
  61.         echo "title:". $r['title']. "\n";
  62.         echo "url:". $r['url']. "\n";
  63.         echo "sent_on:". $r['usent_on']. "\n";
  64.  
  65.         if (isset($r['sent_by'])){
  66.             echo "sent_by:". $r['sent_by']. "\n";
  67.         }
  68.         else{
  69.             echo "sent_by:none\n";
  70.         }
  71.  
  72.         if (isset($r['signature'])){
  73.             echo "signature:". $r['signature'] ."\n";
  74.         }
  75.         else{
  76.             echo "signature:none\n";
  77.         }
  78.        
  79.         echo ".\n";
  80.     }
  81. }
  82.  
  83. else if($method == 'POST'){
  84.    
  85.     // Clean parameters
  86.     $timestamp = intval($_POST['timestamp']);
  87.     $signature = mysql_real_escape_string($_POST['signature']);
  88.     $link      = mysql_real_escape_string($_POST['link']);
  89.     $sender    = mysql_real_escape_string($_POST['sender']);
  90.     $title     = mysql_real_escape_string($_POST['title']);
  91.    
  92.     if ($sender == 'none'){
  93.         $sender = 'NULL';
  94.     }
  95.     else{
  96.         $sender = "'$sender'";
  97.     }
  98.    
  99.     if ($signature == 'none'){
  100.         $signature = 'NULL';
  101.     }
  102.     else{
  103.         $signature = "'$signature'";
  104.     }
  105.  
  106.     // TODO: Check signature, etc, etc...
  107.     $query = 'insert into links(title, url, sent_on, sent_by, signature) values'.
  108.              "('$title', '$link', FROM_UNIXTIME('$timestamp'), $sender, $signature);";
  109.  
  110.  
  111.     $q = mysql_query($query, $db);
  112.    
  113.     if ($q == false){
  114.         die("Error updating database ".mysql_error($db));
  115.     }
  116.     else{
  117.         echo "OK";
  118.     }
  119.  
  120. }
  121.  
  122. mysql_close($db);
  123. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement