示例#1
0
 function hash_code($codeset = "1", $hashlength = 254)
 {
     $fn = "hashes/Hashes_" . $codeset . ".txt";
     if (file_exists($fn)) {
         $previous = file_get_contents($fn);
     } else {
         $previous = "";
     }
     $hashcodes = explode("\n", $previous);
     $found = 1;
     while ($found > 0) {
         // generate a new hash
         $newcode = "";
         for ($x = 0; $x < $hashlength; $x++) {
             if (rand(0, 1) == 1) {
                 $newcode = $newcode . chr(rand(48, 57));
             } else {
                 if (rand(0, 1) == 1) {
                     $newcode = $newcode . chr(rand(65, 90));
                 } else {
                     $newcode = $newcode . chr(rand(97, 122));
                 }
             }
         }
         $found = 0;
         // check for duplicates, each must be unique
         $array_length = count($hashcodes);
         for ($y = 0; $y < $array_length; $y++) {
             if (strcmp($hashcodes[$y], $newcode) == 0) {
                 $found++;
             }
         }
     }
     $hashcodes[] = $newcode;
     file_put_contents_atomic($fn, implode("\n", $hashcodes));
     return $newcode;
 }
示例#2
0
function saveStruct($filepath, $struct, $sortKeys = false, $headerMessage = '')
{
    // $headerMessage added in 2.18
    if (!$filepath) {
        die(__FUNCTION__ . ": no filepath specified!");
    }
    if (!is_array($struct)) {
        die(__FUNCTION__ . ": structure isn't array!");
    }
    // to debug add: . print_r($struct, true)
    // sort keys
    if ($sortKeys) {
        ksort($struct);
    }
    // create struct file data
    $header = '<?php /* This is a PHP data file */ if (!@$LOADSTRUCT) { die("This is not a program file."); }';
    // DO NOT EDIT THIS - isStructFile() uses it to identify struct files
    if ($headerMessage != '') {
        $header .= "\n/*\n{$headerMessage}\n*/";
    }
    $phpData = var_export($struct, true);
    $phpData = preg_replace("/=>\\s*array\\s*/s", "=> array", $phpData);
    // reformat PHP so arrays start on the same line as keys (easier to read)
    $content = $header . "\nreturn {$phpData};\n?>";
    // save file
    file_put_contents_atomic($filepath, $content);
    // opcache - invalidate old versions so opcache.file_update_protection won't prevent updated PHP file from being loaded
    if (function_exists('opcache_invalidate')) {
        opcache_invalidate($filepath, true);
    }
}