Files
E-42/action/save_page.php

52 lines
1.4 KiB
PHP

<?php
try{
if(!isset($_POST['page']) or !isset($_POST['department']) or !isset($_POST['member'])){
header("HTTP/1.1 400 Bad Request 1 ");
die;
}
// check if department is valid
$departmemt = strtoupper($_POST['department']);
$invalid_department = empty($departmemt) || !preg_match('/^[A-Z]-[0-9]*$/', $departmemt);
// check if name is valid
$name = htmlentities($_POST['member']);
$invalid_name = empty( $name) || !preg_match('/^[\w\-()]*$/', $name);
if($invalid_department || $invalid_name){
header("HTTP/1.1 400 Bad Request 2");
die;
}
// check if file exits
$path = __DIR__ . '/../' . $departmemt . '/members/'. $name . '/index.html';
if(!file_exists($path)){
header("HTTP/1.1 400 Bad Request 3");
echo($path);
die;
}
$dom = new DOMDocument();
@$dom->loadHTMLFile($path);
$newNode = $dom->createDocumentFragment();
$newNode->appendXML('<section id = "mytextarea">'.$_POST['page'].'</section>');
$oldNode = $dom->getElementById('mytextarea');
$oldNode->parentNode->replaceChild($newNode, $oldNode);
$dom->saveHTMLFile(__DIR__ . '/../' . $departmemt . '/members/'. $name . '/index.html');
//TODO seguranca: se mandares um script js, ele guarda no ficheiro
}catch(Exception $e){
header("HTTP/1.1 500 Internal Server Error");
die;
}
header("HTTP/1.1 200 OK");
?>