Get in touch:
01524 851 877
07718 896 553

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);
 }
 }
 ?>

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.

Resizing Images With Mogrify

Posted on Nov 23 2009

I needed to resize a few images in a directory recently and knew there would be a much more convenient method than opening gimp on each image, resizing etc etc.

A quick google search and I came across the ‘mogrify’ command. This is part of the ImageMagick suite. This is a nice simple command that allows you to resize images from the CLI.

An example usage would be :

mogrify -resize 640x480 *.jpg
This would resize every .jpg image in the current directory to 640×480. I needed to resize them all to a fixed width and the following code achieves this.

mogrify -resize 640 *.jpg

This will definitely make my life a little easier in the future…

Twitter Website Integration

Posted on Jul 07 2009

I found an interesting article about integrating twitter with your web site. From the article:

“Web developers with social media savvy are in hot demand. Imagine what it would be like to promise your clients a web presence that is fully integrated with their Twitter strategy and makes it easier for their content to go viral on Twitter. Would be nice, no? Well — why imagine? The tools for web developers to add a touch of Twitter to any site are out there and easy to use. Trust us — your clients will love you for it!”

The site lists 10 useful ways to take advantage of twitter on your own sites. I have already started using twitter feed to send these blog posts to twitter but there are many alternative ways it can be utilised.

This list is a good compilation of tools that can be used to great effect along with twitter.

Apache Mod Rewrite And HTTP GET

Posted on May 20 2009

Just a quick post about passing the HTTP GET parameters to pages when using Apache with Mod Rewrite. Obviously if you are using a rule that redirects a number of URL’s to a single script and you try to pass GET parameters they just get tagged on to the end of it which will usually break things. You need to append the GET parameters from within the rewrite rule as shown below to get it to work properly.

RewriteRule ^(.*)/$ index.php?page=$1&%{QUERY_STRING}
Simply appending ‘&%{QUERY_STRING}’ to the end of the rule tells Mod Rewrite to append the GET parameters.

Set Up MediaWiki For No Registration And Read Only

Posted on May 07 2009

A few times recently I have been asked to set up what is basically a personal wiki for someone. I have recommended MediaWiki for it’s ease of use and the first thing I have had to do after the install is disable registrations and make the wiki read only so only the owner can edit it.

I am posting the couple of lines needed for the config file LocalSettings.php to stop me having to google them again as much as anything but I’m sure someone else can make use of this.

This line disables new registrations from the wiki :

$wgGroupPermissions['*']['createaccount'] = false;
The following line disables eidting :

$wgGroupPermissions['*']['edit'] = false;
There are many useful things that can be done to configure how users can or cannot edit pages etc and a lot of them are covered here

IE Application Dependancy

Posted on Mar 23 2009

I have just used windows for the first time in a while to play poker of all things. The application I was using needed to open a browser to direct me to a web page which is fair enough. However it decided to not bother checking what my default browser was and loaded IE instead. Now I am a passionate hater of IE for web usage, I much prefer Firefox, that is my preference etc etc.

After seeing this I had a chat with a few friends that use windows on a regular basis and they said it annoys them as well that some apps ignore their default browser settings and use IE for any web based interactivity. This seems like stupid behavior to me, can application developers just not be bothered to do the registry check and load the default browser nowadays?

Is this a part of the whole MS monopoly on the browser market? If application developers find it so much easier to just work in all their web based interaction in an application via IE because it is the default browser should something not be done to either change the way this is implemented in windows, or, more likele, should something not be done to change the mindset of developers??