topical media & game development

talk show tell print

professional-web-01-v3-watch.php / php



  <?php
  global HTTP_RAW_POST_DATA;
  if (!isset(HTTP_RAW_POST_DATA))
     HTTP_RAW_POST_DATA = file_get_contents("php://input");
  
  require_once ('db.inc');
  require_once ('watch.inc');
  
  function write() {
    global HTTP_RAW_POST_DATA;
    db=openDb();
    dom = new DOMDocument();
    dom->loadXML(HTTP_RAW_POST_DATA);
    if (!dom->relaxNGValidate ( 'watch.rng')) {
        die("unvalid document");
    }  
    xml = simplexml_import_dom(dom);
    foreach (xml->children() as element) {
      element['escaped'] = sqlite_escape_string(trim(element));
    }
    //echo xml->asXML();
    query = db->query(
      "SELECT symbol from watches where symbol='".
        xml->symbol['escaped'].
        "'", 
      SQLITE_NUM);
    req = "";
    if (query->fetch()) {
      req="update watches set ";
      req .= "tag='".xml->tag['escaped']."', ";
      req .= "title='".xml->title['escaped']."', ";
      req .= "description='".xml->description['escaped']."' ";
      req .= "where symbol='".xml->symbol['escaped']."'";
    } else {
      req="insert into watches (symbol, tag, title, description) values (";
      req .= "'".xml->symbol['escaped']."', ";
      req .= "'".xml->tag['escaped']."', ";
      req .= "'".xml->title['escaped']."', ";
      req .= "'".xml->description['escaped']."')";
    }
    //echo req;
    db->queryExec(req);
    echo "<ok/>";
  }
  
   header("Cache-Control: max-age=60");
   header("Content-type: application/xml");
   db=openDb();
   if (strlen(HTTP_RAW_POST_DATA)>0) {
     print write(db);
   } else if (_GET['name']) {
     print getAWatch(db, _GET['name']);
   } else {
     print getAllWatches(db);
   }
  ?>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.