Drupal and debugging PHP code inside body of nodes or blocks

One of the most annoying features in Drupal, which i think is a design flaw, is allowing the developer to add PHP node inside the body on a node or block.

It can make your life so much more difficult in certain cases when you have to debug something.

You may think that the code you write will never fail, or that you are not one of those developers that can break the site and just see a white page because of their code.
Usually when you override a theme and something goes wrong you just see the logs and you can see a clear stack of where the problem is and locate the appropriate file.

That is not the case when your code inside a node fails or even better when it fails inside a block. The stack of the error in the logs can lead you nowhere, usually inside some core module file that means nothing to you and just evals some variable. To make things worse, if your Drupal site is broken and you see just a white screen, then you have no alternative than to dig up the problematic code directly from your database and fix it there!

Recently i have been in such a situation during a migration of a perfectly operating site. Unfortunately due to different php versions and certain php legacy options turned off the site broke.

To make your life easier i will document here where you can look up for your php code in the database.

Here we go:

  SELECT * FROM field_data_body WHERE body_format='php_code';
  SELECT * FROM field_revision_body WHERE body_format='php_code';
  SELECT * FROM block_custom WHERE FORMAT='php_code';
  SELECT * FROM locales_source s LEFT JOIN locales_target t ON s.lid=t.lid
    WHERE s.SOURCE LIKE '%<?php%' OR t.translation LIKE '%<?php%';

So as you can see the places where i found php code is inside the node body, the revisions’ node body, in blocks and also in some translations of blocks’ body.
That’s all for now.

Cheers!

composer problems and SSL errors: OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed Failed to enable crypto

After having some hard time installing some packages with composer, i decided to give back some simple solutions for all of you.

First of all suppose you operate in a shared hosting environment where you are not able to mess with the openssl CA root certificates and change the paths.
So you can simply download locally (e.g. /home/kmak) this http://curl.haxx.se/ca/cacert.pem

After you install locally composer.phar you can create your composer.json by requiting your package:

e.g.

 php composer.phar require slim/slim

Then edit composer.json and add the following config section, so finally your composer.json will look like this:

{
    "config": {
        "cafile":"/home/kmak/cacert.pem"
    },
    "require": {
        "slim/slim": "^3.3"
    }
}

Now you can proceed like this:

 php composer.phar install

If you encouter further SSL certificate errors due to git attempting to clone some project, then you can do this:

 GIT_SSL_NO_VERIFY=TRUE php composer.phar install

Remember disabling SSL certificate verification is a major security issue and should be avoided if possible.

Cheers.

MSSQL: connect to MSSQL server from linux using php

In ubuntu you can do the following:

1. First of all install php5-sybase, freetds-common and freetds-bin packages in debian systems

2. Edit file /etc/freetds/freetds.conf and add accordingly a section regarding your MSSQL server you are trying to cennect to.
Lets assume you created a section [proj1mssql]

3. You can connect from the php code like this:

  $dsn = 'dblib:host=proj1mssql;dbname=mydbname;charset=UTF-8';
  $dbh = new PDO($dsn, $dbuser, $dbpass);

MSSQL: The rowset was using optimistic concurrency and the value of a column has been changed after the containing row was last fetched or resynchronized.

Sometimes you may get the above error when dealing with openquery updates and linked servers.

More specifically:

The OLE DB provider "MSDASQL" FOR linked server
"XXXXXXXXX" could NOT UPDATE TABLE "[MSDASQL]".
The rowset was USING optimistic concurrency AND the VALUE OF
a COLUMN has been changed after the containing ROW was
LAST fetched OR resynchronized.
[SQLSTATE 42000] (Error 7343)  OLE DB provider "MSDASQL" FOR
linked server "XXXXXXXXX" returned message "Row cannot be
located for updating. Some values may have been changed since
it was last read."
.
[SQLSTATE 01000] (Error 7412).  The step failed.

A solution is to go to the SQL server that links to MySql, go to configure screen of the MySql ODBC connection in the linked server. Open Details, click on Cursor/Results tab and check the “Return matched rows instead of affected rows”.

That’s it!

Cheers.

Drupal 7: hiding/showing form api elements when a checkbox is checked or not

I was trying to make a file type element appear/disappear when a checkbox was checked or not.
When i added the ‘#states’ declaration in ‘myfile’ element it didn’t work as expected!
I had to add the ‘myfile’ element in a ‘container’ type element and add to that the ‘#states’ declaration.

  $form['want_to_upload'] = array(
        '#type' => 'checkbox',
        '#title' => t('Yes i want to upload a file'),
  );

  $form['upload_container'] = array(
        '#type' => 'container',
        '#states' => array(
            "visible" => array(
                "input[name='want_to_upload']" => array("checked" => TRUE)),
        ),
  );

  $form['upload_container']['myfile'] = array(
        '#type' => 'file',
        '#title' => t('Choose your file'),
        '#description' => t('You are allow to upload jpg, jpeg, png, gif and pdf, 5MB Max Size'),
  );

Laravel 5 – Form Validation of array of input elements

I have been using Laravel latelly. A problem that i had to overcome was using the validation mechanism of Laravel
for validating a variable number of form elements.

I made a dynamic form that someone could add many email input elements with name=’email[]’

The solution to validate each and everyone of these elements was not obvious and after some searching in the framework
the following code in the controller worked:

        $validator = Validator::make($request->all(), [
            'email' => 'array' //i declare the email as an array of elements
        ]);

        $validator->each('email', ['email']); //Here i declare the rules that will be executed for each elements in the array

        if ($validator->fails()) {
            return redirect('mycontroller/edit/'.$id)
                        ->withErrors($validator)
                        ->withInput();
        }

Cheers!

MySQL INSERT INTO ON DUPLICATE KEY UPDATE , regarding update triggers

This is to share some insight on the subject of mysql triggers in connection with INSERT INTO ON DUPLICATE KEY UPDATE statement.

When you execute an SQL UPDATE query, the update triggers are fired regardless of whether you changed a value or not in a specific row.
e.g.

UPDATE myTable SET myColumn='test' WHERE id=100;

The above query will fire the update trigger (before or after) even if ‘myColumn’ already has the value ‘test’ for row with id=100

This is not the case with INSERT INTO ON DUPLICATE KEY UPDATE query.

e.g.

INSERT INTO myTable (`id`, `myColumn`) VALUES (100, 'test') ON DUPLICATE KEY UPDATE myColumn='test';

The above query will not fire any UPDATE trigger if there is already a row with id=100 and ‘myColumn’ already has value ‘test’ !!!

Cheers.

IE11 and expression evaluation regarding Boolean

It seems that a lot of stuff changes with IE11. Some legacy code stopped working out the blue.
After some debugging it seems that some expressions that have been evaluating to true, now are being evaluated to false.

e.g.

if(document.ActiveXObject) //document.ActiveXObject is a valid function in IE 11
    alert("YES!!!");  //this never gets called in IE 11

Boolean(document.ActiveXObject) gets evaluated to true in IE 10 but in IE 11 is evaluated to false, while it is still a valid function.

Whatever, just thought this might help someone.

Cheers.