1 (edited by evolved 2008-01-27 19:00)

Topic: SimplyPunRW - english translation :)

Hi guys,

I have done my best to translate Thorze's SimplyPunRW mod into english search friendly urls. It only requires 1 code change to get all of the URLs working so since I had to translate it for myself thought others may make use of it. Let me know if you have any problems with install.

#
#---------[ 1. OPEN ]-------------------------------------------------------
#
 
footer.php

#
#---------[ 2. FIND ]-----------------------------------------------------
#
 

// Close the db connection (and free up any result data)
$db->close();

#
#---------[ 3. ADD, BEFORE]-----------------------------------------
#
 
// SimplyPunRW par Thorze
// On commence par récupérer les pid de tous les liens qui appellent un message précis
$pattern = 'viewtopic.php?pid=<PID>'; // URL Rewriting ?
preg_match_all('#'.str_replace('\<PID\>', '([0-9]+)', preg_quote($pattern, '#')).'#', $tpl_main, $pids);
$pids = array_unique($pids[1]);

 
if(!empty($pids)) {
 
    // On récupére ensuite les id des topics qui correspondent aux différents pid
    $result = $db->query('SELECT id, topic_id FROM '.$db->prefix.'posts WHERE id='.implode(' OR id=', $pids)) or error('Unable to fetch post info', __FILE__, __LINE__, $db->error());
    if (!$db->num_rows($result))

        message($lang_common['Bad request']);
 
    while ($pid_id = $db->fetch_assoc($result))
    {
        $adresse[$pid_id['id']] = Array('topic_id' => $pid_id['topic_id']);

 
        if (isset($query))
            $query .= ' OR topic_id='.$pid_id['topic_id'];
        else
            $query = $pid_id['topic_id'];
    }

 
    // On récupére et on classe ensuite les pid de tous les posts de tous les topics concernés
    $result = $db->query('SELECT topic_id, id FROM '.$db->prefix.'posts WHERE topic_id='.$query.' ORDER BY posted') or error('Unable to fetch post info', __FILE__, __LINE__, $db->error());
    while ($id_pid = $db->fetch_assoc($result))

        $posts[$id_pid['topic_id']][] = $id_pid['id'];
 
    // On recherche les pid des liens dans les pid des topics et on récupére la place qu'ils occupent pour calculer leur page
    foreach ($posts AS $id => $pids)

    {
        foreach(array_keys($adresse) AS $pid) {
            if(($position = array_search($pid, $pids)) !== false)

                $adresse[$pid]['p'] = ceil(($position + 1) / $pun_user['disp_posts']);
        }
    }
 

    // On remplace enfin les adresses avec les pid par des adresses uniques et directes avec l'id du topic en précisant la page
    foreach($adresse AS $pid => $url)
        $tpl_main = str_replace(str_replace('<PID>', $pid, $pattern), 'viewtopic.php?id='.$url['topic_id'].'&p='.$url['p'].'', $tpl_main);

}
 
// On en profite pour virer tous les ?p=1 ou &p=1 qui doublonnent aussi inutilement les pages
$tpl_main = preg_replace('#(\?|&)p\=1#', NULL, $tpl_main);

 
// Fonction de rewrite
function pun_url($str,$more=false) {
        if((!isset($more)) || ($more == false)){

            $max = 2;
         }else{
            $max = $more;
         }
         $str = strtr($str,"ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÌÍÎÏìíîïÙÚÛÜùúûüÿÑñÇç'`",
                           "AAAAAAaaaaaaOOOOOOooooooEEEEeeeeIIIIiiiiUUUUuuuuyNnCc  ");
         $str = strtolower($str);
         $str = preg_replace('/[^a-z0-9_\.\s]/',' ',$str);
         $str = preg_replace('/[^a-z0-9_\s]\./','',trim($str));
         $str = str_replace('.',' ',$str);
         $str = str_replace('_',' ',$str);
         $str = " ".$str." ";
 
         $str = ereg_replace(' .{1,'.$max.'} ', ' ', $str);
         $str = ereg_replace(' .{1,'.$max.'} ', ' ', $str);
         $rep = array(" quel ", " crois ", " etes ", " quand ", " suis ", " aux "," moi ", " sont ", " quelle ", " quoi ", " mon ", " est ", " plus ", " que ", " vous ", " faites ", " par "," dans "," pour "," pas "," les "," des "," que "," une "," avec "," qui "," sur "," mes ");
         $str = str_replace($rep," ",$str);
         $str = trim($str);
         $str = preg_replace('/[\s]+/','-',$str);
 
         if(count(explode("-", $str)) >= 8){

            $max++;
            $str = pun_url(str_replace('-',' ',$str),$max);
         }
         if(!$more){
         if((!isset($str)) || ($str == "")){

            $str = "punseo";
           }
         }
           return $str;
  }
 
 
// Rewrite de la navbar
$tpl_main = preg_replace('#<a href="index.php#', '<a href="index.html', $tpl_main);
$tpl_main = preg_replace('#<a href="userlist.php#', '<a href="userlist.html', $tpl_main);
$tpl_main = preg_replace('#<a href="search.php#', '<a href="search.html', $tpl_main);
$tpl_main = preg_replace('#<a href="register.php#', '<a href="register.html', $tpl_main);
$tpl_main = preg_replace('#<a href="login.php#', '<a href="login.html', $tpl_main);
$tpl_main = preg_replace('#<a href="login.html\?action=forget#', '<a href="forgot_password.html', $tpl_main);

 
// Rewrite de viewforum et on ajoute le nom du forum dans l'url
$pattern = 'viewforum.php?id=<ID>';
preg_match_all('#'.str_replace('\<ID\>', '([0-9]+)', preg_quote($pattern, '#')).'#', $tpl_main, $out);
$out2 = array_unique($out[1]);

if(!empty($out2)) 
{
    $tabunique2=array_values($out2);
    $i=0;
    while ($i<count($tabunique2)) 
    {

        $id7 = $tabunique2[$i];
        $result = $db->query('SELECT id, forum_name FROM '.$db->prefix.'forums WHERE id='.$id7) or error('Unable to fetch post info', __FILE__, __LINE__, $db->error());
        while ($fnom = $db->fetch_assoc($result))

        {
            $tpl_main = preg_replace('#<a href="viewforum.php\?id='.$fnom['id'].'#', '<a href="f'.$id7.'-'.pun_url($fnom['forum_name']).'.html', $tpl_main);
            $tpl_main = preg_replace('#<a href="f([0-9]+)-'.pun_url($fnom['forum_name']).'.html([0-9]+)#', '<a href="f\\1\\2.html', $tpl_main);
            $tpl_main = preg_replace('#<a href="f'.$id7.'-'.pun_url($fnom['forum_name']).'.html&p=([0-9]+)#', '<a href="f'.$fnom['id'].'-page\\1-'.pun_url($fnom['forum_name']).'.html', $tpl_main);
        }

        $i++;
    }
}
 
// Rewrite de viewtopic et on ajoute le titre du post dans l'url
$pattern = 'viewtopic.php?id=<ID>';
preg_match_all('#'.str_replace('\<ID\>', '([0-9]+)', preg_quote($pattern, '#')).'#', $tpl_main, $out);
$out3 = array_unique($out[1]);

if(!empty($out3)) 
{
    $tabunique3=array_values($out3);
    $i=0;
    while ($i<count($tabunique3)) 
    {

        $id7 = $tabunique3[$i];
        $result = $db->query('SELECT id, subject FROM '.$db->prefix.'topics WHERE id='.$id7.'') or error('Unable to fetch post info', __FILE__, __LINE__, $db->error());
        while ($fnom = $db->fetch_assoc($result))

        {
            $tpl_main = preg_replace('#<a href="viewtopic.php\?id='.$fnom['id'].'#', '<a href="s'.$fnom['id'].'-'.pun_url($fnom['subject']).'.html', $tpl_main);
            $tpl_main = preg_replace('#<a href="s([0-9]+)-'.pun_url($fnom['subject']).'.html([0-9]+)#', '<a href="s\\1\\2.html', $tpl_main);
            $tpl_main = preg_replace('#<a href="s'.$fnom['id'].'-'.pun_url($fnom['subject']).'.html&p=([0-9]+)#', '<a href="s'.$fnom['id'].'-page\\1-'.pun_url($fnom['subject']).'.html', $tpl_main);
            $tpl_main = preg_replace('#<a href="s'.$fnom['id'].'-'.pun_url($fnom['subject']).'.html&action=new#', '<a href="s'.$fnom['id'].'-show-new.html', $tpl_main);
        }

        $i++;
    }
}
 
// Rewrite du profil
$tpl_main = preg_replace('#<a href="profile.php\?id=([0-9]+)#', '<a href="profil-\\1.html', $tpl_main);
 

// Rewrite des liens de recherche
$tpl_main = preg_replace('#<a href="search.html\?action=show_24h#', '<a href="search-24-hours.html', $tpl_main);
$tpl_main = preg_replace('#<a href="search.html\?action=show_unanswered#', '<a href="search-unanswered.html', $tpl_main);
$tpl_main = preg_replace('#<a href="search.html\?action=show_subscriptions#', '<a href="show_subscriptions.html', $tpl_main);
$tpl_main = preg_replace('#<a href="search.html\?action=show_new#', '<a href="show_new.html', $tpl_main);
$tpl_main = preg_replace('#<a href="search.html\?action=show_user&user_id=([0-9]+)#', '<a href="messages_by-\\1.html', $tpl_main);
$tpl_main = preg_replace('#<a href="search.html\?search_id=([0-9]+)#', '<a href="search\\1.html', $tpl_main);
$tpl_main = preg_replace('#<a href="search([0-9]+).html&p=([0-9]+)#', '<a href="search\\1-page\\2.html', $tpl_main);

 
//Divers misc.php
$tpl_main = preg_replace('#<a href="misc.php\?action=rules#', '<a href="rules.html', $tpl_main);
$tpl_main = preg_replace('#<a href="misc.php\?action=markread#', '<a href="mark_read.html', $tpl_main);

                         
#
#---------[ 4. SAVE/UPLOAD ]----------------------------
#

Then create a file called .htaccess and include the following code:

RewriteEngine on
 
RewriteRule ^index.html$ index.php [L]
 
RewriteRule ^f([0-9]+)-page([0-9]+)-(.*).html$ viewforum.php?id=$1&p=$2 [L]

RewriteRule ^f([0-9]+)(.*)$ viewforum.php?id=$1 [L]
 
RewriteRule ^s([0-9]+)-page([0-9]+)-(.*).html$ viewtopic.php?id=$1&p=$2 [L]

RewriteRule ^s([0-9]+)-new-messages.html$ viewtopic.php?id=$1&action=new [L]
RewriteRule ^s([0-9]+)(.*)$ viewtopic.php?id=$1 [L]

 
RewriteRule ^profil-([0-9]+)(.*)$ profile.php?id=$1 [L]
 
RewriteRule ^register.html$ register.php [L]
RewriteRule ^login.html$ login.php [L]

RewriteRule ^forgot_password.html$ login.php?action=forget [L]
RewriteRule ^search.html$ search.php [L]
 
RewriteRule ^search([0-9]+).html$ search.php?search_id=$1 [L]
RewriteRule ^search([0-9]+)-page([0-9]+).html$ search.php?search_id=$1&p=$2 [L]

RewriteRule ^userlist.html$ userlist.php [L]
RewriteRule ^search-24-hours.html$ search.php?action=show_24h [L]
RewriteRule ^([0-9]+)-my-messages.html$ search.php?action=show_user&user_id=$1 [L]

RewriteRule ^show_subscriptions.html$ search.php?action=show_subscriptions [L]
RewriteRule ^search-unanswered.html$ search.php?action=show_unanswered [L]
RewriteRule ^show_new.html$ search.php?action=show_new [L]
RewriteRule ^messages_by-([0-9]+).html$ search.php?action=show_user&user_id=$1 [L]

 
RewriteRule ^userlist-pseudo(.*)-group(.*)-sort(.*)-order(.*)-([0-9]+)(.*)$ userlist.php?username=$1&show_group=$2&sort_by=$3&sort_dir=$4&p=$5 [L]    

 
RewriteRule ^rules.html$ misc.php?action=rules [L]
RewriteRule ^mark_read.html$ misc.php?action=markread [L]

I've only translated the words that appear in the URL's themselves, not the code markers etc smile

Original mod location can be found : http://www.thorze.net/simplypunrw.html

Rewritten URLs are :

    * Index
    * User list
    * Rules
    * Search
    * Profile
    * viewtopic.php ~ s1-test-post.html
    * viewforum.php ~ f1-test-forum.html
    * show recent posts
    * show unanswered posts
    * show your subscribed topics
    * show your posts

May have been a few others in there too