PHP Function to Read Abyss's Users.

 
Post new topic   Reply to topic    Aprelium Forum Index -> Off Topic Discussions
View previous topic :: View next topic  
Author Message
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3720
Location: USA, GA

PostPosted: Thu Nov 09, 2006 11:19 pm    Post subject: PHP Function to Read Abyss's Users. Reply with quote

Hello Everyone,

I've been learning some SimpleXML last night and decided to share some of
my work with the Abyss Web Server community. I've written a function that will
read your "abyss.conf" and output all User Accounts per Host.

This is very helpful if you wish to read abyss.conf and use those accounts with
your own PHP scripts. My function creates an Associated Array with every single
User Account you add per Host. Enjoy!

Note: PHP5 is required in order to use this function.

Code:
<pre>
<?php
/**
 * The abyss_user_accounts() Function
 * Created by: Josh (TRUSTAbyss)
 */

function abyss_user_accounts($file, $host_id) {

    $xml = simplexml_load_file($file);
   
    $output = array();
       
    if (count($xml->server->host[$host_id]->users->user) > 0) {
       
        foreach ($xml->server->host[$host_id]->users->user as $account) {

            $user = trim((string) $account->name);
            $pass = trim((string) $account->password);
   
            $output["user"][] = $user;
            $output["pass"][] = $pass;
        }
           
        return $output;
    }
    else {
       
        return FALSE;
    }
}

// Add the path to your abyss.conf file.
$file = "C:\\Program Files\\Abyss Web Server\\abyss.conf";

// Let's lookup the current Host so we know which Users to Pull.
$host_id = $_SERVER['INSTANCE_ID'] - 1;

// This just assigns $output variable, the array of the accounts
$output = @abyss_user_accounts($file, $host_id);

// Last but not least, output it!
print_r($output);

// Let's retrieve the first User Account. This is easy! lol

echo "<br>";
echo "Username: " . $output['user'][0] . "\n";
echo "Password: " . $output['pass'][0];
?>
</pre>


Edit: To create an Abyss Web Server user account. Use this code.

Code:
<?php
function create_abyss_password($user, $pass) {

    return md5(base64_encode("$user:$pass"));
}

// Let's create an Abyss Web Server password
// Syntax: abyss_create_password("Username", "Password")

$user = "Josh"; // Username to use
$pass = "password here"; // User's Password in plain Text

echo "Username ".$user." has the password ".abyss_create_password($user, $pass);
?>


Sincerely, Josh (TRUSTAbyss)
_________________
Computer Programmer & Networking Specialist



Last edited by TRUSTAbyss on Mon Aug 04, 2008 9:36 pm; edited 6 times in total
Back to top View user's profile Send private message Visit poster's website MSN Messenger
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Fri Nov 10, 2006 12:20 am    Post subject: Re: PHP Function to Read Abyss's Users. Reply with quote

TRUSTAbyss wrote:
Hello Everyone,

I've been learning some SimpleXML last night and decided to share some of
my work with the Abyss Web Server community. I've written a function that will
read your "abyss.conf" and output all User Accounts per Host.

This is very helpful if you wish to read abyss.conf and use those accounts with
your own PHP scripts. My function creates an Associated Array with every single
User Account you add per Host. Enjoy!

Note: PHP5 is required in order to use this function.


If you want I could write a similar function using php 4.
But I don't have abyss x2 so I would need to know how the config file is laid out
Code:
<root>
 <server>
  <host>
   <users>
    <user />
   </users>
  </host>
 </server>
</root>

how is multiple hosts defined?
_________________
Anthony R

Roganty
| Links-Links.co.uk
Back to top View user's profile Send private message Visit poster's website
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3720
Location: USA, GA

PostPosted: Fri Nov 10, 2006 12:24 am    Post subject: Reply with quote

It's layed out the same way. The Virtual Host is known by its id tag. I will be
more than happy to test your similar function anyway.

Edit: My function can now be used in Controle Structures easier. You can now
write code like this.

Code:
<?php
$user = "josh";

if ($output['user'][0] == $user) {
    echo "Your username is already in abyss.conf";
}
?>

_________________
Computer Programmer & Networking Specialist



Last edited by TRUSTAbyss on Fri Nov 10, 2006 12:50 am; edited 1 time in total
Back to top View user's profile Send private message Visit poster's website MSN Messenger
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Fri Nov 10, 2006 12:49 am    Post subject: Reply with quote

TRUSTAbyss wrote:
It's layed out the same way. The Virtual Host is known by its id tag. I will be
more than happy to test your similar function anyway.


More clarification needed!

by id tag do you mean
Code:
<host id="1"></host>


or
Code:
<host>
<id>1</id>
</host>

_________________
Anthony R

Roganty
| Links-Links.co.uk
Back to top View user's profile Send private message Visit poster's website
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3720
Location: USA, GA

PostPosted: Fri Nov 10, 2006 12:50 am    Post subject: Reply with quote

I mean
Code:
<id></id>

_________________
Computer Programmer & Networking Specialist

Back to top View user's profile Send private message Visit poster's website MSN Messenger
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Fri Nov 10, 2006 12:55 am    Post subject: Reply with quote

TRUSTAbyss wrote:
I mean
Code:
<id></id>


Thanks

It'll be ready for tomorrow
_________________
Anthony R

Roganty
| Links-Links.co.uk
Back to top View user's profile Send private message Visit poster's website
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3720
Location: USA, GA

PostPosted: Fri Nov 10, 2006 12:58 am    Post subject: Reply with quote

I'll be looking forward to it. Good Luck! :-)
_________________
Computer Programmer & Networking Specialist

Back to top View user's profile Send private message Visit poster's website MSN Messenger
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Fri Nov 10, 2006 2:00 am    Post subject: Reply with quote

Ok, its done. (Its called code recycling ;) )

But I want to make absolute certain about the format of the abyss.conf file for x2
Code:
<?xml version="1.0" encoding="UTF-8"?>
<root>
 <server>
  <host>
   <id>1</id>
   <users>
    <user>user1</user>
    <password>pass1</password>
    <user>user2</user>
    <password>pass2</password>
    <user>user3</user>
    <password>pass3</password>
   </users>
  </host>
  <host>
   <id>2</id>
   <users>
    <user>user4</user>
    <password>pass4</password>
    <user>user5</user>
    <password>pass5</password>
    <user>user6</user>
    <password>pass6</password>
   </users>
  </host>
 </server>
</root>


This is my dummy abyss.conf file I'm using to test my script

Is that how users and their passwords are set out?
Is that how hosts are set out?
_________________
Anthony R

Roganty
| Links-Links.co.uk
Back to top View user's profile Send private message Visit poster's website
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3720
Location: USA, GA

PostPosted: Fri Nov 10, 2006 2:36 am    Post subject: Reply with quote

No. This is how it looks.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<root>
 <server>
  <host>
   <id>1</id>
   <users>
    <user>
    <name>user1</name>
    <password>pass1</password>
    </user>
    <user>
    <name>user2</name>
    <password>pass2</password>
    </user>
   </users>
  </host>
  <host>
   <id>2</id>
   <users>
    <user>
    <name>user1</name>
    <password>pass1</password>
    </user>
    <user>
    <name>user2</name>
    <password>pass2</password>
    </user>
   </users>
  </host>
 </server>
</root>

_________________
Computer Programmer & Networking Specialist

Back to top View user's profile Send private message Visit poster's website MSN Messenger
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Fri Nov 10, 2006 11:48 am    Post subject: Code at last!!! Reply with quote

Thanks TRUSTAbyss
Here is the long awaited code

Code:
<?php echo("The updated code can be found below!") ?>


Some notes:
use -1 for the host id to get a list of all users
more functionality can be added quite easily as the whole user list can be accessed
_________________
Anthony R

Roganty
| Links-Links.co.uk


Last edited by roganty on Fri Nov 10, 2006 11:43 pm; edited 1 time in total
Back to top View user's profile Send private message Visit poster's website
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3720
Location: USA, GA

PostPosted: Fri Nov 10, 2006 12:03 pm    Post subject: Reply with quote

Sorry, I just tested it and it doesn't work. What XML format are you using to
parse the XML? Are you using the format I showed you?
_________________
Computer Programmer & Networking Specialist

Back to top View user's profile Send private message Visit poster's website MSN Messenger
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Fri Nov 10, 2006 12:20 pm    Post subject: Reply with quote

TRUSTAbyss wrote:
Sorry, I just tested it and it doesn't work. What XML format are you using to
parse the XML? Are you using the format I showed you?


What isn't working?
Are there any error messages

Code:
<?xml version="1.0" encoding="UTF-8"?>
<root>
 <server>
  <host>
   <id>0</id>
   <users>
    <user>
     <name>user1</name>
     <password>pass1</password>
    </user>
    <user>
     <name>user2</name>
     <password>pass2</password>
    </user>
    <user>
     <name>user3</name>
     <password>pass3</password>
    </user>
   </users>
  </host>
  <host>
   <id>1</id>
   <users>
    <user>
     <name>user4</name>
     <password>pass4</password>
    </user>
    <user>
     <name>user5</name>
     <password>pass5</password>
    </user>
    <user>
     <name>user6</name>
     <password>pass6</password>
    </user>
   </users>
  </host>
  <host>
   <id>2</id>
  </host>
 </server>
</root>


That is the test file that I am using

Does the code at the bottom looks like the following?

Code:
// Add the path to your abyss.conf file.
$file = "/home/anthony/projects/abyss.conf"; //ok, you will need to change this

$host_id = -1; //keep as -1

$otpt = new abyss_user_accounts($file);

$output = $otpt->abyss_user_accounts_for_host($host_id);

print "<pre>" .print_r($output, true). "</pre>";


on my computer the above gives me
Quote:
Array
(
[0] => Array
(
[user1] => pass1
[user2] => pass2
[user3] => pass3
)

[1] => Array
(
[user4] => pass4
[user5] => pass5
[user6] => pass6
)

[2] => Array
(
)

)


Try it with the test file, see if that works
_________________
Anthony R

Roganty
| Links-Links.co.uk
Back to top View user's profile Send private message Visit poster's website
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3720
Location: USA, GA

PostPosted: Fri Nov 10, 2006 12:25 pm    Post subject: Reply with quote

The test file works. Maybe I missed something when I told you the format. I will
send you an "abyss.conf" with dummy hosts in a little while.
_________________
Computer Programmer & Networking Specialist

Back to top View user's profile Send private message Visit poster's website MSN Messenger
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Fri Nov 10, 2006 12:28 pm    Post subject: Reply with quote

TRUSTAbyss wrote:
The test file works. Maybe I missed something when I told you the format. I will
send you an "abyss.conf" with dummy hosts in a little while.


Okay, thanks.
_________________
Anthony R

Roganty
| Links-Links.co.uk
Back to top View user's profile Send private message Visit poster's website
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3720
Location: USA, GA

PostPosted: Fri Nov 10, 2006 12:37 pm    Post subject: Reply with quote

The "abyss.conf" file is sent. Check your "Private Messages" inbox.
_________________
Computer Programmer & Networking Specialist

Back to top View user's profile Send private message Visit poster's website MSN Messenger
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Fri Nov 10, 2006 1:20 pm    Post subject: Reply with quote

TRUSTAbyss wrote:
The "abyss.conf" file is sent. Check your "Private Messages" inbox.


Thanks for the file, that is just what I needed!

I've had a look through it, and I have spotted the areas where my script was falling down.

  • The <root> tag is used in more than one place, for example, root->server->parameters->root
  • The <name> tag is also used in more than one place,for example, root->host->names->name and root->host->users->user->name
  • The <id> tag comes after the <users> tag

I've got to go to work now, but I will work on it tonight when I come home
_________________
Anthony R

Roganty
| Links-Links.co.uk
Back to top View user's profile Send private message Visit poster's website
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Fri Nov 10, 2006 11:42 pm    Post subject: The wait is over - finally! Reply with quote

Ok, here is the new code
Code:
<?php

class abyss_user_accounts{
 
 var $XMLfile = "";
 var $XMLparser;
 var $tag_is = array();
 
 var $hosts_output = array();
 
 function abyss_user_accounts($file){
  $this->output = array();
  $this->XMLfile = $file;
 
  $this->Generate($this->XMLfile);
 }
 
 function abyss_user_accounts_for_host($host_id=-1){
  //print "<pre>" .print_r($this->hosts_output, true). "</pre>";
  if( $host_id == -1 ) return $this->hosts_output;
  else{
   if( isset($this->hosts_output[$host_id]) ){
    if( empty($this->hosts_output[$host_id]) ) return "There are no users for host id #$host_id";
    else return $this->hosts_output[$host_id];
   }else return "There is no host with an id of $host_id";
  }
 }
 
 function Generate($XMLfile){
  if( $fp = @fopen($XMLfile,"r")){
 
   $this->XMLparser = xml_parser_create();
   
   xml_set_object($this->XMLparser, $this);
   xml_parser_set_option($this->XMLparser, XML_OPTION_CASE_FOLDING, 0);
   xml_parser_set_option($this->XMLparser, XML_OPTION_SKIP_WHITE, 1);
   xml_set_element_handler($this->XMLparser, array(&$this, "startElement"), array(&$this, "endElement"));
   xml_set_character_data_handler($this->XMLparser, array(&$this, "characterData"));
   
   while( $data = fread($fp, 4096)){
    if(! xml_parse($this->XMLparser, $data, feof($fp)) ){
     die( sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->XMLparser)), xml_get_current_line_number($this->XMLparser)));
    }
   }
   
   xml_parser_free($this->XMLparser);
   
  }else{
   print "<br /><b>ERROR</b>: Could not open XML file ". $XMLfile ."<br />";
  }
 }
 
 function startElement($parser, $name, $attrs){
  $attnames = array_keys($attrs);
  $tag_is = $this->tag_is;
  //print ":".$name."<br />";
 
  switch( $name ){
   case "root" : if( empty($tag_is) ) $tag_is = array("root"); break;
   
   case "server" : $tag_is[1] ="server"; break;
   case "host" : $tag_is[2] = "host"; $this->add2output = false; break;
   
   case "id" : $tag_is[3] = "id"; break;
   case "users" : $tag_is[3] = "users"; $this->tmp_usr_arr = array(); break;
   
   case "user" : $tag_is[4] = "user"; break;
   case "name" : if( $tag_is[4] == "user" ) $tag_is[5] = "name"; break;
   case "password" : if( $tag_is[4] == "user" ) $tag_is[5] = "password"; break;
  }
 
  $this->tag_is = $tag_is;
 
 }
 
 function endElement($parser, $name){
  $tag_is = $this->tag_is;
 
  switch( $name ){
   case "root" : if( $tag_is[1] != "server" ) $tag_is = array(); break;
   
   case "server" : $tag_is[1] =""; break;
   case "host" : $tag_is[2] = ""; break;
   
   case "id" : $tag_is[3] = ""; break;
   case "users" : $tag_is[3] = ""; break;
   
   case "user" : $tag_is[4] = ""; break;
   case "name" : if( $tag_is[4] == "user" ) $tag_is[5] = ""; break;
   case "password" : if( $tag_is[4] == "user" ) $tag_is[5] = ""; break;
  }
 
  $this->tag_is = $tag_is;
 
 }
 
 function characterData($parser, $data){
  $data = trim($data);
  $tag_is = $this->tag_is;
  $hosts_output = $this->hosts_output;
 
  if(! empty($data) )
   if( $tag_is[0] == "root" )
    if( $tag_is[1] == "server" )
     if( $tag_is[2] == "host" )
      if( $tag_is[3] == "users" ){
       if( $tag_is[4] == "user" ){
        if( $tag_is[5] == "name" ) $this->tmp_usr = $data;
        else
        if( $tag_is[5] == "password" ){
         if( $this->add2output ) $hosts_output[$this->tmp_host_id][$this->tmp_usr] = $data;
         else
         if(! $this->add2output ) $this->tmp_usr_arr[$this->tmp_usr] = $data;
        }
       }
      }else
      if( $tag_is[3] == "id" ){
       $this->tmp_host_id = $data;
       if( empty($this->tmp_usr_arr) ){
        $hosts_output[$this->tmp_host_id] = array();
        $this->add2output = true;
       }else{
        $hosts_output[$this->tmp_host_id] = $this->tmp_usr_arr;
        $this->add2output = false;
       }
      }
 
  $this->hosts_output = $hosts_output;
 
 }
 
}

// Add the path to your abyss.conf file.
$file = "C:\\Program Files\\Abyss Web Server\\abyss.conf";
//$file = "/home/anthony/projects/abyss.conf";

// Let's lookup the current Host so we know which Users to Pull.
$host_id = $_SERVER['INSTANCE_ID'];
//$host_id = -1;

$otpt = new abyss_user_accounts($file);

// This just assigns $output variable, the array of the accounts
$output = $otpt->abyss_user_accounts_for_host($host_id);

// Last but not least, output it!
print "<pre>" .print_r($output, true). "</pre>";

?>

Some notes:
use -1 for the host id to get a list of all users
more functionality can be added quite easily as the whole user list can be accessed
_________________
Anthony R

Roganty
| Links-Links.co.uk
Back to top View user's profile Send private message Visit poster's website
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3720
Location: USA, GA

PostPosted: Sat Nov 11, 2006 12:09 am    Post subject: Reply with quote

It works! Thanks for this contribution. I'm going to try and learn from your code
to build my own XML applications for PHP4 and PHP5.
_________________
Computer Programmer & Networking Specialist

Back to top View user's profile Send private message Visit poster's website MSN Messenger
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Sat Nov 11, 2006 12:16 am    Post subject: Reply with quote

TRUSTAbyss wrote:
It works! Thanks for this contribution. I'm going to try and learn from your code
to build my own XML applications for PHP4 and PHP5.


Don't be so surprised that it works! (mock indignation)

When ever I need to do XML parsing, I always use the same function - Generate()

A good place to start is at the following page:
http://uk.php.net/manual/en/function.xml-parser-create.php
_________________
Anthony R

Roganty
| Links-Links.co.uk
Back to top View user's profile Send private message Visit poster's website
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3720
Location: USA, GA

PostPosted: Mon Aug 04, 2008 9:39 pm    Post subject: Reply with quote

The abyss_user_accounts() function has been updated for better performance. Basically, I added a new function to load the XML in a better way instead of turning the whole XML file into a string followed by an object. This new code should provide better performance.
_________________
Computer Programmer & Networking Specialist

Back to top View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> Off Topic Discussions All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group