/**
 * process post-params on config-update and init settings-array
 *
 * @return array with settings
 */
function processSettingsParams()
{
    // move hack
    unset($_POST['addCatButton']);
    unset($_POST['remCatButton']);
    unset($_POST['categorylist']);
    unset($_POST['category']);
    // init settings array from params
    // process and handle all specials and exceptions while doing this.
    $settings = array();
    // good-look-stats
    $hackStatsPrefix = "hack_goodlookstats_settings_";
    $hackStatsStringLen = strlen($hackStatsPrefix);
    $settingsHackAry = array();
    for ($i = 0; $i <= 5; $i++) {
        $settingsHackAry[$i] = 0;
    }
    $hackStatsUpdate = false;
    // index-page
    $indexPageSettingsPrefix = "index_page_settings_";
    $indexPageSettingsPrefixLen = strlen($indexPageSettingsPrefix);
    $settingsIndexPageAry = array();
    for ($j = 0; $j <= 10; $j++) {
        $settingsIndexPageAry[$j] = 0;
    }
    $indexPageSettingsUpdate = false;
    //
    foreach ($_POST as $key => $value) {
        if (substr($key, 0, $hackStatsStringLen) == $hackStatsPrefix) {
            // good-look-stats
            $idx = (int) substr($key, -1, 1);
            if ($value != "0") {
                $settingsHackAry[$idx] = 1;
            } else {
                $settingsHackAry[$idx] = 0;
            }
            $hackStatsUpdate = true;
        } else {
            if (substr($key, 0, $indexPageSettingsPrefixLen) == $indexPageSettingsPrefix) {
                // index-page
                $idx = (int) substr($key, $indexPageSettingsPrefixLen - strlen($key));
                if ($value != "0") {
                    $settingsIndexPageAry[$idx] = 1;
                } else {
                    $settingsIndexPageAry[$idx] = 0;
                }
                $indexPageSettingsUpdate = true;
            } else {
                switch ($key) {
                    case "path":
                        // tf-path
                        $settings[$key] = trim(checkDirPathString($value));
                        break;
                    case "move_paths":
                        // move-hack-paths
                        $dirAry = explode(":", $value);
                        $val = "";
                        for ($idx = 0; $idx < count($dirAry); $idx++) {
                            if ($idx > 0) {
                                $val .= ':';
                            }
                            $val .= trim(checkDirPathString($dirAry[$idx]));
                        }
                        $settings[$key] = trim($val);
                        break;
                    default:
                        // "normal" key-val-pair
                        $settings[$key] = $value;
                }
            }
        }
    }
    // good-look-stats
    if ($hackStatsUpdate) {
        $settings['hack_goodlookstats_settings'] = convertArrayToByte($settingsHackAry);
    }
    // index-page
    if ($indexPageSettingsUpdate) {
        $settings['index_page_settings'] = convertArrayToInteger($settingsIndexPageAry);
    }
    // return
    return $settings;
}
 /**
  * build new jobs-list string from jobs-list array
  *
  * @param $jobs jobs-list as array (each item being a job as array)
  * @return string
  */
 function _jobsSerialize($jobs)
 {
     $return = '';
     foreach ($jobs as $job) {
         if (!isset($job['U']) || strlen(trim($job['U'])) <= 0 || !isset($job['D']) || strlen(trim($job['D'])) <= 0) {
             continue;
         }
         // New format: U= component must be first.
         $jobstr = 'U' . $this->_delimComponent . $job['U'] . $this->_delimJob;
         foreach ($job as $k => $v) {
             if ($k != 'U' && $k != 'D' && strlen($k) > 0 && strlen($v) > 0) {
                 $jobstr .= $k . $this->_delimComponent . $v . $this->_delimJob;
             }
         }
         // D= component must be last -- make sure it has a trailing slash.
         $jobstr .= 'D' . $this->_delimComponent . checkDirPathString($job['D']);
         $return .= (strlen($return) == 0 ? '' : $this->_delimJobs) . $jobstr;
     }
     return $return;
 }
/**
 * process post-params on config-update and init settings-array
 *
 * @param $updateIndexSettings
 * @param $updateGoodlookinSettings
 * @return array with settings
 */
function processSettingsParams($updateIndexSettings = true, $updateGoodlookinSettings = true)
{
    // move
    if (isset($_POST['categorylist'])) {
        unset($_POST['categorylist']);
    }
    if (isset($_POST['category'])) {
        unset($_POST['category']);
    }
    // res-dir
    if (isset($_POST['resdirlist'])) {
        unset($_POST['resdirlist']);
    }
    if (isset($_POST['resdirentry'])) {
        unset($_POST['resdirentry']);
    }
    // init settings array from params
    // process and handle all specials and exceptions while doing this.
    $settings = array();
    // index-page
    if ($updateIndexSettings) {
        $indexPageSettingsPrefix = "index_page_settings_";
        $indexPageSettingsPrefixLen = strlen($indexPageSettingsPrefix);
        $settingsIndexPageAry = array();
        for ($j = 0; $j <= 11; $j++) {
            $settingsIndexPageAry[$j] = 0;
        }
    }
    // good-look-stats
    if ($updateGoodlookinSettings) {
        $hackStatsPrefix = "hack_goodlookstats_settings_";
        $hackStatsStringLen = strlen($hackStatsPrefix);
        $settingsHackAry = array();
        for ($i = 0; $i <= 5; $i++) {
            $settingsHackAry[$i] = 0;
        }
    }
    //
    foreach ($_POST as $key => $value) {
        if ($updateIndexSettings && substr($key, 0, $hackStatsStringLen) == $hackStatsPrefix) {
            // good-look-stats
            $idx = intval(substr($key, -1, 1));
            $settingsHackAry[$idx] = $value != "0" ? 1 : 0;
        } else {
            if ($updateGoodlookinSettings && substr($key, 0, $indexPageSettingsPrefixLen) == $indexPageSettingsPrefix) {
                // index-page
                $idx = intval(substr($key, $indexPageSettingsPrefixLen - strlen($key)));
                $settingsIndexPageAry[$idx] = $value != "0" ? 1 : 0;
            } else {
                switch ($key) {
                    case "path":
                        // tf-path
                        $settings[$key] = trim(checkDirPathString($value));
                        break;
                    case "docroot":
                        // tf-docroot
                        $settings[$key] = trim(checkDirPathString($value));
                        break;
                    case "move_paths":
                        // move-hack-paths
                        if (strlen($value) > 0) {
                            $val = "";
                            $dirAry = explode(":", $value);
                            for ($idx = 0; $idx < count($dirAry); $idx++) {
                                if ($idx > 0) {
                                    $val .= ':';
                                }
                                $val .= trim(checkDirPathString($dirAry[$idx]));
                            }
                            $settings[$key] = trim($val);
                        } else {
                            $settings[$key] = "";
                        }
                        break;
                    default:
                        // "normal" key-val-pair
                        $settings[$key] = $value;
                }
            }
        }
    }
    // index-page
    if ($updateIndexSettings) {
        $settings['index_page_settings'] = convertArrayToInteger($settingsIndexPageAry);
    }
    // good-look-stats
    if ($updateGoodlookinSettings) {
        $settings['hack_goodlookstats_settings'] = convertArrayToByte($settingsHackAry);
    }
    // return
    return $settings;
}
Esempio n. 4
0
     $targetDir = trim(urldecode($_POST['selector']));
 }
 $dirValid = true;
 if (strlen($targetDir) <= 0) {
     $dirValid = false;
 } else {
     // we need absolute paths or stuff will end up in docroot
     // inform user .. dont move it into a fallback-dir which may be a hastle
     if ($targetDir[0] != '/') {
         echo "Target-dirs must be specified with absolute and not relative paths. <br>";
         $dirValid = false;
     }
 }
 // check dir
 if ($dirValid && checkDirectory($targetDir, 0777)) {
     $targetDir = checkDirPathString($targetDir);
     // move
     $cmd = "mv \"" . $cfg["path"] . $_POST['file'] . "\" " . $targetDir . "";
     $cmd .= ' 2>&1';
     $handle = popen($cmd, 'r');
     // get the output and print it.
     $gotError = -1;
     while (!feof($handle)) {
         $buff = fgets($handle, 30);
         echo nl2br($buff);
         @ob_flush();
         @flush();
         $gotError = $gotError + 1;
     }
     pclose($handle);
     if ($gotError <= 0) {
Esempio n. 5
0
 /**
  * Watch Dir
  *
  * @param $watchDir
  * @param $username
  * @param $options
  * @param array $extra
  * @return mixed
  */
 function _watch($watchDir, $username, $options = '', $extra = array())
 {
     global $cfg;
     // check dir
     if (!@is_dir($watchDir)) {
         $this->_outputError("watch-dir " . $watchDir . " is no dir.\n");
         return false;
     }
     // check username
     if (!IsUser($username)) {
         $this->_outputError("username " . $username . " is no valid user.\n");
         return false;
     }
     // parse options
     $optionsSet = $this->_parseOptions(array('d' => 0, 's' => 0, 'p' => 1), $options, $extra);
     if ($optionsSet === false) {
         return false;
     }
     $profile = isset($optionsSet['p']) ? $optionsSet['p'][0] : null;
     $this->_outputMessage("Processing watch-dir " . $watchDir . " for user " . $username . (!empty($profile) ? " using profile " . $profile : '') . " ...\n");
     // process dir
     if ($dirHandle = @opendir($watchDir)) {
         // get input-files
         $input = array();
         while (false !== ($file = @readdir($dirHandle))) {
             if (@is_file($watchDir . $file)) {
                 array_push($input, $file);
             }
         }
         @closedir($dirHandle);
         if (empty($input)) {
             $this->_outputMessage("done. no files found.\n");
             return true;
         }
         // trailing slash
         $watchDir = checkDirPathString($watchDir);
         // build args for _inject
         $newOptions = $this->_buildOptions('dsp', $optionsSet);
         // Pass-thru options 'd', 's' and 'p'.
         // process input-files
         $ctr = array('files' => count($input), 'ok' => 0);
         foreach ($input as $transfer) {
             // inject, increment if ok
             if ($this->_inject($watchDir . $transfer, $username, $newOptions[0], $newOptions[1]) !== false) {
                 $ctr['ok']++;
             }
         }
         if ($ctr['files'] == $ctr['ok']) {
             $this->_outputMessage("done. files: " . $ctr['files'] . "; ok: " . $ctr['ok'] . "\n");
             return true;
         } else {
             $this->_outputError("done with errors. files: " . $ctr['files'] . "; ok: " . $ctr['ok'] . "\n");
             return false;
         }
     } else {
         $this->_outputError("failed to open watch-dir " . $watchDir . ".\n");
         return false;
     }
 }
 /**
  * prepares start of a bittorrent-client.
  * prepares vars and other generic stuff
  * @param $torrent name of the torrent
  * @param $interactive (1|0) : is this a interactive startup with dialog ?
  */
 function prepareStartTorrentClient($torrent, $interactive)
 {
     if ($this->status < 1) {
         $this->status = -1;
         $this->messages .= "Error. ClientHandler in wrong state on prepare-request.";
         return;
     }
     $this->skip_hash_check = "";
     if ($interactive == 1) {
         // interactive, get vars from request vars
         $this->rate = getRequestVar('rate');
         if (empty($this->rate)) {
             if ($this->rate != "0") {
                 $this->rate = $this->cfg["max_upload_rate"];
             }
         }
         $this->drate = getRequestVar('drate');
         if (empty($this->drate)) {
             if ($this->drate != "0") {
                 $this->drate = $this->cfg["max_download_rate"];
             }
         }
         $this->superseeder = getRequestVar('superseeder');
         if (empty($this->superseeder)) {
             $this->superseeder = "0";
         }
         // should be 0 in most cases
         $this->runtime = getRequestVar('runtime');
         if (empty($this->runtime)) {
             $this->runtime = $this->cfg["torrent_dies_when_done"];
         }
         $this->maxuploads = getRequestVar('maxuploads');
         if (empty($this->maxuploads)) {
             if ($this->maxuploads != "0") {
                 $this->maxuploads = $this->cfg["max_uploads"];
             }
         }
         $this->minport = getRequestVar('minport');
         if (empty($this->minport)) {
             $this->minport = $this->cfg["minport"];
         }
         $this->maxport = getRequestVar('maxport');
         if (empty($this->maxport)) {
             $this->maxport = $this->cfg["maxport"];
         }
         $this->maxcons = getRequestVar('maxcons');
         if (empty($this->maxcons)) {
             $this->maxcons = $this->cfg["maxcons"];
         }
         $this->rerequest = getRequestVar("rerequest");
         if (empty($this->rerequest)) {
             $this->rerequest = $this->cfg["rerequest_interval"];
         }
         $this->sharekill = getRequestVar('sharekill');
         if ($this->runtime == "True") {
             $this->sharekill = "-1";
         }
         if (empty($this->sharekill)) {
             if ($this->sharekill != "0") {
                 $this->sharekill = $this->cfg["sharekill"];
             }
         }
         $this->savepath = getRequestVar('savepath');
         $this->skip_hash_check = getRequestVar('skiphashcheck');
     } else {
         // non-interactive, load settings from db and set vars
         $this->rerequest = $this->cfg["rerequest_interval"];
         $this->skip_hash_check = $this->cfg["skiphashcheck"];
         $this->superseeder = 0;
         // load settings
         $settingsAry = loadTorrentSettings(urldecode($torrent));
         $this->rate = $settingsAry["max_upload_rate"];
         $this->drate = $settingsAry["max_download_rate"];
         $this->runtime = $settingsAry["torrent_dies_when_done"];
         $this->maxuploads = $settingsAry["max_uploads"];
         $this->minport = $settingsAry["minport"];
         $this->maxport = $settingsAry["maxport"];
         $this->maxcons = $settingsAry["maxcons"];
         $this->sharekill = $settingsAry["sharekill"];
         $this->savepath = $settingsAry["savepath"];
         // fallback-values if fresh-torrent is started non-interactive or
         // something else strange happened
         if ($this->rate == '') {
             $this->rate = $this->cfg["max_upload_rate"];
         }
         if ($this->drate == '') {
             $this->drate = $this->cfg["max_download_rate"];
         }
         if ($this->runtime == '') {
             $this->runtime = $this->cfg["torrent_dies_when_done"];
         }
         if ($this->maxuploads == '') {
             $this->maxuploads = $this->cfg["max_uploads"];
         }
         if ($this->minport == '') {
             $this->minport = $this->cfg["minport"];
         }
         if ($this->maxport == '') {
             $this->maxport = $this->cfg["maxport"];
         }
         if ($this->maxcons == '') {
             $this->maxcons = $this->cfg["maxcons"];
         }
         if ($this->sharekill == '') {
             $this->sharekill = $this->cfg["sharekill"];
         }
     }
     // queue
     if ($this->cfg["AllowQueing"]) {
         if (IsAdmin()) {
             $this->queue = getRequestVar('queue');
             if ($this->queue == 'on') {
                 $this->queue = "1";
             } else {
                 $this->queue = "0";
             }
         } else {
             $this->queue = "1";
         }
     } else {
         $this->queue = "0";
     }
     //
     $this->torrent = urldecode($torrent);
     $this->alias = getAliasName($this->torrent);
     $this->owner = getOwner($this->torrent);
     if (empty($this->savepath)) {
         $this->savepath = $this->cfg['path'] . $this->owner . "/";
     }
     // ensure path has trailing slash
     $this->savepath = checkDirPathString($this->savepath);
     // The following lines of code were suggested by Jody Steele jmlsteele@stfu.ca
     // This is to help manage user downloads by their user names
     // if the user's path doesnt exist, create it
     if (!is_dir($this->cfg["path"] . "/" . $this->owner)) {
         if (is_writable($this->cfg["path"])) {
             mkdir($this->cfg["path"] . "/" . $this->owner, 0777);
         } else {
             AuditAction($this->cfg["constants"]["error"], "Error -- " . $this->cfg["path"] . " is not writable.");
             if (IsAdmin()) {
                 $this->status = -1;
                 header("location: admin.php?op=configSettings");
                 return;
             } else {
                 $this->status = -1;
                 $this->messages .= "Error. TorrentFlux settings are not correct (path is not writable) -- please contact an admin.";
             }
         }
     }
     // create AliasFile object and write out the stat file
     include_once "AliasFile.php";
     $this->af = AliasFile::getAliasFileInstance($this->cfg["torrent_file_path"] . $this->alias . ".stat", $this->owner, $this->cfg, $this->handlerName);
     //XFER: before a torrent start/restart save upload/download xfer to SQL
     $torrentTotals = getTorrentTotalsCurrent($this->torrent);
     saveXfer($this->owner, $torrentTotals["downtotal"] + 0, $torrentTotals["uptotal"] + 0);
     // update totals for this torrent
     updateTorrentTotals($this->torrent);
     // set param for sharekill
     if ($this->sharekill <= 0) {
         // nice, we seed forever
         $this->sharekill_param = 0;
     } else {
         // recalc sharekill
         $totalAry = getTorrentTotals(urldecode($torrent));
         $upTotal = $totalAry["uptotal"] + 0;
         $torrentSize = $this->af->size + 0;
         $upWanted = $this->sharekill / 100 * $torrentSize;
         if ($upTotal >= $upWanted) {
             // we already have seeded at least
             // wanted percentage. continue to seed
             // forever is suitable in this case ~~
             $this->sharekill_param = 0;
         } else {
             // not done seeding wanted percentage
             $this->sharekill_param = (int) ($this->sharekill - $upTotal / $torrentSize * 100);
             // the type-cast may have floored the value. (tornado lacks
             // precision because only (really?) accepting percentage-values)
             // better to seed more than less so we add a percent in case ;)
             if ($upWanted % $upTotal != 0) {
                 $this->sharekill_param += 1;
             }
             // sanity-check.
             if ($this->sharekill_param <= -1) {
                 $this->sharekill_param = 0;
             }
         }
     }
     if ($this->cfg["AllowQueing"]) {
         if ($this->queue == "1") {
             $this->af->QueueTorrentFile();
             // this only writes out the stat file (does not start torrent)
         } else {
             if ($this->setClientPort() === false) {
                 return;
             }
             $this->af->StartTorrentFile();
             // this only writes out the stat file (does not start torrent)
         }
     } else {
         if ($this->setClientPort() === false) {
             return;
         }
         $this->af->StartTorrentFile();
         // this only writes out the stat file (does not start torrent)
     }
     $this->status = 2;
 }
Esempio n. 7
0
function cliWatchDir($tpath = "", $username = "")
{
    global $cfg;
    if (isset($tpath) && $tpath != "" && isset($username) && $username != "") {
        if (is_dir($tpath)) {
            $cfg['user'] = $username;
            $watchDir = checkDirPathString($tpath);
            if ($dirHandle = opendir($tpath)) {
                while (false !== ($file = readdir($dirHandle))) {
                    if (strtolower(substr($file, -8)) == ".torrent") {
                        $file_name = stripslashes($file);
                        $file_name = str_replace(array("'", ","), "", $file_name);
                        $file_name = cleanFileName($file_name);
                        echo "Injecting and Starting " . $watchDir . $file . " as " . $file_name . " for user " . $cfg['user'] . "...";
                        if (is_file($watchDir . $file) && copy($watchDir . $file, $cfg["torrent_file_path"] . $file_name)) {
                            @unlink($watchDir . $file);
                            chmod($cfg["torrent_file_path"] . $file_name, 0644);
                            AuditAction($cfg["constants"]["file_upload"], $file_name);
                            // init stat-file
                            injectTorrent($file_name);
                            // file-prio
                            if ($cfg["enable_file_priority"]) {
                                include_once "setpriority.php";
                                // Process setPriority Request.
                                setPriority($file_name);
                            }
                            // queue
                            if ($cfg["AllowQueing"]) {
                                $_REQUEST['queue'] = 'on';
                            } else {
                                $_REQUEST['queue'] = 'off';
                            }
                            // start
                            include_once "ClientHandler.php";
                            $clientHandler = ClientHandler::getClientHandlerInstance($cfg);
                            $clientHandler->startTorrentClient($file_name, 0);
                            // just 2 secs..
                            sleep(2);
                            if ($clientHandler->status == 3) {
                                // hooray
                                echo " done\n";
                            } else {
                                // start failed
                                echo "\n ERROR : " . $clientHandler->messages . "\n";
                            }
                        } else {
                            echo "\n ERROR: File could not be found or could not be copied: " . $watchDir . $file . "\n";
                        }
                    }
                }
                closedir($dirHandle);
            }
        } else {
            echo "ERROR: " . $tpath . " is not a dir.\n";
            exit;
        }
    } else {
        printUsage();
    }
    exit;
}
Esempio n. 8
0
 /**
  * process a feed
  *
  * @param $sdir
  * @param $filter
  * @param $hist
  * @param $url
  * @return boolean
  */
 function instance_processFeed($sdir, $filter, $hist, $url)
 {
     // (re)set state
     $this->state = RSSD_STATE_NULL;
     // validate
     if (!checkDirectory($sdir, 0777)) {
         $this->state = RSSD_STATE_ERROR;
         $msg = "Save-Dir " . $sdir . " not valid.";
         array_push($this->messages, $msg);
         $this->_outputError($msg . "\n");
         return false;
     }
     if (!is_file($filter)) {
         $this->state = RSSD_STATE_ERROR;
         $msg = "Filter-File " . $filter . " not valid.";
         array_push($this->messages, $msg);
         $this->_outputError($msg . "\n");
         return false;
     }
     // output
     $this->_outputMessage("Processing feed " . $url . " ...\n");
     // set vars
     $this->_dirSave = checkDirPathString($sdir);
     $this->_fileFilters = $filter;
     $this->_fileHistory = $hist;
     $this->_urlRSS = $url;
     $this->_filters = array();
     $this->_history = array();
     $this->_historyNew = array();
     $this->_data = array();
     $this->_filesSaved = array();
     // load _filters
     if (!$this->_loadFilters()) {
         return false;
     }
     // load history
     if (!$this->_loadHistory()) {
         return false;
     }
     // load data
     if (!$this->_loadData()) {
         return false;
     }
     // something to do ?
     if ($this->_data['items_count'] <= 0) {
         // no
         // state
         $this->state = RSSD_STATE_OK;
         return true;
     }
     // process data
     if (!$this->_processData()) {
         return false;
     }
     // update history
     if (!$this->_updateHistory()) {
         return false;
     }
     // state
     $this->state = RSSD_STATE_OK;
     // output
     $this->_outputMessage("feed processed. downloaded and saved " . count($this->_filesSaved) . " torrents.\n");
     // return
     return true;
 }
     }
 }
 if (!empty($list_changes)) {
     $tmpl->setloop('list_changes', $list_changes);
 }
 if (empty($list_send)) {
     $doSend = false;
 } else {
     $tmpl->setloop('list_send', $list_send);
 }
 if (!empty($list_restart)) {
     $tmpl->setloop('list_restart', $list_restart);
 }
 // save settings
 if ($cfg["showdirtree"] == 1) {
     $newSavepath = checkDirPathString($settingsNew['savepath']);
     if ($newSavepath != $settingsCurrent['savepath']) {
         if (@checkDirectory($newSavepath, 0777) !== true) {
             $tmpl->setvar('error', "savepath " . $newSavepath . " not valid and change not saved.");
         } else {
             $ch->savepath = $newSavepath;
         }
     }
 }
 $ch->rate = $settingsNew['max_upload_rate'];
 $ch->drate = $settingsNew['max_download_rate'];
 $ch->maxuploads = $settingsNew['max_uploads'];
 $ch->superseeder = $settingsNew['superseeder'];
 $ch->runtime = $settingsNew['die_when_done'];
 $ch->sharekill = $settingsNew['sharekill'];
 $ch->minport = $settingsNew['minport'];
 /**
  * init start of a client.
  *
  * @param $interactive
  * @param $enqueue
  * @param $setPort
  * @param $recalcSharekill
  */
 function _init($interactive, $enqueue = false, $setPort = false, $recalcSharekill = false)
 {
     global $cfg;
     // request-vars / defaults / database
     if ($interactive) {
         // interactive, get vars from request vars
         $this->settingsInit();
     } else {
         // non-interactive, load settings from db
         $this->skip_hash_check = $cfg["skiphashcheck"];
         // load settings, default if settings could not be loaded (fresh transfer)
         if ($this->settingsLoad() !== true) {
             $this->settingsDefault();
         }
     }
     // queue
     if ($enqueue) {
         $this->queue = $cfg['isAdmin'] ? $enqueue : true;
     } else {
         $this->queue = false;
     }
     // savepath-check
     if (empty($this->savepath)) {
         $this->savepath = $cfg["enable_home_dirs"] != 0 ? $cfg['path'] . $this->owner . "/" : $cfg['path'] . $cfg["path_incoming"] . "/";
     } else {
         $this->savepath = checkDirPathString($this->savepath);
     }
     // check target-directory, create if not present
     if (!checkDirectory($this->savepath, 0777)) {
         $this->state = CLIENTHANDLER_STATE_ERROR;
         $msg = "Error checking savepath " . $this->savepath;
         array_push($this->messages, $msg);
         AuditAction($cfg["constants"]["error"], $msg);
         $this->logMessage($msg . "\n", true);
         // write error to stat
         $sf = new StatFile($this->transfer, $this->owner);
         $sf->time_left = 'Error';
         $sf->write();
         return false;
     }
     // umask
     $this->umask = $cfg["enable_umask"] != 0 ? " umask 0000;" : "";
     // nice
     $this->nice = $cfg["nice_adjust"] != 0 ? "nice -n " . $cfg["nice_adjust"] . " " : "";
     // set param for sharekill
     $this->sharekill = intval($this->sharekill);
     // recalc sharekill
     if ($recalcSharekill) {
         if ($this->_recalcSharekill() === false) {
             return false;
         }
     } else {
         $this->sharekill_param = $this->sharekill;
         $this->logMessage("setting sharekill-param to " . $this->sharekill_param . "\n", true);
     }
     // set port if start (only if not queue)
     if ($setPort && !$this->queue) {
         if ($this->_setClientPort() === false) {
             return false;
         }
     }
     // get current transfer
     $transferTotals = $this->getTransferCurrent($this->transfer);
     //XFER: before a transfer start/restart save upload/download xfer to SQL
     if ($cfg['enable_xfer'] == 1) {
         Xfer::save($this->owner, $transferTotals["downtotal"], $transferTotals["uptotal"]);
     }
     // update totals for this transfer
     $this->_updateTotals();
     // set state
     $this->state = CLIENTHANDLER_STATE_READY;
 }
 /**
  * updates jobs
  *
  * @param $content
  * @return boolean
  */
 function _jobsUpdate($content)
 {
     global $cfg;
     $jobsSane = array();
     $jobs = explode($this->_delimJobs, trim($content));
     if ($jobs !== false && count($jobs) > 0) {
         while (count($jobs) > 0) {
             $job = array_shift($jobs);
             $jobAry = explode($this->_delimJob, trim($job));
             $savedir = trim(array_shift($jobAry));
             $url = trim(array_shift($jobAry));
             $filtername = trim(array_shift($jobAry));
             if (strlen($savedir) > 0 && strlen($url) > 0 && strlen($filtername) > 0) {
                 array_push($jobsSane, array('savedir' => trim(checkDirPathString($savedir)), 'url' => $url, 'filtername' => $filtername));
             }
         }
         $jobsString = "";
         $resultCount = count($jobsSane);
         for ($i = 0; $i < $resultCount; $i++) {
             $jobsString .= $jobsSane[$i]["savedir"] . $this->_delimJob;
             $jobsString .= $jobsSane[$i]["url"] . $this->_delimJob;
             $jobsString .= $jobsSane[$i]["filtername"];
             if ($i < $resultCount - 1) {
                 $jobsString .= $this->_delimJobs;
             }
         }
         // update setting
         updateSetting("tf_settings", "fluxd_Rssad_jobs", $jobsString);
         // log
         AuditAction($cfg["constants"]["fluxd"], "Rssad Jobs Saved : \n" . $jobsString);
         return true;
     } else {
         return false;
     }
 }
 /**
  * prepares start of a bittorrent-client.
  * prepares vars and other generic stuff
  * @param $torrent name of the torrent
  * @param $interactive (1|0) : is this a interactive startup with dialog ?
  */
 function prepareStartTorrentClient($torrent, $interactive)
 {
     if ($this->state < 1) {
         $this->state = -1;
         $this->messages .= "Error. ClientHandler in wrong state on prepare-request.";
         return;
     }
     $this->skip_hash_check = "";
     if ($interactive == 1) {
         // interactive, get vars from request vars
         $this->rate = getRequestVar('rate');
         if (empty($this->rate)) {
             if ($this->rate != "0") {
                 $this->rate = $this->cfg["max_upload_rate"];
             }
         }
         $this->drate = getRequestVar('drate');
         if (empty($this->drate)) {
             if ($this->drate != "0") {
                 $this->drate = $this->cfg["max_download_rate"];
             }
         }
         $this->superseeder = getRequestVar('superseeder');
         if (empty($this->superseeder)) {
             $this->superseeder = "0";
         }
         // should be 0 in most cases
         $this->runtime = getRequestVar('runtime');
         if (empty($this->runtime)) {
             $this->runtime = $this->cfg["torrent_dies_when_done"];
         }
         $this->maxuploads = getRequestVar('maxuploads');
         if (empty($this->maxuploads)) {
             if ($this->maxuploads != "0") {
                 $this->maxuploads = $this->cfg["max_uploads"];
             }
         }
         $this->minport = getRequestVar('minport');
         if (empty($this->minport)) {
             $this->minport = $this->cfg["minport"];
         }
         $this->maxport = getRequestVar('maxport');
         if (empty($this->maxport)) {
             $this->maxport = $this->cfg["maxport"];
         }
         $this->maxcons = getRequestVar('maxcons');
         if (empty($this->maxcons)) {
             $this->maxcons = $this->cfg["maxcons"];
         }
         $this->rerequest = getRequestVar("rerequest");
         if (empty($this->rerequest)) {
             $this->rerequest = $this->cfg["rerequest_interval"];
         }
         $this->sharekill = getRequestVar('sharekill');
         if ($this->runtime == "True") {
             $this->sharekill = "-1";
         }
         if (empty($this->sharekill)) {
             if ($this->sharekill != "0") {
                 $this->sharekill = $this->cfg["sharekill"];
             }
         }
         $this->savepath = getRequestVar('savepath');
         $this->skip_hash_check = getRequestVar('skiphashcheck');
     } else {
         // non-interactive, load settings from db and set vars
         $this->rerequest = $this->cfg["rerequest_interval"];
         $this->skip_hash_check = $this->cfg["skiphashcheck"];
         $this->superseeder = 0;
         // load settings
         $settingsAry = loadTorrentSettings(urldecode($torrent));
         $this->rate = $settingsAry["max_upload_rate"];
         $this->drate = $settingsAry["max_download_rate"];
         $this->runtime = $settingsAry["torrent_dies_when_done"];
         $this->maxuploads = $settingsAry["max_uploads"];
         $this->minport = $settingsAry["minport"];
         $this->maxport = $settingsAry["maxport"];
         $this->maxcons = $settingsAry["maxcons"];
         $this->sharekill = $settingsAry["sharekill"];
         $this->savepath = $settingsAry["savepath"];
         // fallback-values if fresh-torrent is started non-interactive or
         // something else strange happened
         if ($this->rate == '') {
             $this->rate = $this->cfg["max_upload_rate"];
         }
         if ($this->drate == '') {
             $this->drate = $this->cfg["max_download_rate"];
         }
         if ($this->runtime == '') {
             $this->runtime = $this->cfg["torrent_dies_when_done"];
         }
         if ($this->maxuploads == '') {
             $this->maxuploads = $this->cfg["max_uploads"];
         }
         if ($this->minport == '') {
             $this->minport = $this->cfg["minport"];
         }
         if ($this->maxport == '') {
             $this->maxport = $this->cfg["maxport"];
         }
         if ($this->maxcons == '') {
             $this->maxcons = $this->cfg["maxcons"];
         }
         if ($this->sharekill == '') {
             $this->sharekill = $this->cfg["sharekill"];
         }
     }
     // queue
     if ($this->cfg["AllowQueing"]) {
         if (IsAdmin()) {
             $this->queue = getRequestVar('queue');
             if ($this->queue == 'on') {
                 $this->queue = "1";
             } else {
                 $this->queue = "0";
             }
         } else {
             $this->queue = "1";
         }
     } else {
         $this->queue = "0";
     }
     //
     $this->torrent = urldecode($torrent);
     $this->alias = getAliasName($this->torrent);
     $this->owner = getOwner($this->torrent);
     if (empty($this->savepath)) {
         $this->savepath = $this->cfg['path'] . $this->owner . "/";
     }
     // ensure path has trailing slash
     $this->savepath = checkDirPathString($this->savepath);
     // check target-directory, create if not present
     if (!checkDirectory($this->savepath, 0777)) {
         AuditAction($this->cfg["constants"]["error"], "Error checking " . $this->savepath . ".");
         $this->state = -1;
         $this->messages .= "Error. TorrentFlux settings are not correct (path-setting).";
         global $argv;
         if (isset($argv)) {
             die($this->messages);
         } else {
             if (IsAdmin()) {
                 @header("location: admin.php?op=configSettings");
                 exit;
             } else {
                 $this->messages .= " please contact an admin.";
                 showErrorPage($this->messages);
             }
         }
     }
     // create AliasFile object and write out the stat file
     include_once "AliasFile.php";
     $this->af = AliasFile::getAliasFileInstance($this->cfg["torrent_file_path"] . $this->alias . ".stat", $this->owner, $this->cfg, $this->handlerName);
     // set param for sharekill
     $this->sharekill = intval($this->sharekill);
     if ($this->sharekill == 0) {
         // nice, we seed forever
         $this->sharekill_param = 0;
     } elseif ($this->sharekill > 0) {
         // recalc sharekill
         // sanity-check. catch "data-size = 0".
         $transferSize = intval($this->af->size);
         if ($transferSize > 0) {
             $totalAry = getTorrentTotals($this->torrent);
             $upTotal = $totalAry["uptotal"] + 0;
             $downTotal = $totalAry["downtotal"] + 0;
             $upWanted = $this->sharekill / 100 * $transferSize;
             $sharePercentage = $upTotal / $transferSize * 100;
             if ($upTotal >= $upWanted && $downTotal >= $transferSize) {
                 // we already have seeded at least wanted percentage.
                 // skip start of client
                 // set state
                 $this->state = 1;
                 // message
                 $this->messages = "skipping start of transfer " . $this->torrent . " due to share-ratio (has: " . @number_format($sharePercentage, 2) . " ; set:" . $this->sharekill . ")";
                 // DEBUG : log the messages
                 AuditAction($this->cfg["constants"]["debug"], $this->messages);
                 // return
                 return;
             } else {
                 // not done seeding wanted percentage
                 $this->sharekill_param = intval(ceil($this->sharekill - $sharePercentage));
                 // sanity-check.
                 if ($this->sharekill_param < 1) {
                     $this->sharekill_param = 1;
                 }
             }
         } else {
             $this->messages = "data-size is 0 when recalcing share-kill for " . $this->torrent . ". setting sharekill absolute to " . $this->sharekill;
             AuditAction($this->cfg["constants"]["error"], $this->messages);
             $this->sharekill_param = $this->sharekill;
         }
     } else {
         $this->sharekill_param = $this->sharekill;
     }
     // set port if start (not queue)
     if (!($this->cfg["AllowQueing"] && $this->queue == "1")) {
         if ($this->setClientPort() === false) {
             return;
         }
     }
     //XFER: before a torrent start/restart save upload/download xfer to SQL
     $torrentTotals = getTorrentTotalsCurrent($this->torrent);
     saveXfer($this->owner, $torrentTotals["downtotal"] + 0, $torrentTotals["uptotal"] + 0);
     // update totals for this torrent
     updateTorrentTotals($this->torrent);
     // write stat-file
     if ($this->cfg["AllowQueing"] && $this->queue == "1") {
         $this->af->QueueTorrentFile();
     } else {
         $this->af->StartTorrentFile();
     }
     // set state
     $this->state = 2;
 }