CakePHP: Reporting Failed Downloads with the Media View - cakephp

I'm using CakePHP's Media view to force file downloads. My code is pretty much exactly like the example provided in the cookbook, which I'll paste here for your convenience:
<?php
class ExampleController extends AppController {
public function download () {
$this->viewClass = 'Media';
// Download app/outside_webroot_dir/example.zip
$params = array(
'id' => 'example.zip',
'name' => 'example',
'download' => true,
'extension' => 'zip',
'path' => APP . 'outside_webroot_dir' . DS
);
$this->set($params);
}
}
In the database, I have a field that keeps track of how many times the file was downloaded. I'm looking for a way to make sure that this number is as accurate as possible, so if a user's download gets cancelled or times out, the number does not increment. Is there some way for CakePHP's Media view to report that the download was, indeed, successful?

Detecting when a file has finished downloading is no easy task. This is something that would be done on the client side with javascript, but browsers do not give you any hooks for that.
There is a pretty clever solution here (setting a cookie and then looking for it with javascript), but it only tells you when the download has started.

Related

Cache View in CakePHP 3.0

I understand that we can cache an action or view page in CakePHP 2.0. Refer to this link http://book.cakephp.org/2.0/en/core-libraries/helpers/cache.html.
class PostsController extends AppController {
public $helpers = array('Cache');
}
public $cacheAction = array(
'view' => 36000,
'index' => 48000
);
However, it seems that CakePHP 3.0 has removed helper(http://book.cakephp.org/3.0/en/core-libraries/caching.html). Is there any other way in CakePHP 3.0 I can cache the view page. For example, I have a index view/action. And I would like to cache that page. Thanks.
Is there any other way in CakePHP 3.0 I can cache the view page.
Not without developing something yourself. Like for example saving the rendered content to a file in callback for Dispatcher.afterDispatch event and then checking for the file in callback for Dispatcher.beforeDispatch event and returning the cached response.
Better to use you something like Varnish which is more suited for the job.

Cakephp force download makes site useless

I am facing a rare problem with cakephp force download code. Below is my code:-
public function download($cid = null,$id = null) {
$this->viewClass= 'Media';
$Video = $this->Video->findById($id);
$ext = explode('.',$Video['Video']['name']);
$params = array(
'id' => $Video['Video']['name'],
'name' => $Video['Video']['title'],
'download' => true,
'extension' => $ext[1],
'path' => APP . 'webroot/movies/clips_mov'.DS
);
$this->set($params);
}
This is the code I am using since ages and almost all the tutorials on the web are point to this code only. Below is the code of download link code onclick of which executes this code and file starts downloading:-
<a href="<?php echo Router::url('/videos/download/1/'.$video['Download']
['video_id'], true);?>" >Download</a>
Now the problem starts. When I click the download link the file downloading starts and goes on well. But during the time file is downloading the site becomes useless. No matter on whatever link or button i click on the site the page keeps on loading but never loads. The files are bit large most of them are from 200MB to 1GB . The site becomes again useful and starts working fine when the file download is finish or if I cancel the downloading. But during the download the site becomes useless and we cannot browse it till then.
Please help me. Thanks in advance.

Cakephp 2.6 how do I return to the main application after entering a plugin

I am using Luis Dias' Report Manager Plugin to generate some quick reports for an application I am developing. From my application dashboard I enter the plugin using the following:
<td style="text-align:center">
<button style="height:75px; width:175px; background-color:BurlyWood; font-size:20px; font-family:Verdana"
onclick="window.location.href='<?php echo Router::url(array('controller'=>'report_manager'
,'action'=>'reports'))?>'">Report Management</button>
I'd really like to open the Report Generator Wizard in a new window but that's a different issue..
Once I am done with the report generator I'd like to return to my dashboard in my application. However, I am now in the Plugin's domain and can't figure out a command to "route" me back to the calling application.
Thanks in advance
Mike
To 'escape' from a plugin when routing you need to pass plugin => false in the route array. For example:-
$this->Html->url([
'controller' => 'pages',
'action' => 'view',
1,
'plugin' => false
]);
If you don't pass the plugin attribute it assumes you want to remain in the context of the current plugin. You need to be careful with this wherever you use links where plugins are in use.

CakePHP 1.3 cleares all cached pages after adding new post

I am using CakePHP 1.3 and trying to enable cache for view pages, cache system works fine and caches all pages. But when we add a new post (insert new record to database) or edit an old one (update a record of the table) CakePHP deletes all cached pages, not just the edited page!
app/config/core.php :
Cache::config('default', array('engine' => 'File','duration' => 8640000));
app/controllers/articles_controller.php :
var $helpers = array('Cache');
var $cacheAction = array(
'view' => array('duration' => 8640000),
'latest' => array('duration' => 8640000),
);
How can I tell Cake to delete just the cached version of changed page and not all cached pages?
This it actually pretty hard, so I can't just give you a piece of code to solve this. You need to edit the actual cake files in the lib folder that manage caching. Note: this is super not recommended by the cake people. However the lib/Cake/Cache/Engine/FileEngine.php is the file that has the operations of the file engine. You seem interested in the delete function:
/**
* Delete a key from the cache
*
* #param string $key Identifier for the data
* #return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
*/
public function delete($key) {
if ($this->_setKey($key) === false || !$this->_init) {
return false;
}
$path = $this->_File->getRealPath();
$this->_File = null;
//#codingStandardsIgnoreStart
return #unlink($path);
//#codingStandardsIgnoreEnd
}
Also, instead of editing the core cake files you could add your own file engine and use parted of the cake engine by moving the code and just extending the code there (that's totally cool in open source).
Its also possible that by reading the code used to implement the file caching engine you will find your actual solution. Good Luck.

CakeDC User Plugin - Is there Documentation Anywhere?

Browsing through GitHub and I found a pretty powerful CakePHP plugin called CakeDC Users that has a lot of features (Account verification, password reset, etc) for a creating a login/authentication system. I like it because it seems to be written by some of the actual CakePHP developers and it gets updated a lot but there seems to be absolutely zero documentation anywhere on it. I've just come across this plugin recently, since I was trying to see if there's a better way than "rolling" with my own solution. So I was wondering if anybody here has had experience with it and if so could point to some decent documentation online.
Edit There is some stuff at the bottom of the readme, but it hasn't been too intuitive for me.
Alternate question, if you don't use this plugin, is there a login/authentication plugin you use in CakePHP that you use for login/authentication?
I have ran into the same problem with using the CakeDC plugins, a lot of them have little/no documentation.
However, there is not "Zero" documentation for it, you can see how to set it up for the most part at the bottom of the github page in the read me. Also you need to put this inside your AppController::beforeFilter() method.
$this->Auth->authorize = 'controller';
$this->Auth->fields = array('username' => 'email', 'password' => 'passwd');
$this->Auth->loginAction = array('plugin' => 'users', 'controller' => 'users', 'action' => 'login', 'admin' => false);
$this->Auth->loginRedirect = '/';
$this->Auth->logoutRedirect = '/';
$this->Auth->authError = __('Sorry, but you need to login to access this location.', true);
$this->Auth->loginError = __('Invalid e-mail / password
combination. Please try again', true);
$this->Auth->autoRedirect = true;
$this->Auth->userModel = 'User';
$this->Auth->userScope = array('User.active' => 1);
if ($this->Auth->user()) {
$this->set('userData', $this->Auth->user());
$this->set('isAuthorized', ($this->Auth->user('id') != ''));
}
Also, you need an isAuthorized() function, something as simple as this will do:
public function isAuthorized() {
return true;
}
Additionally, you will need to allow the 'login' action (this will involve editing the plugin files). Just add 'login' to the $this->Auth->allow() in users_controller.php.
This question is pretty old now, but as it's not marked as resolved and we've been doing a lot on the documentation since then I think it's worth to update:
Documentation can be found here:
For the version 3+ of the framework
https://github.com/CakeDC/users/blob/master/Docs/Home.md
Tutorial > http://www.cakedc.com/jorge_gonzalez/2016/02/21/cakedc_users_plugin_for_cakephp_3_-_update
CakePHP Facebook login tutorial >
http://www.cakedc.com/jorge_gonzalez/2016/02/21/cakephp_facebook_login_using_cakedc_users_plugin_-_update_3_1_5
For the (old) version 2
https://github.com/CakeDC/users/blob/2.x/Docs/Home.md
After exhaustive search I found a tutorial on how to use CakeDC!
Here it is

Resources