fe03417d4c4b9520f8f2218db0f35a4b7563eea6
[acontent.git] / docs / updater / index.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 \r
15 require (TR_INCLUDE_PATH.'vitals.inc.php');\r
16 require_once('classes/PatchListParser.class.php');\r
17 require_once(TR_INCLUDE_PATH. 'classes/FileUtility.class.php');\r
18 require_once(TR_INCLUDE_PATH. 'classes/DAO/PatchesDAO.class.php');\r
19 require_once('include/common.inc.php');\r
20 \r
21 set_time_limit(0);\r
22 \r
23 $patchesDAO = new PatchesDAO();\r
24 \r
25 /**\r
26  * Generate html of each patch row at main patch page\r
27  */\r
28 function print_patch_row($patch_row, $row_id, $enable_radiotton)\r
29 {\r
30         global $id, $patch_id;   // current selected patch\r
31         global $dependent_patches;\r
32 \r
33         if ($dependent_patches =="")\r
34                 $description = $patch_row["description"];\r
35         else\r
36                 $description = $patch_row["description"] . _AT('update_dependent_update_not_installed') . "<span style='color: red'>" . $dependent_patches . "</span>";\r
37 ?>\r
38         <tr <?php if ($enable_radiotton) echo 'onkeydown="document.form[\'m'. $row_id.'\'].checked = true; trans.utility.rowselect(this);" onmousedown="document.form[\'m'. $row_id.'\'].checked = true; trans.utility.rowselect(this);" id="r_'. $row_id .'"'; ?>>\r
39                 <td><input type="radio" name="id" value="<?php echo $row_id; ?>" id="m_<?php echo $patch_row["system_patch_id"]; ?>" <?php if (!$enable_radiotton) echo 'disabled="disabled" '; if (strcmp($row_id, $id) == 0 || strcmp($row_id, $patch_id) == 0) echo "checked "?> /></td>\r
40                 <td><label for="m_<?php echo $patch_row["system_patch_id"]; ?>"><?php echo $patch_row["system_patch_id"]; ?></label></td>\r
41                 <td><?php echo $description; ?></td>\r
42                 <td><?php if (!isset($patch_row['status'])) echo _AT("not_installed"); else echo $patch_row["status"]; ?></td>\r
43                 <td><?php echo $patch_row["available_to"]; ?></td>\r
44                 <td><?php echo $patch_row["author"]; ?></td>\r
45                 <td><?php if (isset($patch_row['status'])) echo ($patch_row["installed_date"]=='0000-00-00 00:00:00')?_AT('na'):$patch_row["installed_date"]; ?></td>\r
46                 <td>\r
47                 <?php \r
48                 if (preg_match('/Installed/', $patch_row["status"]) > 0 && ($patch_row["remove_permission_files"]<> "" || $patch_row["backup_files"]<>"" || $patch_row["patch_files"]<> ""))\r
49                         echo '\r
50                   <div class="row buttons">\r
51                                 <input type="button" align="middle" name="info" value="'._AT('view_message').'" onclick="location.href=\''. $_SERVER['PHP_SELF'] .'?patch_id='.$row_id.'\'" />\r
52                         </div>';\r
53                 ?>\r
54                 </td>\r
55         </tr>\r
56 <?php\r
57 }\r
58 \r
59 // split a string by given delimiter and return an array\r
60 function get_array_by_delimiter($subject, $delimiter)\r
61 {\r
62         return preg_split('/'.preg_quote($delimiter).'/', $subject, -1, PREG_SPLIT_NO_EMPTY);\r
63 }\r
64 \r
65 $skipFilesModified = false;\r
66 \r
67 if ($_POST['yes'])  $skipFilesModified = true;\r
68 \r
69 if ($_POST['no'])\r
70 {\r
71         unset($_SESSION['remove_permission']);\r
72         $msg->addFeedback('CANCELLED');\r
73         header('Location: index.php');\r
74         exit;\r
75 }\r
76 \r
77 require (TR_INCLUDE_PATH.'header.inc.php');\r
78 \r
79 if (trim($_POST['who']) != '') $who = trim($_POST['who']);\r
80 elseif (trim($_REQUEST['who']) != '') $who = trim($_REQUEST['who']);\r
81 else $who = "public";\r
82 \r
83 // check the connection to server update.atutor.ca\r
84 $connection_test_file = UPDATE_SERVER . '/index.php';\r
85 $connection = @file_get_contents($connection_test_file);\r
86 \r
87 if (!$connection) \r
88 {\r
89         $infos = array('CANNOT_CONNECT_PATCH_SERVER', UPDATE_SERVER);\r
90         $msg->addInfo($infos);\r
91         $server_connected = false;\r
92 }\r
93 else\r
94         $server_connected = true;\r
95 \r
96 // get patch list if successfully connect to patch server\r
97 if ($server_connected)\r
98 {\r
99         $patch_folder = UPDATE_SERVER . '/patch/' . str_replace('.', '_', VERSION) . '/';\r
100         $patch_list_xml = @file_get_contents($patch_folder . 'patch_list.xml');\r
101         \r
102         if ($patch_list_xml) \r
103         {\r
104                 $patchListParser = new PatchListParser();\r
105                 $patchListParser->parse($patch_list_xml);\r
106                 $patch_list_array = $patchListParser->getMyParsedArrayForVersion(VERSION);\r
107         }\r
108 }\r
109 // end of get patch list\r
110 \r
111 $module_content_folder = TR_CONTENT_DIR . "updater/temp";\r
112 if (!is_dir($module_content_folder)) mkdir($module_content_folder);\r
113 \r
114 if ($_POST['install_upload'] && $_POST['uploading'])\r
115 {\r
116         include_once(TR_INCLUDE_PATH . 'lib/pclzip.lib.php');\r
117         \r
118         // clean up module content folder\r
119         FileUtility::clr_dir($module_content_folder);\r
120         \r
121         // 1. unzip uploaded file to module's content directory\r
122         $archive = new PclZip($_FILES['patchfile']['tmp_name']);\r
123 \r
124         if ($archive->extract(PCLZIP_OPT_PATH, $module_content_folder) == 0)\r
125         {\r
126             FileUtility::clr_dir($module_content_folder);\r
127             $msg->addError('CANNOT_UNZIP');\r
128         }\r
129 }\r
130 \r
131 // Installation process\r
132 if ($_POST['install'] || $_POST['install_upload'] && !isset($_POST["not_ignore_version"]))\r
133 {\r
134         \r
135         if (isset($_POST['id'])) $id=$_POST['id'];\r
136         else $id = $_REQUEST['id'];\r
137 \r
138         if ($_POST['install'] && $id == "")\r
139         {\r
140                 $msg->addError('CHOOSE_UNINSTALLED_PATCH');\r
141         }\r
142         else\r
143         {\r
144                 if ($_POST['install'])\r
145                 {\r
146                         $patchURL = $patch_folder . $patch_list_array[$id][patch_folder] . "/";\r
147                 }\r
148                 else if ($_POST['install_upload'])\r
149                 {\r
150                         $patchURL = $module_content_folder . "/";\r
151                 }\r
152                         \r
153                 $patch_xml = @file_get_contents($patchURL . 'patch.xml');\r
154                 \r
155                 if ($patch_xml === FALSE) \r
156                 {\r
157                         $msg->addError('PATCH_XML_NOT_FOUND');\r
158                 }\r
159                 else\r
160                 {\r
161                         require_once('classes/PatchParser.class.php');\r
162                         require_once('classes/Patch.class.php');\r
163                         \r
164                         $patchParser = new PatchParser();\r
165                         $patchParser->parse($patch_xml);\r
166                         \r
167                         $patch_array = $patchParser->getParsedArray();\r
168 \r
169                         if ($_POST["ignore_version"]) $patch_array["applied_version"] = VERSION;\r
170                         \r
171                         if ($_POST["install_upload"])\r
172                         {\r
173                                 $current_patch_list = array('system_patch_id' => $patch_array['system_patch_id'],\r
174                                                                                                                                                 'applied_version' => $patch_array['applied_version'],\r
175                                                                                                                                                 'patch_folder' => $patchURL,\r
176                                                                                                                                                 'available_to' => 'private',\r
177                                                                                                                                                 'author' => $patch_array['author'],\r
178                                                                                                                                                 'sql' => $patch_array['sql'],\r
179                                                                                                                                                 'description' => $patch_array['description'],\r
180                                                                                                                                                 'dependent_patches' => $patch_array['dependent_patches']);\r
181                         }\r
182 \r
183                         if ($_POST["install"])\r
184                         {\r
185                                 $current_patch_list = $patch_list_array[$id];\r
186                                 $current_patch_list["sql"] = $patch_array["sql"];\r
187                         }\r
188 \r
189                         if ($_POST["install_upload"] && is_patch_installed($patch_array["system_patch_id"]))\r
190                                 $msg->addError('UPDATE_ALREADY_INSTALLED');\r
191                         else\r
192                         {\r
193                                 $patch = new Patch($patch_array, $current_patch_list, $skipFilesModified, $patchURL);\r
194                         \r
195                                 if ($patch->applyPatch())  $patch_id = $patch->getPatchID();\r
196                         }\r
197                 }\r
198         }\r
199 }\r
200 // end of patch installation\r
201 \r
202 // display permission and backup files message\r
203 if (isSet($_REQUEST['patch_id']))  $patch_id = $_REQUEST['patch_id'];\r
204 elseif ($_POST['patch_id']) $patch_id=$_POST['patch_id'];\r
205 \r
206 if ($patch_id > 0)\r
207 {\r
208         // clicking on button "Done" at displaying remove permission info page\r
209         if ($_POST['done'])\r
210         {\r
211                 $permission_files = array();\r
212                 \r
213                 if (is_array($_SESSION['remove_permission']))\r
214                 {\r
215                         foreach ($_SESSION['remove_permission'] as $file)\r
216                         {\r
217                                 if (is_writable($file))  $permission_files[] = $file;\r
218                         }\r
219                 }\r
220                 \r
221                 if (count($permission_files) == 0)\r
222                 {\r
223                         $updateInfo = array("remove_permission_files"=>"", "status"=>"Installed");\r
224                 \r
225                         $patchesDAO->UpdateByArray($patch_id, $updateInfo);\r
226                 }\r
227                 else\r
228                 {\r
229                         foreach($permission_files as $permission_file)\r
230                                 $remove_permission_files .= $permission_file. '|';\r
231                 \r
232                         $updateInfo = array("remove_permission_files"=>preg_quote($remove_permission_files), "status"=>"Partly Installed");\r
233                         \r
234                         $patchesDAO->UpdateByArray($patch_id, $updateInfo);\r
235                 }\r
236         \r
237         }\r
238         \r
239         // display remove permission info\r
240         unset($_SESSION['remove_permission']);\r
241 \r
242         $row = $patchesDAO->getByID($patch_id);\r
243         \r
244         if ($row["remove_permission_files"]<> "")\r
245         {\r
246                 $remove_permission_files = $_SESSION['remove_permission'] = get_array_by_delimiter($row["remove_permission_files"], "|");\r
247 \r
248                 if (count($_SESSION['remove_permission']) > 0)\r
249                 {\r
250                         if ($_POST['done']) $msg->printErrors('REMOVE_WRITE_PERMISSION');\r
251                         else $msg->printInfos('PATCH_INSTALLED_AND_REMOVE_PERMISSION');\r
252                         \r
253                         $feedbacks[] = _AT('remove_write_permission');\r
254                         \r
255                         foreach($remove_permission_files as $remove_permission_file)\r
256                                 if ($remove_permission_file <> "") $feedbacks[count($feedbacks)-1] .= "<strong>" . $remove_permission_file . "</strong><br />";\r
257 \r
258                         $notes = '<form action="'. $_SERVER['PHP_SELF'].'?patch_id='.$patch_id.'" method="post" name="remove_permission">\r
259                   <div class="row buttons">\r
260                                 <input type="hidden" name="patch_id" value="'.$patch_id.'" />\r
261                                 <input type="submit" name="done" value="'._AT('done').'" accesskey="d" />\r
262                         </div>\r
263                         </form>';\r
264                 }\r
265 \r
266                 print_errors($feedbacks, $notes);\r
267         }\r
268 \r
269         // display backup file info after remove permission step\r
270         if ($row["remove_permission_files"] == "")\r
271         {\r
272                 $msg->printFeedbacks('PATCH_INSTALLED_SUCCESSFULLY');\r
273                 \r
274                 if ($row["backup_files"]<> "")\r
275                 {\r
276                         $backup_files = get_array_by_delimiter($row["backup_files"], "|");\r
277         \r
278                         if (count($backup_files) > 0)\r
279                         {\r
280                                 $feedbacks[] = _AT('updater_show_backup_files');\r
281                                 \r
282                                 foreach($backup_files as $backup_file)\r
283                                         if ($backup_file <> "") $feedbacks[count($feedbacks)-1] .= "<strong>" . $backup_file . "</strong><br />";\r
284                         }\r
285                 }\r
286 \r
287                 if ($row["patch_files"]<> "")\r
288                 {\r
289                         $patch_files = get_array_by_delimiter($row["patch_files"], "|");\r
290         \r
291                         if (count($patch_files) > 0)\r
292                         {\r
293                                 $feedbacks[] = _AT('updater_show_patch_files');\r
294                                 \r
295                                 foreach($patch_files as $patch_file)\r
296                                         if ($patch_file <> "") $feedbacks[count($feedbacks)-1] .= "<strong>" . $patch_file . "</strong><br />";\r
297                                         \r
298                         }\r
299                 }\r
300                 \r
301                 if (count($feedbacks)> 0)\r
302                         print_feedback($feedbacks);\r
303                 else\r
304                         print_feedback(array());\r
305         }\r
306 }\r
307 \r
308 $msg->printAll();\r
309 \r
310 // display installed patches\r
311 $rows = $patchesDAO->getPatchByVersion(VERSION);\r
312 \r
313 if (is_array($rows)) $num_of_patches_in_db = count($rows);\r
314 else $num_of_patches_in_db = 0;\r
315 \r
316 $num_of_patches = $num_of_patches_in_db + count($patch_list_array);\r
317 \r
318 $savant->assign('num_of_patches', $num_of_patches);\r
319 $savant->assign('patches_in_db', $rows);\r
320 $savant->assign('patch_list_array', $patch_list_array);\r
321 $savant->assign('patches_in_db', $rows);\r
322 $savant->assign('patches_in_db', $rows);\r
323 \r
324 $savant->display('updater/index.tmpl.php');\r
325 ?>\r