/** * Creates suggestions for correcting problems with orphaned folders * * @return void * @since 1.5.5 */ public function createFolderSuggestions() { $query = $this->_db->getQuery(true); $query->select('COUNT(id)')->from(_JOOM_TABLE_ORPHANS)->where("type = 'folder'"); $this->_db->setQuery($query); $total = $this->_db->loadResult(); $start = false; if (!$this->_mainframe->getUserState('joom.maintenance.check.createfoldersuggestions')) { $start = true; $this->_mainframe->setUserState('joom.maintenance.check.index', 0); $this->_mainframe->setUserState('joom.maintenance.check.createfoldersuggestions', true); } $refresher = new JoomRefresher(array('name' => JText::_('COM_JOOMGALLERY_MAIMAN_CREATE_FOLDER_SUGGESTIONS'), 'remaining' => $total, 'start' => $start)); $index = $this->_mainframe->getUserState('joom.maintenance.check.index'); $query->clear('select')->select('id, type, fullpath'); $this->_db->setQuery($query, $index, 1); while ($folder = $this->_db->loadObject()) { $folder_name_parts = explode('_', $folder->fullpath); $count_parts = count($folder_name_parts); if (!$count_parts) { $index++; $this->_db->setQuery($query, $index, 1); continue; } if (is_numeric($folder_name_parts[$count_parts - 1])) { $category_id = (int) $folder_name_parts[$count_parts - 1]; if (strpos($folder->fullpath, JPath::clean($this->_ambit->get('thumb_path'), '/')) !== false) { $type = 'thumb'; } else { if (strpos($folder->fullpath, JPath::clean($this->_ambit->get('img_path'), '/')) !== false) { $type = 'img'; } else { $type = 'orig'; } } $query->clear()->select('c.cid AS category_id')->select('m.id AS orphan_id')->select('c.name')->from(_JOOM_TABLE_CATEGORIES . ' AS c')->leftJoin(_JOOM_TABLE_MAINTENANCE . ' AS m ON c.cid = m.refid')->where('c.cid = ' . $category_id); $this->_db->setQuery($query); if ($suggestion = $this->_db->loadObject()) { $query->clear()->update(_JOOM_TABLE_ORPHANS)->set('refid = ' . $suggestion->category_id)->set("title = '" . $suggestion->name . "'")->where('id = ' . $folder->id); $this->_db->setQuery($query); $this->_db->query(); if ($suggestion->orphan_id) { $query->clear()->update(_JOOM_TABLE_MAINTENANCE)->set($type . 'orphan = ' . $folder->id)->where('id = ' . $suggestion->orphan_id); $this->_db->setQuery($query); $this->_db->query(); } } } $index++; if (!$refresher->check()) { $this->_mainframe->setUserState('joom.maintenance.check.index', $index); $refresher->refresh($total - $index); } // Set query for next loop $query->clear()->select('id, type, fullpath')->from(_JOOM_TABLE_ORPHANS)->where("type = 'folder'"); $this->_db->setQuery($query, $index, 1); } // Finished $this->_mainframe->setUserState('joom.maintenance.checked', time()); $refresher->reset(null, null, JText::_('COM_JOOMGALLERY_MAIMAN_FINALIZING')); $refresher->refresh(0, 'display'); }
/** * Extract images from zip * * @return boolean True on success, false otherwise. * @since 1.0.0 */ protected function uploadBatch() { // Check access $category = $this->getCategory($this->catid); if (!$category || !$this->_user->authorise('joom.upload', _JOOM_OPTION . '.category.' . $this->catid) && (!$this->_user->authorise('joom.upload.inown', _JOOM_OPTION . '.category.' . $this->catid) || !$category->owner || $category->owner != $this->_user->get('id'))) { $this->setError(JText::_('COM_JOOMGALLERY_COMMON_MSG_YOU_ARE_NOT_ALLOWED_TO_UPLOAD_INTO_THIS_CATEGORY')); return false; } // Load the refresher in order to initialise it right now at the beginning require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/refresher.php'; $refresher = new JoomRefresher(); // Check existence of temp directory if (!JFolder::exists($this->_ambit->get('temp_path'))) { $this->setError(JText::_('COM_JOOMGALLERY_UPLOAD_ERROR_TEMP_MISSING')); return false; } // Check existence of uploaded zip if ($zippack = JRequest::getVar('zippack', '', 'files')) { if (!JFile::exists($zippack['tmp_name'])) { $this->setError(JText::_('COM_JOOMGALLERY_UPLOAD_ERROR_FILE_NOT_UPLOADED')); return false; } // Make temp path writeable if it is not, workaround for servers with wwwrun-problem $permissions_changed = false; if (!is_writeable($this->_ambit->get('temp_path'))) { JoomFile::chmod($this->_ambit->get('temp_path'), '0777', true); $permissions_changed = true; } // Create subdirectory in tmp folder // $zippack['name'] = original name of uploaded archive // strip extension before and add a random number at the end $extractdir = $this->_ambit->get('temp_path') . JFile::stripExt($zippack['name']) . '-' . mt_rand(10000, 99999); $createerr = JFolder::create($extractdir); // Check createerr if (!$createerr) { $this->setError(JText::_('COM_JOOMGALLERY_UPLOAD_ERROR_FILE_NOT_UPLOADED')); return false; } // Move uploaded file to a new directory with original name // because the uploaded archive is saved like php8900.tmp and JArchive // needs a valid extension $zipfile = $extractdir . '/' . $zippack['name']; JFile::upload($zippack['tmp_name'], $zipfile); // Extract archive to new directory, JArchive chooses the right adapter // according to the extension jimport('joomla.filesystem.archive'); $extracterr = JArchive::extract($zipfile, $extractdir); // TODO LF constant - Check error code of extraction if (!$extracterr) { $this->setError(JText::_('COM_JOOMGALLERY_UPLOAD_ERROR_FILE_NOT_UPLOADED')); return false; } // Get all files from extraction directory with the allowed // extension, recursively, with full path $inclusions = '.jpg$|.JPG$|.jpeg$|.JPEG$|.jpe$|.JPE$|.png$|.PNG$|.gif$|.GIF$'; $ziplist = JFolder::files($extractdir, $inclusions, true, true); // Set back temp path permissions if they were changed before if ($permissions_changed) { JoomFile::chmod($this->_ambit->get('temp_path'), '0755', true); } $sizeofzip = count($ziplist); // For each file extracted from zip get original filename and create // unique filename. Copy to new location, delete file in temp. location, // make thumbnail and add to database $this->_debugoutput .= '<p></p><hr />'; if ($sizeofzip == 1) { $this->_debugoutput .= JText::_('COM_JOOMGALLERY_UPLOAD_OUTPUT_FILE_IN_BATCH'); } else { $this->_debugoutput .= JText::sprintf('COM_JOOMGALLERY_UPLOAD_OUTPUT_FILES_IN_BATCH', $sizeofzip); } natcasesort($ziplist); $start = true; // Set session for extractdir in case of refreshing and deleting at end $this->_mainframe->setUserState('joom.upload.batch.subdir', $extractdir); } else { $ziplist = $this->_mainframe->getUserState('joom.upload.batch.files'); $sizeofzip = count($ziplist); $start = false; } // Counter of successfully uploaded images $counter = $this->_mainframe->getUserState('joom.upload.batch.counter', 0); // Reset the refresher in order to set total count of images to process $refresher->reset($sizeofzip, $start); foreach ($ziplist as $key => $file) { // Check remaining time if (!$refresher->check()) { $this->_mainframe->setUserState('joom.upload.batch.files', $ziplist); //$this->_mainframe->setUserState('joom.upload.debugoutput', $this->_debugoutput); $this->_mainframe->setUserState('joom.upload.debug', $this->debug); $this->_mainframe->setUserState('joom.upload.batch.counter', $counter); $refresher->refresh(count($ziplist)); } if ($this->_site && $this->counter > $this->_config->get('jg_maxuserimage') - 1 && $this->_user->get('id')) { $timespan = $this->_config->get('jg_maxuserimage_timespan'); $this->_debugoutput .= '<hr />' . JText::sprintf('COM_JOOMGALLERY_UPLOAD_OUTPUT_MAY_ADD_MAX_OF', $this->_config->get('jg_maxuserimage'), $timespan > 0 ? JText::plural('COM_JOOMGALLERY_UPLOAD_NEW_IMAGE_MAXCOUNT_TIMESPAN', $timespan) : '') . '<br />'; break; } // Trigger event 'onJoomBeforeUpload' $plugins = $this->_mainframe->triggerEvent('onJoomBeforeUpload'); if (in_array(false, $plugins, true)) { unset($ziplist[$key]); continue; } // Get the filename without path, JFile::getName() does not // work on local installations $filepathinfos = pathinfo($file); $origfilename = $filepathinfos['basename']; $filesize = filesize($file); $tag = strtolower(JFile::getExt($origfilename)); $this->_debugoutput .= '<hr />'; $this->_debugoutput .= JText::sprintf('COM_JOOMGALLERY_UPLOAD_FILENAME', $origfilename) . '<br />'; // Image size must not exceed the setting in backend if we are in frontend if ($this->_site && $filesize > $this->_config->get('jg_maxfilesize')) { $this->_debugoutput .= JText::sprintf('COM_JOOMGALLERY_UPLOAD_OUTPUT_MAX_ALLOWED_FILESIZE', $this->_config->get('jg_maxfilesize')) . '<br />'; $this->debug = true; unset($ziplist[$key]); continue; } // Check for right format if ($tag != 'jpeg' && $tag != 'jpg' && $tag != 'jpe' && $tag != 'gif' && $tag != 'png' || strlen($file) == 0 || $file == 'none') { $this->_debugoutput .= JText::_('COM_JOOMGALLERY_UPLOAD_OUTPUT_INVALID_IMAGE_TYPE') . '<br />'; $this->debug = true; unset($ziplist[$key]); continue; } // Check filename for special characters if not allowed if ($this->_config->get('jg_filenamewithjs') == 0) { if ($this->_site && $this->_config->get('jg_useruseorigfilename') || !$this->_site && $this->_config->get('jg_useorigfilename')) { $filename = $origfilename; } else { $filename = $this->imgtitle; } if (JoomFile::checkValidFilename($filename, '', true) == false) { if ($this->_site) { $this->_debugoutput .= JText::_('COM_JOOMGALLERY_COMMON_ERROR_INVALID_FILENAME') . '<br />'; } else { $this->_debugoutput .= JText::_('COM_JOOMGALLERY_UPLOAD_ERROR_INVALIDSC_FILENAME') . '<br />'; } $this->debug = true; unset($ziplist[$key]); continue; } } // Get the serial number if numbering is activated $filecounter = null; if ($this->_site && $this->_config->get('jg_useruploadnumber') || !$this->_site && $this->_config->get('jg_filenamenumber')) { $filecounter = $this->_getSerial(); } // Create new filename // If generic filename set in backend use it if ($this->_site && $this->_config->get('jg_useruseorigfilename') || !$this->_site && $this->_config->get('jg_useorigfilename')) { $oldfilename = $origfilename; $newfilename = JoomFile::fixFilename($origfilename); } else { $oldfilename = $this->imgtitle; $newfilename = JoomFile::fixFilename($this->imgtitle); } // Check the new filename if (JoomFile::checkValidFilename($oldfilename, $newfilename) == false) { if ($this->_site) { $this->_debugoutput .= JText::_('COM_JOOMGALLERY_COMMON_ERROR_INVALID_FILENAME') . '<br />'; } else { $this->_debugoutput .= JText::sprintf('COM_JOOMGALLERY_UPLOAD_ERROR_INVALID_FILENAME', $newfilename, $oldfilename) . '<br />'; } $this->debug = true; unset($ziplist[$key]); continue; } $newfilename = $this->_genFilename($newfilename, $tag, $filecounter); // Move the image from temp folder to originals folder $return = JFile::move($file, $this->_ambit->getImg('orig_path', $newfilename, null, $this->catid)); if (!$return) { $this->_debugoutput .= JText::sprintf('COM_JOOMGALLERY_UPLOAD_OUTPUT_PROBLEM_MOVING', $this->_ambit->getImg('orig_path', $newfilename, null, $this->catid)) . ' ' . JText::_('COM_JOOMGALLERY_COMMON_CHECK_PERMISSIONS') . '<br />'; $this->debug = true; unset($ziplist[$key]); continue; } // Try to set permissions to 644 $return = JoomFile::chmod($this->_ambit->getImg('orig_path', $newfilename, null, $this->catid), '0644'); $this->_debugoutput .= JText::_('COM_JOOMGALLERY_UPLOAD_OUTPUT_UPLOAD_COMPLETE') . '<br />'; // Create thumbnail and detail image if (!$this->resizeImage($this->_ambit->getImg('orig_path', $newfilename, null, $this->catid), $newfilename)) { $this->rollback($this->_ambit->getImg('orig_path', $newfilename, null, $this->catid), $this->_ambit->getImg('img_path', $newfilename, null, $this->catid), $this->_ambit->getImg('thumb_path', $newfilename, null, $this->catid)); $this->debug = true; unset($ziplist[$key]); continue; } // Insert the database entry $row = JTable::getInstance('joomgalleryimages', 'Table'); if (!$this->registerImage($row, $origfilename, $newfilename, $tag, $filecounter)) { $this->rollback($this->_ambit->getImg('orig_path', $newfilename, null, $this->catid), $this->_ambit->getImg('img_path', $newfilename, null, $this->catid), $this->_ambit->getImg('thumb_path', $newfilename, null, $this->catid)); $this->debug = true; unset($ziplist[$key]); continue; } $this->_debugoutput .= JText::_('COM_JOOMGALLERY_UPLOAD_OUTPUT_IMAGE_SUCCESSFULLY_ADDED') . '<br />'; $this->_debugoutput .= JText::sprintf('COM_JOOMGALLERY_UPLOAD_NEW_FILENAME', $newfilename) . '<br /><br />'; $counter++; $this->_mainframe->triggerEvent('onJoomAfterUpload', array($row)); $this->counter++; unset($ziplist[$key]); } // Remove subdir in temp finally $tempdir = $this->_mainframe->getUserState('joom.upload.batch.subdir'); if ($tempdir) { JFolder::delete($tempdir); } // Message about new images if ($this->_site && $counter) { require_once JPATH_COMPONENT . '/helpers/messenger.php'; $messenger = new JoomMessenger(); $message = array('from' => $this->_user->get('id'), 'subject' => JText::_('COM_JOOMGALLERY_MESSAGE_NEW_IMAGES_SUBMITTED_SUBJECT'), 'body' => JText::sprintf('COM_JOOMGALLERY_MESSAGE_NEW_IMAGES_SUBMITTED_BODY', $this->_config->get('jg_realname') ? $this->_user->get('name') : $this->_user->get('username'), $counter), 'mode' => 'upload'); $messenger->send($message); } $this->_debugoutput .= '<hr /><br />'; // Reset file counters, delete original and create special gif selection and debug information $this->_mainframe->setUserState('joom.upload.filecounter', 0); $this->_mainframe->setUserState('joom.upload.batch.counter', 0); $this->_mainframe->setUserState('joom.upload.delete_original', false); $this->_mainframe->setUserState('joom.upload.create_special_gif', false); $this->_mainframe->setUserState('joom.upload.debug', false); $this->_mainframe->setUserState('joom.upload.debugoutput', null); echo $this->_debugoutput; if (!$this->_site || JRequest::getBool('redirect')) { return !$this->debug; } JHTML::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/helpers/html'); ?> <p> <?php echo JHTML::_('joomgallery.icon', 'arrow.png', 'arrow'); ?> <a href="<?php echo JRoute::_('index.php?view=upload&tab=batch'); ?> "> <?php echo JText::_('COM_JOOMGALLERY_UPLOAD_MORE_UPLOADS'); ?> </a> </p> <p> <?php echo JHTML::_('joomgallery.icon', 'arrow.png', 'arrow'); ?> <a href="<?php echo JRoute::_('index.php?view=userpanel'); ?> "> <?php echo JText::_('COM_JOOMGALLERY_COMMON_BACK_TO_USER_PANEL'); ?> </a> </p> <p> <?php echo JHTML::_('joomgallery.icon', 'arrow.png', 'arrow'); ?> <a href="<?php echo JRoute::_('index.php?view=gallery'); ?> "> <?php echo JText::_('COM_JOOMGALLERY_COMMON_BACK_TO_GALLERY'); ?> </a> </p> <?php return !$this->debug; }