345c47939f35e3540f265029ff0d87810eaf876d
[atutor.git] / docs / mods / _core / modules / install_modules.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* Inclusive Design Institute                                           */
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_MODULES);
18 require(AT_INCLUDE_PATH.'../mods/_core/modules/classes/ModuleListParser.class.php');
19 require_once(AT_INCLUDE_PATH.'../mods/_core/file_manager/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.'mods/_core/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.'mods/_core/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 // check if the module has been installed
197 $sql = "SELECT * FROM ".TABLE_PREFIX."modules WHERE dir_name = '" . $module_list_array[$i]["history"][0]["install_folder"] . "'";
198 $result = mysql_query($sql, $db) or die(mysql_error());
199
200 require (AT_INCLUDE_PATH.'header.inc.php');
201
202 $msg->printAll();
203
204 ?>
205
206
207 <script language="JavaScript">
208 <!--
209
210 String.prototype.trim = function() {
211         return this.replace(/^\s+|\s+$/g,"");
212 }
213
214 // This function validates if and only if a zip file is given
215 function validate_filename() {
216   // check file type
217   var file = document.frm_upload.modulefile.value;
218   if (!file || file.trim()=='') {
219     alert('Please give a zip file!');
220     return false;
221   }
222   
223   if(file.slice(file.lastIndexOf(".")).toLowerCase() != '.zip') {
224     alert('Please upload ZIP file only!');
225     return false;
226   }
227 }
228
229 //  End -->
230 //-->
231 </script>
232
233 <?php 
234 $savant->assign('keys', $keys);
235 $savant->assign('result', $result);
236 $savant->assign('module_list', $module_list);
237 $savant->assign('module_list_array', $module_list_array);
238 $savant->display('admin/modules/install_modules.tmpl.php');
239 require (AT_INCLUDE_PATH.'footer.inc.php'); ?>