Skip to content

Instantly share code, notes, and snippets.

@SchumacherFM
Created November 30, 2012 10:10
Show Gist options
  • Save SchumacherFM/4174929 to your computer and use it in GitHub Desktop.
Save SchumacherFM/4174929 to your computer and use it in GitHub Desktop.
Dynamic Magento controller Actions with PHP __call() method. Works without any rewrite condition. Good or bad? ;-)
<?php
class XXX_FileRestriction_IdController extends Mage_Core_Controller_Front_Action
{
/**
* Allow URLs like http://domain.tld/downloader/id/4711
* without any rewrite syntax
*
* @param string $method
* @param array $args
*
* @return bool
* @throws Varien_Exception
*/
public function __call($method, $args)
{
switch (substr($method, -6)) {
case 'Action' :
$id = (int)str_replace('Action', '', $method);
$this->_downloader($id);
return TRUE;
}
throw new Varien_Exception("Invalid method " . get_class($this) . "::" . $method . "(" . print_r($args, 1) . ")");
}
/**
* @param $id
*
* @return bool
*/
protected function _downloader($id)
{
$isLoggedIn = Mage::helper('customer')->isLoggedIn();
$customer = Mage::helper('customer')->getCustomer();
$customerGroupId = (int)$customer->getGroupId();
$customerId = (int)$customer->getEntityId();
/**
* @var $collection XXX_FileRestriction_Model_Resource_Files_Collection
*/
$collection = Mage::getModel('xxx_filerestriction/files')->getCollection();
$collection->addRestrictedIdsFilter(array($id));
if ($collection->count() == 0) {
return $this->_sendError404();
}
/**
* @var $file XXX_FileRestriction_Model_Files
*/
$file = $collection->getFirstItem();
$allowGroups = $file->getAllowCustomerGroups();
/*trimExplode in short*/
$allowIds = array_flip(array_merge(array_diff(array_map('trim', explode(',', $file->getAllowCustomerIds())), array(''))));
$isDownloadAllowed = FALSE;
if (!empty($allowGroups) || !empty($allowIds)) {
$isGroupAllowed = ($allowGroups & pow(2, $customerGroupId)) > 0;
$isCustomerIdAllowed = isset($allowIds[$customerId]);
if ($isLoggedIn && ($isGroupAllowed || $isCustomerIdAllowed)) {
$isDownloadAllowed = TRUE;
}
} else {
$isDownloadAllowed = TRUE;
}
if ($isDownloadAllowed) {
$downloads = (int)$file->getDownloads();
$file->setDownloads($downloads + 1);
$file->save();
if (Mage::helper('xxx_filerestriction')->isExternalUri($file->getFileUri())) {
$this->_redirectUrl($file->getFileUri());
} else {
$filePath = Mage::getBaseDir('media') . DS . $file->getFileUri();
$fileName = basename($filePath);
$content = array(
'type' => 'filename',
'value' => $filePath,
);
$this->_prepareDownloadResponse($fileName, $content);
return;
}
} else {
return $this->_sendError404();
}
}
/**
* @return bool
*/
protected function _sendError404()
{
$this->getResponse()->setHeader('HTTP/1.1', '404 Not Found');
$this->getResponse()->setHeader('Status', '404 File not found');
// $this->getResponse()->setBody('404 File not found');
$this->_prepareDownloadResponse('notFound.txt', 'File not found'); /*???*/
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment