3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Response { public function __construnct() { header('Content-Type: application/json'); } public function error(string $msg, array $data = []){ return json_encode([ 'status' => 0, ] + compact('msg','data')); } public function success(string $msg, array $data = []) { return json_encode([ 'status' => 1, ] + compact('msg','data')); } } class View { public function render(string $file, $data = NULL) { if(stripos($file, '..')) { throw new \Exception('Invalid view'); } $dir = dirname(__FILE__); $tpl = $dir.DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR.$file.'.tpl'; if(!file_exists($tpl)) { throw new \Exception('Missing template'); } ob_start(); include($tpl); extract(compact('data')); return ob_get_clean(); } } $response = new Response(); try { if(mb_strtolower($_SERVER['REQUEST_METHOD']) != 'post') { throw new \Exception('Wrong HTTP method'); } if(!isset($_POST['file'])) { throw new \Exception('File is not defined'); } $file = $_POST['file']; if(stripos($file, '..')) { throw new \Exception('Invalid file name'); } $file = dirname(__FILE__).DIRECTORY_SEPARATOR.$file; if(!file_exists($file)) { throw new \Exception('File not exists'); } if(pathinfo($file, PATHINFO_EXTENSION) !== 'xml') { throw new \Exception('File has wrong mimetype'); } $plan = simplexml_load_file($file); $view = new View; $html = $view->render('plan' , $plan); // pr($data); echo $response->success('File was parsed' , compact('html')); } catch (\Exception $e) { echo $response->error($e->getMessage()); } function pr($var) { echo '<pre>'; print_r($var); echo '</pre>'; } function h($var) { return htmlspecialchars($var); }

preferences:
63.13 ms | 402 KiB | 5 Q