PageRenderTime 36ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/system/helpers/hash_helper.php

https://github.com/NAzT/Dominater-Framework
PHP | 145 lines | 80 code | 16 blank | 49 comment | 27 complexity | 07e69c03ef926235d47572e8846542be MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * HerbIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package HerbIgniter
  8. * @author Herb
  9. * @copyright Copyright (c) 2009 Gudagi
  10. * @license http://gudagi.net/herbigniter/HI_user_guide/license.html
  11. * @link http://gudagi.net/herbigniter/
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * HerbIgniter Persistent Hashing Helpers
  18. *
  19. * @package HerbIgniter
  20. * @subpackage Helpers
  21. * @category Hash Codes
  22. * @author Herb
  23. * @link http://gudagi.net/herbigniter/HI_user_guide/helpers/hash_helper.html
  24. */
  25. // ------------------------------------------------------------------------
  26. /**
  27. * Write File Atomically
  28. *
  29. * Dumps a file avoiding concurrency issues on Linux, sorry Windows
  30. * (ok, I'm not sorry, Windows is!)
  31. *
  32. * NOTE: This function also appears in file_helper.php
  33. *
  34. * @access public
  35. * @param string path to file
  36. * @param string content
  37. * @return string
  38. */
  39. if ( ! function_exists('file_put_contents_atomic'))
  40. {
  41. //file_put_contents() will cause concurrency problems - that is, it doesn't write files atomically (in a single operation), which sometimes means that one php script will be able to, for example, read a file before another script is done writing that file completely.
  42. //The following function was derived from a function in Smarty (http://smarty.php.net) which uses rename() to replace the file - rename() is atomic on Linux.
  43. //On Windows, rename() is not currently atomic, but should be in the next release. Until then, this function, if used on Windows, will fall back on unlink() and rename(), which is still not atomic...
  44. define("FILE_PUT_CONTENTS_ATOMIC_TEMP", dirname(__FILE__)."/cache");
  45. define("FILE_PUT_CONTENTS_ATOMIC_MODE", 0777);
  46. function file_put_contents_atomic($filename, $content) {
  47. $temp = tempnam(FILE_PUT_CONTENTS_ATOMIC_TEMP, 'temp');
  48. if (!($f = @fopen($temp, 'wb'))) {
  49. $temp = FILE_PUT_CONTENTS_ATOMIC_TEMP
  50. . DIRECTORY_SEPARATOR
  51. . uniqid('temp');
  52. if (!($f = @fopen($temp, 'wb'))) {
  53. trigger_error("file_put_contents_atomic() : error writing temporary file '$temp'", E_USER_WARNING);
  54. return false;
  55. }
  56. }
  57. @fwrite($f, $content);
  58. @fclose($f);
  59. if (!@rename($temp, $filename)) {
  60. @unlink($filename);
  61. @rename($temp, $filename);
  62. }
  63. @chmod($filename, FILE_PUT_CONTENTS_ATOMIC_MODE);
  64. return true;
  65. }
  66. }
  67. if(!function_exists('hash_code'))
  68. {
  69. // Generates a unique 32 bit string based on previous executions
  70. // Requiries a writeable directory 'hashes' to store a flatfile of the hash codes
  71. // URL-safe hashing only letters Aa-Zz and 0-9
  72. // Optional parameters: define a set, for multiple exclusive hash sets,
  73. // define a hash length, defaulting to 254 chars
  74. // 1.55409285284366e+60 unique values
  75. function hash_code( $codeset = "1", $hashlength = 254 ) {
  76. $fn = "hashes/Hashes_" . $codeset . ".txt";
  77. if ( file_exists($fn) )
  78. $previous = file_get_contents($fn);
  79. else $previous = "";
  80. $hashcodes = explode("\n",$previous);
  81. $found = 1;
  82. while ( $found > 0 ) {
  83. // generate a new hash
  84. $newcode = "";
  85. for ( $x = 0; $x < $hashlength; $x++ ) {
  86. if ( rand(0,1) == 1 ) {
  87. $newcode = $newcode . chr(rand(48,57));
  88. } else
  89. if ( rand(0,1) == 1 ) {
  90. $newcode = $newcode . chr(rand(65,90));
  91. } else $newcode = $newcode . chr(rand(97,122));
  92. }
  93. $found = 0; // check for duplicates, each must be unique
  94. $array_length = count($hashcodes);
  95. for ( $y = 0; $y < $array_length; $y++ ) {
  96. if ( strcmp( $hashcodes[$y], $newcode ) == 0 ) $found++;
  97. }
  98. }
  99. $hashcodes[] = $newcode;
  100. file_put_contents_atomic($fn,implode("\n",$hashcodes));
  101. return $newcode;
  102. }
  103. }
  104. if(!function_exists('hash_temp'))
  105. {
  106. function hash_temp( $hashlength = 254 ) {
  107. $newcode = "";
  108. for ( $x = 0; $x < $hashlength; $x++ ) {
  109. if ( rand(0,1) == 1 ) {
  110. $newcode = $newcode . chr(rand(48,57));
  111. } else
  112. if ( rand(0,1) == 1 ) {
  113. $newcode = $newcode . chr(rand(65,90));
  114. } else $newcode = $newcode . chr(rand(97,122));
  115. }
  116. $hashcodes[] = $newcode;
  117. return $newcode;
  118. }
  119. }
  120. if(!function_exists('hash_key'))
  121. {
  122. function hash_key( $length=255 ) { return "id VARCHAR(" . $length . ")"; }
  123. }
  124. if(!function_exists('hash_ref'))
  125. {
  126. function hash_ref( $name='id', $length=255 ) { return $name . " VARCHAR(" . $length . ")"; }
  127. }
  128. /* End of file hash_helper.php */
  129. /* Location: ./system/helpers/hash_helper.php */