AdminHandler.php 27.0 KB
Newer Older
H
Init  
HFO4 已提交
1 2 3 4 5 6 7
<?php
namespace app\index\model;

use think\Model;
use think\Db;
use \app\index\model\Mail;
use \app\index\model\FileManage;
H
HFO4 已提交
8
use \Krizalys\Onedrive\Client;
H
Init  
HFO4 已提交
9 10 11 12 13 14 15 16 17

class AdminHandler extends Model{

	public $siteOptions;
	public $pageData;
	public $listData;
	public $pageNow;
	public $pageTotal;
	public $dataTotal;
H
HFO4 已提交
18
	public $dbVerInfo;
H
Init  
HFO4 已提交
19 20 21 22 23
	
	public function __construct($options){
		$this->siteOptions = $options;
	}

H
HFO4 已提交
24 25 26 27 28 29 30 31 32 33
	public function checkDbVersion(){
		$versionInfo = json_decode(@file_get_contents(ROOT_PATH. "application/version.json"),true);
		$dbVerNow = Option::getValue("database_version");
		if(!isset($versionInfo["db_version"]) || $dbVerNow < (int)$versionInfo["db_version"]){
			$this->dbVerInfo = array('now' => $dbVerNow, 'require' => $versionInfo["db_version"]);
			return true;
		}
		return false;
	}

H
HFO4 已提交
34 35 36 37 38 39 40 41
	public function checkCron(){
		$cronRecord = Db::name("corn")->where("name","delete_unseful_chunks")->find();
		if($cronRecord["last_excute"]+$cronRecord["interval_s"]+"1800"<time()){
			return false;
		}
		return true;
	}

H
Init  
HFO4 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	public function getStatics(){
		$statics["fileNum"] = Db::name('files')->count();
		$statics["privateShareNum"] = Db::name('shares')->where("type","private")->count();
		$statics["publicShareNum"] = Db::name('shares')->where("type","public")->count();
		$statics["userNum"] = Db::name('users')->where("user_status",0)->count();
		if($statics["fileNum"]==0){
			$statics["imgRate"] = 0;
			$statics["audioRate"] = 0;
			$statics["videoRate"] = 0;
			$statics['otherRate'] = 0;
		}else{
			$statics["imgRate"] =floor(Db::name('files')
			->where('pic_info',"<>"," ")
			->where('pic_info',"<>","0,0")
			->where('pic_info',"<>","null,null")
			->count()/$statics["fileNum"]*10000)/100;
			$statics["audioRate"] =floor(Db::name('files')
			->where(function ($query) {
			    $query->where('orign_name', "like","%mp3")
			    ->whereor('orign_name', "like","%flac")
			    ->whereor('orign_name', "like","%wma")
			    ->whereor('orign_name', "like","%aac")
			    ->whereor('orign_name', "like","%wav")
			    ->whereor('orign_name', "like","%ogg");
			})
			->count()/$statics["fileNum"]*10000)/100;
			$statics["videoRate"] =floor(Db::name('files')
			->where(function ($query) {
			    $query->where('orign_name', "like","%mp4")
			    ->whereor('orign_name', "like","%avi")
			    ->whereor('orign_name', "like","%rmvb")
			    ->whereor('orign_name', "like","%aac")
			    ->whereor('orign_name', "like","%wav")
			    ->whereor('orign_name', "like","%mkv");
			})
			->count()/$statics["fileNum"]*10000)/100;
			$statics['otherRate'] = 100-($statics["videoRate"]+$statics["audioRate"]+$statics["imgRate"]);
		}
		$timeNow=time();
		$statics["trendFile"]="";
		$statics["trendUser"]="";
		$statics["trendDate"]="";
		for ($i=0; $i < 13; $i++) { 
			$statics["trendFile"].= Db::name('files')->where('upload_date','between time',[date("Y-m-d",$timeNow-(12-$i)*3600*24),date("Y-m-d",$timeNow-(11-$i)*3600*24)])->count().",";
		}
		for ($i=0; $i < 13; $i++) { 
			$statics["trendUser"].= Db::name('users')->where('user_date','between time',[date("Y-m-d",$timeNow-(12-$i)*3600*24),date("Y-m-d",$timeNow-(11-$i)*3600*24)])->count().",";
		}
		for ($i=0; $i < 13; $i++) { 
			$statics["trendDate"].='"'.date("m月d日",$timeNow-(12-$i)*3600*24).'",';
		}
		$statics["trendFile"] = rtrim($statics["trendFile"],",");
		$statics["trendDate"] = rtrim($statics["trendDate"],",");
		$statics["trendUser"] = rtrim($statics["trendUser"],",");
		return $statics;
	}

	public function saveBasicSetting($options){
		$siteUrl = rtrim($options["siteURL"],"/")."/";
		$options["siteURL"]=$siteUrl;
		return $this->saveOptions($options);
	}

H
HFO4 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	public function saveColorSetting($options){
		$colorOptions = [];
		foreach ($options["color"] as $key => $value) {
			$color = \json_decode($value,true);
			$validate = ($color!==null)&&array_key_exists("palette",$color) &&
						array_key_exists("common",$color["palette"]) &&
						array_key_exists("black",$color["palette"]["common"]) &&
						array_key_exists("white",$color["palette"]["common"]) &&
						array_key_exists("background",$color["palette"]) &&
						array_key_exists("primary",$color["palette"]) &&
						array_key_exists("secondary",$color["palette"]) &&
						array_key_exists("error",$color["palette"]) &&
						array_key_exists("text",$color["palette"]) &&
						array_key_exists("explorer",$color["palette"]) &&
						array_key_exists("main",$color["palette"]["primary"]) &&
						array_key_exists("main",$color["palette"]["secondary"]);
			if(!$validate){
				return ["error"=>1,"msg"=>"第 ".($key+1)." 行配色方案格式有误"];
			}
			$colorOptions[$color["palette"]["primary"]["main"]] = $color;
		}
		if(!array_key_exists($options["header"],$colorOptions)){
			return ["error"=>1,"msg"=>"第 ".($key+1)." 默认配色不存在"];
		}
		$optionsForSet = [
			"defaultTheme" => $options["header"],
			"themes" => json_encode($colorOptions),
		];
		return $this->saveOptions($optionsForSet);
	}

H
Init  
HFO4 已提交
136 137 138 139 140 141 142 143 144 145 146
	public function saveRegSetting($options){
		foreach(["email_active","login_captcha","reg_captcha","forget_captcha"] as $key){
			$options[$key] = array_key_exists($key,$options) ? $options[$key] : 0;
		}
		return $this->saveOptions($options);
	}

	public function saveMailSetting($options){
		return $this->saveOptions($options);
	}

H
HFO4 已提交
147 148 149 150
	public function saveAria2Setting($options){
		return $this->saveOptions($options);
	}

H
HFO4 已提交
151 152 153 154
	public function saveTaskOption($options){
		return $this->saveOptions($options);
	}

H
Init  
HFO4 已提交
155 156 157 158 159 160 161 162 163
	public function saveMailTemplate($options){
		return $this->saveOptions($options);
	}

	public function AddGroup($options){
		$options["max_storage"] = $options["max_storage"]*$options["sizeTimes"];
		unset($options["sizeTimes"]);
		$options["grade_policy"] = 0;
		$options["policy_list"] = $options["policy_name"];
H
HFO4 已提交
164
		$options["aria2"] = $options["aria2"] ? "1,1,1" : "0,0,0";
H
Init  
HFO4 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
		try {
			Db::name("groups")->insert($options);
		} catch (Exception $e) {
			return ["error"=>1,"msg"=>$e->getMessage()];
		}
		return ["error"=>200,"msg"=>"设置已保存"];
	}

	public function addPolicy($options){
		$options["max_size"] = $options["max_size"]*$options["sizeTimes"];
		unset($options["sizeTimes"]);
		$options["server"] = isset($options["server"]) ? $options["server"] : "/Upload";
		foreach (["bucketname","bucket_private","bucketname","ak","sk","op_name","op_pwd","mimetype","namerule"] as $key => $value) {
			$options[$value] = isset($options[$value]) ? $options[$value] : "0";
		}
		if(empty($options["filetype"])){
			$options["filetype"]="[]";
		}else{
			$options["filetype"] = json_encode([0=>["ext"=>$options["filetype"],"title"=>"default"]]);
		}
		if($options["policy_type"] == "upyun"){
			$options["server"] = "https://v0.api.upyun.com/".$options["bucketname"];
		}
		try {
			Db::name("policy")->insert($options);
		} catch (Exception $e) {
			return ["error"=>1,"msg"=>$e->getMessage()];
		}
H
HFO4 已提交
193
		return ["error"=>200,"msg"=>"设置已保存","id"=>Db::name('policy')->getLastInsID()];
H
Init  
HFO4 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
	}

	public function editPolicy($options){
		$policyId = $options["id"];
		$options["max_size"] = $options["max_size"]*$options["sizeTimes"];
		unset($options["sizeTimes"]);
		unset($options["id"]);
		if(empty($options["filetype"])){
			$options["filetype"]="[]";
		}else{
			$options["filetype"] = json_encode([0=>["ext"=>$options["filetype"],"title"=>"default"]]);
		}
		try {
			Db::name("policy")->where("id",$policyId)->update($options);
		} catch (Exception $e) {
			return ["error"=>1,"msg"=>$e->getMessage()];
		}
		return ["error"=>200,"msg"=>"设置已保存"];
	}

	public function saveGroup($options){
		$groupId = $options["id"];
		unset($options["id"]);
		$options["max_storage"] = $options["max_storage"]*$options["sizeTimes"];
		unset($options["sizeTimes"]);
H
HFO4 已提交
219
		$options["aria2"] = $options["aria2"] ? "1,1,1" : "0,0,0";
H
Init  
HFO4 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
		try {
			Db::name("groups")->where("id",$groupId)->update($options);
		} catch (Exception $e) {
			return ["error"=>1,"msg"=>$e->getMessage()];
		}
		return ["error"=>200,"msg"=>"设置已保存"];
	}

	public function saveOptions($options){
		try {
			foreach ($options as $key => $value) {
				Db::name("options")->where("option_name",$key)->update(["option_value"=>$value]);
			}
		} catch (Exception $e) {
			return ["error"=>1,"msg"=>$e->getMessage()];
		}
		return ["error"=>200,"msg"=>"设置已保存"];
	}

	public function getAvaliableGroup(){
		$groupData = Db::name("groups")->where("id","neq",2)->select();
		return $groupData;
	}

	public function saveCron($options){
		$cronId = $options["id"];
		unset($options["id"]);
		Db::name("corn")->where("id",$cronId)->update($options);
	}

	public function getAvaliablePolicy(){
		$policyData = Db::name("policy")->select();
		return $policyData;
	}

	public function sendTestMail($options){
		$mailObj = new Mail();
		if(empty($options["receiveMail"])){
			return ["error"=>1,"msg"=>"接收邮箱不能为空"];
		}
		$sendResult = $mailObj->Send($options["receiveMail"],"发信测试",$options["subject"],$options["content"]);
		if($sendResult){
			return ["error"=>200,"msg"=>"发送成功"];
		}else{
			return ["error"=>1,"msg"=>$mailObj->errorMsg];
		}
	}

	public function deleteSingle($id){
		$fileRecord = Db::name("files")->where("id",$id)->find();
		return FileManage::DeleteHandler([0 => rtrim($fileRecord["dir"],"/")."/".$fileRecord["orign_name"]],$fileRecord["upload_user"]);
	}

	public function deletePolicy($id){
		$groupData = Db::name("groups")->where("policy_name",$id)->select();
		if(!empty($groupData)){
			return ["error"=>true,"msg"=>"此上传策略正在被以下用户组使用:".join(",",array_column($groupData, "group_name"))];
		}
		Db::name("policy")->where("id",$id)->delete();
		return ["error"=>false,"msg"=>"已删除"];
	}

	public function deleteGroup($id){
		$userData = Db::name("users")->where("user_group",$id)->find();
		if(!empty($userData)){
			return ["error"=>true,"msg"=>"此用户组下仍有用户,请先删除这些用户"];
		}
		if($id == 1 || $id == 2){
			return ["error"=>true,"msg"=>"系统保留用户组,无法删除"];
		}
		Db::name("groups")->where("id",$id)->delete();
		return ["error"=>false,"msg"=>"已删除"];
	}

	public function getConfigFile($type){
		switch ($type) {
			case 'common':
				$configPath = ROOT_PATH ."application/config.php"; 
				$basicPath = "application/config.php";
				break;
			case 'database':
				if(file_exists( ROOT_PATH ."application/database.lock")){
					return ["出于安全考虑,默认禁止直接编辑数据库配置文件。如果需要开启编辑,请手动删除 application/database.lock 文件。","application/database.php"];
				}
				$configPath = ROOT_PATH ."application/database.php"; 
				$basicPath = "application/database.php";
				break;
			case 'route':
				$configPath = ROOT_PATH ."application/route.php"; 
				$basicPath = "application/route.php";
				break;
			case 'tags':
				$configPath = ROOT_PATH ."application/tags.php"; 
				$basicPath = "application/tags.php";
				break;
			default:
				die("");
				break;
		}
		return [file_get_contents($configPath),$basicPath];
	}

	public function saveConfigFile($options){
		switch ($options["type"]) {
			case 'common':
				file_put_contents(ROOT_PATH ."application/config.php",$options["content"]);
				break;
			case 'route':
				file_put_contents(ROOT_PATH ."application/route.php",$options["content"]);
				break;
			case 'tags':
				file_put_contents(ROOT_PATH ."application/tags.php",$options["content"]);
				break;
			case 'database':
				if(file_exists( ROOT_PATH ."application/database.lock")){
					return ["error"=>true,"msg"=>"出于安全考虑,默认禁止直接编辑数据库配置文件。如果需要开启编辑,请手动删除 application/database.lock 文件。"];
				}
				file_put_contents(ROOT_PATH ."application/database.php",$options["content"]);
				break;
			default:
				# code...
				break;
		}
		return ["error"=>false,"msg"=>""];
	}

	public function deleteMultiple($id){
		$fileInfo = json_decode($id,true);
		$pathGroup = [];
		foreach ($fileInfo as $key => $value) {
			$pathGroup[$value["uid"]] = isset($pathGroup[$value["uid"]]) ? $pathGroup[$value["uid"]] : [];
			array_push($pathGroup[$value["uid"]], $value["path"]);
		}
		foreach ($pathGroup as $key => $value) {
			FileManage::DeleteHandler($value,$key);
		}
		return ["error"=>200,"msg"=>"删除成功"];
	}

	public function deleteShare($ids){
		Db::name("shares")->where("id","in",$ids)->delete();
		return ["error"=>false,"msg"=>"删除成功"];
	}

	public function deleteOrder($id){
		Db::name("order")->where("id",$id)->delete();
		return ["error"=>false,"msg"=>"删除成功"];
	}

	public function deleteUser($id,$userNow){
		if($userNow == $id){
			return ["error"=>true,"msg"=>"我的老伙计,你可不能删除你自己"];
		}
		//删除用户所有文件及目录
		FileManage::DirDeleteHandler([0 => "/"],$id);
		//删除此用户所有分享
		Db::name("shares")->where("owner",$id)->delete();
		//删除此用户
		Db::name("users")->where("id",$id)->delete();
		return ["error"=>false,"msg"=>"删除成功"];
	}

	public function changeShareType($id){
		$shareId = $id;
		$shareObj = new ShareHandler($shareId,false);
		if(!$shareObj->querryStatus){
			 return array(
				"error" => 1,
				"msg" => "分享不存在"
				);
		}
		return $shareObj->changePromission(0,true);
	}

	public function getUserInfo($id){
		$userData = Db::name("users")->where("id",$id)->find();
		$userData["used_storage"] =getSize($userData["used_storage"]);
		return $userData;
	}

H
HFO4 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
	public function listDownloads(){
		$pageSize = 10;
		$this->pageData = Db::name("download")
		->order("id desc")
		->paginate($pageSize);
		$this->dataTotal = Db::name("download")
		->order("id desc")
		->count();
		$this->pageTotal = ceil($this->dataTotal/$pageSize);
		$this->listData = $this->pageData->all();
		$userCache=[];
		$userCacheList=[];
		foreach ($this->listData as $key => $value) {
			if(in_array($value["owner"], $userCacheList)){
				$this->listData[$key]["user"] = $userCache[$value["owner"]];
			}else{
				$this->listData[$key]["user"] = Db::name("users")->where("id",$value["owner"])->find();
				array_push($userCacheList,$value["owner"]);
				$userCache[$value["owner"]] = $this->listData[$key]["user"];
			}
			$connectInfo = json_decode($value["info"],true);
			if(isset($connectInfo["dir"])){
				$this->listData[$key]["fileName"] = basename($connectInfo["dir"]);
				$this->listData[$key]["completedLength"] = $connectInfo["completedLength"];
				$this->listData[$key]["totalLength"] = $connectInfo["totalLength"];
				$this->listData[$key]["downloadSpeed"] = $connectInfo["downloadSpeed"];
			}else{
				if(floor($value["source"])==$value["source"]){
H
HFO4 已提交
428
					$this->listData[$key]["fileName"] = Db::name("files")->where("id",$value["source"])->column("orign_name");
H
HFO4 已提交
429 430 431 432 433 434 435 436 437 438 439
				}else{
					$this->listData[$key]["fileName"] = $value["source"];
				}
				$this->listData[$key]["completedLength"] = 0;
				$this->listData[$key]["totalLength"] = 0;
				$this->listData[$key]["downloadSpeed"] = 0;
			}
		}
		$this->pageNow = input("?get.page")?input("get.page"):1;
	}

H
HFO4 已提交
440 441 442 443 444 445 446
	public function getTasks(){
		$taskData = Db::name("task")
		->order("id DESC")
		->paginate(10);
		return $taskData;
	}

H
Init  
HFO4 已提交
447
	public function listFile(){
H
Fix #32  
HFO4 已提交
448
		$pageSize = !cookie('?pageSize') ? 10 : cookie('pageSize');
H
Init  
HFO4 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
		$orderType = empty(cookie('orderMethodFile')) ? "id DESC" : cookie('orderMethodFile');
		$this->pageData = Db::name("files")
		->where(function ($query) {
			if(!empty(cookie('fileSearch'))){
			    $query->where('orign_name', "like","%".cookie('fileSearch')."%");
			}
		})
		->where(function ($query) {
			if(!empty(cookie('filePolicy'))){
			    $query->where('policy_id', cookie('filePolicy'));
			}
		})
		->where(function ($query) {
			if(!empty(cookie('searchValue'))){
			    $query->where(cookie('searchCol'),"like", cookie('searchValue'));
			}
		})
		->order($orderType)
		->paginate($pageSize);
		$this->dataTotal = Db::name("files")
		->where(function ($query) {
			if(!empty(cookie('fileSearch'))){
			    $query->where('orign_name', "like","%".cookie('fileSearch')."%");
			}
		})
		->where(function ($query) {
			if(!empty(cookie('filePolicy'))){
			    $query->where('policy_id', cookie('filePolicy'));
			}
		})
		->where(function ($query) {
			if(!empty(cookie('searchValue'))){
			    $query->where(cookie('searchCol'),"like", cookie('searchValue'));
			}
		})
		->order($orderType)
		->count();
		$this->pageTotal = ceil($this->dataTotal/$pageSize);
		$this->listData = $this->pageData->all();
		$userCache=[];
		$userCacheList=[];
		foreach ($this->listData as $key => $value) {
			if(in_array($value["upload_user"], $userCacheList)){
				$this->listData[$key]["user"] = $userCache[$value["upload_user"]];
			}else{
				$this->listData[$key]["user"] = Db::name("users")->where("id",$value["upload_user"])->find();
				array_push($userCacheList,$value["upload_user"]);
				$userCache[$value["upload_user"]] = $this->listData[$key]["user"];
			}
		}
		$this->pageNow = input("?get.page")?input("get.page"):1;
	}

	public function listUser(){
H
Fix #32  
HFO4 已提交
503
		$pageSize = !cookie('?pageSize') ? 10 : cookie('pageSize');
H
Init  
HFO4 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
		$orderType = empty(cookie('orderMethodUser')) ? "id DESC" : cookie('orderMethodUser');
		$this->pageData = Db::name("users")
		->where(function ($query) {
			if(!empty(cookie('userStatus'))){
			    $query->where('user_status', cookie('userStatus')-1);
			}
		})
		->where(function ($query) {
			if(!empty(cookie('userSearch'))){
			    $query->where('user_nick', "like","%".cookie('userSearch')."%")
			    ->whereOr("id",cookie('userSearch'))
			    ->whereOr("user_email","like","%".cookie('userSearch')."%");
			}
		})
		->where(function ($query) {
			if(!empty(cookie('userGroup'))){
			    $query->where('user_group', cookie('userGroup'));
			}
		})
		->where(function ($query) {
			if(!empty(cookie('searchValueUser'))){
			    $query->where(cookie('searchColUser'),"like", cookie('searchValueUser'));
			}
		})
		->order($orderType)
		->paginate($pageSize);
		$this->dataTotal = Db::name("users")
		->where(function ($query) {
			if(!empty(cookie('userStatus'))){
			    $query->where('user_status', cookie('userStatus')-1);
			}
		})
		->where(function ($query) {
			if(!empty(cookie('userSearch'))){
			    $query->where('user_nick', "like","%".cookie('userSearch')."%")
			    ->whereOr("id",cookie('userSearch'))
			    ->whereOr("user_email","like","%".cookie('userSearch')."%");
			}
		})
		->where(function ($query) {
			if(!empty(cookie('userGroup'))){
			    $query->where('user_group', cookie('userGroup'));
			}
		})
		->where(function ($query) {
			if(!empty(cookie('searchValueUser'))){
			    $query->where(cookie('searchColUser'),"like", cookie('searchValueUser'));
			}
		})
		->order($orderType)
		->count();
		$this->pageTotal = ceil($this->dataTotal/$pageSize);
		$this->listData = $this->pageData->all();
		$groupCache=[];
		$groupCacheList=[];
		foreach ($this->listData as $key => $value) {
			if(in_array($value["user_group"], $groupCacheList)){
				$this->listData[$key]["group"] = $groupCache[$value["user_group"]];
			}else{
				$this->listData[$key]["group"] = Db::name("groups")->where("id",$value["user_group"])->find();
				array_push($groupCacheList,$value["user_group"]);
				$groupCache[$value["user_group"]] = $this->listData[$key]["group"];
			}
		}
		$this->pageNow = input("?get.page")?input("get.page"):1;
	}

	public function listShare(){
H
Fix #32  
HFO4 已提交
572
		$pageSize = !cookie('?pageSize') ? 10 : cookie('pageSize');
573
		$orderType = empty(cookie('orderMethodShare')) ? "share_time DESC" : cookie('orderMethodShare');
H
Init  
HFO4 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
		$this->pageData = Db::name("shares")
		->where(function ($query) {
			if(!empty(cookie('shareSearch'))){
			    $query->where('source_name', "like","%".cookie('shareSearch')."%")->whereOr('origin_name', "like","%".cookie('shareSearch')."%");
			}
		})
		->where(function ($query) {
			if(!empty(cookie('shareType'))){
			    $query->where('type', cookie('shareType'));
			}
		})
		->order($orderType)
		->paginate($pageSize);
		$this->dataTotal = Db::name("shares")
		->where(function ($query) {
			if(!empty(cookie('shareSearch'))){
			    $query->where('source_name', "like","%".cookie('shareSearch')."%")->whereOr('origin_name', "like","%".cookie('shareSearch')."%");
			}
		})
		->where(function ($query) {
			if(!empty(cookie('shareType'))){
			    $query->where('type', cookie('shareType'));
			}
		})
		->order($orderType)
		->count();
		$this->pageTotal = ceil($this->dataTotal/$pageSize);
		$this->listData = $this->pageData->all();
		$userCache=[];
		$userCacheList=[];
		foreach ($this->listData as $key => $value) {
			if(in_array($value["owner"], $userCacheList)){
				$this->listData[$key]["user"] = $userCache[$value["owner"]];
			}else{
				$this->listData[$key]["user"] = Db::name("users")->where("id",$value["owner"])->find();
				array_push($userCacheList,$value["owner"]);
				$userCache[$value["owner"]] = $this->listData[$key]["user"];
			}
		}
		$this->pageNow = input("?get.page")?input("get.page"):1;
	}

	public function listPolicy(){
H
Fix #32  
HFO4 已提交
617
		$pageSize =!cookie('?pageSize') ? 10 : cookie('pageSize');
H
Init  
HFO4 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
		$this->pageData = Db::name("policy")
		->where(function ($query) {
			if(!empty(cookie('policySearch'))){
			    $query->where('policy_name', "like","%".cookie('policySearch')."%");
			}
		})
		->where(function ($query) {
			if(!empty(cookie('policyType'))){
			    $query->where('policy_type', cookie('policyType'));
			}
		})
		->order("id DESC")
		->paginate($pageSize);
		$this->dataTotal = Db::name("policy")
		->where(function ($query) {
			if(!empty(cookie('policySearch'))){
			    $query->where('policy_name', "like","%".cookie('policySearch')."%");
			}
		})
		->where(function ($query) {
			if(!empty(cookie('policyType'))){
			    $query->where('policy_type', cookie('policyType'));
			}
		})
		->order("id DESC")
		->count();
		$this->pageTotal = ceil($this->dataTotal/$pageSize);
		$this->listData = $this->pageData->all();
		$this->pageNow = input("?get.page")?input("get.page"):1;
		foreach ($this->listData as $key => $value) {
			$this->listData[$key]["file_num"] = Db::name("files")->where("policy_id",$value["id"])->count();
			$this->listData[$key]["file_size"] = Db::name("files")->where("policy_id",$value["id"])->sum("size");
		}
	}

	public function getFileInfo($id){
		$fileRecord = Db::name("files")->where("id",$id)->find();
		$policyRecord = Db::name("policy")->where("id",$fileRecord["policy_id"])->find();
		$fileRecord["policy"] = $policyRecord;
		return $fileRecord;
	}

	public function saveThemeFile($options){
		$fileName=$options["name"];
		$dir = ROOT_PATH."application/index/view/";
		$fileList=[];
		$fileList=$fileList+scandir($dir);
		$pathList=["/"=>$fileList];
		foreach (["admin","explore","file","home","index","member","profile","share"] as $key => $value) {
			$childPath = scandir($dir.$value."/");
			$fileList=array_merge($fileList,$childPath);
			$pathList = array_merge($pathList,[$value => $childPath]);
		}
		foreach ($fileList as $key => $value) {
			if(substr_compare($value, ".html", -strlen(".html")) != 0){
				unset($fileList[$key]);
			}
		}
		foreach($pathList as $key=>$val){
		    if(in_array($fileName.".html",$val)){
		        $parentPath = $key;
		        break;
		    }
		}
		file_put_contents($dir.rtrim($parentPath,"/")."/".$fileName.".html",$options["content"]);
		return ["error"=>false,"msg"=>"成功"];
	}

	public function saveUser($options){
		if(empty($options["user_pass"])){
			unset($options["user_pass"]);
		}else{
			$options["user_pass"] = md5(config('salt').$options["user_pass"]);
		}
		$userId = $options["uid"];
		unset($options["uid"]);
		try {
			Db::name("users")->where("id",$userId)->update($options);
		} catch (Exception $e) {
			return ["error"=>1,"msg"=>$e->getMessage()];
		}
		return ["error"=>200,"msg"=>"设置已保存"];
	}

	public function banUser($id,$uid){
		if($id == $uid){
			return ["error"=>1,"msg"=>"我的老伙计,你怎么能封禁你自己?"];
		}
		$userData = Db::name("users")->where("id",$id)->find();
		$statusNew = $userData["user_status"] == 1 ? 0 : 1;
		Db::name("users")->where("id",$id)->update(["user_status" => $statusNew]);
		return ["error"=>200,"msg"=>"设置已保存"];
	}

	public function listGroup(){
		$pageSize = empty(cookie('pageSize')) ? 10 : cookie('pageSize');
		$this->pageData = Db::name("groups")
		->order("id DESC")
		->paginate($pageSize);
		$this->dataTotal = Db::name("groups")
		->order("id DESC")
		->count();
		$this->pageTotal = ceil($this->dataTotal/$pageSize);
		$this->listData = $this->pageData->all();
		$this->pageNow = input("?get.page")?input("get.page"):1;
		foreach ($this->listData as $key => $value) {
			$this->listData[$key]["policy"] = Db::name("policy")->where("id",$value["policy_name"])->find();
			$this->listData[$key]["user_num"] = Db::name("users")->where("user_group",$value["id"])->count();
		}
	}
	
	public function addUser($options){
		$options["user_pass"] = md5(config('salt').$options["user_pass"]);
		if(Db::name('users')->where('user_email',$options["user_email"])->find() !=null){
			return ["error" => true,"msg"=>"该邮箱已被注册"];
		}
		$sqlData = [
			'user_email' => $options["user_email"],
			'user_pass' => $options["user_pass"],
			'user_status' => $options["user_status"],
			'user_group' => $options["user_group"],
			'group_primary' => $options["user_group"],
			'user_date' => date("Y-m-d H:i:s"),
			'user_nick' => $options["user_nick"],
			'user_activation_key' => "n",
			'used_storage' => 0,
			'two_step'=>"0",
			'webdav_key' =>$options["user_pass"],
			'delay_time' =>0,
			'avatar' => "default",
			'profile' => true,
		];
		if(Db::name('users')->insert($sqlData)){
			$userId = Db::name('users')->getLastInsID();
			Db::name('folders')->insert( [
				'folder_name' => '根目录',
				'parent_folder' => 0,
				'position' => '.',
				'owner' => $userId,
				'date' => date("Y-m-d H:i:s"),
				'position_absolute' => '/',
			]);
		}
		return ["error"=>0,"msg"=>"设置已保存"];
	}
H
HFO4 已提交
763 764 765 766 767 768 769 770 771 772 773 774 775 776
	
	public function updateOnedriveToken($policyId){
		$policyData = Db::name("policy")->where("id",$policyId)->find();

		if(empty($policyData)){
			throw new \think\Exception("Policy not found");
		}
		$onedrive = new Client([
			'client_id' => $policyData["bucketname"],
		]);
		$url = $onedrive->getLogInUrl([
			'offline_access',
			'files.readwrite.all',
		], Option::getValue("siteURL")."Admin/oneDriveCalllback");
H
HFO4 已提交
777
		echo "<script>location.href='".$url."'</script>正在跳转至Onedrive账号授权页面,如果没有跳转,请<a href='$url'>点击这里</a>。";
H
HFO4 已提交
778 779 780 781 782 783 784 785 786

		Db::name("policy")->where("id",$policyId)->update([
			"sk" => json_encode($onedrive->getState()),
		]);
		\think\Session::set('onedrive.pid',$policyId);
		
	}

	public function oneDriveCalllback($code){
787 788 789
		if(input("?get.error")){
			throw new \think\Exception(input("get.error_description"));
		}
H
HFO4 已提交
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
		$policyId = \think\Session::get('onedrive.pid');
		$policyData = Db::name("policy")->where("id",$policyId)->find();
		$onedrive = new Client([
			'client_id' => $policyData["bucketname"],
		
			// Restore the previous state while instantiating this client to proceed in
			// obtaining an access token.
			'state' => json_decode($policyData["sk"]),
		]);
		
		// Obtain the token using the code received by the OneDrive API.
		$onedrive->obtainAccessToken($policyData["ak"], $_GET['code']);
		
		// Persist the OneDrive client' state for next API requests.
		Db::name("policy")->where("id",$policyId)->update([
			"sk" => json_encode($onedrive->getState()),
		]);
H
HFO4 已提交
807
		echo "<script>location.href='/Admin/PolicyList?page=1'</script>";
H
HFO4 已提交
808 809
	}

H
Init  
HFO4 已提交
810 811
}
?>