Beispiel #1
0
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;
    }
}
Beispiel #2
0
Datei: core.php Projekt: ejz/core
function toStorage($file, $settings = array())
{
    $dir = isset($settings['dir']) ? $settings['dir'] : false;
    clearstatcache();
    $delete = isset($settings['delete']) ? $settings['delete'] : false;
    // $dir - это каталог! $file - это файл!
    if (!is_file($file) or !is_dir($dir)) {
        _log('INVALID FILE OR DIRECTORY!', E_USER_WARNING);
        return null;
    }
    $dir = rtrim($dir, '/');
    $defaultName = file_get_name($file);
    $defaultExt = file_get_ext($file);
    $name = isset($settings['name']) ? $settings['name'] : $defaultName;
    $ext = isset($settings['ext']) ? $settings['ext'] : $defaultExt;
    //
    $name = preg_replace('~[^a-zA-Z0-9\\.-]~', ' ', $name);
    $name = preg_replace('~\\s+~', '-', $name);
    if (!$name) {
        $name = mt_rand();
    }
    $target = $dir . '/' . $name . ($ext ? ".{$ext}" : "");
    $i = 0;
    $initialTarget = $target;
    while (file_exists($target)) {
        $target = preg_replace($i ? '~-\\d+(\\.\\w+)$~' : '~(\\.\\w+)$~', '-' . ++$i . '$1', $target);
    }
    if ($delete) {
        rename($file, $target);
    } else {
        copy($file, $target);
    }
    if ($initialTarget != $target and md5_file($initialTarget) === md5_file($target)) {
        unlink($target);
        $target = $initialTarget;
    }
    return $target;
}
Beispiel #3
0
function directory_contents($Dir_Path, $ext = NULL)
{
    $dir_data = scandir($Dir_Path);
    $new_dir_data = array();
    foreach ($dir_data as $k => $object) {
        if ($object != "." && $object != "..") {
            $f_type = filetype($Dir_Path . "/" . $object);
            if (isset($ext)) {
                if ($ext == $f_type) {
                    $new_dir_data[] = $object;
                } elseif (file_get_ext($object) == $ext) {
                    $new_dir_data[] = $object;
                }
            } else {
                $new_dir_data[$f_type][] = $object;
            }
        }
    }
    return $new_dir_data;
}
Beispiel #4
0
 public function testToStorage()
 {
     $tmp = rtrim(`mktemp --suffix=.png`);
     $dir = rtrim(`mktemp -d`);
     file_put_contents($tmp, $rand = mt_rand());
     $target = toStorage($tmp, array('dir' => $dir));
     $this->assertTrue(file_get_name($tmp) === file_get_name($target));
     $this->assertTrue(file_get_ext($tmp) === file_get_ext($target));
     $this->assertEquals($rand, file_get_contents($target));
     $this->assertTrue(is_file($tmp));
     //
     file_put_contents($tmp, mt_rand());
     $target_1 = toStorage($tmp, array('dir' => $dir));
     $this->assertTrue(file_get_name($target) . "-1" === file_get_name($target_1));
     //
     file_put_contents($tmp, $rand);
     $target_2 = toStorage($tmp, array('dir' => $dir));
     $this->assertTrue($target_2 === $target);
     //
     file_put_contents($tmp, $rand = mt_rand());
     $target = toStorage($tmp, array('dir' => $dir, 'ext' => 'bla', 'delete' => true));
     $this->assertTrue(file_get_name($tmp) === file_get_name($target));
     $this->assertTrue('bla' === file_get_ext($target));
     $this->assertEquals($rand, file_get_contents($target));
     $this->assertTrue(!is_file($tmp));
 }
Beispiel #5
0
Datei: Stat.php Projekt: ejz/stat
 public static function analyze($stamp, $delete = false)
 {
     // STAMP TO UNIXTIME
     if (is_numeric($stamp) and intval($stamp) <= 0) {
         $stamp = strtotime("today " . intval($stamp) . " day");
     } elseif (is_numeric($stamp) and intval($stamp) > 0) {
         $stamp = $stamp;
     } else {
         $stamp = strtotime($stamp);
     }
     //
     $db = self::$db;
     $dir = self::$dir;
     $paths = self::getPath($stamp, $glob = true);
     $hosts = array();
     foreach ($paths as $path) {
         foreach (nsplit(file_get_contents($path)) as $hit) {
             $hit = unserialize($hit);
             if (!$hit) {
                 continue;
             }
             $hit['URL'] = urldecode($hit['URL']);
             $hit['REF'] = urldecode($hit['REF']);
             //
             $host = $hit['HOST'];
             $host = strtolower($host);
             $host = preg_replace("~^www\\.~", "", $host);
             if (!isset($hosts[$host])) {
                 $hosts[$host] = array('hits' => 0, 'ips' => array(), 'google' => array(), 'yandex' => array(), 'serp' => array(), 'top_all' => array(), 'top_serp' => array(), 'top_ref' => array(), 'kws' => array());
             }
             $is_valid_ext = !in_array(file_get_ext($hit['URL']), array('jpg', 'jpeg', 'png', 'gif', 'bmp', 'css', 'js', 'ico'));
             // HITS
             $hosts[$host]['hits'] += 1;
             // IPS
             $hosts[$host]['ips'][$hit['IP']] = true;
             // GOOGLE
             if ($is_google = is_numeric(strpos(host($hit['REF']), 'google'))) {
                 $hosts[$host]['google'][$hit['IP']] = true;
             }
             // YANDEX
             if ($is_yandex = is_numeric(strpos(host($hit['REF']), 'yandex'))) {
                 $hosts[$host]['yandex'][$hit['IP']] = true;
             }
             // TOP_ALL
             if ($is_valid_ext) {
                 if (!isset($hosts[$host]['top_all'][$hit['URL']])) {
                     $hosts[$host]['top_all'][$hit['URL']] = array();
                 }
                 $hosts[$host]['top_all'][$hit['URL']][$hit['IP']] = true;
             }
             // TOP_SERP
             if (($is_google or $is_yandex) and $is_valid_ext) {
                 if (!isset($hosts[$host]['top_serp'][$hit['URL']])) {
                     $hosts[$host]['top_serp'][$hit['URL']] = array();
                 }
                 $hosts[$host]['top_serp'][$hit['URL']][$hit['IP']] = true;
             }
             // TOP_REF
             $is_same = (host($hit['REF']) === $host or host($hit['REF']) === "www.{$host}");
             if (!$is_google and !$is_yandex and host($hit['REF']) and $is_valid_ext and !$is_same) {
                 $_ = "{$hit['URL']}, {$hit['REF']}";
                 if (!isset($hosts[$host]['top_ref'][$_])) {
                     $hosts[$host]['top_ref'][$_] = array();
                 }
                 $hosts[$host]['top_ref'][$_][$hit['IP']] = true;
             }
             // KWS
             if ($is_google or $is_yandex) {
                 $query = parse_url($hit['REF'], PHP_URL_QUERY);
                 do {
                     if ($query) {
                         $kw = null;
                         parse_str($query, $_);
                         $query = $_;
                         if (is_null($kw)) {
                             $kw = ($is_google and isset($query['q']) and $query['q']) ? $query['q'] : null;
                         }
                         if (is_null($kw)) {
                             $kw = ($is_yandex and isset($query['text']) and $query['text']) ? $query['text'] : null;
                         }
                         $kw = normLatin($kw);
                         $kw = normRu($kw);
                         if (!$kw) {
                             break;
                         }
                         $kw = "{$kw}, {$hit['URL']}";
                         if (!isset($hosts[$host]['kws'][$kw])) {
                             $hosts[$host]['kws'][$kw] = array();
                         }
                         $hosts[$host]['kws'][$kw][$hit['IP']] = true;
                     }
                 } while (false);
             }
         }
     }
     foreach ($hosts as $host => $value) {
         extract($value);
         // TOP ALL
         array_walk($top_all, function (&$value) {
             $value = count($value);
         });
         arsort($top_all);
         array_walk($top_all, function (&$value, $key) {
             $value = "{$value}, {$key}";
         });
         $top_all = array_values($top_all);
         $top_all = array_slice($top_all, 0, 100);
         $top_all = implode("\n", $top_all);
         // ---
         // TOP SERP
         array_walk($top_serp, function (&$value) {
             $value = count($value);
         });
         arsort($top_serp);
         array_walk($top_serp, function (&$value, $key) {
             $value = "{$value}, {$key}";
         });
         $top_serp = array_values($top_serp);
         $top_serp = array_slice($top_serp, 0, 100);
         $top_serp = implode("\n", $top_serp);
         // ---
         // TOP REF
         array_walk($top_ref, function (&$value) {
             $value = count($value);
         });
         arsort($top_ref);
         array_walk($top_ref, function (&$value, $key) {
             $value = "{$value}, {$key}";
         });
         $top_ref = array_values($top_ref);
         $top_ref = array_slice($top_ref, 0, 1000);
         $top_ref = implode("\n", $top_ref);
         // ---
         // KWS
         array_walk($kws, function (&$value) {
             $value = count($value);
         });
         arsort($kws);
         array_walk($kws, function (&$value, $key) {
             $value = "{$value}, {$key}";
         });
         $kws = array_values($kws);
         $kws = array_slice($kws, 0, 1000);
         $kws = implode("\n", $kws);
         // ---
         $bean = $db->bean('stat');
         $bean->import(array('host' => $host, 'stamp' => date(SQL_FORMAT_DATE, $stamp), 'hits' => $hits, 'ips' => count($ips), 'google' => count($google), 'yandex' => count($yandex), 'serp' => count($google) + count($yandex), 'top_all' => $top_all, 'top_serp' => $top_serp, 'top_ref' => $top_ref, 'kws' => $kws))->replace();
     }
     if ($delete) {
         foreach ($paths as $path) {
             unlink($path);
         }
     }
 }