Google Analytics for WordPress – multisite not sufficient permissions solution

It is one of those times that you relive all that has passed.

I came across my own solution i had shared with the community back then, more than 2 years ago.
http://wordpress.org/support/topic/plugin-google-analytics-for-wordpress-problem-and-solution-with-multisite-network-enabled-site

Time flies and time mosquitoes.

The solution is you need to edit the file wp-content/plugins/google-analytics-for-wordpress/admin/yst_plugin_tools.php at line 16

and replace the line

var $accesslvl = 'edit_users';

with

var $accesslvl = 'activate_plugins';

Cheers

jQuery UI selectable, how to programmatically select option

If you need to programmatically select an option, using events, so that your callbacks are called then you can do the following:
Suppose we have a ul element with id mySelectUlID, and i want to add an extra element to the end of it and also select it.

This is done like this:

    $('#mySelectUlID.ui-selected').addClass('ui-unselecting').removeClass('ui-selected');
    $('#mySelectUlID').append('<li class="ui-widget-content ui-selectee ui-selecting">another</li>');

    $('#mySelectUlID').selectable("refresh");

    $('#mySelectUlID').data("ui-selectable")._mouseStop(null);

The code is valid for the latest jQuery 1.10 and is self explaining i believe.

Cheers.

Jquery Tools library problem with modal overlay not appearing … a solution

So i was using the library of jquery tools recently and wanted to use the modal box with the nice overlay in the background.

I did all that was suggested yet the overlay didn’t appear. The solution was simple. I had downloaded a custom version of the jquery tools library and in an effort to save some bytes didn’t include a crucial part of the library named Expose.

The overlay is based on the ‘Expose’ part of the library. If you take a carefull look a the modal box documentation it is mentioned: ‘This feature can be achieved with the Expose tool which is tightly integrated with this overlay tool.

If you want a custom version of jquery tools, don’t forget to click on Expose.

That’s all for now.

Cheers.

Problem with Varnish(2.1). Drupal’s pages aren’t cached by varnish.

I had a problem with varnish, trying to boost a drupal site by caching it. I noticed that while js, css and image files were getting cached, no other page could be cached by varnish and on each request the apache was asked to fetch the page.

That was not acceptable since the site isn’t updated really often. I found out that the reason why varnish didn’t cache is drupal’s (v7) file:
/var/www/drupal/includes/bootstrap.inc , line 1283

  'Expires' => 'Sun, 19 Nov 1978 05:00:00 GMT',

I didn’t want to just comment it out in the file, since a lot more problems could be faced regarding drupal and varnish and caching and updating drupal core later on etc.

A solution was to add the following lines in the varnish config file vcl_fetch :

if(beresp.http.Content-Type ~ "text/html") {
        set beresp.ttl = 1d;
    }

So now all pages are cached for 1 day and then the cache gets updated again when someone requests a page.

Cheers.

PS 1: I followed the instructions at https://drupal.org/node/1054886 and the documentation of varnish.
PS 2: A nice command to check if any page is requested from apache and is not in varnish cache is this:

varnishtop -b -i txurl

IE9 DOMParser is missing method selectSingleNode, selectNodes (and possibly others too …), a solution … (XMLSerializer missing methods also)

When you have browsers which have the DOMParser (like chrome, firefox etc.)
you can do the following in javascript:

var dom = (new DOMParser).parseFromString(str, "text/xml");
dom.selectSingleNode(...);
//etc.

In IE9 although DOMParser is available it seems that it lacks some methods, so you get errors like:
Object doesn’t support property or method ‘selectSingleNode’

The solution you can use in javascript level is something like that:

var dom = new ActiveXObject("Microsoft.XMLDOM");
dom.loadXML(str);
dom.selectSingleNode(...);
//etc.

To all of you who are using sarissa.js and you face the same problems then you need a little code tweaking for it to work. I will share what i did and worked for me. I don’t know if some other parts of the library require also fixing…

in sarissa.js at line 474 (file version 0.9.9.5) replace the line:

if(!window.DOMParser){

with:

if(!window.DOMParser || navigator.userAgent.indexOf("MSIE 9")>-1){

PS. In addition the case with XMLSerializer is the same like DOMParser. So in sarissa.js change the line 596 from:

if(!window.XMLSerializer && Sarissa.getDomDocument && Sarissa.getDomDocument("","foo", null).xml){

to:

if((!window.XMLSerializer && Sarissa.getDomDocument && Sarissa.getDomDocument("","foo", null).xml) || navigator.userAgent.indexOf("MSIE 9")>-1){

That’s all for now.
Cheers.

curl : How to set PHPSESSID in the request headers of a call, how to solve waiting and getting no response

I have been trying to use curl in PHP to post some data in some page of mine that requires login and thus a session to be present.
So i had to send the session along with the curl request. The problem was that the script seemed to be waiting for a response.

What happened actually was that my target script was waiting for the session to become available.
As is stated in session_write_close():

“session data is locked to prevent concurrent writes only one script may operate on a session at any time”

So my target script was waiting for the session to become writable while the current script (that made the post ) couldn’t complete because it was waiting the response of the target (cruel things the deadlocks). The solution is to call session_write_close() and free the session before you make the call curl_exec()

That’s it. Take a look at the following code. Take care.

Cheers.

<?php
    session_start();
     
    ...
    ...

    $ch = curl_init();
    if(!$ch){
        die('Failed to init curl ...');
    }

    curl_setopt($ch, CURLOPT_URL,            "https://example.org/subdir/something.php" );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($ch, CURLOPT_POST,           1 );
    curl_setopt($ch, CURLOPT_POSTFIELDS,     $raw_data );
    curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: text/xml')); //remove this line if you want submit text/html
   
    $strCookie = 'PHPSESSID=' . session_id() . '; path=/';
    curl_setopt( $ch, CURLOPT_COOKIE, $strCookie ); //We set our session in the headers of the request!

    session_write_close(); //kmak - this is what makes the session transmission possible ... otherwise we will wait and wait ...
    $result=curl_exec ($ch);
    if(!$result)
    {
        die("Failed in curl_exec with error:".curl_error($ch));
    }
    curl_close($ch);

Drupal custom script and problem with file_copy, file_save, file_move and generally file functions.

I have recently been occupied with developing a custom script that uses drupal 7 api in order to programmatically create content nodes.
For all these i have found this very usefull post.

My nodes had to have many attachements so i was really happy that i had this code.

NOTE: i was executing my script by hand from a command line like this:

$php myscript.php

Despite doing everything correctly i couldn’t make file_copy() to succeed.

To make a long story short, the solution to this was to place my custom script in the root directory where my drupal site was running and then run it by hand again.
For security reasons of course i had to change the permissions to my script accordingly so that it couldn’t be executed from the web server but only by command line.

To make a short story long, the reason for the script not working originally had to do with drupal’s public:// stream handler producing relative paths
to the drupal installation directory, thus causing various checks like php’s is_dir(“public://”) functions to fail miserably when run from other place.

That was it.

Cheers!

Ubuntu 11.10 – how to remove unity / solve boot freeze / solve problem opening folders

I have been troubled for hours by some problems, so now i share something with you, so that you have an easier life.
In short if you want to remove unity in Ubuntu 11.10 remove all packages named *unity* from the ‘Ubuntu Software Center’
except: libunity6 and unity-greeter !!!!

If you use lightdm for login screen and you remove unity-greeter then at boot time your system will freeze!
If you use gdm then you can remove unity-greeter without problem.

In addition if you remove libunity6 then nautilus is also removed because it has it as a dependency.
Nautilus is the program that is responsible for opening your folders and showing you their contents when you click on them and generally it’s like the explorer in windows systems.

Cheers.

PS. Just in case you are stuck in command line and you are a novice user in ubuntu just make sure you install again the removed packages:

$sudo apt-get install unity-greeter
$sudo apt-get install nautilus