Index: branches/5.1.x/core/units/images/image_event_handler.php =================================================================== diff -u -N -r12127 -r12657 --- branches/5.1.x/core/units/images/image_event_handler.php (.../image_event_handler.php) (revision 12127) +++ branches/5.1.x/core/units/images/image_event_handler.php (.../image_event_handler.php) (revision 12657) @@ -1,6 +1,6 @@ Array ('subitem' => true), + 'OnCleanResizedImages' => Array ('subitem' => true), + ); + + $this->permMapping = array_merge($this->permMapping, $permissions); + } + + function mapEvents() { parent::mapEvents(); // ensure auto-adding of approve/decine and so on events @@ -357,6 +373,117 @@ $object->addFilter('includes_filter_h', $includes_or_filter_h, HAVING_FILTER); $object->addFilter('excepts_filter_h', $excepts_and_filter_h, HAVING_FILTER); } -} -?> \ No newline at end of file + /** + * [AGENT] Remove unused images from "/system/images" and "/system/images/pending" folders + * + * @param kEvent $event + */ + function OnCleanImages(&$event) + { + // 1. get images, that are currently in use + $active_images = $this->_getActiveImages( $this->Application->getUnitOption('img', 'TableName') ); + $active_images[] = 'noimage.gif'; + + // 2. get images on disk + $this->_deleteUnusedImages(FULL_PATH . IMAGES_PATH, $active_images); + + // 3. get images in use from "images/pending" folder + $active_images = $this->_getPendingImages(); + + // 4. get image on disk + $this->_deleteUnusedImages(FULL_PATH . IMAGES_PENDING_PATH, $active_images); + } + + /** + * Gets image filenames (no path) from given table + * + * @param string $image_table + * @return Array + */ + function _getActiveImages($image_table) + { + $sql = 'SELECT LocalPath, ThumbPath + FROM ' . $image_table . ' + WHERE COALESCE(LocalPath, "") <> "" OR COALESCE(ThumbPath) <> ""'; + $images = $this->Conn->Query($sql); + + $active_images = Array (); + foreach ($images as $image) { + if ($image['LocalPath']) { + $active_images[] = basename($image['LocalPath']); + } + + if ($image['ThumbPath']) { + $active_images[] = basename($image['ThumbPath']); + } + } + + return $active_images; + } + + /** + * Gets active images, that are currently beeing edited inside temporary tables + * + * @return Array + */ + function _getPendingImages() + { + $tables = $this->Conn->GetCol('SHOW TABLES'); + $mask_edit_table = '/'.TABLE_PREFIX.'ses_(.*)_edit_' . TABLE_PREFIX . 'Images/'; + + $active_images = Array (); + + foreach ($tables as $table) { + if (!preg_match($mask_edit_table, $table)) { + continue; + } + + $active_images = array_unique( array_merge($active_images, $this->_getActiveImages($table)) ); + } + + return $active_images; + } + + /** + * Deletes all files in given path, except of given $active_images + * + * @param string $path + * @param Array $active_images + */ + function _deleteUnusedImages($path, &$active_images) + { + $images = glob($path . '*.*'); + if ($images) { + $images = array_map('basename', $images); + + // delete images, that are on disk, but are not mentioned in Images table + $delete_images = array_diff($images, $active_images); + foreach ($delete_images as $delete_image) { + unlink($path . $delete_image); + } + } + } + + /** + * [AGENT] Remove all images from "/system/images/resized" and "/system/images/pending/resized" folders + * + * @param kEvent $event + */ + function OnCleanResizedImages(&$event) + { + $images = glob(FULL_PATH . IMAGES_PATH . 'resized/*.*'); + if ($images) { + foreach ($images as $image) { + unlink($image); + } + } + + $images = glob(FULL_PATH . IMAGES_PENDING_PATH . 'resized/*.*'); + if ($images) { + foreach ($images as $image) { + unlink($image); + } + } + } +} \ No newline at end of file