class-simple-job-board-rewrite.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. if (!defined('ABSPATH')) {
  3. exit;
  4. } // Exit if accessed directly
  5. /**
  6. * Simple_Job_Board_Rewrite Class
  7. *
  8. * This is used to define the job board rewrite rules. These rewrite rules prevent
  9. * the hotlinking of resumes & also force resumes to download rather than
  10. * opening in browser.
  11. *
  12. * @link https://wordpress.org/plugins/simple-job-board
  13. * @since 2.1.0
  14. * @since 2.2.3 Updated anti-hotlinking rules specific to uploads/jobpost.
  15. * @since 2.4.3 Removed the anti-hotlinking rules specific to uploads/jobpost.
  16. *
  17. * @package Simple_Job_Board
  18. * @subpackage Simple_Job_Board/includes
  19. * @author PressTigers <support@presstigers.com>
  20. */
  21. class Simple_Job_Board_Rewrite {
  22. /**
  23. * job_board_rewrite function.
  24. *
  25. * @since 2.1.0
  26. */
  27. public function job_board_rewrite() {
  28. if (!function_exists('get_home_path')) {
  29. require_once( ABSPATH . 'wp-admin/includes/file.php' );
  30. }
  31. // Home Path
  32. $root_path = get_home_path();
  33. $file_existing_permission = '';
  34. $uploads_dir = wp_upload_dir();
  35. // Rules for Download Files Forcefully
  36. $forcedownload_rule = "AddType application/octet-stream .pdf .txt\n";
  37. // Changing File to Writable Mode
  38. if (file_exists($root_path . '.htaccess') && !is_writable($root_path . '.htaccess')) {
  39. $file_existing_permission = substr(decoct(fileperms($root_path . '.htaccess')), -4);
  40. chmod($root_path . '.htaccess', 0777);
  41. }
  42. // Appending rules in .htaccess
  43. if (file_exists($root_path . '.htaccess') && is_writable($root_path . '.htaccess')) {
  44. $forcedownload_rule = explode("\n", $forcedownload_rule);
  45. // Anti-Hotlinking Rules Writing in .htaccess file
  46. if (!function_exists('insert_with_markers')) {
  47. require_once( ABSPATH . 'wp-admin/includes/misc.php' );
  48. }
  49. // Remove Hotlinking Rules
  50. insert_with_markers($root_path . '.htaccess', 'Hotlinking', '');
  51. /* Revert File Permission */
  52. if (!empty($file_existing_permission)) {
  53. chmod( $root_path . '.htaccess', $file_existing_permission );
  54. }
  55. }
  56. $file = array(
  57. 'basedir' => $uploads_dir['basedir'] . '/jobpost',
  58. 'file' => '.htaccess',
  59. 'rules' => 'deny from all',
  60. );
  61. // Protect resume files from hotlinking
  62. if (wp_mkdir_p($file['basedir']) && !file_exists(trailingslashit($file['basedir']) . $file['file'])) {
  63. if ($file_handle = @fopen(trailingslashit($file['basedir']) . $file['file'], 'w')) {
  64. fwrite($file_handle, $file['rules']);
  65. fclose($file_handle);
  66. }
  67. }
  68. }
  69. }
  70. new Simple_Job_Board_Rewrite();