$cnt = '001'; if (isset($_POST['download'])) { $filename = $filename . '.json'; } else { $cnt = count(glob('templates/*.json')) + 1; $filename = str_pad($cnt, 3, '0', STR_PAD_LEFT) . ' - ' . $filename . '.json'; } if (!isset($_POST['download']) && file_exists('templates/' . $filename)) { header('Content-type: text/html'); die(json_encode(array('message' => 'The template could not be saved because the another template already exists with the same name. Please rename your template and try again.', 'success' => false))); } $data = array('code' => $code, 'setup_code' => $setup_code, 'prepend_code' => $prepend_code, 'append_code' => $append_code, 'options' => null, 'whitelist' => $whitelist, 'blacklist' => $blacklist, 'definitions' => $definitions); if (count($options)) { $sandbox = new \PHPSandbox\PHPSandbox(); foreach ($options as $name => $value) { if ($name == 'error_level' && $value != error_reporting() || $name != 'error_level' && $sandbox->get_option($name) != $value) { //save unique options only $data['options'][$name] = $value; } } } if (isset($_POST['download'])) { header('Content-disposition: attachment; filename="' . $filename . '";'); header('Content-type: application/json'); die(json_encode($data)); } else { if (file_put_contents('templates/' . $filename, json_encode($data))) { header('Content-type: text/html'); die(json_encode(array('message' => 'The template "' . $template . '" was saved successfully!', 'name' => $cnt . ' - ' . $template, 'file' => $filename, 'success' => true))); } }