<?php
class FilesAdmin extends Files{
	var $aDirs;
	function __construct( ){
		$this->generateCache( );
		$this->generateThumbDirs( );
	}
	static function &getInstance( ){
		static $oInstance = null;
		if( !isset( $oInstance ) ){
			$oInstance = new FilesAdmin( );
		}
		return $oInstance;
	}
	function listAllFilesAdmin( $iLink, $iLinkType ){
		if( isset( $this->aFiles[$iLinkType] ) ){
			$aTypes = ( $iLinkType == 1 ) ? $GLOBALS['aFilePositionPage'] : $GLOBALS['aFilePositionProduct'];
			$oDB = DataBase::getInstance( );
			$sFileName = $this->throwDbNames( $iLinkType );
			$aFilesByTypes = $this->throwAllFilesByLink( $iLink, $iLinkType );
			$aFiles = Array( );
			if( is_array( $aFilesByTypes ) ) foreach( $aFilesByTypes as $aFilesByType ) $aFiles = array_merge( $aFiles, $aFilesByType );
			foreach( $aFiles as &$aFile ) $aFile['iLinkType'] = $iLinkType;
			return $aFiles;
		}
	}
	function throwAllFilesByLink( $iLink, $iLinkType ){
		if( isset( $this->aFiles[$iLinkType][$iLink] ) && isset( $this->aImages[$iLinkType][$iLink] ) ){
			return array_merge( $this->aImages[$iLinkType][$iLink], $this->aFiles[$iLinkType][$iLink] );
		} else{
			if( isset( $this->aImages[$iLinkType][$iLink] ) ) return $this->aImages[$iLinkType][$iLink];
			elseif( isset( $this->aFiles[$iLinkType][$iLink] ) ) return $this->aFiles[$iLinkType][$iLink];
		}
	}
	function deleteSelectedFiles( $aFiles, $iLinkType, $sIndex = 'iFile', $sFieldName = 'sFileName' ){
		if( isset( $aFiles ) && is_array( $aFiles ) ){
			$oDB = DataBase::getInstance( );
			$sFileName = $this->throwDbNames( $iLinkType );
			$this->deleteFiles( array_keys( $aFiles ), $iLinkType, $sIndex, $sFieldName, false );
			if( $iLinkType < 50 ) $oDB->deleteInFile( $sFileName, $aFiles, 'iFile' );
			elseif( $iLinkType == 50 ) $oDB->executeSQL( 'UPDATE `' . $oDB->throwTableName( $sFileName ) . '` SET `sLogoUrl` = NULL WHERE `iProducer` IN( \'' . implode( '\', \'', array_keys( $aFiles ) ) . '\' )' );
		}
	}
	function deleteFiles( $aData, $iLinkType, $sIndex, $sFieldName = 'sFileName', $bDeleteInDB = true ){
		$oDB = DataBase::getInstance( );
		$sFileName = $this->throwDbNames( $iLinkType );
		$aFilesNamesIn = Array( );
		foreach( $aData as $sFileNameIn ) $aFilesNamesIn[] = $oDB->escape( $sFileNameIn );
		$aLangs = glob( DIR_LANG . '??.php' );
		foreach( $aLangs as $iLang => $sLang ){
			$aLangs[$iLang] = preg_replace( '!^.*/([a-z]{2})\\.php$!', '$1', $sLang );
		}
		$sSubquery = 'SELECT `sFileName`, `iPhoto` FROM `' . $oDB->throwTableName( DB_PRODUCTS_FILES ) . '`
										UNION ALL SELECT `sFileName`, `iPhoto` FROM `' . $oDB->throwTableName( DB_PAGES_FILES ) . '`
										UNION ALL SELECT `sFileName`, `iPhoto` FROM `' . $oDB->throwTableName( DB_BOXES_FILES ) . '`
										UNION ALL SELECT `sLogoUrl` AS `sFileName`, 1 AS `iPhoto` FROM `' . $oDB->throwTableName( DB_PRODUCERS ) . '`
										UNION ALL SELECT `sFile` AS `sFileName`, 0 AS `iPhoto` FROM `' . $oDB->throwTableName( DB_BANNERS ) . '`';
		$aSubqueries = Array( );
		foreach( $aLangs as $sLang ){
			$aSubqueries[] = str_replace( '`' . MYSQL_TABLE_PREFIX . LANGUAGE . '_', '`' . MYSQL_TABLE_PREFIX . $sLang . '_', $sSubquery );
		}
		$sSubquery = implode( ' UNION ALL ', $aSubqueries );
		$sql = 'SELECT `sFileName`, `iPhoto`, COUNT( `sFileName` ) AS `iCount`
							FROM
								( SELECT `sFileName`, `iPhoto`
										FROM
											( ' . $sSubquery . ' ) AS `sq2`
										WHERE `sFileName` IN
											( SELECT `' . $sFieldName . '` AS `sFileName` FROM `' . $oDB->throwTableName( $sFileName ) . '`
													WHERE `' . $sIndex . '` IN ( \'' . implode( '\', \'', $aFilesNamesIn ) . '\' )
											)
								) AS `sq1`
							GROUP BY `sFileName`, `iPhoto`
							HAVING `iCount` <= 1';
		$aDelete = $oDB->executeSQL( $sql, true );
		if( $bDeleteInDB ) $oDB->deleteInFile( $sFileName, $aData, $sIndex );
		foreach( $aDelete as $aFile ){
			$this->deleteFilesFromDirs( $aFile['sFileName'], $aFile['iPhoto'] );
		}
	}
	function throwFilesInDir( $iStart = 0, $iLimit = null, $sPhrase = null ){
		$oDB = DataBase::getInstance( );
		$aFiles = glob( DIR_FILES . ( isset( $sPhrase ) ? ( '*' . $sPhrase . '*.*' ) : '*.*' ) );
		if( isset( $aFiles ) ){
			foreach( $aFiles as $iFile => &$sFileName ) if( !is_file( $sFileName ) ) unset( $aFiles[$iFile] );
			else $sFileName = str_replace( DIR_FILES, '', $sFileName );
			$iCount = count( $aFiles );
			if( isset( $iLimit ) ){
				$aReturn['aFiles'] = array_slice( $aFiles, $iStart, $iLimit );
				$aReturn['aPagesCountData'] = Array( 'iCount' => ceil( $iCount / $iLimit ), 'iPageNumber' => ( $iStart / $iLimit ) + 1 );
			}
			foreach( $aReturn['aFiles'] as &$mFile ){
				$mFile = Array( 'sFileName' => $mFile, 'iPhoto' => ( $oDB->checkCorrectFile( $mFile, 'gif|jpg|png|jpeg' ) == true ) ? 1 : 0 );
			}
			return $aReturn;
		}
	}
	function deleteFilesFromDirs( $sFileName, $iImage ){
		if( $iImage == 1 && isset( $this->aDirs ) ){
			foreach( $this->aDirs as $mDir => $bValue ){
				if( is_file( DIR_FILES . $mDir . '/' . $sFileName ) ) unlink( DIR_FILES . $mDir . '/' . $sFileName );
			}
		}
		if( is_file( DIR_FILES . $sFileName ) ) unlink( DIR_FILES . $sFileName );
	}
	function generateThumbDirs( ){
		$oDir = dir( DIR_FILES );
		while( false !== ( $mDir = $oDir->read( ) ) ){
			if( is_numeric( $mDir ) && is_dir( DIR_FILES . $mDir ) ){
				$this->aDirs[$mDir] = true;
			}
		}
		$oDir->close( );
	}
	function saveFiles( $aForm, $iLink, $iLinkType ){
		if( isset( $aForm['aFilesDescription'] ) && is_array( $aForm['aFilesDescription'] ) ){
			$aFiles = $this->throwAllFilesByLink( $iLink, $iLinkType );
			if( is_array( $aFiles ) ) foreach( $aFiles as $iType => $aInfo ){
				foreach( $aInfo as $aData ){
					$aForm['aFilesDescription'][$aData['iFile']] = trim( $aForm['aFilesDescription'][$aData['iFile']] );
					if( isset( $aForm['aFilesDescription'][$aData['iFile']] ) && $aForm['aFilesDescription'][$aData['iFile']] != $aData['sDescription'] ){
						$aChange[$aData['iFile']][0] = $aForm['aFilesDescription'][$aData['iFile']];
					}
					if( isset( $aForm['aFilesSizes'][$aData['iFile']] ) && $aForm['aFilesSizes'][$aData['iFile']] != $aData['iSize'] ){
						$aChange[$aData['iFile']][1] = ( $aForm['aFilesSizes'][$aData['iFile']] > 0 ) ? $aForm['aFilesSizes'][$aData['iFile']] : '';
					}
					if( $aForm['aFilesPositions'][$aData['iFile']] != $aData['iPosition'] ){
						$aChange[$aData['iFile']][2] = $aForm['aFilesPositions'][$aData['iFile']];
					}
					if( isset( $aForm['aFilesTypes'][$aData['iFile']] ) && $aForm['aFilesTypes'][$aData['iFile']] != $aData['iType'] ){
						$aChange[$aData['iFile']][3] = $aForm['aFilesTypes'][$aData['iFile']];
					}
					if( isset( $aForm['aFilesTextSearch'][$aData['iFile']] ) && $aForm['aFilesTextSearch'][$aData['iFile']] != $aData['sTextSearch'] ){
						$aChange[$aData['iFile']][4] = $aForm['aFilesTextSearch'][$aData['iFile']];
					}
				}
			}
			if( isset( $aChange ) ){
				$oDB = DataBase::getInstance( );
				$sFile = $this->throwDbNames( $iLinkType );
				foreach( $aChange as $iFile => $aData ){
					$aSave = Array( );
					if( isset( $aData[0] ) ) $aSave['sDescription'] = $aData[0];
					if( isset( $aData[1] ) ) $aSave['iSize'] = $aData[1];
					if( isset( $aData[2] ) ) $aSave['iPosition'] = $aData[2];
					if( isset( $aData[3] ) ) $aSave['iType'] = $aData[3];
					if( isset( $aData[4] ) ) $aSave['sTextSearch'] = $aData[4];
					$oDB->update( $sFile, $aSave, Array( 'iFile' => $iFile ) );
					if( isset( $aData[1] ) ) $this->generateThumbs( $oDB->executeSQL( 'SELECT `sFileName` FROM `' . $oDB->throwTableName( $sFile ) . '` WHERE `iFile` = ' . intval( $iFile ), true, DataBase::SINGLE_VALUE ), $aSave['iSize'] );
				}
			}
		}
	}
	function addFilesUploaded( $aForm, $iLink, $iLinkType, $sLinkName ){
		if( isset( $_FILES['aNewFiles']['name'] ) ){
			$iCount = count( $_FILES['aNewFiles']['name'] );
			$i2 = 0;
			$oDB = DataBase::getInstance( );
			$this->mData = null;
			for( $i = 0;$i < $iCount;$i++ ){
				if( !empty( $_FILES['aNewFiles']['name'][$i] ) && $oDB->checkCorrectFile( $_FILES['aNewFiles']['name'][$i], 'php|php2|php3|php4|php5|phtml|pwml|inc|asp|aspx|ascx|jsp|cfm|cfc|pl|bat|exe|com|dll|vbs|js|reg|cgi|htaccess|asis|sh|shtml|shtm|phtm' ) != true ){
					$this->mData[$i2]['iSize'] = ( $aForm['aNewFilesSizes'][$i] > 0 ) ? $aForm['aNewFilesSizes'][$i] : null;
					$this->mData[$i2]['iType'] = is_numeric( $aForm['aNewFilesTypes'][$i] ) ? $aForm['aNewFilesTypes'][$i] : 1;
					$this->mData[$i2]['iPosition'] = is_numeric( $aForm['aNewFilesPositions'][$i] ) ? $aForm['aNewFilesPositions'][$i] : 0;
					$this->mData[$i2]['sDescription'] = changeTxt( $aForm['aNewFilesDescriptions'][$i], '' );
					$this->mData[$i2]['sFileName'] = $oDB->uploadFile( Array( 'tmp_name' => $_FILES['aNewFiles']['tmp_name'][$i], 'name' => $_FILES['aNewFiles']['name'][$i] ), DIR_FILES );
					$this->mData[$i2][$sLinkName] = $iLink;
					$i2++;
				}
			}
			if( isset( $this->mData ) ) $this->addFiles( $iLinkType );
		}
	}
	function addFilesUploadedMulti( /*$aForm,*/ $iLink, $iLinkType, $sLinkName ){
		if( isset( $_FILES['aNewFiles']['name'] ) ){
			$iCount = count( $_FILES['aNewFiles']['name'] );
			$i2 = 0;
			$oDB = DataBase::getInstance( );
			$this->mData = null;
			for( $i = 0;$i < $iCount;$i++ ){
				if( !empty( $_FILES['aNewFiles']['name'][$i] ) && $oDB->checkCorrectFile( $_FILES['aNewFiles']['name'][$i], 'php|php2|php3|php4|php5|phtml|pwml|inc|asp|aspx|ascx|jsp|cfm|cfc|pl|bat|exe|com|dll|vbs|js|reg|cgi|htaccess|asis|sh|shtml|shtm|phtm' ) != true ){
					$this->mData[$i2]['iSize'] = null;
					$this->mData[$i2]['iType'] = 1;
					$this->mData[$i2]['iPosition'] = 0;
					$this->mData[$i2]['sDescription'] = '';
					$this->mData[$i2]['sFileName'] = $oDB->uploadFile( Array( 'tmp_name' => $_FILES['aNewFiles']['tmp_name'][$i], 'name' => $_FILES['aNewFiles']['name'][$i] ), DIR_FILES );
					$this->mData[$i2][$sLinkName] = $iLink;
					$i2++;
				}
			}
			if( isset( $this->mData ) ) $this->addFiles( $iLinkType, 0 );
		}
		if( isset( $_FILES['aNewImages']['name'] ) ){
			$iCount = count( $_FILES['aNewImages']['name'] );
			$i2 = 0;
			$oDB = DataBase::getInstance( );
			$oFoto = FotoJobs::getInstance( );
			$this->mData = null;
			$watermark = Array();
			for( $i = 0;$i < $iCount;$i++ ){
				if( !empty( $_FILES['aNewImages']['name'][$i] ) && $oDB->checkCorrectFile( $_FILES['aNewImages']['name'][$i], 'gif|jpg|png|jpeg' ) == true ){
					$this->mData[$i2]['iSize'] = null;
					$this->mData[$i2]['iType'] = $iLinkType == 2 ? 7 : 5;
					$this->mData[$i2]['iPosition'] = 0;
					$this->mData[$i2]['sDescription'] = '';
					$this->mData[$i2]['sFileName'] = $oDB->uploadFile( Array( 'tmp_name' => $_FILES['aNewImages']['tmp_name'][$i], 'name' => $_FILES['aNewImages']['name'][$i] ), DIR_FILES );
					
					$watermark[] = $this->mData[$i2]['sFileName'];
					
					$this->mData[$i2][$sLinkName] = $iLink;
					$i2++;
				}
			}
			if( isset( $this->mData ) ) $this->addFiles( $iLinkType, 1 );
			if( $_POST['iWatermark'] ){
				foreach( $watermark as $sFileName ){
					$oFoto->addWatermark( 'upload/'.$sFileName, 'templates/images/watermark.png', $GLOBALS['config']['sWatermarkPos'], null );
				}
			}
		}
	}
	function addFilesFromServer( $aForm, $iLink, $iLinkType, $sLinkName ){
		if( isset( $aForm['aDirFiles'] ) ){
			$i = 0;
			$oDB = DataBase::getInstance( );
			$this->mData = null;
			foreach( $aForm['aDirFiles'] as $iKey => $sFile ){
				if( is_file( DIR_FILES . $sFile ) ){
					$this->mData[$i]['sFileName'] = $sFile;
					$this->mData[$i]['iSize'] = 100;
					$this->mData[$i]['iType'] = 1;
					$this->mData[$i]['iPosition'] = 0;
					$this->mData[$i]['sDescription'] = '';
					$this->mData[$i][$sLinkName] = $iLink;
					$i++;
				}
			}
			if( isset( $this->mData ) ) $this->addFiles( $iLinkType );
			$this->mData = null;
		}
	}
	function addFilesFromServerFull( $aForm, $iLink, $iLinkType, $sLinkName ){
		if( isset( $aForm['aDirFilesFull'] ) ){
			$i = 0;
			$oDB = DataBase::getInstance( );
			$this->mData = null;
			foreach( $aForm['aDirFilesFull'] as $iKey => $aFile ){
				if( is_file( DIR_FILES . $aFile['sFileName'] ) ){
					$this->mData[$i]['sFileName'] = $aFile['sFileName'];
					$this->mData[$i]['iSize'] = isset( $aFile['iSize'] ) ? $aFile['iSize'] : 100;
					$this->mData[$i]['iType'] = isset( $aFile['iType'] ) ? $aFile['iType'] : 1;
					$this->mData[$i]['iPosition'] = isset( $aFile['iPosition'] ) ? $aFile['iPosition'] : 0;
					$this->mData[$i]['sDescription'] = isset( $aFile['sDescription'] ) ? $aFile['sDescription'] : '';
					$this->mData[$i][$sLinkName] = $iLink;
					$i++;
				}
			}
			if( isset( $this->mData ) ) $this->addFiles( $iLinkType );
			$this->mData = null;
		}
	}
	function addFiles( $iLinkType, $iPhoto = null ){
		if( isset( $this->mData ) && is_array( $this->mData ) ){
			$oDB = DataBase::getInstance( );
			$sFile = $this->throwDbNames( $iLinkType );
			foreach( $this->mData as $aData ){
				$aData['iPhoto'] = isset( $iPhoto ) ? $iPhoto : ( $oDB->checkCorrectFile( $aData['sFileName'], 'gif|jpg|png|jpeg' ) == true ) ? 1 : 0;
				if( $aData['iPhoto'] == 1 ){
					$this->generateThumbs( $aData['sFileName'], $aData['iSize'] );
				} else{
					$aData['iSize'] = null;
				}
				$aData['iFile'] = null;
				$oDB->save( $sFile, $aData );
			}
			$this->mData = null;
		}
	}
	function generateThumbs( $sFileName, $iSize = 0 ){
		$oFoto = & FotoJobs::getInstance( );
		$aImgSize = $oFoto->throwImgSize( DIR_FILES . $sFileName );
		if( defined( 'MAX_DIMENSION_OF_IMAGE' ) && ( $aImgSize['width'] > MAX_DIMENSION_OF_IMAGE || $aImgSize['height'] > MAX_DIMENSION_OF_IMAGE ) ){
			if( $aImgSize['width'] < $oFoto->iMaxForThumbSize && $aImgSize['height'] < $oFoto->iMaxForThumbSize ){
				$oFoto->setThumbSize( 'l' . MAX_DIMENSION_OF_IMAGE );
				$oFoto->createThumb( DIR_FILES . $sFileName, DIR_FILES, $sFileName );
			}
		}
		if( $iSize == 0 ) $aSizes = flat_array( $GLOBALS['aProductMinSizes'], $GLOBALS['aPageMinSizes'], $GLOBALS['aProducersMinSizes'] );
		else $aSizes = flat_array( $iSize, $GLOBALS['aProductMinSizes'], $GLOBALS['aPageMinSizes'], $GLOBALS['aProducersMinSizes'] );
		sort( $aSizes, SORT_NUMERIC );
		foreach( $aSizes as $iSize ){
			$sThumbsDir = DIR_FILES . $iSize . '/';
			if( !is_dir( $sThumbsDir ) ) mkdir( $sThumbsDir );
			if( !is_file( $sThumbsDir . $sFileName ) ) $oFoto->createCustomThumb( DIR_FILES . $sFileName, $sThumbsDir, $iSize, $sFileName, true );
		}
	}
}
?>