<?php
//$root=$_SERVER['DOCUMENT_ROOT'];
$root=".";
include($root."/php/setup.php");
include($root."/php/file.php");
//session_start();
// currently replaced all instances of remote_addr with http_x_real_ip
// in document for using behind coolify permanently anyway ~
//$user_ip = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'];
$status="failure";
$tnow=time();
//header('Access-Control-Allow-Origin: https://filebrowser.anatwi.xyz, https://secureline. analiestar.com');
//header("Access-Control-Allow-Headers: Content-Type")
////header("Access-Control-Allow-Credentials", "true");
//header("Access-Control-Allow-Methods", "GET,OPTIONS,POST");
//header("Access-Control-Allow-Headers", "Access-Control-Allow-Headers, Origin,Accept, X- Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers");
//function getMonthTimestamps($month, $year) {
// // Create a DateTime object for the first day of the specified month
// // The format 'Y-m-d' is used to create a reliable starting point (e.g., '2023-01-01')
// $startDate = new DateTimeImmutable("{$year}-{$month}-01");
//
// // Get the first day of the month (timestamp)
// $earliestTimestamp = $startDate->getTimestamp();
//
// // Get the last day of the month
// $endDate = $startDate->modify('last day of this month');
// $lastTimestamp = $endDate->getTimestamp();
//
// return (object) [
// 'earliest' => $earliestTimestamp,
// 'latest' => $lastTimestamp
// ];
//}
$_POST = json_decode(file_get_contents('php://input'), true);
if(isset($_POST['setting'])) {
$setting=stripslashes(htmlspecialchars($_POST['setting']));
$value=stripslashes(htmlspecialchars($_POST['value']));
$key=stripslashes(htmlspecialchars($_POST['key']));
//$status.=":".$setting;
switch($setting) {
case "scannote":
// on month change
$month=$value;
$year=$key;
//$tsobj=getMonthTimestamps($month,$year);
//$tsobj['earliest'];
//$tsobj['latest'];
//
$testfile=$year."-".$month;
$testlen=strlen($testfile);
$dblist=explode("\n",fileread($dbfile));
$exists=array($tnow); // index0 = synctime
for($i=0;$i<count($dblist);$i++) {
if(IsJsonString($dblist[$i])) {
$dbobj=json_decode($dblist[$i],true);
if(substr($dbobj['file'],0,$testlen)==$testfile) {
array_push($exists,$dbobj['file']);
}
}
}
$status=json_encode($exists);
break;
case "loadnote":
$name=$value;
$dblist=explode("\n",fileread($dbfile));
$exists=false;
for($i=0;$i<count($dblist);$i++) {
if(IsJsonString($dblist[$i])) {
$dbobj=json_decode($dblist[$i],true);
if($dbobj['file']==$name) {
$exists=true;
break;
}
}
}
$data="";
if($exists) {
$notefile=$root."/"."_userdata/notes/".$name.".log";
$data=fileread($notefile);
} else {
$data=""; // new
}
$status=base64_encode($data);
break;
case "savenote":
$note=base64_decode($value);
//$name=$key;
$obj=array();
$keydec=base64_decode($key);
if(IsJsonString($keydec)) {
$obj=json_decode($keydec,true);
$name=$obj['name'];
$synctime=$obj['synctime'];
$merge=false;
$delete=true;
if(strlen($note>0)) {
$delete=false; // deletes only entry, not actual file
} // also prevents overwrite with 0 data
$notefile=$root."/"."_userdata/notes/".$name.".log";
if(file_exists($notefile)) {
$modtime=filemtime($notefile);
if($modtime>$synctime && $synctime!=-1) {
// merge instead of overwrite?
$merge=true;
}
}
if(!$delete) {
if($merge) {
fileappend($notefile,"\n".$note);
} else {
filewrite($notefile,$note); // overwrites
}
$size=filesize($notefile);
$mtim=filemtime($notefile);
} else { // optional real file delete on write 0
//unlink($notefile);
}
$dblist=explode("\n",fileread($dbfile));
// { created: ts, modified: ts, file: name, size: byte, user: _ } // user not in use yet
$edited=false;
$exists=false;
$dbdata="";
for($i=0;$i<count($dblist);$i++) {
if(IsJsonString($dblist[$i])) {
$dbobj=json_decode($dblist[$i],true);
if($dbobj['file']==$name) {
$exists=true;
// make edits here
$dbobj['modified']=$mtim;
$dbobj['size']=$size;
if(!$delete) {
$dbdata.=json_encode($dbobj,JSON_FORCE_OBJECT)."\n";
} // else skip add back = deleted
$edited=true;
} else {
// add back others
$dbdata.=$dblist[$i]."\n";
}
}
}
if(!$exists && !$delete) {
// add new here
$dbobj=array(
"created" => $tnow,
"modified" => $mtim,
"file" => $name,
"size" => $size,
"user" => "_",
);
$dbdata.=json_encode($dbobj,JSON_FORCE_OBJECT)."\n";
$edited=true;
}
if($edited) {
filewrite($dbfile,$dbdata);
}
if($merge) {
$status="mergednote:".$tnow;
} else {
$status="savednote:".$tnow;
}
} else {
$status="errornote:".$tnow;
}
break;
case "newwall": // loadwp
// videos from list with remote files
//$videos=array();
//$videofile=$root."/_userdata/twily-s-stuff-PMV.log";
//if(file_exists($videofile)) {
// if(filesize($videofile)>3) {
// $videos=explode("\n",fileread($videofile));
// }
//}
//----videos disabled uncomment here, below (vrnd1) and main.js vid1 vidCal
// images from list with local files
//$images=array();
//$imagesfile=$root."/_userdata/twily-s-stuff-pony-clop.log";
//if(file_exists($imagesfile)) {
// if(filesize($imagesfile)>3) {
// $images=explode("\n",fileread($imagesfile));
// }
//} // supply path in main.js cal_photo
// images located in _userdata/wallpapers (no list)
// Use glob() to find all image files with common extensions
// GLOB_BRACE allows multiple patterns
$images=array();
$imageFiles = glob($imageDir . '*.{jpg,jpeg,png,gif,webp}', GLOB_BRACE);
if (!empty($imageFiles)) {
foreach ($imageFiles as $filename) {
$imageData = @getimagesize($filename);
if ($imageData !== false) {
$width = $imageData[0];
$height = $imageData[1];
$baseName = basename($filename); // Get just the file name without the path
//array_push($images,array("name" => $baseName, "width" => $width, "height" => $height));
array_push($images,$baseName);
}
}
}
//----
for($i=0;$i<count($images)-1;$i++) { // make sure always is a file on a line
if($images[$i]=="") {
array_splice($images,$i,1);
$i--;
}
}
$lastRnd1=-1;
$lastRnd2=-1;
//$lastVRnd1=-1;
$locked=false;
if(file_exists($wplockfile)) {
$lastwall=fileread($wplockfile);
$locked=true; // has been locked
if(IsJsonString($lastwall)) {
$obj=json_decode($lastwall,true);
$lastRnd1=$obj['rnd1'];
$lastRnd2=$obj['rnd2'];
//$lastVRnd1=$obj['vrnd1'];
if($tnow>($obj['ts']+$obj['lock'])) {
$locked=false; // new day
}
}
}
if(!$locked) {
$tries=10;
$rnd1=random_int(0, count($images)-1); // rndMinMax
if(count($images)>1) {
$tries=10;
while(($rnd1==$lastRnd1 || $rnd1==$lastRnd2) && $tries>0) {
$rnd1=random_int(0, count($images)-1);
$tries--;
}
}
$rnd2=random_int(0, count($images)-1);
if(count($images)>1) {
$tries=10;
while(($rnd2==$rnd1 || $rnd2==$lastRnd2 || $rnd2==$lastRnd1) && $tries>0) {
$rnd2=random_int(0, count($images)-1);
$tries--;
}
}
//$vrnd1=random_int(0, count($videos)-1);
//if($count($videos)>1) {
// $tries=10;
// while($vrnd1==$lastVRnd1 && $tries>0) {
// $vrnd1=random_int(0, count($videos)-1);
// $tries--;
// }
//}
// -- random end
$tomorrow_midnight = new DateTime('tomorrow');
$tomorrow = new DateTime('tomorrow'); // Represents midnight at the beginning of the next day
$seconds_remaining = $tomorrow->getTimestamp() - $tnow;
$lockobj=array(
"img1" => $images[$rnd1],
"img2" => $images[$rnd2],
"rnd1" => $rnd1,
"rnd2" => $rnd2,
"ts" => $tnow,
"lock" => $seconds_remaining, // lock is second until next day
//"vid1" => $videos[$vrnd1],
//"vrnd1" => $vrnd,
);
filewrite($wplockfile,json_encode($lockobj,JSON_FORCE_OBJECT));
$statobj=array(
"img1" => $images[$rnd1],
"img2" => $images[$rnd2],
//"vid1" => $videos[$vrnd1],
);
} else {
$statobj=array(
"img1" => $images[$lastRnd1],
"img2" => $images[$lastRnd2],
//"vid1" => $videos[$lastVRnd1],
);
}
$status=json_encode($statobj,JSON_FORCE_OBJECT);
break;
case "savesetting":
$settingdata="";
if(file_exists($settingfile)) {
if(filesize($settingfile)>2) {
$settingdata=fileread($settingfile);
}
}
$settings=array();
if(IsJsonString($settingdata)) {
$settings=json_decode($settingdata,true);
}
$edited=false;
switch($value) {
case "lightmode";
$settings['lightmode']=($key=="1")?1:0;
$edited=true;
break;
case "imagemode";
$settings['imagemode']=($key=="1")?1:0;
$edited=true;
break;
case "wrapon";
$settings['wrapon']=($key=="1")?1:0;
$edited=true;
break;
default:
}
if($edited) {
filewrite($settingfile,json_encode($settings,JSON_FORCE_OBJECT));
$status="success";
}
break;
case "loadsettings":
$settingdata="";
if(file_exists($settingfile)) {
if(filesize($settingfile)>2) {
$settingdata=fileread($settingfile);
}
}
$status=$settingdata;
break;
case "synctime":
// could also check file changes and load new?
// also check wallpaper time change~?
$locked=false;
$wplocktime=-1;
if(file_exists($wplockfile)) {
$lastwall=fileread($wplockfile);
$locked=true; // has been locked
if(IsJsonString($lastwall)) {
$obj=json_decode($lastwall,true);
$wplocktime=$obj['ts']+$obj['lock'];
if($tnow>$wplocktime) {
$locked=false;
}
}
} // same check as newwall
// if new wall, ping back to ping to newwall
$wplockmodified=-1;
$dblogmodified=-1;
if(file_exists($wplockfile)) {
$wplockmodified=filemtime($wplockfile);
} else {
$wplockmodified=-1;
}
if(file_exists($dbfile)) {
$dblogmodified=filemtime($dbfile);
} else {
$dplogmodified=-1;
}
$ret=array(
"ts" => $tnow,
"wplocked" => ($locked)?1:0,
"wplocktime" => $wplocktime,
"wplockmod" => $wplockmodified,
"dblogmod" => $dblogmodified
);
//$status=$tnow;
$status=json_encode($ret,JSON_FORCE_OBJECT);
break;
default:
}
}
//headers['Connection'] = 'close';
echo $status;
//session_write_close();
?>
Top