Get in touch:
01524 851 877
07718 896 553

Matt's Blog


Codeigniter REST server with Tank auth

Posted on Apr 18 2014

I’ve just had the need within codeigniter to integrate basic auth in Phil Sturgeons REST server¬†with the tank auth authentication library. As it turns out it is quite a trivial task.

In application/libraries/REST_Controller.php find the following function:

protected function _check_login($username = '', $password = NULL)</pre>
 {
 if (empty($username))
 {
 return FALSE;
 }

 $auth_source = strtolower($this->config->item('auth_source'));

if ($auth_source == 'ldap')
 {
 log_message('debug', 'performing LDAP authentication for $username');
 return $this->_perform_ldap_auth($username, $password);
 }

$valid_logins = & $this->config->item('rest_valid_logins');

if ( ! array_key_exists($username, $valid_logins))
 {
 return FALSE;
 }

// If actually NULL (not empty string) then do not check it
 if ($password !== NULL AND $valid_logins[$username] != $password)
 {
 return FALSE;
 }
 return TRUE;
 }

And replace it with the following:

protected function _check_login($username = '', $password = NULL)</pre>
{
 if (empty($username))
 {
 return FALSE;
 }
 return $this->tank_auth->login($username, $password, FALSE, FALSE, TRUE);
 }

If you are not auto loading the tank auth library, be sure to do that in your constructor as well.

Twitter Bootstrap Navbar and Multiple Level Dropdowns

Posted on Jun 23 2012

Twitter bootstrap is a regular addition to many sites I make at the moment. It has some great features and is extremely simple to use. The first drawback I have found with it though is a lack of support for multiple level dropdowns. These can be quite useful on navbar’s where you have a lot of options.

Whilst doing a bit of research into this, I came across this discussion on the bootstrap github page:

https://github.com/twitter/bootstrap/issues/424

There are a few additions you can include in the bootstrap CSS file to easily enable support for multi level dropdowns. The CSS is shown below:


.nav li.dropdown ul.dropdown-menu li:HOVER ul

{

display:block;

position:absolute;

left:100%;

-webkit-border-radius: 3px;

-moz-border-radius: 3px;

border-radius: 3px;

}

&nbsp;

.nav li.dropdown ul.dropdown-menu ul

{

display: none;

float:right;

position: relative;

top: auto;

margin-top: -30px;

}

&nbsp;

.nav li.dropdown ul.dropdown-menu .dropdown-menu::before

{

content: '';

display: inline-block;

border-top: 7px solid transparent;

border-bottom: 7px solid transparent;

border-right:7px solid #CCC;

border-right-color: rgba(0, 0, 0, 0.2);

position: absolute;

top: 9px;

left: -14px;

}

&nbsp;

.nav li.dropdown ul.dropdown-menu .dropdown-menu::after

{

content: '';

display: inline-block;

border-top: 6px solid transparent;

border-bottom: 6px solid transparent;

border-right:6px solid white;

position: absolute;

top: 10px;

left: -12px;

}

Once this has been added you can easily place an extra level of dropdown in your HTML like so:


<ul class="nav">

<li class="active"><a href="#">Regular link</a></li>

<li class="dropdown" id="menu1">

<a class="dropdown-toggle" data-toggle="dropdown" href="#menu1">

Dropdown

<b class="caret"></b>

</a>

<ul class="dropdown-menu">

<li><a href="#">Action</a></li>

<li><a href="#">Another action</a></li>

<li><a href="#">Something else here</a></li>

<li class="divider"></li>

<li>

<a href="#">Separated link</a>

<ul class="dropdown-menu">

<li><a href="#">Sub Menu</a></li>

</ul>

</li>

</ul>

</li>

</ul>

&nbsp;

Thanks to all the guys on that thread who posted solutions to this. It has come in very useful.

.nav li.dropdown ul.dropdown-menu li:HOVER ul
{
display:block;
position:absolute;
left:100%;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
}
.nav li.dropdown ul.dropdown-menu ul
{
display: none;
float:right;
position: relative;
top: auto;
margin-top: -30px;
}
.nav li.dropdown ul.dropdown-menu .dropdown-menu::before
{
content: ”;
display: inline-block;
border-top: 7px solid transparent;
border-bottom: 7px solid transparent;
border-right:7px solid #CCC;
border-right-color: rgba(0, 0, 0, 0.2);
position: absolute;
top: 9px;
left: -14px;
}
.nav li.dropdown ul.dropdown-menu .dropdown-menu::after
{
content: ”;
display: inline-block;
border-top: 6px solid transparent;
border-bottom: 6px solid transparent;
border-right:6px solid white;
position: absolute;
top: 10px;
left: -12px;
}.nav li.dropdown ul.dropdown-menu li:HOVER ul 

{

display:block;

position:absolute;

left:100%;

-webkit-border-radius: 3px;

-moz-border-radius: 3px;

border-radius: 3px;

}

.nav li.dropdown ul.dropdown-menu ul

{

display: none;

float:right;

position: relative;

top: auto;

margin-top: -30px;

}

.nav li.dropdown ul.dropdown-menu .dropdown-menu::before

{

content: ”;

display: inline-block;

border-top: 7px solid transparent;

border-bottom: 7px solid transparent;

border-right:7px solid #CCC;

border-right-color: rgba(0, 0, 0, 0.2);

position: absolute;

top: 9px;

left: -14px;

}

.nav li.dropdown ul.dropdown-menu .dropdown-menu::after

{

content: ”;

display: inline-block;

border-top: 6px solid transparent;

border-bottom: 6px solid transparent;

border-right:6px solid white;

position: absolute;

top: 10px;

left: -12px;

}

Center Bullet Points On Centered UL

Posted on Jun 20 2012

If you center an unordered list within any section on an HTML page you might notice that although the text gets centered, the bullet points remain on the left side of the page. There is a quick and easy CSS fix for this:


#your_containing_div ul

{

display: inline-block;

text-align:left;

}

Nice PHP print_r Formatted Table Function

Posted on Jan 16 2012

Whilst looking through php.net I stumbled across the following function that will print out an array in a nicely formatted table. When dealing with lots of nested array something like this makes it much easier to see all of the data contained within an array.

<?php
 function print_nice($elem,$max_level=10,$print_nice_stack=array()){
 if(is_array($elem) || is_object($elem)){
 if(in_array(&$elem,$print_nice_stack,true)){
 echo "<font color=red>RECURSION</font>";
 return;
 }
 $print_nice_stack[]=&$elem;
 if($max_level<1){
 echo "<font color=red>nivel maximo alcanzado</font>";
 return;
 }
 $max_level--;
 echo "<table border=1 cellspacing=0 cellpadding=3 width=100%>";
 if(is_array($elem)){
 echo '<tr><td colspan=2  style="background-color:#333333;"><strong><font  color=white>ARRAY</font></strong></td></tr>';
 }else{
 echo '<tr><td colspan=2 style="background-color:#333333;"><strong>';
 echo '<font color=white>OBJECT Type: '.get_class($elem).'</font></strong></td></tr>';
 }
 $color=0;
 foreach($elem as $k => $v){
 if($max_level%2){
 $rgb=($color++%2)?"#888888":"#BBBBBB";
 }else{
 $rgb=($color++%2)?"#8888BB":"#BBBBFF";
 }
 echo '<tr><td valign="top" style="width:40px;background-color:'.$rgb.';">';
 echo '<strong>'.$k."</strong></td><td>";
 print_nice($v,$max_level,$print_nice_stack);
 echo "</td></tr>";
 }
 echo "</table>";
 return;
 }
 if($elem === null){
 echo "<font color=green>NULL</font>";
 }elseif($elem === 0){
 echo "0";
 }elseif($elem === true){
 echo "<font color=green>TRUE</font>";
 }elseif($elem === false){
 echo "<font color=green>FALSE</font>";
 }elseif($elem === ""){
 echo "<font color=green>EMPTY STRING</font>";
 }else{
 echo str_replace("\n","<strong><font color=red>*</font></strong><br>\n",$elem);
 }
 }
 ?>

VGA Multi Head Monitor Switching Off And On – Radeon X1300 Pro

Posted on May 27 2011

I was setting up a fourth monitor on my system today and when I finally got my xorg.conf correct the new monitor (VGA) was switching itself off and then straight back on again approximately every 30 seconds.

At first I thought it was the monitor itself so swapped it for another one but as it turns out it was s daemon called ‘upowerd’ causing the problem. Thanks to the guys over at #radeon on freenode for pointing this out.

upowerd polls the monitor every 30 seconds and on some VGA setups this causes the monitor to briefly switch off and then on again. Killing the upowerd process stops the monitor doing so but within a couple of minutes the process starts back up again. In the end I just moved the binary somewhere else so it couldn’t be started and it has cured the problem. From what I can tell upowerd not running isn’t a huge problem but time will tell if it comes back to bite me in the future…

** EDIT – Well there is a problem with not running upowerd it would seem. The PC won’t go into standby without it running. I have decided to look into sorting this out properly but in the short term I am just running upowerd before I put the machine into standby and killing it when I switch it back on. This seems to do the job OK but not an ideal long term solution…

XEmacs window height 0 too small bug and a fix

Posted on Feb 10 2011

I am an avid user of XEmacs and have recently started using it for my LaTeX editing as well after getting fed up with a few limitations of my old editor. I ran into a bug in it however that can halt usage of it rather quickly which occurs when you already have one .tex file open and try to open another file.

If you attempt this with some versions of XEmacs the open file dialog will not appear and you will see the following error:

window height 0 too small (after splitting)

There is a workaround for this bug (which hopefully will be fixed in an upcoming XEmacs release).

First you have to locate the file minibuf.elc. In Ubuntu 10.10 (with XEmacs version 21.4.22) this file is located in :

/usr/share/xemacs-21.4.22/lisp/minibuf.elc

Open this file and within it you need to search for the following string:

split-window frame-height 3

Replace the 3 in that line with a 5. After this open your init.el file. This is usually in ~/.xemacs – In this file add the following line:

(load-library "minibuf.elc")

Et Voila! After you have done all this you should be able to get the open file dialog to appear.

Image File Conversion Script

Posted on Jan 12 2011

Here’s a nice simple script to convert all the pdf files in a directory to jpg files using the convert program. To use this you will need ImageMagick installed on your Linux/Unix host.

#!/bin/bash

for file in `ls *.pdf`
do
   convert $file `echo $file | sed 's/\.pdf$/\.jpg/'`
done

Ubuntu On Lenovo Thinkpad X201 (Blank Screen Problem)

Posted on Apr 26 2010

Just got myself one of these and had a few problems getting Ubuntu to even boot from a USB stick on it. Although it sounded like everything was going smoothly the screen was blank for some reason.

A quick google about brought up this bug:

Launchpad Bug Report

The solution to the problem is in comment #8 on there. Basically you need to use the following boot option to get the display working correctly

xforcevesa i915.modeset=0
Just stick this in at the bootloader and it should be fine. When Ubuntu is fully installed just put the same line into /etc/default/grub to make the change permanent.

Resizing An Image With PHP

Posted on Apr 08 2010

I came across this PHP class today that is great for resizing images. It is extremely easy to use and quite a small class. The class file can be downloaded here:

SimpleImage.php

A quick example usage to show how simple it is…

include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->resize(250,400);
$image->save('picture2.jpg');

The last time I had to do some image resizing with PHP was a number of years ago and I seem to remember it being a lot more complicated than this.

Thanks to White Hat Web Design for making this little class and making it available.

OpenWRT default blocking of ICMP

Posted on Jan 29 2010

I have been setting up an OpenWRT router at home specifically because I wanted to use it as an end point for a Hurrican Electric IPv6 Tunnel. I need this so I can do some tests using my SyntaxHighlighter.autoloader( 'applescript http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushAppleScript.js', 'actionscript3 as3 http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushAS3.js', 'bash shell http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushBash.js', 'coldfusion cf http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushColdFusion.js', 'cpp c http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushCpp.js', 'c# c-sharp csharp http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushCSharp.js', 'css http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushCss.js', 'delphi pascal http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushDelphi.js', 'diff patch pas http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushDiff.js', 'erl erlang http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushErlang.js', 'groovy http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushGroovy.js', 'hive http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushHive.js', 'java http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushJava.js', 'jfx javafx http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushJavaFX.js', 'js jscript javascript http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushJScript.js', 'objc obj-c http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushObjectiveC.js', 'perl pl http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushPerl.js', 'php http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushPhp.js', 'pig http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushPig.js', 'text plain http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushPlain.js', 'py python http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushPython.js', 'ruby rails ror rb http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushRuby.js', 'sass scss http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushSass.js', 'scala http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushScala.js', 'sql http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushSql.js', 'vb vbnet http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushVb.js', 'xml xhtml xslt html http://evolution-systems.co.uk/wp-content/plugins/syntax-highlighter-mt/scripts/shBrushXml.js' ); SyntaxHighlighter.all();