Blame view

administrator/components/com_jmap/framework/file/seospider.php 8.96 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
<?php
// namespace administrator\components\com_jmap\framework\files;
/**
 * @package JMAP::FRAMEWORK::administrator::components::com_jmap
 * @subpackage framework
 * @subpackage file
 * @author Joomla! Extensions Store
 * @copyright (C) 2015 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
 */
defined('_JEXEC') or die('Restricted access');

// CSV import fields
define ('COM_JMAP_SEOSPIDER_LINK', 0);
define ('COM_JMAP_SEOSPIDER_H1', 1);
define ('COM_JMAP_SEOSPIDER_H2', 2);
define ('COM_JMAP_SEOSPIDER_H3', 3);
define ('COM_JMAP_SEOSPIDER_CANONICAL', 4);
define ('COM_JMAP_SEOSPIDER_FIELD_NUM', 5);

jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.archive');

/**
 * Importer class for seospider records
 * 
 * @package JMAP::FRAMEWORK::administrator::components::com_jmap
 * @subpackage framework
 * @subpackage file
 * @since 3.5
 */
class JMapFileSeospider extends JObject {
	/**
	 * Database connector
	 * 
	 * @access private
	 * @var Object
	 */
	private $dbo;
	
	/**
	 * Application object
	 *
	 * @access private
	 * @var Object
	 */
	private $app;
	
	/**
	 * Wrapper for the db quote function
	 *
	 * @access private
	 * @return mixed
	 */
	private function dbQuote($val, $skip = false) {
		if(!$skip) {
			return $this->dbo->quote($val);
		}
		
		return $val;
	}
	
	/**
	 * Store uploaded file to cache folder,
	 * fully manage error messages and ask for database insert
	 * 
	 * @access public
	 * @return boolean 
	 */
	public function import() {
		// Get file info
		$file = $this->app->input->files->get('datasourceimport', null, 'raw');
		$tmpFile = $file['tmp_name'];
		$tmpFileName = $file['name'];
		try {
			if(!$tmpFile || !$tmpFileName) {
				throw new JMapException(JText::_('COM_JMAP_NOFILE_SELECTED'), 'error');
			}
			
			$tmpFileExtension = @array_pop(explode('.', $tmpFileName));
			if($tmpFileExtension != 'csv') {
				throw new JMapException(JText::_('COM_JMAP_SEOSPIDER_EXT_ERROR'), 'error');
			}

			// Deserialize contents
			$fileHandle = fopen($tmpFile, "r");
			if(!is_resource($fileHandle)) {
				throw new JMapException(JText::_('COM_JMAP_DATA_FILE_NOT_READABLE'), 'error');
			}
			
			// Parse the CSV files dataset into an importable array
			$skip = true;
			$dbQueryArray = array();
			while ( $csvRecord = fgetcsv ( $fileHandle, 0, ';', '"' ) ) {
				// Skip prima riga intestazioni
				if($skip) {
					$skip = false;
					continue;
				}
				//Insert
				array_push ( $dbQueryArray, $csvRecord );
			}
			
			// Check if some valid data to import are available
			if(!count($dbQueryArray)) {
				throw new JMapException(JText::_('COM_JMAP_SEOSPIDER_NO_IMPORT_DATA_FOUND'), 'warning');
			}
			
			$allowedTags = null;
			if(JComponentHelper::getParams('com_jmap')->get('seospider_override_headings_html', 0)) {
				$allowedTags = '<p><div><span><a><section><article><img><video><ul><li><br>';
			}
			
			// Prepare the values array
			foreach ($dbQueryArray as $dbRecord) {
				// Ensure at least that the number of csv fields are correct
				if(count($dbRecord) != COM_JMAP_SEOSPIDER_FIELD_NUM) {
					continue;
				}
				
				// Check if the link as primary key already exists in this table
				$selectQuery = "SELECT" . $this->dbo->quoteName('id') .
							   "\n FROM " . $this->dbo->quoteName('#__jmap_headings') .
							   "\n WHERE" .
							   $this->dbo->quoteName('linkurl') . " = " . $this->dbo->quote($dbRecord[COM_JMAP_SEOSPIDER_LINK]);
				$linkExists = $this->dbo->setQuery ( $selectQuery )->loadResult();
	
				// NULLify fields
				$emptyRow = false;
				$skipQuoteH1 = false;
				$skipQuoteH2 = false;
				$skipQuoteH3 = false;
				if(!$dbRecord[COM_JMAP_SEOSPIDER_H1]) {
					$dbRecord[COM_JMAP_SEOSPIDER_H1] = 'NULL';
					$skipQuoteH1 = true;
				}
				if(!$dbRecord[COM_JMAP_SEOSPIDER_H2]) {
					$dbRecord[COM_JMAP_SEOSPIDER_H2] = 'NULL';
					$skipQuoteH2 = true;
				}
				if(!$dbRecord[COM_JMAP_SEOSPIDER_H3]) {
					$dbRecord[COM_JMAP_SEOSPIDER_H3] = 'NULL';
					$skipQuoteH3 = true;
				}
				
				if($dbRecord[COM_JMAP_SEOSPIDER_H1] == 'NULL' && $dbRecord[COM_JMAP_SEOSPIDER_H2] == 'NULL' && $dbRecord[COM_JMAP_SEOSPIDER_H3] == 'NULL') {
					$emptyRow = true;
				}
				
				// If the link exists just update it, otherwise insert a new one
				if($linkExists) {
					$query = "UPDATE" .
							 "\n " . $this->dbo->quoteName('#__jmap_headings') .
							 "\n SET " .
							 "\n " . $this->dbo->quoteName('h1') . " = " . $this->dbQuote(strip_tags($dbRecord[COM_JMAP_SEOSPIDER_H1], $allowedTags), $skipQuoteH1) . "," .
							 "\n " . $this->dbo->quoteName('h2') . " = " . $this->dbQuote(strip_tags($dbRecord[COM_JMAP_SEOSPIDER_H2], $allowedTags), $skipQuoteH2) . "," .
							 "\n " . $this->dbo->quoteName('h3') . " = " . $this->dbQuote(strip_tags($dbRecord[COM_JMAP_SEOSPIDER_H3], $allowedTags), $skipQuoteH3) .
							 "\n WHERE " .
							 "\n " . $this->dbo->quoteName('linkurl') . " = " . $this->dbo->quote($dbRecord[COM_JMAP_SEOSPIDER_LINK]);
					$this->dbo->setQuery ( $query );
					$this->dbo->execute ();
					
					if($emptyRow) {
						$query = "DELETE" .
								 "\n FROM " . $this->dbo->quoteName('#__jmap_headings') .
								 "\n WHERE " .
								 "\n " . $this->dbo->quoteName('linkurl') . " = " . $this->dbo->quote($dbRecord[COM_JMAP_SEOSPIDER_LINK]);
						$this->dbo->setQuery ( $query );
						$this->dbo->execute ();
					}
				} else {
					if(!$emptyRow) {
						$query = "INSERT INTO" .
								 "\n " . $this->dbo->quoteName('#__jmap_headings') . "(" .
								 $this->dbo->quoteName('linkurl') . "," .
								 $this->dbo->quoteName('h1') . "," .
								 $this->dbo->quoteName('h2') . "," .
								 $this->dbo->quoteName('h3') . ") VALUES (" .
								 $this->dbo->quote($dbRecord[COM_JMAP_SEOSPIDER_LINK]) . "," .
								 $this->dbQuote(strip_tags($dbRecord[COM_JMAP_SEOSPIDER_H1], $allowedTags), $skipQuoteH1) . "," .
								 $this->dbQuote(strip_tags($dbRecord[COM_JMAP_SEOSPIDER_H2], $allowedTags), $skipQuoteH2) . "," .
								 $this->dbQuote(strip_tags($dbRecord[COM_JMAP_SEOSPIDER_H3], $allowedTags), $skipQuoteH3) . ")";
						$this->dbo->setQuery ( $query );
						$this->dbo->execute ();
					}
				}
				
				if ($this->dbo->getErrorNum ()) {
					throw new JMapException(JText::sprintf('COM_JMAP_SEOSPIDER_ERROR_STORING_DATA', $this->dbo->getErrorMsg()), 'error');
				}
				
				// Check if the canonical link as primary key already exists in this table
				$selectQuery = "SELECT" . $this->dbo->quoteName('id') .
							   "\n FROM " . $this->dbo->quoteName('#__jmap_canonicals') .
							   "\n WHERE" .
							   $this->dbo->quoteName('linkurl') . " = " . $this->dbo->quote($dbRecord[COM_JMAP_SEOSPIDER_LINK]);
				$canonicalLinkExists = $this->dbo->setQuery ( $selectQuery )->loadResult();
				
				// If the link exists just update it, otherwise insert a new one
				$canonicalLink = trim(strip_tags($dbRecord[COM_JMAP_SEOSPIDER_CANONICAL]));
				$validCanonicalLink = filter_var(filter_var($canonicalLink, FILTER_SANITIZE_URL), FILTER_VALIDATE_URL);
				if($canonicalLinkExists) {
					if($validCanonicalLink) {
						$query = "UPDATE" .
								 "\n " . $this->dbo->quoteName('#__jmap_canonicals') .
								 "\n SET " .
								 "\n " . $this->dbo->quoteName('canonical') . " = " . $this->dbo->quote(strip_tags($dbRecord[COM_JMAP_SEOSPIDER_CANONICAL])) .
								 "\n WHERE " .
								 "\n " . $this->dbo->quoteName('linkurl') . " = " . $this->dbo->quote($dbRecord[COM_JMAP_SEOSPIDER_LINK]);
						$this->dbo->setQuery ( $query );
						$this->dbo->execute ();
					}
					if(!$canonicalLink) {
						$query = "DELETE" .
								 "\n FROM " . $this->dbo->quoteName('#__jmap_canonicals') .
								 "\n WHERE " .
								 "\n " . $this->dbo->quoteName('linkurl') . " = " . $this->dbo->quote($dbRecord[COM_JMAP_SEOSPIDER_LINK]);
						$this->dbo->setQuery ( $query );
						$this->dbo->execute ();
					}
				} else {
					if($canonicalLink && $validCanonicalLink) {
						$query = "INSERT INTO" .
								 "\n " . $this->dbo->quoteName('#__jmap_canonicals') . "(" .
								 $this->dbo->quoteName('linkurl') . "," .
								 $this->dbo->quoteName('canonical') . ") VALUES (" .
								 $this->dbo->quote($dbRecord[COM_JMAP_SEOSPIDER_LINK]) . "," .
								 $this->dbo->quote(strip_tags($dbRecord[COM_JMAP_SEOSPIDER_CANONICAL])) . ")";
						$this->dbo->setQuery ( $query );
						$this->dbo->execute ();
					}
				}
				
				if ($this->dbo->getErrorNum ()) {
					throw new JMapException(JText::sprintf('COM_JMAP_SEOSPIDER_ERROR_STORING_DATA', $this->dbo->getErrorMsg()), 'error');
				}
			}
		} catch(JMapException $e) {
			$this->setError($e);
			return false;
		} catch (Exception $e) {
			$jmapException = new JMapException(JText::sprintf('COM_JMAP_SEOSPIDER_ERROR_STORING_DATA', $e->getMessage()), 'error');
			$this->setError($jmapException);
			return false;
		}
		
		return true;
	}

	/**
	 * Class constructor
	 * 
	 * @access public
	 * @param Object $dbo
	 * @param Object $app
	 * @return Object &
	 */
	public function __construct($dbo, $app) {
		// DB connector
		$this->dbo = $dbo;
		
		// Application
		$this->app = $app;
	}
}