Exemplo n.º 1
0
function bp_group_documents_increment_download_count()
{
    $document_id = (string) $_POST['document_id'];
    if (isset($document_id) && ctype_digit($document_id)) {
        $document = new BP_Group_Documents($document_id);
        $document->increment_download_count();
    }
}
Exemplo n.º 2
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']);
}