Fabriciool

burrone

Oct 3rd, 2013
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.63 KB | None | 0 0
  1. <?php
  2.  if(!empty($_GET['page']))
  3.  {
  4.   include($_GET['page']);
  5.  }
  6.  else
  7.  {
  8.   include('home.php');
  9.  }
  10. ?>
  11.    
  12. $page = 'home.php';
  13.  
  14. $allowedPages = array('one.php', 'two.php', ...);
  15.  
  16. if (!empty($_GET['page']) && in_array($_GET['page'], $allowedPages))
  17.     $page = $_GET['page'];
  18.  
  19. include $page;
  20.    
  21. define('DEFAULT_PAGE', 'home.php');
  22. define('ALLOWED_PAGES_EXPRESSION', '^[/]+.php$|^[/]+.html$');
  23.  
  24. function ValidateRequestedPage($p)
  25. {
  26.     $errors_found = False;
  27.  
  28.         // Make sure this isn't someone trying to reference directories absolutely.
  29.     if (preg_match('^/.+$', $p))
  30.     {
  31.         $errors_found = True;
  32.     }
  33.  
  34.         // Disable access to hidden files (IE, .htaccess), and parent directory.
  35.     if (preg_match('^..+$', $p))
  36.     {
  37.         $errors_found = True;
  38.     }
  39.  
  40.  
  41.         // This shouldn't be needed for secure servers, but test for remote includes just in case...
  42.     if (preg_match('.+://.+', $p))
  43.     {
  44.         $errors_found = True;
  45.     }
  46.  
  47.     if (!preg_match(ALLOWED_PAGES_EXPRESSION, $p))
  48.     {
  49.         $errors_found = True;
  50.     }
  51.  
  52.     return !$errors_found;
  53. }
  54.  
  55. if (!isset($_GET['page'])) { $page = DEFAULT_PAGE; }
  56. else { $page = $_GET['page']; }
  57.  
  58. if ( !ValidateRequestedPage($page) )
  59. {
  60.     /* This is called when an error has occured on the page check. You probably
  61.        want to show a 404 here instead of returning False. */
  62.     return False;
  63. }
  64.  
  65. // This suggests that a valid page is being used.
  66. require_once($page);
  67.    
  68. define('DEFAULT_PAGE', 'home.php');
  69. define('ALLOWED_PAGES_EXPRESSION', '^[/]+.php$|^[/]+.html$');
  70.  
  71. function ValidateRequestedPage($p)
  72. {
  73.     $errors_found = False;
  74.  
  75.         // Make sure this isn't someone trying to reference directories absolutely.
  76.     if (preg_match('^/.+$', $p))
  77.     {
  78.         $errors_found = True;
  79.     }
  80.  
  81.         // Disable access to hidden files (IE, .htaccess), and parent directory.
  82.     if (preg_match('^..+$', $p))
  83.     {
  84.         $errors_found = True;
  85.     }
  86.  
  87.  
  88.         // This shouldn't be needed for secure servers, but test for remote includes just in case...
  89.     if (preg_match('.+://.+', $p))
  90.     {
  91.         $errors_found = True;
  92.     }
  93.  
  94.     if (!preg_match(ALLOWED_PAGES_EXPRESSION, $p))
  95.     {
  96.         $errors_found = True;
  97.     }
  98.  
  99.     return !$errors_found;
  100. }
  101.  
  102. if (!isset($_GET['page'])) { $page = DEFAULT_PAGE; }
  103. else { $page = $_GET['page']; }
  104.  
  105. if ( !ValidateRequestedPage($page) )
  106. {
  107.     /* This is called when an error has occured on the page check. You probably
  108.        want to show a 404 here instead of returning False. */
  109.     return False;
  110. }
  111.  
  112. // This suggests that a valid page is being used.
  113. require_once($page);
Advertisement
Add Comment
Please, Sign In to add comment