summaryrefslogtreecommitdiffstats
path: root/api/add.php
blob: 7edeb3bd5ce69e4621922441bc16c5b902d165ab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
session_start();
$req_keys = ["name", "author", "thumbnail", "magazine"];
header("Location: " . $_SERVER["HTTP_REFERER"]);
foreach ($req_keys as $key) {
  if (!isset($_POST[$key])) {
    http_response_code(400);
    exit(0);
  }
}
$path = "%PWD%src/db.xml";
require_once "%PWD%src/data.php";

$name = $_POST["name"];
$author = $_POST["author"];
$thumbnail = $_POST["thumbnail"];
$magazine = $_POST["magazine"];
$id = $_POST["id"];

$doc = get_doc();
error_log("ID: " . $id);

if ($id) {
  $xpath = new DOMXpath($doc);
  $game = $xpath->query('//game[@id="' . $id . '"]')->item(0);
  $xpath->query('//game[@id="' . $id . '"]/name')->item(0)->nodeValue = $name;
  $xpath->query('//game[@id="' . $id . '"]/author')->item(0)->nodeValue = $author;
  $xpath->query('//game[@id="' . $id . '"]/thumbnail')->item(0)->nodeValue = $thumbnail;
  $xpath->query('//game[@id="' . $id . '"]/magazine')->item(0)->nodeValue = $magazine;
  $_SESSION["eid"] = null;
} else {
  $root = $doc->getElementsByTagName("root")->item(0);
  $game = $doc->createElement("game", "");

  $mid = $doc->getElementsByTagName("mid")->item(0);
  $game->setAttribute("id", $mid->nodeValue);
  $mid->nodeValue = intval($mid->nodeValue) + 1;

  $count = $doc->getElementsByTagName("count")->item(0);
  $count->nodeValue = intval($count->nodeValue) + 1;
  $game->setAttribute("order", $count->nodeValue);

  $name = $doc->createElement("name", $name);
  $author = $doc->createElement("author", $author);
  $thumbnail = $doc->createElement("thumbnail", $thumbnail);
  $magazine = $doc->createElement("magazine", $magazine);
  $game->append($name);
  $game->append($author);
  $game->append($thumbnail);
  $game->append($magazine);

  $root->appendChild($game);
}

$doc->save($path);