private static function getPath($mtime, $glob) { $dir = self::$dir; $rand = rand_from_string($mtime . '') % 1000; $date = date(SQL_FORMAT_DATE, intval($mtime)); if ($glob) { return glob($dir . "/stat_{$date}_*.log", GLOB_NOSORT); } else { return $dir . "/stat_{$date}_{$rand}.log"; } }
function toStorage($file, $settings = array()) { @($naming = $settings['naming'] ? $settings['naming'] : "%s"); @($ext = $settings['ext'] ? $settings['ext'] : ""); @($delete = $settings['delete'] ? $settings['delete'] : false); @($subdir = $settings['subdir'] ? $settings['subdir'] : false); @($dir = $settings['dir'] ? $settings['dir'] : false); $add = 0; clearstatcache(); $dir = rtrim($dir, '/'); if (host($file)) { $tempDirectory = rtrim(`mktemp -d`); $content = curl($file); if (!is_string($content)) { _warn(__FUNCTION__, 'INVALID FILE (URL)!'); return null; } $path = parse_url($file, PHP_URL_PATH); $name = basename($path); if (!$name) { $name = mt_rand() . '.tmp'; } $file = "{$tempDirectory}/{$name}"; file_put_contents($file, $content); } // $dir - это каталог! $file - это файл! if (!is_file($file) or !is_dir($dir)) { _warn(__FUNCTION__, 'INVALID FILE OR DIRECTORY!'); return null; } $hash = function ($_) { return hash('sha256', $_, false); }; // $name = file_get_name($file); $ext = $ext ? $ext : file_get_ext($file); if ($ext) { $ext = ".{$ext}"; } // if (!$name) { $name = mt_rand(); } $name = str_replace(' ', '-', $name); if (is_callable($naming)) { $name = $naming($name); } elseif (is_string($naming)) { $name = sprintf($naming, $name); } $name = preg_replace('~\\W+~', ' ', $name); $name = preg_replace('~\\s+~', '-', $name); // $name = Normalizer::go($name, 'tr,latinRu,en,hyphen'); if (!$name) { $name = mt_rand(); } // $fileHash = $hash(file_get_contents($file)); if (!$subdir) { $subdir = rand_from_string($fileHash) % 1000; } if (!is_dir($_ = $dir . '/' . $subdir)) { mkdir($_, 0755); } $_ = "/{$subdir}/{$name}{$ext}"; while (true) { if (is_file($dir . $_) and $hash(file_get_contents($dir . $_)) === $fileHash) { if ($delete) { unlink($file); } if (isset($tempDirectory)) { `rm -rf '{$tempDirectory}'`; } return $_; } if (!is_file($dir . $_)) { file_put_contents($dir . $_, file_get_contents($file)); if ($delete) { unlink($file); } if (isset($tempDirectory)) { `rm -rf '{$tempDirectory}'`; } return $_; } $_ = "/{$subdir}/{$name}-{$add}{$ext}"; $add += 1; } }
function getUserAgent($re = null, $seed = null) { $LIST = __DIR__ . '/ua.list.txt'; if (!is_file($LIST)) { _log("INVALID UA LIST!", E_USER_ERROR); } if (is_array($seed)) { $seed = json_encode($seed); } if ($seed and is_string($seed . '')) { $seed = rand_from_string($seed . ''); } else { $seed = rand_from_string(microtime(true) . ''); } $list = nsplit(file_get_contents($LIST)); if ($re and is_string($re)) { $list = array_values(array_filter($list, function ($line) use($re) { if (preg_match('~^\\w+$~', $re)) { $re = "~{$re}~i"; } return @preg_match($re, $line); })); } if (!$list) { return ''; } $ua = $list[$seed % count($list)]; return $ua; }
public function testRandFromString() { $arr = array(rand_from_string("a"), rand_from_string("b"), rand_from_string("c")); $this->assertTrue(count(array_filter($arr, 'is_numeric')) === 3); $this->assertTrue(count(array_unique($arr)) === 3); }