2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2010 */
6 /* Inclusive Design Institute */
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 /************************************************************************/
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');
21 // delete all folders and files in $dir
22 function clear_dir($dir)
24 if ($dh = opendir($dir))
26 while (($file = readdir($dh)) !== false)
28 if (($file == '.') || ($file == '..'))
31 if (is_dir($dir.$file))
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);
50 $msg->addInfo(array('CANNOT_CONNECT_MOD_SERVER'));
55 $module_folder = $update_server . '/modules/';
57 $module_list_xml = @file_get_contents($module_folder . 'module_list.xml');
61 $moduleListParser = new ModuleListParser();
62 $moduleListParser->parse($module_list_xml);
63 $module_list_array = $moduleListParser->getParsedArray();
65 // end of get module list
67 $module_content_folder = AT_CONTENT_DIR . "module/";
69 if (!is_dir($module_content_folder)) mkdir($module_content_folder);
71 // end of get module list
73 $module_content_folder = AT_CONTENT_DIR . "module/";
75 if (!is_dir($module_content_folder)) mkdir($module_content_folder);
77 // Installation process
78 if ((isset($_POST['install']) || isset($_POST["download"]) || isset($_POST["version_history"])) && !isset($_POST["id"]))
80 $msg->addError('NO_ITEM_SELECTED');
82 else if (isset($_POST['install']) || isset($_POST["download"]) || isset($_POST["version_history"]) || isset($_POST["install_upload"]))
84 if ($_POST['version_history'])
86 header('Location: '.AT_BASE_HREF.'mods/_core/modules/version_history.php?id='.$_POST["id"]);
90 // install and download
91 if ($_POST["install_upload"])
92 $module_zip_file = $_FILES['modulefile']['tmp_name'];
94 $module_zip_file = $module_folder . $module_list_array[$_POST["id"]]['history'][0]['location'].$module_list_array[$_POST["id"]]['history'][0]['filename'];
96 $file_content = file_get_contents($module_zip_file);
98 if (!$file_content & ($_POST['install'] || $_POST['download']))
100 $msg->addError('FILE_NOT_EXIST');
104 if ($_POST['install'] || $_POST['install_upload'])
106 clear_dir($module_content_folder);
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'];
112 $local_module_zip_file = $module_content_folder. $module_list_array[$_POST["id"]]['history'][0]['filename'];
114 $fp = fopen($local_module_zip_file, "w");
115 fwrite($fp, $file_content);
118 // unzip uploaded file to module's content directory
119 include_once(AT_INCLUDE_PATH . '/classes/pclzip.lib.php');
121 $archive = new PclZip($local_module_zip_file);
123 if ($archive->extract(PCLZIP_OPT_PATH, $module_content_folder) == 0)
125 clear_dir($module_content_folder);
126 $msg->addError('CANNOT_UNZIP');
129 if (!$msg->containsErrors())
131 // find unzip module folder name
134 if ($dh = opendir($module_content_folder))
136 while (($module_folder = readdir($dh)) !== false)
138 if ($module_folder <> "." && $module_folder <> ".." && is_dir($module_content_folder.$module_folder)) break;
144 if ($module_folder == "." || $module_folder == ".." || !isset($module_folder))
145 $msg->addError('EMPTY_ZIP_FILE');
148 // check if the same module exists in "mods" folder. If exists, it has been installed
149 if (!$msg->containsErrors())
151 if (is_dir("../../../mods/". $module_folder))
152 $msg->addError('ALREADY_INSTALLED');
155 if (!$msg->containsErrors())
157 header('Location: module_install_step_1.php?mod='.urlencode($module_folder).SEP.'new=1');
162 if ($_POST['download'])
164 $id = intval($_POST['id']);
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));
180 if (isset($_POST['mod'])) {
181 $dir_name = str_replace(array('.','..'), '', $_POST['mod']);
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');
188 } else if (isset($_POST['install_manually'])) {
189 $msg->addError('NO_ITEM_SELECTED');
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);
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());
200 require (AT_INCLUDE_PATH.'header.inc.php');
207 <script language="JavaScript">
210 String.prototype.trim = function() {
211 return this.replace(/^\s+|\s+$/g,"");
214 // This function validates if and only if a zip file is given
215 function validate_filename() {
217 var file = document.frm_upload.modulefile.value;
218 if (!file || file.trim()=='') {
219 alert('Please give a zip file!');
223 if(file.slice(file.lastIndexOf(".")).toLowerCase() != '.zip') {
224 alert('Please upload ZIP file only!');
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'); ?>