#!/usr/local/bin/php
<?
# Slashster: Open Source Friend of a Friend Software
# Copyright 2004 Mark El-Wakil
# Contact: hide@address.com
# http://seventhcycle.net
#
# This file is part of Slashster.
#
# Slashster is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Slashster is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Slashster; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
set_time_limit(0);
require("cronpth.php");
$mpage = new PageClass("sqlonly");
$x = 0;
$fdir = $mpage->sys[siteroot] . "cron";
$pid = posix_getpid();
$fd = fopen("$fdir/schedule2.pid", "w");
fwrite($fd, $pid);
fclose($fd);
while(1)
{
echo "$x: ";
$sleeptime = 0;
$sql = "select * from UserTable where Nupdated IS NULL";
$r = $mpage->DoQuery($sql,1);
if (!sizeof($r)) {
$sql = "select * from UserTable where UpdNow = 1";
$r = $mpage->DoQuery($sql,1);
}
if (!sizeof($r)) {
$sql = "select * from UserTable where (LastSeen > Nupdated) AND date_sub(LastSeen, interval 15 minute) > Nupdated
order by Nupdated asc";
$r = $mpage->DoQuery($sql,1);
}
if (!sizeof($r)) {
$sql = "select * from UserTable where (LastSeen < Nupdated) order by Nupdated asc limit 20";
$r = $mpage->DoQuery($sql,1);
$sleeptime = 5;
}
echo "\n\n$sql\n\n";
$t1 = $mpage->getmicrotime();
foreach ($r as $s)
{
Cfile($s[Id]);
}
$t2 = $mpage->getmicrotime();
$t3 = ($t2 - $t1);
$slp = floor(max($sleeptime - $t3, 0));
$x++;
sleep($slp);
}
function Cfile($id)
{
global $mpage;
$tarr = array($id);
$arr = array();
$arr[1] = array();
$arr[2] = array();
$arr[3] = array();
$arr[4] = array();
for ($y = 1; $y <= 4; $y++)
{
$sql = "select distinct End from FriendTable where Start IN (".join(',', $tarr).") AND
End NOT IN (".join(',', $tarr).") ";
$rt = $mpage->DoQuery($sql, 1);
for ($x = 0; $x < sizeof($rt); $x++)
{
if ($y != 4) array_push($tarr, $rt[$x][End]);
array_push($arr[$y], $rt[$x][End]);
}
}
$fp = fopen($mpage->sys[siteroot] . "friendlist/$id.txt", "w");
if (flock($fp, LOCK_EX))
{
for ($x = 1; $x <= 4; $x++)
{
fwrite($fp, join(",",$arr[$x]) ."\r\n");
}
flock($fp, LOCK_UN); // release the lock
}
else
{
echo "Couldn't lock the file $id!";
exit;
}
fclose($fp);
$sql = "update UserTable set Nupdated = NOW(), UpdNow=0 where Id = \"$id\" ";
$mpage->DoQuery($sql);
}
?>