Advertisement
Guest User

Untitled

a guest
Aug 1st, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.98 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Rewrite URLs
  4. Version: 2.3.6
  5. Plugin URI: http://www.pierovdfn.it/redirect/plugin_rewriteurls.html
  6. Description: Rewrite URLs
  7. Author: Piero VDFN
  8. Author URI: http://www.pierovdfn.it/
  9. */
  10.  
  11. /**
  12. * Note: this is not a fork of PrettyURLs because it is rewritten from scratch,
  13. * but it has some defects of Pretty URLs because some code is from this plugin
  14. * but only a few.
  15. * It has too his features, for example using index.php/Params form or his link
  16. * and it uses the same settings for mod_rewrite.
  17. *
  18. * Thanks to NoWhereMan for the help.
  19. */
  20.  
  21. /**
  22. * Use /category/mycat (true) or /category/1 (false)?
  23. * It's also /2010/09/24/my-title/ or /2010/09/24/entry100924-154800/
  24. * If you edit this, delete cache from control panel or it could not work.
  25. */
  26. define('REWRITEURLS_TITLES', true);
  27.  
  28. /**
  29. * What kind of URL do you want?
  30. * By default it uses 0 or 1 (it depends on .htaccess file) but you can force
  31. * a mode removing the "#" (that means comment) from the beginning of a line.
  32. * However you must declare the mode just one time, so just one of lines 34 to
  33. * 37 has not to be a comment.
  34. */
  35. #define('REWRITEURLS_MODE', intval(!file_exists(ABS_PATH . '.htaccess')));
  36. #define('REWRITEURLS_MODE', 0); # Force /myurl/params
  37. #define('REWRITEURLS_MODE', 1); # Force /myurl/index.php/params
  38. define('REWRITEURLS_MODE', 2); # Force /myurl/?u=/params
  39.  
  40. /**
  41. * This constant is very important: don't edit if you don't know Flatpress.
  42. *
  43. * Explanation: the post titles are saved in a directory that you obtain with
  44. * entry_dir($id). The plugins saves them during the publish_post hook.
  45. * In the revision 340 of Flatpress (that means between 0.909.1 and 0.1010),
  46. * the position of do_action('publish_post') has been changed: after that it
  47. * is called after moving entry to drafts so we have to use draft instead of
  48. * entry to modify the title.
  49. *
  50. * This constant so mean entry or draft?
  51. */
  52. define('REWRITEURLS_DRAFT', version_compare(SYSTEM_VER, '0.1010', '>=')==1);
  53.  
  54. class plugin_rewriteurls {
  55. /**
  56. * This array contains the cache.
  57. *
  58. * @var array
  59. */
  60. var $cache=array();
  61.  
  62. /**
  63. * This array contains cached titles.
  64. *
  65. * @var array
  66. */
  67. var $titles=array();
  68.  
  69. /**
  70. * This is the REWRITEURLS_MODE.
  71. *
  72. * @var integer
  73. */
  74. var $mode=null;
  75.  
  76. /**
  77. * This is the base URL.
  78. *
  79. * @var string
  80. */
  81. var $baseurl='';
  82.  
  83. /**
  84. * This is the original URL.
  85. *
  86. * @var string
  87. */
  88. var $originalurl='';
  89.  
  90. /**
  91. * This is the current URL
  92. *
  93. * @var string
  94. */
  95. var $url='';
  96.  
  97. /**
  98. * This is the array of URL tokens.
  99. *
  100. * @var array
  101. */
  102. var $tokens=array();
  103.  
  104. /**
  105. * Here the global variable $fp_params is saved by reference.
  106. *
  107. * @var array
  108. */
  109. var $fp_params;
  110.  
  111. # Init
  112. /**
  113. * This function is the constructor of the class.
  114. * It assigns by reference $GLOBALS['fp_params'] to $this->fp_params.
  115. * It also sets hooks.
  116. */
  117. function plugin_rewriteurls() {
  118. global $fp_params;
  119. $this->fp_params=&$fp_params;
  120.  
  121. # Params
  122. add_filter('init', array($this, 'params_init'));
  123.  
  124. # Links
  125. add_filter('post_link', array($this, 'permalink'), 0, 2);
  126. add_filter('comments_link', array($this,'commentlink'),0,2);
  127. add_filter('feed_link', array($this, 'feedlink'), 0, 2);
  128. add_filter('post_comments_feed_link', array($this, 'commentsfeedlink'), 0, 3);
  129. add_filter('category_link', array($this, 'categorylink'), 0, 2);
  130. add_filter('page_link', array($this, 'staticlink'), 0, 2);
  131. add_filter('year_link', array($this, 'yearlink'), 0, 2);
  132. add_filter('month_link', array($this, 'monthlink'), 0, 3);
  133. add_filter('day_link', array($this, 'daylink'), 0, 4);
  134.  
  135. # Cache for title
  136. if(REWRITEURLS_TITLES) {
  137. add_filter('publish_post', array($this, 'addToCache'), 10, 2);
  138. add_filter('delete_post', array($this, 'deleteFromCache'));
  139. add_action('update_categories', array($this, 'makeCache'));
  140. }
  141. }
  142.  
  143. /**
  144. * This function initializes the parameters.
  145. *
  146. * It's a callback of the init hook.
  147. */
  148. function params_init() {
  149. $this->initTokens();
  150.  
  151. // If cache does not exist, we make it
  152. if(!file_exists(CACHE_DIR.'rewriteurls-cats.tmp')) {
  153. $this->makeCache('all');
  154. }
  155.  
  156. // If we're not in index, we don't do anything
  157. if (!defined('MOD_INDEX')) {
  158. return;
  159. }
  160.  
  161. $cur=$this->getToken();
  162.  
  163. if(empty($cur)) {
  164. } elseif(preg_match('/(20[0-9]{2}|category|feed|page)/', $cur)) {
  165. # Entries
  166. if(!$this->handleEntry()) {
  167. return;
  168. }
  169.  
  170. # Handle feeds
  171. $cur=$this->getToken();
  172. if($cur=='feed') {
  173. $cur=$this->nextToken();
  174. $cur=$cur===FALSE ? 'rss2' : $cur;
  175. $this->fp_params['feed']=$cur;
  176. $cur=$this->nextToken();
  177. }
  178.  
  179. # Handle page / paged
  180. $cur=$this->getToken();
  181. if($cur=='page') {
  182. $cur=$this->nextToken();
  183. if(is_numeric($cur)) {
  184. $this->fp_params['paged']=$cur;
  185. $cur=$this->nextToken();
  186. }
  187. }
  188.  
  189. # Handle possible broken medium link
  190. if(!$this->checkForMedia() && !empty($cur)) {
  191. $this->send404();
  192. }
  193. } elseif($this->countTokens()==1) {
  194. # Static pages
  195. $this->fp_params['page']=$cur;
  196. } else {
  197. # Error 404
  198. $this->send404();
  199. }
  200.  
  201. return;
  202. }
  203.  
  204. /**
  205. * This function sends a 404 Error.
  206. */
  207. function send404() {
  208. // PrettyURL's method do send 404: a fake entry!
  209. $this->fp_params['entry']='entry000000-000000';
  210.  
  211. // Get what remain of URL and send it to plugins
  212. $url=$this->getTokensUrl(true);
  213. $url=apply_filters('prettyurls_unhandled_url', $url);
  214. }
  215.  
  216. /**
  217. * This function handles the entries.
  218. *
  219. * @return boolean: Can we continue?
  220. */
  221. function handleEntry() {
  222. $cur=$this->getToken();
  223. $fp_params=&$this->fp_params;
  224.  
  225. // Handle the category
  226. if($cur=='category') {
  227. # Handle category
  228. $cur=$this->nextToken();
  229. if(REWRITEURLS_TITLES) {
  230. $cats=$this->loadCache('cats');
  231. $catid=array_search($cur, $cats);
  232. if($catid===false) {
  233. $this->send404();
  234. return false;
  235. }
  236. $fp_params['cat']=$catid;
  237. $this->nextToken();
  238. } elseif(is_numeric($cur)) {
  239. $fp_params['cat']=$cur;
  240. $this->nextToken();
  241. } else {
  242. $this->send404();
  243. return false;
  244. }
  245. return true;
  246. }
  247.  
  248. // If it's a blog post, $cur must always be numeric
  249. if(!is_numeric($cur)) {
  250. return true;
  251. }
  252.  
  253. // Year
  254. $fp_params['y']=$y=substr($cur, 2, 2);
  255.  
  256. # Month
  257. $cur=$this->nextToken();
  258. if(!is_numeric($cur) || $cur<1 || $cur>12) {
  259. return true;
  260. }
  261. $fp_params['m']=$m=$cur;
  262.  
  263. # Day
  264. $cur=$this->nextToken();
  265. if(!is_numeric($cur) || $cur<1 || $cur>31) {
  266. return true;
  267. }
  268. $fp_params['d']=$d=$cur;
  269.  
  270. # Entry Title/Id
  271. $cur=$this->nextToken();
  272. if(empty($cur)) {
  273. // It was just a day
  274. } elseif(REWRITEURLS_TITLES) {
  275. // Transform the title to the id
  276. $month_cache=&$this->loadCache($y.$m);
  277.  
  278. // Check only in the cache
  279. if(!isset($month_cache[$d][$cur])) {
  280. $this->send404();
  281. return false;
  282. }
  283.  
  284. $fp_params['entry']=$month_cache[$d][$cur];
  285. } else {
  286. // $cur is the id
  287. $fp_params['entry']=$cur;
  288. }
  289.  
  290. # Comments
  291. $cur=$this->nextToken();
  292. if($cur=='comments') {
  293. $fp_params['comments']=true;
  294. $this->nextToken();
  295. }
  296.  
  297. return true;
  298. }
  299.  
  300. /**
  301. * Check if there a medium file with a wrong URL in tokens.
  302. *
  303. * @return boolean: Is it a medium?
  304. */
  305. function checkForMedia() {
  306. $cur=$this->getToken();
  307.  
  308. // Check for symbolic images/ and attachs/
  309. if($cur=='images' || $cur=='attachs') {
  310. // Get the directory from the constants
  311. $cur=strtoupper($cur);
  312. @$cur=constant($cur.'_DIR');
  313. }
  314.  
  315. // Add the trailing slash
  316. if(substr($cur, -1, 1)!='/') {
  317. $cur.='/';
  318. }
  319.  
  320. // Check if it is *NOT* an known directory
  321. if($cur!=FP_CONTENT && $cur!=PLUGINS_DIR && $cur!=IMAGES_DIR && $cur!=ATTACHS_DIR) {
  322. return false;
  323. }
  324.  
  325. // Get the full URL
  326. $this->nextToken();
  327. $medium=$cur.$this->getTokensUrl(true);
  328. $medium=substr($medium, 0, -1);
  329.  
  330. // Check if it exists
  331. if(file_exists($medium)) {
  332. // It exists :-) Do the redirect
  333. @utils_status_header(301);
  334. @header('Location: '.BLOG_BASEURL.$medium);
  335. die;
  336.  
  337. return true;
  338. } else {
  339. // It doesn't exist but it's a medium, so send a 404
  340. $this->prevToken();
  341. $this->send404();
  342. return false;
  343. }
  344. }
  345.  
  346. # Tokens
  347. /**
  348. * This function initializes the URL tokens.
  349. */
  350. function initTokens() {
  351. $url=$this->get_url();
  352.  
  353. // Remove the query string
  354. if(($pos=plru_strrpos($url, '?'))!==false) {
  355. $url=substr($url, 0, $pos);
  356. }
  357.  
  358. // Modify the URL via plugins
  359. $url=apply_filters('rewriteurls_replace', $url);
  360.  
  361. // Get tokens and clean them
  362. $tokens=explode('/', $url);
  363. $this->tokens=&$tokens;
  364. $tokens=array_filter($tokens);
  365. $tokens=array_values($tokens);
  366.  
  367. // If there isn't any token, we can return here
  368. if(count($tokens)==0) {
  369. $this->originalurl = '/';
  370. return true;
  371. }
  372.  
  373. // Remove index.php
  374. if($tokens[0] == 'index.php') {
  375. unset($tokens[0]);
  376. $tokens = array_values($tokens);
  377. }
  378.  
  379. $this->originalurl = implode('/', $tokens);
  380.  
  381. // Go to position 0
  382. reset($tokens);
  383.  
  384. return true;
  385. }
  386.  
  387. /**
  388. * This function get the current token.
  389. *
  390. * @return mixed: The token
  391. */
  392. function getToken() {
  393. return current($this->tokens);
  394. }
  395.  
  396. /**
  397. * This function seeks the tokens.
  398. *
  399. * @param integer $offset: How much do I have to move?
  400. * @return mixed: The current value
  401. */
  402. function seekToken($offset) {
  403. if($offset==0) {
  404. return $this->getToken();
  405. }
  406.  
  407. $func=$offset < 0 ? 'prev' : 'next';
  408. $offset=abs($offset);
  409.  
  410. $last=false;
  411. for($i=1; $i<=$offset; $i++) {
  412. $last=call_user_func_array($func, array(&$this->tokens));
  413. }
  414.  
  415. return $last;
  416. }
  417.  
  418. /**
  419. * This function goes to next token.
  420. *
  421. * @return mixed: The current value
  422. */
  423. function nextToken() {
  424. return next($this->tokens);
  425. }
  426.  
  427. /**
  428. * This function goes to previous token.
  429. *
  430. * @return mixed: The current value
  431. */
  432. function prevToken() {
  433. return prev($this->tokens);
  434. }
  435.  
  436. /**
  437. * This function return the number of tokens.
  438. *
  439. * @return integer: The number of tokens
  440. */
  441. function countTokens() {
  442. return count($this->tokens);
  443. }
  444.  
  445. /**
  446. * This function gets the current URL from tokens
  447. *
  448. * @param boolean $leftovers: Just return leftovers
  449. * @return string: The URL
  450. */
  451. function getTokensUrl($leftovers=false) {
  452. // Return just leftover tokens
  453. if($leftovers) {
  454. $url=$this->getToken().'/';
  455. $i=0;
  456. while($last=$this->nextToken()) {
  457. $url.=$last.'/';
  458. $i++;
  459. }
  460. $this->seekToken(-$i);
  461. return $url;
  462. }
  463.  
  464. // Return complete token
  465. return implode('/', $this->tokens).'/';
  466. }
  467.  
  468. /**
  469. * This function has been taken from PrettyURLs.
  470. * It gets the mode, the baseurl and the current URL.
  471. */
  472. function get_url() {
  473. $baseurl=BLOG_BASEURL;
  474.  
  475. if(defined('REWRITEURLS_MODE')) {
  476. $opt=REWRITEURLS_MODE;
  477. } else {
  478. $opt=null;
  479. }
  480.  
  481. $url=substr($_SERVER['REQUEST_URI'], strlen(BLOG_ROOT)-1);
  482.  
  483. switch($opt) {
  484. case 1:
  485. $baseurl .= 'index.php/';
  486. // Don't break, because also 0 and null uses PATH_INFO
  487.  
  488. case null:
  489. case 0:
  490. if(!empty($_SERVER['PATH_INFO'])) {
  491. $url=$_SERVER['PATH_INFO'];
  492. }
  493. break;
  494.  
  495. case 2:
  496. $baseurl .= '?u=/';
  497. // Use just $_GET, not $fp_params because we want the raw one
  498. $url = @$_GET['u'];
  499. break;
  500. }
  501.  
  502. $this->baseurl=$baseurl;
  503. $this->mode=$opt;
  504. $this->originalurl=$url;
  505.  
  506. return $url;
  507. }
  508.  
  509. # Database
  510. /**
  511. * This function makes the cache of titles (for entry and categories).
  512. * By default it makes only categories cache.
  513. *
  514. * @param string $w: Type of cache (cat|entry|both)
  515. * @param boolean $force:
  516. * @return boolean: True
  517. */
  518. function makeCache($w='cat', $force=false) {
  519. // We have to do so to handle categories with the boolean true.
  520. if($w===true) {
  521. $w='cat';
  522. }
  523.  
  524. // Don't create cache if the titles mode is off.
  525. if(!$force && !REWRITEURLS_TITLES) {
  526. return true;
  527. }
  528.  
  529. // Do the categories
  530. if($w!='entries') {
  531. # $w can be cat but also both ;)
  532. $this->makeCatCache();
  533. }
  534.  
  535. if($w!='cat') {
  536. # $w can be entry but also both ;)
  537. $this->makeEntryCache();
  538. }
  539.  
  540. if(count($this->cache) && is_array($this->cache)) {
  541. // Sort by title every cache file to be faster to find an entry
  542. foreach($this->cache as $k=>$v) {
  543. asort($this->cache[$k]);
  544. }
  545. }
  546.  
  547. @$this->saveCache();
  548.  
  549. // In every case we return true!
  550. // We don't want entry save function to fail!
  551. return true;
  552.  
  553. }
  554.  
  555. /**
  556. * Make the cache of the categories.
  557. *
  558. * @return boolean: Process ok?
  559. */
  560. function makeCatCache() {
  561. $d=entry_categories_get('defs');
  562. $this->cache['cats']=array();
  563.  
  564. if(!count($d) || !is_array($d)) {
  565. return false;
  566. }
  567.  
  568. $list=array();
  569.  
  570. foreach($d as $k=>$v) {
  571. $v=urldecode(sanitize_title($v));
  572.  
  573. // Check for doubles
  574. $a=$v;
  575. $i=2;
  576. while(in_array($a, $list)) {
  577. $a=$v.'-'.$i;
  578. $i++;
  579. }
  580.  
  581. $list[$k]=$a;
  582. }
  583.  
  584. $this->cache['cats']=$list;
  585.  
  586. return @$this->saveCache()>0;
  587. }
  588.  
  589. /**
  590. * Make the cache of entries.
  591. *
  592. * @return boolean: Process ok?
  593. */
  594. function makeEntryCache() {
  595. // Query the FPDB to have all entries
  596. $o=new FPDB_Query(array('start'=>0, 'count'=>-1, 'fullparse'=>false), null);
  597.  
  598. // Inizialize the temporary list
  599. $list=array();
  600.  
  601. // Fill the temporary list with entry titles
  602. while($o->hasMore()) {
  603. list($id, $entry)=$o->getEntry();
  604. $list[$id]=urldecode(sanitize_title($entry['subject']));
  605. $this->cache[substr($id, 5, 4)]=array();
  606. }
  607.  
  608. // If the temporary list is empty, it doesn't make sense to continue
  609. if(count($list)==0) {
  610. return false;
  611. }
  612.  
  613. // Sort list by keys so we have older entries before
  614. ksort($list);
  615. foreach($list as $id=>$v) {
  616. // Cache name and day
  617. $cname=substr($id, 5, 4);
  618. $day=substr($id, 9, 2);
  619.  
  620. // Do we have already the title or do we have to make it?
  621. $file=entry_dir($id).'/rewriteurls.txt';
  622. if(file_exists($file)) {
  623. // We've already the sanitized title
  624. $title=io_load_file($file);
  625. $this->cache[$cname][$day][$title]=$id;
  626. } else {
  627. // We make the sanitized title
  628. $a=$v;
  629.  
  630. // Check for doubles
  631. $i=2;
  632. while(isset($this->cache[$cname][$day][$a])) {
  633. $a=$v."-$i";
  634. $i++;
  635. }
  636.  
  637. // Save the title
  638. $this->cache[$cname][$day][$a]=$id;
  639. io_write_file($file, $a);
  640. }
  641. }
  642.  
  643. return @$this->saveCache()>0;
  644. }
  645.  
  646. /**
  647. * This function saves the cache of the plugin.
  648. *
  649. * @return integer: The number of files saved
  650. */
  651. function saveCache() {
  652. // No file to save? Return false!
  653. if(!count($this->cache)) {
  654. return false;
  655. }
  656.  
  657. $i=0;
  658. foreach($this->cache as $file=>$list) {
  659. io_write_file(CACHE_DIR."rewriteurls-$file.tmp",
  660. serialize($list)) ? $i++ : null;
  661. }
  662.  
  663. // Return the number of saved files
  664. return $i;
  665. }
  666.  
  667. /**
  668. * This function load a file of cache.
  669. *
  670. * @param string $name: The cache name
  671. * @param boolean $force: Force loading?
  672. * @return array: The cache
  673. */
  674. function &loadCache($name, $force=false) {
  675. // Don't reload if already in $this->cache and not forced
  676. if(!empty($this->cache[$name]) && !$force) {
  677. return $this->cache[$name];
  678. }
  679.  
  680. $cache = io_load_file(CACHE_DIR . "rewriteurls-$name.tmp");
  681.  
  682. if(!empty($cache)) {
  683. $cache = @unserialize($cache);
  684. }
  685.  
  686. // Force $cache to be an array
  687. if(!is_array($cache)) {
  688. $cache = array();
  689. }
  690.  
  691. $this->cache[$name] = $cache;
  692. return $this->cache[$name];
  693. }
  694.  
  695. /**
  696. * This function returns the entry title by its id.
  697. *
  698. * @param string $id: The entry id
  699. * @param boolean $force: Force to get the title
  700. * @return string: The entry title sanitized
  701. */
  702. function getEntryTitleById($id, $force=false) {
  703. // If the title mode is off, the id is the title
  704. if(!REWRITEURLS_TITLES) {
  705. return $id;
  706. }
  707.  
  708. // If we stored in $this->titles, return the stored value
  709. if(!$force && isset($this->titles[$id])) {
  710. return $this->titles[$id];
  711. }
  712.  
  713. // It's useless to search for a title if the entry doesn't exist.
  714. if(!entry_exists($id)) {
  715. return false;
  716. }
  717.  
  718. $file=entry_dir($id).'/rewriteurls.txt';
  719.  
  720. // Do we have already the title file?
  721. if(!file_exists($file)) {
  722. // If the title file doesn't exist, trigger a fake entry publishing
  723. $o=new FPDB_Query(array('count'=>1, 'fullparse'=>false, 'id'=>$id), null);
  724. $o->hasMore();
  725. $arr=$o->getEntry();
  726. $this->addToCache($id, $arr[1], false);
  727. } else {
  728. // Otherwise we load it from the file
  729. $this->titles[$id]=trim(io_load_file($file));
  730. }
  731.  
  732. return $this->titles[$id];
  733. }
  734.  
  735. /**
  736. * This function is the callback called when an entry is added
  737. * or edited. It adds the title to the cache.
  738. *
  739. * @param string $id: The entry id
  740. * @param array $arr: The entry data
  741. * @param boolean $draft: Should I check if it's a draft?
  742. * @return boolean: Always true
  743. */
  744. function addToCache($id, $arr, $draft = true) {
  745. $dfile = draft_dir($id).'/rewriteurls.txt';
  746. $efile = entry_dir($id).'/rewriteurls.txt';
  747.  
  748. // Flatpress >= 0.1010: we need to pay attention to drafts!
  749. if(REWRITEURLS_DRAFT && $draft) {
  750. if(file_exists($efile) && !file_exists($dfile)) {
  751. rename($efile, $dfile);
  752. }
  753.  
  754. $file = $dfile;
  755. } else {
  756.  
  757. $file = $efile;
  758. }
  759.  
  760. // Load the cache for the entry month
  761. $cachename = substr($id, 5, 4);
  762. $d = substr($id, 9, 2);
  763. $cache = &$this->loadCache($cachename);
  764.  
  765. // The candidate subject
  766. $subj = urldecode(sanitize_title($arr['subject']));
  767.  
  768. if(file_exists($file)) {
  769. // Load the current title
  770. $now = io_load_file($file);
  771. // Trim because manual edits can add newlines (for example Gedit does)
  772. $now = trim($now);
  773.  
  774. // If current the title if the same, our work is finished here
  775. if($now == $subj || preg_match("/{$subj}-([0-9])*/", $now)){
  776. $this->titles[$id] = $now;
  777. return true;
  778. }
  779.  
  780. // Otherwise we remove the current title from the cache
  781. if(isset($cache[$d][$now])) {
  782. unset($cache[$d][$now]);
  783. }
  784. }
  785.  
  786. // Look for doubles
  787. $a = $subj;
  788. $i = 2;
  789. while(isset($cache[$d][$a])) {
  790. $a = $subj."-$i";
  791. $i++;
  792. }
  793.  
  794. // Save the title
  795. $a = trim($a);
  796. io_write_file($file, $a);
  797. $cache[$d][$a] = $id;
  798. @$this->saveCache();
  799. $this->titles[$id] = $a;
  800.  
  801. // Return always true, as it's an hook handler
  802. return true;
  803. }
  804.  
  805. /**
  806. * This function is the callback called when an entry is removed
  807. * or edited. It deletes the title from the cache.
  808. *
  809. * @param string $id: The entry id
  810. * @return boolean: True
  811. */
  812. function deleteFromCache($id) {
  813. // Load the cache
  814. $cachen = substr($id, 5, 4);
  815. $day = substr($id, 9, 2);
  816. $cache = &$this->loadCache($cachen);
  817.  
  818. // Remove from the cache
  819. if(isset($cache[$day]) && is_array($cache[$day])) {
  820. $key = array_search($id, $cache[$day]);
  821. if($key !== false && isset($cache[$day][$key])) {
  822. unset($cache[$day][$key]);
  823. }
  824. }
  825. @$this->saveCache();
  826.  
  827. // Remove the file from the entry directory
  828. $file = entry_dir($id).'/rewriteurls.txt';
  829. if(file_exists($file)) {
  830. @unlink($file);
  831. }
  832. @rmdir(dirname($file));
  833.  
  834. // Return always true, as it's an hook handler
  835. return true;
  836. }
  837.  
  838. # Links
  839. /**
  840. * Get the permalink of an entry
  841. *
  842. * @param string $str: The default link
  843. * @param string $id: The entry ID
  844. * @return string: The link
  845. */
  846. function permalink($str, $id) {
  847. $u=$this->baseurl;
  848. $d=date_from_id($id);
  849. $u.="20{$d['y']}/{$d['m']}/{$d['d']}/";
  850.  
  851. // Auto check if we don't use titles: it returns the id
  852. if(($title = $this->getEntryTitleById($id)) !== FALSE) {
  853. $u .= urlencode($title) . '/';
  854. } else {
  855. $u=$str;
  856. }
  857.  
  858. return $u;
  859. }
  860.  
  861. /**
  862. * Get the comment link of an entry
  863. *
  864. * @param string $str: The default link
  865. * @param string $id: The entry ID
  866. * @return string: The link
  867. */
  868. function commentlink($str, $id) {
  869. return $this->permalink($str, $id) . 'comments/';
  870. }
  871.  
  872. /**
  873. * Get the link of feeds
  874. *
  875. * @param string $str: The default link
  876. * @param string $type: The feed type
  877. * @return string: The link
  878. */
  879. function feedlink($str, $type) {
  880. return $this->baseurl."feed/$type/";
  881. }
  882.  
  883. /**
  884. * Get the link of feeds of comments of an entry
  885. *
  886. * @param string $str: The default link
  887. * @param string $type: The feed type
  888. * @param string $id: The entry ID
  889. * @return string: The link
  890. */
  891. function commentsfeedlink($str, $type, $id) {
  892. return $this->commentlink($str, $id)."feed/$type/";
  893. }
  894.  
  895. /**
  896. * Get the link of a static page
  897. *
  898. * @param string $str: The default link
  899. * @param string $id: The page ID
  900. * @return string: The link
  901. */
  902. function staticlink($str, $id) {
  903. return $this->baseurl.$id.'/';
  904. }
  905.  
  906. /**
  907. * Get the link of a category
  908. *
  909. * @param string $str: The default link
  910. * @param string $catid: The category ID
  911. * @return string: The link
  912. */
  913. function categorylink($str, $catid) {
  914. $u=$this->baseurl.'category/';
  915.  
  916. if(REWRITEURLS_TITLES) {
  917. $cats=$this->loadCache('cats');
  918.  
  919. if(isset($cats[$catid])) {
  920. $u .= urlencode($cats[$catid]) . '/';
  921. } else {
  922. $u = $str;
  923. }
  924. } else {
  925. $u .= "{$catid}/";
  926. }
  927.  
  928. return $u;
  929. }
  930.  
  931. /**
  932. * Get the link of a year
  933. *
  934. * @param string $str: The default link
  935. * @param integer $y: The year
  936. * @return string: The link
  937. */
  938. function yearlink($str, $y) {
  939. return $this->baseurl."20$y/";
  940. }
  941.  
  942. /**
  943. * Get the link of a month
  944. *
  945. * @param string $str: The default link
  946. * @param integer $y: The year
  947. * @param integer $m: The month
  948. * @return string: The link
  949. */
  950. function monthlink($str, $y, $m) {
  951. return $this->baseurl."20$y/$m/";
  952. }
  953.  
  954. /**
  955. * Get the link of a day
  956. *
  957. * @param string $str: The default link
  958. * @param integer $y: The year
  959. * @param integer $m: The month
  960. * @param integer $d: The day
  961. * @return string: The link
  962. */
  963. function daylink($str, $y, $m, $d) {
  964. return $this->baseurl."20$y/$m/$d";
  965. }
  966.  
  967. /**
  968. * Get the link of the next/prev page
  969. *
  970. * @param integer $v: The verse of the link (1=Next, -1=Prev)
  971. * @return string: The link
  972. */
  973. function nextprevlink($v) {
  974. $fp_params = &$this->fp_params;
  975. $func = $v<0 ? 'Prev' : 'Next';
  976. $func = "get{$func}Page";
  977.  
  978. // Code from PrettyURLs
  979. global $fpdb;
  980. $q=&$fpdb->getQuery();
  981. list($caption, $id)=call_user_func(array(&$q, $func));
  982. // End Of PrettyURLs Code
  983.  
  984. // When the caption is empty, there isn't any link so stop here
  985. if(empty($caption)) {
  986. return array();
  987. }
  988.  
  989. /*
  990. * Get the params of this page.
  991. * Use _GET instead of $fp_params because this one can be modified by plugins.
  992. */
  993. $getparams = $_GET;
  994. $xparams = array();
  995.  
  996. if(isset($getparams['x'])) {
  997. $xparams = utils_kexplode($getparams['x'], ':;', false);
  998. }
  999.  
  1000. if(!isset($fp_params['paged'])) {
  1001. $fp_params['paged'] = 1;
  1002. }
  1003.  
  1004. // There could be conflicts, however only with $_GET and not x.
  1005. if(REWRITEURLS_MODE == 2 && isset($getparams['u'])) {
  1006. unset($getparams['u']);
  1007. }
  1008.  
  1009. if ($q->single) {
  1010. // Single entry
  1011.  
  1012. // Get the link
  1013. if($fp_params['comments']) {
  1014. $url=$this->commentlink('', $id);
  1015. } else {
  1016. $url=$this->permalink('', $id);
  1017. }
  1018.  
  1019. # Remove entry from $fp_params
  1020. if(isset($getparams['entry'])) {
  1021. unset($getparams['entry']);
  1022. }
  1023. if(isset($xparams['entry'])) {
  1024. unset($xparams['entry']);
  1025. }
  1026.  
  1027. // Remove comments from $fp_params
  1028. if(isset($getparams['comments'])) {
  1029. unset($getparams['comments']);
  1030. }
  1031. if(isset($xparams['comments'])) {
  1032. unset($xparams['comments']);
  1033. }
  1034.  
  1035. # From PrettyURLs: add « and »
  1036. if($v>0) {
  1037. $caption.=' &raquo; ';
  1038. } else {
  1039. $caption=' &laquo; '.$caption;
  1040. }
  1041. # End of code from PrettyURLs
  1042.  
  1043. } else {
  1044. // A listing page
  1045.  
  1046. // Don't print if page < 1
  1047. if($fp_params['paged'] + $v < 1) {
  1048. return array();
  1049. }
  1050.  
  1051. // Initial URL
  1052. $url = $this->original_url == '/' ? '' : $this->originalurl;
  1053. $url .= substr($url, -1, 1) != '/' ? '/' : '';
  1054.  
  1055. # Search for page/ param
  1056. $pagepos = plru_strrpos($url, 'page/');
  1057.  
  1058. # It there isn't, we add it
  1059. if($pagepos === FALSE) {
  1060. $pagepos = strlen($url);
  1061. $url .= 'page/' . $fp_params['paged'] . '/';
  1062. }
  1063.  
  1064. // 5 is the length of "page/"
  1065. $pagepos += 5;
  1066.  
  1067. $page = $fp_params['paged'] + $v;
  1068. $add = strpos($url, '/', $pagepos);
  1069. $add = $add !== FALSE ? substr($url, $add) : '/';
  1070. $url = substr($url, 0, $pagepos) . $page . $add;
  1071.  
  1072. # Remove paged from other params
  1073. if(isset($getparams['paged'])) {
  1074. unset($getparams['paged']);
  1075. }
  1076. if(isset($xparams['paged'])) {
  1077. unset($xparams['paged']);
  1078. }
  1079.  
  1080. // Remove any double slash
  1081. $url = str_replace('//', '/', $url);
  1082. $url = substr($url, 0, 1)=='/' ? substr($url, 1) : $url;
  1083. $url = $this->baseurl . $url;
  1084. }
  1085.  
  1086. # Rebuild query string
  1087. if(count($xparams)) {
  1088. // We don't care for the moment about urlencoding this one
  1089. $getparams['x'] = plru_implode(':', ';', $xparams);
  1090. } elseif(isset($getparams['x'])) {
  1091. unset($getparams['x']);
  1092. }
  1093.  
  1094. if(!empty($getparams)) {
  1095. $getparams = plru_implode('=', '&', $getparams, 'urlencode');
  1096. $url .= '?' . $getparams;
  1097. }
  1098.  
  1099. return array($caption, $url);
  1100. }
  1101.  
  1102. }
  1103.  
  1104. // Init the class
  1105. global $plugin_rewriteurls;
  1106. $plugin_rewriteurls=new plugin_rewriteurls();
  1107.  
  1108. // Next and Prev Links
  1109. if(!defined('MOD_ADMIN_PANEL') && !function_exists('get_nextpage_link')) {
  1110. /**
  1111. * Flatpress uses the Wordpress method to obtain the nextpage link:
  1112. * they use just one function.
  1113. *
  1114. * So we declare this function but is a shortcut for the class
  1115. *
  1116. * @return array
  1117. */
  1118. function get_nextpage_link() {
  1119. global $plugin_rewriteurls;
  1120. $couple=$plugin_rewriteurls->nextprevlink(1);
  1121. return apply_filters('rewriteurls_nextpage', $couple);
  1122. }
  1123.  
  1124. /**
  1125. * Flatpress uses the Wordpress method to obtain the prevpage link:
  1126. * they use just one function.
  1127. *
  1128. * So we declare this function but is a shortcut for the class.
  1129. *
  1130. * @return array
  1131. */
  1132. function get_prevpage_link() {
  1133. global $plugin_rewriteurls;
  1134. $couple=$plugin_rewriteurls->nextprevlink(-1);
  1135. return apply_filters('rewriteurls_prevpage', $couple);
  1136. }
  1137. }
  1138.  
  1139. /**
  1140. * This function is an hack for strrpos function in PHP 4.
  1141. * In PHP 4 strrpos it's stupid, so I had to write an hack.
  1142. *
  1143. * @author: Piero VDFN <vogliadifarniente@gmail.com>
  1144. * @url: http://www.vdfn.altervista.org/2011/01/08/strrpos/comments/
  1145. * @license: Public Domain
  1146. * @param string $haystack: The string where search $needle
  1147. * @param string $needle: The string we need to search
  1148. * @param integer $offset: The offset (see PHP Manual)
  1149. * @return integer: The position of the $needle
  1150. */
  1151. function plru_strrpos($haystack, $needle, $offset=0) {
  1152. if(version_compare(PHP_VERSION, '5.0.0', '<')) {
  1153. # PHP 4 :-(
  1154. $oldpos=false;
  1155. $len=strlen($needle);
  1156.  
  1157. if($offset<0) {
  1158. $haystack=substr($haystack, 0, $offset);
  1159. $offset=0;
  1160. }
  1161.  
  1162. $off=$offset;
  1163. while(($pos=strpos($haystack, $needle, $off))!==false) {
  1164. $off=$pos+$len;
  1165. $oldpos=$pos;
  1166. }
  1167.  
  1168. return $oldpos;
  1169.  
  1170. }
  1171.  
  1172. return strrpos($haystack, $needle, $offset);
  1173. }
  1174.  
  1175. /**
  1176. * Join an array, like implode but is also uses keys.
  1177. *
  1178. * @author: Piero VDFN <vogliadifarniente@gmail.com>
  1179. * @url: http://www.pierovdfn.it/2011/06/11/php-implode-considerando-anche-le-chiavi/comments/
  1180. * @license: Public Domain
  1181. * @param string $glueval: glue for keys and values
  1182. * @param string $gluekey: glue for couples
  1183. * @param array $array: The array to join
  1184. * @param callback $filter: A filter for elements
  1185. * @return string: The joined array
  1186. */
  1187. function plru_implode($glueval, $gluekey, $array, $filter = '') {
  1188. if(count($array) == 0) {
  1189. return '';
  1190. }
  1191.  
  1192. $r = '';
  1193. foreach($array as $key => $val) {
  1194. if(is_callable($filter)) {
  1195. $key = call_user_func($filter, $key);
  1196. $val = call_user_func($filter, $val);
  1197. }
  1198.  
  1199. $r .= $key . $glueval . $val . $gluekey;
  1200. }
  1201. $r = substr($r, 0, -strlen($gluekey));
  1202.  
  1203. return $r;
  1204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement