PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/mods/_core/themes/install_themes.php

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