<?
// Wapmess, originally developed by Gцran (depeh) Johansson, Sweden
//
// Feel free to improve this program in any way you can, but please share your version!
// More info and the latest version is avaiable at:
// http://sourceforge.net/projects/wapmess/
require("icqlib.php");
wml_header();
no_cache_header();
xml_header();
// The current user ICQ user-id
$my_uin=$_REQUEST["u"];
// The current user ICQ password
$my_pwd=$_REQUEST["p"];
// The receiver(ICQ Uin) of the message to be sent
$uin=$_REQUEST["uin"];
// The message that should be sent to the icq-server
$msg=$_REQUEST["msg"];
// m is 1 if the user wants to write a new Message
$m=$_REQUEST["m"];
// c is the counter of how many times the wap-client has been refreshing
$c=$_REQUEST["c"];
$nocache_no=rand(1000, 9999);
$mainurl=$wwwname."?u=".$my_uin."&p=".$my_pwd."&".$nocache_no;
$indexurl=$wwwpath."/index.php?u=".$my_uin."&p=".$my_pwd."&".$nocache_no;
# Koppla upp mot databasservern
$db = mysql_connect($db_server, $db_user, $db_pass);
# Vдlj DATABAS
mysql_select_db($db_name);
$query="SELECT lifesign FROM alive WHERE uin='".$my_uin."'";
$sqlres=mysql_query($query) or die("get_alive Invalid query: " . mysql_error());
$num_rows = mysql_num_rows($sqlres);
if ($num_rows==1)
{
$row = mysql_fetch_array($sqlres);
$lifesign=$row["lifesign"];
}
$query="SELECT statusstr FROM uinstatus WHERE uin='".$my_uin."'";
$sqlres=mysql_query($query) or die("get_unistatus Invalid query: " . mysql_error());
$num_rows = mysql_num_rows($sqlres);
if ($num_rows==1)
{
$row = mysql_fetch_array($sqlres);
$status_str=$row["statusstr"];
$status_split=split(",",$status_str);
for ($x=0;$x<count($status_split);$x++)
{
$statusrow_split=split("=",$status_split[$x],2);
$uin_status[$statusrow_split[0]]=$statusrow_split[1];
}
}
$client_refresh_time=100; // 10 sek
if ($c>6)
{
$client_refresh_time=200; // 20 sek
}
if ($c>10)
{
$client_refresh_time=300; // 30 sek
}
if ($c>12)
{
$client_refresh_time=600; // 1 min
}
if ($c>14)
{
$client_refresh_time=1200; // 2 min
}
if ($c>16)
{
$client_refresh_time=3000; // 5 min
}
if ($c>18)
{
$client_refresh_time=6000; // 10 min
}
if ($c>20)
{
$client_refresh_time=12000; // 20 min
}
$query="SELECT cnick,cuin FROM contactlist WHERE uin='".$my_uin."' ORDER BY cnick";
$sqlres=mysql_query($query) or die("contactlist Invalid query: " . mysql_error());
$num_rows = mysql_num_rows($sqlres);
if ($num_rows<1)
{
?>
<wml>
<card id="kort1" title="<?=$app_name?>">
<p align="center">
<b>No contacts in list!</b>
</p>
</card>
</wml>
<?
exit;
}
else
{
$status_list="<b>Online</b><br/>";
$wml_list="<select name=\"mott\">";
while ($row = mysql_fetch_array($sqlres))
{
$status_symbol="?";
if ($uin_status[$row["cuin"]]=="")
{
$status_symbol="?";
}
else if ($uin_status[$row["cuin"]]=="1")
{
$status_symbol=":)";
$status_list.=$row["cnick"]."<br/>";
}
else if ($uin_status[$row["cuin"]]=="0")
{
$status_symbol=":(";
}
$wml_list.="<option value=\"".$row["cuin"]."\">".$row["cnick"]." ".$status_symbol."</option>\n";
}
$wml_list.="</select>";
}
$now=time();
$nowtime=date("H:i:s");
if ($lifesign<$now-$alive_limit_seconds)
{
$server_alive=0;
$life_text=":(";
}
else
{
$server_alive=1;
$life_text=":)";
}
// Kolla om meddelande ska skickas
if ($msg && $uin)
{
// Lдgg till meddelandet i outgoing tabellen
$when=date("Y-m-d H:i");
$query = "INSERT INTO outgoing ( `id` , `fromuin` , `touin` , `msg` , `msgtime` , `seen` ) VALUES (0,'".$my_uin."','".$uin."','".smart_utf8_decode($msg)."','".$when."',0)";
if ($debug)print "sql=$query\n";
$sqlres=mysql_query($query) or die("<wml><card id=\"card1\"><p>send_alive Invalid query: ". mysql_error()."</p></card></wml>");
}
$show_timer=1;
if ($m==1)
{
?>
<wml>
<card id="card1" title="New Msg">
<p><a href="<?=amp($mainurl);?>">Back</a>
<br/>
<?=$wml_list?>
<input name="reply" type="text" emptyok="true"/>
<anchor>Send Msg
<go method="post" href="<?=amp($mainurl)?>">
<postfield name="uin" value="$(mott)"/>
<postfield name="msg" value="$(reply)"/>
</go>
</anchor>
</p>
</card>
</wml>
<?
exit;
}
if ($server_alive==1)
{
$mess_uin="";
$query = "SELECT DISTINCT incoming.fromuin from incoming,contactlist WHERE incoming.touin = contactlist.uin AND incoming.touin=\"".$my_uin."\" AND contactlist.cuin=incoming.fromuin AND incoming.seen=0 ORDER BY incoming.fromuin LIMIT 1";
if ($debug)print "sql=$query\n";
$sqlres=mysql_query($query) or die("<wml><card id=\"card1\"><p>send_alive Invalid query: ". mysql_error()."</p></card></wml>");
$rescount = mysql_num_rows($sqlres);
if ($rescount==1)
{
$row = mysql_fetch_array($sqlres);
$mess_uin=$row["fromuin"];
}
// Sцk ut de meddelanden frеn anvдndare som finns pе min anvдndarlista
$query = "SELECT incoming.id,incoming.fromuin,incoming.touin,incoming.msg,incoming.msgtime,contactlist.uin,contactlist.cnick,contactlist.cuin from incoming,contactlist WHERE incoming.touin = contactlist.uin AND incoming.touin=\"".$my_uin."\" AND contactlist.cuin=incoming.fromuin AND incoming.seen=0 AND incoming.fromuin=\"".$mess_uin."\" ORDER BY incoming.msgtime";
if ($debug)print "sql=$query\n";
$sqlres=mysql_query($query) or die("<wml><card id=\"card1\"><p>send_alive Invalid query: ". mysql_error()."</p></card></wml>");
$msg_count = mysql_num_rows($sqlres);
if ($msg_count>0)
{
$show_timer=0;
}
//$show_timer=$msg_count;
$mess_nick=array();
$mess_text=array();
while ($row = mysql_fetch_array($sqlres))
{
$msg_timestamp=strtotime($row["msgtime"]);
$start_of_day=strtotime(date("Y-m-d 00:00",time()));
$a_week_ago=$start_of_day-(60*60*24*7);
#print $msg_timestamp."\n";
#print $start_of_day."\n";
#print $a_week_ago."\n";
if ($msg_timestamp>$start_of_day)
{
$msg_date = date("H:i",$msg_timestamp);
}
else if ($msg_timestamp>$a_week_ago)
{
$msg_date = substr(date("l",$msg_timestamp),0,3)." ".date("H:i",$msg_timestamp);
}
else
{
$msg_date = date("ymd H:i");
}
$mess_text[$row["cuin"]].="<small>".$msg_date."</small> ".to_wml($row["msg"])."<br/>\n";
$mess_nick[$row["cuin"]]=$row["cnick"];
$mess_id[$row["cuin"]].=$row["id"].",";
$mess_count[$row["cuin"]]++;
$query = "UPDATE incoming SET incoming.seen = 1 WHERE incoming.id = ".$row["id"];
#if ($debug)print "sql=$query\n";
$sqlres2=mysql_query($query) or die("<wml><card id=\"card1\"><p>update incoming Invalid query: ". mysql_error()."</p></card></wml>");
}
// Rita ut MAIN-sidan!
?>
<wml>
<? if (count($mess_text)!=1)
{
?>
<card <?
if ($show_timer)
{
$c++;
print "ontimer=\"".amp($mainurl."&c=".$c)."\" ";
}
?>id="card1" title="<?=$app_name?>">
<?
if ($show_timer)
{
print "<timer value=\"$client_refresh_time\"/>";
}
?>
<?
if (count($mess_text)==0)
{
?>
<p><a href="<?=amp($mainurl."&m=1");?>">New msg</a></p>
<?
}
?>
</card>
<?
}
?>
<?
$card_num=2;
//for ($i=0;$i<count($mess_nick);$i++)
foreach ($mess_text as $key => $value)
{
$mess_id[$key]=substr($mess_id[$key], 0, -1);
?>
<card id="card<?=$card_num?>" title="<?=$mess_nick[$key];?>">
<p><a href="<?=amp($mainurl);?>">Back</a>
<br/>
<?=$mess_text[$key]?>
<input name="reply<?=$card_num?>" type="text" emptyok="true"/>
<anchor>Send Reply
<go method="post" href="<?=amp($mainurl);?>">
<postfield name="uin" value="<?=$key?>"/>
<postfield name="msg" value="$(reply<?=$card_num?>)"/>
</go>
</anchor>
</p>
</card>
<?
$card_num++;
}
?>
</wml>
<?
}
else
{
// Om man har skickat ett meddelande och blivit utloggad ur servern(SERVER DEAD) under tiden
// ska givetvis meddelandet skickas ivдg, loggas man in.
if ($msg && $uin)
{
?>
<wml>
<card id="kort1" ontimer="<?=amp($indexurl);?>" title="Reconnecting">
<timer value="1" />
<p align="center">
<b>Reconnecting...</b>
</p>
</card>
</wml>
<?
exit;
}
else
{
?>
<wml>
<card id="kort1" title="<?=$app_name?>">
<p align="center">
<b>Timeout from Server!</b>
</p>
<p align="center">
<a href="<?=amp($indexurl);?>">Reconnect!</a>
</p>
</card>
</wml>
<?
}
}
?>