コード例 #1
0
ファイル: stack.php プロジェクト: TheFox/phpdl
function main()
{
    global $CONFIG, $SCHEDULER_LAST;
    print "\n";
    printd("start\n");
    printd("cwd: " . getcwd() . "\n");
    $date = date('Ymd');
    fileWrite($CONFIG['PHPDL_STACK_PIDFILE'], posix_getpid());
    $n = 0;
    while (!$SHUTDOWN) {
        $n++;
        $dbh = dbConnect();
        $scheduler = scheduler($dbh);
        $filesDownloading = filesDownloading($dbh);
        if ($SCHEDULER_LAST != $scheduler) {
            plog("scheduler '{$scheduler}' matched\n");
            $SCHEDULER_LAST = $scheduler;
        }
        $res = mysql_query("select id from packets where archive = '0' and active = '1' and ftime = '0' order by sortnr, id;", $dbh);
        if (mysql_num_rows($res)) {
            while ($row = mysql_fetch_assoc($res)) {
                $packet = new dlpacket($CONFIG['DB_HOST'], $CONFIG['DB_NAME'], $CONFIG['DB_USER'], $CONFIG['DB_PASS']);
                if ($packet->loadById($row['id'])) {
                    $packetDirBn = getPacketFilename($packet->get('id'), $packet->get('name'));
                    $packetDownloadDir = 'downloads/loading/' . $packetDirBn;
                    $packetFinishedDir = 'downloads/finished/' . $packetDirBn;
                    if ($packet->loadFiles()) {
                        if ($packet->filesUnfinished()) {
                            if ($scheduler > 0) {
                                if ($filesDownloading < $CONFIG['DL_SLOTS']) {
                                    if (!$packet->get('stime')) {
                                        $packet->save('stime', mktime());
                                    }
                                    if ($nextfile = $packet->getFileNextUnfinished()) {
                                        while ($nextfile->get('error')) {
                                            printd("nextfile " . $nextfile->get('id') . "\n");
                                            $nextfile = $packet->getFileNextUnfinished();
                                            if (!$nextfile) {
                                                break;
                                            }
                                            sleep(1);
                                        }
                                        if ($nextfile) {
                                            printd("packet " . $packet->get('id') . ": download " . $nextfile->get('id') . "\n");
                                            $sh = 'php wget.php ' . $nextfile->get('id') . ' 1>> log/wget.' . $date . '.log 2>> log/wget.' . $date . '.log &';
                                            printd("exec '{$sh}'\n");
                                            system($sh);
                                            sleep(1);
                                            break;
                                        }
                                    }
                                } else {
                                    plog("no free download slots (" . $CONFIG['DL_SLOTS'] . ")\n");
                                }
                            }
                        } else {
                            printd("packet " . $packet->get('id') . ": all files finished\n");
                            if (!$packet->get('stime')) {
                                $packet->set('stime', mktime());
                            }
                            $packet->save('ftime', mktime());
                            $packet->md5Verify();
                            $packet->sizeVerify();
                            if (!$packet->fileErrors()) {
                                if (file_exists($packetFinishedDir)) {
                                    $files = scandir($packetDownloadDir);
                                    foreach ($files as $file) {
                                        if ($file != '.' && $file != '..' && file_exists($packetDownloadDir . '/' . $file)) {
                                            rename($packetDownloadDir . '/' . $file, $packetFinishedDir . '/' . $file);
                                        }
                                    }
                                    reset($files);
                                    rmdir($packetDownloadDir);
                                } else {
                                    rename($packetDownloadDir, $packetFinishedDir);
                                }
                            }
                        }
                    }
                }
                unset($packet);
            }
        } else {
            plog("no active download\n");
        }
        dbClose($dbh);
        $SHUTDOWN = shutdownCheck();
        sleep(5);
    }
    shutdown();
}
コード例 #2
0
ファイル: index.php プロジェクト: TheFox/phpdl
         $packet->set('archive', 0);
         $packet->set('md5Verified', 0);
         $packet->set('sizeVerified', 0);
         $packet->set('stime', 0);
         $packet->set('ftime', 0);
         $packet->save();
         $dbh = dbConnect();
         mysql_query("update files set pid = '0', md5Verified = '0', error = '0', stime = '0', ftime = '0' where _packet = '{$id}';", $dbh);
         dbClose($dbh);
     }
     if (!$noredirect) {
         header('Location: ?');
     }
     break;
 case 'packetFilesErrorResetExec':
     $packet = new dlpacket($CONFIG['DB_HOST'], $CONFIG['DB_NAME'], $CONFIG['DB_USER'], $CONFIG['DB_PASS']);
     if ($packet->loadById($id)) {
         if ($user->get('id') == $packet->get('_user')) {
             if ($packet->loadFiles()) {
                 foreach ($packet->files as $fileId => $file) {
                     if ($file->get('error')) {
                         $file->set('error', $DLFILE_ERROR['ERROR_NO_ERROR']);
                         $file->set('pid', 0);
                         $file->set('md5Verified', 0);
                         $file->set('stime', 0);
                         $file->set('ftime', 0);
                         $file->save();
                     }
                 }
             }
             $packet->set('archive', 0);