2 /****************************************************************/
\r
4 /****************************************************************/
\r
5 /* Copyright (c) 2002-2010 */
\r
6 /* Inclusive Design Institute */
\r
7 /* http://atutor.ca */
\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
15 $_user_location = 'admin';
\r
16 // 1. define relative path to `include` directory:
\r
17 define('AT_INCLUDE_PATH', '../../../include/');
\r
18 require (AT_INCLUDE_PATH . 'vitals.inc.php');
\r
19 require_once(AT_INCLUDE_PATH.'../mods/_core/file_manager/filemanager.inc.php');
\r
20 require (AT_INCLUDE_PATH . 'classes/pclzip.lib.php');
\r
21 require (AT_INCLUDE_PATH . '../mods/_core/themes/classes/ThemeParser.class.php');
\r
22 admin_authenticate(AT_ADMIN_PRIV_THEMES);
\r
25 if(isset($_POST['import'])) {
\r
27 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
\r
28 header('Location: index.php');
\r
33 * Imports a theme from a URL or Zip file to Atutor
\r
35 * @author Shozub Qureshi
\r
37 function import_theme() {
\r
41 if (isset($_POST['url']) && ($_POST['url'] != 'http://') ) {
\r
42 if ($content = @file_get_contents($_POST['url'])) {
\r
44 // save file to /themes/
\r
45 $filename = pathinfo($_POST['url']);
\r
46 $filename = $filename['basename'];
\r
47 $full_filename = AT_CONTENT_DIR . '/' . $filename;
\r
49 if (!$fp = fopen($full_filename, 'w+b')) {
\r
50 //Cannot open file ($filename)";
\r
51 $errors = array('CANNOT_OPEN_FILE', $filename);
\r
52 $msg->addError($errors);
\r
53 header('Location: index.php');
\r
57 if (fwrite($fp, $content, strlen($content) ) === FALSE) {
\r
58 //"Cannot write to file ($filename)";
\r
59 $errors = array('CANNOT_WRITE_FILE', $filename);
\r
60 $msg->addError($errors);
\r
61 header('Location: index.php');
\r
66 $_FILES['file']['name'] = $filename;
\r
67 $_FILES['file']['tmp_name'] = $full_filename;
\r
68 $_FILES['file']['size'] = strlen($content);
\r
70 $url_parts = pathinfo($_POST['url']);
\r
71 $package_base_name_url = $url_parts['basename'];
\r
73 $ext = pathinfo($_FILES['file']['name']);
\r
74 $ext = $ext['extension'];
\r
77 if ($_FILES['file']['error'] == 1) {
\r
78 $errors = array('FILE_MAX_SIZE', ini_get('upload_max_filesize'));
\r
79 $msg->addError($errors);
\r
80 header('Location: index.php');
\r
84 //If file has no name or no address or if the extension is not .zip
\r
85 if (!$_FILES['file']['name']
\r
86 || (!is_uploaded_file($_FILES['file']['tmp_name']) && !$_POST['url'])) {
\r
88 $msg->addError('FILE_NOT_SELECTED');
\r
89 header('Location: index.php');
\r
93 if (($ext != 'zip')) {
\r
94 $msg->addError('IMPORT_NOT_PROPER_FORMAT');
\r
95 header('Location: index.php');
\r
99 //check if file size is ZERO
\r
100 if ($_FILES['file']['size'] == 0) {
\r
101 $msg->addError('IMPORTFILE_EMPTY');
\r
102 header('Location: index.php');
\r
106 // new directory name is the filename minus the extension
\r
107 $fldrname = substr($_FILES['file']['name'], 0, -4);
\r
108 $fldrname = str_replace(' ', '_', $fldrname);
\r
109 $import_path = '../../../themes/' . $fldrname;
\r
111 //check if Folder by that name already exists
\r
112 if (is_dir($import_path)) {
\r
114 while (is_dir($import_path . '_' . $i)) {
\r
117 $fldrname = $fldrname . '_' . $i;
\r
118 $import_path = $import_path . '_' . $i;
\r
121 //if folder does not exist previously
\r
122 if (!@mkdir($import_path, 0700)) {
\r
123 $msg->addError('IMPORTDIR_FAILED');
\r
124 header('Location: index.php');
\r
128 // unzip file and save into directory in themes
\r
129 $archive = new PclZip($_FILES['file']['tmp_name']);
\r
131 //extract contents to importpath/foldrname
\r
132 if (!$archive->extract($import_path)) {
\r
133 $errors = array('IMPORT_ERROR_IN_ZIP', $archive->errorInfo(true));
\r
134 clr_dir($import_path);
\r
135 $msg->addError($errors);
\r
136 header('Location: index.php');
\r
140 $handle = opendir($import_path);
\r
141 while ($file = readdir($handle)) {
\r
142 if (is_dir($import_path.'/'.$file) && $file != '.' && $file != '..') {
\r
147 //copy contents from importpath/foldrname to importpath
\r
148 copys($import_path.'/'.$folder, $import_path);
\r
150 //delete importpath/foldrname
\r
151 clr_dir($import_path.'/'.$folder);
\r
153 $theme_xml = @file_get_contents($import_path . '/theme_info.xml');
\r
155 //Check if XML file exists (if it doesnt send error and clear directory)
\r
156 if ($theme_xml == false) {
\r
157 /** Next version 1.4.4, require themes.xml
\r
158 $msg->addError('MISSING_THEMEXML');
\r
161 clr_dir($import_path);
\r
163 header('Location: index.php');
\r
166 $version = '1.4.x';
\r
167 $extra_info = 'unspecified';
\r
169 //parse information
\r
170 $xml_parser = new ThemeParser();
\r
171 $xml_parser->parse($theme_xml);
\r
173 $version = $xml_parser->theme_rows['version'];
\r
174 $extra_info = $xml_parser->theme_rows['extra_info'];
\r
175 $type = $xml_parser->theme_rows['type'];
\r
178 $title = str_replace('_', ' ', $fldrname);
\r
179 $last_updated = date('Y-m-d');
\r
182 //if version number is not compatible with current Atutor version, set theme as disabled
\r
183 if ($version != VERSION) {
\r
187 //save information in database
\r
188 $sql = "INSERT INTO ".TABLE_PREFIX."themes VALUES ('$title', '$version', '$fldrname', '$type', '$last_updated', '$extra_info', '$status')";
\r
189 $result = mysql_query($sql, $db);
\r
191 write_to_log(AT_ADMIN_LOG_INSERT, 'themes', mysql_affected_rows($db), $sql);
\r
194 $msg->addError('IMPORT_FAILED');
\r
195 header('Location: index.php');
\r
199 if (isset($_POST['url'])) {
\r
200 @unlink($full_filename);
\r