move code up one directory
[atutor.git] / mods / _core / themes / install_themes.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_ADMIN);
18 require(AT_INCLUDE_PATH.'../mods/_core/themes/classes/ThemeListParser.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         $infos = array('CANNOT_CONNECT_SERVER', $update_server);
51         $msg->addError($infos);
52         
53         require(AT_INCLUDE_PATH.'header.inc.php');
54   $msg->printAll();
55         require(AT_INCLUDE_PATH.'footer.inc.php');
56         exit;
57 }
58
59 // get theme list
60 $theme_folder = $update_server . '/themes/';
61 $local_theme_folder = "../../../themes/";
62
63 $theme_list_xml = @file_get_contents($theme_folder . 'theme_list.xml');
64
65 if ($theme_list_xml) 
66 {
67         $themeListParser = new ThemeListParser();
68         $themeListParser->parse($theme_list_xml);
69         $theme_list_array = $themeListParser->getParsedArray();
70 }
71 // end of get theme list
72
73 $theme_content_folder = AT_CONTENT_DIR . "theme/";
74
75 // create theme content dir if not exists
76 if (!is_dir($theme_content_folder)) mkdir($theme_content_folder);
77
78 // Installation process
79 if ((isset($_POST['install']) || isset($_POST["download"]) || isset($_POST["version_history"])) && !isset($_POST["id"]))
80 {
81         $msg->addError('NO_ITEM_SELECTED');
82 }
83 else if (isset($_POST['install']) || isset($_POST["download"]) || isset($_POST["version_history"]) || isset($_POST["import"]))
84 {
85         if ($_POST['version_history'])
86         {
87                 header('Location: '.AT_BASE_HREF.'mods/_core/themes/version_history.php?id='.$_POST["id"]);
88                 exit;
89         }
90
91         // install and download
92         if ($_POST["import"])
93         {
94                 if (isset($_POST['url']) && ($_POST['url'] != 'http://') ) 
95                 {
96                         $file_content = file_get_contents($_POST['url']);
97                         $filename = pathinfo($_POST['url']);
98                         $filename = $filename['basename'];
99                 }
100                 else
101                 {
102                         $file_content = file_get_contents($_FILES['themefile']['tmp_name']);
103                         $filename = $_FILES['themefile']['name'];
104                 }
105         }
106         else
107         {
108                 $file_content = file_get_contents($theme_folder . $theme_list_array[$_POST["id"]]['history'][0]['location'].$theme_list_array[$_POST["id"]]['history'][0]['filename']);
109         }
110                 
111         if (!$file_content & ($_POST['install'] || $_POST['download']))
112         {
113                 $msg->addError('FILE_NOT_EXIST');
114         }
115         else
116         {
117                 if ($_POST['install'] || $_POST['import'])
118                 {
119                         clear_dir($theme_content_folder);
120                         
121                         // download zip file from update.atutor.ca and write into theme content folder
122                         if ($_POST["import"])
123                                 $local_theme_zip_file = $theme_content_folder . $filename;
124                         else
125                                 $local_theme_zip_file = $theme_content_folder. $theme_list_array[$_POST["id"]]['history'][0]['filename'];
126                         
127                         $fp = fopen($local_theme_zip_file, "w");
128                         fwrite($fp, $file_content);
129                         fclose($fp);
130                         
131                         // unzip uploaded file to theme's content directory
132                         include_once(AT_INCLUDE_PATH . '/classes/pclzip.lib.php');
133                         
134                         $archive = new PclZip($local_theme_zip_file);
135                 
136                         if ($archive->extract(PCLZIP_OPT_PATH, $theme_content_folder) == 0)
137                         {
138                     clear_dir($theme_content_folder);
139                     $msg->addError('CANNOT_UNZIP');
140                   }
141                 
142                   if (!$msg->containsErrors())
143                   {
144                                 // find unzip theme folder name
145                                 clearstatcache();
146                                 
147                                 if ($dh = opendir($theme_content_folder)) 
148                                 {
149                                         while (($this_theme_folder = readdir($dh)) !== false)
150                                         {
151                                                 if ($this_theme_folder <> "." && $this_theme_folder <> ".." && is_dir($theme_content_folder.$this_theme_folder)) break;
152                                         }
153                                         
154                                         closedir($dh);
155                                 }
156
157                                 if ($this_theme_folder == "." || $this_theme_folder == ".." || !isset($this_theme_folder))
158                                         $msg->addError('EMPTY_ZIP_FILE');
159                         }
160                 
161                   // check if the same theme exists in "themes" folder. If exists, it has been installed
162                   if (!$msg->containsErrors())
163                   {
164                   debug($local_theme_folder. $this_theme_folder);
165                         if (is_dir($local_theme_folder. $this_theme_folder))
166                                 $msg->addError('ALREADY_INSTALLED');
167                   }
168
169                   if (!$msg->containsErrors())
170                   {
171                                 header('Location: theme_install_step_1.php?theme='.urlencode($this_theme_folder).SEP.'title='.urlencode($theme_list_array[$_POST["id"]]["name"]));
172                                 exit;
173                         }
174                 }
175                 
176                 if ($_POST['download'])
177                 {
178                         $id = intval($_POST['id']);
179                 
180                         header('Content-Type: application/x-zip');
181                         header('Content-transfer-encoding: binary'); 
182                         header('Content-Disposition: attachment; filename="'.htmlspecialchars($theme_list_array[$id]['history'][0]['filename']).'"');
183                         header('Expires: 0');
184                         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
185                         header('Pragma: public');
186                         header('Content-Length: '.strlen($file_content));
187                 
188                         echo $file_content;
189                         exit;
190                 }
191         }
192 }
193
194 require (AT_INCLUDE_PATH.'header.inc.php');
195
196 $msg->printErrors();
197
198 ?>
199
200 <form name="frm_upload" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
201 <div class="input-form" style="width:95%;">
202         <div class="row">
203                 <h3><?php echo _AT('import_theme'); ?></h3>
204         </div>
205
206         <div class="row">
207                 <input type="hidden" name="MAX_FILE_SIZE" value="52428800" />
208                 <label for="file"><?php echo _AT('upload_theme_package'); ?></label><br />
209                 <input type="file" name="themefile" size="40" id="file" />
210         </div>
211
212         <div class="row">
213                 <label for="url"><?php echo _AT('specify_url_to_theme_package'); ?></label><br />
214                 <input type="text" name="url" value="http://" size="40" id="url" />
215         </div>
216                 
217         <div class="row buttons">
218                 <input type= "submit" name="import" value="<?php echo _AT('import'); ?>" onclick="javascript: return validate_filename(); " />
219         </div>
220 </div>
221 </form>
222
223 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
224 <?php 
225 ?>
226 <table class="data" summary="" rules="all">
227 <thead>
228         <tr>
229                 <th scope="col">&nbsp;</th>
230                 <th scope="col"><?php echo _AT('title');?></th>
231                 <th scope="col"><?php echo _AT('installed').'?';?></th>
232                 <th scope="col"><?php echo _AT('atutor_version_tested_with');?></th>
233                 <th scope="col"><?php echo _AT('description');?></th>
234                 <th scope="col"><?php echo _AT('theme_screenshot');?></th>
235         </tr>
236 </thead>
237         
238 <tfoot>
239 <tr>
240         <td colspan="6">
241                 <input type="submit" name="install" value="<?php echo _AT('install'); ?>" />
242                 <input type="submit" name="download" value="<?php echo _AT('download'); ?>" />
243                 <input type="submit" name="version_history" value="<?php echo _AT('version_history'); ?>" />
244         </td>
245 </tr>
246 </tfoot>
247
248 <tbody>
249 <?php 
250 $num_of_themes = count($theme_list_array);
251
252 if ($num_of_themes == 0)
253 {
254 ?>
255
256 <tr>
257         <td colspan="6"><?php echo _AT('none_found'); ?></td>
258 </tr>
259
260 <?php 
261 }
262 else
263 {
264         // display themes
265         if(is_array($theme_list_array))
266         {
267                 for ($i=0; $i < $num_of_themes; $i++)
268                 {
269                         // check if the theme has been installed
270                         if (is_dir($local_theme_folder . $theme_list_array[$i]["history"][0]["install_folder"]))
271                                 $installed = true;
272                         else
273                                 $installed = false;
274
275 ?>
276         <tr onmousedown="document.form['m<?php echo $i; ?>'].checked = true; rowselect(this);"  id="r_<?php echo $i; ?>">
277                 <td><input type="radio" name="id" value="<?php echo $i; ?>" id="m<?php echo $i; ?>" <?php if ($installed) echo 'disabled="disabled"'; ?> /></td>
278                 <td><label for="m<?php echo $i; ?>"><?php echo $theme_list_array[$i]["name"]; ?></label></td>
279                 <td><?php if ($installed) echo _AT("installed"); else echo _AT("not_installed"); ?></td>
280                 <td><?php echo $theme_list_array[$i]["history"][0]["atutor_version"]; ?></td>
281                 <td><?php echo $theme_list_array[$i]["description"]; ?></td>
282                 <td><?php if (file_get_contents($theme_folder.$theme_list_array[$i]["history"][0]["screenshot_file"])) { ?>
283                         <img src="<?php echo $theme_folder.$theme_list_array[$i]["history"][0]["screenshot_file"]; ?>" border="1" alt="<?php echo _AT('theme_screenshot'); ?>" />
284                         <?php }?>
285                 </td>
286         </tr>
287
288 <?php 
289                 }
290         }
291
292 ?>
293 </tbody>
294
295 <?php 
296 }
297 ?>
298 </table>
299 </form>
300
301 <script language="JavaScript">
302 <!--
303
304 String.prototype.trim = function() {
305         return this.replace(/^\s+|\s+$/g,"");
306 }
307
308 // This function validates if and only if a zip file is given
309 function validate_filename() {
310   // check file type
311   var file;
312   
313   if (document.frm_upload.themefile.value != '')
314         file = document.frm_upload.themefile.value;
315   else if (document.frm_upload.url.value != 'http://')
316         file = document.frm_upload.url.value;
317         
318   if (!file || file.trim()=='') {
319     alert('Please give a zip file!');
320     return false;
321   }
322   
323   if(file.slice(file.lastIndexOf(".")).toLowerCase() != '.zip') {
324     alert('Please upload ZIP file only!');
325     return false;
326   }
327 }
328
329 //  End -->
330 //-->
331 </script>
332
333 <?php require (AT_INCLUDE_PATH.'footer.inc.php'); ?>