forum.coppermine-gallery.net

Support => Older/other versions => cpg1.1.X Support (standalone) => Topic started by: Oasis on September 25, 2003, 05:38:01 pm

Title: another question (regarding user album urls)
Post by: Oasis on September 25, 2003, 05:38:01 pm
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
Title: another question (regarding user album urls)
Post by: Tarique Sani on September 25, 2003, 05:59:37 pm
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
Title: another question (regarding user album urls)
Post by: Oasis on September 25, 2003, 10:11:58 pm
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?
Title: another question (regarding user album urls)
Post by: Tarique Sani on September 26, 2003, 05:20:13 am
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
Title: mod for nicer url's
Post by: gtroll on September 26, 2003, 09:51:35 am
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
Code: [Select]

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
Code: [Select]

$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
Code: [Select]

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.
Title: another question (regarding user album urls)
Post by: moorey on September 26, 2003, 10:31:37 am
Very nice.  I love mod_rewrites!

*hugs* Apache
Title: another question (regarding user album urls)
Post by: Oasis on September 26, 2003, 12:46:47 pm
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
Title: another question (regarding user album urls)
Post by: xarumanx on September 26, 2003, 01:55:37 pm
From a developers point of view, this shouldn't be a big problem, but AFAIK not much hoster support rewritemap...
Title: More about mod for nicer URLs
Post by: gtroll on September 26, 2003, 02:10:51 pm
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:
Title: another question (regarding user album urls)
Post by: Oasis on September 27, 2003, 02:41:48 pm
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:
Quote
RewriteEngine 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:
Quote
RewriteEngine 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
Title: another question (regarding user album urls)
Post by: Tarique Sani on September 27, 2003, 02:55:46 pm
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
Title: another question (regarding user album urls)
Post by: Oasis on September 27, 2003, 03:27:56 pm
Quote
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 ( )


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.
Title: another question (regarding user album urls)
Post by: Tarique Sani on September 27, 2003, 03:34:06 pm
Quote from: "oasis"
actually it doesn't. Try clicking on it~  :)

Thats because you already got rewrite going ....
Title: another question (regarding user album urls)
Post by: Oasis on September 27, 2003, 03:42:48 pm
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....
Title: another question (regarding user album urls)
Post by: Tarique Sani on September 27, 2003, 05:47:40 pm
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 :)
Title: another question (regarding user album urls)
Post by: Oasis on September 27, 2003, 06:16:06 pm
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?
Title: another question (regarding user album urls)
Post by: Tarique Sani on September 27, 2003, 06:21:55 pm
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
Title: another question (regarding user album urls)
Post by: Oasis on September 27, 2003, 06:39:31 pm
oh gosh that's a good idea.. I'll give it a shot
Title: another question (regarding user album urls)
Post by: Oasis on September 27, 2003, 08:49:37 pm
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:

Quote
HTACCESS FILE

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


Quote
ALBUM.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;
?>
Title: another question (regarding user album urls)
Post by: Oasis on September 27, 2003, 09:04:57 pm
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!
Title: another question (regarding user album urls)
Post by: kegobeer on September 27, 2003, 11:56:00 pm
Quote
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.

The easiest way is to use frames.  A buddy of mine does this so the url never changes in the address bar.  Make an index.html file with two frames, one that is only one pixel wide.  Use that frame to call cpg into the large frame.

That should keep your url in place.
Title: another question (regarding user album urls)
Post by: Tarique Sani on September 28, 2003, 06:08:56 am
Quote from: "oasis"
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.

There is something in the rewrite rules which allows this ... Can't recall

BTW - thanks for the files, this should go into the FAQ....
Title: another question (regarding user album urls)
Post by: Oasis on September 28, 2003, 08:58:18 am
OK I've done it!

I tweaked it a bit, and removed useless code. I've also made it so that the url will stay in place.

HTACCESS hasn't changed much, but the most important thing is that now it points to index.php not album.php as it previously did.
Quote
RewriteEngine On
RewriteRule ^([a-z0-9&_=]+)$ index.php?username=$1


Second, ALBUM.PHP has change A LOT!:
Quote
<?php
require('include/init.inc.php')
if (isset($HTTP_GET_VARS['username'])) {
        $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 = -10000;
        }
        $idnum=$row+10000;
        $catid = $idnum;
        $cat = $catid;
$HTTP_GET_VARS['cat'] = $cat;
}
?>


Lastly, I'm afraid I had to chang a tiny bit of INDEX.PHP:
I removed from the top:
Quote
require('include/init.inc.php');

and added
Quote
require('album.php');


the removed line has been moved to album.php, where it still gets called.
What it does now is when I enter http://photo.enctu.org/USERNAME in the browser,
mod_rewrite makes it call index.php?username=USERNAME
ALBUM.PHP will process the variable and turn it into the user_id $cat,
which is the variable INDEX.PHP uses for calling out the albums of a specific category(in this case a user). INDEX.PHP will be tricked into thinking that the user specified a cat, and will go to that category. And best of all, since INDEX.PHP is giving all the information, there is no need for redirection, which means that the url will not change!!!

Try: http://photo.enctu.org/stunion

Oh god, I can't help feeling pleased with myself for doing all this with hardly any knowledge of PHP. Thanks to everyone who helped me. Coppermine RULES!
Title: another question (regarding user album urls)
Post by: moorey on September 28, 2003, 09:20:09 am
Very nice. Great work!
Title: another question (regarding user album urls)
Post by: Joachim Müller on September 28, 2003, 12:25:31 pm
Quote from: "tarique"
BTW - thanks for the files, this should go into the FAQ....

hehe, and what exactly should go there? This thread is somewhat "oversized" for the faq... :wink:
Could anyone do a resumé that I can put into the faq?

Thanks

GauGau
Title: Not User, but Album
Post by: lasa on October 07, 2003, 08:58:22 pm
Very nice script!
Can you please rewrite this script?
I want to use it for albums, not for the users.

Lasa.
Title: another question (regarding user album urls)
Post by: kegobeer on October 08, 2003, 02:14:33 am
Quote
Can you please rewrite this script?

Are you kidding?  :roll:
Title: another question (regarding user album urls)
Post by: lasa on October 08, 2003, 08:15:28 am
Some small PHP modifications. :wink:
I can't code PHP.... :oops:
Title: another question (regarding user album urls)
Post by: Joachim Müller on October 08, 2003, 09:54:36 am
Quote from: "lasa"
Some small PHP modifications. :wink:
I can't code PHP.... :oops:
Then how do you know these are going to be small modifications if you can't code php? :evil:

GauGau
Title: another question (regarding user album urls)
Post by: lasa on October 08, 2003, 11:15:38 am
Because the only thing you have to change is the album instead of the user. I have tried it, but it don't work out...
Title: another question (regarding user album urls)
Post by: lasa on October 08, 2003, 11:32:55 am
I have made this, but it doesn't work:

.htaccess
Code: [Select]

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


album.php
Code: [Select]

<?php 

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

// Connect to the database 
mysql_connect &#40;***,***,'***'&#41;;
mysql_select_db &#40;***'&#41;;


// Perform a database query 
function db_query&#40;$query&#41; 
&#123; 
global $query_stats

$query_start getmicrotime&#40;&#41;; 
$result mysql_query&#40;$query&#41;; 
$query_end getmicrotime&#40;&#41;; 
$query_stats[&#93; = $query_end - $query_start; 

if &#40;!$result&#41; db_error&#40;"While executing query \"$query\""&#41;; 

return $result
&
#125; 

// Error message if a query failed 
function db_error&#40;$the_error&#41; 
&#123; 
global $CONFIG

if &
#40;!$CONFIG['debug_mode'&#93;&#41; &#123; 
cpg_die&#40;CRITICAL_ERROR, 'There was an error while processing a database query', __FILE__, __LINE__&#41;; 
&#125; else &#123; 

$the_error .= "\n\nmySQL error&#58; ".mysql_error&#40;&#41;."\n"; 

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

cpg_die&#40;CRITICAL_ERROR, $out, __FILE__, __LINE__&#41;; 
&#125; 
&#125; 

// Fetch all rows in an array 
function db_fetch_rowset&#40;$result&#41; 
&#123; 
$rowset = array&#40;&#41;; 

while &#40;$row = mysql_fetch_array&#40;$result&#41;&#41; $rowset[&#93; = $row; 

return $rowset
&
#125; 

function getmicrotime&#40;&#41;&#123; 
list&#40;$usec, $sec&#41; = explode&#40;" ",microtime&#40;&#41;&#41;; 
return &#40;&#40;float&#41;$usec + &#40;float&#41;$sec&#41;; 
&#125; 

function cpg_die&#40;$msg_code, $msg_text, $error_file, $error_line, $output_buffer = false&#41; 
&#123; 
$ob ob_get_contents&#40;&#41;; 
if &#40;$ob&#41; ob_end_clean&#40;&#41;; 
exit; 
&
#125; 


$album $HTTP_GET_VARS['album'&#93;; 
$results mysql_query&#40;"SELECT aid FROM cpg00d_albums WHERE title = '$album'"&#41;; 

list&#40;$row&#41; = mysql_fetch_row&#40;$results&#41;; 
if &#40;!$row&#41; &#123; 
$row 0
&
#125; 
$idnum=$row
Header&#40;"Location&#58; thumbnails.php?album=".$idnum&#41;; 
exit; 
?>



I have replaced my MySQL data by *** for my privacy.
Title: another question (regarding user album urls)
Post by: lasa on October 10, 2003, 08:35:29 pm
Nobody can help me?
Title: another question (regarding user album urls)
Post by: intranet on October 14, 2003, 01:37:35 pm
Is this the complete Mod ? I was trying to follow along, but got a bit confused once it was completed.

Will adding the following code to the appropriate files work as is ( also moving the line from index.php to album.php )
, or is there other code previous in the thread that is required?

Quote from: "oasis"
OK I've done it!

I tweaked it a bit, and removed useless code. I've also made it so that the url will stay in place.

HTACCESS hasn't changed much, but the most important thing is that now it points to index.php not album.php as it previously did.
Quote
RewriteEngine On
RewriteRule ^([a-z0-9&_=]+)$ index.php?username=$1


Second, ALBUM.PHP has change A LOT!:
Quote
<?php
require('include/init.inc.php')
if (isset($HTTP_GET_;VARS['username'])) {
        $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 = -10000;
        }
        $idnum=$row+10000;
        $catid = $idnum;
        $cat = $catid;
$HTTP_GET_VARS['cat'] = $cat;
}
?>


Lastly, I'm afraid I had to chang a tiny bit of INDEX.PHP:
I removed from the top:
Quote
require('include/init.inc.php');

and added
Quote
require('album.php');


the removed line has been moved to album.php, where it still gets called.
What it does now is when I enter http://photo.enctu.org/USERNAME in the browser,
mod_rewrite makes it call index.php?username=USERNAME
ALBUM.PHP will process the variable and turn it into the user_id $cat,
which is the variable INDEX.PHP uses for calling out the albums of a specific category(in this case a user). INDEX.PHP will be tricked into thinking that the user specified a cat, and will go to that category. And best of all, since INDEX.PHP is giving all the information, there is no need for redirection, which means that the url will not change!!!

Try: http://photo.enctu.org/stunion

Oh god, I can't help feeling pleased with myself for doing all this with hardly any knowledge of PHP. Thanks to everyone who helped me. Coppermine RULES!
Title: another question (regarding user album urls)
Post by: Oasis on October 15, 2003, 08:03:54 am
Quote from: "intranet"
Is this the complete Mod ? I was trying to follow along, but got a bit confused once it was completed.

Will adding the following code to the appropriate files work as is ( also moving the line from index.php to album.php )
, or is there other code previous in the thread that is required?


No intranet, this is all you need. When I said I tweaked the code a bit, actually I meant I rewrote it.  :lol:
So all you will need is the new .htaccess file, the new(shorter) album.php file and the slight change in index.php.
Oh by the, if you changed your table prefix, you will have to edit "cpg11d_users" in album.php

@lasa: try editing the newer version and not that old one... you do realise that albums are called by thumbnail.php and not index.php... also, it will not work if you have special characters(it will only take numbers, characters, hyphens and underscores) or spaces in the album name. Also, instead of cpg11d_users you'll have to call the table with the information about the albums. Good luck~
Title: another question (regarding user album urls)
Post by: Oasis on October 15, 2003, 08:20:04 am
NOTE: This is not the user album url rewrite code! If you're looking for that, look further back.

@lasa: try this

.htaccess
Quote
RewriteEngine On
RewriteRule ^([a-z0-9&_=]+)$ thumbnails.php?albumname=$1


albumname.php
Quote
<?php
require('include/init.inc.php')
if (isset($HTTP_GET_VARS['albumname'])) {
$albumname = $HTTP_GET_VARS['albumname'];
$results = mysql_query("SELECT aid FROM cpg11d_album WHERE title = '$albumname'");
list($row) = mysql_fetch_row($results);
if (!$row) {
$row = 0;
}
$album = $row;
$HTTP_GET_VARS[album'] = $album;
}
?>


replace in thumbnails.php
Quote
require('include/init.inc.php');

with
Quote
require('include/albumname.php');


I have not tested this because my gallery is now live so I can't change the settings. But, hopefully, if you enter http://yourdomain.com/pathtocoppermine/THISISALBUMNAME
it will go to the album with that name.

IF YOU HAVE TWO ALBUMS WITH THE SAME NAME, IT WILL NOT WORK!
IF YOU HAVE ALBUM NAMES WITH SPACES OR ANY SPECIAL CHARACTERS IT WILL NOT WORK.
So use this only if you are the sole admin and users can't create albums.
Title: Re: another question (regarding user album urls)
Post by: TheGamer1701 on July 26, 2005, 03:14:36 pm
Hey,

it works great for, only that I just can see the last 4 uploaded pictures, not more.
But I want to see the category of the user (album overview)

Any idea what I have to change?
Or where this come from?

Thx