Index: branches/5.0.x/core/units/images/image_event_handler.php =================================================================== diff -u -r12117 -r12167 --- branches/5.0.x/core/units/images/image_event_handler.php (.../image_event_handler.php) (revision 12117) +++ branches/5.0.x/core/units/images/image_event_handler.php (.../image_event_handler.php) (revision 12167) @@ -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 +370,119 @@ $object->addFilter('includes_filter_h', $includes_or_filter_h, HAVING_FILTER); $object->addFilter('excepts_filter_h', $excepts_and_filter_h, HAVING_FILTER); } + + /** + * [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 Index: branches/5.0.x/core/kernel/startup.php =================================================================== diff -u -r12117 -r12167 --- branches/5.0.x/core/kernel/startup.php (.../startup.php) (revision 12117) +++ branches/5.0.x/core/kernel/startup.php (.../startup.php) (revision 12167) @@ -1,6 +1,6 @@ configData[$prefix]; $event_manager =& $this->Application->recallObject('EventManager'); + /* @var $event_manager kEventManager */ $register_classes = getArrayValue($config,'RegisterClasses'); if (!$register_classes) $register_classes = Array(); @@ -451,11 +455,10 @@ } $regular_events = getArrayValue($config, 'RegularEvents'); - if($regular_events) - { - foreach($regular_events as $short_name => $regular_event_info) - { - $event_manager->registerRegularEvent( $short_name, $config['Prefix'].':'.$regular_event_info['EventName'], $regular_event_info['RunInterval'], $regular_event_info['Type'] ); + if ($regular_events) { + foreach ($regular_events as $short_name => $regular_event_info) { + $event_status = array_key_exists('Status', $regular_event_info) ? $regular_event_info['Status'] : STATUS_ACTIVE; + $event_manager->registerRegularEvent( $short_name, $config['Prefix'].':'.$regular_event_info['EventName'], $regular_event_info['RunInterval'], $regular_event_info['Type'], $event_status); } } Index: branches/5.0.x/core/units/images/images_config.php =================================================================== diff -u -r12117 -r12167 --- branches/5.0.x/core/units/images/images_config.php (.../images_config.php) (revision 12117) +++ branches/5.0.x/core/units/images/images_config.php (.../images_config.php) (revision 12167) @@ -1,6 +1,6 @@ Array( - 1 => 'id', - 2 => 'page', - 3 => 'event', - ), + 1 => 'id', + 2 => 'page', + 3 => 'event', + ), + + 'RegularEvents' => Array ( + 'clean_catalog_images' => Array ('EventName' => 'OnCleanImages', 'RunInterval' => 604800, 'Type' => reAFTER, 'Status' => STATUS_DISABLED), + 'clean_resized_catalog_images' => Array ('EventName' => 'OnCleanResizedImages', 'RunInterval' => 2592000, 'Type' => reAFTER, 'Status' => STATUS_DISABLED), + ), + 'IDField' => 'ImageId', 'StatusField' => Array('Enabled', 'DefaultImg'), // field, that is affected by Approve/Decline events 'TitleField' => 'Name', // field, used in bluebar when editing existing item Index: branches/5.0.x/core/units/agents/agent_eh.php =================================================================== diff -u -r12117 -r12167 --- branches/5.0.x/core/units/agents/agent_eh.php (.../agent_eh.php) (revision 12117) +++ branches/5.0.x/core/units/agents/agent_eh.php (.../agent_eh.php) (revision 12167) @@ -1,6 +1,6 @@ $agent_params['EventName'], 'AgentName' => $agent_name, 'AgentType' => AGENT_TYPE_SYSTEM, + 'Status' => array_key_exists('Status', $agent_params) ? $agent_params['Status'] : STATUS_ACTIVE, 'RunInterval' => $agent_params['RunInterval'], 'RunMode' => $run_mode, ); @@ -87,6 +88,11 @@ function customProcessing(&$event, $type) { if ($event->Name == 'OnMassDelete' && $type == 'before') { + if ($this->Application->isDebugMode()) { + // allow to delete system agents in debug mode + return ; + } + $ids = $event->getEventParam('ids'); if ($ids) { $id_field = $this->Application->getUnitOption($event->Prefix, 'IDField'); Index: branches/5.0.x/core/kernel/event_manager.php =================================================================== diff -u -r12117 -r12167 --- branches/5.0.x/core/kernel/event_manager.php (.../event_manager.php) (revision 12117) +++ branches/5.0.x/core/kernel/event_manager.php (.../event_manager.php) (revision 12167) @@ -1,6 +1,6 @@ (int)$agent_data['RunInterval'], 'LastRunOn' => (int)$agent_data['LastRunOn'], 'NextRunOn' => (int)$agent_data['NextRunOn'], + 'Status' => $agent_data['Status'], ); } } @@ -202,15 +203,15 @@ * @param int $run_interval run interval in seconds * @param int $type before or after regular event */ - function registerRegularEvent($short_name, $event_name, $run_interval, $type = reBEFORE) + function registerRegularEvent($short_name, $event_name, $run_interval, $type = reBEFORE, $status = STATUS_ACTIVE) { if($type == reBEFORE) { - $this->beforeRegularEvents[$short_name] = Array('EventName' => $event_name, 'RunInterval' => $run_interval); + $this->beforeRegularEvents[$short_name] = Array('EventName' => $event_name, 'RunInterval' => $run_interval, 'Status' => $status); } else { - $this->afterRegularEvents[$short_name] = Array('EventName' => $event_name, 'RunInterval' => $run_interval); + $this->afterRegularEvents[$short_name] = Array('EventName' => $event_name, 'RunInterval' => $run_interval, 'Status' => $status); } }