function autoDetect(&$event)
 {
     $selection = $event->getArgument('selection');
     if (!is_array($selection)) {
         $selection = "all";
     }
     $tools =& zmgToolboxConstants::getTools();
     $imagetools =& zmgToolboxConstants::getImageTools();
     $getall = false;
     if (!is_array($selection) && $selection == "all") {
         $getall = true;
         $selection = $tools;
     }
     $toolkey = intval(zmgFactory::getConfig()->get('plugins/toolbox/general/conversiontool'));
     $imagetool = $imagetools[$toolkey - 1];
     if ($getall) {
         //auto-detect currently selected imagetool first
         zmgimport('org.zoomfactory.var.plugins.toolbox.tools.' . $imagetool . 'Tool');
         zmgCallAbstract('zmg' . ucfirst($imagetool) . 'Tool', 'autoDetect');
     }
     //auto-detect other tools as well
     foreach ($selection as $tool) {
         if (!in_array($tool, $imagetools)) {
             zmgimport('org.zoomfactory.var.plugins.toolbox.tools.' . $tool . 'Tool');
             zmgCallAbstract('zmg' . ucfirst($tool) . 'Tool', 'autoDetect');
         } else {
             if (!$getall) {
                 if ($tool != $imagetool) {
                     zmgimport('org.zoomfactory.var.plugins.toolbox.tools.' . $tool . 'Tool');
                 }
                 zmgCallAbstract('zmg' . ucfirst($tool) . 'Tool', 'autoDetect');
             }
         }
     }
     zmgToolboxPlugin::throwErrors();
 }
 function finalizeUpload($gid = 0)
 {
     //finish the SwfUpload sequence...
     if ($gid === 0) {
         return zmgToolboxPlugin::registerError(T_('Upload media'), T_('No valid gallery ID provided'));
     }
     $session =& zmgFactory::getSession();
     $events =& zmgFactory::getEvents();
     $config =& zmgFactory::getConfig();
     $db =& zmgDatabase::getDBO();
     $gallery = new zmgGallery($db);
     $gallery->load($gid);
     //now we got the gallery and its data, retrieve the uploaded media
     $media = $session->get('uploadtool.fancyfiles');
     if (!is_array($media) || count($media) == 0) {
         return zmgToolboxPlugin::registerError(T_('Upload media'), T_('No media have been uploaded; nothing to do.'));
     }
     zmgimport('org.zoomfactory.lib.helpers.zmgFileHelper');
     $src_path = ZMG_ABS_PATH . DS . "etc" . DS . "cache" . DS;
     $dest_path = zmgEnv::getRootPath() . DS . $config->get('filesystem/mediapath') . $gallery->dir . DS;
     foreach ($media as $medium) {
         $obj = new zmgMedium($db);
         $name = zmgSQLEscape(zmgGetParam($_REQUEST, 'zmg_upload_name', ''));
         $descr = zmgSQLEscape(zmgGetParam($_REQUEST, 'zmg_upload_descr', ''));
         $data = array('name' => $name, 'filename' => $medium, 'descr' => $descr, 'published' => 1, 'gid' => $gallery->gid);
         $obj->setGalleryDir($gallery->dir);
         //saves a SQL query later on...
         //do some additional validation of strings
         $data['name'] = $events->fire('onvalidate', $data['name']);
         if (!$data['name']) {
             $data['name'] = $name;
         }
         $data['descr'] = $events->fire('onvalidate', $data['descr']);
         if (!$data['descr']) {
             $data['descr'] = $descr;
         }
         if (!$obj->bind($data)) {
             zmgToolboxPlugin::registerError(T_('Upload media'), T_('Medium could not be saved') . ': ' . $obj->getError());
         } else {
             if (!zmgFileHelper::copy($src_path . $medium, $dest_path . $medium)) {
                 zmgToolboxPlugin::registerError(T_('Upload media'), T_('Unable to copy file') . ' ' . $medium);
             } else {
                 if (!zmgFileHelper::delete($src_path . $medium)) {
                     zmgToolboxPlugin::registerError(T_('Upload media'), T_('Unable to delete temporary file') . ' ' . $medium);
                 } else {
                     if (!zmgToolboxPlugin::processMedium($obj, $gallery)) {
                         zmgToolboxPlugin::registerError(T_('Upload media'), T_('Medium could not be processed') . ' ' . $medium);
                     } else {
                         if (!$obj->store()) {
                             //now save this medium in our DB
                             zmgToolboxPlugin::registerError(T_('Upload media'), T_('Medium could not be saved') . ': ' . $obj->getError());
                         }
                     }
                 }
             }
         }
         //delete medium from session data: fourth parameter as TRUE
         $session->update('uploadtool.fancyfiles', $medium, ZMG_DATATYPE_ARRAY, true);
     }
     zmgToolboxPlugin::throwErrors();
 }