News:

CPG Release 1.6.28
added submissions from {406man}
cleaned up a few PHP (8.4) deprecations
fixed PHP deprecation in calendar
removed security vulnerability
(please upgrade when possible)

Main Menu

another question (regarding user album urls)

Started by Oasis, September 25, 2003, 05:38:01 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Oasis

Hi..
I am trying to use coppermine as a Yahoo Photo style online album for students in our university. However, the urls for the users' personal albums are hard if not impossible to remember... Is there a way to make a simpler url for users' albums using just their usernames and not their userid numbers? for example instead of

http://photo.enctu.org/index.php?cat=10002
I would like it to be accessible by http://photo.enctu.org/album/oasis

any help would be appreciated
Pixnet Gallery: http://www.pixnet.net
iNSiGNiA Weblog: http://www.jayliu.org

Tarique Sani

It depends on how adept you are with PHP, I will roughly outline what it will entail for having URL like http://photo.enctu.org/album/oasis

1) the album will have to be a PHP file instead of a directory - this PHP file should take the username from PATH_INFO query the database and redirect the page to correct location

2) use .htaccess to for album (even without .php extension) to be interpreted as a PHP file
SANIsoft PHP applications for E Biz

Oasis

thanks!

I can see what you're getting at... I think that would probably do, but I'm not too good at php. At most, I can do simple changes to the scripts, but I can't start from scratch...

Could anyone write such a file, or could it be added into the release?



Also, I was thinking whether it could be done with the mod_rewrite module in apache. I know rewritemap can check flatfiles but is there any way it could be tweaked to read from an sql database to match the category number to the corresponding username?
Pixnet Gallery: http://www.pixnet.net
iNSiGNiA Weblog: http://www.jayliu.org

Tarique Sani

Quote from: "oasis"Also, I was thinking whether it could be done with the mod_rewrite module in apache. I know rewritemap can check flatfiles but is there any way it could be tweaked to read from an sql database to match the category number to the corresponding username?
mod_rewrite cannot get information which is not already there in the URL

eg: http://photo.enctu.org/index/cat/10002 can be rewritten as http://photo.enctu.org/index.php?cat=10002

Nothing more
SANIsoft PHP applications for E Biz

gtroll

This type of url rewriting is used in php-nuke all the time, let me explain how the same method might be used for your need.
You have a header file with a function that rewrites the urls and include it at the top of the page.
A footer file is used to call it, which is included at the bottom of the page. And rewrite the url as proposed by tarique in your htaccess
Excerpts from my php-nuke header.php

ob_start();
function replace_for_mod_rewrite(&$s)
{
$urlin =
array(
"'(?<!/)index.php?cat=([0-9]*)'",
"'(?<!/)photos/([0-9]*)&'")
$urlout = array(
"photos/\\1",
"index.php?cat=\\1&"
);
$s = preg_replace($urlin, $urlout, $s);
return $s;
}

footer.php

$contents = ob_get_contents(); // store buffer in $contents
ob_end_clean(); // delete output buffer and stop buffering
echo replace_for_mod_rewrite($contents); //display modified buffer to screen

in htaccess

RewriteEngine On
RewriteRule ^photos/([0-9]*) index.php?cat=\\1

Now to get this to work with your user names instead of the number you'd have to write a mysql query to look up the user name for a given gallery number and have a rewrite map to each one.

moorey

Very nice.  I love mod_rewrites!

*hugs* Apache

Oasis

Quote from: "tarique"mod_rewrite cannot get information which is not already there in the URL

rewritemap can get information which is saved in a flatfile or external script. Could it be possible for this flatfile be generated on the fly by querying mysql for the corresponding username of the provided ID number?

EDIT: sorry, i have ideas but i'm not a programmer and I can't put them into code or even know whether it is possible, so forgive me if the question sounds stupid
Pixnet Gallery: http://www.pixnet.net
iNSiGNiA Weblog: http://www.jayliu.org

xarumanx

From a developers point of view, this shouldn't be a big problem, but AFAIK not much hoster support rewritemap...

<?php echo signature(); ?>
?>


gtroll

Most free hosts don't support mod_rewrite but the better budget ones do...
In reality it might be easier to rewrite the coppermine code to user the user name as the parameter than to do extensive rewrite tables with sql queries and ecetera. Your right you're beyond your coding knowledge with your questions and will probably have to rely on someone else to implement this if you do, but there are no stupid questions... (except those answered in the manual or FAQ)  :wink:

Oasis

OK unfortunately rewritemap doesn't seem to like windows, but I have managed to simplify the problem a bit.
So far I have managed to use RewriteRule to shorten the url to
http://photo.enctu.org/10002
this is my .htaccess:
QuoteRewriteEngine On
RewriteRule ^([a-z0-9&=]+)$ index.php?cat=$1
It's good enough, but is there any where I could add some code in coppermine that will let me do this:

First I will change htaccess into:
QuoteRewriteEngine On
RewriteRule ^([a-z0-9&=]+)$ index.php?username=$1
That should turn requests for: http://photo.enctu.org/oasis
into: http://photo.enctu.org/index.php?username=oasis

so when a request for http://photo.enctu.org/oasis gets sent to the server, it obviously calls index.php and gives it the username variable. I have a feeling that the username variable could then be used to query mysql for the corresponding user_id (which would be 10002) and set a variable $cat with that value, which will call the correct albums of that user.

Am I correct? And if I am, how should I continue now?
I don't know where to put the part for the query
Pixnet Gallery: http://www.pixnet.net
iNSiGNiA Weblog: http://www.jayliu.org

Tarique Sani

Quote from: "oasis"That should turn requests for: http://photo.enctu.org/oasis
into: http://photo.enctu.org/index.php?username=oasis
:idea:  http://photo.enctu.org/oasis will return a 404 - so instead of going thru all the hassle of rewrite - why not just write a custom 404 document extract the username and redirect using header ( )

Yeah it messes up the Web Server logs - but a small price I would say
SANIsoft PHP applications for E Biz

Oasis

Quotehttp://photo.enctu.org/oasis will return a 404 - so instead of going thru all the hassle of rewrite - why not just write a custom 404 document extract the username and redirect using header ( )

actually it doesn't. Try clicking on it~  :)
Now it just goes to the homepage, because cat=oasis doesn't exist. If, however you link to http://photo.enctu.org/10002 it goes straight to my album (oasis' album).. So that means that index.php IS processing the variable...... now i just need it to process it one more step... query the user_id from the username.
Pixnet Gallery: http://www.pixnet.net
iNSiGNiA Weblog: http://www.jayliu.org

Tarique Sani

Quote from: "oasis"actually it doesn't. Try clicking on it~  :)
Thats because you already got rewrite going ....
SANIsoft PHP applications for E Biz

Oasis

oh, heh... :P  I see what you mean now..

So how do I make index.php recognize the username and retrieve the corresponding user_id?

by the way, I should remind you that I will be redirecting(rewriting) the shorter url to index.php?USERNAME=XXXX instead of index.php?cat=XXXX
so the original code shouldn't have to be change much, just have some lines added to it....
Pixnet Gallery: http://www.pixnet.net
iNSiGNiA Weblog: http://www.jayliu.org

Tarique Sani

OK this is not tested out but the cat for user album is "1"+user_id padded with enough zeros to make it 4 digit

so if your user_id is 2 then the cat for you will be 10002 BUT if your user_id is 12 then the cat for you will be 10012

Also to keep the compatibility between the future versions of CPG do not write any code into index.php by just redirect to it after calculating the correct cat

IMPORTANT : Share your complete code once you finish it :)
SANIsoft PHP applications for E Biz

Oasis

You can do that by adding 10000 to the user_id (I doubt I'll ever have more than 9999 users so it should be ok), but now... I can call it out from the cpg11d_users table using the $username, but how do I replace $cat with the value that is returned? where do I put the code to tell it to use the value pulled out from the database as $cat?
Pixnet Gallery: http://www.pixnet.net
iNSiGNiA Weblog: http://www.jayliu.org

Tarique Sani

I suggest take a break,  the solution will hit you in the morning :)

Dont make changes to index.php and don't rewrite URL to directly point to index.php but a different script which in turn uses the header function to redirect to index.php?cat=1000x
SANIsoft PHP applications for E Biz

Oasis

oh gosh that's a good idea.. I'll give it a shot
Pixnet Gallery: http://www.pixnet.net
iNSiGNiA Weblog: http://www.jayliu.org

Oasis

woah.... finally got it going...

now http://photo.enctu.org/oasis redirects to http://photo.enctu.org/index.php?cat=10002  :D

One last question though... Is there a way I could do it so the the url doesn't change automatically? What I mean is that currently when the page loads the url in the address bar is changed... what should I do to keep it there? Is there any other function other than header(location: URL) ? One that will keep the shortened url in the address bar.

by the way, here are the files:

QuoteHTACCESS FILE

RewriteEngine On
RewriteRule ^([a-z0-9&=]+)$ album.php?username=$1

QuoteALBUM.PHP FILE

<?php

/**************************************************************************
   Database functions
 **************************************************************************/

// Connect to the database
function db_connect()
{
        global $CONFIG;
        $result = @mysql_connect(host, user, pass);
        if (!$result)
                return false;
        if (!mysql_select_db(coppermine))
                return false;
        return $result;
}

// Perform a database query
function db_query($query)
{
        global $query_stats;

        $query_start = getmicrotime();
        $result = mysql_query($query);
        $query_end = getmicrotime();
        $query_stats[] = $query_end - $query_start;

        if (!$result) db_error("While executing query \"$query\"");

        return $result;
}

// Error message if a query failed
function db_error($the_error)
{
        global $CONFIG;

        if (!$CONFIG['debug_mode']) {
            cpg_die(CRITICAL_ERROR, 'There was an error while processing a database query', __FILE__, __LINE__);
        } else {

                $the_error .= "\n\nmySQL error: ".mysql_error()."\n";

                $out = "<br />There was an error while processing a database query.<br /><br/>
                    <form name='mysql'><textarea rows=\"8\" cols=\"60\">".htmlspecialchars($the_error)."</textarea></form>";

            cpg_die(CRITICAL_ERROR, $out, __FILE__, __LINE__);
        }
}

// Fetch all rows in an array
function db_fetch_rowset($result)
{
        $rowset = array();

        while ($row = mysql_fetch_array($result)) $rowset[] = $row;

        return $rowset;
}

function getmicrotime(){
        list($usec, $sec) = explode(" ",microtime());
        return ((float)$usec + (float)$sec);
}

function cpg_die($msg_code, $msg_text,  $error_file, $error_line, $output_buffer = false)
{
    $ob = ob_get_contents();
        if ($ob) ob_end_clean();
        exit;
}


db_connect() || die("<b>Coppermine critical error</b>:<br />Unable to connect to database !<br /><br />MySQL said: <b>".mysql_error()."</b>");
$username = $HTTP_GET_VARS['username'];
$results = mysql_query("SELECT user_id FROM cpg11d_users WHERE user_name = '$username'");

list($row) = mysql_fetch_row($results);
if (!$row) {
$row = 0;
}
$idnum=$row+10000;
$catid = $idnum;
    Header("Location: http://photo.enctu.org/index.php?cat=".$catid);
    exit;
?>
Pixnet Gallery: http://www.pixnet.net
iNSiGNiA Weblog: http://www.jayliu.org

Oasis

i know practically no php, and these are just bits and pieces I taken from other files, edited, and put together, so the code is probably pretty messy... If someone can clean it up for me that would be great!
Pixnet Gallery: http://www.pixnet.net
iNSiGNiA Weblog: http://www.jayliu.org