Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 43.98 KB | None | 0 0
  1. <?php
  2. define("OK", true);
  3. require_once("global.php");
  4. session_start();
  5.  
  6. if(!get_magic_quotes_gpc()){
  7.         $_REQUEST[USERNAME]=addslashes($_REQUEST[USERNAME]);
  8.         $_REQUEST[PASSWORD]=addslashes($_REQUEST[PASSWORD]);
  9. }
  10. //print_r($_GET);
  11. if($_REQUEST[Memo]) $_POST[Memo]=$_REQUEST[Memo];
  12. if(!isset($_SESSION['goip_username'])){
  13.         $rs=$db->fetch_array($db->query("SELECT id FROM user WHERE username='".$_REQUEST[USERNAME]."' and password='".md5($_REQUEST[
  14. PASSWORD])."'"));
  15.  
  16.         if(empty($rs[0])){
  17.                 require_once ('login.php');
  18.                 exit;
  19.         }
  20.         $userid=$rs[0];
  21. }
  22. else $userid=$_SESSION[goip_userid];
  23. ?>
  24. <html>
  25. <head>
  26. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  27. <link href="style.css" rel="stylesheet" type="text/css">
  28. <title>Sending Messge</title>
  29. </head>
  30. <body>
  31. <?php
  32. ignore_user_abort(true);
  33. ob_end_flush();
  34. set_time_limit(0);
  35. ini_set("memory_limit", "1024M");
  36. echo str_pad(" ", 256);
  37. if($goipcronport)
  38.     $port=$goipcronport;
  39. else
  40.     $port=44444;
  41.     //ob_end_flush();
  42.  
  43. function restart(&$goiprow,$len,$msg)
  44. {
  45.     global $db;
  46.     global $port;
  47.     global $re_ask_timer;
  48.     $query=$db->query("SELECT prov.*,goip.* FROM goip,prov where prov.id=goip.provider and alive=1 and gsm_status!='LOGOUT' and  goip.id=$goiprow[id]");
  49.     $rs=$db->fetch_array($query);
  50.     //echo "SELECT prov.*,goip.* FROM goip,prov where prov.id=goip.provider and alive=1 and gsm_status!='LOGOUT' and goip.id=$goiprow[id]". "restart: $rs[name] !!!<br>";
  51.     if($rs[0]){
  52.         if( $rs['remain_count']==0 || $rs['remain_count_d']==0) {
  53.             echo "GoIP Line($goiprow[name]) remain count is down<br>";
  54.             return;
  55.         }
  56.         $buf="DONE $goiprow[messageid]\n";
  57.         if (@socket_sendto($goiprow[sock],$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  58.             echo ("sendto error");
  59.  
  60.         $goiprow['timer']=3;
  61.         $goiprow['send']="MSG";
  62.         //echo "$sendid $goiprow[id] $goiprow[messageid] <br>";
  63.         $goiprow['time']=time();//計時
  64.         $goiprow[host]=$rs[host];
  65.         $goiprow[port]=$rs[port];
  66.         $buf="START ".$goiprow['messageid']." $goiprow[host] $goiprow[port]\n";
  67.         //echo $buf."<br>"."<br>"."<br>"."<br>"."<br>"."<br>";
  68.         if (@socket_sendto($goiprow[sock],$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  69.             echo ("sendto error");
  70.         for($i=0;$i<3;$i++){
  71.             //echo "check:$i";
  72.             $read=array($goiprow[sock]);
  73.             $err=socket_select($read, $write, $except, 5);
  74.             if($err>0){
  75.                 //echo "11213134";
  76.                 if(($n=@socket_recvfrom($goiprow[sock],$buf,1024,0,$ip,$port1))==false){
  77.                     echo("recvform error".socket_strerror($ret)."<br>");
  78.                     continue;
  79.                 }
  80.                 else{
  81.                     //echo "111111111:$buf";
  82.                     if($buf=="OK"){
  83.                         $flag=1;
  84.                         break;
  85.                     }
  86.                 }
  87.             }
  88.         }//for
  89.         if($i>=3)
  90.             die("Cannot get response from process named \"goipcron\". please check this process.");
  91.         //$buf="MSG ".$goiprow['messageid']." $len $msg\n";
  92.         //if (@socket_sendto($socket,$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  93.             //echo ("sendto error");
  94.     }
  95.     else {
  96.         echo "$goiprow[name] logout, after 100 seconds ask again.<br>";
  97.         $goiprow[send]="RMSG";
  98.         $goiprow[timer]=$re_ask_timer;
  99.     }
  100. }
  101.  
  102. /*發送程序*/
  103.     function dolastsend( &$goipsend,$len,$msg)
  104.     {
  105.         global $port;
  106.         $sendid=$goipsend[messageid];
  107.         //echo "dolastsend $goipsend[send]";
  108.         if($goipsend[send]=="RMSG"){
  109.             if($goipsend[timer] <=1 ){
  110.                 restart($goipsend,$len,$msg);
  111.                 //echo "$goipsend[send] $goipsend[timer]";
  112.                 //$goipsend[send]="MSG";
  113.                 //$goipsend[timer]=3;
  114.             }
  115.             else return;
  116.         }
  117.        
  118.         if($goipsend[send]=="HELLO"){
  119.             $buf="HELLO $sendid\n";
  120.             if (@socket_sendto($goipsend[sock],$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  121.                 echo ("sendto error");                     
  122.             //$goipsend[timer]=0;
  123.            
  124.         }
  125.         elseif($goipsend[send]=="PASSWORD"){
  126.             $buf="PASSWORD $sendid $goipsend[password]\n";
  127.            
  128.             @socket_sendto($goipsend[sock],$buf, strlen($buf), 0, "127.0.0.1", $port);
  129.             //$goipsend[timer]=0;                  
  130.         }
  131.         elseif($goipsend[send]=="SEND"){
  132.             if($goipsend[telrow][msg])
  133.                 $buf="SMS $sendid $goipsend[telid] $goipsend[password] ".$goipsend[telrow][tel]." ".$goipsend[telrow][msg];
  134.             else
  135.                 $buf="SEND $sendid $goipsend[telid] $goipsend[tel]\n";
  136.             @socket_sendto($goipsend[sock],$buf, strlen($buf), 0, "127.0.0.1", $port);
  137.             echo "$buf ($goipsend[name] $goipsend[prov]) <br>";
  138.             //$goipsend[timer]=0;
  139.         }  
  140.         elseif($goipsend[send]=="MSG"){
  141.             $buf="MSG ".$sendid." $len $msg\n";
  142.             echo "$buf ($goipsend[name] $goipsend[prov]) <br>";
  143.             @socket_sendto($goipsend[sock],$buf, strlen($buf), 0, "127.0.0.1", $port);
  144.             //$goipsend[timer]=0;
  145.         }              
  146.     }
  147.  
  148.  
  149.     function do_cron($db,$crontime,$port)
  150.     {
  151.         if(!$port) $port=44444;
  152.         $rs=$db->fetch_array($db->query("SELECT id FROM message WHERE crontime>0 and crontime<$crontime and over=0"));//是否有未執行的比新計劃還要前的計劃
  153.         $flag=1;
  154.         if(empty($rs[0])){
  155.             $flag=0;
  156.             /* 此是最新計劃, 喚醒服務進程*/
  157.             if (($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) <= 0) {
  158.                 echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n";
  159.                 exit;
  160.             }
  161.             if (socket_sendto($socket,"CRON", 4, 0, "127.0.0.1", $port)===false)
  162.                 echo ("sendto error:". socket_strerror($socket));
  163.             for($i=0;$i<3;$i++){
  164.                 $read=array($socket);
  165.                 $err=socket_select($read, $write, $except, 5);
  166.                 if($err>0){    
  167.                     if(($n=@socket_recvfrom($socket,$buf,1024,0,$ip,$port))==false){
  168.                         //echo("recvform error".socket_strerror($ret)."<br>");
  169.                         continue;
  170.                     }
  171.                     else{
  172.                         if($buf=="OK"){
  173.                             $flag=1;
  174.                             break;
  175.                         }
  176.                     }
  177.                 }
  178.                
  179.             }//for
  180.  
  181.         }//最新計劃
  182.         if($flag)
  183.             echo "The task has been inserted.";
  184.         else
  185.             echo "The task has been inserted,but cannot get response from process named \"goipcron\". please check this process.";     
  186.     }
  187.    
  188.     function checktime()
  189.     {
  190.         $dabuf=explode(' ',$_REQUEST[datehm]);
  191.         $date=explode('-',$dabuf[0]);
  192.         $time=explode(':',$dabuf[1]);
  193.         //print_r($dabuf);
  194.         //print_r($date);
  195.         //print_r($time);
  196.         //echo date ("Y-m-d H:i:s", $_REQUEST[datehm]);
  197.         $crontime=mktime ($time[0],$time[1],0,$date[1],$date[2],$date[0]);
  198.         $nowtime=time();
  199.         //if($nowtime > $crontime)
  200.             //die("現在時間".date ("Y-m-d H:i:s")."比設定的時間$_POST[datehm]晚");
  201.         return $crontime;
  202.     }
  203.    
  204.     function checkover($goipdb)
  205.     {
  206.         global $endless_send;
  207.         if(!$endless_send) return false;
  208.         //echo "checkover <br>";
  209.         foreach($goipdb as $the0 => $goipsend){
  210.             //echo $goipdb[$the0][send]." <br>";
  211.             if($goipsend[timer]>0){//重試
  212.                 if($goipdb[$the0][send]!="RMSG"){  //如果其他已結束,將不等待連不上的GOIP
  213.                     return false;//未完成
  214.                 }
  215.             }
  216.         }
  217.         return true;               
  218.     }
  219.  
  220.  
  221. /*要有一個權限檢查*/
  222. //print_r($_POST);
  223. if(!isset($_POST['crowdid']) && !isset($_POST['groupid']) && !isset($_POST['alltype']) && !isset($_REQUEST['method']) && $_REQUEST['action']!='upload')
  224.     die("Permission denied");
  225.  
  226. if(get_magic_quotes_gpc())
  227.     $memo=stripslashes($_POST[Memo]);
  228. else
  229.     $memo=$_POST[Memo];
  230. if(!get_magic_quotes_gpc())
  231.     $_POST[Memo]=addslashes($_POST[Memo]);
  232.    
  233.     //exit;
  234.  
  235. $query=$db->query("SELECT * from prov ");
  236. while($row=$db->fetch_assoc($query)) {
  237.     //echo $row[id]." ".$row[inter]."<br>";
  238.     $row[interlen]=strlen($row[inter]);
  239.     $row[locallen]=strlen($row[local]);
  240.     $prov[$row[id]]=$row;
  241.     $prov_db[$row[prov]]=$row[id];
  242. }
  243.  
  244. if(isset($_POST['groupid'])){   //發送一個組的若幹id
  245. if(empty($_POST[groupid]))
  246.     die("Permission denied");
  247. if($_SESSION['goip_permissions'] > 1){
  248.  
  249.     $query=$db->query("SELECT id FROM refgroup WHERE groupsid=$_POST[groupid] and userid=$_SESSION[goip_userid]");
  250.     $rs=$db->fetch_array($query);
  251.     if(empty($rs[0])){
  252.         $query=$db->query("SELECT groups.crowdid FROM refcrowd,groups WHERE groups.id=$_POST[groupid] and refcrowd.crowdid=groups.crowdid and refcrowd.userid=$_SESSION[goip_userid]");
  253.         $rs=$db->fetch_array($query);
  254.         if(empty($rs[0])){
  255.             die("Permission denied");
  256.         }
  257.     }
  258. }
  259.  
  260. //  exit;
  261.  
  262.         $totalnum=0;
  263.  
  264.  
  265. /*tels 6元素的數組*/
  266.         $num=$_POST['boxsa'];
  267.  
  268.         for($i=0;$i<$num;$i++)
  269.         {  
  270.             if(!empty($_POST["ida$i"])){
  271.                 if($Ida=="")
  272.                     $Ida=$_POST["ida$i"];
  273.                 else
  274.                     $Ida=$_POST["ida$i"].",$Ida";
  275.             }
  276.         }
  277.         for($i=0;$i<$num;$i++)
  278.         {  
  279.             if(!empty($_POST["idb$i"])){
  280.                 if($Idb=="")
  281.                     $Idb=$_POST["idb$i"];
  282.                 else
  283.                     $Idb=$_POST["idb$i"].",$Idb";
  284.             }
  285.         }
  286.         for($i=0;$i<$num;$i++)
  287.         {  
  288.             if(!empty($_POST["idc$i"])){
  289.                 if($Idc=="")
  290.                     $Idc=$_POST["idc$i"];
  291.                 else
  292.                     $Idc=$_POST["idc$i"].",$Idc";
  293.             }
  294.         }
  295.  
  296.         $total=get_count_from_sms($_POST['Memo']);
  297.         if($_POST[submit2]){
  298.             $crontime=checktime();
  299.             if($_REQUEST[id])
  300.                 $db->query("update message set receiverid='$Ida', receiverid1='$Idb', receiverid2='$Idc', msg='$_REQUEST[Memo]', userid=$_SESSION[goip_userid],total='$total',crontime=$crontime where id=$_REQUEST[id]");
  301.             else
  302.                 $db->query("INSERT INTO message (receiverid,receiverid1,receiverid2,msg,total,userid,crontime,groupid) VALUES ('$Ida','$Idb','$Idc','$_REQUEST[Memo]','$total',$_SESSION[goip_userid],$crontime,'$_REQUEST[groupid]')");   
  303.             do_cron($db,$crontime,$goipcronport);
  304.             exit;
  305.         }
  306.        
  307.         $tels=array();
  308.         if(!$Ida) $Ida=0;
  309.         if(!$Idb) $Idb=0;
  310.         if(!$Idc) $Idc=0;
  311.         $query=$db->query("(SELECT id,name,provider,tel,0 as lev FROM receiver where id in ($Ida)) union (SELECT id,name1,provider1,tel1,1 FROM receiver where id in ($Idb) ) union (SELECT id,name2,provider2,tel2,2 FROM receiver where id in ($Idc) ) ORDER BY tel");   
  312.         $teltmp=0;
  313.         while($row=$db->fetch_assoc($query)) {
  314.             if(!$row[tel]) continue;
  315.             if(!($prov[$row[provider]][locallen] && !strncmp($row[tel], $prov[$row[provider]][local], $prov[$row[provider]][locallen])) && $row[tel][0] !='+')
  316.                 $row[tel]=$prov[$row[provider]][inter].$row[tel];
  317.             $row['total']=$total;
  318.             if($row[tel] && $row[tel] != $teltmp) {
  319.                 $tels[$row[provider]][]=$row;
  320.                 $teltmp=$row[tel];
  321.                 $totalnum++;
  322.             }
  323.         }
  324.             //print_r($rsdb);
  325.         /*刪除重複的號碼*/
  326.         //foreach($tels as $prov => $telrow){
  327.             //$tels[$prov]=array_unique($telrow);
  328.         //}
  329.         $db->query("INSERT INTO message (receiverid,receiverid1,receiverid2,msg,userid,groupid,total) VALUES ('$Ida','$Idb','$Idc','$_POST[Memo]',$_SESSION[goip_userid],'$_POST[groupid]','$total')");
  330.         $sendsiddb=$db->fetch_array($db->query("SELECT LAST_INSERT_ID()"));
  331.         $sendid=$sendsiddb[0];
  332.         echo "sending message:$memo <br>";
  333.         echo "total will send: $totalnum <br>";
  334.         startdo($db, $tels, $sendid);
  335. }elseif(isset($_POST['crowdid'])){//以組爲最小單位發送
  336.         $num=$_POST['boxsa'];
  337.  
  338.         for($i=0;$i<$num;$i++)
  339.         {  
  340.             if(!empty($_POST["id$i"])){
  341.                 if($Id=="")
  342.                     $Id=$_POST["id$i"];
  343.                 else
  344.                     $Id=$_POST["id$i"].",$Id";
  345.             }
  346.         }
  347.         if($_POST['jr']) $Ida=$Id;
  348.         if($_POST['jr1']) $Idb=$Id;
  349.         if($_POST['jr2']) $Idc=$Id;
  350.         $total=get_count_from_sms($_POST['Memo']);
  351.         if($_POST[submit2]){
  352.             $crontime=checktime();
  353.             if($_GET[id])
  354.                 $db->query("update message set type=1,groupid='$Ida', groupid1='$Idb', groupid2='$Idc', msg='$_POST[Memo]', userid=$_SESSION[goip_userid],total='$total',crontime=$crontime where id=$_GET[id]");
  355.             else
  356.                 $db->query("INSERT INTO message (type,groupid,groupid1,groupid2,msg,total,userid,crontime) VALUES (1,'$Ida','$Idb','$Idc','$_POST[Memo]','$total',$_SESSION[goip_userid],$crontime)"); 
  357.             do_cron($db,$crontime,$goipcronport);
  358.             exit;
  359.         }
  360.         if(!$Ida) $Ida=0;
  361.         if(!$Idb) $Idb=0;
  362.         if(!$Idc) $Idc=0;      
  363.         $query=$db->query("(SELECT receiver.id,name,provider,tel,0 as lev FROM receiver inner join recvgroup on (receiver.id=recvgroup.recvid ) where groupsid in ($Ida))
  364. union ( SELECT receiver.id,name,provider,tel,1 FROM receiver inner join recvgroup on (receiver.id=recvgroup.recvid ) where groupsid in ($Idb))
  365. union ( SELECT receiver.id,name,provider,tel,2 FROM receiver inner join recvgroup on (receiver.id=recvgroup.recvid ) where groupsid in ($Idc))
  366. order by tel");
  367.         $tels=array();
  368.         $teltmp=0;
  369.         while($row=$db->fetch_assoc($query)) {
  370.             if(!$row[tel]) continue;
  371.             if(!($prov[$row[provider]][locallen] && !strncmp($row[tel], $prov[$row[provider]][local], $prov[$row[provider]][locallen])) && $row[tel][0] !='+')
  372.                 $row[tel]=$prov[$row[provider]][inter].$row[tel];
  373.             if($row[tel] && $row[tel] != $teltmp) {
  374.                 $row['total']=$total;
  375.                 $tels[$row[provider]][]=$row;
  376.                 $teltmp=$row[tel];
  377.                 $totalnum++;
  378.                 //echo $teltmp."<br>";
  379.             }
  380.         }
  381.         //die("haha");
  382.  
  383.         echo "sending message:$memo <br>";
  384.         echo "total will send: $totalnum <br>";
  385.         //print_r($tels);
  386.         $db->query("INSERT INTO message (type,groupid,groupid1,groupid2,msg,userid,total) VALUES (1,'$Ida','$Idb','$Idc','$_POST[Memo]',$_SESSION[goip_userid],'$total')");
  387.         $sendsiddb=$db->fetch_array($db->query("SELECT LAST_INSERT_ID()"));
  388.         $sendid=$sendsiddb[0];
  389.         startdo($db, $tels, $sendid);
  390.         //}
  391.  
  392. }
  393. elseif(isset($_POST['alltype'])) {//向全部人發送
  394.     //print_r($_POST);
  395.     $jr=$_POST['jr']?1:0;
  396.     $jr1=$_POST['jr1']?1:0;
  397.     $jr2=$_POST['jr2']?1:0;
  398.     $total=get_count_from_sms($_POST['Memo']);
  399.     if($_POST[submit2]){
  400.         $crontime=checktime();
  401.         if($_GET[id])
  402.             $db->query("update message set type=2, recv=$jr, recv1=$jr1, recv2=$jr1, msg='$_POST[Memo]', userid=$_SESSION[goip_userid],total='$total',crontime=$crontime where id=$_GET[id]");
  403.         else
  404.             $db->query("INSERT INTO message (type,msg,userid,total,crontime,recv,recv1,recv2) VALUES (2, '$_POST[Memo]', $_SESSION[goip_userid],'$total',$crontime,$jr,$jr1,$jr2)");   
  405.         do_cron($db,$crontime,$goipcronport);
  406.         exit;
  407.     }
  408.     $query=$db->query("(SELECT id,name,provider,tel,0 as lev FROM receiver where $jr) union (SELECT id,name1,provider1,tel1,1 FROM receiver where $jr1 ) union (SELECT id,name2,provider2,tel2,2 FROM receiver where $jr2) ORDER BY tel"); 
  409.     $tels=array();
  410.     $teltmp=0;
  411.     while($row=$db->fetch_assoc($query)) {
  412.         if(!$row[tel]) continue;
  413.         if(!($prov[$row[provider]][locallen] && !strncmp($row[tel], $prov[$row[provider]][local], $prov[$row[provider]][locallen])) && $row[tel][0] !='+')
  414.             $row[tel]=$prov[$row[provider]][inter].$row[tel];
  415.         if($row[tel] && $row[tel] != $teltmp) {
  416.             $row['total']=$total;
  417.             $tels[$row[provider]][]=$row;
  418.             $teltmp=$row[tel];
  419.             $totalnum++;
  420.         }
  421.     }
  422.     $db->query("INSERT INTO message (type,msg,userid,recv,recv1,recv2,total) VALUES (2,'$_POST[Memo]',$_SESSION[goip_userid],$jr,$jr1,$jr2,'$total')");
  423.     $sendsiddb=$db->fetch_array($db->query("SELECT LAST_INSERT_ID()"));
  424.     $sendid=$sendsiddb[0];
  425.     echo "sending message:$memo <br>";
  426.     echo "total will send: $totalnum <br>";
  427.     startdo($db, $tels, $sendid);
  428. }
  429. elseif($_REQUEST[method]==2) //输入一个号码
  430. {
  431.         $provid=$_REQUEST[smsprovider];
  432.         $mobile=$_REQUEST[smsnum];
  433.     //$goipid=$_REQUEST['smsgoip'];
  434.     if(!$_REQUEST[smsgoip] && $_REQUEST['goipname']){
  435.         $rs=$db->fetch_array($db->query("select id from goip where name='$_REQUEST[goipname]'"));
  436.         $_REQUEST[smsgoip]=$rs[0];
  437.     }
  438.     $total=get_count_from_sms($_REQUEST['Memo']);
  439.         if($_REQUEST[submit2]){
  440.                 $crontime=checktime();
  441.                 if($_REQUEST[id])
  442.                         $db->query("update message set msg='$_REQUEST[Memo]', userid=$userid,crontime=$crontime,tel='$mobile',prov='$provid',goipid='$_REQUEST[smsgoip]',total='$total' where id=$_REQUEST[id]");
  443.                 else
  444.                         $db->query("INSERT INTO message (type,msg,userid,crontime,tel,prov,goipid,total) VALUES (4, '$_REQUEST[Memo]',$userid, $crontime,'$mobile','$provid','$_REQUEST[smsgoip]','$total')");
  445.                 do_cron($db,$crontime,$goipcronport);
  446.                 exit;
  447.         }
  448.     $mobiles=array();
  449.     $mobiles=explode(",",$mobile);
  450.     foreach($mobiles as $tel_num){
  451.             if(!($prov[$provid][locallen] && !strncmp($tel_num, $prov[$provid][local], $prov[$provid][locallen])) && $tel_num[0] !='+')
  452.                     $tel_num=$prov[$provid][inter].$tel_num;
  453.             $xrow['total']=$total;
  454.             $xrow['provider']=$provid;
  455.             $xrow['lev']=0;
  456.             $xrow['id']=0;
  457.             $xrow['tel']=$tel_num;
  458.             $tels[$provid][]=$xrow;
  459.             //$teltmp=$row[tel];
  460.             $totalnum++;
  461.     }
  462.  
  463.         $db->query("INSERT INTO message (msg,userid,type,tel,prov,goipid,total) VALUES ('$_POST[Memo]',$userid,4,'$mobile','$provid','$_REQUEST[smsgoip]',$total)");
  464.     //echo "INSERT INTO message (msg,userid,type,tel,prov,goipid) VALUES ('$_POST[Memo]',$userid,4,'$mobile','$provid','$_REQUEST[smsgoip]')";
  465.         $sendsiddb=$db->fetch_array($db->query("SELECT LAST_INSERT_ID()"));                                      
  466.         $sendid=$sendsiddb[0];
  467.                 echo "sending message:$memo <br>";                                                              
  468.                 echo "total will send: $totalnum <br>";                                                          
  469.         startdo($db, $tels, $sendid, $_REQUEST[smsgoip]);  
  470. }
  471. elseif($_REQUEST['action']=="upload"){
  472.     require_once('../xmlClass.php');
  473.     $data = implode("",file($_FILES['xmlfile']['tmp_name'])) or die("could not open XML input file");
  474.     $obj = new xml($data,"xml");
  475. /*
  476.     $db->query("select * from prov order by id");
  477.     while($row=$db->fetch_assoc($query)) {
  478.         $prov_db[$row['prov']]=$row[id];
  479.     }
  480. */
  481.     for($i=0;$i<sizeof($xml["send_sms"]);$i++){
  482.         $msg=addslashes($xml["send_sms"][$i]->content[0]);
  483.                 $tel_num=addslashes($xml["send_sms"][$i]->number[0]);
  484.         $provname=addslashes($xml["send_sms"][$i]->provider[0]);
  485.         if($tel_num && $prov && $prov_db[$provname]){
  486.             $provid=$prov_db[$provname];
  487.             if(!($prov[$provid][locallen] && !strncmp($tel_num, $prov[$provid][local], $prov[$provid][locallen])) && $tel_num[0]!='+')
  488.             $tel_num=$prov[$provid][inter].$tel_num;
  489.  
  490.             $xrow['provider']=$provid;
  491.             $xrow['lev']=0;
  492.             $xrow['id']=0;
  493.             $xrow['tel']=$tel_num;
  494.             $xrow['msg']=$msg;
  495.             $xrow['total']=get_count_from_sms($msg);
  496.             $tels[$provid][]=$xrow;
  497.             $totalnum++;
  498.             if($totalnum==1)
  499.                 $mobile=$tel_num;
  500.             else
  501.                 $mobile.=",".$tel_num;
  502.         }
  503.         //$goipname=addslashes($xml["send_sms"][$i]->goipname[0]);
  504.         echo "$msg $tel_num $provname $provid $goipname";
  505.     }
  506.     if($totalnum > 0){
  507.         //echo "INSERT INTO message (userid,type,tel) VALUES ($userid,9,'$mobile')";
  508.         //die;
  509.         $db->query("INSERT INTO message (userid,type,tel) VALUES ($userid,9,'$mobile')");
  510.         $sendsiddb=$db->fetch_array($db->query("SELECT LAST_INSERT_ID()"));
  511.         $sendid=$sendsiddb[0];
  512.         echo "total will send: $totalnum <br>";
  513.         startdo($db, $tels, $sendid);
  514.     }
  515.     else die("none sms");
  516.  
  517. }
  518. else print_r($_POST);
  519.  
  520. function startdo($db, $tels,$sendid, $goipid=0){
  521.         global $prov;
  522.         global $port;
  523.         global $memo;
  524.         global $userid;
  525.         global $endless_send;
  526.         global $re_ask_timer;
  527.  
  528.         session_write_close();
  529.                 $query=$db->query("SELECT send_page_jump_enable FROM system WHERE 1 ");
  530.                 $sys_rs=$db->fetch_array($query);
  531.                
  532.                 if($sys_rs['send_page_jump_enable']==1){
  533.             $i=0;
  534.             $sqlv="insert into sends (messageid,userid,telnum,provider,recvid,recvlev,time,msg,total) values";
  535.             foreach($tels as $provname => $provtels){
  536.                 foreach($provtels as $send){
  537.                     $sql.="($sendid,$userid,'".$send[tel]."','$provname',".$send[id].",".$send[lev].",'','".$send[msg]."','".$send[total]."'),";
  538.                     $i++;
  539.                     if($i%2000==0){
  540.                         $sql[strlen($sql)-1]="";
  541.                         $db->query($sqlv.$sql);
  542.                         $sql="";
  543.                     }
  544.                 }
  545.             }
  546.             if($sql){
  547.                 $sql[strlen($sql)-1]="";
  548.                 $db->query($sqlv.$sql);
  549.             }
  550.             //die;
  551.             echo "<script language='javascript'>";
  552.             echo "window.location = 'resend.php?messageid=".$sendid."&USERNAME=".$_REQUEST[USERNAME]."&PASSWORD=".$_REQUEST[PASSWORD]."'";
  553.             echo "</script>";
  554.             die;
  555. }
  556.  
  557.         //print_r($prov);
  558.         $msg=$memo;
  559.         $len=strlen($msg);     
  560.         $nowtime=date ("Y-m-d H:i:s");
  561.         //$sendid=1111;
  562.         //$id=0;
  563.         //$query=$db->query("SELECT prov.*,goip.* FROM goip,prov where prov.id=goip.provider and alive=1 and gsm_status!='LOGOUT' ORDER BY name"); 
  564.         $db->query("update message set over=1 where id=$sendid");
  565.         $query=$db->query("SELECT prov.*,goip.* FROM goip,prov where prov.id=goip.provider ORDER BY name");
  566.         $socks=array();
  567.         $errortels=array();
  568.         $goipdb=array();
  569.         while($goiprow=$db->fetch_array($query)) {
  570.             $goipname[]=$goiprow[provider];
  571.  
  572.             /*把信息傳過去*/         
  573.             if(($goipid && $goiprow[id]==$goipid) || (!$goipid && $tels[$goiprow[provider]])){ //有要發給這個服務商的號碼才通信
  574.                 $errortels[$goiprow[provider]]=array();
  575.                 if (($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) <= 0) {
  576.                     echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n";
  577.                     exit;
  578.                 }
  579.                 $goiprow['sock']=$socket;
  580.                 $goiprow['time']=time();//計時
  581.                 //echo "$goiprow[name], $goiprow[remain_count] $goiprow[remain_count_d]<br>";
  582.                 if( $goiprow['remain_count']==0 || $goiprow['remain_count_d']==0) {
  583.                     echo "GoIP Line($goiprow[name]) remain count is down<br>";
  584.                     continue;
  585.                 }
  586.                 if($goiprow[alive] != 1 || $goiprow[gsm_status] == 'LOGOUT'){
  587.                     $goiprow['timer']=$re_ask_timer;
  588.                                     $goiprow['send']="RMSG";
  589.                     $goiprow['messageid']=$sendid+($goiprow[id] << 16);
  590.                     $goipdb[]=$goiprow;
  591.                     $socks[]=$socket;
  592.                     echo "$goiprow[name] logout, after 100 seconds ask again.<br>";
  593.                     continue;
  594.                 }
  595.                 $goiprow['timer']=3;
  596.                 $goiprow['send']="MSG";
  597.                 $goiprow['messageid']=$sendid+($goiprow[id] << 16);
  598.                 $goipdb[]=$goiprow;
  599.                 echo "$sendid $goiprow[id] $goiprow[messageid] <br>";
  600.                 $buf="START ".$goiprow['messageid']." $goiprow[host] $goiprow[port]\n";
  601.                 if (@socket_sendto($socket,$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  602.                     echo ("sendto error");
  603.                 for($i=0;$i<3;$i++){
  604.                     $read=array($socket);
  605.                     $err=socket_select($read, $write, $except, 5);
  606.                     if($err>0){    
  607.                         if(($n=@socket_recvfrom($socket,$buf,1024,0,$ip,$port1))==false){
  608.                             //echo("recvform error".socket_strerror($ret)."<br>");
  609.                             continue;
  610.                         }
  611.                         else{
  612.                             if($buf=="OK"){
  613.                                 $flag=1;
  614.                                 break;
  615.                             }
  616.                         }
  617.                     }
  618.                 }//for
  619.                 if($i>=3)
  620.                     die("Cannot get response from process named \"goipcron\". please check this process.");
  621.                 $buf="MSG ".$goiprow['messageid']." $len $msg\n";
  622.                 if (@socket_sendto($socket,$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  623.                     echo ("sendto error");
  624.                 $socks[]=$socket;
  625.                 //print_r($goiprow);
  626.             }
  627.             //print_r($goiprow);
  628.         }
  629.         foreach($tels as $provtmp => $valuetmp){
  630.             foreach($goipdb as $goiptmp){
  631.                 if($goiptmp['provider']==$provtmp)
  632.                     continue 2;
  633.             }
  634.             $n=count($valuetmp);
  635.             echo "<font color='#FF0000'>Will send {$n} message(s) to receivers of ".$prov[$provtmp]['prov'].", but cannot find any login GoIP of ".$prov[$provtmp]['prov']." </font><br>";
  636.         }
  637.         //$read = array($socket);
  638.         $timeout=5;
  639.         for(;;){
  640.             $read=$socks;
  641.             flush();
  642.             if(count($read)==0)
  643.                 break;
  644.             $err=socket_select($read, $write, $except, $timeout);
  645.             //echo "select:$err <br>";
  646.             if($err===false)
  647.                 echo "select error!";
  648.             elseif($err==0){ //全體超時
  649.                 //echo "select timeout";
  650.                 $i=0;
  651.                 $flag=1;
  652.                 $nowtime=time();
  653.                 //reset($goipdb);
  654.                 //while (list (, $goipsend) = each ($goipdb)) {
  655.                 foreach($goipdb as $the0 => $goipsend){
  656.                     //$goipsend=$goipdb[$the0];
  657.                     $goipdb[$the0]['time']=$nowtime;
  658.                     //echo("<br>$i $goipsend[send] timer:".$goipsend[timer]."<br>");
  659.                     if($goipsend[timer]>0){//重試
  660.                        
  661.                         if($goipdb[$the0][send]!="RMSG")  //如果其他已結束,將不等待連不上的GOIP
  662.                             $flag=0;//未完成
  663.                         dolastsend( $goipdb[$the0],$len,$msg);
  664.                         $goipdb[$the0]['timer']--;
  665.                         //echo("<br>$i $goipsend[send] timer:".$goipsend[timer]."<br>");
  666.                         $i++;
  667.                     }
  668.                     else{ //累計失敗
  669.                         if($goipsend[send]=="OK") //已完成的
  670.                             continue;
  671.                        
  672.                         if($goipsend[send]=="SEND"){
  673.                             //echo "inser: $goipnow[telid] $goipnow[tel] faile<br>";
  674.                             echo "<font color='#FF0000'>$goipnow[telid] $goipnow[tel] faile</font><br>";
  675.                             foreach($goipdb as $the => $goiptmp){
  676.                                 if($goiptmp[send]=="OK" && $goiptmp[provider]==$goipsend[provider]){
  677.                                     if($goipsend[telrow][error] && in_array($goiptmp[id],$goipsend[telrow][error]))
  678.                                         continue;//已發送錯誤
  679.                                     $goipdb[$the][send]="SEND";
  680.                                     $goipdb[$the][tel]=$goipsend[tel];
  681.                                     $goipdb[$the][telid]=$goipsend[telid];
  682.                                     //$goipsend[send]=="OK"; //結束
  683.                                     dolastsend( $goiptmp,$len,$msg);
  684.                                     $goipdb[$the0][send]="RMSG";//超時的goip,100s後通訊
  685.                                     $goipdb[$the0][timer]=$re_ask_timer;
  686.                                     $goipdb[$the0][tel]=0;
  687.                                     $goipdb[$the0][telid]=0;
  688.                                     break;
  689.                                 }                                  
  690.                             }
  691.                         }
  692.                         else{
  693.                             /*100s 後重新通訊*/
  694.                             echo "<font color='#FFOO00'>cannot get response from goip: $goipsend[send] ($goipsend[name] $goipsend[prov])</font><br>";
  695.                             $goipdb[$the0][send]="RMSG";
  696.                             $goipdb[$the0][timer]=$re_ask_timer;
  697.                         }
  698.                         if($goipsend[send]=="SEND"){//沒有找到空閑的goip,把號碼壓回,100s後重新通訊
  699.                             if($goipsend[telrow][error])
  700.                                 array_push($errortels[$goipsend[provider]], $goipsend[telrow]);//壓回出錯數組
  701.                             else
  702.                                 array_push($tels[$goipsend[provider]], $goipsend[telrow]); //壓回
  703.                             //array_push($tels[$goipsend[provider]], $goipsend[telrow]); //壓回
  704.                             /*刪除數據庫*/
  705.                             $db->query("delete from sends where id=$goipsend[telid]"); 
  706.                             $goipdb[$the0][send]="RMSG";
  707.                             $goipdb[$the0][timer]=$re_ask_timer;
  708.                             $goipdb[$the0][tel]=0;
  709.                             $goipdb[$the0][telid]=0;                       
  710.                         }  
  711.                     }  
  712.                 }
  713.                
  714.                 if($flag && !$endless_send)
  715.                     break; //全部結束
  716.                
  717.             }//全體超時
  718.             else{ //可讀
  719.            
  720.               foreach($read as $socket){
  721.                 unset($buf);
  722.                 //$buf="";
  723.                
  724.                 if(($n=@socket_recvfrom($socket,$buf,1024,0,$ip,$port1))==false){
  725.                     echo("recvform error".socket_strerror($ret)."<br>");
  726.                     continue;
  727.                 }
  728.                 //echo "hahaha ".$buf;
  729.                   $comm=explode(" ",$buf);
  730.                     //$teli=(int)substr($comm[1], -1)-1;
  731.                     foreach($goipdb as $the => $goipnow){
  732.                         //echo "$key => $val\n";
  733.                         if($goipnow[sock]==$socket){
  734.                             break;
  735.                         }  
  736.                     }
  737.                    
  738.                     if(empty($goipnow)){ //不是期望的套接口
  739.                         continue;
  740.                     }
  741.                     if(strncmp($goipnow[messageid],$comm[1], strlen($goipnow[messageid])))//不是期望的id
  742.                         continue;
  743.                     //echo "$goipnow[name] <br>";
  744.                     if($comm[0]=="OK"){
  745.                         //更新數據庫,發送成功
  746.                         if(is_numeric($comm[3]))
  747.                         $db->query("update sends set over=1,sms_no=$comm[3],goipid=$goipnow[id],time=now() where id='".$goipnow[telid]."' and messageid=$sendid");
  748.                         else
  749.                         $db->query("update sends set over=1,goipid=$goipnow[id],time=now() where id='".$goipnow[telid]."' and messageid=$sendid"); 
  750.                         /**/
  751.                         if($goipnow[send]!="SEND"){//不處于發送狀態,無視
  752.                             //echo "net send status <br>";
  753.                             continue;
  754.                         }  
  755.                         if($comm[2]==$goipnow[telid]){ //是現在發的號碼,可以發下一個了
  756.                             echo "<font color='#00FF00'>$goipnow[telid] ".$goipnow[telrow][name]." $goipnow[tel] ($goipnow[name] $goipnow[prov]) ok</font><br>";
  757.                             $goipdb[$the]['send']="OK";//結束了                 
  758.                             $goipdb[$the][telid]=0;
  759.                             $goipdb[$the][tel]=0;
  760.                             $goipdb[$the][timer]=0;
  761.                             if(!check_sms_remain_count($db,$goipnow[id],$goipnow[name],$goipdb[$the][telrow][total])) continue;
  762.                             if(($goipdb[$the][telrow]=array_pop($tels[$goipnow[provider]]))!==NULL){//出來   
  763.                                 /*寫入數據庫,得到id, 發送*/
  764.                                                
  765.                                     $goipdb[$the][tel]=$goipdb[$the][telrow][tel];
  766.                                 $db->query("INSERT INTO sends (messageid,userid,telnum,goipid,provider,recvid,recvlev,msg,total) VALUES ($sendid,$userid,'".$goipdb[$the][tel]."',$goipnow[id],'$goipnow[provider]',".$goipdb[$the][telrow][id].",'".$goipdb[$the][telrow][lev]."','".$goipdb[$the][telrow][msg]."','".$goipdb[$the][telrow][total]."')");     
  767.                                 $telid=$db->fetch_array($db->query("SELECT LAST_INSERT_ID()"));
  768.                                 $goipdb[$the][telid]=$telid[0];
  769.                                 //$goipdb[$the][telid]=$testid++;
  770.                                 //echo "inser: ".$goipdb[$the][telid]." ".$goipdb[$the][tel]." start<br>";
  771.                                 $goipdb[$the]['send']="SEND";
  772. /*                             
  773.                                 $buf="SEND ".$goipdb[$the][messageid]." ".$goipdb[$the][telid]." ".$goipdb[$the][tel]."\n";
  774.  
  775.                                 echo "$buf ($goipnow[name] $goipnow[prov])<br>";
  776.                                 if (socket_sendto($socket,$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  777.                                     echo ("sendto error");
  778. */
  779.                                 $goipdb[$the][timer]=3;
  780.                                 dolastsend( $goipdb[$the],$len,$msg);
  781.                             }
  782.                             elseif($errortels[$goipnow[provider]]){
  783.                                 foreach($errortels[$goipnow[provider]] as $telthe => $nowrow){
  784.                                     if(!in_array($goipnow[id],$nowrow['error'])){
  785.                                         $goipdb[$the][telrow]=$nowrow;
  786.                                         unset($errortels[$goipnow[provider]][$telthe]);        
  787.                                             $goipdb[$the][tel]=$goipdb[$the][telrow][tel];             
  788.                                         $db->query("INSERT INTO sends (messageid,userid,telnum,goipid,provider,recvid,recvlev,msg,total) VALUES ($sendid,$userid,'".$goipdb[$the][tel]."',$goipnow[id],'$goipnow[provider]',".$goipdb[$the][telrow][id].",'".$goipdb[$the][telrow][lev]."','".$goipdb[$the][telrow][msg]."','".$goipdb[$the][telrow][total]."')"); 
  789.                                         $telid=$db->fetch_array($db->query("SELECT LAST_INSERT_ID()"));
  790.            
  791.                                         $goipdb[$the][telid]=$telid[0];
  792.                                         //$goipdb[$the][telid]=$testid++;
  793.                                         //echo "inser: ".$goipdb[$the][telid]." ".$goipdb[$the][tel]." start<br>";
  794.                                         $goipdb[$the]['send']="SEND";
  795. /*
  796.                                         $buf="SEND ".$goipdb[$the][messageid]." ".$goipdb[$the][telid]." ".$goipdb[$the][tel]."\n";
  797.                                         echo "$buf ($goipnow[name] $goipnow[prov])<br>";
  798.                                         if (socket_sendto($socket,$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  799.                                             echo ("sendto error");
  800. */
  801.                                         $goipdb[$the][timer]=3;
  802.                                         dolastsend( $goipdb[$the],$len,$msg);
  803.                                         break;
  804.                                     }
  805.                                 }                          
  806.                             }
  807.                             if(checkover($goipdb)) break 2;
  808.                         }
  809.  
  810.                     }
  811.                     elseif($comm[0]=="WAIT"){
  812.                         echo "WAIT $goipnow[send] $comm[2] ($goipnow[name] $goipnow[prov] $goipnow[tel])<br>";
  813.                         if($goipnow[send]=="SEND" && $comm[2]==$goipnow[telid]){
  814.                             $goipdb[$the][timer]=3;//持續發送
  815.                         }
  816.                     }
  817.                     elseif($comm[0]=="MSG"){ //不應該收到
  818.                     }
  819.                     elseif($comm[0]=="SEND"){
  820.                         //$goipnow['ok']=1;
  821.                         //$goipnow[send]="SEND";       
  822.                         if($goipnow[send]=="SEND")//已經處于發送狀態
  823.                             continue;
  824.                         $goipdb[$the]['send']="OK";//結束了
  825.                         $goipdb[$the][telid]=0;
  826.                         $goipdb[$the][tel]=0;
  827.                         $goipdb[$the][timer]=0;
  828.                         if(!check_sms_remain_count($db,$goipnow[id],$goipnow[name],0)) continue;
  829.                         if(($goipdb[$the][telrow]=array_pop($tels[$goipnow[provider]]))!==NULL){//出來   
  830.                             /*寫入數據庫,得到id, 發送*/             
  831.                                 $goipdb[$the][tel]=$goipdb[$the][telrow][tel];                 
  832.                             $db->query("INSERT INTO sends (messageid,userid,telnum,goipid,provider,recvid,recvlev,msg,total) VALUES ($sendid,$userid,'".$goipdb[$the][tel]."',$goipnow[id],'$goipnow[provider]',".$goipdb[$the][telrow][id].",'".$goipdb[$the][telrow][lev]."','".$goipdb[$the][telrow][msg]."','".$goipdb[$the][telrow][total]."')"); 
  833.                             $telid=$db->fetch_array($db->query("SELECT LAST_INSERT_ID()"));
  834.  
  835.                             $goipdb[$the][telid]=$telid[0];
  836.                             //$goipdb[$the][telid]=$testid++;
  837.                             //echo "inser: ".$goipdb[$the][telid]." ".$goipdb[$the][tel]." start<br>";
  838.                             $goipdb[$the]['send']="SEND";
  839.                             //$buf="SEND ".$goipdb[$the][messageid]." ".$goipdb[$the][telid]." ".$goipdb[$the][tel]."\n";
  840.  
  841. /*
  842.                             echo "$buf ($goipnow[name] $goipnow[prov])<br>";
  843.                             if (socket_sendto($socket,$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  844.                                 echo ("sendto error");
  845. */
  846.                             $goipdb[$the][timer]=3;
  847.                             dolastsend( $goipdb[$the],$len,$msg);
  848.                         }
  849.                         elseif($errortels[$goipnow[provider]]){
  850.                             foreach($errortels[$goipnow[provider]] as $telthe => $nowrow){
  851.                                 if(!in_array($goipnow[id],$nowrow[error])){
  852.                                     $goipdb[$the][telrow]=$nowrow;
  853.                                     unset($errortels[$goipnow[provider]][$telthe]);
  854.                                                
  855.                                     $goipdb[$the][tel]=$goipdb[$the][telrow][tel];                 
  856.                                     $db->query("INSERT INTO sends (messageid,userid,telnum,goipid,provider,recvid,recvlev,msg,total) VALUES ($sendid,$userid,'".$goipdb[$the][tel]."',$goipnow[id],'$goipnow[provider]',".$goipdb[$the][telrow][id].",'".$goipdb[$the][telrow][lev]."','".$goipdb[$the][telrow][msg]."','".$goipdb[$the][telrow][total]."')"); 
  857.                                     $telid=$db->fetch_array($db->query("SELECT LAST_INSERT_ID()"));
  858.        
  859.                                     $goipdb[$the][telid]=$telid[0];
  860.                                     //$goipdb[$the][telid]=$testid++;
  861.                                     //echo "inser: ".$goipdb[$the][telid]." ".$goipdb[$the][tel]." start<br>";
  862.                                     $goipdb[$the]['send']="SEND";
  863. /*
  864.                                     $buf="SEND ".$goipdb[$the][messageid]." ".$goipdb[$the][telid]." ".$goipdb[$the][tel]."\n";
  865.                                     echo "$buf ($goipnow[name] $goipnow[prov])<br>";
  866.                                     if (socket_sendto($socket,$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  867.                                         echo ("sendto error");
  868. */
  869.                                     $goipdb[$the][timer]=3;
  870.                                     dolastsend( $goipdb[$the],$len,$msg);
  871.                                     break;
  872.                                 }
  873.                             }                          
  874.                         }
  875.                         if(checkover($goipdb)) break 2;
  876.                     }
  877.                     elseif($comm[0]=="PASSWORD"){
  878.                         //echo "PASSWORD ".$goipnow['send'];
  879.                         //$teli=substr($comm[1], -1);
  880.                         if($goipnow['send']!="PASSWORD" && $goipnow['send']!="MSG")//不是發送密碼狀態就不處理
  881.                             continue;
  882.                         if($goipnow['send']=="MSG"){
  883.                             $goipdb[$the]['timer']=3;
  884.                             $goipdb[$the]['send']="PASSWORD";
  885.                         }
  886.                         if($goipdb[$the]['send']=="PASSWORD"){
  887.                             if($goipdb[$the]['timer']-- > 0)
  888.                                 dolastsend( $goipdb[$the],$len,$msg);
  889.                             else {
  890.                                 $goipdb[$the]['timer']=$re_ask_timer;
  891.                                 $goipdb[$the]['send']="RMSG";
  892.                             }
  893.                         }
  894.                         /*
  895.                         socket_sendto($socket,"PASSWORD $goipnow[messageid] $goipnow[password]\n", strlen("PASSWORD $comm[1] $goipnow[password]\n"), 0, "127.0.0.1", $port);
  896.                         $goipdb[$the][send]="PASSWORD";
  897.                         $goipdb[$the][timer]=3;
  898.                         */ 
  899.                     }
  900.                     elseif($comm[0]=="ERROR"){
  901.                         echo "<font color='#FF0000'>$buf($goipnow[name] $goipnow[prov] $goipnow[tel] ".$goipdb[$the][send].")</font><br>";
  902.                         if($goipdb[$the][send]=="PASSWORD" && ($comm[2]=="SENDID" || $comm[2]=="GSM_LOGOUT")){
  903.                                 $goipdb[$the]['timer']=$re_ask_timer;
  904.                                 $goipdb[$the]['send']="RMSG";                          
  905.                         }
  906.                         elseif($goipdb[$the][send]=="SEND" && ($comm[2]=="SENDID" || $comm[2]=="GSM_LOGOUT")){//sendid失敗
  907.                             $goipdb[$the]['telrow']['error'][]=$goipdb[$the]['id'];
  908.                             $findokflag=0;
  909.                             foreach($goipdb as $the1 => $goiptmp){
  910.                                 if($goiptmp[send]=="OK" && $goiptmp[provider]==$goipsend[provider] && !in_array($goiptmp[id],$goipdb[$the]['telrow']['error']) ){
  911.                                     $goipdb[$the1][send]="SEND";
  912.                                     $goipdb[$the1][tel]=$goipnow[tel];
  913.                                     $goipdb[$the1][telid]=$goipnow[telid];
  914.                                     $goipdb[$the1][telrow]=$goipdb[$the]['telrow'];
  915.                                     //$goipdb[$the1][telrow]['error']=$goipnow[telrow]['error'];
  916.                                     $goipdb[$the1][timer]=3;
  917.                                     $findokflag=1;
  918.                                     $db->query("update sends set goipid=$goiptmp[id] where id=$goipnow[telid]");
  919.                                     dolastsend( $goipdb[$the1],$len,$msg);
  920.                                     break;
  921.                                 }
  922.                             }
  923.                             if(!$findokflag){
  924.                                 array_push($errortels[$goipsend[provider]], $goipdb[$the][telrow]);
  925.                                 $db->query("delete from sends where id=$goipnow[telid]");
  926.                                 //array_push($tels[$goipsend[provider]], $goipsend[tel]); //壓回
  927.                             }
  928.                            
  929.                             $goipdb[$the][telid]=0;
  930.                             $goipdb[$the][tel]=0;
  931.                             if($comm[2]=="SENDID"){
  932.                                 $goipdb[$the]['send']="MSG";//結束了
  933.                                 $goipdb[$the][timer]=3;
  934.                                 dolastsend($goipdb[$the],$len,$msg);
  935.                             }
  936.                             elseif($comm[2]=="GSM_LOGOUT"){
  937.                                 echo "GSM_LOGOUT!!!!ask after 100S<br>";
  938.                                 $goipdb[$the]['timer']=$re_ask_timer;
  939.                                 $goipdb[$the]['send']="RMSG";                                                                       }
  940.                         }
  941.                         elseif($goipdb[$the][send]=="SEND" && $comm[2]==$goipnow[telid]){//發送失敗
  942.                             $goipdb[$the]['telrow']['error'][]=$goipdb[$the]['id'];
  943.                             $findokflag=0;
  944.                             foreach($goipdb as $the1 => $goiptmp){
  945.                                 if(!check_sms_remain_count($db,$goiptmp['id'],$goiptmp['name'])) continue;
  946.  
  947.                                 if($goiptmp[send]=="OK" && $goiptmp[provider]==$goipsend[provider] && !in_array($goiptmp[id],$goipdb[$the]['telrow']['error']) ){
  948.                                     $goipdb[$the1][send]="SEND";
  949.                                     $goipdb[$the1][tel]=$goipnow[tel];
  950.                                     $goipdb[$the1][telid]=$goipnow[telid];
  951.                                     $goipdb[$the1][telrow]=$goipdb[$the]['telrow'];
  952.                                     //$goipdb[$the1][telrow]['error']=$goipnow[telrow]['error'];
  953.                                     $goipdb[$the1][timer]=3;
  954.                                     $findokflag=1;
  955.                                     $db->query("update sends set goipid=$goiptmp[id] where id=$goipnow[telid]");
  956.                                     dolastsend( $goipdb[$the1],$len,$msg);
  957.                                     break;
  958.                                 }
  959.                             }
  960.                        
  961.                             if(!$findokflag){
  962.                                 array_push($errortels[$goipsend[provider]], $goipdb[$the][telrow]);
  963.                                 $db->query("delete from sends where id=$goipnow[telid]");
  964.                                 //array_push($tels[$goipsend[provider]], $goipsend[tel]); //壓回
  965.                             }
  966.                            
  967.                             $goipdb[$the]['send']="OK";//結束了
  968.                             $goipdb[$the][telid]=0;
  969.                             $goipdb[$the][tel]=0;
  970.                             $goipdb[$the][timer]=0;
  971.                             if(!check_sms_remain_count($db,$goipdb[$the][id],$goipdb[$the][name])) continue;
  972.                             if(($goipdb[$the][telrow]=array_pop($tels[$goipnow[provider]]))!==NULL){//出來   
  973.                                 /*寫入數據庫,得到id, 發送*/
  974.                                                
  975.                                     $goipdb[$the][tel]=$goipdb[$the][telrow][tel];             
  976.                                 $db->query("INSERT INTO sends (messageid,userid,telnum,goipid,provider,recvid,recvlev,msg,total) VALUES ($sendid,$userid,'".$goipdb[$the][tel]."',$goipnow[id],'$goipnow[provider]',".$goipdb[$the][telrow][id].",'".$goipdb[$the][telrow][lev]."','".$goipdb[$the][telrow][msg]."','".$goipdb[$the][telrow][total]."')"); 
  977.                                 $telid=$db->fetch_array($db->query("SELECT LAST_INSERT_ID()"));
  978.    
  979.                                 $goipdb[$the][telid]=$telid[0];
  980.                                 //$goipdb[$the][telid]=$testid++;
  981.                                 //echo "inser: ".$goipdb[$the][telid]." ".$goipdb[$the][tel]." start<br>";
  982.                                 $goipdb[$the]['send']="SEND";
  983. /*
  984.                                 $buf="SEND ".$goipdb[$the][messageid]." ".$goipdb[$the][telid]." ".$goipdb[$the][tel]."\n";
  985.                                 echo "$buf ($goipnow[name] $goipnow[provider])<br>";
  986.                                 if (socket_sendto($socket,$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  987.  
  988.                                     echo ("sendto error");
  989. */
  990.                                 $goipdb[$the][timer]=3;
  991.                                 dolastsend( $goipdb[$the],$len,$msg);
  992.                             }
  993.                             elseif($errortels[$goipnow[provider]]){
  994.                                 foreach($errortels[$goipnow[provider]] as $telthe => $nowrow){
  995.                                     if(!in_array($goipnow[id],$nowrow[error])){
  996.                                         $goipdb[$the][telrow]=$nowrow;
  997.                                         unset($errortels[$goipnow[provider]][$telthe]);        
  998.                                             $goipdb[$the][tel]=$goipdb[$the][telrow][tel]; 
  999.                                         $db->query("INSERT INTO sends (messageid,userid,telnum,goipid,provider,recvid,recvlev,msg,total) VALUES ($sendid,$userid,'".$goipdb[$the][tel]."',$goipnow[id],'$goipnow[provider]',".$goipdb[$the][telrow][id].",'".$goipdb[$the][telrow][lev]."','".$goipdb[$the][telrow][msg]."','".$goipdb[$the][telrow][total]."')"); 
  1000.                                         $telid=$db->fetch_array($db->query("SELECT LAST_INSERT_ID()"));
  1001.            
  1002.                                         $goipdb[$the][telid]=$telid[0];
  1003.                                         //$goipdb[$the][telid]=$testid++;
  1004.                                         //echo "inser: ".$goipdb[$the][telid]." ".$goipdb[$the][tel]." start<br>";
  1005.                                         $goipdb[$the]['send']="SEND";
  1006. /*
  1007.                                         $buf="SEND ".$goipdb[$the][messageid]." ".$goipdb[$the][telid]." ".$goipdb[$the][tel]."\n";
  1008.                                         echo "$buf ($goipnow[name] $goipnow[prov])<br>";
  1009.                                         if (socket_sendto($socket,$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  1010.                                             echo ("sendto error");
  1011. */
  1012.  
  1013.                                         $goipdb[$the][timer]=3;
  1014.                                         dolastsend( $goipdb[$the],$len,$msg);
  1015.                                         break;
  1016.                                     }
  1017.                                 }                          
  1018.                             }
  1019.                             if(checkover($goipdb)) break 2;
  1020.                         }
  1021.                         else if($goipnow[timer]>0){
  1022.                             dolastsend( $goipdb[$the],$len,$msg);
  1023.                             $goipdb[$the][timer]--;
  1024.                         }
  1025.                         else //等待它超時吧
  1026.                             continue;
  1027.                     }//elseif($comm[0]=="ERROR"){
  1028.                     $goipdb[$the]['time']=time();
  1029.                 //}//foreach($bufline as $line){
  1030.               }//foreach($read
  1031.                 $i=0;
  1032.                
  1033.                 $nowtime=time();
  1034.                 foreach($goipdb as $the0 => $goipsend){
  1035.                     $flag=1;
  1036.                     //echo "12346789000000000000000000 $goipsend[time]  $nowtime<br>";
  1037.                     if($goipsend['time'] <$nowtime-$timeout && $goipsend[send]!="OK"){//超時了
  1038.                         $goipdb[$the0]['time']=$nowtime;
  1039.                         if($goipsend[timer]>0){//重試
  1040.                                                     if($goipdb[$the0][send]!="RMSG")  //如果其他已結束,將不等待連不上的GOIP
  1041.                                                             $flag=0;//未完成
  1042.                             dolastsend( $goipdb[$the0],$len,$msg);
  1043.                             $goipdb[$the0][timer]--;
  1044.                             //echo("<br>$i timer:".$goipsend[timer]."<br>");
  1045.                             $i++;
  1046.                         }
  1047.                         else{ //累計失敗
  1048.                             if($goipsend[send]=="SEND"){
  1049.                                 echo "<font color='#FF0000'>$goipnow[name] $goipnow[telid] $goipnow[tel] cannot get answer</font><br>";
  1050.                                 //$db->query("dela");  
  1051.                                 foreach($goipdb as $the => $goiptmp){
  1052.                                     if($goiptmp[send]=="OK" && $goiptmp[provider]==$goipsend[provider]){
  1053.                                         if($goipsend[telrow][error] && in_array($goiptmp[id],$goipsend[telrow][error]))
  1054.                                             continue;//已發送錯誤
  1055.                                         $goipdb[$the][send]="SEND";
  1056.                                         $goipdb[$the][tel]=$goipsend[tel];
  1057.                                         $goipdb[$the][telid]=$goipsend[telid];
  1058.                                         $goipdb[$the0][send]=="OK"; //結束
  1059.                                         dolastsend( $goiptmp,$len,$msg);
  1060.                                         $goipdb[$the0][send]="RMSG";//超時的goip,100s後通訊
  1061.                                         $goipdb[$the0][timer]=$re_ask_timer;
  1062.                                         $goipdb[$the0][tel]=0;
  1063.                                         $goipdb[$the0][telid]=0;
  1064.                                         break;
  1065.                                     }
  1066.                                        
  1067.                                 }
  1068.                             }
  1069.                             else{
  1070.                                 echo "<font color='#FFOO00'>2cannot get response from goip:  $goipsend[send] ($goipsend[name] $goipsend[provider])</font><br>";
  1071.                                 /*100s 後重新通訊*/
  1072.                                 $goipdb[$the0][send]="RMSG";
  1073.                                 $goipdb[$the0][timer]=$re_ask_timer;
  1074.                             }
  1075.                             if($goipsend[send]=="SEND"){//沒有找到空閑的goip,把號碼壓回,100s後重新通訊
  1076.                                 if($goipsend[telrow][error])
  1077.                                     array_push($errortels[$goipsend[provider]], $goipsend[telrow]);//壓回出錯數組
  1078.                                 else
  1079.                                     array_push($tels[$goipsend[provider]], $goipsend[telrow]); //壓回
  1080.                                
  1081.                                 /*刪除數據庫*/
  1082.                                 $db->query("delete from sends where id=$goipsend[telid]"); 
  1083.                                 $goipdb[$the0][send]="RMSG";
  1084.                                 $goipdb[$the0][timer]=$re_ask_timer;
  1085.                                 $goipdb[$the0][tel]=0;
  1086.                                 $goipdb[$the0][telid]=0;                       
  1087.                             }  
  1088.                             if(checkover($goipdb)) break 2;
  1089.                         }
  1090.                     }
  1091.                     //$goipdb[$the0]['time']=$nowtime;
  1092.                    
  1093.                     //else $flag++;//完成
  1094.                 }
  1095.             }//else{ //可讀
  1096.             /*檢查超時*/
  1097.         }//for(;;){
  1098.         foreach($socks as $socket){
  1099.             foreach($goipdb as $the => $goipnow){
  1100.                 //echo "$key => $val\n";
  1101.                 if($goipnow[sock]==$socket){
  1102.                     break;
  1103.                 }  
  1104.             }
  1105.             if($goipnow[sock]==$socket)
  1106.                 socket_sendto($socket,"DONE $goipnow[messageid]\n", strlen("DONE $goipnow[messageid]\n"), 0, "127.0.0.1", $port);                      
  1107.         }
  1108.        
  1109.        
  1110.         $i=0;
  1111.         foreach($tels as $provname => $provtels){
  1112.             foreach($provtels as $send){
  1113.                 $db->query("INSERT INTO sends (messageid,userid,telnum,provider,recvid,recvlev,time,msg,total) VALUES ($sendid,$userid,'".$send[tel]."','$provname',".$send[id].",".$send[lev].",'','".$send[msg]."','".$send[total]."')");
  1114.                 $i++;
  1115.             }  
  1116.         }
  1117.         foreach($errortels as $provname => $provtels){
  1118.             foreach($provtels as $send){
  1119.                 $db->query("INSERT INTO sends (messageid,userid,telnum,provider,recvid,recvlev,time,msg,total) VALUES ($sendid,$userid,'".$send[tel]."','$provname',".$send[id].",".$send[lev].",'','".$send[msg]."', '".$send[total]."')");   
  1120.                 $i++;
  1121.             }
  1122.         }
  1123.         $db->query("update message set over=2 where id=$sendid");
  1124.         echo "All sendings done! Failure:{$i}";
  1125.         echo "<br><br>";
  1126.         echo "<a href=sendinfo.php?id=$sendid target=main><font size=2'>Click me to check details.</font></a>";
  1127.        
  1128. }
  1129. ?>
  1130. </body>
  1131. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement