📄 btg.php
字号:
return $this->addExtraOutput(); } if($file['error'] != 0) { $errors = array( 0=>"There is no error, the file uploaded with success.", 1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini.", 2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.", 3=>"The uploaded file was only partially uploaded. Please try again.", 4=>"File missing.", 6=>"Missing a temporary folder." ); $this->log_error("Failed: ".$errors[(int)$file['error']], (int)$file['error']); return $this->addExtraOutput(); } if(!is_uploaded_file($file['tmp_name'])) { $this->log_error("Failed: Temporary file is not uploaded!"); return $this->addExtraOutput(); } // File should be OK. $filename = basename($file['name']); return $this->contextCreateWithData($filename, $file['tmp_name']); } /// Start a torrent public function contextCreateWithData($torrent_filename, $tmpfile="") { $this->attachLast(); if(!$this->sessionAttached) return $this->addExtraOutput(""); if($tmpfile == "") // If no specific tempfile was specified, use the torrents filename for this. $tmpfile = $torrent_filename; // We are only interested in the files name, path information is not interesting... $torrent_filename = basename($torrent_filename); $data = new SBuffer(); $data->read($tmpfile); $r = $this->executeCommand(new contextCreateWithDataCommand($torrent_filename, $data, $this->autostart)); if($r == NULL) return $this->addExtraOutput(""); $output = ""; if($r instanceof ackCommand) $output.="<ack/>"; return $this->addExtraOutput($output); } /// Get status of one or more contexts function contextStatus($contextID=contextCommand::UNDEFINED_CONTEXT, $showAll=true) { $this->attachLast(); if(!$this->sessionAttached) return $this->addExtraOutput(""); $output = ""; $r = $this->executeCommand(new contextStatusCommand((int)$contextID, (bool)$showAll), true); if($r instanceof contextStatusResponseCommand || $r instanceof contextAllStatusResponseCommand) { $output = "<contexts>\n"; $status = $r->getStatus(); if(is_array($status)) { foreach($status as $contextStatus) { $output .= "<context>\n"; $output .= "<id>".$contextStatus->getContextID()."</id>\n"; $output .= "<filename>".htmlspecialchars(basename($contextStatus->getFilename()))."</filename>\n"; $output .= "<status>".$contextStatus->getStatus()."</status>\n"; $output .= "<statustext>".$contextStatus->getStatusName()."</statustext>\n"; $output .= "<downloadtotal>".$contextStatus->getDownloadTotal()."</downloadtotal>\n"; $output .= "<uploadtotal>".$contextStatus->getUploadTotal()."</uploadtotal>\n"; $output .= "<failedbytes>".$contextStatus->getFailedBytes()."</failedbytes>\n"; $output .= "<downloadrate>".$contextStatus->getDownloadRate()."</downloadrate>\n"; $output .= "<uploadrate>".$contextStatus->getUploadRate()."</uploadrate>\n"; $output .= "<done>".$contextStatus->getDone()."</done>\n"; $output .= "<filesize>".$contextStatus->getFilesize()."</filesize>\n"; $output .= "<leechers>".$contextStatus->getLeechers()."</leechers>\n"; $output .= "<seeders>".$contextStatus->getSeeders()."</seeders>\n"; $d = $contextStatus->getTimeLeftD(); $h = $contextStatus->getTimeLeftH(); $m = $contextStatus->getTimeLeftM(); $s = $contextStatus->getTimeLeftS(); if($d > 0) $timeleft = sprintf("%dd, %dh, %dm", $d, $h, $m); elseif($h > 0) $timeleft = sprintf("%dh, %dm, %ds", $h, $m, $s); elseif($m > 0) $timeleft = sprintf("%dm, %ds", $m, $s); else $timeleft = sprintf("%ds", $s); $output .= "<timeleft>".$timeleft."</timeleft>\n"; $trackerStatus = $contextStatus->getTrackerStatus(); $output .= "<trackerstatus>".$trackerStatus->getStatus()."</trackerstatus>\n"; $output .= "<trackerstatustext>".htmlspecialchars($trackerStatus->getDescription())."</trackerstatustext>\n"; $output .= "<trackerstatusmessage>".htmlspecialchars($trackerStatus->getMessage())."</trackerstatusmessage>\n"; $output .= "<activitycounter>".$contextStatus->getActivityCounter()."</activitycounter>\n"; $output .= "</context>\n"; } }else { $output .= "<context>\n"; $output .= "<id>".$status->getContextID()."</id>\n"; $output .= "<filename>".htmlspecialchars(basename($status->getFilename()))."</filename>\n"; $output .= "<status>".$status->getStatus()."</status>\n"; $output .= "<statustext>".$status->getStatusName()."</statustext>\n"; $output .= "<downloadtotal>".$status->getDownloadTotal()."</downloadtotal>\n"; $output .= "<uploadtotal>".$status->getUploadTotal()."</uploadtotal>\n"; $output .= "<failedbytes>".$status->getFailedBytes()."</failedbytes>\n"; $output .= "<downloadrate>".$status->getDownloadRate()."</downloadrate>\n"; $output .= "<uploadrate>".$status->getUploadRate()."</uploadrate>\n"; $output .= "<done>".$status->getDone()."</done>\n"; $output .= "<filesize>".$status->getFilesize()."</filesize>\n"; $output .= "<leechers>".$status->getLeechers()."</leechers>\n"; $output .= "<seeders>".$status->getSeeders()."</seeders>\n"; $h = $status->getTimeLeftH(); $m = $status->getTimeLeftM(); $s = $status->getTimeLeftS(); if($h > 0) $timeleft = sprintf("%dh, %dm, %ds", $h, $m, $s); elseif($m > 0) $timeleft = sprintf("%dm, %ds", $m, $s); else $timeleft = sprintf("%ds", $s); $output .= "<timeleft>".$timeleft."</timeleft>\n"; $trackerStatus = $status->getTrackerStatus(); $output .= "<trackerstatus>".$trackerStatus->getStatus()."</trackerstatus>\n"; $output .= "<trackerstatustext>".htmlspecialchars($trackerStatus->getDescription())."</trackerstatustext>\n"; $output .= "<trackerstatusmessage>".htmlspecialchars($trackerStatus->getMessage())."</trackerstatusmessage>\n"; $output .= "</context>\n"; } $output .="</contexts>\n"; } else if($contextID == contextCommand::UNDEFINED_CONTEXT && $r instanceof errorCommand) { $output .= "<contexts/>"; }else if($r instanceof errorCommand) { $this->log_error($r->getMessage()); } return $this->addExtraOutput($output); } /// Set limit function contextLimit($contextID=contextCommand::UNDEFINED_CONTEXT, $uploadLimit, $downloadLimit, $seedLimit, $seedTimeout, $allContexts=false) { $this->attachLast(); if(!$this->sessionAttached) return $this->addExtraOutput(""); $output = ""; if(!$this->sessionAttached) $this->log_error("No session attached, can't set limits."); else { $r = $this->executeCommand(new contextLimitCommand((int)$contextID, (int)$uploadLimit, (int)$downloadLimit, (int)$seedLimit, (int)$seedTimeout, (bool)$allContexts), false); if($r instanceof ackCommand) { $output .= "<ack/>\n"; } } return $this->addExtraOutput($output); } /// Get limits of a context function contextLimitStatus($contextID=contextCommand::UNDEFINED_CONTEXT) { $this->attachLast(); $output = ""; if(!$this->sessionAttached) $this->log_error("No session attached, can't get context limit status."); else { $output ="<limits>\n"; if(strstr($contextID, ",") != false) { // $contextID is actualy a , delimited list of contextIDs $ids = explode(",", $contextID); foreach($ids as $id) { $r = $this->executeCommand(new contextLimitStatusCommand((int)$id, false), true); if($r instanceof contextLimitStatusResponseCommand) { $output .= "<limit>\n"; $output .= "<id>".$id."</id>\n"; $output .= "<uploadLimit>".$r->getUploadLimit()."</uploadLimit>\n"; $output .= "<downloadLimit>".$r->getDownloadLimit()."</downloadLimit>\n"; $output .= "<seedLimit>".$r->getSeedLimit()."</seedLimit>\n"; $output .= "<seedTimeout>".$r->getSeedTimeout()."</seedTimeout>\n"; $output .= "</limit>\n"; } } }else { $r = $this->executeCommand(new contextLimitStatusCommand((int)$contextID, false), false); if($r instanceof contextLimitStatusResponseCommand) { $output .= "<limit>\n"; $output .= "<id>".$contextID."</id>\n"; $output .= "<uploadLimit>".$r->getUploadLimit()."</uploadLimit>\n"; $output .= "<downloadLimit>".$r->getDownloadLimit()."</downloadLimit>\n"; $output .= "<seedLimit>".$r->getSeedLimit()."</seedLimit>\n"; $output .= "<seedTimeout>".$r->getSeedTimeout()."</seedTimeout>\n"; $output .= "</limit>\n"; } } $output.="</limits>\n"; } return $this->addExtraOutput($output); } /// Start a torrent public function contextStart($id) { $this->attachLast(); if(!$this->sessionAttached) return $this->addExtraOutput(""); $r = $this->executeCommand(new contextStartCommand((int)$id, false)); if($r == NULL) return $this->addExtraOutput(""); $output = ""; if($r instanceof ackCommand) $output.="<ack/>"; return $this->addExtraOutput($output); } /// Stop a torrent public function contextStop($id) { $this->attachLast(); if(!$this->sessionAttached) return $this->addExtraOutput(""); $r = $this->executeCommand(new contextStopCommand((int)$id, false)); if($r == NULL) return $this->addExtraOutput(""); $output = ""; if($r instanceof ackCommand) $output.="<ack/>"; return $this->addExtraOutput($output); } /// Delete a torrent public function contextAbort($id, $eraseData=false) { $this->attachLast(); if(!$this->sessionAttached) return $this->addExtraOutput(""); $r = $this->executeCommand(new contextAbortCommand((int)$id, (bool)$eraseData, false)); if($r == NULL) return $this->addExtraOutput(""); $output = ""; if($r instanceof ackCommand) $output.="<ack/>"; return $this->addExtraOutput($output); } /// Delete a torrent public function contextClean($id) { $this->attachLast(); if(!$this->sessionAttached) return $this->addExtraOutput(""); $r = $this->executeCommand(new contextCleanCommand((int)$id, false)); if($r == NULL) return $this->addExtraOutput(""); $output = ""; if($r instanceof ackCommand) $output.="<ack/>"; return $this->addExtraOutput($output); } /// Get peers of a context function contextPeers($contextID=contextCommand::UNDEFINED_CONTEXT) { $this->attachLast(); if(!$this->sessionAttached) return $this->addExtraOutput(""); $output = ""; if(!$this->sessionAttached) $this->log_error("No session attached, can't get context peers."); else { $r = $this->executeCommand(new contextPeersCommand((int)$contextID, false), true); if($r instanceof contextPeersResponseCommand) { $output .= "<peers>\n"; $output .= "<id>".$contextID."</id>\n"; foreach($r->getList() as $peer) { $output .= "<peer>\n"; $output .= "<ip>".$r->getAddress()->getAddress()."</ip>\n"; $output .= "<seeder>".$r->getSeeder()."</seeder>\n"; $output .= "</peer>\n"; } $output .= "</peers>\n"; }else if($r instanceof errorCommand) { $output .= "<peers>\n"; $output .= "<id>".$contextID."</id>\n"; $output .= "</peers>\n"; } } return $this->addExtraOutput($output); } /// Get file info of a context function contextFileInfo($contextID=contextCommand::UNDEFINED_CONTEXT) { $this->attachLast(); if(!$this->sessionAttached) return $this->addExtraOutput(""); $output = ""; if(!$this->sessionAttached) $this->log_error("No session attached, can't get file info."); else { $r = $this->executeCommand(new contextFileInfoCommand((int)$contextID, false), false); if($r instanceof contextFileInfoResponseCommand) { $output .= "<fileinfo>\n"; $output .= "<id>".$contextID."</id>\n"; foreach($r->getFileInfoList() as $file) { $output .= "<file>\n"; $output .= "<filename>".$file->getFilename()."</filename>\n"; $output .= "<filesize>".$file->getFileSize()."</filesize>\n"; $output .= "<pieces>".$file->size()."</pieces>\n"; // Save ont the sent data.. if($file->isFull()) $output .= "<isfull/>\n"; if($file->isEmpty()) $output .= "<isempty/>\n"; $output .= "<bits>"; foreach($file->getBits() as $bit) { if($bit) $output.="1"; else $output.="0"; } $output .= "</bits>\n"; $output .= "</file>\n"; } $output .= "</fileinfo>\n"; } } return $this->addExtraOutput($output); }}// Create BTG interface and ajax interfacetry{ require_once("config.php"); require_once("version.php"); $btg = new BTG($btg_config_url, BTG_BUILD, $btg_config_autostart); if(isset($btg_ca_cert) && isset($btg_client_cert)) $btg->setSSLparams($btg_ca_cert, $btg_client_cert); $ajax = new PHPAjax("btg.php"); if($btg_config_debug >= 1) $btg->setDebug(1); if($btg_config_debug >= 2) $ajax->setDebug(1); // Register AJAX functions $ajax->register('btg_sessionList', array($btg, 'sessionList')); $ajax->register('btg_sessionAttach', array($btg, 'sessionAttach')); $ajax->register('btg_sessionSetup', array($btg, 'sessionSetup')); $ajax->register('btg_sessionDetach', array($btg, 'sessionDoDetach')); $ajax->register('btg_sessionQuit', array($btg, 'sessionQuit')); $ajax->register('btg_cleanAll', array($btg, 'cleanAll')); $ajax->register('btg_contextStatus', array($btg, 'contextStatus')); $ajax->register('btg_contextLimit', array($btg, 'contextLimit')); $ajax->register('btg_contextLimitStatus', array($btg, 'contextLimitStatus')); $ajax->register('btg_contextStart', array($btg, 'contextStart')); $ajax->register('btg_contextStop', array($btg, 'contextStop')); $ajax->register('btg_contextAbort', array($btg, 'contextAbort')); $ajax->register('btg_contextClean', array($btg, 'contextClean')); $ajax->register('btg_contextPeers', array($btg, 'contextPeers')); $ajax->register('btg_contextFileInfo', array($btg, 'contextFileInfo')); $ajax->register('btg_auth', array($btg, 'auth')); $ajax->register('btg_deauth', array($btg, 'deauth')); $ajax->register('btg_globallimit', array($btg, 'globalLimit')); $ajax->register('btg_globallimitstatus', array($btg, 'globalLimitStatus')); // Handle any requests if($ajax->handle_client_request()) { exit(); }}catch(Exception $e){ @header("Content-Type: text/xml"); $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; $output.= "<ajaxResponse>\n"; $output.= "<error>".$e->getMessage()."</error>"; $output.= "<debug>Got Exception. Debug trace:\n"; $output.= $e->getTraceAsString(); $output.= "</debug>"; $output.= "</ajaxResponse>\n"; echo $output;}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -