Highway to PSR2
[openemr.git] / portal / import_template.php
blob8a10fdcbb3368b4d37af9ff2a73827222c311856
1 <?php
2 /**
4 * Copyright (C) 2016-2017 Jerry Padgett <sjpadgett@gmail.com>
6 * LICENSE: This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @package OpenEMR
20 * @author Jerry Padgett <sjpadgett@gmail.com>
21 * @link http://www.open-emr.org
25 require_once("../interface/globals.php");
27 if ($_POST['mode'] == 'get') {
28 echo file_get_contents($_POST['docid']);
29 exit;
30 } else if ($_POST['mode'] == 'save') {
31 file_put_contents($_POST['docid'], $_POST['content']);
32 exit(true);
33 } else if ($_POST['mode'] == 'delete') {
34 unlink($_POST['docid']);
35 exit(true);
38 // so it is an import
39 if (!isset($_POST['up_dir'])) {
40 define("UPLOAD_DIR", $GLOBALS['OE_SITE_DIR'] . '/documents/onsite_portal_documents/templates/');
41 } else {
42 if ($_POST['up_dir'] > 0) {
43 define("UPLOAD_DIR", $GLOBALS['OE_SITE_DIR'] . '/documents/onsite_portal_documents/templates/'. $_POST['up_dir'] . '/');
44 } else {
45 define("UPLOAD_DIR", $GLOBALS['OE_SITE_DIR'] . '/documents/onsite_portal_documents/templates/');
49 if (!empty($_FILES["tplFile"])) {
50 $tplFile = $_FILES["tplFile"];
52 if ($tplFile["error"] !== UPLOAD_ERR_OK) {
53 header("refresh:2;url= import_template_ui.php");
54 echo "<p>". xlt("An error occurred: Missing file to upload: Use back button!") . "</p>";
55 exit;
58 // ensure a safe filename
59 $name = preg_replace("/[^A-Z0-9._-]/i", "_", $tplFile["name"]);
60 $parts = pathinfo($name);
61 $name = $parts["filename"].'.tpl';
62 // don't overwrite an existing file
63 while (file_exists(UPLOAD_DIR . $name)) {
64 $i = rand(0, 128);
65 $newname = $parts["filename"] . "-" . $i . "." . $parts["extension"].".replaced";
66 rename(UPLOAD_DIR .$name, UPLOAD_DIR .$newname);
69 // preserve file from temporary directory
70 $success = move_uploaded_file($tplFile["tmp_name"], UPLOAD_DIR . $name);
71 if (!$success) {
72 echo "<p>". xlt("Unable to save file: Use back button!") . "</p>";
73 exit;
76 // set proper permissions on the new file
77 chmod(UPLOAD_DIR . $name, 0644);
78 header("location: " . $_SERVER['HTTP_REFERER']);