What is the best way to log errors in Zend Framework 1? - zend-framework

We built an app in Zend Framework (v1) and have not worked a lot in setting up error reporting and logging. Is there any way we could get some level or error reporting without too much change in the code? Is there a ErrorHandler plugin available?
The basic requirement is to log errors that happens within the controller, missing controllers, malformed URLs, etc.
I also want to be able to log errors within my controllers. Will using error controller here, help me identify and log errors within my controllers? How best to do this with minimal changes?

I would use Zend_Log and use the following strategy.
If you are using Zend_Application in your app, there is a resource for logging. You can read more about the resource here
My advice would be to choose between writing to a db or log file stream. Write your log to a db if you plan on having some sort of web interface to it, if not a flat file will do just fine.
You can setup the logging to a file with this simple example
resources.log.stream.writerName = "Stream"
resources.log.stream.writerParams.stream = APPLICATION_PATH "/../data/logs/application.log"
resources.log.stream.writerParams.mode = "a"
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority = 4
Also, I would suggest sending Critical errors to an email account that is checked regularly by your development team. The company I work for sends them to errors#companyname.com and that forwards to all of the developers from production sites.
From what I understand, you can't setup a Mail writer via a factory, so the resource won't do you any good, but you can probably set it up in your ErrorController or Bootstrap.
$mail = new Zend_Mail();
$mail->setFrom('errors#example.org')
->addTo('project_developers#example.org');
$writer = new Zend_Log_Writer_Mail($mail);
// Set subject text for use; summary of number of errors is appended to the
// subject line before sending the message.
$writer->setSubjectPrependText('Errors with script foo.php');
// Only email warning level entries and higher.
$writer->addFilter(Zend_Log::WARN);
$log = new Zend_Log();
$log->addWriter($writer);
// Something bad happened!
$log->error('unable to connect to database');
// On writer shutdown, Zend_Mail::send() is triggered to send an email with
// all log entries at or above the Zend_Log filter level.
You will need to do a little work to the above example but the optimal solution would be to grab the log resource in your bootstrap file, and add the email writer to it, instead of creating a second log instance.

You can use Zend_Controller_Plugin_ErrorHandler . As you can see on the documentation page there is an example that checks for missing controller/action and shows you how to set the appropriate headers.
You can then use Zend_Log to log your error messages to disk/db/mail.

Related

Thunderbird WebExtensions / MailExtensions development - How to deal with events such as "new mail"?

I'm trying to write my very first Thunderbird extension. If possible, I'd like to only use the newer WebExtensions / MailExtensions APIs.
Two things my extension needs to do:
Performs an action when a new mail arrives and is not junk.
When a message is read, check if there are still unread messages and, if not, performs an action.
The only examples I've found online dealing with "new mail event" hooks look like there are not using the newer APIs. For example:
Components.classes["#mozilla.org/messenger/msgnotificationservice;1"]
.getService(Components.interfaces.nsIMsgFolderNotificationService);
notificationService.addListener(myListener, notificationService.msgAdded);
or
Components.classes['#mozilla.org/messenger/services/session;1']
.getService(Components.interfaces.nsIMsgMailSession)
.AddFolderListener(myListener, Components.interfaces.nsIFolderListener.all);
... where myListener would be called when a new email arrives.
Those codes generate the error Components.classes is undefined in Thunderbird 91. If I understand properly this is because more stuff is required to stay compatible with the legacy API.
My question:
What is the proper way to listen to a new email event, using the WebExtensions / MailExtensions APIs?
Links I did read (but maybe I missed something!):
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Developing_WebExtensions_for_Thunderbird
https://webextension-api.thunderbird.net/en/91/
Oh! I found it!
background.js :
browser.messages.onNewMailReceived.addListener((folder, messages) => {
// ...
});
Those permissions are required: messagesRead and accountsRead.

Configure multiple emails for Serilog.Email sink based on log level

In my asp.net core 2.2 project (C#), I want serilog to use different sinks depending on the log level. For example, for every level below warning I want to write to a sql database. For Warning I want to send email to warn#domain.com and for error and above I want to send an email to error#domain.com.
googled a bunch of different "serilog different email", "serilog email log level". Nothing relevant comes up.
In my startup.cs file (asp.net core 2.2 project):
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.Enrich.FromLogContext()
.Enrich.WithMachineName()
.WriteTo.MSSqlServer(configuration["ConnectionStrings:DefaultConnection"], "censor_logs", columnOptions: columnOptions, autoCreateSqlTable: true)
.CreateLogger();
in appsettings.json
"Serilog": {
"MinimumLevel": "Warning"
}
Would like log.Warn to go to an email sink to a particular email address
log.Error and above to go to another email link
and
all log activity (including those above) to go to a sql database. Right now, I just get all log levels warn and above to SQL.

Need Yii2 Equivalent of Zend_Session_Namespace

I am currently migrating an old Zend 1.1 website and need a replacement for the uses of Zend_Session_Namespace.
Does one exist for Yii2? Or alternatively is there a plugin or something to add this functionality?
-Edit:
Specifically the ability to set expiry timeouts and hop limits like Zend has.
Thank you.
UPDATE
The info you have added in the edit was never mentioned earlier and makes your question too broad you might create a separate question for that.
By default session data are stored in files. The implementation is locking a file from opening a session to the point it's closed either by session_write_close() (in Yii it could be done as Yii::$app->session->close()) or at the end of request. While session file is locked all other requests which are trying to use the same session are blocked i.e. waiting for the initial request to release the session file. this can work for dev or small projects. But when it comes to handling massive concurrent requests, it is better to use more sophisticated storage, such as a database.
Zend_Session_Namespace instances provide the primary API for manipulating session data in the Zend Framework. Namespaces are used to segregate all session data, if you are converting the script to Yii2 framework you might need to look into https://www.yiiframework.com/doc/api/2.0/yii-web-session
A simple example to compare both of the functionalities by example are
Zend Framework 1.1 Counting Page Views
$defaultNamespace = new Zend_Session_Namespace('Default');
if (isset($defaultNamespace->numberOfPageRequests)) {
// this will increment for each page load.
$defaultNamespace->numberOfPageRequests++;
} else {
$defaultNamespace->numberOfPageRequests = 1; // first time
}
echo "Page requests this session: ",
$defaultNamespace->numberOfPageRequests;
Yii2 Framework Counting Page Views
public function actionIndex()
{
$session = new \yii\web\Session();
$session->open();
$visits = $session->get('visits', 0);
$visits = $visits+1;
$session->set('visits', $visits);
return "Total visits $visits";
}

Set default Global XSS filter- Session - CodeIgniter 3x

Hope someone can help me explain some of my questions in order:
1. When i set application/config/config.php:
Determines whether the XSS filter is always active when GET, POST or
COOKIE data is encountered.
$config['global_xss_filtering'] = TRUE;
So if I set the default value is FALSE. What benefits will I get? For example, the performance or processing speed of the server?
2. Session
function save(){
$data = $this->input->post('number',TRUE);
$this->session->set_userdata('TEST',$data);
}
//Suppose Client request GET to action
function insert(){
$num = $this->session->userdata('TEST');
//Do I need to filter data in session?
$num_clean = $this->security->xss_clean($num );
$this->model->run_insert($num_clean);
}
I do not trust the user. And I still do not understand much about: session activity
The server just sends the ID Session to the client. Does the server send the data, which I set up to the session, to the client?
Best way xss_clean for session Which i am using is: Filter the client data by xss_clean input class. Is that enough? And need to re-filter session again?
Hope someone helped me because I just using only Codeigniter's XSS filter. Thanks
part 1:
From CodeIgniter User Guide Version 2.2.6
XSS Filtering
CodeIgniter comes with a Cross Site Scripting Hack prevention filter which can either run automatically to filter all POST and COOKIE data that is encountered, or you can run it on a per item basis. By default it does not run globally since it requires a bit of processing overhead, and since you may not need it in all cases.
It's not something that should be used for general runtime processing since it requires a fair amount of processing overhead.
So answerto your 1st part of question : yes ,
setting $config['global_xss_filtering'] = false; has performance benefits. also in codeigniter 3 its This feature is DEPRECATED. So i prefer to set it false.
part 2 :
Session is different from cookie
Unlike a cookie, the information is not stored on the users computer. So when you store a session ,its safe to trust the session data.
session data are stored in server. Most sessions set a user-key on the user's computer that looks something like this: 765487cf34ert8dede5a562e4f3a7e12. Then, when a session is opened on another page, it scans the computer for a user-key. If there is a match, it accesses that session, if not, it starts a new session.
here is a simple guide to session to read https://www.w3schools.com/php/php_sessions.asp
deftailed one : http://php.net/manual/en/intro.session.php
in short $num_clean = $this->security->xss_clean($num ); this is unnecessary.

Drupal: How to automatically send (cck) node content + file attachment via email

I am still quite new to Drupal and have very limited programming skills.
I am trying to build a job board site using cck + views. I have created 2 related content types: a "job post" and a "job application" - both are related using a nodereference field.
The job application node has 4 fields: id of the job post to which the person is applying, email of the applicant, cover letter (body field) and attached cv (cck field that allows users to attach/upload a document).
Question: Once a job application is created I would like the content of the node (including the attached file) to be automatically sent via email to the person who posted the job (destination email address is in a cck field in the related "job post" node).
Thus my requirements are: (1) to automtically "transfer" the destination email address from the "job post" content type to the "job application" content type; and (2) to automatically send all the "job application" node contents + file attachment to the destination email.
Is there any module that can help me achieve this?
Thank you so much for your support.
My email address is: wedge.paul#gmail.com
To give it to you straight: No, there is no module that will do this. Largely because you have already made most content types and it is pretty unique to your project.
Still, you may not have limited programming skill, I would advice learning it when working with drupal. What you are asking is really not that hard to create by writing a custom module. Writing a custom module is really not that hard, and starting to write a custom module in Drupal is really well documented.
I can tell you what to use in the custom module, however it is better if you create it yourself (for future projects).
So you create your custom module:
function mymod_nodeapi{ //here all the action happens when a node is created
switch ($op) {
//if the node is inserted in the database
case 'insert':
//if node is a job application
if($node->type = "jobapplication"){
//using node_load function, you can load other nodes in a variable
$relatednode = node_load($node->nodereference);
//using drupal_mail function, you can mail people
drupal_mail();
}
break;
}
}
This code has not been tested and can't be copy pasted. However node_load and drupal_mail as well as hook_nodeapi... use those functions and you'll get there.
Lullabot's video tutorial "Learning CCK for Drupal" is based on the concept of a job application/posting site as a case study. It may be worth investigating.
no, I'm not connected in any way to Lullabot; just a fellow Drupaler