urlapprove.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2004-2006 Patrick R. Michaud (pmichaud@pobox.com)
  3. This file is part of PmWiki; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published
  5. by the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version. See pmwiki.php for full details.
  7. This script provides a URL-approval capability. To enable this
  8. script, add the following line to a configuration file:
  9. include_once('scripts/urlapprove.php');
  10. The URL prefixes to be allowed are stored as patterns in
  11. $WhiteUrlPatterns. This array can be loaded from config.php, or
  12. from the wiki pages given by the $ApprovedUrlPagesFmt[] array.
  13. Any http: or https: URL that isn't in $WhiteUrlPatterns is rendered
  14. using $UnapprovedLinkFmt.
  15. The script also provides ?action=approveurls and ?action=approvesites,
  16. which scan the current page for any new URLs to be automatically added
  17. the first page of $UrlApprovalPagesFmt.
  18. Finally, the script will block any post containing more than
  19. $UnapprovedLinkCountMax unapproved urls in it. By default this
  20. is set to a very large number, leaving the posting of unapproved
  21. urls wide open, but by setting $UnapprovedLinkCountMax to a smaller
  22. number you can limit the number of unapproved urls that make it into
  23. a page. (Wikispammers seem to like to post long lists of urls, while
  24. more "normal" authors tend to only post a few.)
  25. */
  26. $LinkFunctions['http:'] = 'LinkHTTP';
  27. $LinkFunctions['https:'] = 'LinkHTTP';
  28. SDV($ApprovedUrlPagesFmt, array('$SiteGroup.ApprovedUrls'));
  29. SDV($UnapprovedLinkFmt,
  30. "\$LinkText<a class='apprlink' href='{\$PageUrl}?action=approvesites'>$[(approve sites)]</a>");
  31. $HTMLStylesFmt['urlapprove'] = '.apprlink { font-size:smaller; }';
  32. SDV($ApproveUrlPattern,
  33. "\\bhttps?:[^\\s$UrlExcludeChars]*[^\\s.,?!$UrlExcludeChars]");
  34. $WhiteUrlPatterns = (array)$WhiteUrlPatterns;
  35. SDV($HandleActions['approveurls'], 'HandleApprove');
  36. SDV($HandleAuth['approveurls'], 'edit');
  37. SDV($HandleActions['approvesites'], 'HandleApprove');
  38. SDV($HandleAuth['approvesites'], 'edit');
  39. SDV($UnapprovedLinkCountMax, 1000000);
  40. array_splice($EditFunctions, array_search('PostPage', $EditFunctions),
  41. 0, 'BlockUnapprovedPosts');
  42. function LinkHTTP($pagename,$imap,$path,$title,$txt,$fmt=NULL) {
  43. global $EnableUrlApprovalRequired, $IMap, $WhiteUrlPatterns, $FmtV,
  44. $UnapprovedLinkCount, $UnapprovedLinkFmt;
  45. if (!IsEnabled($EnableUrlApprovalRequired,1))
  46. return LinkIMap($pagename,$imap,$path,$title,$txt,$fmt);
  47. static $havereadpages;
  48. if (!$havereadpages) { ReadApprovedUrls($pagename); $havereadpages=true; }
  49. $p = str_replace(' ','%20',$path);
  50. $url = str_replace('$1',$p,$IMap[$imap]);
  51. foreach((array)$WhiteUrlPatterns as $pat) {
  52. if (preg_match("!^$pat(/|$)!i",$url))
  53. return LinkIMap($pagename,$imap,$path,$title,$txt,$fmt);
  54. }
  55. $FmtV['$LinkUrl'] = PUE(str_replace('$1',$path,$IMap[$imap]));
  56. $FmtV['$LinkText'] = $txt;
  57. $FmtV['$LinkAlt'] = str_replace(array('"',"'"),array('&#34;','&#39;'),$title);
  58. @$UnapprovedLinkCount++;
  59. return FmtPageName($UnapprovedLinkFmt,$pagename);
  60. }
  61. function ReadApprovedUrls($pagename) {
  62. global $ApprovedUrlPagesFmt,$ApproveUrlPattern,$WhiteUrlPatterns;
  63. foreach((array)$ApprovedUrlPagesFmt as $p) {
  64. $pn = FmtPageName($p, $pagename);
  65. StopWatch("ReadApprovedUrls $pn begin");
  66. $apage = ReadPage($pn, READPAGE_CURRENT);
  67. preg_match_all("/$ApproveUrlPattern/",@$apage['text'],$match);
  68. foreach($match[0] as $a)
  69. $WhiteUrlPatterns[] = preg_quote($a,'!');
  70. StopWatch("ReadApprovedUrls $pn end");
  71. }
  72. }
  73. function HandleApprove($pagename, $auth='edit') {
  74. global $ApproveUrlPattern,$WhiteUrlPatterns,$ApprovedUrlPagesFmt,$action;
  75. Lock(2);
  76. $page = ReadPage($pagename);
  77. $text = preg_replace('/[()]/','',$page['text']);
  78. preg_match_all("/$ApproveUrlPattern/",$text,$match);
  79. ReadApprovedUrls($pagename);
  80. $addpat = array();
  81. foreach($match[0] as $a) {
  82. if ($action=='approvesites')
  83. $a=preg_replace("!^([^:]+://[^/]+).*$!",'$1',$a);
  84. $addpat[] = $a;
  85. }
  86. if (count($addpat)>0) {
  87. $aname = FmtPageName($ApprovedUrlPagesFmt[0],$pagename);
  88. $apage = RetrieveAuthPage($aname, $auth);
  89. if (!$apage) Abort("?cannot edit $aname");
  90. $new = $apage;
  91. if (substr($new['text'],-1,1)!="\n") $new['text'].="\n";
  92. foreach($addpat as $a) {
  93. foreach((array)$WhiteUrlPatterns as $pat)
  94. if (preg_match("!^$pat(/|$)!i",$a)) continue 2;
  95. $urlp = preg_quote($a,'!');
  96. $WhiteUrlPatterns[] = $urlp;
  97. $new['text'].=" $a\n";
  98. }
  99. $_POST['post'] = 'y';
  100. PostPage($aname,$apage,$new);
  101. }
  102. Redirect($pagename);
  103. }
  104. function BlockUnapprovedPosts($pagename, &$page, &$new) {
  105. global $EnableUrlApprovalRequired, $UnapprovedLinkCount,
  106. $UnapprovedLinkCountMax, $EnablePost, $MessagesFmt, $BlockMessageFmt;
  107. if (!IsEnabled($EnableUrlApprovalRequired, 1)) return;
  108. if ($UnapprovedLinkCount <= $UnapprovedLinkCountMax) return;
  109. if ($page['=auth']['admin']) return;
  110. $EnablePost = 0;
  111. $MessagesFmt[] = $BlockMessageFmt;
  112. }