PageRenderTime 32ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/install/installer/assets/helper.destroyer.php

http://section-cms.googlecode.com/
PHP | 147 lines | 71 code | 17 blank | 59 comment | 23 complexity | c0f07e4497dc245e258983f6e2de4ec7 MD5 | raw file
  1. <?php if(!defined('INST_BASEDIR')) die('Direct access is not allowed!');
  2. /* ====================================================================
  3. *
  4. * PHP Setup Wizard
  5. *
  6. * -= SELF-DESTRUCTION =-
  7. *
  8. * This file contains functions that are used to delete ALL the files
  9. * inside the Installer folder. Be VERY careful with this script and
  10. * only modify it if you are absolutly certain of what you are doing.
  11. *
  12. * ================================================================= */
  13. /**
  14. * You got to admit, that's pretty funny for a function name :)
  15. * What it does is simply trying to delete all the installer files
  16. * including this one!
  17. */
  18. function DeleteYourself()
  19. {
  20. /*
  21. * >>> IMPORTANT! <<<<
  22. *
  23. * NOTE: This code only provides you with the ability to completly make the Installer
  24. * remove itself from the webserver (if it can). HOWEVER - you can modify this
  25. * function to only delete SOME files or some spesific files rather than deleting
  26. * everything.
  27. *
  28. * Example: You have made a very descriptive "step-by-step" guide to how to get
  29. * this installer going. The file "installer.php" is only allowed to be
  30. * executed directly so this method could only delete that file. Then,
  31. * in your guide you could say "Delete the script file installer.php if
  32. * the Installer itself is unable to" - rather then saying delete the
  33. * Installer folder, which contains 55+ files including images etc.
  34. * Just a suggestion :)
  35. */
  36. global $config;
  37. // Read the base directory for files to delete and sort the array so that the longest
  38. // scopes are deleted first - example: base/aaa.txt base/folder/bbb.txt - then bbb.txt
  39. // will be the first because it has scope of 2, and aaa.txt has the scope 1
  40. $files = ReadFiles(INST_BASEDIR.INST_RUNFOLDER, $config['self_destruct_filter']);
  41. rsort($files);
  42. // Get all unique directories into one array and sort by scope as well
  43. $dirs = array();
  44. foreach($files as $file)
  45. {
  46. if(!isset($dirs[$file['dir']]))
  47. $dirs[$file['dir']] = $file['scope'];
  48. }
  49. arsort($dirs);
  50. // Success rate is stored
  51. $fileDelete = array('success'=>0, 'failed'=>0, 'total'=>count($files));
  52. $dirDelete = array('success'=>0, 'failed'=>0, 'total'=>count($dirs));
  53. // Begin with deleting the files
  54. foreach($files as $file)
  55. {
  56. if(unlink($file['dir'].$file['name']))
  57. $fileDelete['success']++;
  58. else $fileDelete['failed']++;
  59. }
  60. if($config['self_destruct_removes_folders'])
  61. {
  62. // And then delete the directories
  63. foreach($dirs as $dir=>$scope)
  64. {
  65. if(rmdir($dir))
  66. $dirDelete['success']++;
  67. else $dirDelete['failed']++;
  68. }
  69. }
  70. else
  71. {
  72. // Total reset to zero, so no folder should be
  73. // deleted, no success and no failure!
  74. $dirDelete['total'] = 0;
  75. }
  76. // Return back the success of deletion
  77. return array('dirs'=>$dirDelete, 'files'=>$fileDelete);
  78. }
  79. /**
  80. * Read everything in a folder, and recursivly go through all
  81. * subfolders to scan for more files. Define the files to include
  82. * in the read
  83. */
  84. function ReadFiles($dir, $filterExt=array())
  85. {
  86. $files = array();
  87. $checkFile = "";
  88. $extension = "";
  89. // Fix the separators in the path
  90. $dir = FixPath($dir);
  91. if(is_dir($dir))
  92. {
  93. if($opendir = opendir($dir))
  94. {
  95. // Read everything in the folder, $checkFile can be both
  96. // folder, file and for some reason '.' and '..'
  97. while(($checkFile = readdir($opendir)))
  98. {
  99. // Construct a subfolder variable and if that is
  100. // valid directory - scan those files first
  101. $subFolder = FixPath($dir.DIRECTORY_SEPARATOR.$checkFile);
  102. // If this is a directory - scan that folder
  103. // first before reading the files in here
  104. $fromFolder = array();
  105. if(is_dir($subFolder) && $checkFile != '.' && $checkFile != '..')
  106. $fromFolder = ReadFiles($subFolder, $filterExt);
  107. // Put all "from folder" files into the $files array
  108. foreach($fromFolder as $folderFile)
  109. $files[] = $folderFile;
  110. // Get file extension
  111. if(strrpos($checkFile, '.') != false)
  112. $extension = strtolower(substr($checkFile, (strrpos($checkFile, '.') + 1 )));
  113. else $extension = false;
  114. // If file has extension and either set to delete ALL extensions (empty array)
  115. // or this extension is in the array of "extensions to delete" - add to
  116. if($extension !== false && (count($filterExt) == 0 || in_array($extension, $filterExt)))
  117. {
  118. // To be sure its a file...
  119. if(filetype($dir.'/'.$checkFile) == "file")
  120. {
  121. $scope = count(explode(DIRECTORY_SEPARATOR, $dir));
  122. $files[] = array('scope'=>$scope, 'dir'=>$dir, 'name'=>$checkFile, 'ext'=>$extension);
  123. }
  124. }
  125. }
  126. closedir( $opendir );
  127. }
  128. }
  129. return $files;
  130. }