remove old readme
[atutor.git] / docs / mods / _standard / patcher / index_admin.php
1 <?php\r
2 /************************************************************************/\r
3 /* ATutor                                                               */\r
4 /************************************************************************/\r
5 /* Copyright (c) 2002-2010                                              */\r
6 /* Inclusive Design Institute                                           */\r
7 /* http://atutor.ca                                                     */\r
8 /*                                                                      */\r
9 /* This program is free software. You can redistribute it and/or        */\r
10 /* modify it under the terms of the GNU General Public License          */\r
11 /* as published by the Free Software Foundation.                        */\r
12 /************************************************************************/\r
13 // $Id$\r
14 \r
15 define('AT_INCLUDE_PATH', '../../../include/');\r
16 require (AT_INCLUDE_PATH.'vitals.inc.php');\r
17 admin_authenticate(AT_ADMIN_PRIV_PATCHER);\r
18 require_once('classes/PatchListParser.class.php');\r
19 require_once('include/common.inc.php');\r
20 \r
21 set_time_limit(0);\r
22 \r
23 /**\r
24  * Generate html of each patch row at main patch page\r
25  */\r
26 function print_patch_row($patch_row, $row_id, $enable_radiotton)\r
27 {\r
28         global $id, $patch_id;   // current selected patch\r
29         global $dependent_patches;\r
30 \r
31         if ($dependent_patches =="")\r
32                 $description = $patch_row["description"];\r
33         else\r
34                 $description = $patch_row["description"] . _AT('patch_dependent_patch_not_installed') . "<span style='color: red'>" . $dependent_patches . "</span>";\r
35 ?>\r
36         <tr <?php if ($enable_radiotton) echo 'onmousedown="document.form[\'m'. $row_id.'\'].checked = true; rowselect(this);" id="r_'. $row_id .'"'; ?>>\r
37                 <td><input type="radio" name="id" value="<?php echo $row_id; ?>"<?php if ($enable_radiotton) echo 'id="m'. $row_id.'"'; ?> <?php if (!$enable_radiotton) echo 'disabled="disabled" '; if (strcmp($row_id, $id) == 0 || strcmp($row_id, $patch_id) == 0) echo "checked "?> /></td>\r
38                 <td><label <?php if ($enable_radiotton) echo 'for="m'.$row_id.'"'; ?>><?php echo $patch_row["atutor_patch_id"]; ?></label></td>\r
39                 <td><?php echo $description; ?></td>\r
40                 <td><?php if (!isset($patch_row['status'])) echo _AT("not_installed"); else echo $patch_row["status"]; ?></td>\r
41                 <td><?php echo $patch_row["available_to"]; ?></td>\r
42                 <td><?php echo $patch_row["author"]; ?></td>\r
43                 <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
44                 <td>\r
45                 <?php \r
46                 if (preg_match('/Installed/', $patch_row["status"]) > 0 && ($patch_row["remove_permission_files"]<> "" || $patch_row["backup_files"]<>"" || $patch_row["patch_files"]<> ""))\r
47                         echo '\r
48                   <div class="row buttons">\r
49                                 <input type="button" align="middle" name="info" value="'._AT('view_message').'" onclick="location.href=\''. $_SERVER['PHP_SELF'] .'?patch_id='.$row_id.'\'" />\r
50                         </div>';\r
51                 ?>\r
52                 </td>\r
53         </tr>\r
54 <?php\r
55 }\r
56 \r
57 // split a string by given delimiter and return an array\r
58 function get_array_by_delimiter($subject, $delimiter)\r
59 {\r
60         return preg_split('/'.preg_quote($delimiter).'/', $subject, -1, PREG_SPLIT_NO_EMPTY);\r
61 }\r
62 \r
63 $skipFilesModified = false;\r
64 \r
65 if ($_POST['yes'])  $skipFilesModified = true;\r
66 \r
67 if ($_POST['no'])\r
68 {\r
69         unset($_SESSION['remove_permission']);\r
70         $msg->addFeedback('CANCELLED');\r
71         header('Location: index_admin.php');\r
72         exit;\r
73 }\r
74 \r
75 require (AT_INCLUDE_PATH.'header.inc.php');\r
76 \r
77 if (trim($_POST['who']) != '') $who = trim($_POST['who']);\r
78 elseif (trim($_REQUEST['who']) != '') $who = trim($_REQUEST['who']);\r
79 else $who = "public";\r
80 \r
81 // check the connection to server update.atutor.ca\r
82 $update_server = "http://update.atutor.ca"; \r
83 $connection_test_file = $update_server . '/index.php';\r
84 $connection = @file_get_contents($connection_test_file);\r
85 \r
86 if (!$connection) \r
87 {\r
88         $infos = array('CANNOT_CONNECT_PATCH_SERVER', $update_server);\r
89         $msg->addInfo($infos);\r
90         $server_connected = false;\r
91 }\r
92 else\r
93         $server_connected = true;\r
94 \r
95 // get patch list if successfully connect to patch server\r
96 if ($server_connected)\r
97 {\r
98         $patch_folder = $update_server . '/patch/' . str_replace('.', '_', VERSION) . '/';\r
99         \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 = AT_CONTENT_DIR . "patcher";\r
112                 \r
113 if ($_POST['install_upload'] && $_POST['uploading'])\r
114 {\r
115         include_once(AT_INCLUDE_PATH . '/classes/pclzip.lib.php');\r
116         \r
117         // clean up module content folder\r
118         clear_dir($module_content_folder);\r
119         \r
120         // 1. unzip uploaded file to module's content directory\r
121         $archive = new PclZip($_FILES['patchfile']['tmp_name']);\r
122 \r
123         if ($archive->extract(PCLZIP_OPT_PATH, $module_content_folder) == 0)\r
124         {\r
125     clear_dir($module_content_folder);\r
126     $msg->addError('CANNOT_UNZIP');\r
127   }\r
128 }\r
129 \r
130 // Installation process\r
131 if ($_POST['install'] || $_POST['install_upload'] && !isset($_POST["not_ignore_version"]))\r
132 {\r
133         \r
134         if (isset($_POST['id'])) $id=$_POST['id'];\r
135         else $id = $_REQUEST['id'];\r
136 \r
137         if ($_POST['install'] && $id == "")\r
138         {\r
139                 $msg->addError('CHOOSE_UNINSTALLED_PATCH');\r
140         }\r
141         else\r
142         {\r
143                 if ($_POST['install'])\r
144                 {\r
145                         $patchURL = $patch_folder . $patch_list_array[$id][patch_folder] . "/";\r
146                 }\r
147                 else if ($_POST['install_upload'])\r
148                 {\r
149                         $patchURL = $module_content_folder . "/";\r
150                 }\r
151                         \r
152                 $patch_xml = @file_get_contents($patchURL . 'patch.xml');\r
153                 \r
154                 if ($patch_xml === FALSE) \r
155                 {\r
156                         $msg->addError('PATCH_XML_NOT_FOUND');\r
157                 }\r
158                 else\r
159                 {\r
160                         require_once('classes/PatchParser.class.php');\r
161                         require_once('classes/Patch.class.php');\r
162                         \r
163                         $patchParser = new PatchParser();\r
164                         $patchParser->parse($patch_xml);\r
165                         \r
166                         $patch_array = $patchParser->getParsedArray();\r
167 \r
168                         if ($_POST["ignore_version"]) $patch_array["applied_version"] = VERSION;\r
169                         \r
170                         if ($_POST["install_upload"])\r
171                         {\r
172                                 $current_patch_list = array('atutor_patch_id' => $patch_array['atutor_patch_id'],\r
173                                                                                                                                                 'applied_version' => $patch_array['applied_version'],\r
174                                                                                                                                                 'patch_folder' => $patchURL,\r
175                                                                                                                                                 'available_to' => 'private',\r
176                                                                                                                                                 'author' => $patch_array['author'],\r
177                                                                                                                                                 'sql' => $patch_array['sql'],\r
178                                                                                                                                                 'description' => $patch_array['description'],\r
179                                                                                                                                                 'dependent_patches' => $patch_array['dependent_patches']);\r
180                         }\r
181 \r
182                         if ($_POST["install"])\r
183                         {\r
184                                 $current_patch_list = $patch_list_array[$id];\r
185                                 $current_patch_list["sql"] = $patch_array["sql"];\r
186                         }\r
187 \r
188                         if ($_POST["install_upload"] && is_patch_installed($patch_array["atutor_patch_id"]))\r
189                                 $msg->addError('PATCH_ALREADY_INSTALLED');\r
190                         else\r
191                         {\r
192                                 $patch = new Patch($patch_array, $current_patch_list, $skipFilesModified, $patchURL);\r
193                         \r
194                                 if ($patch->applyPatch())  $patch_id = $patch->getPatchID();\r
195                         }\r
196                 }\r
197         }\r
198 }\r
199 // end of patch installation\r
200 \r
201 // display permission and backup files message\r
202 if (isSet($_REQUEST['patch_id']))  $patch_id = $_REQUEST['patch_id'];\r
203 elseif ($_POST['patch_id']) $patch_id=$_POST['patch_id'];\r
204 \r
205 if ($patch_id > 0)\r
206 {\r
207         // clicking on button "Done" at displaying remove permission info page\r
208         if ($_POST['done'])\r
209         {\r
210                 $permission_files = array();\r
211                 \r
212                 if (is_array($_SESSION['remove_permission']))\r
213                 {\r
214                         foreach ($_SESSION['remove_permission'] as $file)\r
215                         {\r
216                                 if (is_writable($file))  $permission_files[] = $file;\r
217                         }\r
218                 }\r
219                 \r
220                 if (count($permission_files) == 0)\r
221                 {\r
222                         $updateInfo = array("remove_permission_files"=>"", "status"=>"Installed");\r
223                 \r
224                         updatePatchesRecord($patch_id, $updateInfo);\r
225                 }\r
226                 else\r
227                 {\r
228                         foreach($permission_files as $permission_file)\r
229                                 $remove_permission_files .= $permission_file. '|';\r
230                 \r
231                         $updateInfo = array("remove_permission_files"=>preg_quote($remove_permission_files), "status"=>"Partly Installed");\r
232                         \r
233                         updatePatchesRecord($patch_id, $updateInfo);\r
234                 }\r
235         \r
236         }\r
237         \r
238         // display remove permission info\r
239         unset($_SESSION['remove_permission']);\r
240 \r
241         $sql = "SELECT * FROM ".TABLE_PREFIX."patches \r
242                  WHERE patches_id = " . $patch_id;\r
243 \r
244         $result = mysql_query($sql, $db) or die(mysql_error());\r
245         $row = mysql_fetch_assoc($result);\r
246         \r
247         if ($row["remove_permission_files"]<> "")\r
248         {\r
249                 $remove_permission_files = $_SESSION['remove_permission'] = get_array_by_delimiter($row["remove_permission_files"], "|");\r
250 \r
251                 if (count($_SESSION['remove_permission']) > 0)\r
252                 {\r
253                         if ($_POST['done']) $msg->printErrors('REMOVE_WRITE_PERMISSION');\r
254                         else $msg->printInfos('PATCH_INSTALLED_AND_REMOVE_PERMISSION');\r
255                         \r
256                         $feedbacks[] = _AT('remove_write_permission');\r
257                         \r
258                         foreach($remove_permission_files as $remove_permission_file)\r
259                                 if ($remove_permission_file <> "") $feedbacks[count($feedbacks)-1] .= "<strong>" . $remove_permission_file . "</strong><br />";\r
260 \r
261                         $notes = '<form action="'. $_SERVER['PHP_SELF'].'?patch_id='.$patch_id.'" method="post" name="remove_permission">\r
262                   <div class="row buttons">\r
263                                 <input type="hidden" name="patch_id" value="'.$patch_id.'" />\r
264                                 <input type="submit" name="done" value="'._AT('done').'" accesskey="d" />\r
265                         </div>\r
266                         </form>';\r
267                 }\r
268 \r
269                 print_errors($feedbacks, $notes);\r
270         }\r
271 \r
272         // display backup file info after remove permission step\r
273         if ($row["remove_permission_files"] == "")\r
274         {\r
275                 $msg->printFeedbacks('PATCH_INSTALLED_SUCCESSFULLY');\r
276                 \r
277                 if ($row["backup_files"]<> "")\r
278                 {\r
279                         $backup_files = get_array_by_delimiter($row["backup_files"], "|");\r
280         \r
281                         if (count($backup_files) > 0)\r
282                         {\r
283                                 $feedbacks[] = _AT('patcher_show_backup_files');\r
284                                 \r
285                                 foreach($backup_files as $backup_file)\r
286                                         if ($backup_file <> "") $feedbacks[count($feedbacks)-1] .= "<strong>" . $backup_file . "</strong><br />";\r
287                         }\r
288                 }\r
289 \r
290                 if ($row["patch_files"]<> "")\r
291                 {\r
292                         $patch_files = get_array_by_delimiter($row["patch_files"], "|");\r
293         \r
294                         if (count($patch_files) > 0)\r
295                         {\r
296                                 $feedbacks[] = _AT('patcher_show_patch_files');\r
297                                 \r
298                                 foreach($patch_files as $patch_file)\r
299                                         if ($patch_file <> "") $feedbacks[count($feedbacks)-1] .= "<strong>" . $patch_file . "</strong><br />";\r
300                                         \r
301                         }\r
302                 }\r
303                 \r
304                 if (count($feedbacks)> 0)\r
305                         print_feedback($feedbacks);\r
306                 else\r
307                         print_feedback(array());\r
308         }\r
309 }\r
310 \r
311 $msg->printAll();\r
312 ?>\r
313 \r
314 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">\r
315 <div class="input-form">\r
316 \r
317 <table class="data" summary="" style="width: 100%" rules="cols">\r
318 <thead>\r
319         <tr>\r
320                 <th scope="col">&nbsp;</th>\r
321                 <th scope="col"><?php echo _AT('atutor_patch_id');?></th>\r
322                 <th scope="col"><?php echo _AT('description');?></th>\r
323                 <th scope="col"><?php echo _AT('status');?></th>\r
324                 <th scope="col"><?php echo _AT('available_to');?></th>\r
325                 <th scope="col"><?php echo _AT('author');?></th>\r
326                 <th scope="col"><?php echo _AT('installed_date');?></th>\r
327                 <th scope="col"><?php echo _AT('view_message');?></th>\r
328         </tr>\r
329 </thead>\r
330         \r
331 <tbody>\r
332 <?php \r
333 // display installed patches\r
334 $sql = "select * from ".TABLE_PREFIX."patches " .\r
335        "where applied_version = '" . VERSION . "' ".\r
336        "order by atutor_patch_id";\r
337 \r
338 $result = mysql_query($sql, $db);\r
339 $num_of_patches = mysql_num_rows($result) + count($patch_list_array);\r
340 \r
341 if ($num_of_patches == 0)\r
342 {\r
343 ?>\r
344 \r
345 <tr>\r
346         <td colspan="8">\r
347 <?php \r
348         echo _AT('none_found');\r
349 ?>\r
350         </td>\r
351 </tr>\r
352 \r
353 <?php \r
354 }\r
355 else\r
356 {\r
357         while ($row = mysql_fetch_assoc($result))\r
358         {\r
359                         print_patch_row($row, $row['patches_id'], false);\r
360         }\r
361         \r
362         $array_id = 0;\r
363         // display un-installed patches\r
364         if(is_array($patch_list_array))\r
365         {\r
366                 foreach ($patch_list_array as $row_num => $new_patch)\r
367                 {\r
368                         if (!is_patch_installed($new_patch['atutor_patch_id']))\r
369                         {\r
370                                 $dependent_patches_installed = true;\r
371                                 $dependent_patches = "";\r
372                                 
373                                 // check if the dependent patches are installed\r
374                                 if (is_array($new_patch["dependent_patches"]))\r
375                                 {\r
376                                         \r
377                                         foreach ($new_patch["dependent_patches"] as $num => $dependent_patch)\r
378                                         {\r
379                                                 if (!is_patch_installed($dependent_patch))\r
380                                                 {\r
381                                                         $dependent_patches_installed = false;\r
382                                                         $dependent_patches .= $dependent_patch. ", ";\r
383                                                 }\r
384                                         }\r
385                                         \r
386                                         // remove the last comma in the string\r
387                                         if ($dependent_patches <> "") $dependent_patches = substr($dependent_patches, 0, -2);\r
388                                 }\r
389         \r
390                                 // display patch row\r
391                                 if ($dependent_patches_installed)\r
392                                         print_patch_row($new_patch, $array_id++, true);\r
393                                 else\r
394                                 {\r
395                                         print_patch_row($new_patch, $array_id++, false);\r
396                                         $dependent_patches_installed = true;\r
397                                 }\r
398                         }\r
399                         else\r
400                                 $array_id++;\r
401                 }\r
402         }\r
403 ?>\r
404 </tbody>\r
405 <tfoot>\r
406 <tr>\r
407         <td colspan="8">\r
408                 <input type="submit" name="install" value="<?php echo _AT('install'); ?>" />\r
409         </td>\r
410 </tr>\r
411 </tfoot>\r
412 \r
413 <?php \r
414 }\r
415 ?>\r
416 </table>\r
417 \r
418 </div>\r
419 </form>\r
420 \r
421 <form name="frm_upload" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >\r
422         \r
423 <div class="input-form">\r
424                 <div class="row"><?php echo _AT("upload_patch"); ?></div>\r
425 \r
426                 <div class="row">\r
427                         <input type="hidden" name="MAX_FILE_SIZE" value="52428800" />\r
428                         <input type="file" name="patchfile"  size="50" />\r
429                 </div>\r
430                 \r
431                 <div class="row buttons">\r
432                         <input type="submit" name="install_upload" value="Install" onclick="javascript: return validate_filename(); " class="submit" />\r
433                         <input type="hidden" name="uploading" value="1" />\r
434                 </div>\r
435 </div>\r
436 \r
437 </form>\r
438 \r
439 <script language="JavaScript">\r
440 <!--\r
441 \r
442 String.prototype.trim = function() {\r
443         return this.replace(/^\s+|\s+$/g,"");\r
444 }\r
445 \r
446 // This function validates if and only if a zip file is given\r
447 function validate_filename() {\r
448   // check file type\r
449   var file = document.frm_upload.patchfile.value;\r
450   if (!file || file.trim()=='') {\r
451     alert('Please give a zip file!');\r
452     return false;\r
453   }\r
454   \r
455   if(file.slice(file.lastIndexOf(".")).toLowerCase() != '.zip') {\r
456     alert('Please upload ZIP file only!');\r
457     return false;\r
458   }\r
459 }\r
460 \r
461 //  End -->\r
462 //-->\r
463 </script>\r
464 \r
465 <?php require (AT_INCLUDE_PATH.'footer.inc.php'); ?>\r