a02abac584fb205668a8a27307c25cff29fe34cf
[acontent.git] / docs / updater / patch_creator.php
1 <?php\r
2 /************************************************************************/\r
3 /* AContent                                                             */\r
4 /************************************************************************/\r
5 /* Copyright (c) 2010                                                   */\r
6 /* Inclusive Design Institute                                           */\r
7 /*                                                                      */\r
8 /* This program is free software. You can redistribute it and/or        */\r
9 /* modify it under the terms of the GNU General Public License          */\r
10 /* as published by the Free Software Foundation.                        */\r
11 /************************************************************************/\r
12 \r
13 define('TR_INCLUDE_PATH', '../include/');\r
14 require_once (TR_INCLUDE_PATH.'vitals.inc.php');\r
15 \r
16 if ($_POST['create'] || $_POST['save'])\r
17 {\r
18         if (isset($_REQUEST["myown_patch_id"])) $patch_id = $_REQUEST["myown_patch_id"];\r
19         else $patch_id = 0;\r
20         \r
21         // check missing fields\r
22         if (!isset($_POST["system_patch_id"]) || trim($_POST["system_patch_id"]) == "")\r
23                 $missing_fields[] = _AT("system_update_id");\r
24 \r
25         if (!isset($_POST["transformable_version_to_apply"]) || trim($_POST["transformable_version_to_apply"]) == "")\r
26                 $missing_fields[] = _AT("transformable_version_to_apply");\r
27 \r
28         // only check missing upload file when creating a update. don't check when save\r
29         if (is_array($_POST['rb_action']) && $_POST['create'])\r
30         {\r
31                 foreach ($_POST['rb_action'] as $i=>$action)\r
32                 {\r
33                         // must upload a file if action is add or overwrite\r
34                         if ($action == "add" && $_FILES['add_upload_file']['name'][$i] == "" && $_POST['add_uploaded_file'] == "")\r
35                                 $missing_fields[] = _AT("upload_file") . " for ". _AT("file_name") . " <strong>" . $_POST['add_filename'][$i] . "</strong>";\r
36         \r
37                         if ($action == "overwrite" && $_FILES['overwrite_upload_file']['name'][$i] == "" && $_POST['overwrite_uploaded_file'] == "")\r
38                                 $missing_fields[] = _AT("upload_file") . " for ". _AT("file_name") . " <strong>" . $_POST['overwrite_filename'][$i] . "</strong>";\r
39                 }\r
40         }\r
41         // end of checking missing fields\r
42 \r
43         if ($missing_fields) \r
44         {\r
45                 $missing_fields = implode(', ', $missing_fields);\r
46                 $msg->addError(array('EMPTY_FIELDS', $missing_fields));\r
47         }\r
48 \r
49         // main process\r
50         if (!$msg->containsErrors()) \r
51         {\r
52                 $patch_info = array("system_patch_id"=>$_POST["system_patch_id"],\r
53                               "transformable_version_to_apply"=>$_POST["transformable_version_to_apply"],\r
54                               "description"=>$_POST["description"],\r
55                               "sql_statement"=>$_POST["sql_statement"]);\r
56 \r
57                 // remove empty dependent patches\r
58                 if (is_array($_POST["dependent_patch"]))\r
59                 {\r
60                         foreach ($_POST["dependent_patch"] as $dependent_patch)\r
61                                 if (trim($dependent_patch) <> "")\r
62                                         $dependent_patches[] = $dependent_patch;\r
63                 }\r
64                 \r
65                 if (is_array($dependent_patches))\r
66                         $patch_info["dependent_patches"] = $dependent_patches;\r
67                         \r
68                 if (is_array($_POST['rb_action']))\r
69                 {\r
70                         foreach ($_POST['rb_action'] as $i=>$action)\r
71                         {\r
72                                 if ($action == "add" && $_POST['add_filename'][$i] <> "")\r
73                                 {\r
74                                         if ($_FILES['add_upload_file']['tmp_name'][$i] <> "")\r
75                                                 $upload_file = $_FILES['add_upload_file']['tmp_name'][$i];\r
76                                         else\r
77                                                 $upload_file = $_POST['add_uploaded_file'][$i];\r
78                                         \r
79                                         $patch_info["files"][] = array("action"=>$action,\r
80                                                                      "file_name"=>$_POST['add_filename'][$i],\r
81                                                                "directory"=>$_POST['add_dir'][$i],\r
82                                                                "upload_tmp_name"=>$upload_file);\r
83                                 }\r
84                                 \r
85                                 if ($action == "alter" && $_POST['alter_filename'][$i] <> "")\r
86                                         $patch_info["files"][] = array("action"=>$action,\r
87                                                                                        "file_name"=>$_POST['alter_filename'][$i],\r
88                                                                "directory"=>$_POST['alter_dir'][$i],\r
89                                                                "code_from"=>$_POST['alter_code_from'][$i],\r
90                                                                "code_to"=>$_POST['alter_code_to'][$i]);\r
91         \r
92                                 if ($action == "delete" && $_POST['delete_filename'][$i] <> "")\r
93                                         $patch_info["files"][] = array("action"=>$action,\r
94                                                                      "file_name"=>$_POST['delete_filename'][$i],\r
95                                                                "directory"=>$_POST['delete_dir'][$i]);\r
96         \r
97                                 if ($action == "overwrite" && $_POST['overwrite_filename'][$i] <> "")\r
98                                 {\r
99                                         if ($_FILES['overwrite_upload_file']['tmp_name'][$i] <> "")\r
100                                                 $upload_file = $_FILES['overwrite_upload_file']['tmp_name'][$i];\r
101                                         else\r
102                                                 $upload_file = $_POST['overwrite_uploaded_file'][$i];\r
103                                         \r
104                                         $patch_info["files"][] = array("action"=>$action,\r
105                                                                      "file_name"=>$_POST['overwrite_filename'][$i],\r
106                                                                "directory"=>$_POST['overwrite_dir'][$i],\r
107                                                                "upload_tmp_name"=>$upload_file);\r
108                                 }\r
109                         }\r
110                 }\r
111 \r
112                 require_once("classes/PatchCreator.class.php");\r
113                 \r
114                 $patch_creator = new PatchCreator($patch_info, $patch_id);\r
115                 \r
116                 if ($_POST['create'])\r
117                         $patch_creator->create_patch();\r
118                 else if ($_POST['save'])\r
119                 {\r
120                         $patch_creator->saveInfo();\r
121                         header('Location: myown_patches.php');\r
122                 }\r
123 \r
124         }\r
125 }\r
126 \r
127 $msg->printErrors();\r
128 ?>\r