Categories
PHP

Serialized DATA in WordPress

Developers often choose to store specific information in the database like theme options, or settings. Typically this data is serialized in the database so it can be copied or restored easily without compromising the integrity of the information.

ABOUT SERIALIZED DATA

“Serialized data” is just a fancy way of saying “to list data in a specific order”. PHP has a function called serialize() which stores data as a serialized row in the database. For example the “active_plugins” row in wp_options of your database may look like this:

a:8:{i:0;s:31:"query-monitor/query-monitor.php";i:1;s:57:"accesspress-instagram-feed/accesspress-instagram-feed.php";i:2;s:19:"akismet/akismet.php";i:3;s:29:"easy-captcha/easy-captcha.php";i:4;s:43:"google-analytics-dashboard-for-wp/gadwp.php";i:5;s:33:"instagram-feed/instagram-feed.php";i:6;s:19:"jetpack/jetpack.php";i:7;s:47:"really-simple-captcha/really-simple-captcha.php";}

That probably looks like a lot of nonsense, but in reality it’s just taking a list of items and placing it on a single row. The array has a total count of items, each item has a number in the sequence, and each item has a total count of characters.

a:8 = There are 8 items in this array

i:0 = This is item 1 in the array (counting begins with zero)

s:31 = This item has 31 characters

query-monitor/query-monitor.php = The value of the item is located in quotes. The number of characters here should always match the numerical value proceeding it.

You can also break the single line out into multiple lines for easier reading. Just be aware that it must be on a single line if copied back into the database.

$array = array(
'0' => 'query-monitor/query-monitor.php'
'1' => 'accesspress-instagram-feed/accesspress-instagram-feed.php'
'2' => 'akismet/akismet.php'
'3' => 'easy-captcha/easy-captcha.php'
'4' => 'google-analytics-dashboard-for-wp/gadwp.php'
'5' => 'instagram-feed/instagram-feed.php'
'6' => 'jetpack/jetpack.php'
'7' => 'really-simple-captcha/really-simple-captcha.php'
);

POTENTIAL ISSUES

The most common conflict occurs when copying your site to a different environment, as this will change the domain. If your site uses serialized data in a row that contains the domain the character count will no longer match the updated value.

For example, say your site stores serialized data in a row with the URL in it, like this:

a:1:{i:0;s:23:”http://mycooldomain.com”;}

Notice that the value of this item has 23 characters, and is denoted by s:23 in the array.

If copied using one of our Copy Site tools, the URL is search and replaced automatically to the new domain value. That means that the same row on your new environment would end up like this:

a:1:{i:0;s:23:”http://newsite.wpengine.com”;}

But after this change, the value is now 27 characters long meaning s:23 is incorrect. This invalidates the array and the entire row.

So for example, if your theme options stored a full URL path for some settings or an image, it would end up showing default theme settings on the site instead of your custom theme and settings.

If you are performing a basic SQL search/replace on your site, it won’t be able to intelligently update these rows containing serialized data, because it can’t automatically update the character count.

However, if you are performing a search and replace with PHP, this will work for serialized data.

When copying sites, there are a few options to search using PHP, so as to not break the serialized rows:

If you are running into issues with broken serialized data even when using one of the above methods, often times themes or plugins which store important data as serialized rows will offer an export option. With this option, you can export the settings, then import them to the new environment to fix the issue.

'Coz sharing is caring
Categories
linux PHP

How to Test Sendmail From Command Line on Linux

What Is Sendmail?

sendmail is a very plain and simple MTA (Mail Transfer Agent), which implements the SMTP (Simple Mail Transfer Protocol) amongst others and can be used to transmit emails, typically on Linux. While there is a commercial version available which is called “Sendmail”, the sendmail we’re covering in this how-to article is the UNIX-based version of it, which comes with pretty much every Linux distribution as well as *BSD (FreeBSD, OpenBSD, and variants). Using the sendmail command might be the easiest way to send e-mails via Linux shell CLI (Command Line Interface), apart from mailx, which can be used in conjunction with sendmail to make it even easier to send and receive emails from the command line. Like the name already suggests, sendmail itself can only send emails and not store received ones in POP or IMAP mailboxes.

Where Is Sendmail And Its Configuration Files Located?

The first interesting information that we might need for testing sendmail is the path of the binary file that gets executed if we issue the command sendmail on our command line. To figure that out, we’ll use the which command as shown below:

[root@box ~]# which sendmail
/usr/sbin/sendmail

The above output means that the full path to our sendmail command’s binary file is /usr/sbin/sendmail which you should note for the steps further below.

If you want to adjust the configuration files of sendmail, you can usually find them in the directory /etc/mail/on UNIX (FreeBSD, OpenBSD) and Linux (CentOS, Fedora, Debian, Ubuntu) systems. The main configuration file of sendmail is /etc/mail/sendmail.cf, however adjusting that is not part of this tutorial. A good place for more information is the manual page of sendmail, that you can view by running the command man sendmail. Now to the interesting part of this sendmail command line tutorial.

How to Test the Sendmail Command On Linux

To quickly test if the sendmail command is working correctly to then use it for example in shell scripts, via command line or even from PHP scripts (PHP supports sendmail to send emails from PHP scripts – you can set the sendmail path in your php.ini), you can issue the below command on your UNIX or Linux system:

echo "Subject: sendmail test" | sendmail -v my@email.com

my@email.com is obviously the e-mail address you want the test email to be sent to. This sendmail command line example will send a blank email with the subject “sendmail test” to my@email.com if the test is successful. You can also send longer e-mails containing a subject, body and additional headers if you want to, but just to test if sendmail works that’s usually not required. Still, here is how you can do that:

1.) Create a file called mail.txt (or anything you like) in ~/mail.txt with vim or nano or your preferred text editor

2.) Paste the following content to it, but of course adjusting the email addresses, as those are just sendmail command examples:

To: my@email.com
Subject: sendmail test two
From: me@myserver.com
And here goes the e-mail body, test test test..

3.) At last we send the e-mail template we just created with: sendmail -vt < ~/mail.txt

That’s it – you can now test sendmail from the command line and even send full e-mails including headers from Linux/UNIX shell. Below is an example of how the simple sendmail test could look like on a live system:

sendmail test

Sendmail And Spam

One thing I’d like to add is that due to the fact that PHP and other scripts can usually access the sendmail binaries, it can also be used to send out spam and phishing mails and that’s what can happen on shared servers where accounts were compromised through an outdated CMS or weak passwords. If you want to find out which script or POSIX user is sending the spam with sendmail, you can issue the below command and pay close attention to the output:

ps faux | grep sendmail

Sendmail Conclusion

While sendmail can pose a few risks on shared or insecure systems, it is a great lightweight MTA that can be used to send e-mails from shell scripts, PHP applications or even directly the command line. It also automatically negotiates STARTTLS for encrypted transmission of the emails if the remote SMTP server supports it, which is a useful security practice.

'Coz sharing is caring