remove old readme
[atutor.git] / docs / mods / _core / themes / import.php
1 <?php\r
2 /****************************************************************/\r
3 /* ATutor                                                                                                               */\r
4 /****************************************************************/\r
5 /* Copyright (c) 2002-2010                                      */\r
6 /* Inclusive Design Institute                                   */\r
7 /* http://atutor.ca                                                                                             */\r
8 /*                                                              */\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
13 // $Id$\r
14 \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
23 \r
24 \r
25 if(isset($_POST['import'])) {\r
26         import_theme();\r
27         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');\r
28         header('Location: index.php');\r
29         exit;\r
30 }\r
31 \r
32 /**\r
33 * Imports a theme from a URL or Zip file to Atutor\r
34 * @access  private\r
35 * @author  Shozub Qureshi\r
36 */\r
37 function import_theme() {\r
38         global $db;\r
39         global $msg;\r
40         \r
41         if (isset($_POST['url']) && ($_POST['url'] != 'http://') ) {\r
42                 if ($content = @file_get_contents($_POST['url'])) {\r
43         \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
48                         \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
54                                 exit;\r
55                         }\r
56                 \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
62                                 exit;\r
63                         }\r
64                         fclose($fp);\r
65                 }       \r
66                 $_FILES['file']['name']     = $filename;\r
67                 $_FILES['file']['tmp_name'] = $full_filename;\r
68                 $_FILES['file']['size']     = strlen($content);\r
69                 unset($content);\r
70                 $url_parts = pathinfo($_POST['url']);\r
71                 $package_base_name_url = $url_parts['basename'];\r
72         }\r
73         $ext = pathinfo($_FILES['file']['name']);\r
74         $ext = $ext['extension'];\r
75         \r
76         //error in the file\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
81                 exit;\r
82         }\r
83 \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
87 \r
88                         $msg->addError('FILE_NOT_SELECTED');\r
89                         header('Location: index.php');\r
90                         exit;\r
91         }\r
92         \r
93         if (($ext != 'zip')) {\r
94                 $msg->addError('IMPORT_NOT_PROPER_FORMAT');\r
95                 header('Location: index.php');\r
96                 exit;\r
97         }\r
98 \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
103                 exit;\r
104         }\r
105 \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
110 \r
111         //check if Folder by that name already exists\r
112         if (is_dir($import_path)) {\r
113                 $i = 1;\r
114                 while (is_dir($import_path . '_' . $i)) {\r
115                         $i++;\r
116                 }\r
117                 $fldrname    = $fldrname . '_' . $i; \r
118                 $import_path = $import_path . '_' . $i;\r
119         }\r
120         \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
125                 exit;\r
126         }\r
127         \r
128         // unzip file and save into directory in themes\r
129         $archive = new PclZip($_FILES['file']['tmp_name']);\r
130 \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
137                 exit;\r
138         }\r
139 \r
140         $handle = opendir($import_path);\r
141         while ($file = readdir($handle)) { \r
142        if (is_dir($import_path.'/'.$file) && $file != '.' && $file != '..') {\r
143                    $folder = $file;\r
144            }\r
145         }\r
146 \r
147         //copy contents from importpath/foldrname to importpath\r
148         copys($import_path.'/'.$folder, $import_path);\r
149 \r
150         //delete importpath/foldrname\r
151         clr_dir($import_path.'/'.$folder);\r
152 \r
153         $theme_xml = @file_get_contents($import_path . '/theme_info.xml');\r
154 \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
159                 \r
160                 // clean up\r
161                 clr_dir($import_path);\r
162                 \r
163                 header('Location: index.php');\r
164                 exit;\r
165                 */\r
166                 $version = '1.4.x';\r
167                 $extra_info = 'unspecified';\r
168         } else {\r
169                 //parse information\r
170                 $xml_parser = new ThemeParser();\r
171                 $xml_parser->parse($theme_xml);\r
172 \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
176         }\r
177 \r
178         $title        = str_replace('_', ' ', $fldrname);\r
179         $last_updated = date('Y-m-d');\r
180         $status       = '1';\r
181 \r
182         //if version number is not compatible with current Atutor version, set theme as disabled\r
183         if ($version != VERSION) {\r
184                 $status = '0';\r
185         }\r
186 \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
190         \r
191         write_to_log(AT_ADMIN_LOG_INSERT, 'themes', mysql_affected_rows($db), $sql);\r
192 \r
193         if (!$result) {\r
194                 $msg->addError('IMPORT_FAILED');\r
195                 header('Location: index.php');\r
196                 exit;\r
197         }\r
198 \r
199         if (isset($_POST['url'])) {\r
200                 @unlink($full_filename);\r
201         }\r
202 }\r
203 \r
204 ?>