Exemplo n.º 1
0
/**
 * Check if directory is empty
 *
 * @param string Directory path
 * @param boolean TRUE - to decide when dir is not empty if at least one file exists in subdirectories,
 *                FALSE - to decide - if dir contains even only empty subdirectories
 * @return boolean TRUE if directory is empty
 */
function is_empty_directory($dir, $recursive = true)
{
    $result = true;
    if (empty($dir) || !file_exists($dir) || !is_readable($dir)) {
        // Return TRUE if dir doesn't exist or it is not readbale
        return $result;
    }
    // Fix dir path if slash is missed at the end
    $dir = rtrim($dir, '/') . '/';
    $handle = opendir($dir);
    while (($file = readdir($handle)) !== false) {
        if ($file != '.' && $file != '..') {
            // Check what is it, dir or file?
            if ($recursive && is_dir($dir . $file)) {
                // It is a directory - try to find the files inside
                $result = is_empty_directory($dir . $file, $recursive);
                if ($result === false) {
                    // A file was found inside the directory
                    break;
                }
            } else {
                // It is a file then directory is not empty
                $result = false;
                break;
                // Stop here
            }
        }
    }
    closedir($handle);
    return $result;
}
Exemplo n.º 2
0
 /**
  * Check if the media directory or it's location was changed and perform the required data migration
  *
  * @param string the media directory path before update
  * @param string the media directory location before update
  * @return boolean true if the media directory was not changed or the change was successful, false otherwise
  */
 function check_media_dir_change($old_media_dir, $old_media_location = NULL)
 {
     global $Messages;
     $new_media_dir = $this->get_media_dir(false);
     if ($new_media_dir == $old_media_dir) {
         // The media dir was not changed, no need fo further updates
         return true;
     }
     $new_media_location = $this->get('media_location');
     if ($old_media_location == NULL) {
         // Media location was not changed
         $old_media_location = $new_media_location;
     }
     switch ($new_media_location) {
         case 'none':
             if (is_empty_directory($old_media_dir)) {
                 // Delete old media dir if it is empty
                 if (file_exists($old_media_dir) && !rmdir_r($old_media_dir)) {
                     $Messages->add(T_('The old media dir could not be removed, please remove it manually!'), 'warning');
                 }
             } else {
                 // The old media dir is not empty, but it must be cleared before it can be changed to none
                 $Messages->add(T_('Blog media folder is not empty, you cannot change it to "None".'), 'error');
                 return false;
             }
             break;
         case 'default':
         case 'subdir':
         case 'custom':
             global $media_path;
             if (file_exists($new_media_dir)) {
                 // Don't use the existing folder twice
                 $Messages->add(sprintf(T_('Folder %s already exists, it cannot be used for several media locations.'), '<b>' . $new_media_dir . '</b>'), 'error');
                 return false;
             }
             if (in_array(trim($new_media_dir, '/\\'), array($media_path . 'blogs', $media_path . 'import', $media_path . 'shared', $media_path . 'users'))) {
                 // Don't use the reserved paths
                 $Messages->add(sprintf(T_('Please use another folder name, because %s is reserved.'), '<b>' . $new_media_dir . '</b>'), 'error');
                 return false;
             }
             if ($new_media_location == 'custom') {
                 // Check for folder is not used by other blog, and it is not a sub-folder of other blog folder
                 $BlogCache =& get_BlogCache();
                 $BlogCache->clear(true);
                 $BlogCache->load_where('blog_ID != ' . $this->ID);
                 $other_blog_IDs = $BlogCache->get_ID_array();
                 foreach ($other_blog_IDs as $other_blog_ID) {
                     $other_Blog =& $BlogCache->get_by_ID($other_blog_ID, false, false);
                     $other_media_dir = $other_Blog->get_media_dir(false);
                     if (!empty($other_media_dir) && strpos($new_media_dir, $other_media_dir) === 0) {
                         $Messages->add(sprintf(T_('Please use another folder name, because %s is already used for another media location.'), '<b>' . $new_media_dir . '</b>'), 'error');
                         return false;
                     }
                 }
             }
             if ($old_media_location == 'none' || !file_exists($old_media_dir)) {
                 // The media folder was not used before, create the new media folder
                 return $this->get_media_dir(true);
             }
             if (copy_r($old_media_dir, $new_media_dir, '', array('_evocache', '.evocache'))) {
                 // The file have been copied to new folder successfully
                 if (!rmdir_r($old_media_dir)) {
                     // Display a warning if old folder could not be deleted
                     $Messages->add(sprintf(T_('Could not delete the old media folder "%s", please try to delete it manually.'), '<b>' . $old_media_dir . '</b>'), 'warning');
                 }
             } else {
                 // Display a message if some error on copying
                 $Messages->add(sprintf(T_('Could not move the media folder content from "%s" to the new "%s" location.'), '<b>' . $old_media_dir . '</b>', '<b>' . $new_media_dir . '</b>'), 'error');
                 return false;
             }
             break;
         default:
             debug_die('Invalid media location setting received!');
     }
     $Messages->add(T_('The media directory and all of its content were successfully moved to the new location.'), 'note');
     return true;
 }