checkForCookieAgreement()) { $this->getAgreementForm(); exit(); } $sblTableExist = setupDeletePicturesTable::tableExist(); $sblTableHasItems = false; $aData = $this->getLostPictureGroupByFolder(); if(count($aData)) { $sblTableHasItems = true; } $sOutput = $this->getHtmlHeader(); $sOutput .= '

Bilder ohne Artikelzuordnung suchen und löschen

'; if($sblTableExist == true) { if(trim($this->sOutPutMessage) != '') { $sOutput .= '

'.$this->sOutPutTitle.'

'.$this->sOutPutMessage.'
'; } $sOutput.= '

Statistik

eingelesene Bilder aufgeteilt auf die Ordner in '.$this->sFolder.':
'.$this->getLostPictureGroupByFolderAsHtml().'
'; } if($sblTableExist == true) { $sOutput .= '

Bilder prüfen

Was geschieht hier:

In den Unterverzeichnissen in '.$this->sFolder.' werden die enthalten Dateien ausgelesen. Bei diesem Vorgang wird sofort geprüft welche Dateien in der Tabelle oxarticles nicht mehr enthalten sind. Ist ein Bild nicht mehr an einem Artikel hinterlegt, dann erfolgt die Abspeicherung in der Tabelle d3lostpictures.

Die Prüfung erfolgt pro Bildslot und nicht global auf alle Verzeichnisse und Bildfelder in der Tabelle oxarticles.
Dies bedeuted: z.B. die Bilder im Ordner "'.$this->sFolder.'1" werden nur gegen das Feld oxpic1 geprüft und nicht gegen oxpic2 oder oxthumb.

'; } if($sblTableExist == true) { $sOutput.= '

Bilder anzeigen (Begrenzt auf '.$this->iLimitPicture.')

'.$this->getLostPicturesDisplayButtonsAsHtml().'

Als CSV-Datei ausgeben

'.$this->getLostPicturesAsCsvFile().'

Bilder löschen('.$this->iLimitPictureDelete.' Stk pro Durchgang)

'.$this->getLostPicturesDeleteButtonsAsHtml().'
'; } $sOutput .= '
'; if($sblTableExist == false) { $sOutput .= '

Datenbank

Die Bilder ohne Zuordnung werden in dieser Tabelle d3lostpictures abgelegt.
Im Anschluß nach dem löschen der Bilder kann diese Tabelle und auch dieses Script mit einem Mausklick entfernt werden.
'; } $sOutput .= '

Datenbank

'; if($sblTableHasItems == true) { $sOutput .='
'; } if($sblTableExist == true) { $sOutput .= '
'; } $sOutput .= '
'; $sOutput .= '
'; $sOutput .= '

Ablauf / Legende

  1. Datenbanktabelle d3lostpictures anlegen
  2. Bilder einlesen (in Tabelle d3lostpictures)
  3. Bilder löschen (als Datei und Eintrag in d3lostpictures)
  4. Datenbanktabelle d3lostpictures löschen
  5. Script vom Server entfernen
'; $sOutput.=$this->getHtmlFooter(); echo $sOutput; } /** * @return string|void */ public function getAgreementForm() { $oRegistry = oxNew(Registry::class); $sPath = $_SERVER["ORIG_PATH_INFO"]?$_SERVER["ORIG_PATH_INFO"]:$_SERVER['REQUEST_URI']; $sPath = basename($sPath); $sOutput = $this->getHtmlHeader(); $sOutput .='

deletePictures - Read IT!

  • Legen Sie ein Backup der Bilder bzw. des kompletten Shops an. - EN: - Make a Backup
  • Installieren Sie das Script zuerst in einem Testshop und führen dort einen Testlauf durch. - EN: first run in a Testshop
  • Entfernen Sie das Script vom Server nach dem löschen der Bilder. - EN: remove Script after use
- EN: I read it and use it on my own risk.

'; $sOutput .= $this->getHtmlFooter(); echo $sOutput; } /** * @return bool */ public function checkForCookieAgreement() { if(isset($_COOKIE[$this->sCookieName]) && $_COOKIE[$this->sCookieName] == true #&& #isset($_COOKIE[$this->sAdminCookieSid]) && $_COOKIE[$this->sAdminCookieSid] != '' ) { return false; } return true; } public function setAgreement() { $oRegistry = oxNew(Registry::class); $sPath = $_SERVER["ORIG_PATH_INFO"]?$_SERVER["ORIG_PATH_INFO"]:$_SERVER['REQUEST_URI']; $sPath = basename($sPath); //action=setAgreement - entfernen $aUrl = parse_url($oRegistry::getConfig()->getSslShopUrl().$sPath); $sPath = ltrim($aUrl['path'],'/'); if($oRegistry::getConfig()->getRequestParameter('agreement') == true) { setcookie($this->sCookieName, true, strtotime( '+7 days' )); } $oRegistry::getUtils()->redirect($oRegistry::getConfig()->getSslShopUrl().$sPath,false); } public function removeAgreement() { $oRegistry = oxNew(Registry::class); $sPath = $_SERVER["ORIG_PATH_INFO"]?$_SERVER["ORIG_PATH_INFO"]:$_SERVER['REQUEST_URI']; $sPath = basename($sPath); //action=removeAgreement - entfernen $aUrl = parse_url($oRegistry::getConfig()->getSslShopUrl().$sPath); $sPath = ltrim($aUrl['path'],'/'); setcookie($this->sCookieName, false, time()-1000); $oRegistry::getUtils()->redirect($oRegistry::getConfig()->getSslShopUrl().$sPath,false); } /** * @return string */ public function getHtmlFooter() { return ' '; } /** * @return string */ public function getHtmlHeader() { return ' deletePictures - nicht mehr genutzte Bilder löschen '; } /** * todo: Prüfung af unvollständig eingelesenen Ordner */ public function getFolderContents() { //Tabelle leeren //$this->truncateTable(); $aSlotsFromLostPictures = $this->getLostPicturesSlotsFromTable(); $sFolder = $this->getPathToShop().$this->sFolder; $aFolder = $this->getFolders(); natsort($aFolder); foreach ($aFolder as $sTmpFolder) { $this->sOutPutTitle = 'Ordner einlesen'; //echo "
Folder:".$sTmpFolder; if($this->hasFinishedImport($sTmpFolder) == false) { //Einträge pro Slot loeschen $this->truncateTable($sTmpFolder); } if(key_exists($sTmpFolder,$aSlotsFromLostPictures)) { continue; } $this->sOutPutMessage.= "
Prüfe Ordner: ".$sFolder.$sTmpFolder; if ($handle = opendir($sFolder.$sTmpFolder)) { $aFilesOxPics = $this->getAllPicturesFromTable($sTmpFolder); $this->setStartStopFlag($sTmpFolder,'START'); while (false !== ($entry = readdir($handle))) { if ($entry == "." || $entry == ".." || $entry == "dir.txt" || trim($entry) == '') { continue; } if(isset($aFilesOxPics[$entry])) { continue; } $aPictureData = array(); $aTmpImagePro = getimagesize ($sFolder.$sTmpFolder."/".$entry); $aPictureData['D3FOLDER'] = $sTmpFolder; $aPictureData['D3WIDTH'] = $aTmpImagePro[0]; $aPictureData['D3HEIGHT'] = $aTmpImagePro[1]; $aPictureData['D3IMAGETYPE'] = $aTmpImagePro['mime']; $aPictureData['D3LASTCHANGE'] = date ("Y-m-d H:i:s",filemtime($sFolder.$sTmpFolder."/".$entry)) ; $aPictureData['D3FILESIZE'] = filesize ($sFolder.$sTmpFolder."/".$entry) ; $aPictureData['D3FILETYPE'] = 'product'; $aPictureData['D3FILENAME'] = $entry; $aPictureData['D3DATE'] = date ("Y-m-d H:i:s"); $aPictureData['OXID'] = md5($entry.$sTmpFolder.date ("Y-m-d H:i:s")); $oItem = oxNew(BaseModel::class); $oItem->init("d3lostpictures"); $oItem->assign($aPictureData); $oItem->save(); } //die(); $this->setStartStopFlag($sTmpFolder,'STOP'); //Flags entfernen $this->deleteStartStopFlag($sTmpFolder); closedir($handle); } else{ $this->sOutPutMessage.= "
Konnte Ordner nicht oeffnen:".$sFolder.$sTmpFolder; } $this->iCount = 0; } } /** * @return array */ public function getLostPicturesSlotsFromTable() { $oDatabaseProvider = oxNew(DatabaseProvider::class); $oDb = $oDatabaseProvider::getDb($oDatabaseProvider::FETCH_MODE_ASSOC); $sQuery = <<getAll($sQuery); $aTmp = array(); foreach($sRs as $aSlot) { $aTmp[$aSlot['d3folder']] = $aSlot['d3folder']; } return $aTmp; } /** * @return array */ public function getFolders() { $sFolder = $this->getPathToShop() . $this->sFolder; $aFolders = array(); if ($handle = opendir($sFolder)) { while (false !== ($entry = readdir($handle))) { $this->iCount++; if ($entry != "." && $entry != "..") { if(is_dir($sFolder.$entry)) { $aFolders[] = $entry; } } } } return $aFolders; } /** * @param $sSlot * * @return array */ public function getAllPicturesFromTable($sSlot) { $oDatabaseProvider = oxNew(DatabaseProvider::class); //echo $sSlot; $sPictureDbField = 'oxpic'.$sSlot; if($sSlot == 'thumb' || $sSlot == 'icon'){ $sPictureDbField = 'ox'.$sSlot; } $oDb = $oDatabaseProvider::getDb($oDatabaseProvider::FETCH_MODE_ASSOC); $sQuery = <<getAll($sQuery); $aTmp = array(); foreach($aDbResult as $aPicture) { $aTmp[$aPicture[$sPictureDbField]] = $aPicture[$sPictureDbField]; } return $aTmp; } /** * @return string */ public function getPathToShop() { $oRegistry = oxNew(Registry::class); return $oRegistry::getConfig()->getConfigParam('sShopDir'); } /** select * from d3lostpictures where d3folder = '1' AND d3filename not in( select oxpic1 from oxarticles where oxpic1 != '' ); select * from d3lostpictures where d3folder = '2' AND d3filename not in( select oxpic2 from oxarticles where oxpic2 != '' ); */ /** * @param $sSlot */ public function getLostPicturesSlot($sSlot) { $this->sOutPutMessage.= $this->getLostPictureAsTableContent($this->getLostPictures($sSlot)); } /** * @param $aPictures * @param $sSlot * * @return string */ public function getLostPictureAsTableContent($aPictures) { $oRegistry = oxNew(Registry::class); $this->sOutPutTitle = 'Anzeige Bilder'; //dumpvar($aPictures); $sUrlToFolder = $oRegistry::getConfig()->getShopUrl().$this->sFolder; $sContent = ''; foreach ($aPictures as $aPicture) { //$sFileName = $aPicture['D3FILENAME'].'.'.$aPicture['D3FILETYPE']; $sFileName = $aPicture['D3FILENAME']; $sPathWithPicture = $sUrlToFolder.$aPicture['D3FOLDER'].'/'.$sFileName; $sContent .= ' '; } $sContent .= '
Bild Pfad + Bildname Breite * Höhe Dateigrösse letzte Ändeurung
'.$sPathWithPicture.' '.$this->sFolder.$aPicture['D3FOLDER'].'/'.$sFileName.' '.$aPicture['D3WIDTH'].'px * '.$aPicture['D3HEIGHT'].'px '.$this->formatBytes($aPicture['D3FILESIZE']).' '.$aPicture['D3LASTCHANGE'].'
'; return $sContent; } /** * @param $sSlot */ public function getLostPicturesInCsvFile($sSlot) { $oRegistry = oxNew(Registry::class); //$sUrlToFolder = oxRegistry::getConfig()->getShopUrl().$this->sFolder; $sSeparator = '"'; $sColumn = ";".PHP_EOL; $sSeparator = ''; $sColumn = PHP_EOL; $sFileContent = ''; foreach ($this->getLostPictures($sSlot) as $aPicture) { $sFileName = $aPicture['D3FILENAME']; $sFileContent .= $sSeparator.$this->sFolder.$aPicture['D3FOLDER'].'/'.$sFileName.$sSeparator.$sColumn; } $oUtils = $oRegistry::getUtils(); $sFilename = 'Folder_'.$sSlot.'.csv'; ob_start(); //$sPDF = ob_get_contents(); $sCsv = $sFileContent; ob_end_clean(); $oUtils->setHeader("Pragma: public"); $oUtils->setHeader("Cache-Control: must-revalidate, post-check=0, pre-check=0"); $oUtils->setHeader("Expires: 0"); $oUtils->setHeader("Content-type: application/csv"); $oUtils->setHeader("Content-Disposition: attachment; filename=" . $sFilename); $oRegistry::getUtils()->showMessageAndExit($sCsv); } /** * @param $sSlot * * @return mixed */ public function getLostPictures($sSlot) { $oDatabaseProvider = oxNew(DatabaseProvider::class); $oDb = $oDatabaseProvider::getDb($oDatabaseProvider::FETCH_MODE_ASSOC); /* $sPictureDbField = 'oxpic'.$sSlot; if($sSlot == 'thumb' || $sSlot == 'icon'){ $sPictureDbField = 'ox'.$sSlot; }*/ $iCount = $this->getCountLostPictures($sSlot); $sLimit = ''; if($iCount > $this->iLimitPicture) { $sLimit = ' LIMIT '.$this->iLimitPicture; } $sQuery = <<quote($sSlot)} AND D3FILENAME NOT IN('START', 'STOP') ORDER BY D3lASTCHANGE {$sLimit} MYSQL; //echo $sQuery; //die(); $res = $oDb->getAll($sQuery); return $res; } /** * @param $sSlot * * @return false|string */ public function getCountLostPictures($sSlot) { $oDatabaseProvider = oxNew(DatabaseProvider::class); $oDb = $oDatabaseProvider::getDb($oDatabaseProvider::FETCH_MODE_ASSOC); $sQuery = <<getOne($sQuery); } /** * @return array */ public function getLostPictureGroupByFolder() { if($this->tableExist() == false) { return array(); } $oDatabaseProvider = oxNew(DatabaseProvider::class); $oDb = $oDatabaseProvider::getDb($oDatabaseProvider::FETCH_MODE_ASSOC); $sQuery = <<getAll($sQuery); $aTmp = array(); foreach ($aRes as $aFolder) { $aTmp[$aFolder['d3folder']] = $aFolder; } return $aTmp; } /** * @param $sSlot * * @return false|string */ public function getCountLostPictureGroupByFolder($sSlot) { $sPictureDbField = 'oxpic'.$sSlot; if($sSlot == 'thumb' || $sSlot == 'icon'){ $sPictureDbField = 'ox'.$sSlot; } $oDatabaseProvider = oxNew(DatabaseProvider::class); $oDb = $oDatabaseProvider::getDb($oDatabaseProvider::FETCH_MODE_ASSOC); $sQuery = <<getOne($sQuery); } /** * @return string */ public function getLostPictureGroupByFolderAsHtml() { $aData = $this->getLostPictureGroupByFolder(); $aDataUnFinishedFolders = $this->checkAllSlotsIfFinished(); if(count($aData) == 0) { $sHtml= ' Keine Bilder gefunden oder eingelesen. '; return $sHtml; } //dumpvar($aData); $sHtml= ''; foreach ($aData as $aFiles) { $sHtml.=''; } $sHtml.= ''; foreach ($aData as $aFiles) { $sHtml.=''; } $sHtml.= ''; foreach ($aData as $aFiles) { $sHtml.=''; } $sHtml.= ''; foreach ($aData as $sKey => $aFiles) { $sHtml.=''; } $sHtml.= ''; $sHtml.= '
Slot/Ordner: '.$aFiles['d3folder'].'
Anzahl Dateien: '.$aFiles['files'].'
Speicherplatz: '.$this->formatBytes($aFiles['size']).'
Status Ordner: '; if(isset($aDataUnFinishedFolders[$sKey])){ $sHtml.= 'Abbruch'; } else { $sHtml.= 'OK'; } $sHtml.='
'; return $sHtml; } /** * * https://stackoverflow.com/questions/2510434/format-bytes-to-kilobytes-megabytes-gigabytes * @param $size * @param int $precision * * @return string */ public function formatBytes($size) { $mod = 1024; $units = explode(' ','B KB MB GB TB PB'); for ($i = 0; $size > $mod; $i++) { $size /= $mod; } return round($size, 2) . ' ' . $units[$i]; } /** * @return string */ public function getLostPicturesDeleteButtonsAsHtml() { $aData = $this->getLostPictureGroupByFolder(); if(count($aData) == 0) { $sHtml= '
Keine Bilder gefunden oder eingelesen.
'; return $sHtml; } $sHtml = '
'; //dumpvar($aData); foreach ($aData as $aSlot) { $sSlot = $aSlot['d3folder']; if($aSlot['d3folder'] == 'icon' && $aSlot['d3folder'] == 'thumb') { $sSlot = 'Pic'.$aSlot['d3folder']; } $sHtml.='
' ; } $sHtml.= '
'; return $sHtml; } /** * @return string */ public function getLostPicturesAsCsvFile() { $aData = $this->getLostPictureGroupByFolder(); if(count($aData) == 0) { $sHtml= '
Keine Bilder gefunden oder eingelesen.
'; return $sHtml; } $sHtml = '
'; foreach ($aData as $aSlot) { $sSlot = $aSlot['d3folder']; if($aSlot['d3folder'] == 'icon' && $aSlot['d3folder'] == 'thumb') { $sSlot = 'Pic'.$aSlot['d3folder']; } $sHtml.='
' ; } $sHtml.= '
'; return $sHtml; } /** * @return string */ public function getLostPicturesDisplayButtonsAsHtml() { $aData = $this->getLostPictureGroupByFolder(); if(count($aData) == 0) { $sHtml= '

Keine Bilder gefunden oder eingelesen.

'; return $sHtml; } $sHtml = '
'; foreach ($aData as $aSlot) { //$iCount = $this->getCountLostPictures($aSlot['d3folder']); $sSlot = $aSlot['d3folder']; if($aSlot['d3folder'] == 'icon' && $aSlot['d3folder'] == 'thumb') { $sSlot = 'Pic'.$aSlot['d3folder']; } $sHtml .= ' '; } $sHtml.= '
Ausgabe der Bilder in einer tabelarischen Form.
'; return $sHtml; } /** * deletePictures constructor. * * @param $sSlot * * @return string */ public function deleteLostPictures($sSlot) { $oDatabaseProvider = oxNew(DatabaseProvider::class); $oDb = $oDatabaseProvider::getDb($oDatabaseProvider::FETCH_MODE_ASSOC); $sQuery = <<quote($sSlot)} LIMIT {$this->iLimitPictureDelete}; MYSQL; $sRes = $oDb->getAll($sQuery); $sMessage = ''; $this->sOutPutTitle = 'gelöschte Bilder'; foreach($sRes As $aFile) { $blDelete = $this->_removePictureFromFolder($aFile['File'],$sSlot); if($blDelete == true) { $sQueryDelete = <<execute($sQueryDelete); } } return $sMessage; } /** * @param $sSlot * @param $sType */ public function setStartStopFlag($sSlot,$sType) { $aPictureData = array(); $aPictureData['D3FOLDER'] = $sSlot; $aPictureData['D3FILETYPE'] = ''; $aPictureData['D3WIDTH'] = ''; $aPictureData['D3HEIGHT'] = ''; $aPictureData['D3IMAGETYPE'] = ''; $aPictureData['D3LASTCHANGE'] = date ("Y-m-d H:i:s") ; $aPictureData['D3FILESIZE'] = 0; $aPictureData['D3FILETYPE'] = ''; $aPictureData['D3FILENAME'] = $sType; $aPictureData['D3DATE'] = date ("Y-m-d H:i:s"); $aPictureData['OXID'] = md5($sType.$sSlot.date ("Y-m-d H:i:s")); $oItem = oxNew(BaseModel::class); $oItem->init("d3lostpictures"); $oItem->assign($aPictureData); $oItem->save(); } /** * @param $sSlot * * @return object */ public function deleteStartStopFlag($sSlot) { $oDatabaseProvider = oxNew(DatabaseProvider::class); $oDatabaseProvider = oxNew(DatabaseProvider::class); $oDb = $oDatabaseProvider::getDb($oDatabaseProvider::FETCH_MODE_ASSOC); $sQuery = <<execute($sQuery); } /** * @param $sSlot * * @return false|string */ public function hasFinishedImport($sSlot) { $oDatabaseProvider = oxNew(DatabaseProvider::class); $oDb = $oDatabaseProvider::getDb($oDatabaseProvider::FETCH_MODE_ASSOC); $sQuery = <<getOne($sQuery); if($iRes == 0 || $iRes == 2) { return true; } return false; } /** * @return array */ public function checkAllSlotsIfFinished() { $oDatabaseProvider = oxNew(DatabaseProvider::class); $oDb = $oDatabaseProvider::getDb($oDatabaseProvider::FETCH_MODE_ASSOC); $sQuery = <<getAll($sQuery); $aTmp = array(); foreach ($aRes as $aFolder) { $aTmp[$aFolder['d3folder']] = $aFolder; } return $aTmp; } /** * @param $sFilePath * @param $sSlot * * @return bool */ protected function _removePictureFromFolder($sFilePath,$sSlot) { $sPathToFile = $this->getPathToShop().$this->sFolder.$sSlot.'/'.$sFilePath; $this->sOutPutMessage.= "
".$this->sFolder.$sSlot.'/'.$sFilePath; if(file_exists($sPathToFile) == false) { $this->sOutPutMessage.= ' - File not found'; return false; } if(unlink($sPathToFile) == false) { $this->sOutPutMessage.= ' - File could not delete'; return false; } return true; } public function createTable() { return setupDeletePicturesTable::createTable(); } public function dropTable() { if($this->tableExist() == true) { return setupDeletePicturesTable::dropTable(); } return false; } /** * @param $sSlot * * @return bool|void */ public function truncateTable($sSlot) { if($this->tableExist() == true) { setupDeletePicturesTable::truncateTable($sSlot); } } /** * @return bool */ public function tableExist() { return setupDeletePicturesTable::tableExist(); } public function deleteScript() { unlink($_SERVER['SCRIPT_FILENAME']); if (is_file($_SERVER['SCRIPT_FILENAME'])) { exit('Script konnte nicht gelöscht werden.'); } else { exit('Script wurde gelöscht'); } } } /** * Class setupDeletePicturesTable */ class setupDeletePicturesTable extends Base { public $_sTable = 'd3lostpictures'; /** * @throws oxConnectionException */ public static function createTable() { $oDatabaseProvider = oxNew(DatabaseProvider::class); $oDb = $oDatabaseProvider::getDb($oDatabaseProvider::FETCH_MODE_ASSOC); $sQuery = <<execute($sQuery); } /** * @throws oxConnectionException */ public static function dropTable() { $oDatabaseProvider = oxNew(DatabaseProvider::class); $oDb = $oDatabaseProvider::getDb($oDatabaseProvider::FETCH_MODE_ASSOC); $sQuery = <<execute($sQuery); } /** * @param $sSlot * * @throws oxConnectionException */ public static function truncateTable($sSlot) { $oDatabaseProvider = oxNew(DatabaseProvider::class); $oDb = $oDatabaseProvider::getDb($oDatabaseProvider::FETCH_MODE_ASSOC); $sQuery = <<execute($sQuery); } /** * @return bool * @throws oxConnectionException */ public static function tableExist() { $oDatabaseProvider = oxNew(DatabaseProvider::class); $oDb = $oDatabaseProvider::getDb($oDatabaseProvider::FETCH_MODE_ASSOC); $sQuery = <<getOne($sQuery); } } /** @var deletePictures $oBilderLesen */ $oDeletePictures = oxNew('deletePictures'); $oRegistry = oxNew(Registry::class); $sAction = $oRegistry::getConfig()->getRequestParameter('action'); $sParameter = $oRegistry::getConfig()->getRequestParameter('parameter'); if(trim($sAction) != '') { $oDeletePictures->{$sAction}($sParameter); } $oDeletePictures->render();