PageRenderTime 84ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/admin/controller/extension/modification.php

https://github.com/opencartlite/opencart
PHP | 603 lines | 416 code | 156 blank | 31 comment | 98 complexity | 6ed94bd858389ae2dde22f5d57387214 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, GPL-3.0
  1. <?php
  2. class ControllerExtensionModification extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->data += $this->language->load('extension/modification');
  6. $this->document->setTitle($this->language->get('heading_title'));
  7. $this->load->model('setting/modification');
  8. $this->getList();
  9. }
  10. public function insert() {
  11. $this->data += $this->language->load('extension/modification');
  12. $this->document->setTitle($this->language->get('heading_title'));
  13. $this->load->model('setting/modification');
  14. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  15. $this->model_setting_modification->addModification($this->request->post);
  16. $this->session->data['success'] = $this->language->get('text_success');
  17. $url = '';
  18. if (isset($this->request->get['sort'])) {
  19. $url .= '&sort=' . $this->request->get['sort'];
  20. }
  21. if (isset($this->request->get['order'])) {
  22. $url .= '&order=' . $this->request->get['order'];
  23. }
  24. if (isset($this->request->get['page'])) {
  25. $url .= '&page=' . $this->request->get['page'];
  26. }
  27. $this->redirect($this->url->link('extension/modification', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  28. }
  29. $this->getForm();
  30. }
  31. public function update() {
  32. $this->data += $this->language->load('extension/modification');
  33. $this->document->setTitle($this->language->get('heading_title'));
  34. $this->load->model('setting/modification');
  35. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  36. $this->model_setting_modification->editModification($this->request->get['modification_id'], $this->request->post);
  37. $this->session->data['success'] = $this->language->get('text_success');
  38. $url = '';
  39. if (isset($this->request->get['sort'])) {
  40. $url .= '&sort=' . $this->request->get['sort'];
  41. }
  42. if (isset($this->request->get['order'])) {
  43. $url .= '&order=' . $this->request->get['order'];
  44. }
  45. if (isset($this->request->get['page'])) {
  46. $url .= '&page=' . $this->request->get['page'];
  47. }
  48. $this->redirect($this->url->link('extension/modification', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  49. }
  50. $this->getForm();
  51. }
  52. public function delete() {
  53. $this->data += $this->language->load('extension/modification');
  54. $this->document->setTitle($this->language->get('heading_title'));
  55. $this->load->model('setting/modification');
  56. if (isset($this->request->post['selected']) && $this->validateDelete()) {
  57. foreach ($this->request->post['selected'] as $modification_id) {
  58. $this->model_setting_modification->deleteModification($modification_id);
  59. }
  60. $this->session->data['success'] = $this->language->get('text_success');
  61. $url = '';
  62. if (isset($this->request->get['sort'])) {
  63. $url .= '&sort=' . $this->request->get['sort'];
  64. }
  65. if (isset($this->request->get['order'])) {
  66. $url .= '&order=' . $this->request->get['order'];
  67. }
  68. if (isset($this->request->get['page'])) {
  69. $url .= '&page=' . $this->request->get['page'];
  70. }
  71. $this->redirect($this->url->link('extension/modification', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  72. }
  73. $this->getList();
  74. }
  75. public function install() {
  76. $this->data += $this->language->load('extension/payment');
  77. if (!$this->user->hasPermission('modify', 'extension/payment')) {
  78. $this->session->data['error'] = $this->language->get('error_permission');
  79. $this->redirect($this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL'));
  80. } else {
  81. $this->load->model('setting/extension');
  82. $this->model_setting_extension->install('payment', $this->request->get['extension']);
  83. $this->load->model('user/user_group');
  84. $this->model_user_user_group->addPermission($this->user->getId(), 'access', 'payment/' . $this->request->get['extension']);
  85. $this->model_user_user_group->addPermission($this->user->getId(), 'modify', 'payment/' . $this->request->get['extension']);
  86. $this->session->data['success'] = $this->language->get('text_success');
  87. $this->redirect($this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL'));
  88. }
  89. }
  90. public function uninstall() {
  91. $this->data += $this->language->load('extension/payment');
  92. if (!$this->user->hasPermission('modify', 'extension/payment')) {
  93. $this->session->data['error'] = $this->language->get('error_permission');
  94. $this->redirect($this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL'));
  95. } else {
  96. $this->load->model('setting/extension');
  97. $this->load->model('setting/setting');
  98. $this->model_setting_extension->uninstall('payment', $this->request->get['extension']);
  99. $this->model_setting_setting->deleteSetting($this->request->get['extension']);
  100. $this->session->data['success'] = $this->language->get('text_success');
  101. $this->redirect($this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL'));
  102. }
  103. }
  104. protected function getList() {
  105. if (isset($this->request->get['sort'])) {
  106. $sort = $this->request->get['sort'];
  107. } else {
  108. $sort = 'name';
  109. }
  110. if (isset($this->request->get['order'])) {
  111. $order = $this->request->get['order'];
  112. } else {
  113. $order = 'ASC';
  114. }
  115. if (isset($this->request->get['page'])) {
  116. $page = $this->request->get['page'];
  117. } else {
  118. $page = 1;
  119. }
  120. $url = '';
  121. if (isset($this->request->get['sort'])) {
  122. $url .= '&sort=' . $this->request->get['sort'];
  123. }
  124. if (isset($this->request->get['order'])) {
  125. $url .= '&order=' . $this->request->get['order'];
  126. }
  127. if (isset($this->request->get['page'])) {
  128. $url .= '&page=' . $this->request->get['page'];
  129. }
  130. $this->data += $this->language->load('extension/modification');
  131. $this->document->setTitle($this->language->get('heading_title'));
  132. $this->data['breadcrumbs'] = array();
  133. $this->data['breadcrumbs'][] = array(
  134. 'text' => $this->language->get('text_home'),
  135. 'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL')
  136. );
  137. $this->data['breadcrumbs'][] = array(
  138. 'text' => $this->language->get('heading_title'),
  139. 'href' => $this->url->link('extension/modification', 'token=' . $this->session->data['token'] . $url, 'SSL')
  140. );
  141. $this->data['insert'] = $this->url->link('extension/modification/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
  142. $this->data['delete'] = $this->url->link('extension/modification/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');
  143. $this->data['modifications'] = array();
  144. $data = array(
  145. 'sort' => $sort,
  146. 'order' => $order,
  147. 'start' => ($page - 1) * $this->config->get('config_admin_limit'),
  148. 'limit' => $this->config->get('config_admin_limit')
  149. );
  150. $modification_total = $this->model_setting_modification->getTotalModifications();
  151. $results = $this->model_setting_modification->getModifications($data);
  152. foreach ($results as $result) {
  153. $action = array();
  154. $action[] = array(
  155. 'text' => $this->language->get('text_edit'),
  156. 'href' => $this->url->link('extension/modification/update', 'token=' . $this->session->data['token'] . '&modification_id=' . $result['modification_id'] . $url, 'SSL')
  157. );
  158. $this->data['modifications'][] = array(
  159. 'modification_id' => $result['modification_id'],
  160. 'name' => $result['name'],
  161. 'author' => $result['author'],
  162. 'date_added' => $result['date_added'],
  163. 'date_modified' => $result['date_modified'],
  164. 'selected' => isset($this->request->post['selected']) && in_array($result['modification_id'], $this->request->post['selected']),
  165. 'action' => $action
  166. );
  167. }
  168. if (isset($this->session->data['success'])) {
  169. $this->data['success'] = $this->session->data['success'];
  170. unset($this->session->data['success']);
  171. } else {
  172. $this->data['success'] = '';
  173. }
  174. if (isset($this->session->data['error'])) {
  175. $this->data['error'] = $this->session->data['error'];
  176. unset($this->session->data['error']);
  177. } else {
  178. $this->data['error'] = '';
  179. }
  180. $url = '';
  181. if ($order == 'ASC') {
  182. $url .= '&order=DESC';
  183. } else {
  184. $url .= '&order=ASC';
  185. }
  186. if (isset($this->request->get['page'])) {
  187. $url .= '&page=' . $this->request->get['page'];
  188. }
  189. $this->data['sort_name'] = $this->url->link('extension/modification', 'token=' . $this->session->data['token'] . '&sort=name' . $url, 'SSL');
  190. $this->data['sort_author'] = $this->url->link('extension/modification', 'token=' . $this->session->data['token'] . '&sort=author' . $url, 'SSL');
  191. $this->data['sort_date_added'] = $this->url->link('extension/modification', 'token=' . $this->session->data['token'] . '&sort=date_added' . $url, 'SSL');
  192. $this->data['sort_date_modified'] = $this->url->link('extension/modification', 'token=' . $this->session->data['token'] . '&sort=date_modified' . $url, 'SSL');
  193. $url = '';
  194. if (isset($this->request->get['sort'])) {
  195. $url .= '&sort=' . $this->request->get['sort'];
  196. }
  197. if (isset($this->request->get['order'])) {
  198. $url .= '&order=' . $this->request->get['order'];
  199. }
  200. $pagination = new Pagination();
  201. $pagination->total = $modification_total;
  202. $pagination->page = $page;
  203. $pagination->limit = $this->config->get('config_admin_limit');
  204. $pagination->text = $this->language->get('text_pagination');
  205. $pagination->url = $this->url->link('extension/modification', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
  206. $this->data['pagination'] = $pagination->render();
  207. $this->data['sort'] = $sort;
  208. $this->data['order'] = $order;
  209. $this->template = 'extension/modification_list.tpl';
  210. $this->children = array(
  211. 'common/header',
  212. 'common/footer'
  213. );
  214. $this->response->setOutput($this->render());
  215. }
  216. protected function getForm() {
  217. if (isset($this->error['warning'])) {
  218. $this->data['error_warning'] = $this->error['warning'];
  219. } else {
  220. $this->data['error_warning'] = '';
  221. }
  222. if (isset($this->error['name'])) {
  223. $this->data['error_name'] = $this->error['name'];
  224. } else {
  225. $this->data['error_name'] = array();
  226. }
  227. $url = '';
  228. if (isset($this->request->get['sort'])) {
  229. $url .= '&sort=' . $this->request->get['sort'];
  230. }
  231. if (isset($this->request->get['order'])) {
  232. $url .= '&order=' . $this->request->get['order'];
  233. }
  234. if (isset($this->request->get['page'])) {
  235. $url .= '&page=' . $this->request->get['page'];
  236. }
  237. $this->data['breadcrumbs'] = array();
  238. $this->data['breadcrumbs'][] = array(
  239. 'text' => $this->language->get('text_home'),
  240. 'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL')
  241. );
  242. $this->data['breadcrumbs'][] = array(
  243. 'text' => $this->language->get('heading_title'),
  244. 'href' => $this->url->link('catalog/attribute', 'token=' . $this->session->data['token'] . $url, 'SSL')
  245. );
  246. if (!isset($this->request->get['modification_id'])) {
  247. $this->data['action'] = $this->url->link('extension/modification/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
  248. } else {
  249. $this->data['action'] = $this->url->link('extension/modification/update', 'token=' . $this->session->data['token'] . '&modification_id=' . $this->request->get['modification_id'] . $url, 'SSL');
  250. }
  251. $this->data['cancel'] = $this->url->link('extension/modification', 'token=' . $this->session->data['token'] . $url, 'SSL');
  252. if (isset($this->request->get['modification_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
  253. $modification_info = $this->model_extension_modification->getModification($this->request->get['modification_id']);
  254. }
  255. $this->data['token'] = $this->session->data['token'];
  256. $this->template = 'extension/modification_form.tpl';
  257. $this->children = array(
  258. 'common/header',
  259. 'common/footer'
  260. );
  261. $this->response->setOutput($this->render());
  262. }
  263. protected function validateForm() {
  264. if (!$this->user->hasPermission('modify', 'extension/modification')) {
  265. $this->error['warning'] = $this->language->get('error_permission');
  266. }
  267. if (!$this->error) {
  268. return true;
  269. } else {
  270. return false;
  271. }
  272. }
  273. protected function validateDelete() {
  274. if (!$this->user->hasPermission('modify', 'extension/modification')) {
  275. $this->error['warning'] = $this->language->get('error_permission');
  276. }
  277. if (!$this->error) {
  278. return true;
  279. } else {
  280. return false;
  281. }
  282. }
  283. public function upload() {
  284. /*
  285. New XML Modifcation Standard
  286. <modification>
  287. <id><![CDATA[Test]]></id>
  288. <name><![CDATA[1.0]]></name>
  289. <version><![CDATA[1.0]]></version>
  290. <author><![CDATA[http://www.opencart.com]]></author>
  291. <file name="catalog/controller/product/product.php" error="log|skip|abort">
  292. <operation>
  293. <search index="1" error="log|skip|abort"><![CDATA[
  294. code
  295. ]]></search>
  296. <add position="replace|before|after"><![CDATA[
  297. code
  298. ]]></add>
  299. </operation>
  300. </file>
  301. </modification>
  302. */
  303. $this->data += $this->language->load('extension/manager');
  304. $json = array();
  305. if (!$this->user->hasPermission('modify', 'extension/manager')) {
  306. $json['error'] = $this->language->get('error_permission') . "\n";
  307. }
  308. if (!empty($this->request->files['file']['name'])) {
  309. if (strrchr($this->request->files['file']['name'], '.') != '.zip' && strrchr($this->request->files['file']['name'], '.') != '.xml') {
  310. $json['error'] = $this->language->get('error_filetype');
  311. }
  312. if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
  313. $json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
  314. }
  315. } else {
  316. $json['error'] = $this->language->get('error_upload');
  317. }
  318. if (!isset($json['error']) && is_uploaded_file($this->request->files['file']['tmp_name']) && file_exists($this->request->files['file']['tmp_name'])) {
  319. $this->load->model('setting/modification');
  320. $file = $this->request->files['file']['tmp_name'];
  321. $directory = dirname($this->request->files['file']['tmp_name']) . '/' . basename($this->request->files['file']['name'], '.zip') . '/';
  322. // If xml file just put it straight into the DB
  323. if (strrchr($this->request->files['file']['tmp_name'], '.') == '.xml') {
  324. $xml = file_get_contents($this->request->files['file']['tmp_name']);
  325. $dom = new DOMDocument('1.0', 'UTF-8');
  326. $dom->loadXml($xml);
  327. $data = array(
  328. 'code' => $dom->getElementsByTagName('id')->item(0)->nodeValue,
  329. 'name' => $dom->getElementsByTagName('name')->item(0)->nodeValue,
  330. 'version' => $dom->getElementsByTagName('version')->item(0)->nodeValue,
  331. 'author' => $dom->getElementsByTagName('author')->item(0)->nodeValue,
  332. 'xml' => $xml
  333. );
  334. $this->model_setting_modification->addModification($data);
  335. unset($this->request->files['file']['tmp_name']);
  336. } else {
  337. // Unzip the files
  338. $zip = new ZipArchive();
  339. $zip->open($file);
  340. $zip->extractTo($directory);
  341. $zip->close();
  342. // Remove Zip
  343. unlink($file);
  344. // Get a list of files ready to upload
  345. $files = array();
  346. $path = array($directory . '*');
  347. while(count($path) != 0) {
  348. $next = array_shift($path);
  349. foreach(glob($next) as $file) {
  350. if (is_dir($file)) {
  351. $path[] = $file . '/*';
  352. }
  353. $files[] = $file;
  354. }
  355. }
  356. sort($files);
  357. // Connect to the site via FTP
  358. $connection = ftp_connect($this->config->get('config_ftp_host'), $this->config->get('config_ftp_port'));
  359. if (!$connection) {
  360. exit($this->language->get('error_ftp_connection') . $this->config->get('config_ftp_host') . ':' . $this->config->get('config_ftp_port')) ;
  361. }
  362. $login = ftp_login($connection, $this->config->get('config_ftp_username'), $this->config->get('config_ftp_password'));
  363. if (!$login) {
  364. exit('Couldn\'t connect as ' . $this->config->get('config_ftp_username'));
  365. }
  366. if ($this->config->get('config_ftp_root')) {
  367. $root = ftp_chdir($connection, $this->config->get('config_ftp_root'));
  368. if (!$root) {
  369. exit('Couldn\'t change to directory ' . $this->config->get('config_ftp_root'));
  370. }
  371. }
  372. foreach ($files as $file) {
  373. // Upload everything in the upload directory
  374. if (substr(substr($file, strlen($directory)), 0, 7) == 'upload/') {
  375. $destination = substr(substr($file, strlen($directory)), 7);
  376. if (is_dir($file)) {
  377. $list = ftp_nlist($connection, substr($destination, 0, strrpos($destination, '/')));
  378. if (!in_array($destination, $list)) {
  379. if (ftp_mkdir($connection, $destination)) {
  380. echo 'Made directory ' . $destination . '<br />';
  381. }
  382. }
  383. }
  384. if (is_file($file)) {
  385. if (ftp_put($connection, $destination, $file, FTP_ASCII)) {
  386. echo 'Successfully uploaded ' . $file . '<br />';
  387. }
  388. }
  389. } elseif (strrchr(basename($file), '.') == '.sql') {
  390. $sql = file_get_contents($file);
  391. } elseif (strrchr(basename($file), '.') == '.xml') {
  392. $xml = file_get_contents($file);
  393. $dom = new DOMDocument('1.0', 'UTF-8');
  394. $dom->loadXml($xml);
  395. $data = array(
  396. 'code' => $dom->getElementsByTagName('id')->item(0)->nodeValue,
  397. 'name' => $dom->getElementsByTagName('name')->item(0)->nodeValue,
  398. 'version' => $dom->getElementsByTagName('version')->item(0)->nodeValue,
  399. 'author' => $dom->getElementsByTagName('author')->item(0)->nodeValue,
  400. 'xml' => $xml
  401. );
  402. $this->model_setting_modification->addModification($data);
  403. }
  404. }
  405. ftp_close($connection);
  406. rsort($files);
  407. foreach ($files as $file) {
  408. if (is_file($file)) {
  409. unlink($file);
  410. } elseif (is_dir($file)) {
  411. rmdir($file);
  412. }
  413. }
  414. if (file_exists($directory)) {
  415. rmdir($directory);
  416. }
  417. $json['success'] = $this->language->get('text_success');
  418. }
  419. }
  420. $this->response->setOutput(json_encode($json));
  421. }
  422. public function sql() {
  423. $query = '';
  424. foreach($lines as $line) {
  425. if ($line && (substr($line, 0, 2) != '--') && (substr($line, 0, 1) != '#')) {
  426. $query .= $line;
  427. if (preg_match('/;\s*$/', $line)) {
  428. $query = str_replace("DROP TABLE IF EXISTS `oc_", "DROP TABLE IF EXISTS `" . $data['db_prefix'], $query);
  429. $query = str_replace("CREATE TABLE `oc_", "CREATE TABLE `" . $data['db_prefix'], $query);
  430. $query = str_replace("INSERT INTO `oc_", "INSERT INTO `" . $data['db_prefix'], $query);
  431. $result = mysql_query($query, $connection);
  432. if (!$result) {
  433. die(mysql_error());
  434. }
  435. $query = '';
  436. }
  437. }
  438. }
  439. }
  440. }
  441. ?>