/**
  * do not use direct, use the public static methods !
  *
  * @param $args
  * @return FluxCLI
  */
 function FluxCLI($args)
 {
     global $cfg;
     // set user-var
     $cfg["user"] = GetSuperAdmin();
     // set admin-var
     $cfg['isAdmin'] = true;
     // set user-agent
     $cfg['user_agent'] = $this->name . "/" . _REVISION_FLUXCLI;
     $_SERVER['HTTP_USER_AGENT'] = $this->name . "/" . _REVISION_FLUXCLI;
     // parse args and set fields
     $argCount = count($args);
     if ($argCount < 1) {
         // invalid args
         $this->_outputError("invalid args.\n");
         return false;
     }
     $this->_script = basename($args[0]);
     $this->_action = isset($args[1]) ? $args[1] : "";
     if ($argCount > 2) {
         $prm = array_splice($args, 2);
         $this->_args = array_map('trim', $prm);
         $this->_argc = count($this->_args);
     } else {
         $this->_args = array();
         $this->_argc = 0;
     }
 }
 /**
  * _maintenanceTransfers
  *
  * @param $trestart
  * @return boolean
  */
 function _maintenanceTransfers($trestart = false)
 {
     global $cfg, $db, $transfers;
     // set var
     $this->_restartTransfers = $trestart;
     // output
     $this->_outputMessage("transfers-maintenance...\n");
     // sanity-check for transfers-dir
     if (!is_dir($cfg["transfer_file_path"])) {
         $this->state = MAINTENANCEANDREPAIR_STATE_ERROR;
         $msg = "invalid dir-settings. no dir : " . $cfg["transfer_file_path"];
         array_push($this->messages, $msg);
         $this->_outputError($msg . "\n");
         return false;
     }
     // pid-files of transfer-clients
     $pidFiles = array();
     if ($dirHandle = @opendir($cfg["transfer_file_path"])) {
         while (false !== ($file = @readdir($dirHandle))) {
             if (strlen($file) > 3 && substr($file, -4, 4) == ".pid") {
                 array_push($pidFiles, $file);
             }
         }
         @closedir($dirHandle);
     }
     // return if no pid-files found
     if (count($pidFiles) < 1) {
         $this->_outputMessage("no pid-files found.\n");
         $this->_outputMessage("transfers-maintenance done.\n");
         return true;
     }
     // get process-list
     $psString = trim(shell_exec("ps x -o pid='' -o ppid='' -o command='' -ww"));
     // test if client for pid is still up
     $this->_bogusTransfers = array();
     foreach ($pidFiles as $pidFile) {
         $transfer = substr($pidFile, 0, -4);
         if (stristr($psString, $transfer) === false) {
             if (getTransferClient($transfer) != "azureus") {
                 array_push($this->_bogusTransfers, $transfer);
             }
         }
     }
     // return if no stale pid-files
     $this->_countProblems = count($this->_bogusTransfers);
     if ($this->_countProblems < 1) {
         $this->_outputMessage("no stale pid-files found.\n");
         $this->_outputMessage("transfers-maintenance done.\n");
         return true;
     }
     /* repair the bogus clients */
     $this->_countFixed = 0;
     $this->_outputMessage("repairing died clients...\n");
     foreach ($this->_bogusTransfers as $transfer) {
         // output
         $this->_outputMessage("repairing " . $transfer . " ...\n");
         // set stopped flag in db
         stopTransferSettings($transfer);
         // rewrite stat-file
         $sf = new StatFile($transfer, getOwner($transfer));
         $sf->running = 0;
         $sf->percent_done = -100.0;
         $sf->time_left = 'Transfer Died';
         $sf->down_speed = 0;
         $sf->up_speed = 0;
         $sf->seeds = 0;
         $sf->peers = 0;
         $sf->write();
         // delete pid-file
         @unlink($cfg["transfer_file_path"] . $transfer . ".pid");
         // DEBUG : log the repair of the bogus transfer
         if ($cfg['debuglevel'] > 0) {
             AuditAction($cfg["constants"]["debug"], "transfers-maintenance : transfer repaired : " . $transfer);
         }
         // output
         $this->_outputMessage("done.\n");
         // count
         $this->_countFixed++;
     }
     // output
     if ($this->_countProblems > 0) {
         $this->_outputMessage("repaired transfers : " . $this->_countFixed . "/" . $this->_countProblems . "\n");
     }
     /* restart transfers */
     if ($this->_restartTransfers) {
         $this->_fixedTransfers = array();
         $this->_outputMessage("restarting died clients...\n");
         // hold current user
         $whoami = $this->_mode == MAINTENANCEANDREPAIR_MODE_CLI ? GetSuperAdmin() : $cfg["user"];
         foreach ($this->_bogusTransfers as $transfer) {
             // output
             $this->_outputMessage("Starting " . $transfer . " ...\n");
             // set current user to transfer-owner
             $cfg["user"] = getOwner($transfer);
             // clientHandler + start
             $ch = ClientHandler::getInstance(getTransferClient($transfer));
             $ch->start($transfer, false, FluxdQmgr::isRunning());
             // DEBUG : log the restart of the died transfer
             if ($cfg['debuglevel'] > 0) {
                 $staret = $ch->state == CLIENTHANDLER_STATE_OK ? "OK" : "FAILED";
                 AuditAction($cfg["constants"]["debug"], "transfers-maintenance : restarted transfer " . $transfer . " by " . $whoami . " : " . $staret);
             }
             if ($ch->state == CLIENTHANDLER_STATE_OK) {
                 // output
                 $this->_outputMessage("done.\n");
                 // add to ary
                 array_push($this->_fixedTransfers, $transfer);
                 // count
                 $this->_countFixed++;
             } else {
                 $this->messages = array_merge($this->messages, $ch->messages);
                 $this->_outputError(implode("\n", $ch->messages) . "\n");
             }
         }
         // set user back
         $cfg["user"] = $whoami;
         // output
         $this->_countFixed = count($this->_fixedTransfers);
         if ($this->_countFixed > 0) {
             $this->_outputMessage("restarted transfers : " . $this->_countFixed . "/" . $this->_countProblems . "\n");
         }
     }
     /* done */
     $this->_outputMessage("transfers-maintenance done.\n");
     // return
     return true;
 }
function resetOwner($file)
{
    global $cfg, $db;
    include_once "AliasFile.php";
    // log entry has expired so we must renew it
    $rtnValue = "";
    $alias = getAliasName($file) . ".stat";
    if (file_exists($cfg["torrent_file_path"] . $alias)) {
        $af = AliasFile::getAliasFileInstance($cfg["torrent_file_path"] . $alias, $torrentowner, $cfg);
        if (IsUser($af->torrentowner)) {
            // We have an owner!
            $rtnValue = $af->torrentowner;
        } else {
            // no owner found, so the super admin will now own it
            $rtnValue = GetSuperAdmin();
        }
        $host_resolved = gethostbyaddr($cfg['ip']);
        $create_time = time();
        $rec = array('user_id' => $rtnValue, 'file' => $file, 'action' => $cfg["constants"]["reset_owner"], 'ip' => $cfg['ip'], 'ip_resolved' => $host_resolved, 'user_agent' => $_SERVER['HTTP_USER_AGENT'], 'time' => $create_time);
        $sTable = 'tf_log';
        $sql = $db->GetInsertSql($sTable, $rec);
        // add record to the log
        $result = $db->Execute($sql);
        showError($db, $sql);
    }
    return $rtnValue;
}
 $tmpl->setvar('watchdir', $watchdir);
 // users
 $watchusers = array();
 $userCount = count($cfg['users']);
 $foundSel = false;
 for ($i = 0; $i < $userCount; $i++) {
     $tmp = $cfg['users'][$i];
     $sel = (!$isNew || $isRefresh) && $user == $tmp ? 1 : 0;
     if ($sel) {
         $foundSel = true;
     }
     array_push($watchusers, array('name' => $tmp, 'is_selected' => $sel));
 }
 if (!$foundSel) {
     // no or invalid user, just set superadmin by default
     $user = GetSuperAdmin();
     foreach ($watchusers as $k => $watchuser) {
         if ($user == $watchuser['name']) {
             $watchusers[$k]['is_selected'] = 1;
         }
     }
 }
 $tmpl->setloop('watch_users', $watchusers);
 // profiles
 if ($with_profiles) {
     $profiles = GetProfilesByUserName($user, $profile);
     $public_profiles = GetPublicProfiles($profile);
     $tmpl->setloop('profiles', $profiles);
     $tmpl->setloop('public_profiles', $public_profiles);
 }
 // checkdir
/**
 * reset Owner
 *
 * @param $transfer
 * @return string
 */
function resetOwner($transfer)
{
    global $cfg, $db, $transfers;
    // log entry has expired so we must renew it
    $rtnValue = "n/a";
    if (file_exists($cfg["transfer_file_path"] . $transfer . ".stat")) {
        $sf = new StatFile($transfer);
        if (IsUser($sf->transferowner)) {
            $rtnValue = $sf->transferowner;
        } else {
            $rtnValue = GetSuperAdmin();
        }
        /* no owner found, so the super admin will now own it */
        // add entry to the log
        $sql = "INSERT INTO tf_log (user_id,file,action,ip,ip_resolved,user_agent,time)" . " VALUES (" . $db->qstr($rtnValue) . "," . $db->qstr($transfer) . "," . $db->qstr($cfg["constants"]["reset_owner"]) . "," . $db->qstr($cfg['ip']) . "," . $db->qstr($cfg['ip_resolved']) . "," . $db->qstr($cfg['user_agent']) . "," . $db->qstr(time()) . ")";
        $result = $db->Execute($sql);
        if ($db->ErrorNo() != 0) {
            dbError($sql);
        }
    }
    $transfers['owner'][$transfer] = $rtnValue;
    return $rtnValue;
}
// public-stats-switch
switch ($cfg['stats_enable_public']) {
    case 1:
        // load default-language and transfers if cache not set
        if (!isset($_SESSION['user']) || !cacheIsSet($_SESSION['user'])) {
            // common functions
            require_once 'inc/functions/functions.common.php';
            // lang file
            loadLanguageFile($cfg["default_language"]);
        }
        // Fluxd
        Fluxd::initialize();
        // Qmgr
        FluxdServiceMod::initializeServiceMod('Qmgr');
        // public stats... show all .. we set the user to superadmin
        $superAdm = GetSuperAdmin();
        if (isset($superAdm) && $superAdm != "") {
            $cfg["user"] = $superAdm;
            $cfg['isAdmin'] = true;
        } else {
            @ob_end_clean();
            exit;
        }
        break;
    case 0:
    default:
        // main.internal
        require_once "inc/main.internal.php";
}
// process request
Stats::processRequest($_REQUEST);