add sync code

This commit is contained in:
Kieran 2018-11-21 22:01:40 +08:00
parent 9102b068ac
commit 02fb1ef4e8
6 changed files with 105 additions and 11 deletions

View File

@ -13,7 +13,7 @@
(function () {
var u = "//matomo.trash.lol/";
_paq.push(['setTrackerUrl', u + 'piwik.php']);
_paq.push(['setSiteId', '1']);
_paq.push(['setSiteId', '3']);
var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0];
g.type = 'text/javascript'; g.async = true; g.defer = true; g.src = u + 'piwik.js'; s.parentNode.insertBefore(g, s);
})();
@ -87,6 +87,9 @@
</div>
<ins class="adsbygoogle" style="display:block; margin-left: auto; margin-right: auto;" data-ad-client="ca-pub-3289062345896209"
data-ad-slot="9187315106" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<div id="page-upload">
<div class="page-left" style="width: 100%">
<div id="dropzone">Click me!</div>

View File

@ -1,8 +1,12 @@
<?php
define('REDIS_CONFIG', '127.0.0.1');
define('REDIS_CONFIG', 'redis-host');
define('REDIS_PREFIX', 'vc:');
define('USER_IP', isset($_SERVER['HTTP_CF_CONNECTING_IP']) ? $_SERVER['HTTP_CF_CONNECTING_IP'] : $_SERVER['REMOTE_ADDR']);
if(!isset($_COOKIE["VC:UID"])) {
setcookie("VC:UID", uniqid());
}
spl_autoload_register(function ($class_name) {
include dirname(__FILE__) . '/' . strtolower($class_name) . '.php';
});

View File

@ -1,11 +1,48 @@
<?php
class Sync implements RequestHandler {
public function __construct(){
Config::LoadConfig(array("upload_folder"));
ini_set('enable_post_data_reading', 0);
}
public function HandleRequest() : void {
if(isset($_SERVER["HTTP_X_FILE_ID"])) {
$id = $_SERVER["HTTP_X_FILE_ID"];
$fs = new FileStore(Config::$Instance->upload_folder);
if(!$fs->FileExists($id)) {
//resolve the hostnames to ips
$redis = StaticRedis::$Instance;
$sync_hosts = $redis->sMembers(REDIS_PREFIX . 'sync-hosts');
$sync_hosts_ips = array();
foreach($sync_hosts as $host) {
$sync_hosts_ips[] = gethostbyname($host);
}
//check the ip of the host submitting the file for sync
if(in_array(USER_IP, $sync_hosts_ips)) {
$fs->StoreFile("php://input", $id);
} else {
http_response_code(401);
}
}
} else {
http_response_code(400);
}
}
public static function SyncFile($id, $filename, $host) : void {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://$host/sync");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($filename));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/octet-stream",
"X-File-Id: " . $id
));
curl_exec($ch);
curl_close ($ch);
}
}

18
src/php/syncthread.php Normal file
View File

@ -0,0 +1,18 @@
<?php
class SyncThread extends Thread {
private $Destination;
private $FilePath;
private $Id;
public function __constrct($id, $filepath, $host){
$this->Id = $id;
$this->FilePath = $filepath;
$this->Destination = $host;
}
public function run() {
Sync::SyncFile($this->Id, $this->FilePath, $this->Destination);
}
}
?>

View File

@ -1,14 +1,6 @@
<?php
class TrackingEvent {
}
class Tracking {
public static function CreateEventFromDownload(){
return new TrackingEvent();
}
public function TrackDownload($id) {
public function TrackDownload($id) : void {
$redis = StaticRedis::$Instance;
$file_key = REDIS_PREFIX . $id;
@ -16,6 +8,8 @@
$redis->hIncrBy($file_key, 'views', 1);
$redis->hSet($file_key, 'lastview', time());
}
$this->SendMatomoEvent();
}
function IsRangeRequest() : bool {
@ -29,5 +23,20 @@
return False;
}
function SendMatomoEvent() : void {
$msg = "?" . http_build_query(array(
"idsite" => 1,
"rec" => 1,
"apiv" => 1,
"_id" => isset($_COOKIE["VC:UID"]) ? $_COOKIE["VC:UID"] : uniqid(),
"url" => (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]",
"cip" => _UIP,
"ua" => isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : "",
"urlref" => isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : ""
));
StaticRedis::$Instance->publish('ga-page-view-matomo', $msg);
}
}
?>

View File

@ -53,7 +53,30 @@
}
function SyncFileUpload($id) : void {
$redis = StaticRedis::$Instance;
$sync_hosts = $redis->sMembers(REDIS_PREFIX . 'sync-hosts');
if($sync_hosts !== False) {
$fs = new FileStore(Config::$Instance->upload_folder);
foreach($sync_hosts as $host) {
Sync::SyncFile($id, $fs->GetAbsoluteFilePath($id), $host);
}
/*
$sync_threads = array();
foreach($sync_hosts as $host) {
$new_thread = new SyncThread($id, $fs->GetAbsoluteFilePath($id), $host);
$new_thread->start();
$sync_threads[] = $new_thread;
}
foreach($sync_threads as $thread) {
while($thread->isRunning()) {
usleep(100);
}
$thread->join();
}*/
}
}
function SaveUpload($bf) : string {