# # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # These functions help you implement a tinyurl service (for example, http://tinyurl.com/) # tinyurl_next_id - Given a tinyurl identifier, return the next identifer in the sequence. function tinyurl_next_id($id) { # Canonicalize the id $id = tinyurl_canonical_id($id); # Set the list of base characters. # NB: must be regexp-safe, url-safe, and filename-safe $basechars = 'acefjkmnpqrsuxy01234689'; # Convert the id into an array of numbers $idlen = strlen($id); $ida = array(); for ($i = 0; $i < $idlen; $i++) { $ida[] = strpos($basechars, substr($id, $i, 1)); } while (true) { # Increment the array of numbers $p = count($ida)-1; $num_basechars = strlen($basechars); while (true) { $ida[$p]++; if ($ida[$p] < $num_basechars) { break; } $ida[$p] = 0; $p--; if ($p < 0) { array_unshift($ida, 0); $p++; } } # Convert the array of numbers back into an id $idlen = count($ida); $retval = array(); for ($i = 0; $i < $idlen; $i++) { $retval[] = $basechars[$ida[$i]]; } $retval = implode('', $retval); # Political correctness: Never return certain blacklisted ids. # A lot of ids that would be blacklisted here aren't because they're # impossible to create (canonically) given the base characters. $tryagain = false; foreach (array('ass', 'arse', 'cock', 'fuck', 'pussy') as $npcword) { if (strpos($retval, $npcword) !== false) { $tryagain = true; break; } } if ($tryagain) { continue; } break; } return $retval; } # tinyurl_canonical_id - Given a tinyurl identifier, return the canonical # tinyurl identifier. This is needed since, for example, the identifiers "a1" # and "Al" are considered equivalent, in order to make the tinyurls easier to # read if handwritten, for example. function tinyurl_canonical_id($id) { # Lowercase $id = strtolower($id); # Remove invalid chars $id = preg_replace('/[^0-9a-z]/s', '', $id); # Replace character-equivalents $id = strtr($id, 'odli7tbgh5wvz', '0a111f69nsuu2'); return $id; }