Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Rewrite URLs
- Version: 2.3.6
- Plugin URI: http://www.pierovdfn.it/redirect/plugin_rewriteurls.html
- Description: Rewrite URLs
- Author: Piero VDFN
- Author URI: http://www.pierovdfn.it/
- */
- /**
- * Note: this is not a fork of PrettyURLs because it is rewritten from scratch,
- * but it has some defects of Pretty URLs because some code is from this plugin
- * but only a few.
- * It has too his features, for example using index.php/Params form or his link
- * and it uses the same settings for mod_rewrite.
- *
- * Thanks to NoWhereMan for the help.
- */
- /**
- * Use /category/mycat (true) or /category/1 (false)?
- * It's also /2010/09/24/my-title/ or /2010/09/24/entry100924-154800/
- * If you edit this, delete cache from control panel or it could not work.
- */
- define('REWRITEURLS_TITLES', true);
- /**
- * What kind of URL do you want?
- * By default it uses 0 or 1 (it depends on .htaccess file) but you can force
- * a mode removing the "#" (that means comment) from the beginning of a line.
- * However you must declare the mode just one time, so just one of lines 34 to
- * 37 has not to be a comment.
- */
- #define('REWRITEURLS_MODE', intval(!file_exists(ABS_PATH . '.htaccess')));
- #define('REWRITEURLS_MODE', 0); # Force /myurl/params
- #define('REWRITEURLS_MODE', 1); # Force /myurl/index.php/params
- define('REWRITEURLS_MODE', 2); # Force /myurl/?u=/params
- /**
- * This constant is very important: don't edit if you don't know Flatpress.
- *
- * Explanation: the post titles are saved in a directory that you obtain with
- * entry_dir($id). The plugins saves them during the publish_post hook.
- * In the revision 340 of Flatpress (that means between 0.909.1 and 0.1010),
- * the position of do_action('publish_post') has been changed: after that it
- * is called after moving entry to drafts so we have to use draft instead of
- * entry to modify the title.
- *
- * This constant so mean entry or draft?
- */
- define('REWRITEURLS_DRAFT', version_compare(SYSTEM_VER, '0.1010', '>=')==1);
- class plugin_rewriteurls {
- /**
- * This array contains the cache.
- *
- * @var array
- */
- var $cache=array();
- /**
- * This array contains cached titles.
- *
- * @var array
- */
- var $titles=array();
- /**
- * This is the REWRITEURLS_MODE.
- *
- * @var integer
- */
- var $mode=null;
- /**
- * This is the base URL.
- *
- * @var string
- */
- var $baseurl='';
- /**
- * This is the original URL.
- *
- * @var string
- */
- var $originalurl='';
- /**
- * This is the current URL
- *
- * @var string
- */
- var $url='';
- /**
- * This is the array of URL tokens.
- *
- * @var array
- */
- var $tokens=array();
- /**
- * Here the global variable $fp_params is saved by reference.
- *
- * @var array
- */
- var $fp_params;
- # Init
- /**
- * This function is the constructor of the class.
- * It assigns by reference $GLOBALS['fp_params'] to $this->fp_params.
- * It also sets hooks.
- */
- function plugin_rewriteurls() {
- global $fp_params;
- $this->fp_params=&$fp_params;
- # Params
- add_filter('init', array($this, 'params_init'));
- # Links
- add_filter('post_link', array($this, 'permalink'), 0, 2);
- add_filter('comments_link', array($this,'commentlink'),0,2);
- add_filter('feed_link', array($this, 'feedlink'), 0, 2);
- add_filter('post_comments_feed_link', array($this, 'commentsfeedlink'), 0, 3);
- add_filter('category_link', array($this, 'categorylink'), 0, 2);
- add_filter('page_link', array($this, 'staticlink'), 0, 2);
- add_filter('year_link', array($this, 'yearlink'), 0, 2);
- add_filter('month_link', array($this, 'monthlink'), 0, 3);
- add_filter('day_link', array($this, 'daylink'), 0, 4);
- # Cache for title
- if(REWRITEURLS_TITLES) {
- add_filter('publish_post', array($this, 'addToCache'), 10, 2);
- add_filter('delete_post', array($this, 'deleteFromCache'));
- add_action('update_categories', array($this, 'makeCache'));
- }
- }
- /**
- * This function initializes the parameters.
- *
- * It's a callback of the init hook.
- */
- function params_init() {
- $this->initTokens();
- // If cache does not exist, we make it
- if(!file_exists(CACHE_DIR.'rewriteurls-cats.tmp')) {
- $this->makeCache('all');
- }
- // If we're not in index, we don't do anything
- if (!defined('MOD_INDEX')) {
- return;
- }
- $cur=$this->getToken();
- if(empty($cur)) {
- } elseif(preg_match('/(20[0-9]{2}|category|feed|page)/', $cur)) {
- # Entries
- if(!$this->handleEntry()) {
- return;
- }
- # Handle feeds
- $cur=$this->getToken();
- if($cur=='feed') {
- $cur=$this->nextToken();
- $cur=$cur===FALSE ? 'rss2' : $cur;
- $this->fp_params['feed']=$cur;
- $cur=$this->nextToken();
- }
- # Handle page / paged
- $cur=$this->getToken();
- if($cur=='page') {
- $cur=$this->nextToken();
- if(is_numeric($cur)) {
- $this->fp_params['paged']=$cur;
- $cur=$this->nextToken();
- }
- }
- # Handle possible broken medium link
- if(!$this->checkForMedia() && !empty($cur)) {
- $this->send404();
- }
- } elseif($this->countTokens()==1) {
- # Static pages
- $this->fp_params['page']=$cur;
- } else {
- # Error 404
- $this->send404();
- }
- return;
- }
- /**
- * This function sends a 404 Error.
- */
- function send404() {
- // PrettyURL's method do send 404: a fake entry!
- $this->fp_params['entry']='entry000000-000000';
- // Get what remain of URL and send it to plugins
- $url=$this->getTokensUrl(true);
- $url=apply_filters('prettyurls_unhandled_url', $url);
- }
- /**
- * This function handles the entries.
- *
- * @return boolean: Can we continue?
- */
- function handleEntry() {
- $cur=$this->getToken();
- $fp_params=&$this->fp_params;
- // Handle the category
- if($cur=='category') {
- # Handle category
- $cur=$this->nextToken();
- if(REWRITEURLS_TITLES) {
- $cats=$this->loadCache('cats');
- $catid=array_search($cur, $cats);
- if($catid===false) {
- $this->send404();
- return false;
- }
- $fp_params['cat']=$catid;
- $this->nextToken();
- } elseif(is_numeric($cur)) {
- $fp_params['cat']=$cur;
- $this->nextToken();
- } else {
- $this->send404();
- return false;
- }
- return true;
- }
- // If it's a blog post, $cur must always be numeric
- if(!is_numeric($cur)) {
- return true;
- }
- // Year
- $fp_params['y']=$y=substr($cur, 2, 2);
- # Month
- $cur=$this->nextToken();
- if(!is_numeric($cur) || $cur<1 || $cur>12) {
- return true;
- }
- $fp_params['m']=$m=$cur;
- # Day
- $cur=$this->nextToken();
- if(!is_numeric($cur) || $cur<1 || $cur>31) {
- return true;
- }
- $fp_params['d']=$d=$cur;
- # Entry Title/Id
- $cur=$this->nextToken();
- if(empty($cur)) {
- // It was just a day
- } elseif(REWRITEURLS_TITLES) {
- // Transform the title to the id
- $month_cache=&$this->loadCache($y.$m);
- // Check only in the cache
- if(!isset($month_cache[$d][$cur])) {
- $this->send404();
- return false;
- }
- $fp_params['entry']=$month_cache[$d][$cur];
- } else {
- // $cur is the id
- $fp_params['entry']=$cur;
- }
- # Comments
- $cur=$this->nextToken();
- if($cur=='comments') {
- $fp_params['comments']=true;
- $this->nextToken();
- }
- return true;
- }
- /**
- * Check if there a medium file with a wrong URL in tokens.
- *
- * @return boolean: Is it a medium?
- */
- function checkForMedia() {
- $cur=$this->getToken();
- // Check for symbolic images/ and attachs/
- if($cur=='images' || $cur=='attachs') {
- // Get the directory from the constants
- $cur=strtoupper($cur);
- @$cur=constant($cur.'_DIR');
- }
- // Add the trailing slash
- if(substr($cur, -1, 1)!='/') {
- $cur.='/';
- }
- // Check if it is *NOT* an known directory
- if($cur!=FP_CONTENT && $cur!=PLUGINS_DIR && $cur!=IMAGES_DIR && $cur!=ATTACHS_DIR) {
- return false;
- }
- // Get the full URL
- $this->nextToken();
- $medium=$cur.$this->getTokensUrl(true);
- $medium=substr($medium, 0, -1);
- // Check if it exists
- if(file_exists($medium)) {
- // It exists :-) Do the redirect
- @utils_status_header(301);
- @header('Location: '.BLOG_BASEURL.$medium);
- die;
- return true;
- } else {
- // It doesn't exist but it's a medium, so send a 404
- $this->prevToken();
- $this->send404();
- return false;
- }
- }
- # Tokens
- /**
- * This function initializes the URL tokens.
- */
- function initTokens() {
- $url=$this->get_url();
- // Remove the query string
- if(($pos=plru_strrpos($url, '?'))!==false) {
- $url=substr($url, 0, $pos);
- }
- // Modify the URL via plugins
- $url=apply_filters('rewriteurls_replace', $url);
- // Get tokens and clean them
- $tokens=explode('/', $url);
- $this->tokens=&$tokens;
- $tokens=array_filter($tokens);
- $tokens=array_values($tokens);
- // If there isn't any token, we can return here
- if(count($tokens)==0) {
- $this->originalurl = '/';
- return true;
- }
- // Remove index.php
- if($tokens[0] == 'index.php') {
- unset($tokens[0]);
- $tokens = array_values($tokens);
- }
- $this->originalurl = implode('/', $tokens);
- // Go to position 0
- reset($tokens);
- return true;
- }
- /**
- * This function get the current token.
- *
- * @return mixed: The token
- */
- function getToken() {
- return current($this->tokens);
- }
- /**
- * This function seeks the tokens.
- *
- * @param integer $offset: How much do I have to move?
- * @return mixed: The current value
- */
- function seekToken($offset) {
- if($offset==0) {
- return $this->getToken();
- }
- $func=$offset < 0 ? 'prev' : 'next';
- $offset=abs($offset);
- $last=false;
- for($i=1; $i<=$offset; $i++) {
- $last=call_user_func_array($func, array(&$this->tokens));
- }
- return $last;
- }
- /**
- * This function goes to next token.
- *
- * @return mixed: The current value
- */
- function nextToken() {
- return next($this->tokens);
- }
- /**
- * This function goes to previous token.
- *
- * @return mixed: The current value
- */
- function prevToken() {
- return prev($this->tokens);
- }
- /**
- * This function return the number of tokens.
- *
- * @return integer: The number of tokens
- */
- function countTokens() {
- return count($this->tokens);
- }
- /**
- * This function gets the current URL from tokens
- *
- * @param boolean $leftovers: Just return leftovers
- * @return string: The URL
- */
- function getTokensUrl($leftovers=false) {
- // Return just leftover tokens
- if($leftovers) {
- $url=$this->getToken().'/';
- $i=0;
- while($last=$this->nextToken()) {
- $url.=$last.'/';
- $i++;
- }
- $this->seekToken(-$i);
- return $url;
- }
- // Return complete token
- return implode('/', $this->tokens).'/';
- }
- /**
- * This function has been taken from PrettyURLs.
- * It gets the mode, the baseurl and the current URL.
- */
- function get_url() {
- $baseurl=BLOG_BASEURL;
- if(defined('REWRITEURLS_MODE')) {
- $opt=REWRITEURLS_MODE;
- } else {
- $opt=null;
- }
- $url=substr($_SERVER['REQUEST_URI'], strlen(BLOG_ROOT)-1);
- switch($opt) {
- case 1:
- $baseurl .= 'index.php/';
- // Don't break, because also 0 and null uses PATH_INFO
- case null:
- case 0:
- if(!empty($_SERVER['PATH_INFO'])) {
- $url=$_SERVER['PATH_INFO'];
- }
- break;
- case 2:
- $baseurl .= '?u=/';
- // Use just $_GET, not $fp_params because we want the raw one
- $url = @$_GET['u'];
- break;
- }
- $this->baseurl=$baseurl;
- $this->mode=$opt;
- $this->originalurl=$url;
- return $url;
- }
- # Database
- /**
- * This function makes the cache of titles (for entry and categories).
- * By default it makes only categories cache.
- *
- * @param string $w: Type of cache (cat|entry|both)
- * @param boolean $force:
- * @return boolean: True
- */
- function makeCache($w='cat', $force=false) {
- // We have to do so to handle categories with the boolean true.
- if($w===true) {
- $w='cat';
- }
- // Don't create cache if the titles mode is off.
- if(!$force && !REWRITEURLS_TITLES) {
- return true;
- }
- // Do the categories
- if($w!='entries') {
- # $w can be cat but also both ;)
- $this->makeCatCache();
- }
- if($w!='cat') {
- # $w can be entry but also both ;)
- $this->makeEntryCache();
- }
- if(count($this->cache) && is_array($this->cache)) {
- // Sort by title every cache file to be faster to find an entry
- foreach($this->cache as $k=>$v) {
- asort($this->cache[$k]);
- }
- }
- @$this->saveCache();
- // In every case we return true!
- // We don't want entry save function to fail!
- return true;
- }
- /**
- * Make the cache of the categories.
- *
- * @return boolean: Process ok?
- */
- function makeCatCache() {
- $d=entry_categories_get('defs');
- $this->cache['cats']=array();
- if(!count($d) || !is_array($d)) {
- return false;
- }
- $list=array();
- foreach($d as $k=>$v) {
- $v=urldecode(sanitize_title($v));
- // Check for doubles
- $a=$v;
- $i=2;
- while(in_array($a, $list)) {
- $a=$v.'-'.$i;
- $i++;
- }
- $list[$k]=$a;
- }
- $this->cache['cats']=$list;
- return @$this->saveCache()>0;
- }
- /**
- * Make the cache of entries.
- *
- * @return boolean: Process ok?
- */
- function makeEntryCache() {
- // Query the FPDB to have all entries
- $o=new FPDB_Query(array('start'=>0, 'count'=>-1, 'fullparse'=>false), null);
- // Inizialize the temporary list
- $list=array();
- // Fill the temporary list with entry titles
- while($o->hasMore()) {
- list($id, $entry)=$o->getEntry();
- $list[$id]=urldecode(sanitize_title($entry['subject']));
- $this->cache[substr($id, 5, 4)]=array();
- }
- // If the temporary list is empty, it doesn't make sense to continue
- if(count($list)==0) {
- return false;
- }
- // Sort list by keys so we have older entries before
- ksort($list);
- foreach($list as $id=>$v) {
- // Cache name and day
- $cname=substr($id, 5, 4);
- $day=substr($id, 9, 2);
- // Do we have already the title or do we have to make it?
- $file=entry_dir($id).'/rewriteurls.txt';
- if(file_exists($file)) {
- // We've already the sanitized title
- $title=io_load_file($file);
- $this->cache[$cname][$day][$title]=$id;
- } else {
- // We make the sanitized title
- $a=$v;
- // Check for doubles
- $i=2;
- while(isset($this->cache[$cname][$day][$a])) {
- $a=$v."-$i";
- $i++;
- }
- // Save the title
- $this->cache[$cname][$day][$a]=$id;
- io_write_file($file, $a);
- }
- }
- return @$this->saveCache()>0;
- }
- /**
- * This function saves the cache of the plugin.
- *
- * @return integer: The number of files saved
- */
- function saveCache() {
- // No file to save? Return false!
- if(!count($this->cache)) {
- return false;
- }
- $i=0;
- foreach($this->cache as $file=>$list) {
- io_write_file(CACHE_DIR."rewriteurls-$file.tmp",
- serialize($list)) ? $i++ : null;
- }
- // Return the number of saved files
- return $i;
- }
- /**
- * This function load a file of cache.
- *
- * @param string $name: The cache name
- * @param boolean $force: Force loading?
- * @return array: The cache
- */
- function &loadCache($name, $force=false) {
- // Don't reload if already in $this->cache and not forced
- if(!empty($this->cache[$name]) && !$force) {
- return $this->cache[$name];
- }
- $cache = io_load_file(CACHE_DIR . "rewriteurls-$name.tmp");
- if(!empty($cache)) {
- $cache = @unserialize($cache);
- }
- // Force $cache to be an array
- if(!is_array($cache)) {
- $cache = array();
- }
- $this->cache[$name] = $cache;
- return $this->cache[$name];
- }
- /**
- * This function returns the entry title by its id.
- *
- * @param string $id: The entry id
- * @param boolean $force: Force to get the title
- * @return string: The entry title sanitized
- */
- function getEntryTitleById($id, $force=false) {
- // If the title mode is off, the id is the title
- if(!REWRITEURLS_TITLES) {
- return $id;
- }
- // If we stored in $this->titles, return the stored value
- if(!$force && isset($this->titles[$id])) {
- return $this->titles[$id];
- }
- // It's useless to search for a title if the entry doesn't exist.
- if(!entry_exists($id)) {
- return false;
- }
- $file=entry_dir($id).'/rewriteurls.txt';
- // Do we have already the title file?
- if(!file_exists($file)) {
- // If the title file doesn't exist, trigger a fake entry publishing
- $o=new FPDB_Query(array('count'=>1, 'fullparse'=>false, 'id'=>$id), null);
- $o->hasMore();
- $arr=$o->getEntry();
- $this->addToCache($id, $arr[1], false);
- } else {
- // Otherwise we load it from the file
- $this->titles[$id]=trim(io_load_file($file));
- }
- return $this->titles[$id];
- }
- /**
- * This function is the callback called when an entry is added
- * or edited. It adds the title to the cache.
- *
- * @param string $id: The entry id
- * @param array $arr: The entry data
- * @param boolean $draft: Should I check if it's a draft?
- * @return boolean: Always true
- */
- function addToCache($id, $arr, $draft = true) {
- $dfile = draft_dir($id).'/rewriteurls.txt';
- $efile = entry_dir($id).'/rewriteurls.txt';
- // Flatpress >= 0.1010: we need to pay attention to drafts!
- if(REWRITEURLS_DRAFT && $draft) {
- if(file_exists($efile) && !file_exists($dfile)) {
- rename($efile, $dfile);
- }
- $file = $dfile;
- } else {
- $file = $efile;
- }
- // Load the cache for the entry month
- $cachename = substr($id, 5, 4);
- $d = substr($id, 9, 2);
- $cache = &$this->loadCache($cachename);
- // The candidate subject
- $subj = urldecode(sanitize_title($arr['subject']));
- if(file_exists($file)) {
- // Load the current title
- $now = io_load_file($file);
- // Trim because manual edits can add newlines (for example Gedit does)
- $now = trim($now);
- // If current the title if the same, our work is finished here
- if($now == $subj || preg_match("/{$subj}-([0-9])*/", $now)){
- $this->titles[$id] = $now;
- return true;
- }
- // Otherwise we remove the current title from the cache
- if(isset($cache[$d][$now])) {
- unset($cache[$d][$now]);
- }
- }
- // Look for doubles
- $a = $subj;
- $i = 2;
- while(isset($cache[$d][$a])) {
- $a = $subj."-$i";
- $i++;
- }
- // Save the title
- $a = trim($a);
- io_write_file($file, $a);
- $cache[$d][$a] = $id;
- @$this->saveCache();
- $this->titles[$id] = $a;
- // Return always true, as it's an hook handler
- return true;
- }
- /**
- * This function is the callback called when an entry is removed
- * or edited. It deletes the title from the cache.
- *
- * @param string $id: The entry id
- * @return boolean: True
- */
- function deleteFromCache($id) {
- // Load the cache
- $cachen = substr($id, 5, 4);
- $day = substr($id, 9, 2);
- $cache = &$this->loadCache($cachen);
- // Remove from the cache
- if(isset($cache[$day]) && is_array($cache[$day])) {
- $key = array_search($id, $cache[$day]);
- if($key !== false && isset($cache[$day][$key])) {
- unset($cache[$day][$key]);
- }
- }
- @$this->saveCache();
- // Remove the file from the entry directory
- $file = entry_dir($id).'/rewriteurls.txt';
- if(file_exists($file)) {
- @unlink($file);
- }
- @rmdir(dirname($file));
- // Return always true, as it's an hook handler
- return true;
- }
- # Links
- /**
- * Get the permalink of an entry
- *
- * @param string $str: The default link
- * @param string $id: The entry ID
- * @return string: The link
- */
- function permalink($str, $id) {
- $u=$this->baseurl;
- $d=date_from_id($id);
- $u.="20{$d['y']}/{$d['m']}/{$d['d']}/";
- // Auto check if we don't use titles: it returns the id
- if(($title = $this->getEntryTitleById($id)) !== FALSE) {
- $u .= urlencode($title) . '/';
- } else {
- $u=$str;
- }
- return $u;
- }
- /**
- * Get the comment link of an entry
- *
- * @param string $str: The default link
- * @param string $id: The entry ID
- * @return string: The link
- */
- function commentlink($str, $id) {
- return $this->permalink($str, $id) . 'comments/';
- }
- /**
- * Get the link of feeds
- *
- * @param string $str: The default link
- * @param string $type: The feed type
- * @return string: The link
- */
- function feedlink($str, $type) {
- return $this->baseurl."feed/$type/";
- }
- /**
- * Get the link of feeds of comments of an entry
- *
- * @param string $str: The default link
- * @param string $type: The feed type
- * @param string $id: The entry ID
- * @return string: The link
- */
- function commentsfeedlink($str, $type, $id) {
- return $this->commentlink($str, $id)."feed/$type/";
- }
- /**
- * Get the link of a static page
- *
- * @param string $str: The default link
- * @param string $id: The page ID
- * @return string: The link
- */
- function staticlink($str, $id) {
- return $this->baseurl.$id.'/';
- }
- /**
- * Get the link of a category
- *
- * @param string $str: The default link
- * @param string $catid: The category ID
- * @return string: The link
- */
- function categorylink($str, $catid) {
- $u=$this->baseurl.'category/';
- if(REWRITEURLS_TITLES) {
- $cats=$this->loadCache('cats');
- if(isset($cats[$catid])) {
- $u .= urlencode($cats[$catid]) . '/';
- } else {
- $u = $str;
- }
- } else {
- $u .= "{$catid}/";
- }
- return $u;
- }
- /**
- * Get the link of a year
- *
- * @param string $str: The default link
- * @param integer $y: The year
- * @return string: The link
- */
- function yearlink($str, $y) {
- return $this->baseurl."20$y/";
- }
- /**
- * Get the link of a month
- *
- * @param string $str: The default link
- * @param integer $y: The year
- * @param integer $m: The month
- * @return string: The link
- */
- function monthlink($str, $y, $m) {
- return $this->baseurl."20$y/$m/";
- }
- /**
- * Get the link of a day
- *
- * @param string $str: The default link
- * @param integer $y: The year
- * @param integer $m: The month
- * @param integer $d: The day
- * @return string: The link
- */
- function daylink($str, $y, $m, $d) {
- return $this->baseurl."20$y/$m/$d";
- }
- /**
- * Get the link of the next/prev page
- *
- * @param integer $v: The verse of the link (1=Next, -1=Prev)
- * @return string: The link
- */
- function nextprevlink($v) {
- $fp_params = &$this->fp_params;
- $func = $v<0 ? 'Prev' : 'Next';
- $func = "get{$func}Page";
- // Code from PrettyURLs
- global $fpdb;
- $q=&$fpdb->getQuery();
- list($caption, $id)=call_user_func(array(&$q, $func));
- // End Of PrettyURLs Code
- // When the caption is empty, there isn't any link so stop here
- if(empty($caption)) {
- return array();
- }
- /*
- * Get the params of this page.
- * Use _GET instead of $fp_params because this one can be modified by plugins.
- */
- $getparams = $_GET;
- $xparams = array();
- if(isset($getparams['x'])) {
- $xparams = utils_kexplode($getparams['x'], ':;', false);
- }
- if(!isset($fp_params['paged'])) {
- $fp_params['paged'] = 1;
- }
- // There could be conflicts, however only with $_GET and not x.
- if(REWRITEURLS_MODE == 2 && isset($getparams['u'])) {
- unset($getparams['u']);
- }
- if ($q->single) {
- // Single entry
- // Get the link
- if($fp_params['comments']) {
- $url=$this->commentlink('', $id);
- } else {
- $url=$this->permalink('', $id);
- }
- # Remove entry from $fp_params
- if(isset($getparams['entry'])) {
- unset($getparams['entry']);
- }
- if(isset($xparams['entry'])) {
- unset($xparams['entry']);
- }
- // Remove comments from $fp_params
- if(isset($getparams['comments'])) {
- unset($getparams['comments']);
- }
- if(isset($xparams['comments'])) {
- unset($xparams['comments']);
- }
- # From PrettyURLs: add « and »
- if($v>0) {
- $caption.=' » ';
- } else {
- $caption=' « '.$caption;
- }
- # End of code from PrettyURLs
- } else {
- // A listing page
- // Don't print if page < 1
- if($fp_params['paged'] + $v < 1) {
- return array();
- }
- // Initial URL
- $url = $this->original_url == '/' ? '' : $this->originalurl;
- $url .= substr($url, -1, 1) != '/' ? '/' : '';
- # Search for page/ param
- $pagepos = plru_strrpos($url, 'page/');
- # It there isn't, we add it
- if($pagepos === FALSE) {
- $pagepos = strlen($url);
- $url .= 'page/' . $fp_params['paged'] . '/';
- }
- // 5 is the length of "page/"
- $pagepos += 5;
- $page = $fp_params['paged'] + $v;
- $add = strpos($url, '/', $pagepos);
- $add = $add !== FALSE ? substr($url, $add) : '/';
- $url = substr($url, 0, $pagepos) . $page . $add;
- # Remove paged from other params
- if(isset($getparams['paged'])) {
- unset($getparams['paged']);
- }
- if(isset($xparams['paged'])) {
- unset($xparams['paged']);
- }
- // Remove any double slash
- $url = str_replace('//', '/', $url);
- $url = substr($url, 0, 1)=='/' ? substr($url, 1) : $url;
- $url = $this->baseurl . $url;
- }
- # Rebuild query string
- if(count($xparams)) {
- // We don't care for the moment about urlencoding this one
- $getparams['x'] = plru_implode(':', ';', $xparams);
- } elseif(isset($getparams['x'])) {
- unset($getparams['x']);
- }
- if(!empty($getparams)) {
- $getparams = plru_implode('=', '&', $getparams, 'urlencode');
- $url .= '?' . $getparams;
- }
- return array($caption, $url);
- }
- }
- // Init the class
- global $plugin_rewriteurls;
- $plugin_rewriteurls=new plugin_rewriteurls();
- // Next and Prev Links
- if(!defined('MOD_ADMIN_PANEL') && !function_exists('get_nextpage_link')) {
- /**
- * Flatpress uses the Wordpress method to obtain the nextpage link:
- * they use just one function.
- *
- * So we declare this function but is a shortcut for the class
- *
- * @return array
- */
- function get_nextpage_link() {
- global $plugin_rewriteurls;
- $couple=$plugin_rewriteurls->nextprevlink(1);
- return apply_filters('rewriteurls_nextpage', $couple);
- }
- /**
- * Flatpress uses the Wordpress method to obtain the prevpage link:
- * they use just one function.
- *
- * So we declare this function but is a shortcut for the class.
- *
- * @return array
- */
- function get_prevpage_link() {
- global $plugin_rewriteurls;
- $couple=$plugin_rewriteurls->nextprevlink(-1);
- return apply_filters('rewriteurls_prevpage', $couple);
- }
- }
- /**
- * This function is an hack for strrpos function in PHP 4.
- * In PHP 4 strrpos it's stupid, so I had to write an hack.
- *
- * @author: Piero VDFN <vogliadifarniente@gmail.com>
- * @url: http://www.vdfn.altervista.org/2011/01/08/strrpos/comments/
- * @license: Public Domain
- * @param string $haystack: The string where search $needle
- * @param string $needle: The string we need to search
- * @param integer $offset: The offset (see PHP Manual)
- * @return integer: The position of the $needle
- */
- function plru_strrpos($haystack, $needle, $offset=0) {
- if(version_compare(PHP_VERSION, '5.0.0', '<')) {
- # PHP 4 :-(
- $oldpos=false;
- $len=strlen($needle);
- if($offset<0) {
- $haystack=substr($haystack, 0, $offset);
- $offset=0;
- }
- $off=$offset;
- while(($pos=strpos($haystack, $needle, $off))!==false) {
- $off=$pos+$len;
- $oldpos=$pos;
- }
- return $oldpos;
- }
- return strrpos($haystack, $needle, $offset);
- }
- /**
- * Join an array, like implode but is also uses keys.
- *
- * @author: Piero VDFN <vogliadifarniente@gmail.com>
- * @url: http://www.pierovdfn.it/2011/06/11/php-implode-considerando-anche-le-chiavi/comments/
- * @license: Public Domain
- * @param string $glueval: glue for keys and values
- * @param string $gluekey: glue for couples
- * @param array $array: The array to join
- * @param callback $filter: A filter for elements
- * @return string: The joined array
- */
- function plru_implode($glueval, $gluekey, $array, $filter = '') {
- if(count($array) == 0) {
- return '';
- }
- $r = '';
- foreach($array as $key => $val) {
- if(is_callable($filter)) {
- $key = call_user_func($filter, $key);
- $val = call_user_func($filter, $val);
- }
- $r .= $key . $glueval . $val . $gluekey;
- }
- $r = substr($r, 0, -strlen($gluekey));
- return $r;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement