Debugging duplicate query exec in WordPress

During debugging a plugin malfunction I had duplicate option update SQL queries and after som thinking I came up with the following debug line to catch the trace of duplicate function call:

 error_log(var_export(debug_backtrace(), true));

I placed it above the return line in update_option() function in wp-includes/option.php

Finally had a trace in php/apache error_log file.

The second query came from an action hook in another plugin 😦

Posted in Uncategorized, wordpress | Tagged | Leave a comment

Mail sending via SMTP for WordPress

I was looking for some really simple SMTP solution for WordPress, only some code to send emails via SMTP. Without a plugin for localhost usage.

Came up with the following script in wp-content/mu-plugins/smtp.php

<?php
add_action('phpmailer_init', 'custom_phpmailer_init');
function custom_phpmailer_init($phpmailer) {  
 $phpmailer->IsSMTP();
 $phpmailer->Host = 'smtp.example.com';
 $phpmailer->SMTPDebug = 0;
 $phpmailer->SMTPAuth = true;
 $phpmailer->SMTPSecure = "ssl";
 $phpmailer->Port = 465;
 $phpmailer->Username = 'info@example.com';
 $phpmailer->Password = '';

return $phpmailer;
}
Posted in Uncategorized, wordpress | Tagged , | Leave a comment

Blocking web access by country code via htaccess

Some Ukraine and Russian folks don’t know how to behave so I decided to block the whole country. Also they’re not target audience for the targeted web.

# .htaccess

<IfModule mod_geoip.c>
GeoIPEnable On
SetEnvIf GEOIP_COUNTRY_CODE UA blk
SetEnvIf GEOIP_COUNTRY_CODE RU blk
Deny from env=blk
</IfModule>

They were causing 509 Bandwidth Limit Exceeded on some client webs with Joomla :/

Posted in security | Tagged , | Leave a comment

Useful Postfix CLI commands to manage email queue

During dev I sometimes need to check what emails are scheduled to be sent.

Display a list of queued mails

sudo mailq

View message in queue

sudo postcat -vq QUEUE_ID

Delete queued mail after debugging

sudo postsuper -d ALL
Posted in postfix, Uncategorized | Tagged , , | Leave a comment

Fixing MailChimp error “Data did not match any of the schemas described in anyOf.”

While PUTing subscribers into MailChimp I got a bunch of “Data did not match any of the schemas described in anyOf.” error responses from the API server.

Tried googling, but with no luck.

Second try was to look at the array (data) that was sent.

Array
(
    [email_address] => info@example.com
    [status_if_new] => subscribed
    [merge_fields] => Array
        (
            [FNAME] => John
            [LNAME] => Doe
            [COMPANY] => 
        )

)

My thought was that the empty merge field COMPANY was the problem.
So after modifying the code to only send not empty merge field data the error disappeared.

Posted in php, Uncategorized | Tagged , | Leave a comment

Fixing “Automatically populating $HTTP_RAW_POST_DATA is deprecated”

To reproduce try:

curl -X POST -H "Content-Type: application/json" -d "{foo: bar}" http://example.com

Great explanation can be found at https://www.bram.us/2014/10/26/php-5-6-automatically-populating-http_raw_post_data-is-deprecated-and-will-be-removed-in-a-future-version/

For me the solution on the shared hosting was adding the following line in .htaccess:

php_value always_populate_raw_post_data -1

Thanks to https://www.drupal.org/node/2485847#comment-10068266

Posted in php, Uncategorized | Tagged , | Leave a comment

Customizing Monolog HTML formatter for email

Monolog can send emails when there’s an error, but in the provided HTML formatter I missed the REQUEST_URI and REMOTE_ADDR that triggered the error.

Some info is in the exceptions section, but not always and needs horizontal scrolling in the email client 🙂

So I just replicated the HtmlFormatter and added some lines (dirty hacks).

Added service class

<?php

namespace App\Bundle\Service;

use Monolog\Logger;
use Monolog\Formatter\NormalizerFormatter;

class MonologEmailFormatter extends NormalizerFormatter
{
    public function format(array $record)
    {
        // code ...

        $output .= $this->addRow('Request', $_SERVER['REQUEST_URI']);
        if (isset($_SERVER['REMOTE_ADDR'])) {
            $output .= $this->addRow('Remote IP', $_SERVER['REMOTE_ADDR']);
        }
        
        // code ...
    }

Map service ID to class

        <service id="custom.monolog.formatter.html" class="App\Bundle\Service\CustomHtmlFormatter" public="false" />

Change formatter service ID

# app/config/config_prod.yml
swift:
    type: swift_mailer
    from_email: "php@example.com"
    to_email: ["info@example.com"]
    subject: 'Error'
    level: error
    formatter: custom.monolog.formatter.html
    content_type: text/html
Posted in symfony, Uncategorized | Tagged , , , | Leave a comment