functions_cute_url.php

Support for the phpbbmodders_lib.
User avatar
angelside
New member
New member
Posts: 6
Joined: 06 Feb 2007, 15:06
Real name: Sevdin Filiz
Location: Turkey

functions_cute_url.php

Postby angelside » 09 Jun 2008, 14:30

How use functions_cute_url.php, any little example, any .htaccess changes etc.?

Code: Select all

$cute_url = new cute_url_handler();

after ?

phpBB Türkiye :: Turkish phpBB3 support and development
phpBB3 Portal :: usable version 1.2.2 | phpBB3 Portal :: development version 2.0.0

User avatar
igorw
Past Contributor
Past Contributor
Posts: 1967
Joined: 01 Jun 2006, 20:48
Real name: Igor

Re: functions_cute_url.php

Postby igorw » 09 Jun 2008, 15:56

Alright,

First you need to find out where the script is located, relative to the domain root. We are now at /board/ for example. Let's assume that the script is at:

Code: Select all

http://yoursite.com/cute/


So, we create our object:

Code: Select all

$cute_url = new cute_url_handler('/cute/');


The cute url handler has two methods that we will be using. One is the build() method:

Code: Select all

echo $cute_url->build(array('test', 'first', 'second'));

This will output:

Code: Select all

/cute/test/first/second/


You can also use the second argument for GET parameters:

Code: Select all

echo $cute_url->build(array('test', 'first', 'second'), array('arg' => 'value'));

Which results in:

Code: Select all

/cute/test/first/second/?arg=value


The other method that we use is the get method. It's argument works excactly like the second arg of request_var(), it determines the type for the main variable.

It gets a variable from the URL, so if we're at /test/first/, it will return "test" the first time, "first" the second time it's called. let's try it. Go to:

Code: Select all

http://yoursite.com/cute/index.php/test/first/

We can get rid of the index.php part later.

If you try this code:

Code: Select all

print_r($cute_url->url);

You will get the following result:

Code: Select all

Array ( [0] => index.php [1] => test [2] => first )


As you can see, index.php is included here, so it'd have to be filtered out by doing a call to $cute_url->get(). But let's find a better solution. Rewrites.

Create a new .htaccess file if it doesn't exist already, and now we add this:

Code: Select all

# rewrite
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

This will rewrite all files to index.php, except if the file exists, then it's served as normal.

So now, try going to:

Code: Select all

http://yoursite.com/cute/test/first/

You should get this as a result:

Code: Select all

Array ( [0] => test [1] => first )


If you do, congratulations! You did it!

You can now use the get() method without problems. Here, have an example of usage:

Code: Select all

switch ($mode = $cute_url->get(''))
{
   case 'about':
       echo '<h2>About me</h2>';
       echo 'Welcome to my about page. I co-founded phpbbmodders.net, a mod authors\' community, and i\'ve also created a lot of MODs. In my free time i like biking or playing the guitar. I have a very big epiphone with f-holes (like a violin).';
   break;
   
   default:
       echo 'Welcome to my page! I am igor, MOD team member. Click <a href="' . $cute_url->build(array('about')) . '">here</a> for more information about me.';
   break;
}


I hope it's of use to you. :)

mtotheikle
New member
New member
Posts: 25
Joined: 11 Oct 2007, 03:03
Real name: Mike
Location: Spokane, WA

Re: functions_cute_url.php

Postby mtotheikle » 10 Jun 2008, 00:43

Wow Evil >3 that is a really great script, thanks for posting an example!

User avatar
angelside
New member
New member
Posts: 6
Joined: 06 Feb 2007, 15:06
Real name: Sevdin Filiz
Location: Turkey

Re: functions_cute_url.php

Postby angelside » 10 Jun 2008, 08:10

I get build with subpage, but do this way is true and most performance way? sub switch in switch ?

Code: Select all

include($phpbb_root_path&nbsp;.&nbsp;'_functions_cute_url.'&nbsp;.&nbsp;$phpEx);

$cute_url&nbsp;=&nbsp;new&nbsp;cute_url_handler('/');

/*
&nbsp;&nbsp;&nbsp;&nbsp;./
&nbsp;&nbsp;&nbsp;&nbsp;about/
&nbsp;&nbsp;&nbsp;&nbsp;about/phpbb/
&nbsp;&nbsp;&nbsp;&nbsp;about/me/
*/
echo&nbsp;'<br&nbsp;/>
<a&nbsp;href="'
&nbsp;.&nbsp;$cute_url->build(array('.'))&nbsp;.&nbsp;'">home</a><br&nbsp;/>
<a&nbsp;href="'
&nbsp;.&nbsp;$cute_url->build(array('about'))&nbsp;.&nbsp;'">about</a><br&nbsp;/>
<a&nbsp;href="'
&nbsp;.&nbsp;$cute_url->build(array('about','phpbb'))&nbsp;.&nbsp;'">about&nbsp;phpbb</a><br&nbsp;/>
<a&nbsp;href="'
&nbsp;.&nbsp;$cute_url->build(array('about','me'))&nbsp;.&nbsp;'">about&nbsp;me</a><br&nbsp;/><br&nbsp;/>';

switch&
nbsp;($mode&nbsp;=&nbsp;$cute_url->get(''))
{
&
nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'about':
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch&nbsp;($mode&nbsp;=&nbsp;$cute_url->get('about'))
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'phpbb':
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;'<h2>about&nbsp;phpbb</h2>';
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'me':
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;'<h2>about&nbsp;me</h2>';
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;'<h2>about</h2>';
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&
nbsp;&nbsp;&nbsp;&nbsp;break;
&
nbsp;&nbsp;&nbsp;&nbsp;
&
nbsp;&nbsp;&nbsp;&nbsp;default:
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;'<h2>home&nbsp;page</h2>';
&
nbsp;&nbsp;&nbsp;&nbsp;break;
}&
nbsp;&nbsp;Â

phpBB Türkiye :: Turkish phpBB3 support and development
phpBB3 Portal :: usable version 1.2.2 | phpBB3 Portal :: development version 2.0.0

User avatar
igorw
Past Contributor
Past Contributor
Posts: 1967
Joined: 01 Jun 2006, 20:48
Real name: Igor

Re: functions_cute_url.php

Postby igorw » 10 Jun 2008, 10:51

Yep, excactly. Thouth the second switch doesn't need 'about' as default value. ;)

Oh, and this:

Code: Select all

$cute_url->build(array('.'))

Should just be:

Code: Select all

$cute_url->build()

User avatar
angelside
New member
New member
Posts: 6
Joined: 06 Feb 2007, 15:06
Real name: Sevdin Filiz
Location: Turkey

Re: functions_cute_url.php

Postby angelside » 18 Jul 2008, 12:10

Ok, I build manual pages with case. Now, how build dynamic pages from SQL ?

my simple code is here, but not yet use url depth now:

Code: Select all

// full url: http://test.loc/phpbb3/phpBB-3.0.2_page/about_phpbb
print_r($cute_url->url);
/*
Array
(
&nbsp;&nbsp;&nbsp;&nbsp;[0]&nbsp;=>&nbsp;phpbb3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;directory&nbsp;1
&nbsp;&nbsp;&nbsp;&nbsp;[1]&nbsp;=>&nbsp;phpBB-3.0.2_page&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;directory&nbsp;2
&nbsp;&nbsp;&nbsp;&nbsp;[2]&nbsp;=>&nbsp;about_phpbb&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;fake&nbsp;directory
)
*/
echo $cute_url->url[2];
// about_phpbb

$page_name = $cute_url->url[2];

$sql = 'SELECT *
        FROM '
 . PAGE_TABLE . '
            WHERE page_name = '
 .  $page_name . '
        ORDER BY page_id DESC'
; 
$result 
= $db->sql_query($sql);&nbsp;&nbsp;&nbsp;&nbsp

phpBB Türkiye :: Turkish phpBB3 support and development
phpBB3 Portal :: usable version 1.2.2 | phpBB3 Portal :: development version 2.0.0

User avatar
igorw
Past Contributor
Past Contributor
Posts: 1967
Joined: 01 Jun 2006, 20:48
Real name: Igor

Re: functions_cute_url.php

Postby igorw » 26 Jul 2008, 16:22

Yes, however to allow more dynamic handling i'd use $cute_url->get(). Like this you don't have to have a hardcoded ammount of url items.

User avatar
Afterlife(69)
Member
Member
Posts: 175
Joined: 30 Jun 2006, 21:23
Real name: Dean
Location: Sydney, Australia

Re: functions_cute_url.php

Postby Afterlife(69) » 26 Apr 2009, 07:37

This reminds me of a lib I wrote back in '07. Good work :)

Code: Select all

<?php
/**
 * Breadcrumb Url Rewrites
 * 
 * This class is used for a created "breadcrumb" style url structures
 * for dynamic web applications, each "bit" or "directory" of the url
 * is read in to an array inside this class and accessed via the request
 * method.
 *
 * This class also provides a method to be used for generating an absolute
 * url to where the script is located allowing for dynamic creation of absolute
 * paths to the host script.
 * 
 * @author        Dean Newman < webmaster@auscoder.com >
 * @license        GNU/General Public License v2 (GPL)
 * @copyright    Copyright 2007, Dean Newman
 */

/**
 * Crumbs Class
 *
 * @version        1.0.0.0-php5
 * @package        class_crumbs.php
 */
final class crumbs
{
    const    MAX_BITS = 10;

    public    $raw;
    public    $crumbs;

    public function __construct()
    {
        /**
         * Define as array to prevent errors
         */
        $this->raw = array();
        
        
/**
         * If the REQUEST_URI is set and not empty
         */
        if ( isset ( $_SERVER['REQUEST_URI'] ) && ! empty ( $_SERVER['REQUEST_URI'] ) )
        {
            /**
             * Slice off query strings
             */
            $this->raw = $_SERVER['REQUEST_URI'] = str_replace(strstr($_SERVER['REQUEST_URI'], '?'), '', $_SERVER['REQUEST_URI']);

            /**
             * Seperate the sections by forward slash
             */
            $this->crumbs = explode('/', $this->raw, self::MAX_BITS);
            
            
/**
             * Remove the lower level directories and this file
             */
            $index = array_search(basename(dirname($_SERVER['SCRIPT_NAME'])), $this->crumbs);

            if ( in_array(basename($_SERVER['SCRIPT_NAME']), $this->crumbs) )
            {
                $index = array_search(basename($_SERVER['SCRIPT_NAME']), $this->crumbs);
            }

            if ( array_search ($this->raw, $this->crumbs) )
            {
                /**
                 * If the name of this file is found, remove it
                 */
                $index++;
            }
            $this->crumbs = array_slice($this->crumbs, ($index + 1));
            
            
/**
             * Remove empty values
             */
            foreach ( $this->crumbs AS $key => $crumb )
            {
                if ( empty ( $crumb ) )
                {
                    unset($this->crumbs[$key]);
                }
            }
            
            
/**
             * Sort the array by key
             */
            ksort($this->crumbs);
            
            
/**
             * Decode the special chars of the entries
             */
            $this->crumbs = array_map('urldecode', $this->crumbs);
        }
        
        
/**
         * Return parsed url, or empty array.
         */
        return $this->crumbs;
    }
    
    public function __destruct
()
    {
        
    
}
    
    public function push
($item)
    {
        $tmparray = array_merge(array($item), $this->crumbs);
    
        return 
($this->crumbs = $tmparray);
    }
    
    public function shift
()
    {
        return array_shift($this->crumbs);
    }
    
    public function request
($key = 0, $default = null, $type = null, $maxlen = null, $walk = null)
    {
        $return = array_key_exists($key, $this->crumbs) ? $this->crumbs[$key] : $default;
            
        if 
( $type )
        {
            settype($return, $type);
        }
        
        if 
( $walk )
        {
            $return = $walk($return);
        }
        
        if 
( $maxlen )
        {
            $return = substr($return, 0, $maxlen);
        }
    
        return $return
;
    }
    
    public static function get_external_url
()
    {
        $http = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ) ? 'https://' : 'http://';
        $host = ( isset($_SERVER['HTTP_HOST']) ) ? $_SERVER['HTTP_HOST'] : false;
        $path = ( isset($_SERVER['SCRIPT_NAME']) ) ? dirname($_SERVER['SCRIPT_NAME']) : false;

        if ( substr($path, strlen($path) - 1) == '/' )
        {
            $path = substr($path, 0, strlen($path) - 2);
        }

        if ( strstr($_SERVER['SERVER_SOFTWARE'], 'IIS') )
        {
            $path .= '/' . basename($_SERVER['SCRIPT_NAME']);
        }

        return ($http . $host . $path);
    }
}

?>
Life in the house :)

Deatzo Seol
New member
New member
Posts: 2
Joined: 14 Jul 2008, 22:00

Re: functions_cute_url.php

Postby Deatzo Seol » 24 May 2009, 19:59

I have a question regarding template variables and cute_url:

How can I make sure template vars like {T_THEME_PATH} change according to the website path? E.g. when you're in /path/to/entry/, the variable begins with './', so it will never correctly load stylesheets &c.
It's probably a simple solution but I thought I'd ask. ;)

User avatar
igorw
Past Contributor
Past Contributor
Posts: 1967
Joined: 01 Jun 2006, 20:48
Real name: Igor

Re: functions_cute_url.php

Postby igorw » 24 May 2009, 20:47

We actually do something extremely hacky here. We have a hook (placed in board/includes/hooks/) that will fix all of the messed up links. Here's the code:

Code: Select all

<?php
/**
*
* @package phpbbmodders_site
* @version $Id: hook_fix_url.php 237 2008-01-14 18:43:22Z evil3 $
* @copyright (c) 2007 phpbbmodders
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/
if (!defined('IN_PHPBB'))
{
   exit;
}

/**
 * Hook for $template->display()
 * will fix urls of the site so they point to phpbb
 */
function template_display_fix_url(&$hook, $handle, $include_once = true)
{
   global $template;

   // Get possible previous results (and ignore them)
   $result = $hook->previous_hook_result(array('template', 'display'));

   // no idea what to do with this...
   if (!empty($result))
   {
      $result['result'];
   }

   ob_start();

   $template->display($handle, $include_once);

   $output = ob_get_clean();

   $tags_ary = array(
      'a'         => 'href',
      'link'      => 'href',
      'script'   => array('src', 'type'),
      'img'      => 'src',
      'form'      => 'action',
   );

   foreach ($tags_ary as $tag_name => $tag_attribute)
   {
      $tag_name = preg_quote_pound($tag_name);

      if (is_array($tag_attribute))
      {
         $tag_attribute = '(' . implode('|', array_map('preg_quote_pound', $tag_attribute)) . ')';
      }
      else
      {
         $tag_attribute = preg_quote_pound($tag_attribute);
      }

      $output = preg_replace_callback("#(<$tag_name\s$tag_attribute=\"(.+?)\"(\s|)(>|/>))#s", 'fix_url_callback', $output);
   }

   echo $output;

   return true;
}

/**
 * Callback function for fix_url
 */
function fix_url_callback($matches)
{
   if (!empty($matches[1]))
   {
      return fix_url($matches[1]);
   }
}

/**
 * Fountain of Apples would like this function
 * It's used for array_map, returns preg_quoted string, quoting the "pound" (#) char.
 *
 * @param string $string
 * @return string Result
 */
function preg_quote_pound($string)
{
   return preg_quote($string, '#');
}

// we only need this hook if we're in the website
if (defined('IN_WEBSITE'))
{
   // so we'll have to use template->display()
   $phpbb_hook->register(array('template', 'display'), 'template_display_fix_url', 'last');
}

?>

Deatzo Seol
New member
New member
Posts: 2
Joined: 14 Jul 2008, 22:00

Re: functions_cute_url.php

Postby Deatzo Seol » 24 May 2009, 20:56

Well, it's hacky indeed, but it works :D
Thanks :P

User avatar
SamT
Past Contributor
Past Contributor
Posts: 79
Joined: 14 Jun 2009, 16:29
Real name: Sam
Location: Sacramento, CA, USA

Re: functions_cute_url.php

Postby SamT » 11 Aug 2009, 02:18

Do you have the "fix_url" function?

I have actually been trying to figure out way around using a hook, they make my site significantly slower.

User avatar
igorw
Past Contributor
Past Contributor
Posts: 1967
Joined: 01 Jun 2006, 20:48
Real name: Igor

Re: functions_cute_url.php

Postby igorw » 11 Aug 2009, 15:41

Here's the function:

Code: Select all

/**
 * Adjusts an URL from append_sid to point to the correct location (no more relative url)
 *
 * @param string $url The url after being run through append_sid
 * @return string Adjusted URL
 */
function fix_url($url)
{
   global $phpbb_root_path;
   global $cute_url;

   if (!empty($cute_url) && is_object($cute_url))
   {
      $url = str_replace($phpbb_root_path, $cute_url->url_base . 'board/', $url);
   }

   return $url;
}

User avatar
SamT
Past Contributor
Past Contributor
Posts: 79
Joined: 14 Jun 2009, 16:29
Real name: Sam
Location: Sacramento, CA, USA

Re: functions_cute_url.php

Postby SamT » 11 Aug 2009, 20:51

Thank you! :)

imkingdavid
Supporter
Supporter
Posts: 32
Joined: 12 Aug 2009, 16:16
Real name: David

Re: functions_cute_url.php

Postby imkingdavid » 13 Mar 2010, 01:55

Alright, I'm having trouble understanding how to use the fix url hook provided recently in this topic. Can you give a walkthrough about it like you did for the functions_cute_url.php file? Thanks!
phpBB.com Development Team Member | View My MODs | View My Website
Please do NOT contact for support via PM or email.


Return to “Library”

Who is online

Users browsing this forum: CommonCrawl [Bot] and 0 guests