<?xml version="1.0" encoding="utf-8"?>
<modification>
    <code>EIE001</code>
    <name>Adding support for install.php, install.sql when installing extensions</name>
    <version>1.0</version>
    <email>opencart@chankov.net</email>
	<link>http://nik.chankov.net/opencart</link>
    <author>Nik Chankov</author>
	
	<file path="admin/controller/marketplace/install.php">
        <operation>
            <search trim="true" index="0"><![CDATA[
        		$json['next'] = str_replace('&amp;', '&', $this->url->link('marketplace/install/remove', 'user_token=' . $this->session->data['user_token'], true));
            ]]></search>
            <add position="after" trim="false" offset="0"><![CDATA[
		  		$json['text'] = $this->language->get('text_sql');
		  		$json['next'] = str_replace('&amp;', '&', $this->url->link('marketplace/install/sql', 'user_token=' . $this->session->data['user_token'] . '&extension_install_id=' . $extension_install_id, true));
            ]]></add>
        </operation>
		<operation>
            <search trim="true" index="0"><![CDATA[
          		public function remove() {
            ]]></search>
            <add position="before" trim="false" offset="0"><![CDATA[
				public function php() {
					$this->load->language('marketplace/install');
					$json = array();
					if (isset($this->request->get['extension_install_id'])) {
						$extension_install_id = $this->request->get['extension_install_id'];
					} else {
						$extension_install_id = 0;
					}
					if (!$this->user->hasPermission('modify', 'marketplace/install')) {
						$json['error'] = $this->language->get('error_permission');
					}
					if (!isset($this->session->data['install'])) {
						$json['error'] = $this->language->get('error_directory');
					} elseif (!is_dir(DIR_UPLOAD . 'tmp-' . $this->session->data['install'] . '/')) {
						$json['error'] = $this->language->get('error_directory');
					}
					if (!$json) {
						$file = DIR_UPLOAD . 'tmp-' . $this->session->data['install'] . '/install.php';
						if (is_file($file)) {
							try {
								include($file);
							} catch(Exception $exception) {
								$json['error'] = sprintf($this->language->get('error_exception'), $exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine());
							}
						}
					}
					if (!$json) {
						$json['text'] = $this->language->get('text_remove');
						$json['next'] = str_replace('&amp;', '&', $this->url->link('marketplace/install/remove', 'user_token=' . $this->session->data['user_token'], true));
					}
					$this->response->addHeader('Content-Type: application/json');
					$this->response->setOutput(json_encode($json));
				}

				public function sql() {
					$this->load->language('marketplace/install');
					$json = array();
					if (isset($this->request->get['extension_install_id'])) {
						$extension_install_id = $this->request->get['extension_install_id'];
					} else {
						$extension_install_id = 0;
					}
					if (!$this->user->hasPermission('modify', 'marketplace/install')) {
						$json['error'] = $this->language->get('error_permission');
					}
					if (!isset($this->session->data['install'])) {
						$json['error'] = $this->language->get('error_directory');
					} elseif (!is_dir(DIR_UPLOAD . 'tmp-' . $this->session->data['install'] . '/')) {
						$json['error'] = $this->language->get('error_directory');
					}

					if (!$json) {
						$file = DIR_UPLOAD . 'tmp-' . $this->session->data['install'] . '/install.sql';
						if (is_file($file)) {
							$lines = file($file);
							if ($lines) {
								try {
									$sql = '';
									foreach ($lines as $line) {
										if ($line && (substr($line, 0, 2) != '--') && (substr($line, 0, 1) != '#')) {
											$sql .= $line;
											if (preg_match('/;\s*$/', $line)) {
												$sql = str_replace(" `oc_", " `" . DB_PREFIX, $sql);
												$this->db->query($sql);
												$sql = '';
											}
										}
									}
								} catch(Exception $exception) {
									$json['error'] = sprintf($this->language->get('error_exception'), $exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine());
								}
							}					
						}
					}

					if (!$json) {
						$json['text'] = $this->language->get('text_php');
						$json['next'] = str_replace('&amp;', '&', $this->url->link('marketplace/install/php', 'user_token=' . $this->session->data['user_token'] . '&extension_install_id=' . $extension_install_id, true));
					}

					$this->response->addHeader('Content-Type: application/json');
					$this->response->setOutput(json_encode($json));
				}
            ]]></add>
        </operation>
    </file>
	
	<file path="admin/language/en-gb/marketplace/install.php">
        <operation>
            <search trim="true" index="0"><![CDATA[
          		$_['text_remove']      = 'Removing temporary files!';
            ]]></search>
            <add position="after" trim="false" offset="0"><![CDATA[
		  		$_['text_sql']             = 'Running SQL!';
		  		$_['text_php']             = 'Running PHP!';
            ]]></add>
        </operation>
	</file>
</modification>