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

/upload/admin/controller/extension/installer.php

https://github.com/fabiocarneiro/opencart
PHP | 499 lines | 199 code | 56 blank | 244 comment | 43 complexity | db9acbf208b4de3b63f2b4901d110604 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, GPL-3.0
  1. <?php
  2. class ControllerExtensionInstaller extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->language->load('extension/installer');
  6. $this->document->setTitle($this->language->get('heading_title'));
  7. $this->data['heading_title'] = $this->language->get('heading_title');
  8. $this->data['entry_upload'] = $this->language->get('entry_upload');
  9. $this->data['entry_overwrite'] = $this->language->get('entry_overwrite');
  10. $this->data['entry_progress'] = $this->language->get('entry_progress');
  11. $this->data['help_upload'] = $this->language->get('help_upload');
  12. $this->data['button_upload'] = $this->language->get('button_upload');
  13. $this->data['button_continue'] = $this->language->get('button_continue');
  14. $this->data['breadcrumbs'] = array();
  15. $this->data['breadcrumbs'][] = array(
  16. 'text' => $this->language->get('text_home'),
  17. 'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL')
  18. );
  19. $this->data['breadcrumbs'][] = array(
  20. 'text' => $this->language->get('heading_title'),
  21. 'href' => $this->url->link('extension/installer', 'token=' . $this->session->data['token'], 'SSL')
  22. );
  23. $this->data['token'] = $this->session->data['token'];
  24. $directories = glob(DIR_DOWNLOAD . 'temp-*', GLOB_ONLYDIR);
  25. if ($directories) {
  26. $this->data['error_warning'] = $this->language->get('error_warning');
  27. } else {
  28. $this->data['error_warning'] = '';
  29. }
  30. $this->template = 'extension/installer.tpl';
  31. $this->children = array(
  32. 'common/header',
  33. 'common/footer'
  34. );
  35. $this->response->setOutput($this->render());
  36. }
  37. public function upload() {
  38. $this->language->load('extension/installer');
  39. $json = array();
  40. if (!$this->user->hasPermission('modify', 'extension/installer')) {
  41. $json['error'] = $this->language->get('error_permission');
  42. }
  43. if (!empty($this->request->files['file']['name'])) {
  44. if (strrchr($this->request->files['file']['name'], '.') != '.zip' && strrchr($this->request->files['file']['name'], '.') != '.xml') {
  45. $json['error'] = $this->language->get('error_filetype');
  46. }
  47. if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
  48. $json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
  49. }
  50. } else {
  51. $json['error'] = $this->language->get('error_upload');
  52. }
  53. if (!$json) {
  54. // If no temp directory exists create it
  55. $path = 'temp-' . md5(mt_rand());
  56. if (!is_dir(DIR_DOWNLOAD . $path)) {
  57. mkdir(DIR_DOWNLOAD . $path, 0777);
  58. }
  59. // Set the steps required for installation
  60. $json['step'] = array();
  61. $json['overwrite'] = array();
  62. if (strrchr($this->request->files['file']['name'], '.') == '.xml') {
  63. // If xml file copy it to the temporary directory
  64. move_uploaded_file($this->request->files['file']['tmp_name'], DIR_DOWNLOAD . $path . '/install.xml');
  65. $json['step'][] = array(
  66. 'text' => $this->language->get('text_xml'),
  67. 'url' => str_replace('&amp;', '&', $this->url->link('extension/installer/xml', 'token=' . $this->session->data['token'], 'SSL')),
  68. 'path' => $path
  69. );
  70. // Clear temporary files
  71. $json['step'][] = array(
  72. 'text' => $this->language->get('text_success'),
  73. 'url' => str_replace('&amp;', '&', $this->url->link('extension/installer/clear', 'token=' . $this->session->data['token'], 'SSL')),
  74. 'path' => $path
  75. );
  76. }
  77. if (strrchr($this->request->files['file']['name'], '.') == '.zip') {
  78. // If zip file copy it to the temp directory
  79. move_uploaded_file($this->request->files['file']['tmp_name'], DIR_DOWNLOAD . $path . '/upload.zip');
  80. $file = DIR_DOWNLOAD . $path . '/upload.zip';
  81. if (file_exists($file)) {
  82. $zip = zip_open($file);
  83. if ($zip) {
  84. // Zip
  85. $json['step'][] = array(
  86. 'text' => $this->language->get('text_unzip'),
  87. 'url' => str_replace('&amp;', '&', $this->url->link('extension/installer/unzip', 'token=' . $this->session->data['token'], 'SSL')),
  88. 'path' => $path
  89. );
  90. // FTP
  91. $json['step'][] = array(
  92. 'text' => $this->language->get('text_ftp'),
  93. 'url' => str_replace('&amp;', '&', $this->url->link('extension/installer/ftp', 'token=' . $this->session->data['token'], 'SSL')),
  94. 'path' => $path
  95. );
  96. while ($entry = zip_read($zip)) {
  97. $zip_name = zip_entry_name($entry);
  98. // SQL
  99. if (substr($zip_name, 0, 11) == 'install.sql') {
  100. $json['step'][] = array(
  101. 'text' => $this->language->get('text_sql'),
  102. 'url' => str_replace('&amp;', '&', $this->url->link('extension/installer/sql', 'token=' . $this->session->data['token'], 'SSL')),
  103. 'path' => $path
  104. );
  105. }
  106. // XML
  107. if (substr($zip_name, 0, 11) == 'install.xml') {
  108. $json['step'][] = array(
  109. 'text' => $this->language->get('text_xml'),
  110. 'url' => str_replace('&amp;', '&', $this->url->link('extension/installer/xml', 'token=' . $this->session->data['token'], 'SSL')),
  111. 'path' => $path
  112. );
  113. }
  114. // PHP
  115. if (substr($zip_name, 0, 11) == 'install.php') {
  116. $json['step'][] = array(
  117. 'text' => $this->language->get('text_php'),
  118. 'url' => str_replace('&amp;', '&', $this->url->link('extension/installer/php', 'token=' . $this->session->data['token'], 'SSL')),
  119. 'path' => $path
  120. );
  121. }
  122. // Compare admin files
  123. $file = DIR_APPLICATION . substr($zip_name, 13);
  124. if (is_file($file) && substr($zip_name, 0, 13) == 'upload/admin/') {
  125. $json['overwrite'][] = substr($zip_name, 7);
  126. }
  127. // Compare catalog files
  128. $file = DIR_CATALOG . substr($zip_name, 7);
  129. if (is_file($file) && substr($zip_name, 0, 15) == 'upload/catalog/') {
  130. $json['overwrite'][] = substr($zip_name, 7);
  131. }
  132. // Compare image files
  133. $file = DIR_IMAGE . substr($zip_name, 13);
  134. if (is_file($file) && substr($zip_name, 0, 13) == 'upload/image/') {
  135. $json['overwrite'][] = substr($zip_name, 7);
  136. }
  137. // Compare system files
  138. $file = DIR_SYSTEM . substr($zip_name, 14);
  139. if (is_file($file) && substr($zip_name, 0, 14) == 'upload/system/') {
  140. $json['overwrite'][] = substr($zip_name, 7);
  141. }
  142. }
  143. // Clear temporary files
  144. $json['step'][] = array(
  145. 'text' => $this->language->get('text_success'),
  146. 'url' => str_replace('&amp;', '&', $this->url->link('extension/installer/clear', 'token=' . $this->session->data['token'], 'SSL')),
  147. 'path' => ''
  148. );
  149. zip_close($zip);
  150. } else {
  151. $json['error'] = $this->language->get('error_unzip');
  152. }
  153. } else {
  154. $json['error'] = $this->language->get('error_upload');
  155. }
  156. }
  157. }
  158. $this->response->setOutput(json_encode($json));
  159. }
  160. public function unzip() {
  161. $this->language->load('extension/installer');
  162. $json = array();
  163. if (!$this->user->hasPermission('modify', 'extension/installer')) {
  164. $json['error'] = $this->language->get('error_permission');
  165. }
  166. // Sanitize the filename
  167. $file = DIR_DOWNLOAD . str_replace(array('../', '..\\', '..'), '', $this->request->post['path']) . '/upload.zip';
  168. if (!file_exists($file)) {
  169. $json['error'] = $this->language->get('error_file');
  170. }
  171. if (!$json) {
  172. // Unzip the files
  173. $zip = new ZipArchive();
  174. if ($zip->open($file)) {
  175. $zip->extractTo(DIR_DOWNLOAD . str_replace(array('../', '..\\', '..'), '', $this->request->post['path']));
  176. $zip->close();
  177. } else {
  178. $json['error'] = $this->language->get('error_unzip');
  179. }
  180. // Remove Zip
  181. unlink($file);
  182. }
  183. $this->response->setOutput(json_encode($json));
  184. }
  185. public function ftp() {
  186. $this->language->load('extension/installer');
  187. $json = array();
  188. if (!$this->user->hasPermission('modify', 'extension/installer')) {
  189. $json['error'] = $this->language->get('error_permission');
  190. }
  191. $directory = DIR_DOWNLOAD . str_replace(array('../', '..\\', '..'), '', $this->request->post['path']) . '/upload/';
  192. if (!is_dir($directory)) {
  193. $json['error'] = $this->language->get('error_directory');
  194. }
  195. if (!$json) {
  196. // Get a list of files ready to upload
  197. $files = array();
  198. $path = array($directory . '*');
  199. while(count($path) != 0) {
  200. $next = array_shift($path);
  201. foreach(glob($next) as $file) {
  202. if (is_dir($file)) {
  203. $path[] = $file . '/*';
  204. }
  205. $files[] = $file;
  206. }
  207. }
  208. // Connect to the site via FTP
  209. $connection = ftp_connect($this->config->get('config_ftp_host'), $this->config->get('config_ftp_port'));
  210. if ($connection) {
  211. $login = ftp_login($connection, $this->config->get('config_ftp_username'), $this->config->get('config_ftp_password'));
  212. if ($login) {
  213. if ($this->config->get('config_ftp_root')) {
  214. $root = ftp_chdir($connection, $this->config->get('config_ftp_root'));
  215. } else {
  216. $root = ftp_chdir($connection, '/');
  217. }
  218. if ($root) {
  219. foreach ($files as $file) {
  220. // Upload everything in the upload directory
  221. $destination = substr($file, strlen($directory));
  222. if (is_dir($file)) {
  223. $list = ftp_nlist($connection, substr($destination, 0, strrpos($destination, '/')));
  224. if (!in_array($destination, $list)) {
  225. if (!ftp_mkdir($connection, $destination)) {
  226. $json['error'] = sprintf($this->language->get('error_ftp_directory'), $destination);
  227. }
  228. }
  229. }
  230. if (is_file($file)) {
  231. if (!ftp_put($connection, $destination, $file, FTP_BINARY)) {
  232. $json['error'] = sprintf($this->language->get('error_ftp_file'), $file);
  233. }
  234. }
  235. }
  236. } else {
  237. $json['error'] = sprintf($this->language->get('error_ftp_root'), $root);
  238. }
  239. } else {
  240. $json['error'] = sprintf($this->language->get('error_ftp_login'), $this->config->get('config_ftp_username'));
  241. }
  242. ftp_close($connection);
  243. } else {
  244. $json['error'] = sprintf($this->language->get('error_ftp_connection'), $this->config->get('config_ftp_host'), $this->config->get('config_ftp_port'));
  245. }
  246. }
  247. $this->response->setOutput(json_encode($json));
  248. }
  249. public function sql() {
  250. $this->language->load('extension/installer');
  251. $json = array();
  252. if (!$this->user->hasPermission('modify', 'extension/installer')) {
  253. $json['error'] = $this->language->get('error_permission');
  254. }
  255. $file = DIR_DOWNLOAD . str_replace(array('../', '..\\', '..'), '', $this->request->post['path']) . '/install.sql';
  256. if (!file_exists($file)) {
  257. $json['error'] = $this->language->get('error_file');
  258. }
  259. if (!$json) {
  260. $sql = file_get_contents($file);
  261. if ($sql) {
  262. try {
  263. $lines = explode($sql);
  264. $query = '';
  265. foreach($lines as $line) {
  266. if ($line && (substr($line, 0, 2) != '--') && (substr($line, 0, 1) != '#')) {
  267. $query .= $line;
  268. if (preg_match('/;\s*$/', $line)) {
  269. $query = str_replace("DROP TABLE IF EXISTS `oc_", "DROP TABLE IF EXISTS `" . $data['db_prefix'], $query);
  270. $query = str_replace("CREATE TABLE `oc_", "CREATE TABLE `" . $data['db_prefix'], $query);
  271. $query = str_replace("INSERT INTO `oc_", "INSERT INTO `" . $data['db_prefix'], $query);
  272. $result = mysql_query($query, $connection);
  273. if (!$result) {
  274. die(mysql_error());
  275. }
  276. $query = '';
  277. }
  278. }
  279. }
  280. } catch(Exception $e) {
  281. $json['error'] = $e->getMessage();
  282. }
  283. }
  284. }
  285. $this->response->setOutput(json_encode($json));
  286. }
  287. public function xml() {
  288. $this->language->load('extension/installer');
  289. $json = array();
  290. if (!$this->user->hasPermission('modify', 'extension/installer')) {
  291. $json['error'] = $this->language->get('error_permission');
  292. }
  293. $file = DIR_DOWNLOAD . str_replace(array('../', '..\\', '..'), '', $this->request->post['path']) . '/install.xml';
  294. if (!file_exists($file)) {
  295. $json['error'] = $this->language->get('error_file');
  296. }
  297. if (!$json) {
  298. $this->load->model('setting/modification');
  299. // If xml file just put it straight into the DB
  300. $xml = file_get_contents($file);
  301. if ($xml) {
  302. try {
  303. $dom = new DOMDocument('1.0', 'UTF-8');
  304. $dom->loadXml($xml);
  305. if (!@$dom->xml($xml, NULL, LIBXML_DTDVALID)) {
  306. $data = array(
  307. 'name' => $dom->getElementsByTagName('name')->item(0)->nodeValue,
  308. 'version' => $dom->getElementsByTagName('version')->item(0)->nodeValue,
  309. 'author' => $dom->getElementsByTagName('author')->item(0)->nodeValue,
  310. 'code' => $file,
  311. 'status' => 1,
  312. 'sort_order' => 0
  313. );
  314. $this->model_setting_modification->addModification($data);
  315. }
  316. } catch(Exception $e) {
  317. $json['error'] = $e->getMessage();
  318. }
  319. }
  320. }
  321. $this->response->setOutput(json_encode($json));
  322. }
  323. public function php() {
  324. $this->language->load('extension/installer');
  325. $json = array();
  326. if (!$this->user->hasPermission('modify', 'extension/installer')) {
  327. $json['error'] = $this->language->get('error_permission');
  328. }
  329. $file = DIR_DOWNLOAD . str_replace(array('../', '..\\', '..'), '', $this->request->post['path']) . '/install.php';
  330. if (!file_exists($file)) {
  331. $json['error'] = $this->language->get('error_file');
  332. } else {
  333. try {
  334. include($file);
  335. } catch(Exception $e) {
  336. $json['error'] = $e->getMessage();
  337. }
  338. }
  339. $this->response->setOutput(json_encode($json));
  340. }
  341. public function clear() {
  342. $this->language->load('extension/installer');
  343. $json = array();
  344. if (!$this->user->hasPermission('modify', 'extension/installer')) {
  345. $json['error'] = $this->language->get('error_permission');
  346. }
  347. if (!$json) {
  348. /*
  349. $directories = glob(DIR_DOWNLOAD . 'temp-*', GLOB_ONLYDIR);
  350. foreach($directories as $directory) {
  351. // Get a list of files ready to upload
  352. $files = array();
  353. $path = array($directory . '*');
  354. while(count($path) != 0) {
  355. $next = array_shift($path);
  356. foreach(glob($next) as $file) {
  357. if (is_dir($file)) {
  358. $path[] = $file . '/*';
  359. }
  360. $files[] = $file;
  361. }
  362. }
  363. sort($files);
  364. rsort($files);
  365. foreach ($files as $file) {
  366. if (is_file($file)) {
  367. unlink($file);
  368. } elseif (is_dir($file)) {
  369. rmdir($file);
  370. }
  371. }
  372. if (file_exists($directory)) {
  373. rmdir($directory);
  374. }
  375. }
  376. */
  377. }
  378. $this->response->setOutput(json_encode($json));
  379. }
  380. }
  381. ?>