Back to PhocaDownloadDownload class

Method getDownloadData

public static
getDownloadData
(mixed $id, mixed $return, mixed $type = 0)

Method getDownloadData - Source code

public static function getDownloadData($id, $return, $type = 0)
{
    $outcome = array();
    $wheres = array();
    $db = Factory::getDBO();
    $app = Factory::getApplication();
    $params = $app->getParams();
    $user = Factory::getUser();
    $redirectUrl = urlencode(base64_encode($return));
    $returnUrl = 'index.php?option=com_users&view=login&return=' . $redirectUrl;
    $userLevels = implode(',', $user->getAuthorisedViewLevels());
    $limitEnabled = $params->get('user_files_max_count_download', 0);
    if ((int) $limitEnabled > 0) {
        if ((int) $user->id < 1) {
            $app->enqueueMessage(Text::_("COM_PHOCADOWNLOAD_NOT_LOGGED_IN_USERS_NOT_ALLOWED_DOWNLOAD"), 'error');
            $app->redirect(Route::_($returnUrl, false));
            exit;
        }
        $userFileCount = PhocaDownloadStat::getCountFilePerUser($id);
        (int) $userFileCount++;
        // Because we need to count this attempt too.
        if ((int) $userFileCount > (int) $limitEnabled) {
            $app->enqueueMessage(Text::_("COM_PHOCADOWNLOAD_MAX_LIMIT_DOWNLOAD_PER_FILE_REACHED"), 'error');
            $app->redirect(Route::_($returnUrl, false));
            exit;
        }
    }
    $pQ = $params->get('enable_plugin_query', 0);
    $wheres[] = " c.id = " . (int) $id;
    $wheres[] = " c.published = 1";
    $wheres[] = " c.approved \t= 1";
    $wheres[] = " c.catid = cc.id";
    if ($type == 1) {
        // Unique download link does not have any access
        $rightDisplay = 1;
    } else {
        $wheres[] = " cc.access IN (" . $userLevels . ")";
    }
    // Active
    $jnow = Factory::getDate();
    $now = $jnow->toSql();
    $nullDate = $db->getNullDate();
    $wheres[] = ' ( c.publish_up = ' . $db->Quote($nullDate) . ' OR c.publish_up <= ' . $db->Quote($now) . ' )';
    $wheres[] = ' ( c.publish_down = ' . $db->Quote($nullDate) . ' OR c.publish_down >= ' . $db->Quote($now) . ' )';
    if ($pQ == 1) {
        // GWE MOD - to allow for access restrictions
        PluginHelper::importPlugin("phoca");
        //$dispatcher = JEventDispatcher::getInstance();
        $joins = array();
        $results = Factory::getApplication()->triggerEvent('onGetDownload', array(&$wheres, &$joins, $id, $paramsC));
        // END GWE MOD
    }
    /*$query = " SELECT c.filename, c.directlink, c.access"
    		." FROM #__phocadownload AS c"
    		. ($pQ == 1 ? ((count($joins)>0?( " LEFT JOIN " .implode( " LEFT JOIN ", $joins )):"")):"") // GWE MOD
    		. " WHERE " . implode( " AND ", $wheres )
    		. " ORDER BY c.ordering";*/
    $query = ' SELECT c.id, c.catid, c.filename, c.directlink, c.link_external, c.access, c.confirm_license, c.metakey, c.metadesc, cc.access as cataccess, cc.accessuserid as cataccessuserid, c.tokenhits ' . ' FROM #__phocadownload AS c, #__phocadownload_categories AS cc ' . ($pQ == 1 ? count($joins) > 0 ? ' LEFT JOIN ' . implode(' LEFT JOIN ', $joins) : '' : '') . ' WHERE ' . implode(' AND ', $wheres) . ' ORDER BY c.ordering';
    $db->setQuery($query, 0, 1);
    $filename = $db->loadObjectList();
    $limitTokenEnabled = $params->get('token_files_max_count_download', 0);
    if ((int) $limitTokenEnabled > 0) {
        if (isset($filename[0]->tokenhits)) {
            $tokenFileCount = $filename[0]->tokenhits;
            (int) $tokenFileCount++;
            // Because we need to count this attempt too.
            if ((int) $tokenFileCount > (int) $limitTokenEnabled) {
                $app->enqueueMessage(Text::_("COM_PHOCADOWNLOAD_MAX_LIMIT_DOWNLOAD_TOKEN_REACHED"), 'error');
                $app->redirect(Route::_(htmlspecialchars($return)));
                exit;
            }
        }
    }
    //OSE Modified Start;
    if (!empty($filename[0])) {
        phocadownloadimport('phocadownload.utils.external');
        PhocaDownloadExternal::checkOSE($filename[0]);
    }
    //OSE Modified End;
    // - - - - - - - - - - - - - - -
    // USER RIGHT - Access of categories (if file is included in some not accessed category) - - - - -
    // ACCESS is handled in SQL query, ACCESS USER ID is handled here (specific users)
    $rightDisplay = 0;
    if ($type == 1) {
        // Unique download link does not have any access
        $rightDisplay = 1;
    } else {
        if (!empty($filename[0])) {
            $rightDisplay = PhocaDownloadAccess::getUserRight('accessuserid', $filename[0]->cataccessuserid, $filename[0]->cataccess, $user->getAuthorisedViewLevels(), $user->get('id', 0), 0);
        }
        // - - - - - - - - - - - - - - - - - - - - - -
        if ($rightDisplay == 0) {
            $app->enqueueMessage(Text::_("COM_PHOCADOWNLOAD_NO_RIGHTS_ACCESS_CATEGORY_FILE"), 'error');
            $app->redirect(Route::_($returnUrl, false));
            exit;
        }
    }
    if (empty($filename)) {
        $outcome['file'] = "COM_PHOCADOWNLOAD_ERROR_NO_DB_RESULT";
        $outcome['directlink'] = 0;
        $outcome['externallink'] = 0;
        return $outcome;
    }
    if ($type == 1) {
        // Unique download link
    } else {
        if (isset($filename[0]->access)) {
            if (!in_array($filename[0]->access, $user->getAuthorisedViewLevels())) {
                $app->enqueueMessage(Text::_("COM_PHOCADOWNLOAD_PLEASE_LOGIN_DOWNLOAD_FILE"), 'error');
                $app->redirect(Route::_($returnUrl, false));
                exit;
            }
        } else {
            $outcome['file'] = "COM_PHOCADOWNLOAD_ERROR_NO_DB_RESULT";
            $outcome['directlink'] = 0;
            $outcome['externallink'] = 0;
            return $outcome;
        }
    }
    // - - - - - - - - - - - - - - - -
    $filenameT = $filename[0]->filename;
    $directlinkT = $filename[0]->directlink;
    $linkExternalT = $filename[0]->link_external;
    // Unique Download Link
    if ($type == 1) {
        $directlinkT = 0;
        // Unique Download Link cannot work with direct link
    }
    $filePath = PhocaDownloadPath::getPathSet('file');
    if ($filenameT != '') {
        // Important - you cannot use direct link if you have selected absolute path
        // Absolute Path defined by user
        $absolutePath = $params->get('absolute_path', '');
        if ($absolutePath != '') {
            $directlinkT = 0;
        }
        if ($directlinkT == 1) {
            $relFile = Uri::base(true) . '/' . $params->get('download_folder', 'phocadownload') . '/' . $filenameT;
            $outcome['file'] = $relFile;
            $outcome['directlink'] = $directlinkT;
            $outcome['externallink'] = $linkExternalT;
            return $outcome;
        } else {
            if ($directlinkT == 0 && $linkExternalT != '') {
                $relFile = Uri::base(true) . '/' . $params->get('download_folder', 'phocadownload') . '/' . $filenameT;
                $outcome['file'] = $relFile;
                $outcome['directlink'] = $directlinkT;
                $outcome['externallink'] = $linkExternalT;
                return $outcome;
            } else {
                $absFile = str_replace('\\', '/', Path::clean($filePath['orig_abs_ds'] . $filenameT));
            }
        }
        if (File::exists($absFile)) {
            $outcome['file'] = $absFile;
            $outcome['directlink'] = $directlinkT;
            $outcome['externallink'] = $linkExternalT;
            return $outcome;
        } else {
            $outcome['file'] = "COM_PHOCADOWNLOAD_ERROR_NO_ABS_FILE";
            $outcome['directlink'] = 0;
            $outcome['externallink'] = $linkExternalT;
            return $outcome;
        }
    } else {
        $outcome['file'] = "COM_PHOCADOWNLOAD_ERROR_NO_DB_FILE";
        $outcome['directlink'] = 0;
        $outcome['externallink'] = $linkExternalT;
        return $outcome;
    }
}