PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/system/cms/modules/maintenance/controllers/admin.php

https://bitbucket.org/viktorfabry/banditos
PHP | 177 lines | 110 code | 31 blank | 36 comment | 9 complexity | 473831d2be27976de9e20a9999325522 MD5 | raw file
Possible License(s): CC-BY-3.0, BSD-3-Clause, MIT
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /**
  3. * The Maintenance Module - currently only remove/empty cache folder(s)
  4. *
  5. * @author Donald Myers
  6. * @author PyroCMS Dev Team
  7. * @package PyroCMS\Core\Modules\Maintainance\Controllers
  8. */
  9. class Admin extends Admin_Controller
  10. {
  11. private $cache_path;
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. $this->cache_path = FCPATH.APPPATH.'cache/'.SITE_REF.'/';
  16. $this->config->load('maintenance');
  17. $this->lang->load('maintenance');
  18. }
  19. /**
  20. * List all folders
  21. */
  22. public function index()
  23. {
  24. // Discover all the directories in the cache path.
  25. $cache_folders = glob($this->cache_path.'*', GLOB_ONLYDIR);
  26. // Get protected cache folders from module config file
  27. $protected = $this->config->item('maintenance.cache_protected_folders');
  28. $cannot_remove = $this->config->item('maintenance.cannot_remove_folders');
  29. $folders = array();
  30. foreach ($cache_folders as $key => $folder)
  31. {
  32. $basename = basename($folder);
  33. // If the folder is not protected
  34. if( ! in_array($basename, $protected))
  35. {
  36. // Store it in the array of the folders we will be doing something with.
  37. // Just use the filename on the front end to not expose complete paths
  38. $folders[] = array(
  39. 'name' => $basename,
  40. 'count' => count(glob($folder.'/*')),
  41. 'cannot_remove' => in_array($basename, $cannot_remove)
  42. );
  43. }
  44. }
  45. $table_list = config_item('maintenance.export_tables');
  46. asort($table_list);
  47. $tables = array();
  48. foreach ($table_list as $table)
  49. {
  50. $tables[] = array(
  51. 'name' => $table,
  52. 'count' => $this->db->count_all($table),
  53. );
  54. }
  55. $this->template
  56. ->title($this->module_details['name'])
  57. ->set('tables', $tables)
  58. ->set('folders', $folders)
  59. ->build('admin/items');
  60. }
  61. public function cleanup($name = '', $andfolder = 0)
  62. {
  63. if ( ! empty($name))
  64. {
  65. $andfolder = ($andfolder) ? true : false;
  66. $apath = $this->_refind_apath($name);
  67. if ( ! empty($apath))
  68. {
  69. $item_count = count(glob($apath.'/*'));
  70. // just empty or empty and remove?
  71. $which = ($andfolder) ? 'remove' : 'empty';
  72. if ($this->delete_files($apath, $andfolder))
  73. {
  74. $this->session->set_flashdata('success', sprintf(lang('maintenance:'.$which.'_msg'), $item_count, $name));
  75. }
  76. else
  77. {
  78. $this->session->set_flashdata('error', sprintf(lang('maintenance:'.$which.'_msg_err'), $name));
  79. }
  80. }
  81. }
  82. redirect('admin/maintenance/');
  83. }
  84. /**
  85. * Delete files from a path
  86. *
  87. * @param string $apath The path to delete files from.
  88. * @param bool $andfolder Whether to delete the folder itself or not.
  89. *
  90. * @return bool
  91. */
  92. private function delete_files($apath, $andfolder = false)
  93. {
  94. $this->load->helper('file');
  95. if ( ! delete_files($apath, true))
  96. {
  97. return false;
  98. }
  99. if ($andfolder)
  100. {
  101. if ( ! rmdir($apath))
  102. {
  103. return false;
  104. }
  105. }
  106. return true;
  107. }
  108. /**
  109. * Export a table into a specified data format.
  110. *
  111. * @param string $table The name of the table to export.
  112. * @param string $type The type in which to export the data.
  113. */
  114. public function export($table = '', $type = 'xml')
  115. {
  116. $this->load->model('maintenance_m');
  117. $this->load->helper('download');
  118. $this->load->library('format');
  119. $table_list = config_item('maintenance.export_tables');
  120. if (in_array($table, $table_list))
  121. {
  122. $this->maintenance_m->export($table, $type, $table_list);
  123. }
  124. else
  125. {
  126. redirect('admin/maintenance');
  127. }
  128. }
  129. /**
  130. * Rediscover a path.
  131. *
  132. * @param string $name The name of the path.
  133. * @return string The folder name.
  134. */
  135. private function _refind_apath($name)
  136. {
  137. $folders = glob($this->cache_path.'*', GLOB_ONLYDIR);
  138. foreach ($folders as $folder)
  139. {
  140. if (basename($folder) === $name)
  141. {
  142. return $folder;
  143. }
  144. }
  145. }
  146. }