예제 #1
0
/**
 * Defines if the user can download the document
 * @
 * @return type
 * @since 0.5
 * @version 1.9.2, 9/3/2015, fix for download count
 * v1.9.1, 16/1/2015, add bp_group_documents_download_access filter
 */
function cac_catch_group_doc_request()
{
    $error = false;
    if (empty($_GET['get_group_doc'])) {
        return;
    }
    $doc_id = $_GET['get_group_doc'];
    // Check to see whether the current user has access to the doc in question
    $file_deets = explode('/', $doc_id);
    $group_id = $file_deets[0];
    $group = new BP_Groups_Group($group_id);
    $doc_name = $file_deets[1];
    if (empty($group->id)) {
        $error = array('message' => __('That group does not exist.', 'bp-group-documents'), 'redirect' => bp_get_root_domain());
    } else {
        if ($group->status != 'public') {
            // If the group is not public,
            if (!is_super_admin()) {
                //then the user must be logged in and
                // a member of the group to download the document
                if (!is_user_logged_in() || !groups_is_user_member(bp_loggedin_user_id(), $group_id)) {
                    $error = array('message' => sprintf(__('You must be a logged-in member of the group %s to access this document. If you are a member of the group, please log into the site and try again.', 'bp-group-documents'), $group->name), 'redirect' => bp_get_group_permalink($group));
                }
            }
        }
        /**
         * Filter the error.
         *
         * @since 1.9.1
         *
         * @param array $error A compacted array of $error arguments, including the "message" and
         *                    "redirect" values.
         */
        $error = apply_filters('bp_group_documents_download_access', $error);
        //
        // If we have gotten this far without an error, then the download can go through
        if (!$error) {
            $document = new BP_Group_Documents();
            $document->populate_by_file($doc_name);
            $doc_path = $document->get_path();
            clearstatcache();
            if (file_exists($doc_path)) {
                $document->increment_download_count();
                $mime_type = mime_content_type($doc_path);
                $doc_size = filesize($doc_path);
                header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0");
                header("Pragma: hack");
                header("Content-Type: {$mime_type}; name='" . $file_deets[1] . "'");
                header("Content-Length: " . $doc_size);
                header('Content-Disposition: inline; filename="' . $file_deets[1] . '"');
                header("Content-Transfer-Encoding: binary");
                ob_clean();
                flush();
                readfile($doc_path);
                die;
            } else {
                // File does not exist
                $error = array('message' => _e('The file could not be found.', 'bp-group-documents'), 'redirect' => bp_get_group_permalink($group) . 'documents');
            }
        }
    }
    // If we have gotten this far, there was an error. Add a message and redirect
    bp_core_add_message($error['message'], 'error');
    bp_core_redirect($error['redirect']);
}
예제 #2
0
/** bp_group_documents_check_legacy_paths()
 *
 * checks if there are any documents in the old location (documents folder in plugin)
 * and if so, moves them to the new location (wp-content/blogs.dir)
 *
 * This will only fire when the admin page is viewed to save on overhead
 * @version 2, 3/9/2013 fix BP_GROUP_DOCUMENTS_DIR
 * @deprecated since version 1.2.2
 */
function bp_group_documents_check_legacy_paths()
{
    if (defined('BP_GROUP_DOCUMENTS_PATH')) {
        $legacy_path = BP_GROUP_DOCUMENTS_PATH;
    } else {
        $legacy_path = WP_PLUGIN_DIR . '/' . BP_GROUP_DOCUMENTS_DIR . '/documents/';
    }
    if ($dh = @opendir($legacy_path)) {
        $moved_count = 0;
        while (false !== ($file = readdir($dh))) {
            if ($file != "." && $file != "..") {
                $document = new BP_Group_Documents();
                if ($document->populate_by_file($file)) {
                    rename($legacy_path . $file, $document->get_path(0, 1));
                    ++$moved_count;
                }
            }
        }
    }
}