/**
  * @param string $filename
  * @return array($treestruct,$maxdeep)
  */
 public static function getFileListJson($filename)
 {
     $content = file_get_contents($filename);
     $decoder = new BEncoder();
     $id = function () {
         static $id = 0;
         return $id++;
     };
     try {
         $decoder->decode($content);
     } catch (BEncoderException $e) {
         return null;
     }
     $res = $decoder->source;
     //Single file
     if (isset($res['length'])) {
         $json['text'] = $res['name'];
         $json['id'] = $id();
         $json['size'] = static::fileSizeFormat($res['length']);
         $json['type'] = 'f';
         //单一文件
         return array(json_encode($json), 0);
     } elseif (isset($res['files'])) {
         $genEntry = function ($dirarr, $len, &$root) {
             $currEntry =& $root;
             for ($i = 0; $i < count($dirarr); $i++) {
                 //file item
                 if ($i == count($dirarr) - 1) {
                     array_push($currEntry, array('text' => array_pop($dirarr), 'type' => 'f', 'size' => static::fileSizeFormat($len)));
                     return;
                 }
                 $dir =& $dirarr[$i];
                 //Entry exists?
                 $flag = false;
                 for ($j = 0; $j < count($currEntry); $j++) {
                     $entry = $currEntry[$j];
                     if (isset($entry['text']) && $entry['text'] == $dir) {
                         $flag = true;
                         break;
                     }
                 }
                 if ($flag == false) {
                     $tmp = ['text' => $dir, 'type' => 'd', 'size' => -1, 'children' => array()];
                     $n = array_push($currEntry, $tmp);
                     $newentry =& $currEntry[$n - 1];
                 } else {
                     $newentry =& $currEntry[$j];
                 }
                 //Is children exists?
                 if (!isset($newentry['children'])) {
                     $newentry['children'] = array();
                 }
                 unset($currEntry);
                 $currEntry =& $newentry['children'];
             }
             //Structure is built already
         };
         //Set root
         $json = ['text' => $res['name'], 'type' => 't', 'size' => '-1', 'children' => array()];
         $maxdepth = 1;
         foreach ($res['files'] as $file) {
             if (count($file['path']) > $maxdepth) {
                 $maxdepth = count($file['path']);
             }
             $genEntry($file['path'], $file['length'], $json['children']);
         }
         return array(json_encode($json), $maxdepth);
     } else {
         return null;
     }
 }
 /**
  * @param string $infoPath
  * @param string $mainTracker
  * @param array $backupTracker
  */
 public static function buildTorrentFile($infoPath, $mainTracker, $backupTracker)
 {
     $be = new BEncoder();
     $info = file_get_contents($infoPath);
     $info = $be->decode($info);
     if (empty($info)) {
         return null;
     }
     /** @var User $user */
     $user = User::findOne(Yii::$app->user->getId());
     $passkey = $user->passkey;
     $seed = [];
     $seed['announce'] = $mainTracker . "passkey={$passkey}";
     foreach ($backupTracker as $tracker) {
         $seed['announce-list'] = $tracker . "passkey={$passkey}";
     }
     $seed['created date'] = $seed['created by'] = time();
     $seed['comment'] = 'Welcome To NGPT';
     $seed['encoding'] = 'UTF-8';
     $seed['info'] = $info;
     return $be->encode($seed);
 }
 public function actionTorrent()
 {
     $from = '/tmp/torrentinfo/';
     $to = 'frontend/web/torrents/';
     $files = scandir($from);
     $be = new BEncoder();
     foreach ($files as $file) {
         if (substr($file, -4) != 'info') {
             continue;
         }
         if (file_exists($to . $file)) {
             continue;
         }
         $origin = file_get_contents($from . $file);
         $origin = 'd' . $origin . 'e';
         $arr = $be->decode($origin)['info'];
         if (empty($arr)) {
             echo 'Error: ' . $from . $file;
             return;
         }
         var_dump($arr);
         $res = $be->encode($arr);
         if (empty($res)) {
             echo 'Error: ' . $from . $file;
             return;
         }
         file_put_contents($to . $file, $res);
     }
 }