fixperms.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2002-2004 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 will attempt to fix ownerships and permissions on wiki
  8. page files in the wiki.d directory as pages are accessed. It does
  9. this by checking that the page being accessed is writable, and if not
  10. it renames the file to append a date, makes a copy of the file back to
  11. the original name (thus restoring file ownership), and explicitly
  12. sets file permissions on the new file according to the current umask.
  13. It also checks the permissions on the .flock file and removes it
  14. as appropriate.
  15. Finally, this script adds ?action=fixallperms, which will check
  16. and fix all pages in $WikiDir (wiki.d) with invalid permissions.
  17. You can enable this script by adding the following line at the end
  18. of config.php:
  19. include_once("scripts/fixperms.php");
  20. */
  21. if (file_exists("$WikiDir/.flock") && !is_writable("$WikiDir/.flock"))
  22. unlink("$WikiDir/.flock");
  23. $pagefile = FmtPageName("$WikiDir/$PageFileFmt",$pagename);
  24. if (file_exists($pagefile) && !is_writable($pagefile)) {
  25. Lock(2);
  26. @rename($pagefile,"$pagefile,$Now") &&
  27. @copy("$pagefile,$Now",$pagefile) &&
  28. @chmod($pagefile,0666 & ~umask());
  29. Lock(0);
  30. }
  31. if ($action=='fixallperms') {
  32. Lock(2);
  33. $dfp=opendir($WikiDir);
  34. $uid=posix_geteuid();
  35. if ($dfp) {
  36. while(($pf=readdir($dfp))!=false) {
  37. if (!preg_match("/^$GroupNamePattern\.$PageTitlePattern\$/",$pf))
  38. continue;
  39. $pagefile="$WikiDir/$pf";
  40. if (fileowner($pagefile)==$uid && is_writable($pagefile)) continue;
  41. @rename($pagefile,"$pagefile,$Now") &&
  42. @copy("$pagefile,$Now",$pagefile) &&
  43. @chmod($pagefile,0666 & ~umask());
  44. }
  45. closedir($dfp);
  46. }
  47. Lock(0);
  48. }
  49. ?>