made a copy
[atutor.git] / admin / modules / install_modules.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto          */
7 /* http://atutor.ca                                                     */
8 /*                                                                      */
9 /* This program is free software. You can redistribute it and/or        */
10 /* modify it under the terms of the GNU General Public License          */
11 /* as published by the Free Software Foundation.                        */
12 /************************************************************************/
13 // $Id: 
14
15 define('AT_INCLUDE_PATH', '../../include/');
16 require (AT_INCLUDE_PATH.'vitals.inc.php');
17 admin_authenticate(AT_ADMIN_PRIV_ADMIN);
18 require(AT_INCLUDE_PATH.'classes/Module/ModuleListParser.class.php');
19 require(AT_INCLUDE_PATH.'lib/filemanager.inc.php');
20
21 // delete all folders and files in $dir
22 function clear_dir($dir)
23 {
24         if ($dh = opendir($dir)) 
25         {
26                 while (($file = readdir($dh)) !== false)
27                 {
28                         if (($file == '.') || ($file == '..'))
29                                 continue;
30
31                         if (is_dir($dir.$file)) 
32                                 clr_dir($dir.$file);
33                         else 
34                                 unlink($dir.$file);
35                 }
36                 
37                 closedir($dh);
38         }
39 }
40
41 set_time_limit(0);
42
43 // check the connection to server update.atutor.ca
44 $update_server = "http://update.atutor.ca"; 
45 $connection_test_file = $update_server . '/index.php';
46 $connection = @file_get_contents($connection_test_file);
47
48 if (!$connection) 
49 {
50         $msg->addInfo(array('CANNOT_CONNECT_MOD_SERVER'));
51 }
52 else
53 {
54         // get module list
55         $module_folder = $update_server . '/modules/';
56         
57         $module_list_xml = @file_get_contents($module_folder . 'module_list.xml');
58         
59         if ($module_list_xml) 
60         {
61                 $moduleListParser = new ModuleListParser();
62                 $moduleListParser->parse($module_list_xml);
63                 $module_list_array = $moduleListParser->getParsedArray();
64         }
65         // end of get module list
66         
67         $module_content_folder = AT_CONTENT_DIR . "module/";
68         
69         if (!is_dir($module_content_folder)) mkdir($module_content_folder);
70 }
71 // end of get module list
72
73 $module_content_folder = AT_CONTENT_DIR . "module/";
74
75 if (!is_dir($module_content_folder)) mkdir($module_content_folder);
76
77 // Installation process
78 if ((isset($_POST['install']) || isset($_POST["download"]) || isset($_POST["version_history"])) && !isset($_POST["id"]))
79 {
80         $msg->addError('NO_ITEM_SELECTED');
81 }
82 else if (isset($_POST['install']) || isset($_POST["download"]) || isset($_POST["version_history"]) || isset($_POST["install_upload"]))
83 {
84         if ($_POST['version_history'])
85         {
86                 header('Location: '.AT_BASE_HREF.'admin/modules/version_history.php?id='.$_POST["id"]);
87                 exit;
88         }
89
90         // install and download
91         if ($_POST["install_upload"])
92                 $module_zip_file = $_FILES['modulefile']['tmp_name'];
93         else
94                 $module_zip_file = $module_folder . $module_list_array[$_POST["id"]]['history'][0]['location'].$module_list_array[$_POST["id"]]['history'][0]['filename'];
95                 
96         $file_content = file_get_contents($module_zip_file);
97
98         if (!$file_content & ($_POST['install'] || $_POST['download']))
99         {
100                 $msg->addError('FILE_NOT_EXIST');
101         }
102         else
103         {
104                 if ($_POST['install'] || $_POST['install_upload'])
105                 {
106                         clear_dir($module_content_folder);
107                         
108                         // download zip file from update.atutor.ca and write into module content folder
109                         if ($_POST["install_upload"])
110                                 $local_module_zip_file = $module_content_folder . $_FILES['modulefile']['name'];
111                         else
112                                 $local_module_zip_file = $module_content_folder. $module_list_array[$_POST["id"]]['history'][0]['filename'];
113                         
114                         $fp = fopen($local_module_zip_file, "w");
115                         fwrite($fp, $file_content);
116                         fclose($fp);
117                         
118                         // unzip uploaded file to module's content directory
119                         include_once(AT_INCLUDE_PATH . '/classes/pclzip.lib.php');
120                         
121                         $archive = new PclZip($local_module_zip_file);
122                 
123                         if ($archive->extract(PCLZIP_OPT_PATH, $module_content_folder) == 0)
124                         {
125                     clear_dir($module_content_folder);
126                     $msg->addError('CANNOT_UNZIP');
127                   }
128                 
129                   if (!$msg->containsErrors())
130                   {
131                                 // find unzip module folder name
132                                 clearstatcache();
133                                 
134                                 if ($dh = opendir($module_content_folder)) 
135                                 {
136                                         while (($module_folder = readdir($dh)) !== false)
137                                         {
138                                                 if ($module_folder <> "." && $module_folder <> ".." && is_dir($module_content_folder.$module_folder)) break;
139                                         }
140                                         
141                                         closedir($dh);
142                                 }
143
144                                 if ($module_folder == "." || $module_folder == ".." || !isset($module_folder))
145                                         $msg->addError('EMPTY_ZIP_FILE');
146                         }
147                 
148                   // check if the same module exists in "mods" folder. If exists, it has been installed
149                   if (!$msg->containsErrors())
150                   {
151                         if (is_dir("../../mods/". $module_folder))
152                                 $msg->addError('ALREADY_INSTALLED');
153                   }
154
155                   if (!$msg->containsErrors())
156                   {
157                                 header('Location: module_install_step_1.php?mod='.urlencode($module_folder).SEP.'new=1');
158                                 exit;
159                         }
160                 }
161                 
162                 if ($_POST['download'])
163                 {
164                         $id = intval($_POST['id']);
165                 
166                         header('Content-Type: application/x-zip');
167                         header('Content-transfer-encoding: binary'); 
168                         header('Content-Disposition: attachment; filename="'.htmlspecialchars($module_list_array[$id]['history'][0]['filename']).'"');
169                         header('Expires: 0');
170                         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
171                         header('Pragma: public');
172                         header('Content-Length: '.strlen($file_content));
173                 
174                         echo $file_content;
175                         exit;
176                 }
177         }
178 }
179
180 if (isset($_POST['mod'])) {
181         $dir_name = str_replace(array('.','..'), '', $_POST['mod']);
182
183         if (isset($_POST['install_manually'])) {
184                 header('Location: '.AT_BASE_HREF.'admin/modules/module_install_step_2.php?mod='.urlencode($dir_name).SEP.'new=1'.SEP.'mod_in=1');
185                 exit;
186         }
187
188 } else if (isset($_POST['install_manually'])) {
189         $msg->addError('NO_ITEM_SELECTED');
190 }
191
192 $module_list = $moduleFactory->getModules(AT_MODULE_STATUS_UNINSTALLED | AT_MODULE_STATUS_MISSING | AT_MODULE_STATUS_PARTIALLY_UNINSTALLED, AT_MODULE_TYPE_EXTRA);
193 $keys = array_keys($module_list);
194 natsort($keys);
195
196 require (AT_INCLUDE_PATH.'header.inc.php');
197
198 $msg->printAll();
199
200 ?>
201
202 <form name="frm_upload" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
203         
204 <div class="input-form">
205                 <div class="row"><?php echo _AT("upload_module"); ?></div>
206
207                 <div class="row">
208                         <input type="hidden" name="MAX_FILE_SIZE" value="52428800" />
209                         <input type="file" name="modulefile"  size="50" />
210                 </div>
211                 
212                 <div class="row buttons">
213                         <input type="submit" name="install_upload" value="<?php echo _AT('install'); ?>" onclick="javascript: return validate_filename(); " class="submit" />
214                         <input type="hidden" name="uploading" value="1" />
215                 </div>
216 </div>
217
218 </form>
219
220 <?php 
221 if (count($keys) > 0)
222 {
223 ?>
224 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="installform">
225 <table class="data" summary="" rules="cols">
226 <thead>
227 <tr>
228         <th scope="col">&nbsp;</th>
229         <th scope="col"><?php echo _AT('module_name'); ?></th>
230         <th scope="col"><?php echo _AT('directory_name'); ?></th>
231         <th scope="col"><?php echo _AT('description'); ?></th>
232 </tr>
233 </thead>
234
235 <tfoot>
236 <tr>
237         <td colspan="4">
238                 <input type="submit" name="install_manually"  value="<?php echo _AT('install'); ?>" />
239         </td>
240 </tr>
241 </tfoot>
242
243 <tbody>
244 <?php if (!empty($keys)): ?>
245         <?php foreach($keys as $dir_name) : $module =& $module_list[$dir_name]; ?>
246                 <tr onmousedown="document.installform['m_<?php echo $dir_name; ?>'].checked = true; rowselect(this);" id="r_<?php echo $dir_name; ?>">
247                         <td valign="top"><input type="radio" id="m_<?php echo $dir_name; ?>" name="mod" value="<?php echo $dir_name; ?>" /></td>
248                         <td valign="top"><label for="m_<?php echo $row['dir_name']; ?>"><?php echo $module->getName(); ?></label></td>
249                         <td valign="top"><code><?php echo $dir_name; ?>/</code></td>
250                         <td valign="top"><?php echo $module->getDescription($_SESSION['lang']); ?></td>
251                 </tr>
252         <?php endforeach; ?>
253 <?php else: ?>
254         <tr>
255                 <td colspan="4"><?php echo _AT('none_found'); ?></td>
256         </tr>
257 <?php endif; ?>
258 </tbody>
259 </table>
260 </form>
261 <br />
262 <?php 
263 }
264 ?>
265
266 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
267 <?php 
268 ?>
269 <table class="data" summary="" rules="cols">
270 <thead>
271         <tr>
272                 <th scope="col">&nbsp;</th>
273                 <th scope="col"><?php echo _AT('module_name');?></th>
274                 <th scope="col"><?php echo _AT('description');?></th>
275                 <th scope="col"><?php echo _AT('version');?></th>
276                 <th scope="col"><?php echo _AT('atutor_version_tested_with');?></th>
277                 <th scope="col"><?php echo _AT('maintainers');?></th>
278                 <th scope="col"><?php echo _AT('installed').'?';?></th>
279         </tr>
280 </thead>
281         
282 <tfoot>
283 <tr>
284         <td colspan="7">
285                 <input type="submit" name="install" value="<?php echo _AT('install'); ?>" />
286                 <input type="submit" name="download" value="<?php echo _AT('download'); ?>" />
287                 <input type="submit" name="version_history" value="<?php echo _AT('version_history'); ?>" />
288         </td>
289 </tr>
290 </tfoot>
291
292 <tbody>
293 <?php 
294 $num_of_modules = count($module_list_array);
295
296 if ($num_of_modules == 0)
297 {
298 ?>
299
300 <tr>
301         <td colspan="7"><?php echo _AT('none_found'); ?></td>
302 </tr>
303
304 <?php 
305 }
306 else
307 {
308         // display modules
309         if(is_array($module_list_array))
310         {
311                 for ($i=0; $i < $num_of_modules; $i++)
312                 {
313                         // check if the module has been installed
314                         $sql = "SELECT * FROM ".TABLE_PREFIX."modules WHERE dir_name = '" . $module_list_array[$i]["history"][0]["install_folder"] . "'";
315                         $result = mysql_query($sql, $db) or die(mysql_error());
316
317                         if (mysql_num_rows($result) == 0) $installed = false;
318                         else $installed = true;
319
320 ?>
321         <tr onmousedown="document.form['m<?php echo $i; ?>'].checked = true; rowselect(this);"  id="r_<?php echo $i; ?>">
322                 <td><input type="radio" name="id" value="<?php echo $i; ?>" id="m<?php echo $i; ?>" <?php if ($installed) echo 'disabled="disabled"'; ?> /></td>
323                 <td><label for="m<?php echo $i; ?>"><?php echo $module_list_array[$i]["name"]; ?></label></td>
324                 <td><?php echo $module_list_array[$i]["description"]; ?></td>
325                 <td><?php echo $module_list_array[$i]["history"][0]["version"]; ?></td>
326                 <td><?php echo $module_list_array[$i]["atutor_version"]; ?></td>
327                 <td><?php echo $module_list_array[$i]["history"][0]["maintainer"]; ?></td>
328                 <td><?php if ($installed) echo _AT("installed"); else echo _AT("not_installed"); ?></td>
329         </tr>
330
331 <?php 
332                 }
333         }
334
335 ?>
336 </tbody>
337
338 <?php 
339 }
340 ?>
341 </table>
342 </form>
343
344 <script language="JavaScript">
345 <!--
346
347 String.prototype.trim = function() {
348         return this.replace(/^\s+|\s+$/g,"");
349 }
350
351 // This function validates if and only if a zip file is given
352 function validate_filename() {
353   // check file type
354   var file = document.frm_upload.modulefile.value;
355   if (!file || file.trim()=='') {
356     alert('Please give a zip file!');
357     return false;
358   }
359   
360   if(file.slice(file.lastIndexOf(".")).toLowerCase() != '.zip') {
361     alert('Please upload ZIP file only!');
362     return false;
363   }
364 }
365
366 //  End -->
367 //-->
368 </script>
369
370 <?php require (AT_INCLUDE_PATH.'footer.inc.php'); ?>