/libs/lib-secure.php

  1. <?php 
  2.  
  3. /** 
  4. * Generates and stores "secure" URLs for private documents. 
  5. */ 
  6.  
  7. if ( ! function_exists('gde_activate') ) { 
  8. // no access if parent plugin is disabled or when accessed directly 
  9. wp_die('<p>'.__('You do not have sufficient permissions to access this page.').'</p>'); 
  10.  
  11. /** 
  12. * Generate and store secure document link 
  13. * 
  14. * @since 2.5.0.3 
  15. * @return string Secure document link or false on failure 
  16. */ 
  17. function gde_make_secure_url( $url ) { 
  18. global $wpdb; 
  19.  
  20. $table = $wpdb->prefix . 'gde_secure'; 
  21. $data = $wpdb->get_results( "SELECT * FROM $table WHERE url = '$url'", ARRAY_A ); 
  22.  
  23. if ( ! empty( $data ) ) { 
  24. // a secure url to this doc already exists 
  25. $data = $data[0]; 
  26. return $data['murl']; 
  27. } else { 
  28. // make a new entry 
  29. $newcode = gde_secure_code(); 
  30. $url_to_mask = GDE_PLUGIN_URL . "load.php?s=" . $newcode; 
  31. $masked_url = gde_get_short_url( $url_to_mask ); 
  32.  
  33. if ( ! $wpdb->insert( 
  34. $table,  
  35. array( 
  36. 'code' => $newcode,  
  37. 'url' => $url,  
  38. 'murl' => $masked_url 
  39. ),  
  40. array( 
  41. '%s', '%s', '%s' 
  42. ) ) { 
  43. gde_dx_log("Unable to record secure URL in database"); 
  44. return false; 
  45. } else { 
  46. return $masked_url; 
  47.  
  48. /** 
  49. * Generate random document code 
  50. * 
  51. * @since 2.5.0.1 
  52. * @return string Random string used for secure doc link in database 
  53. */ 
  54. function gde_secure_code( $length = 10 ) { 
  55. $alpha = 'aeioubdghjmnpqrstvyz'; 
  56. $alpha .= strtoupper( $alpha ); 
  57. $numer = '1234567890'; 
  58.  
  59. $code = ''; 
  60. $alt = time() % 2; 
  61. for ( $i = 0; $i < $length; $i++ ) { 
  62. if ( $alt == 1 ) { 
  63. $code .= $alpha[(rand() % strlen( $alpha ))]; 
  64. $alt = 0; 
  65. } else { 
  66. $code .= $numer[(rand() % strlen( $numer ))]; 
  67. $alt = 1; 
  68. return $code; 
  69.  
  70. ?> 
.