Ejemplo n.º 1
0
function search_updatelist($options = '')
{
    global $win32, $setctl, $bd, $runinit;
    if ($runinit['astream']) {
        if (!updaterunning()) {
            $setctl->set('updatemid', db_thread_id());
        } else {
            errormessage('Another update is running. Please try again later.', true);
        }
    }
    kprintheader(get_lang(17));
    $updateall = false;
    @ini_set('output_buffering', '1');
    if (isset($options['deleteunused'])) {
        $deleteunused = 1;
    } else {
        $deleteunused = 0;
    }
    if (isset($options['debugmode'])) {
        $debugmode = 1;
    } else {
        $debugmode = 0;
    }
    if (isset($options['sleeppertrans'])) {
        $sleeptrans = $options['sleeppertrans'];
    } else {
        $sleeptrans = 0;
    }
    if (isset($options['rebuildid3'])) {
        $updateall = true;
    }
    $setctl->publish('updusecache');
    $setctl->publish('followsymlinks');
    $db_out = $db_mtime = $db_unique = $db_path = array();
    $filecntr = 0;
    $file = '';
    $fixurl = 'http://www.kplaylist.net/forum/viewtopic.php?p=3672';
    echo '<font class="notice">' . get_lang(296, '<a href="' . $fixurl . '" target="_blank">' . $fixurl . '</a>') . '</font><br/><br/>';
    echo '<font class="notice">' . get_lang(136) . '..</font><br/>';
    flush();
    $data = array();
    $datacnt = 0;
    if ($debugmode) {
        @ini_set('display_errors', 'On');
        echo '<!-- update debug step 0 - mem limit detected: ' . @ini_get('memory_limit') . ' -->';
    }
    $kqm = new kq_Measure();
    $kqm->start();
    echo '<br/><div id="up_status2" class="notice"></div>';
    echo '<br/><div id="up_status" class="notice"></div><br/>';
    $cnt = 0;
    for ($i = 0, $c = $bd->getcnt(); $i < $c; $i++) {
        if ($bd->gtype($i) == 'l') {
            $grabdata = array();
            if ($debugmode) {
                echo '<!-- update debug step 1 - grabbing filelist from ' . $bd->getpath($i) . ' -->';
            }
            $stripc = strlen($bd->getpath($i));
            GetDirArray($bd->getpath($i), $grabdata, $cnt, $stripc);
            $data[$i] = $grabdata;
            $datacnt += count($data[$i]);
        }
    }
    if ($debugmode) {
        echo '<!-- update debug step 2 -->';
    }
    if ($datacnt > 0) {
        $result = db_execquery('SELECT count(*) FROM ' . TBL_SEARCH . ' WHERE xid = 0');
        $row = db_fetch_row($result);
        $dbrows = $row[0];
        $result = db_execquery('SELECT fsize, id, md5, fname, drive, mtime, fpath FROM ' . TBL_SEARCH . ' WHERE xid = 0 ORDER BY id ASC', true);
        $dcntr = 0;
        updateup_status(get_lang(314, $dcntr, $dbrows));
        while ($row = db_fetch_row($result)) {
            if (UPDUSECACHE) {
                $db_out[$dcntr] = $row;
            } else {
                $db_out[$dcntr] = array($row[0], $row[1]);
            }
            if ($dcntr % 50 == 0) {
                updateup_status(get_lang(314, $dcntr, $dbrows));
            }
            if (!isset($db_mtime[$row[0]][$row[5]])) {
                $db_mtime[$row[0]][$row[5]] = $dcntr;
            }
            if (!isset($db_unique[$row[0]][$row[2]])) {
                $db_unique[$row[0]][$row[2]] = $dcntr;
            }
            if (strlen($row[6]) != 0 || strlen($row[3]) != 0) {
                $path = crc32($row[6] . $row[3]);
                if (isset($db_path[$path])) {
                    $ids = $db_path[$path];
                    $ids[] = $dcntr;
                    $db_path[$path] = $ids;
                } else {
                    $db_path[$path] = array($dcntr);
                }
            }
            $dcntr++;
        }
        updateup_status(get_lang(314, $dcntr, $dbrows), 'up_status');
        db_free($result);
        if ($debugmode) {
            echo '<!-- update debug step 3 -->';
        }
        if ($updateall) {
            $db_mtime = array();
        }
        $query = null;
        updateup_status(get_lang(18, $datacnt));
        $totalqupds = $dcntr;
        $totalins = $datacnt;
        $qins = $qupd = $failed = $qupdins = $skips = $qdels = $fdups = 0;
        $rowinsertid = -1;
        if ($datacnt > 0) {
            for ($drive = 0, $drivec = $bd->getcnt(); $drive < $drivec; $drive++) {
                if ($bd->gtype($drive) == 'l') {
                    for ($i = 0, $ic = count($data[$drive]); $i < $ic; $i++) {
                        $filein = $data[$drive][$i];
                        $file = $bd->getpath($drive) . $filein;
                        if ($i % 50 == 0 || $debugmode) {
                            $countups = $qupd + $qupdins;
                            $out = get_lang(20, $qins, $countups);
                            $out .= kp_strlen($filein) > 60 ? addslashes(kp_substr($filein, 0, 60)) . '...' : addslashes($filein);
                            updateup_status($out, 'up_status');
                        }
                        $fsize = filesize($file);
                        $mtime = filemtime($file);
                        if (!$fsize) {
                            echo '<font class="notice">' . get_lang(19, $file) . '</font><br/>';
                            flush();
                            $skips++;
                            continue;
                        }
                        $filecntr++;
                        if (isset($db_mtime[$fsize][$mtime])) {
                            $i2 = $db_mtime[$fsize][$mtime];
                            if (UPDUSECACHE) {
                                $userow = $db_out[$i2];
                            } else {
                                $userow = getsrow($db_out[$i2][1]);
                            }
                            if ($db_out[$i2][0] != -1 && $userow[6] . $userow[3] == $filein && $userow[4] == $drive) {
                                $db_out[$i2][0] = -1;
                                $qupd++;
                                continue;
                            }
                        }
                        $md5 = md5file($file);
                        if ($sleeptrans > 0 && !$win32) {
                            usleep($sleeptrans);
                        }
                        if (!empty($md5)) {
                            if (isset($db_unique[$fsize][$md5])) {
                                $idupdate = $db_unique[$fsize][$md5];
                                if ($db_out[$idupdate][0] == -1) {
                                    if (UPDUSECACHE) {
                                        $userow = $db_out[$idupdate];
                                    } else {
                                        $userow = getsrow($db_out[$idupdate][1]);
                                    }
                                    $checkf = $bd->getpath($userow[4]) . $userow[6] . $userow[3];
                                    /* duplicate check - not in production - uncomment to use. But, do not use if you do not understand what it does.
                                    									$identical = false;
                                    									$fp1 = fopen($checkf, 'rb');
                                    									$fp2 = fopen($file, 'rb');
                                    									if ($fp1 && $fp2)
                                    									{
                                    										$identical = true;
                                    										while (!feof($fp1))
                                    										{
                                    											$data1 = fread($fp1, 32768);
                                    											$data2 = fread($fp2, 32768);
                                    											if (strcmp($data1,$data2) != 0) 
                                    											{
                                    												$identical = false;
                                    												break;
                                    											}
                                    										}									
                                    										fclose($fp1);
                                    										fclose($fp2);
                                    									}
                                    
                                    									if ($identical) $extra = '! '; else $extra = '';*/
                                    echo '<font class="notice">' . get_lang(168, $file, $checkf) . '</font><br/><br/>';
                                    $fdups++;
                                    continue;
                                }
                            } else {
                                $idupdate = -1;
                            }
                            if ($idupdate != -1) {
                                $db_out[$idupdate][0] = -1;
                                if (UPDUSECACHE) {
                                    $userow = $db_out[$idupdate];
                                } else {
                                    $userow = getsrow($db_out[$idupdate][1]);
                                }
                                if ($updateall) {
                                    $userow[5] = 0;
                                }
                                if ($mtime != $userow[5]) {
                                    $fid = get_file_info($file);
                                    $query = search_qupdorins($db_out[$idupdate][1], $fid, $filein, $md5, $drive, $mtime, 0, $fsize, time());
                                    $qupdins++;
                                } else {
                                    if ($userow[6] . $userow[3] != $filein || $userow[4] != $drive) {
                                        $query = search_qupdfree($filein, $drive, $db_out[$idupdate][1]);
                                        $qupdins++;
                                    }
                                }
                            } else {
                                $frel = getrelative($filein);
                                $ffilein = kp_basename($filein);
                                $checkex = crc32($frel . $ffilein);
                                $useid = -1;
                                if (isset($db_path[$checkex])) {
                                    $ids = $db_path[$checkex];
                                    for ($i3 = 0, $c3 = count($ids); $i3 < $c3; $i3++) {
                                        $cid = $ids[$i3];
                                        if (UPDUSECACHE) {
                                            $userow = $db_out[$cid];
                                        } else {
                                            $userow = getsrow($db_out[$cid][1]);
                                        }
                                        if ($userow[3] == $ffilein && $userow[6] == $frel) {
                                            $useid = $cid;
                                            break;
                                        }
                                    }
                                }
                                $fid = get_file_info($file);
                                if ($useid == -1) {
                                    $query = search_qupdorins(0, $fid, $filein, $md5, $drive, $mtime, 0, $fsize, time());
                                    if (UPDUSECACHE) {
                                        $db_out[$dcntr] = array(-1, 0, $md5, kp_basename($filein), $drive, $mtime, getrelative($filein));
                                    } else {
                                        $db_out[$dcntr] = array(-1, 0);
                                    }
                                    $db_unique[$fsize][$md5] = $dcntr;
                                    $rowinsertid = $dcntr;
                                    $dcntr++;
                                    $qins++;
                                } else {
                                    $query = search_qupdorins($db_out[$useid][1], $fid, $filein, $md5, $drive, $mtime, 0, $fsize, time());
                                    $db_out[$useid][0] = -1;
                                    $qupdins++;
                                }
                            }
                            if ($query !== null) {
                                $result = db_execquery($query, true);
                                if (!$result) {
                                    $failed++;
                                    echo '<font class="wtext">' . get_lang(22, $query) . '</font><br/>';
                                } else {
                                    if ($rowinsertid != -1) {
                                        $db_out[$rowinsertid][1] = db_insert_id();
                                        $rowinsertid = -1;
                                    }
                                }
                                $query = null;
                            }
                        } else {
                            echo '<font class="notice">' . get_lang(23, $file) . '</font><br/>';
                            flush();
                            $skips++;
                        }
                    }
                    // end of file loop
                }
                // if local drive
            }
            // end of drive loop
        }
        // if found any files
        $fordel = 0;
        for ($i2 = 0; $i2 < $dcntr; $i2++) {
            if ($db_out[$i2][0] != -1) {
                $fordel++;
            }
        }
        if ($deleteunused) {
            if ($skips == 0) {
                for ($i2 = 0; $i2 < $dcntr; $i2++) {
                    if ($db_out[$i2][0] != -1) {
                        if (UPDUSECACHE) {
                            $userow = $db_out[$i2];
                        } else {
                            $userow = getsrow($db_out[$i2][1]);
                        }
                        echo '<font class="notice">' . get_lang(24, $userow[6] . $userow[3]);
                        $result = db_execquery('DELETE FROM ' . TBL_SEARCH . ' WHERE id = ' . $db_out[$i2][1], true);
                        if ($result) {
                            $qdels++;
                        }
                        echo '</font><br/>';
                        $fordel = 0;
                    }
                }
                echo '<br/>';
            } else {
                if ($fordel > 0) {
                    echo '<font class="notice">' . get_lang(335) . '</font><br/>';
                }
            }
        } else {
            $one = false;
            for ($i2 = 0; $i2 < $dcntr; $i2++) {
                if ($db_out[$i2][0] != -1) {
                    $one = true;
                    if (UPDUSECACHE) {
                        $userow = $db_out[$i2];
                    } else {
                        $userow = getsrow($db_out[$i2][1]);
                    }
                    echo '<font class="notice">' . get_lang(315, $userow[6] . $userow[3]) . '</font><br/>';
                }
            }
            if ($one) {
                echo '<br/>';
            }
        }
        $kqm->stop();
        updateup_status(get_lang(26), 'up_status');
        echo '<font class="notice">' . get_lang(25, $qins, $qupdins, $qdels, $failed, $skips, $filecntr, $kqm->result(3), $fordel);
        echo '</font><br/><br/>';
    } else {
        for ($i = 0, $c = $bd->getcnt(); $i < $c; $i++) {
            if ($bd->gtype($i) == 'l') {
                echo '<font class="notice">' . get_lang(28, $bd->getpath($i)) . '</font><br/>';
            }
        }
    }
    // network update && clean up
    for ($i = 0, $c = $bd->getcnt(); $i < $c; $i++) {
        if ($bd->gtype($i) == 'n') {
            updateup_status(get_lang(253));
            updateup_status('', 'up_status');
            $kpn = new kpnetwork();
            if ($kpn->setdrive($i)) {
                if ($kpn->checklogin()) {
                    $cnt = $kpn->preparesync($i);
                    $host = $kpn->getnetworkhost();
                    if ($cnt > 0) {
                        updateup_status(get_lang(347, $host->geturl(), $cnt));
                        if ($kpn->genchlist($updateall, $deleteunused) > 0) {
                            $kpn->dosync();
                        }
                        updateup_status(get_lang(348, $host->geturl(), get_lang(181)), 'up_status');
                    } else {
                        if ($cnt == -1) {
                            updateup_status(get_lang(348, $host->geturl(), $kpn->geterrorstr()), 'up_status');
                        }
                    }
                } else {
                    updateup_status(get_lang(348, $host->geturl(), $kpn->geterrorstr()), 'up_status');
                }
            }
        } else {
            if ($bd->gtype($i) == 'l') {
                if ($deleteunused) {
                    $res = db_execquery('SELECT count(*) as cnt FROM ' . TBL_SEARCH . ' WHERE xid != 0 AND drive = ' . $i);
                    $row = db_fetch_assoc($res);
                    if ($row['cnt'] > 0) {
                        $res = db_execquery('DELETE FROM ' . TBL_SEARCH . ' WHERE xid != 0 AND drive = ' . $i);
                    }
                }
            }
        }
    }
    echo '<input type="button" value="' . get_lang(27) . '" name="close" class="fatbuttom" onclick="javascript: self.close();"/><br/><br/>';
    if ($runinit['astream']) {
        $setctl->set('updatemid', 0);
    }
    $setctl->set('basedir_changed', 0);
    cache_updateall();
    kprintend();
}