[plain text] [download]
  1. <?php
  2. /*
  3. Plugin Name: Stupid URIs
  4. Plugin URI: http://rephrase.net/box/word/stupid/
  5. Description: Hate clean, informative URIs? Make yours stupid.
  6. Author: Sam Angove
  7. Version: 1.0
  8. Author URI: http://rephrase.net
  9. */
  10.  
  11.  
  12.  
  13. // Base class: intercepts query vars coming in, rewrites links going out.
  14.  
  15. class Stupid {
  16.         function decode_callback($matches) {
  17.                 return $matches[1] . '=' . $this->decode($matches[2]) . $matches[3];
  18.         }
  19.        
  20.         function encode_callback($matches) {
  21.                 return $matches[1] . '=' . $this->encode($matches[2]) . $matches[3];
  22.         }
  23.        
  24.         function query($query) {
  25.                 return preg_replace_callback("#(.*?)=(.*?)(&|\z)#s", array($this, 'decode_callback'), $query);
  26.         }
  27.         function category_uri($uri) {
  28.                 global $wp_rewrite;
  29.                 return $this->uri($uri, $wp_rewrite->get_category_permastruct());       
  30.         }
  31.        
  32.         function date_uri($uri) {
  33.                 global $wp_rewrite;
  34.                 return $this->uri($uri, $wp_rewrite->get_date_permastruct());   
  35.         }
  36.         function post_uri($uri) {
  37.                 return $this->uri($uri, get_settings('permalink_structure'));   
  38.         }
  39.        
  40.         function pretty_uri($uri, $structure) {
  41.                 $uri = parse_url($uri);
  42.                 $path = $uri['path'];
  43.                        
  44.                 $home = parse_url(get_settings('home'));
  45.                 $home_path = $home['path'];
  46.                        
  47.                 $newuri = $uri['scheme'] . '://' . $uri['host']. trailingslashit($home_path);
  48.                
  49.                 $path = str_replace($home_path, '', $path);
  50.                 $path = split('/', $path);
  51.                 $structure = split('/', $structure);
  52.                
  53.                 $newpath = array();
  54.                
  55.                 foreach ($path as $key => $segment) {
  56.                         if (trim($segment)) {
  57.                                 if ($structure[$key] != $segment) {
  58.                                         $segment = $this->encode($segment);
  59.                                         $newpath[] = $segment;
  60.                                 } else {
  61.                                         $newpath[] = $segment;
  62.                                 }
  63.                         }
  64.                 }
  65.                 $newuri .= implode('/', $newpath);
  66.                 return $newuri;
  67.         }
  68.        
  69.         function uri($uri, $structure = false) {
  70.                
  71.                 if ($structure) return $this->pretty_uri($uri, $structure);
  72.                
  73.                 $uri = parse_url($uri);
  74.                 $query = preg_replace_callback("#(.*?)=(.*?)(&|\z)#s", array($this, 'encode_callback'), $uri['query']);
  75.                
  76.                 $newuri = $uri['scheme'] . '://' . $uri['host'] . $uri['path'] . '?' . $query;
  77.                
  78.                 return $newuri;
  79.         }
  80.        
  81.         function rewrite($rewrite) {
  82.                
  83.                 static $rewrote = false;
  84.                
  85.                 // infinite loops just aren't cool, dig?
  86.                 if ($rewrote) return $rewrite;
  87.                         else $rewrote = true;
  88.                        
  89.                 if ($this->rules) {
  90.                         foreach ($this->rules as $tag => $what) {
  91.                                 $rewrite->add_rewrite_tag($tag, $what[0], $what[1]);
  92.                         }
  93.                         $rules = $rewrite->rewrite_rules();
  94.                         $rewrite->rules = $rules;
  95.                 }
  96.         }
  97. }
  98.  
  99. // Classes providing the encode/decode functions to make those URIs useless.
  100.  
  101. class Backwards extends Stupid {
  102.        
  103.         function encode($str) {
  104.                 $newstr = '';
  105.                 for ($i=strlen($str)-1; $i>-1; $i--) {
  106.                         $newstr .= substr($str, $i, 1);
  107.                 }
  108.                 return $newstr;
  109.         }       
  110.         function decode($str) {
  111.                 return $this->encode($str);     
  112.         }
  113.        
  114. }
  115.  
  116.  
  117. class Banshee extends Stupid {
  118.        
  119.         var $encode_table = array();
  120.         var $decode_table = array();
  121.         var $rules = array();
  122.        
  123.        
  124.         function Banshee() {
  125.                 $chars = "!0123456789abcdefghijklmnopqrstuvwxyz-_.";
  126.                 $count = strlen($chars);
  127.                 for ($i=1; $i < $count; $i++) {
  128.                         $char = substr($chars, $i, 1);
  129.                         $this->encode_table[$i] = $char;
  130.                         $this->decode_table[$char] = $i;
  131.                 }
  132.                
  133.                 $this->rules['%year%']   = array('([iae]+)', "year=");
  134.                 $this->rules['%monthnum%']      = array('([iae]+)', "monthnum=");
  135.                 $this->rules['%day%']     = array('([iae]+)', "day=");
  136.                 $this->rules['%hour%']    = array('([iae]+)', "hour=");
  137.                 $this->rules['%minute%']        = array('([iae]+)', "minute=");
  138.                 $this->rules['%second%']        = array('([iae]+)', "second=");
  139.                 $this->rules['%post_id%']       = array('([iae]+)', "p=");
  140.         }
  141.        
  142.         function encode($text) {
  143.                 $length = strlen($text);
  144.                 for ($i=0; $i < $length; $i++) {
  145.                         $letter = substr($text, $i, 1);
  146.                         $key = $this->decode_table[$letter];
  147.                         $t = array();
  148.                         $keylength = strlen($key);
  149.                         for ($j = 0; $j < $keylength; $j++) {
  150.                                 $t[] = str_repeat('i', substr($key, $j, 1));   
  151.                         }
  152.                         $space[] = implode($t, 'a');
  153.                 }
  154.                 $text = implode($space, 'e');
  155.                                                        
  156.                 return $text;
  157.         }
  158.        
  159.         function decode($bad) {
  160.                        
  161.                 $url = '';
  162.                 $parts = explode('e', $bad);
  163.                
  164.                 foreach ($parts as $part) {
  165.                         if (strstr($part, 'a')) {
  166.                                 $num = '';
  167.                                 $real = explode('a', $part);
  168.                                 foreach ($real as $r) {
  169.                                         $num .= strlen($r);     
  170.                                 }
  171.                         } else {
  172.                                 $num = strlen($part);
  173.                         }
  174.                         $url .= $this->encode_table[$num];
  175.                 }
  176.                 return $url;
  177.         }
  178. }
  179.  
  180. class Morse extends Stupid {
  181.        
  182.         var $encode_table;
  183.         var $decode_table = array();
  184.         var $rules = array();
  185.        
  186.         function Morse() {
  187.                 $this->encode_table  = array('a' => '.-', 'b' => '-...', 'c' => '-.-.', 'd' => '-..', 'e' => '.',
  188.                         'f' => '..-.', 'g' => '--.', 'h' => '....', 'i' => '..', 'j' => '.---',
  189.                         'k' => '-.-', 'l' => '.-..', 'm' => '--', 'n' => '-.', 'o' => '---',
  190.                         'p' => '.--.', 'q' => '--.-', 'r' => '.-.', 's' => '...', 't' => '-',
  191.                         'u' => '..-', 'v' => '...-', 'w' => '.--', 'x' => '-..-', 'y' => '-.--',
  192.                         'z' => '--..', '0' => '-----', '1' => '.----', '2' => '..---', '3' => '...--',
  193.                         '4' => '....-', '5' => '.....', '6' => '-....', '7' => '--...', '8' => '---..',
  194.                         '9' => '----.', '.' => '.-.-.-', ',' => '--..--', '?' => '..--..', '-' => '------',
  195.                         '%' => '.-----');
  196.                         //note: last few not actual Morse code
  197.                
  198.                 foreach ($this->encode_table as $c => $m) {
  199.                         $this->decode_table[$m] = $c;
  200.                 }
  201.                
  202.                 $this->rules['%year%']   = array('([.\-]{5}_[.\-]{5}_[.\-]{5}_[.\-]{5})_?', "year=");
  203.                 $this->rules['%monthnum%']      = array('([.\-]{5}_[.\-]{5})_?', "monthnum=");
  204.                 $this->rules['%day%']     = array('([.\-]{5}_[.\-]{5})_?', "day=");
  205.                 $this->rules['%hour%']    = array('([.\-]{5}_[.\-]{5})_?', "hour=");
  206.                 $this->rules['%minute%']        = array('([.\-]{5}_[.\-]{5})_?', "minute=");
  207.                 $this->rules['%second%']        = array('([.\-]{5}_[.\-]{5})_?', "second=");
  208.                 $this->rules['%post_id%']       = array('([.\-_]+)_?', "p=");
  209.         }
  210.  
  211.         function encode($str) {
  212.                
  213.                 $chars = array();
  214.                 for ($i=0;$i<strlen($str);$i++) {
  215.                         $char = substr($str, $i, 1);
  216.                         if ($this->encode_table[$char]) {
  217.                                 $chars[] = $this->encode_table[$char];
  218.                         } else {
  219.                                 $chars[] = $char;
  220.                         }
  221.                 }
  222.                 $str = implode('_', $chars);
  223.                
  224.                 // can't let them end with a '.', or we get '.../' etc. in paths
  225.                 if (substr($str, strlen($uri)-1, 1) != '-') $str .= '_';
  226.                 return $str;
  227.         }
  228.         function decode($var) {
  229.                
  230.                 if (!preg_match("#[^.\-_]#", $var)) {
  231.                         if (substr($var, strlen($var)-1, 1) == '_') $var = substr($var, 0, strlen($var)-1);
  232.                         $coded = explode('_', $var);
  233.                         $var = '';
  234.                         foreach ($coded as $c) {
  235.                                 $var .= $this->decode_table[$c];       
  236.                         }
  237.                 }
  238.                 return $var;
  239.         }
  240. }
  241.  
  242.  
  243. class Zxy extends Stupid {
  244.        
  245.         var $table;
  246.        
  247.         function Zxy() {
  248.                 $chars = "abcdefghijklmnopqrstuvwxyz";
  249.                 $back = strrev($chars);
  250.                 $count = strlen($chars);
  251.                 for ($i=0; $i < $count; $i++) {
  252.                         $char = substr($chars, $i, 1);
  253.                         $bchar = substr($back, $i, 1);
  254.                         $this->table[$char] = $bchar;
  255.                 }
  256.         }
  257.        
  258.         function encode($str) {
  259.                 $length = strlen($str);
  260.                 for ($i=0; $i<$length; $i++) {
  261.                         $char = substr($str, $i, 1);
  262.                        
  263.                         $add = ($this->table[$char]) ? $this->table[$char] : $char;
  264.                         $newstr .= $add;
  265.                 }
  266.                 return $newstr;
  267.         }       
  268.         function decode($str) {
  269.                 return $this->encode($str);     
  270.         }
  271.        
  272. }
  273.  
  274.  
  275. $stupid = new Backwards();
  276.  
  277. add_filter('query_string', array($stupid, 'query'));
  278. add_filter('year_link', array($stupid, 'date_uri'));
  279. add_filter('month_link', array($stupid, 'date_uri'));
  280. add_filter('day_link', array($stupid, 'date_uri'));
  281. add_filter('post_link', array($stupid, 'post_uri'));
  282. add_filter('category_link', array($stupid, 'category_uri'));
  283. add_filter('generate_rewrite_rules', array($stupid, 'rewrite'));
  284.  
  285. ?>