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