PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/manager/actions/import_site.static.php

https://github.com/good-web-master/modx.evo.custom
PHP | 209 lines | 176 code | 23 blank | 10 comment | 30 complexity | ded9c422597ae295d6eec8eff66363e5 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, MIT, BSD-3-Clause
  1. <?php
  2. if(IN_MANAGER_MODE!="true") die("<b>INCLUDE_ORDERING_ERROR</b><br /><br />Please use the MODx Content Manager instead of accessing this file directly.");
  3. if(!$modx->hasPermission('import_static')) {
  4. $e->setError(3);
  5. $e->dumpError();
  6. }
  7. // Files to upload
  8. $allowedfiles = array('html','htm','shtml','xml');
  9. ?>
  10. <script type="text/javascript">
  11. parent.tree.ca = "parent";
  12. function setParent(pId, pName) {
  13. document.importFrm.parent.value=pId;
  14. document.getElementById('parentName').innerHTML = pId + " (" + pName + ")";
  15. }
  16. function reloadTree() {
  17. // redirect to welcome
  18. document.location.href = "index.php?r=1&a=7";
  19. }
  20. </script>
  21. <h1><?php echo $_lang['import_site_html']; ?></h1>
  22. <div class="sectionBody">
  23. <?php
  24. if(!isset($_POST['import'])) {
  25. echo "<p>".$_lang['import_site_message']."</p>";
  26. ?>
  27. <fieldset style="padding:10px"><legend><?php echo $_lang['import_site']; ?></legend>
  28. <form action="index.php" method="post" name="importFrm">
  29. <input type="hidden" name="import" value="import" />
  30. <input type="hidden" name="a" value="95" />
  31. <input type="hidden" name="parent" value="0" />
  32. <table border="0" cellspacing="0" cellpadding="2">
  33. <tr>
  34. <td nowrap="nowrap"><b><?php echo $_lang['import_parent_resource']; ?></b></td>
  35. <td>&nbsp;</td>
  36. <td><b><span id="parentName">0 (<?php echo $site_name; ?>)</span></b></td>
  37. </tr>
  38. <tr>
  39. <td valign="top"><b><?php echo $_lang['import_site_maxtime']; ?></b></td>
  40. <td>&nbsp;</td>
  41. <td><input type="text" name="maxtime" value="30" />
  42. <br />
  43. <small><?php echo $_lang['import_site_maxtime_message']; ?></small>
  44. </td>
  45. </tr>
  46. </table>
  47. <ul class="actionButtons">
  48. <li><a href="#" onclick="document.importFrm.submit();"><img src="<?php echo $_style["icons_save"] ?>" /> <?php echo $_lang["import_site_start"]; ?></a></li>
  49. </ul>
  50. </form>
  51. </fieldset>
  52. <?php
  53. } else {
  54. $maxtime = $_POST['maxtime'];
  55. if(!is_numeric($maxtime)) {
  56. $maxtime = 30;
  57. }
  58. @set_time_limit($maxtime);
  59. $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $importstart = $mtime;
  60. $parent = $_POST['parent'];
  61. $filepath = "../assets/import/";
  62. $filesfound = 0;
  63. $files = getFiles($filepath);
  64. // no. of files to import
  65. printf($_lang['import_files_found'], $filesfound);
  66. // import files
  67. if(count($files)>0) {
  68. importFiles($parent,$filepath,$files);
  69. }
  70. $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $importend = $mtime;
  71. $totaltime = ($importend - $importstart);
  72. printf ("<p>".$_lang['import_site_time']."</p>", round($totaltime, 3));
  73. ?>
  74. <ul class="actionButtons">
  75. <li><a href="#" onclick="reloadTree();"><img src="<?php echo $_style["icons_close"] ?>" /> <?php echo $_lang["close"]; ?></a></li>
  76. </ul>
  77. <script type="text/javascript">
  78. parent.tree.ca = "";
  79. </script>
  80. <?php
  81. }
  82. function importFiles($parent,$filepath,$files) {
  83. global $modx;
  84. global $_lang, $allowedfiles;
  85. global $dbase, $table_prefix;
  86. global $default_template, $search_default, $cache_default, $publish_default;
  87. $createdon = time();
  88. $createdby = $modx->getLoginUserID();
  89. if (!is_array($files)) return;
  90. foreach($files as $id => $value){
  91. if(is_array($value)) {
  92. // create folder
  93. $alias = !isset($modx->documentListing[$id]) ? $id:$id.'-'.substr(uniqid(''),-3);
  94. $modx->documentListing[$alias] = true;
  95. printf("<p>".$_lang['import_site_importing_document']."</p>", $id);
  96. $sql = "INSERT INTO $dbase.`".$table_prefix."site_content`
  97. (type, contentType, pagetitle, alias, published, parent, isfolder, content, template, menuindex, searchable, cacheable, createdby, createdon) VALUES
  98. ('document', 'text/html', '".$modx->db->escape($id)."', '".$modx->stripAlias($alias)."', ".$publish_default.", '$parent', 1, '', '".$default_template."', 0, ".$search_default.", ".$cache_default.", $createdby, $createdon);";
  99. $rs = mysql_query($sql);
  100. if($rs) $new_parent = mysql_insert_id(); // get new parent id
  101. else {
  102. echo "<p>".$_lang["import_site_failed_db_error"].mysql_error()."</p>";
  103. exit;
  104. }
  105. echo "<p class=\"success\">".$_lang["import_site_success"]."</p>";
  106. importFiles($new_parent,$filepath."/$id/",$value);
  107. }
  108. else {
  109. // create dcoument
  110. $filename = $value;
  111. $fparts = explode(".",$value);
  112. $value = $fparts[0];
  113. $ext = (count($fparts)>1)? $fparts[count($fparts)-1]:"";
  114. printf("<p>".$_lang['import_site_importing_document']."</p>", $filename);
  115. $alias = !isset($modx->documentListing[$value]) ? $value:$value.'-'.substr(uniqid(''),-3);
  116. $modx->documentListing[$alias] = true;
  117. if(!in_array($ext,$allowedfiles)) echo "<p class=\"success\">".$_lang["import_site_skip"]."</p>";
  118. else {
  119. $file = getFileContent("$filepath/$filename");
  120. if (preg_match("/<title>(.*)<\/title>/i",$file,$matches)) {
  121. $pagetitle = $matches[1];
  122. } else $pagetitle = $value;
  123. if(!$pagetitle) $pagetitle = $value;
  124. if (preg_match("/<body[^>]*>(.*)[^<]+<\/body>/is",$file,$matches)) {
  125. $content = $matches[1];
  126. } else $content = $file;
  127. $sql = "INSERT INTO $dbase.`".$table_prefix."site_content`
  128. (type, contentType, pagetitle, alias, published, parent, isfolder, content, template, menuindex, searchable, cacheable, createdby, createdon) VALUES
  129. ('document', 'text/html', '".$modx->db->escape($pagetitle)."', '".$modx->stripAlias($alias)."', ".$publish_default.", '$parent', 0, '".$modx->db->escape($content)."', '".$default_template."', 0, ".$search_default.", ".$cache_default.", $createdby, $createdon);";
  130. $rs = mysql_query($sql);
  131. if(!$rs) {
  132. echo "<p><span class=\"fail\">".$_lang["import_site_failed"]."</span> ".$_lang["import_site_failed_db_error"].mysql_error()."</p>";
  133. exit;
  134. }
  135. echo "<p class=\"success\">".$_lang["import_site_success"]."</p>";
  136. }
  137. }
  138. }
  139. }
  140. function getFiles($directory,$listing = array(), $count = 0){
  141. global $_lang;
  142. global $filesfound;
  143. $dummy = $count;
  144. if (@$handle = opendir($directory)) {
  145. while ($file = readdir($handle)) {
  146. if ($file=='.' || $file=='..') continue;
  147. else if ($h = @opendir($directory.$file."/")) {
  148. closedir($h);
  149. $count = -1;
  150. $listing["$file"] = getFiles($directory.$file."/",array(), $count + 1);
  151. }
  152. else {
  153. $listing[$dummy] = $file;
  154. $dummy = $dummy + 1;
  155. $filesfound++;
  156. }
  157. }
  158. }
  159. else {
  160. echo "<p><span class=\"fail\">".$_lang["import_site_failed"]."</span> ".$_lang["import_site_failed_no_open_dir"].$directory.".</p>";
  161. }
  162. @closedir($handle);
  163. return ($listing);
  164. }
  165. function getFileContent($file) {
  166. global $_lang;
  167. // get the file
  168. if(@$handle = fopen($file, "r")) {
  169. $buffer = "";
  170. while (!feof ($handle)) {
  171. $buffer .= fgets($handle, 4096);
  172. }
  173. fclose ($handle);
  174. }
  175. else {
  176. echo "<p><span class=\"fail\">".$_lang['import_site_failed']."</span> ".$_lang["import_site_failed_no_retrieve_file"].$file.".</p>";
  177. }
  178. return $buffer;
  179. }
  180. /**
  181. * @deprecated Use $modx->stripAlias()
  182. */
  183. function stripAlias($alias) {
  184. return $GLOBALS['modx']->stripAlias($alias);
  185. }
  186. ?>