/admin/controller/extension/manager.php

https://bitbucket.org/sandeepbhaskar/inspiredliving · PHP · 160 lines · 114 code · 39 blank · 7 comment · 27 complexity · df6ccafc5b9e8269c156871745f71089 MD5 · raw file

  1. <?php
  2. class ControllerExtensionManager extends Controller {
  3. public function index() {
  4. $this->load->language('extension/manager');
  5. $this->document->setTitle($this->language->get('heading_title'));
  6. $this->data['breadcrumbs'] = array();
  7. $this->data['breadcrumbs'][] = array(
  8. 'text' => $this->language->get('text_home'),
  9. 'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
  10. 'separator' => false
  11. );
  12. $this->data['breadcrumbs'][] = array(
  13. 'text' => $this->language->get('heading_title'),
  14. 'href' => $this->url->link('extension/manager', 'token=' . $this->session->data['token'], 'SSL'),
  15. 'separator' => ' :: '
  16. );
  17. $this->data['heading_title'] = $this->language->get('heading_title');
  18. $this->data['button_upload'] = $this->language->get('button_upload');
  19. $this->data['token'] = $this->session->data['token'];
  20. $this->template = 'extension/manager.tpl';
  21. $this->children = array(
  22. 'common/header',
  23. 'common/footer'
  24. );
  25. $this->response->setOutput($this->render());
  26. }
  27. public function upload() {
  28. $this->language->load('extension/manager');
  29. $json = array();
  30. if (!$this->user->hasPermission('modify', 'extension/manager')) {
  31. $json['error'] = $this->language->get('error_permission') . "\n";
  32. }
  33. if (!empty($this->request->files['file']['name'])) {
  34. if (strrchr($this->request->files['file']['name'], '.') != '.zip') {
  35. $json['error'] = $this->language->get('error_filetype');
  36. }
  37. if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
  38. $json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
  39. }
  40. } else {
  41. $json['error'] = $this->language->get('error_upload');
  42. }
  43. if (!isset($json['error']) && is_uploaded_file($this->request->files['file']['tmp_name']) && file_exists($this->request->files['file']['tmp_name'])) {
  44. // Unzip the files
  45. $file = $this->request->files['file']['tmp_name'];
  46. $directory = dirname($this->request->files['file']['tmp_name']) . '/' . basename($this->request->files['file']['name'], '.zip') . '/';
  47. $zip = new ZipArchive();
  48. $zip->open($file);
  49. $zip->extractTo($directory);
  50. $zip->close();
  51. // Remove Zip
  52. unlink($file);
  53. // Get a list of files ready to upload
  54. $files = array();
  55. $path = array($directory . '*');
  56. while(count($path) != 0) {
  57. $next = array_shift($path);
  58. foreach(glob($next) as $file) {
  59. if (is_dir($file)) {
  60. $path[] = $file . '/*';
  61. }
  62. $files[] = $file;
  63. }
  64. }
  65. sort($files);
  66. // Connect to the site via FTP
  67. $connection = ftp_connect($this->config->get('config_ftp_host'), $this->config->get('config_ftp_port'));
  68. if (!$connection) {
  69. exit($this->language->get('error_ftp_connection') . $this->config->get('config_ftp_host') . ':' . $this->config->get('config_ftp_port')) ;
  70. }
  71. $login = ftp_login($connection, $this->config->get('config_ftp_username'), $this->config->get('config_ftp_password'));
  72. if (!$login) {
  73. exit('Couldn\'t connect as ' . $this->config->get('config_ftp_username'));
  74. }
  75. if ($this->config->get('config_ftp_root')) {
  76. $root = ftp_chdir($connection, $this->config->get('config_ftp_root'));
  77. if (!$root) {
  78. exit('Couldn\'t change to directory ' . $this->config->get('config_ftp_root'));
  79. }
  80. }
  81. foreach ($files as $file) {
  82. // Upload everything in the upload directory
  83. if (substr(substr($file, strlen($directory)), 0, 7) == 'upload/') {
  84. $destination = substr(substr($file, strlen($directory)), 7);
  85. if (is_dir($file)) {
  86. $list = ftp_nlist($connection, substr($destination, 0, strrpos($destination, '/')));
  87. if (!in_array($destination, $list)) {
  88. if (ftp_mkdir($connection, $destination)) {
  89. echo 'made directory ' . $destination . '<br />';
  90. }
  91. }
  92. }
  93. if (is_file($file)) {
  94. if (ftp_put($connection, $destination, $file, FTP_ASCII)) {
  95. echo 'Successfully uploaded ' . $file . '<br />';
  96. }
  97. }
  98. } elseif (strrchr(basename($file), '.') == '.sql') {
  99. //file_get_contents($file);
  100. } elseif (strrchr(basename($file), '.') == '.xml') {
  101. //file_get_contents($file);
  102. }
  103. }
  104. ftp_close($connection);
  105. rsort($files);
  106. foreach ($files as $file) {
  107. if (is_file($file)) {
  108. unlink($file);
  109. } elseif (is_dir($file)) {
  110. rmdir($file);
  111. }
  112. }
  113. if (file_exists($directory)) {
  114. rmdir($directory);
  115. }
  116. $json['success'] = $this->language->get('text_success');
  117. }
  118. $this->response->setOutput(json_encode($json));
  119. }
  120. }
  121. ?>