Advertisement
Guest User

erro de instalação

a guest
May 4th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.20 KB | None | 0 0
  1. <?php
  2. class ControllerExtensionInstaller extends Controller {
  3. private $error = array();
  4.  
  5. public function index() {
  6. $this->load->language('extension/installer');
  7.  
  8. $this->document->setTitle($this->language->get('heading_title'));
  9.  
  10. $data['heading_title'] = $this->language->get('heading_title');
  11.  
  12. $data['text_loading'] = $this->language->get('text_loading');
  13.  
  14. $data['entry_upload'] = $this->language->get('entry_upload');
  15. $data['entry_overwrite'] = $this->language->get('entry_overwrite');
  16. $data['entry_progress'] = $this->language->get('entry_progress');
  17.  
  18. $data['help_upload'] = $this->language->get('help_upload');
  19.  
  20. $data['button_upload'] = $this->language->get('button_upload');
  21. $data['button_clear'] = $this->language->get('button_clear');
  22. $data['button_continue'] = $this->language->get('button_continue');
  23.  
  24. $data['breadcrumbs'] = array();
  25.  
  26. $data['breadcrumbs'][] = array(
  27. 'text' => $this->language->get('text_home'),
  28. 'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
  29. );
  30.  
  31. $data['breadcrumbs'][] = array(
  32. 'text' => $this->language->get('heading_title'),
  33. 'href' => $this->url->link('extension/installer', 'token=' . $this->session->data['token'], 'SSL')
  34. );
  35.  
  36. $data['token'] = $this->session->data['token'];
  37.  
  38. $directories = glob(DIR_UPLOAD . 'temp-*', GLOB_ONLYDIR);
  39.  
  40. if ($directories) {
  41. $data['error_warning'] = $this->language->get('error_temporary');
  42. } else {
  43. $data['error_warning'] = '';
  44. }
  45.  
  46. $data['header'] = $this->load->controller('common/header');
  47. $data['column_left'] = $this->load->controller('common/column_left');
  48. $data['footer'] = $this->load->controller('common/footer');
  49.  
  50. $this->response->setOutput($this->load->view('extension/installer.tpl', $data));
  51. }
  52.  
  53. public function upload() {
  54. $this->load->language('extension/installer');
  55.  
  56. $json = array();
  57.  
  58. // Check user has permission
  59. if (!$this->user->hasPermission('modify', 'extension/installer')) {
  60. $json['error'] = $this->language->get('error_permission');
  61. }
  62.  
  63. if (!$json) {
  64. if (!empty($this->request->files['file']['name'])) {
  65. if (substr($this->request->files['file']['name'], -10) != '.ocmod.zip' && substr($this->request->files['file']['name'], -10) != '.ocmod.xml') {
  66. $json['error'] = $this->language->get('error_filetype');
  67. }
  68.  
  69. if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
  70. $json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
  71. }
  72. } else {
  73. $json['error'] = $this->language->get('error_upload');
  74. }
  75. }
  76.  
  77. if (!$json) {
  78. // If no temp directory exists create it
  79. $path = 'temp-' . md5(mt_rand());
  80.  
  81. if (!is_dir(DIR_UPLOAD . $path)) {
  82. mkdir(DIR_UPLOAD . $path, 0777);
  83. }
  84.  
  85. // Set the steps required for installation
  86. $json['step'] = array();
  87. $json['overwrite'] = array();
  88.  
  89. if (strrchr($this->request->files['file']['name'], '.') == '.xml') {
  90. $file = DIR_UPLOAD . $path . '/install.xml';
  91.  
  92. // If xml file copy it to the temporary directory
  93. move_uploaded_file($this->request->files['file']['tmp_name'], $file);
  94.  
  95. if (file_exists($file)) {
  96. $json['step'][] = array(
  97. 'text' => $this->language->get('text_xml'),
  98. 'url' => str_replace('&amp;', '&', $this->url->link('extension/installer/xml', 'token=' . $this->session->data['token'], 'SSL')),
  99. 'path' => $path
  100. );
  101.  
  102. // Clear temporary files
  103. $json['step'][] = array(
  104. 'text' => $this->language->get('text_remove'),
  105. 'url' => str_replace('&amp;', '&', $this->url->link('extension/installer/remove', 'token=' . $this->session->data['token'], 'SSL')),
  106. 'path' => $path
  107. );
  108. } else {
  109. $json['error'] = $this->language->get('error_file');
  110. }
  111. }
  112.  
  113. // If zip file copy it to the temp directory
  114. if (strrchr($this->request->files['file']['name'], '.') == '.zip') {
  115. $file = DIR_UPLOAD . $path . '/upload.zip';
  116.  
  117. move_uploaded_file($this->request->files['file']['tmp_name'], $file);
  118.  
  119. if (file_exists($file)) {
  120. $zip = zip_open($file);
  121.  
  122. if ($zip) {
  123. // Zip
  124. $json['step'][] = array(
  125. 'text' => $this->language->get('text_unzip'),
  126. 'url' => str_replace('&amp;', '&', $this->url->link('extension/installer/unzip', 'token=' . $this->session->data['token'], 'SSL')),
  127. 'path' => $path
  128. );
  129.  
  130. // FTP_V
  131. $json['step'][] = array(
  132. 'text' => $this->language->get('text_ftp'),
  133. 'url' => str_replace('&amp;', '&', $this->url->link('extension/installer/ftp', 'token=' . $this->session->data['token'], 'SSL')),
  134. 'path' => $path
  135. );
  136.  
  137. // Send make and array of actions to carry out
  138. while ($entry = zip_read($zip)) {
  139. $zip_name = zip_entry_name($entry);
  140.  
  141. // SQL
  142. if (substr($zip_name, 0, 11) == 'install.sql') {
  143. $json['step'][] = array(
  144. 'text' => $this->language->get('text_sql'),
  145. 'url' => str_replace('&amp;', '&', $this->url->link('extension/installer/sql', 'token=' . $this->session->data['token'], 'SSL')),
  146. 'path' => $path
  147. );
  148. }
  149.  
  150. // XML
  151. if (substr($zip_name, 0, 11) == 'install.xml') {
  152. $json['step'][] = array(
  153. 'text' => $this->language->get('text_xml'),
  154. 'url' => str_replace('&amp;', '&', $this->url->link('extension/installer/xml', 'token=' . $this->session->data['token'], 'SSL')),
  155. 'path' => $path
  156. );
  157. }
  158.  
  159. // PHP
  160. if (substr($zip_name, 0, 11) == 'install.php') {
  161. $json['step'][] = array(
  162. 'text' => $this->language->get('text_php'),
  163. 'url' => str_replace('&amp;', '&', $this->url->link('extension/installer/php', 'token=' . $this->session->data['token'], 'SSL')),
  164. 'path' => $path
  165. );
  166. }
  167.  
  168. // Compare admin files
  169. $file = DIR_APPLICATION . substr($zip_name, 13);
  170.  
  171. if (is_file($file) && substr($zip_name, 0, 13) == 'upload/admin/') {
  172. $json['overwrite'][] = substr($zip_name, 7);
  173. }
  174.  
  175. // Compare catalog files
  176. $file = DIR_CATALOG . substr($zip_name, 15);
  177.  
  178. if (is_file($file) && substr($zip_name, 0, 15) == 'upload/catalog/') {
  179. $json['overwrite'][] = substr($zip_name, 7);
  180. }
  181.  
  182. // Compare image files
  183. $file = DIR_IMAGE . substr($zip_name, 13);
  184.  
  185. if (is_file($file) && substr($zip_name, 0, 13) == 'upload/image/') {
  186. $json['overwrite'][] = substr($zip_name, 7);
  187. }
  188.  
  189. // Compare system files
  190. $file = DIR_SYSTEM . substr($zip_name, 14);
  191.  
  192. if (is_file($file) && substr($zip_name, 0, 14) == 'upload/system/') {
  193. $json['overwrite'][] = substr($zip_name, 7);
  194. }
  195. }
  196.  
  197. // Clear temporary files
  198. $json['step'][] = array(
  199. 'text' => $this->language->get('text_remove'),
  200. 'url' => str_replace('&amp;', '&', $this->url->link('extension/installer/remove', 'token=' . $this->session->data['token'], 'SSL')),
  201. 'path' => $path
  202. );
  203.  
  204. zip_close($zip);
  205. } else {
  206. $json['error'] = $this->language->get('error_unzip');
  207. }
  208. } else {
  209. $json['error'] = $this->language->get('error_file');
  210. }
  211. }
  212. }
  213.  
  214. $this->response->addHeader('Content-Type: application/json');
  215. $this->response->setOutput(json_encode($json));
  216. }
  217.  
  218. public function unzip() {
  219. $this->load->language('extension/installer');
  220.  
  221. $json = array();
  222.  
  223. if (!$this->user->hasPermission('modify', 'extension/installer')) {
  224. $json['error'] = $this->language->get('error_permission');
  225. }
  226.  
  227. // Sanitize the filename
  228. $file = DIR_UPLOAD . str_replace(array('../', '..\\', '..'), '', $this->request->post['path']) . '/upload.zip';
  229.  
  230. if (!file_exists($file)) {
  231. $json['error'] = $this->language->get('error_file');
  232. }
  233.  
  234. if (!$json) {
  235. // Unzip the files
  236. $zip = new ZipArchive();
  237.  
  238. if ($zip->open($file)) {
  239. $zip->extractTo(DIR_UPLOAD . str_replace(array('../', '..\\', '..'), '', $this->request->post['path']));
  240. $zip->close();
  241. } else {
  242. $json['error'] = $this->language->get('error_unzip');
  243. }
  244.  
  245. // Remove Zip
  246. unlink($file);
  247. }
  248.  
  249. $this->response->addHeader('Content-Type: application/json');
  250. $this->response->setOutput(json_encode($json));
  251. }
  252.  
  253. public function ftp_V() {
  254. $this->load->language('extension/installer');
  255.  
  256. $json = array();
  257.  
  258. if (!$this->user->hasPermission('modify', 'extension/installer')) {
  259. $json['error'] = $this->language->get('error_permission');
  260. }
  261.  
  262. // Check FTP status
  263. if (!$this->config->get('config_ftp_status')) {
  264. $json['error'] = $this->language->get('error_ftp_status');
  265. }
  266.  
  267. $directory = DIR_UPLOAD . str_replace(array('../', '..\\', '..'), '', $this->request->post['path']) . '/upload/';
  268.  
  269. if (!is_dir($directory)) {
  270. $json['error'] = $this->language->get('error_directory');
  271. }
  272.  
  273. if (!$json) {
  274. // Get a list of files ready to upload
  275. $files = array();
  276.  
  277. $path = array($directory . '*');
  278.  
  279. while (count($path) != 0) {
  280. $next = array_shift($path);
  281.  
  282. foreach (glob($next) as $file) {
  283. if (is_dir($file)) {
  284. $path[] = $file . '/*';
  285. }
  286.  
  287. $files[] = $file;
  288. }
  289. }
  290.  
  291. // Connect to the site via FTP
  292. $connection = ftp_connect($this->config->get('config_ftp_hostname'), $this->config->get('config_ftp_v_port'));
  293.  
  294. if ($connection) {
  295. $login = ftp_v_login($connection, $this->config->get('config_ftp_v_username'), $this->config->get('config_ftp_v_password'));
  296.  
  297. if ($login) {
  298. if ($this->config->get('config_ftp_v_root')) {
  299. $root = ftp_v_chdir($connection, $this->config->get('config_ftp_v_root'));
  300. } else {
  301. $root = ftp_v_chdir($connection, '/');
  302. }
  303.  
  304. if ($root) {
  305. foreach ($files as $file) {
  306. $destination = substr($file, strlen($directory));
  307.  
  308. // Upload everything in the upload directory
  309. // Many people rename their admin folder for security purposes which I believe should be an option during installation just like setting the db prefix.
  310. // the following code would allow you to change the name of the following directories and any extensions installed will still go to the right directory.
  311. if (substr($destination, 0, 5) == 'admin') {
  312. $destination = basename(DIR_APPLICATION) . substr($destination, 5);
  313. }
  314.  
  315. if (substr($destination, 0, 7) == 'catalog') {
  316. $destination = basename(DIR_CATALOG) . substr($destination, 7);
  317. }
  318.  
  319. if (substr($destination, 0, 5) == 'image') {
  320. $destination = basename(DIR_IMAGE) . substr($destination, 5);
  321. }
  322.  
  323. if (substr($destination, 0, 6) == 'system') {
  324. $destination = basename(DIR_SYSTEM) . substr($destination, 6);
  325. }
  326.  
  327. if (is_dir($file)) {
  328. $list = ftp_v_nlist($connection, substr($destination, 0, strrpos($destination, '/')));
  329.  
  330. // Basename all the directories because on some servers they don't return the fulll paths.
  331. $list_data = array();
  332.  
  333. foreach ($list as $list) {
  334. $list_data[] = basename($list);
  335. }
  336.  
  337. if (!in_array(basename($destination), $list_data)) {
  338. if (!ftp_v_mkdir($connection, $destination)) {
  339. $json['error'] = sprintf($this->language->get('error_ftp_v_directory'), $destination);
  340. }
  341. }
  342. }
  343.  
  344. if (is_file($file)) {
  345. if (!ftp_v_put($connection, $destination, $file, ftp_v_BINARY)) {
  346. $json['error'] = sprintf($this->language->get('error_ftp_v_file'), $file);
  347. }
  348. }
  349. }
  350. } else {
  351. $json['error'] = sprintf($this->language->get('error_ftp_v_root'), $root);
  352. }
  353. } else {
  354. $json['error'] = sprintf($this->language->get('error_ftp_v_login'), $this->config->get('config_ftp_v_username'));
  355. }
  356.  
  357. ftp_v_close($connection);
  358. } else {
  359. $json['error'] = sprintf($this->language->get('error_ftp_v_connection'), $this->config->get('config_ftp_v_hostname'), $this->config->get('config_ftp_v_port'));
  360. }
  361. }
  362.  
  363. $this->response->addHeader('Content-Type: application/json');
  364. $this->response->setOutput(json_encode($json));
  365. }
  366.  
  367. public function sql() {
  368. $this->load->language('extension/installer');
  369.  
  370. $json = array();
  371.  
  372. if (!$this->user->hasPermission('modify', 'extension/installer')) {
  373. $json['error'] = $this->language->get('error_permission');
  374. }
  375.  
  376. $file = DIR_UPLOAD . str_replace(array('../', '..\\', '..'), '', $this->request->post['path']) . '/install.sql';
  377.  
  378. if (!file_exists($file)) {
  379. $json['error'] = $this->language->get('error_file');
  380. }
  381.  
  382. if (!$json) {
  383. $lines = file($file);
  384.  
  385. if ($lines) {
  386. try {
  387. $sql = '';
  388.  
  389. foreach ($lines as $line) {
  390. if ($line && (substr($line, 0, 2) != '--') && (substr($line, 0, 1) != '#')) {
  391. $sql .= $line;
  392.  
  393. if (preg_match('/;\s*$/', $line)) {
  394. $sql = str_replace(" `oc_", " `" . DB_PREFIX, $sql);
  395.  
  396. $this->db->query($sql);
  397.  
  398. $sql = '';
  399. }
  400. }
  401. }
  402. } catch(Exception $exception) {
  403. $json['error'] = sprintf($this->language->get('error_exception'), $exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine());
  404. }
  405. }
  406. }
  407.  
  408. $this->response->addHeader('Content-Type: application/json');
  409. $this->response->setOutput(json_encode($json));
  410. }
  411.  
  412. public function xml() {
  413. $this->load->language('extension/installer');
  414.  
  415. $json = array();
  416.  
  417. if (!$this->user->hasPermission('modify', 'extension/installer')) {
  418. $json['error'] = $this->language->get('error_permission');
  419. }
  420.  
  421. $file = DIR_UPLOAD . str_replace(array('../', '..\\', '..'), '', $this->request->post['path']) . '/install.xml';
  422.  
  423. if (!file_exists($file)) {
  424. $json['error'] = $this->language->get('error_file');
  425. }
  426.  
  427. if (!$json) {
  428. $this->load->model('extension/modification');
  429.  
  430. // If xml file just put it straight into the DB
  431. $xml = file_get_contents($file);
  432.  
  433. if ($xml) {
  434. try {
  435. $dom = new DOMDocument('1.0', 'UTF-8');
  436. $dom->loadXml($xml);
  437.  
  438. $name = $dom->getElementsByTagName('name')->item(0);
  439.  
  440. if ($name) {
  441. $name = $name->nodeValue;
  442. } else {
  443. $name = '';
  444. }
  445.  
  446. $code = $dom->getElementsByTagName('code')->item(0);
  447.  
  448. if ($code) {
  449. $code = $code->nodeValue;
  450.  
  451. // Check to see if the modification is already installed or not.
  452. $modification_info = $this->model_extension_modification->getModificationByCode($code);
  453.  
  454. if ($modification_info) {
  455. $json['error'] = sprintf($this->language->get('error_exists'), $modification_info['name']);
  456. }
  457. } else {
  458. $json['error'] = $this->language->get('error_code');
  459. }
  460.  
  461. $author = $dom->getElementsByTagName('author')->item(0);
  462.  
  463. if ($author) {
  464. $author = $author->nodeValue;
  465. } else {
  466. $author = '';
  467. }
  468.  
  469. $version = $dom->getElementsByTagName('version')->item(0);
  470.  
  471. if ($version) {
  472. $version = $version->nodeValue;
  473. } else {
  474. $version = '';
  475. }
  476.  
  477. $link = $dom->getElementsByTagName('link')->item(0);
  478.  
  479. if ($link) {
  480. $link = $link->nodeValue;
  481. } else {
  482. $link = '';
  483. }
  484.  
  485. $modification_data = array(
  486. 'name' => $name,
  487. 'code' => $code,
  488. 'author' => $author,
  489. 'version' => $version,
  490. 'link' => $link,
  491. 'xml' => $xml,
  492. 'status' => 1
  493. );
  494.  
  495. if (!$json) {
  496. $this->model_extension_modification->addModification($modification_data);
  497. }
  498. } catch(Exception $exception) {
  499. $json['error'] = sprintf($this->language->get('error_exception'), $exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine());
  500. }
  501. }
  502. }
  503.  
  504. $this->response->addHeader('Content-Type: application/json');
  505. $this->response->setOutput(json_encode($json));
  506. }
  507.  
  508. public function php() {
  509. $this->load->language('extension/installer');
  510.  
  511. $json = array();
  512.  
  513. if (!$this->user->hasPermission('modify', 'extension/installer')) {
  514. $json['error'] = $this->language->get('error_permission');
  515. }
  516.  
  517. $file = DIR_UPLOAD . str_replace(array('../', '..\\', '..'), '', $this->request->post['path']) . '/install.php';
  518.  
  519. if (!file_exists($file)) {
  520. $json['error'] = $this->language->get('error_file');
  521. }
  522.  
  523. if (!$json) {
  524. try {
  525. include($file);
  526. } catch(Exception $exception) {
  527. $json['error'] = sprintf($this->language->get('error_exception'), $exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine());
  528. }
  529. }
  530.  
  531. $this->response->addHeader('Content-Type: application/json');
  532. $this->response->setOutput(json_encode($json));
  533. }
  534.  
  535. public function remove() {
  536. $this->load->language('extension/installer');
  537.  
  538. $json = array();
  539.  
  540. if (!$this->user->hasPermission('modify', 'extension/installer')) {
  541. $json['error'] = $this->language->get('error_permission');
  542. }
  543.  
  544. $directory = DIR_UPLOAD . str_replace(array('../', '..\\', '..'), '', $this->request->post['path']);
  545.  
  546. if (!is_dir($directory)) {
  547. $json['error'] = $this->language->get('error_directory');
  548. }
  549.  
  550. if (!$json) {
  551. // Get a list of files ready to upload
  552. $files = array();
  553.  
  554. $path = array($directory);
  555.  
  556. while (count($path) != 0) {
  557. $next = array_shift($path);
  558.  
  559. // We have to use scandir function because glob will not pick up dot files.
  560. foreach (array_diff(scandir($next), array('.', '..')) as $file) {
  561. $file = $next . '/' . $file;
  562.  
  563. if (is_dir($file)) {
  564. $path[] = $file;
  565. }
  566.  
  567. $files[] = $file;
  568. }
  569. }
  570.  
  571. rsort($files);
  572.  
  573. foreach ($files as $file) {
  574. if (is_file($file)) {
  575. unlink($file);
  576. } elseif (is_dir($file)) {
  577. rmdir($file);
  578. }
  579. }
  580.  
  581. if (file_exists($directory)) {
  582. rmdir($directory);
  583. }
  584.  
  585. $json['success'] = $this->language->get('text_success');
  586. }
  587.  
  588. $this->response->addHeader('Content-Type: application/json');
  589. $this->response->setOutput(json_encode($json));
  590. }
  591.  
  592. public function clear() {
  593. $this->load->language('extension/installer');
  594.  
  595. $json = array();
  596.  
  597. if (!$this->user->hasPermission('modify', 'extension/installer')) {
  598. $json['error'] = $this->language->get('error_permission');
  599. }
  600.  
  601. if (!$json) {
  602. $directories = glob(DIR_UPLOAD . 'temp-*', GLOB_ONLYDIR);
  603.  
  604. foreach ($directories as $directory) {
  605. // Get a list of files ready to upload
  606. $files = array();
  607.  
  608. $path = array($directory);
  609.  
  610. while (count($path) != 0) {
  611. $next = array_shift($path);
  612.  
  613. // We have to use scandir function because glob will not pick up dot files.
  614. foreach (array_diff(scandir($next), array('.', '..')) as $file) {
  615. $file = $next . '/' . $file;
  616.  
  617. if (is_dir($file)) {
  618. $path[] = $file;
  619. }
  620.  
  621. $files[] = $file;
  622. }
  623. }
  624.  
  625. rsort($files);
  626.  
  627. foreach ($files as $file) {
  628. if (is_file($file)) {
  629. unlink($file);
  630. } elseif (is_dir($file)) {
  631. rmdir($file);
  632. }
  633. }
  634.  
  635. if (file_exists($directory)) {
  636. rmdir($directory);
  637. }
  638. }
  639.  
  640. $json['success'] = $this->language->get('text_clear');
  641. }
  642.  
  643. $this->response->addHeader('Content-Type: application/json');
  644. $this->response->setOutput(json_encode($json));
  645. }
  646. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement