displaying page not found error with zend acl - zend-framework

whenever a controller is called if it is not registered in zend acl then we ususally get erro r like this
Fatal error: Uncaught exception 'Zend_Acl_Exception' with message
'Resource 'hsfasfdadsf' not found' in /usr/share/php/libzend-framework-php/Zend/Acl.php:365
Stack trace:
#0 /var/www/update/library/Management/Access.php(55): Zend_Acl->get('hsfasfdadsf')
#1 /usr/share/php/libzend-framework-php/Zend/Controller/Plugin/Broker.php(309): Management_Access->preDispatch(Object(Zend_Controller_Request_Http))
#2 /usr/share/php/libzend-framework-php/Zend/Controller/Front.php(941):
isn't there a way to check if the controller and action is registered in zend acl, i tried
if(!$acl->get($controller))
{
$request->setControllerName('error');
$request->setActionName('notfound');
}
but did not work

First solution:
Avoid those exceptions, e.g.
if (!$acl->has($your_resource)) {
// .. handle it the way you need
}
Second one
Handle those exceptions in ErrorController, i.e.:
if ($errors->exception instanceof Zend_Acl_Exception) {
// send needed headers...
// prepare log message...
// render info: resource_not_found.phtml
$this->_helper->viewRenderer('resource_not_found');
}

Related

Laravel 5.2 + mono log critical error send to mail

How I can send every critical error exception to email. in current Laravel 5.2 it log in /storage/logs/laravel.log file. I want get that critical error in to mail with MailHandler but don't know how to implement it.
in file we get this type of error :
[2017-12-07 09:32:51] local.ERROR: ParseError: syntax error, unexpected 'if' (T_IF) in /home/ilogix/Workspace/cudefender/site/app/Http/Controllers/FormsController.php:53
Stack trace:
#0 /home/ilogix/Workspace/cudefender/site/vendor/composer/ClassLoader.php(301): Composer\Autoload\includeFile('/home/ilogix/Wo...')
#1 [internal function]: Composer\Autoload\ClassLoader->loadClass('App\\Http\\Contro...')
#2 [internal function]: spl_autoload_call('App\\Http\\Contro...')
#3 /home/ilogix/Workspace/cudefender/site/vendor/laravel/framework/src/Illuminate/Routing/Route.php(280): ReflectionMethod->__construct('App\\Http\\Contro...', 'index')
i need same in email by monolog mail handler.
You can edit the app/Exceptions/Handler.php file to send emails to a given address. For example, edit the report function to:
public function report(Exception $e)
{
try {
\Mail::raw( $e->__toString(), function ( $m ) {
$m->from('no-reply#your-app.com', 'APP_NAME');
$m->to( 'name#domain.com' )->subject('APP_NAME - Fatal Error' );
});
} catch ( \Exception $e ) {
// ignore
}
parent::report($e);
}
This code will send the contents of the Exception $e to name#domain.com. You can change the from address, and subject to be anything you want.
Depending on the error encountered, the message you receive would look like:
Symfony\Component\HttpKernel\Exception\NotFoundHttpException in
your-app/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:161
Stack trace: #0
your-app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(821):
Illuminate\Routing\RouteCollection->match(Object(Illuminate\Http\Request))
...

Google authentication

I got a problem on google authenticate, it worked for a month but since a few days I got this error :
Fatal error: Uncaught exception 'apiAuthException' with message
'Invalid token format' in /home/project/html/google/auth/apiOAuth2.php:127 Stack trace:
#0 /home/project/html/google/auth/apiOAuth2.php(89): apiOAuth2->setAccessToken('{? "access_tok...')
#1 /home/project/html/google/apiClient.php(132): apiOAuth2->authenticate(Array)
#2 /home/project/html/hk/connects/google.php(22): apiClient->authenticate()
#3 {main} thrown in /home/project/html/google/auth/apiOAuth2.php on line 127
In apiOAuth2.php I have the code :
$accessToken = json_decode($accessToken, true);
if (! isset($accessToken['access_token']) || ! isset($accessToken['expires_in']) || ! isset($accessToken['refresh_token'])) {
throw new apiAuthException("Invalid token format");
}
I noticed that google doesn't send me the $accessToken['refresh_token'].
It doesn't seem to come from google cause I did a correct connexion on http://stackoverflow.com
Maybe it's cause of my code :
session_start();
$client = new apiClient();
$plus = new apiPlusService($client);
if (!isset($_GET['code'])) {
header('Location: '.$client->createAuthUrl()); // Calls the same page
} else {
$client->authenticate(); // Fails at this level
}
EDIT:
I figured a way to do it, like I don't know what is refresh_token made for I added this line :
if (!isset($accessToken['refresh_token'])) $accessToken['refresh_token'] = 12345678910;
It works for the moment...
There is a new explicit parameter called "access_type" required by the google authentication api to obtain a valid refresh token.
With this parameter you declare that you need offline access to the account and the api provides you a refresh token.
Download the latest google PHP SDK that automatically handles the new required parameter

SoapClient error fallback in PHP

In PHP, if you try to instantiate a new SoapClient, and the WSDL is not accessible (server down or whatever), a PHP fatal error is thrown:
Fatal error: SOAP-ERROR: Parsing WSDL:
Couldn't load from
'http://example.com/servlet/app/SomeService?wsdl'
: failed to load external entity
"http://example.com/servlet/app/SomeService?wsdl"
Fatal errors in PHP, as far as I know, are not recoverable.
Is there any way to fallback from this? Can this fatal error somehow be avoided?
Edit: I should say that I am running on PHP 5.2, if it makes any difference.
This has already been discussed :
https://bugs.php.net/bug.php?id=47584
http://www.php.net/manual/en/class.soapclient.php#104046
Rasmus himself proposed the following solution:
<?php
try {
$x = #new SoapClient("non-existent.wsdl",array("exceptions" => 1));
} catch (SoapFault $E) {
echo $E->faultstring;
}
echo "ok\n";
See this topic How do I catch a PHP Fatal Error
Basically you cant recover from a fatal error but you can provide a better experience to the user when registering a shutdown function
register_shutdown_function('handleShutdown');
function handleShutdown(){
$error = error_get_last();
if($error !== NULL){
echo "Sorry for the inconvenience, an error just occurred.";
}
}

Zend Framework : Invalid contrller specified (error) ??? Help

I moved my project from windows to ubuntu and had this error while trying to run the project:
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with
message 'Invalid controller specified (error)' in
/home/truong/webdev/qtcmsv2/library/Zend/Controller/Dispatcher/Standard.php:248
Stack trace:
#0 /home/truong/webdev/qtcmsv2/library/Zend/Controller/Front.php(954):
Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http),
Object(Zend_Controller_Response_Http))
#1 /home/truong/webdev/qtcmsv2/library/Zend/Application/Bootstrap/Bootstrap.php(97):
Zend_Controller_Front->dispatch()
#2 /home/truong/webdev/qtcmsv2/library/Zend/Application.php(366):
Zend_Application_Bootstrap_Bootstrap->run()
#3 /home/truong/webdev/qtcmsv2/index.php(56):
Zend_Application->run()
#4 {main} thrown in
/home/truong/webdev/qtcmsv2/library/Zend/Controller/Dispatcher/Standard.php
on line 248
I did have ErrorController.php file in my default module :
class ErrorController extends Zend_Controller_Action {
/**
* This action handles
* - Application errors
* - Errors in the controller chain arising from missing
* controller classes and/or action methods
*/
public function errorAction()
{
$errors = $this->_getParam('error_handler');
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
// 404 error -- controller or action not found
$this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found');
$this->view->title = 'HTTP/1.1 404 Not Found';
break;
default:
// application error; display error page, but don't change
// status code
$this->view->title = 'Application Error';
break;
}
$this->view->message = $errors->exception;
}
}
My project ran very smoothly in Windows, but the error above always occurs when running in Ubuntu.
How can I solve this problem?? Please help me!
ps : Sorry for my bad English.
Means ZF can't find the error controller so it could be that something is not where it belongs, something is configured incorrectly (like a path), is misnamed, permissions are off, etc. Hard to tell from the info you provided as the error could be in index.php, your bootstrap, config, etc.

Zend Application - a 404 error instead of Zend_Acl_Exception - how?

At the present, when I type a wrong address I get the following:
exception 'Zend_Acl_Exception' with message 'Resource 'default_asda' not found' in /home/alkimi/www/ ...
I would like to, instead of this, display a costumized 404.
How can we configure the framework for doing so?
Thanks a lot,
MEM
You get that exception when you attempt to query your ACL for a non-existant resource. You should check your ACL for the resource before calling isAllowed, eg
if (!$acl->has($resource)) {
// do something that triggers or leads to a 404
}
You can check if action and controller exists (is dispatchable) before checking permissions:
$front = Zend_Controller_Front::getInstance();
if (!$front->getDispatcher()->isDispatchable($request)) {
throw new Zend_Exception('Page not found', 404);
return false;
}