diff options
| author | Maksymilian Jopek <maks@jopek.eu> | 2022-03-16 23:20:59 +0100 |
|---|---|---|
| committer | Maksymilian Jopek <maks@jopek.eu> | 2022-03-16 23:20:59 +0100 |
| commit | bf7852bccce954aff2d7c94161f4d301a0f29684 (patch) | |
| tree | fc3f2aac4f843990b02992e02903df22857a1928 /api/add.php | |
| download | mlos-bf7852bccce954aff2d7c94161f4d301a0f29684.tar.gz mlos-bf7852bccce954aff2d7c94161f4d301a0f29684.tar.zst mlos-bf7852bccce954aff2d7c94161f4d301a0f29684.zip | |
Initial commit w/ v1.0.0
Diffstat (limited to 'api/add.php')
| -rw-r--r-- | api/add.php | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/api/add.php b/api/add.php new file mode 100644 index 0000000..b2e4c2d --- /dev/null +++ b/api/add.php @@ -0,0 +1,55 @@ +<?php +$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 = "/home/maks/school/clientApps/mlos/src/db.xml"; +require_once "/home/maks/school/clientApps/mlos/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); + var_dump($game); + var_dump($game->getElementsByTagName); + $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; +} 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); |
