[Webinar] Learn how to a build a cloud-first strategyRegister Now


How do implement this plugin in Joomla?

Posted on 2014-08-18
Medium Priority
Last Modified: 2014-08-20
Hi all,

I have a Ubuntu 14.04 server with Varnish 4 installed and I have finally managed to get Wordpress working on it, with the especially tricky part being the backend logging in. Instead with my joomla sites I am still having some difficulty logging in the back end. The front end is working fine so no problems there but the back end, even with the right combination of login/password just does not work. I found an article on the net that might solve my problem but I don't really know how to implement it: http://www.richardbutterworth.co.uk/blog/15-varnish-and-joomla

This is an excerpt:
So, firstly there is a Joomla plug-in to set/unset the cookie, as follows...

class  plgSystemVarnish extends JPlugin
  function onAfterRoute( )
    $user = JFactory::getUser( );
    $option = JRequest::getVar( 'option' );
    if( !$user->guest || $option == 'com_users' )
      setcookie( 'varnishbypass', 'True', time( ) + 604800, '/' );
      setcookie( 'varnishbypass', '', time( ) - 604800, '/' );
  // setting the cookie in the afterRoute event is probably adequate,
  // but in an outburst of belt and braces I decided to set/unset the
  // cookie on the log in and out events too. Probably unnecessary,
  // but apparently harmless.
  function onUserLogin( $response )
    setcookie( 'varnishbypass', 'True', time( ) + 604800, '/' );
  function onUserLogout( $response )
    setcookie( 'varnishbypass', '', time( ) - 604800, '/' );
...then an adaptation to the VCL configuration from the snipt site as follows...

  if( req.url ~ "^/administrator" || req.request == "POST" ||
        req.http.cookie ~ "varnishbypass" )
    return( pass );
...and even more Ta Da! this works and respects the users logging in an out.

Open in new window

Now the bottom part about editing the VCL configuration I understand but the top part I have no idea where to put that code in. It seems to be a plugin but how do I install it? Do I just save that code in a .php file inside the joomla "plugins/extension" and try to get Joomla installation to discover it? Or do I have to save it in an installable format for joomla installer? Or is there another way?

Any help will be greatly appreciated
Question by:badwolfff

Author Comment

ID: 40269004
Ah by the way I forgot! On the online version I can't get into the admin backed so I can't install anything... am I in the shit?
LVL 51

Accepted Solution

Steve Bink earned 2000 total points
ID: 40270269
Please see here: http://docs.joomla.org/J3.x:Creating_a_Plugin_for_Joomla

For Joomla plugins, you're normally looking for an XML file to handle the installation and parameters.  It does not look like you have that option with this, so you'll need to install it manually.  This is not difficult.

Take the class code and paste it into a new file.  Name the file Varnish.php.  The capitalization is important.  This plugin is associated with system functionality (the "System" in "class plgSystemVarnish") so that's where we'll install it.  In your website's root directory, find the directory "plugins", then the directory "system" under that.  Create a subdirectory of "system" named "varnish", and copy your new PHP file there.  Don't forget to include an empty index.html for safety (you can copy one from another directory).

Now you should be able to install the plugin.  This is much better if you can get into the administrative back-end and use the "Discover" functionality.  Otherwise, time to do it the hard way - SQL:
INSERT INTO `##_extensions` (
`name`, `type`, `element`, `folder`, 
`client_id`, `enabled`, `access`, `protected`, 
`custom_data`, `system_data`
'plg_varnish', 'plugin', 'varnish', 'system', 
0, 1, 1, 0, 
'{"name":"plg_varnish","type":"plugin","creationDate":"August 2014","author":"Give Dude Some Credit","copyright":"Copyright (C) 2014 Where Credit is Due","authorEmail":"youremail@domain.com","authorUrl":"www.authorwebsite.com","version":"3.0.0","description":"Varnish Plugin","group":""}', 
'', '');

Open in new window

A few notes:
These instructions are J3+.  The process is similar in J2.5, but you might have to tweak the SQL.
Consider making/obtaining the XML for this plugin.  It's always better to do it right.
The "manifest_cache" field is a serialized version of the standard manifest file's attributes.  See Joomla's docs on manifest files to learn more.
In the query, the table name should be corrected to use your proper table prefix.
If anything goes wrong, you can always disable/remove the plugin with the appropriate UPDATE/DELETE query:/* to disable */ UPDATE ##_extensions SET enabled=0 WHERE name='plg_varnish'; /* to remove */ DELETE FROM ##_extensions WHERE name='plg_varnish';  Don't forget to remove the directory/files if you remove the extension.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month20 days, 13 hours left to enroll

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question