public function parseEntries($xml) { $res = true; for ($i = 0; $i < count($xml->entry); ++$i) { $ent = $xml->entry[$i]; $entry = new EcEntry($this); $entry->deviceId = (string) $ent->ecPhoneID; $entry->created = (string) $ent->ecTimeCreated; $entry->project = $this->project; $entry->values = array(); foreach ($this->fields as $key => $fld) { if ($fld->type == 'gps' || $fld->type == 'location') { $lat = "{$key}_lat"; $lon = "{$key}_lon"; $alt = "{$key}_alt"; $acc = "{$key}_acc"; $src = "{$key}_provider"; $bearing = "{$key}_bearing"; $entry->values[$key] = array('latitude' => (string) $ent->{$lat}, 'longitude' => (string) $ent->{$lon}, 'altitude' => (string) $ent->{$alt}, 'accuracy' => (string) $ent->{$acc}, 'provider' => (string) $ent->{$src}, 'bearing' => (string) $ent->{$bearing}); } else { $entry->values[$key] = (string) $ent->{$key}; } } if (!preg_match('/^[0-9]+$/', $entry->created)) { $date = false; try { $date = new DateTime($entry->created, new DateTimeZone('UTC')); } catch (Exception $ex) { $date = new DateTime('now', new DateTimeZone('UTC')); } $entry->created = $date->getTimestamp(); } //TODO: need to check field names in the xml against fields in the form, and possibly //alert users to form version errors. $res = $entry->post(); if ($res !== true) { return $res; } } return $res; }
public function parseEntries($xml) { $res = true; for ($i = 0; $i < count($xml->entry); $i++) { $ent = $xml->entry[$i]; $entry = new EcEntry($this); $entry->deviceId = (string) $ent->ecPhoneID; $entry->created = (string) $ent->ecTimeCreated; //$entry->form = $this->id; $entry->project = $this->project; $entry->values = array(); foreach ($this->fields as $key => $fld) { //if($val->getName() != "ecPhoneID" && $val->getName() != "ecTimeCreated" && $this->fields[$val->getName()]->type != 'gps') // $entry->values[$val->getName()] = (string)$val; //elseif($this->fields[$val->getName()]->type != 'gps') //{} if ($fld->type == 'gps') { $lat = "{$key}_lat"; $lon = "{$key}_lon"; $alt = "{$key}_alt"; $acc = "{$key}_acc"; $src = "{$key}_provider"; $entry->values[$key] = array('latitude' => (string) $ent->{$lat}, 'longitude' => (string) $ent->{$lon}, 'altitude' => (string) $ent->{$alt}, 'accuracy' => (string) $ent->{$acc}, 'provider' => (string) $ent->{$src}); } else { $entry->values[$key] = (string) $ent->{$key}; } } //TODO: need to check field names in the xml against fields in the form, and possibly //alert users to form version errors. $res = $entry->post(); if ($res !== true) { return $res; } } return $res; }
function uploadData() { global $url, $log; $flog = fopen('ec/uploads/fileUploadLog.log', 'a'); $prj = new EcProject(); $prj->name = preg_replace('/\\/upload\\.?(xml|json)?$/', '', $url); $prj->fetch(); if ($_SERVER["REQUEST_METHOD"] == "POST") { if (count($_POST) == 0) { parse_str(file_get_contents("php://input"), $_POST); } if (count($_FILES) > 0) { foreach ($_FILES as $file) { if (preg_match("/.+\\.xml\$/", $file["name"])) { $ts = new DateTime("now", new DateTimeZone("UTC")); $ts = $ts->getTimestamp(); $fn = "{$ts}-{$file["name"]}"; for ($i = 1; file_exists("../ec/rescue/{$fn}"); $i++) { $fn = "{$ts}-{$i}-{$file['name']}"; } move_uploaded_file($file['tmp_name'], "./ec/rescue/{$fn}"); $res = $prj->parseEntries(file_get_contents("./ec/rescue/{$fn}")); if (preg_match("/(CHROME|FIREFOX)/i", $_SERVER["HTTP_USER_AGENT"])) { echo $res; } else { //fwrite($flog, "$res\r\m"); $log->write("debug", "{$res}"); echo $res === true ? "1" : "0"; } } else { if (preg_match("/\\.(png|gif|rtf|docx?|pdf|jpg|jpeg|txt|avi|mpe?g|mov|mpe?g?3|wav|mpe?g?4|3gp)\$/", $file['name'])) { try { //if(!fileExists("./uploads/{$prj->name}")) mkdir("./uploads/{$prj->name}"); move_uploaded_file($file['tmp_name'], "./ec/uploads/{$prj->name}~" . ($_REQUEST["type"] == "thumbnail" ? "tn~" : "") . "{$file['name']}"); $log->write('debug', $file['name'] . " copied to uploads directory\n"); echo 1; } catch (Exception $e) { $log->write("error", $e . "\r\n"); echo "0"; } } else { $log->write("error", $file['name'] . " error : file type not allowed\r\n"); echo "0"; } } } } else { $log->write("POST", "data : " . serialize($_POST) . "\r\n"); $tn = $_POST["table"]; unset($_POST["table"]); try { $ent = new EcEntry($prj->tables[$tn]); if (array_key_exists("ecPhoneID", $_POST)) { $ent->deviceId = $_POST["ecPhoneID"]; } else { $ent->deviceId = "web"; } if (array_key_exists("ecTimeCreated", $_POST)) { $ent->created = $_POST["ecTimeCreated"]; } else { $d = new DateTime('now', new DateTimeZone('UTC')); $ent->created = $d->getTimestamp(); } $ent->project = $prj; foreach ($prj->tables[$tn]->fields as $key => $fld) { if ($fld->type == 'gps' || $fld->type == 'location') { $lat = "{$key}_lat"; $lon = "{$key}_lon"; $alt = "{$key}_alt"; $acc = "{$key}_acc"; $src = "{$key}_provider"; $bearing = "{$key}_bearing"; $ent->values[$key] = array('latitude' => (string) getValIfExists($_POST, $lat), 'longitude' => (string) getValIfExists($_POST, $lon), 'altitude' => (string) getValIfExists($_POST, $alt), 'accuracy' => (string) getValIfExists($_POST, $acc), 'provider' => (string) getValIfExists($_POST, $src), 'bearing' => (string) getValIfExists($_POST, $bearing)); } else { if (!array_key_exists($key, $_POST)) { $ent->values[$key] = ""; continue; } else { if ($fld->type != "branch") { $ent->values[$key] = (string) $_POST[$key]; } } } } $log->write("debug", "posting ... \r\n"); $res = $ent->post(); $log->write("debug", "response : {$res} \r\n"); if ($res === true) { header("HTTP/1.1 200 OK"); echo 1; } else { header("HTTP/1.1 405 Bad Request"); $log->write("error", "error : {$res}\r\n"); echo $res; } } catch (Exception $e) { $log->write("error", "error : " . $e->getMessage() . "\r\n"); $msg = $e->getMessage(); if (preg_match("/^Message/", $msg)) { header("HTTP/1.1 405 {$msg}"); } else { header("HTTP/1.1 405 Bad Request"); } echo $msg; } } } fclose($flog); }