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.

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