double you double you double you dot lifted pixel dot net

www.liftedpixel.net

Storing URLs

24th Nov, 2014 | Back


This is just going to be a mess because I'm really just taking notes. I might clean it up into a readable blog post later. Also, my blog software can use 'markup', so sometimes there are characters missing because it thinks I mean to italicize something. I'll figure out how to disable that at some point, because I never use markup.

The goal here is to create a table in my OpenSim database that will hold info on the URLs that I've requested through llRequestURL. Then I'll make a PHP script that spits out the URLs that are currently being used and have an option to delete URLs that are no longer being used. If I can figure out how to add my own scripting extensions, maybe I can make an actual script command that saves the URL when it requests it.

First, I made a table in my OpenSim database. Normally I use an external webhost for this sort of thing, but I figured I could be a big girl and mess with my actual database for once. Here's the MySQL code:

use opensim;
create table ppurls
(id int not null autoincrement,
url varchar(64),
objectuuid varchar(64),
created timestamp,
primary key (id));
describe ppurls;

That gives me this:

mysql> describe ppurls;
+------------+-------------+------+-----+-------------------+-----------------------------+
| Field      | Type        | Null | Key | Default           | Extra                       |
+------------+-------------+------+-----+-------------------+-----------------------------+
| id         | int(11)     | NO   | PRI | NULL              | autoincrement              |
| url        | varchar(64) | YES  |     | NULL              |                             |
| objectuuid | varchar(64) | YES  |     | NULL              |                             |
| created    | timestamp   | NO   |     | CURRENTTIMESTAMP | on update CURRENTTIMESTAMP |
+------------+-------------+------+-----+-------------------+-----------------------------+
4 rows in set (0.00 sec)

Wrote an LSL script that gets a URL and sends it via GET to a PHP script that writes the info in the database. Here's the LSL:

string URL;
key HttpRequest;

WriteURL ( string TheURL ) { // Location of PHP script string ScriptAddress = "http://liftedpixel.net/pixelplanet/addppurl.php"; // Minimal security string PASS = "xxxxx";

// We're gunna use get
list Params = [ HTTP_METHOD, "GET" ];
// Get just the UUID part of the URL
string xURL = llGetSubString ( TheURL, -37, -2 );
// Get the UUID of the object containing the script
string objectuuid = llGetKey ();
// Put the address together
string Address = ScriptAddress + "?url=" + xURL + "&objectuuid=" + objectuuid + "&PASS=" + PASS;

// Make the request
HttpRequest = llHTTPRequest ( Address, Params, "" );

}

default { touchstart ( integer numdetected ) { llRequestURL (); }

http_request ( key id, string method, string body )
{
    if ( method == URL_REQUEST_GRANTED )
    {
        URL = body;
        llSay ( PUBLIC_CHANNEL, "My URL: " + URL );

        WriteURL ( URL );
    }

    else if ( method == "GET" )
    {
        llHTTPResponse ( id, 200, "Testing 123" );
    }
}

http_response ( key request_id, integer status, list metadata, string body )
{
    if ( request_id == HttpRequest )
    {
        llSay ( PUBLIC_CHANNEL, body );
    }
}

}


And here's the PHP:

<?php

$url = $GET["url"]; $objectuuid = $GET["objectuuid"]; $PASS = $_GET["PASS"];

$thePASS = "xxxxx";

include "connectwrite.php";

if ( $PASS == $thePASS ) { $query = "INSERT INTO ppurls ( url, objectuuid ) VALUES ( '$url', '$objectuuid' )";

if ( !mysqli_query( $connect, $query ) )
{
    die( 'Error: ' . mysqli_error( $connect ) );
}

echo "URL added...";

}

else { echo "Password incorrect..."; } ?>


And it puts the info into the database like so:

mysql> select * from ppurls;
+----+--------------------------------------+--------------------------------------+---------------------+
| id | url                                  | objectuuid                           | created             |
+----+--------------------------------------+--------------------------------------+---------------------+
|  1 | eb18b03a-c437-4869-9392-057222c18aac | 918212aa-518a-417f-8b14-63986a3f0267 | 2014-11-24 16:03:41 |
|  2 | 1db290c7-0c71-452f-84e3-33e654c15e94 | 918212aa-518a-417f-8b14-63986a3f0267 | 2014-11-24 16:08:19 |
+----+--------------------------------------+--------------------------------------+---------------------+
2 rows in set (0.00 sec)

So far, so good. Now to make a page that displays the URLs with an option to delete stale ones. The PHP part is this:
<?php
include "connect.php";

$sqlCommand = "SELECT * FROM ppurls"; $query = mysqliquery($connect, $sqlCommand) or die (mysqlierror());

while ($row = mysqlifetchassoc($query)) { // Get the info $id = $row['id']; $url = $row['url']; $objectuuid = $row['objectuuid']; $timestamp = $row['created'];

// Spit out the results
echo "<div class='well well-sm'>";
echo "<h3>Object Name: x</h3><p>Entry ID: $id</p><p>ObjectUUID: $objectuuid</p><p>URL: <a href='http://os.liftedpixel.net:9000/lslhttp/$url/'>$url</a></p><p>Created: $timestamp</p>";
echo "<form action='deleteppurl.php' method='post' class='form-inline' role='form'><input name='id' type='hidden' value='$id'><input name='password' type='text' class='form-control input-sm' placeholder='Password'> <input type='submit' value='Delete' class='btn btn-sm btn-danger'></form>";
echo "</div>";

} // close while statement ?>


And to delete from the database, it references this PHP script:


<?php

$id = $POST["id"]; $pass = $POST["password"];

$thePASS = "xxxxx";

include "connectwrite.php";

if ( $pass == $thePASS ) { $query = "DELETE FROM ppurls WHERE id = $id";

if ( !mysqli_query( $connect, $query ) )
{
    die( 'Error: ' . mysqli_error( $connect ) );
}

echo "URL deleted.";

}

else { echo "Password incorrect."; }

echo "
Returning in a few seconds..."; sleep (5); ?> [script] window.location = "http://liftedpixel.net/pixelplanet/displayppurl.php"; [/script]


When I'm feeling up to it, I'll write a join that grabs the objects name from the asset table based on it's UUID. For now, I just put an X. This project took me about 2 hours to put together. I'm going to upload all the scripts to GitHub now so they're easier to look at.

Here's the GitHub link: https://github.com/liftedpixel/lslscripts/tree/master/PPURLs

Top | Back