Advertisement
Guest User

Untitled

a guest
Dec 19th, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 43.99 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. //          ob_flush();
  555.             die;
  556. }
  557.  
  558.         //print_r($prov);
  559.         $msg=$memo;
  560.         $len=strlen($msg);     
  561.         $nowtime=date ("Y-m-d H:i:s");
  562.         //$sendid=1111;
  563.         //$id=0;
  564.         //$query=$db->query("SELECT prov.*,goip.* FROM goip,prov where prov.id=goip.provider and alive=1 and gsm_status!='LOGOUT' ORDER BY name"); 
  565.         $db->query("update message set over=1 where id=$sendid");
  566.         $query=$db->query("SELECT prov.*,goip.* FROM goip,prov where prov.id=goip.provider ORDER BY name");
  567.         $socks=array();
  568.         $errortels=array();
  569.         $goipdb=array();
  570.         while($goiprow=$db->fetch_array($query)) {
  571.             $goipname[]=$goiprow[provider];
  572.  
  573.             /*把信息傳過去*/         
  574.             if(($goipid && $goiprow[id]==$goipid) || (!$goipid && $tels[$goiprow[provider]])){ //有要發給這個服務商的號碼才通信
  575.                 $errortels[$goiprow[provider]]=array();
  576.                 if (($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) <= 0) {
  577.                     echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n";
  578.                     exit;
  579.                 }
  580.                 $goiprow['sock']=$socket;
  581.                 $goiprow['time']=time();//計時
  582.                 //echo "$goiprow[name], $goiprow[remain_count] $goiprow[remain_count_d]<br>";
  583.                 if( $goiprow['remain_count']==0 || $goiprow['remain_count_d']==0) {
  584.                     echo "GoIP Line($goiprow[name]) remain count is down<br>";
  585.                     continue;
  586.                 }
  587.                 if($goiprow[alive] != 1 || $goiprow[gsm_status] == 'LOGOUT'){
  588.                     $goiprow['timer']=$re_ask_timer;
  589.                                     $goiprow['send']="RMSG";
  590.                     $goiprow['messageid']=$sendid+($goiprow[id] << 16);
  591.                     $goipdb[]=$goiprow;
  592.                     $socks[]=$socket;
  593.                     echo "$goiprow[name] logout, after 100 seconds ask again.<br>";
  594.                     continue;
  595.                 }
  596.                 $goiprow['timer']=3;
  597.                 $goiprow['send']="MSG";
  598.                 $goiprow['messageid']=$sendid+($goiprow[id] << 16);
  599.                 $goipdb[]=$goiprow;
  600.                 echo "$sendid $goiprow[id] $goiprow[messageid] <br>";
  601.                 $buf="START ".$goiprow['messageid']." $goiprow[host] $goiprow[port]\n";
  602.                 if (@socket_sendto($socket,$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  603.                     echo ("sendto error");
  604.                 for($i=0;$i<3;$i++){
  605.                     $read=array($socket);
  606.                     $err=socket_select($read, $write, $except, 5);
  607.                     if($err>0){    
  608.                         if(($n=@socket_recvfrom($socket,$buf,1024,0,$ip,$port1))==false){
  609.                             //echo("recvform error".socket_strerror($ret)."<br>");
  610.                             continue;
  611.                         }
  612.                         else{
  613.                             if($buf=="OK"){
  614.                                 $flag=1;
  615.                                 break;
  616.                             }
  617.                         }
  618.                     }
  619.                 }//for
  620.                 if($i>=3)
  621.                     die("Cannot get response from process named \"goipcron\". please check this process.");
  622.                 $buf="MSG ".$goiprow['messageid']." $len $msg\n";
  623.                 if (@socket_sendto($socket,$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  624.                     echo ("sendto error");
  625.                 $socks[]=$socket;
  626.                 //print_r($goiprow);
  627.             }
  628.             //print_r($goiprow);
  629.         }
  630.         foreach($tels as $provtmp => $valuetmp){
  631.             foreach($goipdb as $goiptmp){
  632.                 if($goiptmp['provider']==$provtmp)
  633.                     continue 2;
  634.             }
  635.             $n=count($valuetmp);
  636.             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>";
  637.         }
  638.         //$read = array($socket);
  639.         $timeout=5;
  640.         for(;;){
  641.             $read=$socks;
  642.             flush();
  643.             if(count($read)==0)
  644.                 break;
  645.             $err=socket_select($read, $write, $except, $timeout);
  646.             //echo "select:$err <br>";
  647.             if($err===false)
  648.                 echo "select error!";
  649.             elseif($err==0){ //全體超時
  650.                 //echo "select timeout";
  651.                 $i=0;
  652.                 $flag=1;
  653.                 $nowtime=time();
  654.                 //reset($goipdb);
  655.                 //while (list (, $goipsend) = each ($goipdb)) {
  656.                 foreach($goipdb as $the0 => $goipsend){
  657.                     //$goipsend=$goipdb[$the0];
  658.                     $goipdb[$the0]['time']=$nowtime;
  659.                     //echo("<br>$i $goipsend[send] timer:".$goipsend[timer]."<br>");
  660.                     if($goipsend[timer]>0){//重試
  661.                        
  662.                         if($goipdb[$the0][send]!="RMSG")  //如果其他已結束,將不等待連不上的GOIP
  663.                             $flag=0;//未完成
  664.                         dolastsend( $goipdb[$the0],$len,$msg);
  665.                         $goipdb[$the0]['timer']--;
  666.                         //echo("<br>$i $goipsend[send] timer:".$goipsend[timer]."<br>");
  667.                         $i++;
  668.                     }
  669.                     else{ //累計失敗
  670.                         if($goipsend[send]=="OK") //已完成的
  671.                             continue;
  672.                        
  673.                         if($goipsend[send]=="SEND"){
  674.                             //echo "inser: $goipnow[telid] $goipnow[tel] faile<br>";
  675.                             echo "<font color='#FF0000'>$goipnow[telid] $goipnow[tel] faile</font><br>";
  676.                             foreach($goipdb as $the => $goiptmp){
  677.                                 if($goiptmp[send]=="OK" && $goiptmp[provider]==$goipsend[provider]){
  678.                                     if($goipsend[telrow][error] && in_array($goiptmp[id],$goipsend[telrow][error]))
  679.                                         continue;//已發送錯誤
  680.                                     $goipdb[$the][send]="SEND";
  681.                                     $goipdb[$the][tel]=$goipsend[tel];
  682.                                     $goipdb[$the][telid]=$goipsend[telid];
  683.                                     //$goipsend[send]=="OK"; //結束
  684.                                     dolastsend( $goiptmp,$len,$msg);
  685.                                     $goipdb[$the0][send]="RMSG";//超時的goip,100s後通訊
  686.                                     $goipdb[$the0][timer]=$re_ask_timer;
  687.                                     $goipdb[$the0][tel]=0;
  688.                                     $goipdb[$the0][telid]=0;
  689.                                     break;
  690.                                 }                                  
  691.                             }
  692.                         }
  693.                         else{
  694.                             /*100s 後重新通訊*/
  695.                             echo "<font color='#FFOO00'>cannot get response from goip: $goipsend[send] ($goipsend[name] $goipsend[prov])</font><br>";
  696.                             $goipdb[$the0][send]="RMSG";
  697.                             $goipdb[$the0][timer]=$re_ask_timer;
  698.                         }
  699.                         if($goipsend[send]=="SEND"){//沒有找到空閑的goip,把號碼壓回,100s後重新通訊
  700.                             if($goipsend[telrow][error])
  701.                                 array_push($errortels[$goipsend[provider]], $goipsend[telrow]);//壓回出錯數組
  702.                             else
  703.                                 array_push($tels[$goipsend[provider]], $goipsend[telrow]); //壓回
  704.                             //array_push($tels[$goipsend[provider]], $goipsend[telrow]); //壓回
  705.                             /*刪除數據庫*/
  706.                             $db->query("delete from sends where id=$goipsend[telid]"); 
  707.                             $goipdb[$the0][send]="RMSG";
  708.                             $goipdb[$the0][timer]=$re_ask_timer;
  709.                             $goipdb[$the0][tel]=0;
  710.                             $goipdb[$the0][telid]=0;                       
  711.                         }  
  712.                     }  
  713.                 }
  714.                
  715.                 if($flag && !$endless_send)
  716.                     break; //全部結束
  717.                
  718.             }//全體超時
  719.             else{ //可讀
  720.            
  721.               foreach($read as $socket){
  722.                 unset($buf);
  723.                 //$buf="";
  724.                
  725.                 if(($n=@socket_recvfrom($socket,$buf,1024,0,$ip,$port1))==false){
  726.                     echo("recvform error".socket_strerror($ret)."<br>");
  727.                     continue;
  728.                 }
  729.                 //echo "hahaha ".$buf;
  730.                   $comm=explode(" ",$buf);
  731.                     //$teli=(int)substr($comm[1], -1)-1;
  732.                     foreach($goipdb as $the => $goipnow){
  733.                         //echo "$key => $val\n";
  734.                         if($goipnow[sock]==$socket){
  735.                             break;
  736.                         }  
  737.                     }
  738.                    
  739.                     if(empty($goipnow)){ //不是期望的套接口
  740.                         continue;
  741.                     }
  742.                     if(strncmp($goipnow[messageid],$comm[1], strlen($goipnow[messageid])))//不是期望的id
  743.                         continue;
  744.                     //echo "$goipnow[name] <br>";
  745.                     if($comm[0]=="OK"){
  746.                         //更新數據庫,發送成功
  747.                         if(is_numeric($comm[3]))
  748.                         $db->query("update sends set over=1,sms_no=$comm[3],goipid=$goipnow[id],time=now() where id='".$goipnow[telid]."' and messageid=$sendid");
  749.                         else
  750.                         $db->query("update sends set over=1,goipid=$goipnow[id],time=now() where id='".$goipnow[telid]."' and messageid=$sendid"); 
  751.                         /**/
  752.                         if($goipnow[send]!="SEND"){//不處于發送狀態,無視
  753.                             //echo "net send status <br>";
  754.                             continue;
  755.                         }  
  756.                         if($comm[2]==$goipnow[telid]){ //是現在發的號碼,可以發下一個了
  757.                             echo "<font color='#00FF00'>$goipnow[telid] ".$goipnow[telrow][name]." $goipnow[tel] ($goipnow[name] $goipnow[prov]) ok</font><br>";
  758.                             $goipdb[$the]['send']="OK";//結束了                 
  759.                             $goipdb[$the][telid]=0;
  760.                             $goipdb[$the][tel]=0;
  761.                             $goipdb[$the][timer]=0;
  762.                             if(!check_sms_remain_count($db,$goipnow[id],$goipnow[name],$goipdb[$the][telrow][total])) continue;
  763.                             if(($goipdb[$the][telrow]=array_pop($tels[$goipnow[provider]]))!==NULL){//出來   
  764.                                 /*寫入數據庫,得到id, 發送*/
  765.                                                
  766.                                     $goipdb[$the][tel]=$goipdb[$the][telrow][tel];
  767.                                 $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]."')");     
  768.                                 $telid=$db->fetch_array($db->query("SELECT LAST_INSERT_ID()"));
  769.                                 $goipdb[$the][telid]=$telid[0];
  770.                                 //$goipdb[$the][telid]=$testid++;
  771.                                 //echo "inser: ".$goipdb[$the][telid]." ".$goipdb[$the][tel]." start<br>";
  772.                                 $goipdb[$the]['send']="SEND";
  773. /*                             
  774.                                 $buf="SEND ".$goipdb[$the][messageid]." ".$goipdb[$the][telid]." ".$goipdb[$the][tel]."\n";
  775.  
  776.                                 echo "$buf ($goipnow[name] $goipnow[prov])<br>";
  777.                                 if (socket_sendto($socket,$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  778.                                     echo ("sendto error");
  779. */
  780.                                 $goipdb[$the][timer]=3;
  781.                                 dolastsend( $goipdb[$the],$len,$msg);
  782.                             }
  783.                             elseif($errortels[$goipnow[provider]]){
  784.                                 foreach($errortels[$goipnow[provider]] as $telthe => $nowrow){
  785.                                     if(!in_array($goipnow[id],$nowrow['error'])){
  786.                                         $goipdb[$the][telrow]=$nowrow;
  787.                                         unset($errortels[$goipnow[provider]][$telthe]);        
  788.                                             $goipdb[$the][tel]=$goipdb[$the][telrow][tel];             
  789.                                         $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]."')"); 
  790.                                         $telid=$db->fetch_array($db->query("SELECT LAST_INSERT_ID()"));
  791.            
  792.                                         $goipdb[$the][telid]=$telid[0];
  793.                                         //$goipdb[$the][telid]=$testid++;
  794.                                         //echo "inser: ".$goipdb[$the][telid]." ".$goipdb[$the][tel]." start<br>";
  795.                                         $goipdb[$the]['send']="SEND";
  796. /*
  797.                                         $buf="SEND ".$goipdb[$the][messageid]." ".$goipdb[$the][telid]." ".$goipdb[$the][tel]."\n";
  798.                                         echo "$buf ($goipnow[name] $goipnow[prov])<br>";
  799.                                         if (socket_sendto($socket,$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  800.                                             echo ("sendto error");
  801. */
  802.                                         $goipdb[$the][timer]=3;
  803.                                         dolastsend( $goipdb[$the],$len,$msg);
  804.                                         break;
  805.                                     }
  806.                                 }                          
  807.                             }
  808.                             if(checkover($goipdb)) break 2;
  809.                         }
  810.  
  811.                     }
  812.                     elseif($comm[0]=="WAIT"){
  813.                         echo "WAIT $goipnow[send] $comm[2] ($goipnow[name] $goipnow[prov] $goipnow[tel])<br>";
  814.                         if($goipnow[send]=="SEND" && $comm[2]==$goipnow[telid]){
  815.                             $goipdb[$the][timer]=3;//持續發送
  816.                         }
  817.                     }
  818.                     elseif($comm[0]=="MSG"){ //不應該收到
  819.                     }
  820.                     elseif($comm[0]=="SEND"){
  821.                         //$goipnow['ok']=1;
  822.                         //$goipnow[send]="SEND";       
  823.                         if($goipnow[send]=="SEND")//已經處于發送狀態
  824.                             continue;
  825.                         $goipdb[$the]['send']="OK";//結束了
  826.                         $goipdb[$the][telid]=0;
  827.                         $goipdb[$the][tel]=0;
  828.                         $goipdb[$the][timer]=0;
  829.                         if(!check_sms_remain_count($db,$goipnow[id],$goipnow[name],0)) continue;
  830.                         if(($goipdb[$the][telrow]=array_pop($tels[$goipnow[provider]]))!==NULL){//出來   
  831.                             /*寫入數據庫,得到id, 發送*/             
  832.                                 $goipdb[$the][tel]=$goipdb[$the][telrow][tel];                 
  833.                             $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]."')"); 
  834.                             $telid=$db->fetch_array($db->query("SELECT LAST_INSERT_ID()"));
  835.  
  836.                             $goipdb[$the][telid]=$telid[0];
  837.                             //$goipdb[$the][telid]=$testid++;
  838.                             //echo "inser: ".$goipdb[$the][telid]." ".$goipdb[$the][tel]." start<br>";
  839.                             $goipdb[$the]['send']="SEND";
  840.                             //$buf="SEND ".$goipdb[$the][messageid]." ".$goipdb[$the][telid]." ".$goipdb[$the][tel]."\n";
  841.  
  842. /*
  843.                             echo "$buf ($goipnow[name] $goipnow[prov])<br>";
  844.                             if (socket_sendto($socket,$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  845.                                 echo ("sendto error");
  846. */
  847.                             $goipdb[$the][timer]=3;
  848.                             dolastsend( $goipdb[$the],$len,$msg);
  849.                         }
  850.                         elseif($errortels[$goipnow[provider]]){
  851.                             foreach($errortels[$goipnow[provider]] as $telthe => $nowrow){
  852.                                 if(!in_array($goipnow[id],$nowrow[error])){
  853.                                     $goipdb[$the][telrow]=$nowrow;
  854.                                     unset($errortels[$goipnow[provider]][$telthe]);
  855.                                                
  856.                                     $goipdb[$the][tel]=$goipdb[$the][telrow][tel];                 
  857.                                     $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]."')"); 
  858.                                     $telid=$db->fetch_array($db->query("SELECT LAST_INSERT_ID()"));
  859.        
  860.                                     $goipdb[$the][telid]=$telid[0];
  861.                                     //$goipdb[$the][telid]=$testid++;
  862.                                     //echo "inser: ".$goipdb[$the][telid]." ".$goipdb[$the][tel]." start<br>";
  863.                                     $goipdb[$the]['send']="SEND";
  864. /*
  865.                                     $buf="SEND ".$goipdb[$the][messageid]." ".$goipdb[$the][telid]." ".$goipdb[$the][tel]."\n";
  866.                                     echo "$buf ($goipnow[name] $goipnow[prov])<br>";
  867.                                     if (socket_sendto($socket,$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  868.                                         echo ("sendto error");
  869. */
  870.                                     $goipdb[$the][timer]=3;
  871.                                     dolastsend( $goipdb[$the],$len,$msg);
  872.                                     break;
  873.                                 }
  874.                             }                          
  875.                         }
  876.                         if(checkover($goipdb)) break 2;
  877.                     }
  878.                     elseif($comm[0]=="PASSWORD"){
  879.                         //echo "PASSWORD ".$goipnow['send'];
  880.                         //$teli=substr($comm[1], -1);
  881.                         if($goipnow['send']!="PASSWORD" && $goipnow['send']!="MSG")//不是發送密碼狀態就不處理
  882.                             continue;
  883.                         if($goipnow['send']=="MSG"){
  884.                             $goipdb[$the]['timer']=3;
  885.                             $goipdb[$the]['send']="PASSWORD";
  886.                         }
  887.                         if($goipdb[$the]['send']=="PASSWORD"){
  888.                             if($goipdb[$the]['timer']-- > 0)
  889.                                 dolastsend( $goipdb[$the],$len,$msg);
  890.                             else {
  891.                                 $goipdb[$the]['timer']=$re_ask_timer;
  892.                                 $goipdb[$the]['send']="RMSG";
  893.                             }
  894.                         }
  895.                         /*
  896.                         socket_sendto($socket,"PASSWORD $goipnow[messageid] $goipnow[password]\n", strlen("PASSWORD $comm[1] $goipnow[password]\n"), 0, "127.0.0.1", $port);
  897.                         $goipdb[$the][send]="PASSWORD";
  898.                         $goipdb[$the][timer]=3;
  899.                         */ 
  900.                     }
  901.                     elseif($comm[0]=="ERROR"){
  902.                         echo "<font color='#FF0000'>$buf($goipnow[name] $goipnow[prov] $goipnow[tel] ".$goipdb[$the][send].")</font><br>";
  903.                         if($goipdb[$the][send]=="PASSWORD" && ($comm[2]=="SENDID" || $comm[2]=="GSM_LOGOUT")){
  904.                                 $goipdb[$the]['timer']=$re_ask_timer;
  905.                                 $goipdb[$the]['send']="RMSG";                          
  906.                         }
  907.                         elseif($goipdb[$the][send]=="SEND" && ($comm[2]=="SENDID" || $comm[2]=="GSM_LOGOUT")){//sendid失敗
  908.                             $goipdb[$the]['telrow']['error'][]=$goipdb[$the]['id'];
  909.                             $findokflag=0;
  910.                             foreach($goipdb as $the1 => $goiptmp){
  911.                                 if($goiptmp[send]=="OK" && $goiptmp[provider]==$goipsend[provider] && !in_array($goiptmp[id],$goipdb[$the]['telrow']['error']) ){
  912.                                     $goipdb[$the1][send]="SEND";
  913.                                     $goipdb[$the1][tel]=$goipnow[tel];
  914.                                     $goipdb[$the1][telid]=$goipnow[telid];
  915.                                     $goipdb[$the1][telrow]=$goipdb[$the]['telrow'];
  916.                                     //$goipdb[$the1][telrow]['error']=$goipnow[telrow]['error'];
  917.                                     $goipdb[$the1][timer]=3;
  918.                                     $findokflag=1;
  919.                                     $db->query("update sends set goipid=$goiptmp[id] where id=$goipnow[telid]");
  920.                                     dolastsend( $goipdb[$the1],$len,$msg);
  921.                                     break;
  922.                                 }
  923.                             }
  924.                             if(!$findokflag){
  925.                                 array_push($errortels[$goipsend[provider]], $goipdb[$the][telrow]);
  926.                                 $db->query("delete from sends where id=$goipnow[telid]");
  927.                                 //array_push($tels[$goipsend[provider]], $goipsend[tel]); //壓回
  928.                             }
  929.                            
  930.                             $goipdb[$the][telid]=0;
  931.                             $goipdb[$the][tel]=0;
  932.                             if($comm[2]=="SENDID"){
  933.                                 $goipdb[$the]['send']="MSG";//結束了
  934.                                 $goipdb[$the][timer]=3;
  935.                                 dolastsend($goipdb[$the],$len,$msg);
  936.                             }
  937.                             elseif($comm[2]=="GSM_LOGOUT"){
  938.                                 echo "GSM_LOGOUT!!!!ask after 100S<br>";
  939.                                 $goipdb[$the]['timer']=$re_ask_timer;
  940.                                 $goipdb[$the]['send']="RMSG";                                                                       }
  941.                         }
  942.                         elseif($goipdb[$the][send]=="SEND" && $comm[2]==$goipnow[telid]){//發送失敗
  943.                             $goipdb[$the]['telrow']['error'][]=$goipdb[$the]['id'];
  944.                             $findokflag=0;
  945.                             foreach($goipdb as $the1 => $goiptmp){
  946.                                 if(!check_sms_remain_count($db,$goiptmp['id'],$goiptmp['name'])) continue;
  947.  
  948.                                 if($goiptmp[send]=="OK" && $goiptmp[provider]==$goipsend[provider] && !in_array($goiptmp[id],$goipdb[$the]['telrow']['error']) ){
  949.                                     $goipdb[$the1][send]="SEND";
  950.                                     $goipdb[$the1][tel]=$goipnow[tel];
  951.                                     $goipdb[$the1][telid]=$goipnow[telid];
  952.                                     $goipdb[$the1][telrow]=$goipdb[$the]['telrow'];
  953.                                     //$goipdb[$the1][telrow]['error']=$goipnow[telrow]['error'];
  954.                                     $goipdb[$the1][timer]=3;
  955.                                     $findokflag=1;
  956.                                     $db->query("update sends set goipid=$goiptmp[id] where id=$goipnow[telid]");
  957.                                     dolastsend( $goipdb[$the1],$len,$msg);
  958.                                     break;
  959.                                 }
  960.                             }
  961.                        
  962.                             if(!$findokflag){
  963.                                 array_push($errortels[$goipsend[provider]], $goipdb[$the][telrow]);
  964.                                 $db->query("delete from sends where id=$goipnow[telid]");
  965.                                 //array_push($tels[$goipsend[provider]], $goipsend[tel]); //壓回
  966.                             }
  967.                            
  968.                             $goipdb[$the]['send']="OK";//結束了
  969.                             $goipdb[$the][telid]=0;
  970.                             $goipdb[$the][tel]=0;
  971.                             $goipdb[$the][timer]=0;
  972.                             if(!check_sms_remain_count($db,$goipdb[$the][id],$goipdb[$the][name])) continue;
  973.                             if(($goipdb[$the][telrow]=array_pop($tels[$goipnow[provider]]))!==NULL){//出來   
  974.                                 /*寫入數據庫,得到id, 發送*/
  975.                                                
  976.                                     $goipdb[$the][tel]=$goipdb[$the][telrow][tel];             
  977.                                 $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]."')"); 
  978.                                 $telid=$db->fetch_array($db->query("SELECT LAST_INSERT_ID()"));
  979.    
  980.                                 $goipdb[$the][telid]=$telid[0];
  981.                                 //$goipdb[$the][telid]=$testid++;
  982.                                 //echo "inser: ".$goipdb[$the][telid]." ".$goipdb[$the][tel]." start<br>";
  983.                                 $goipdb[$the]['send']="SEND";
  984. /*
  985.                                 $buf="SEND ".$goipdb[$the][messageid]." ".$goipdb[$the][telid]." ".$goipdb[$the][tel]."\n";
  986.                                 echo "$buf ($goipnow[name] $goipnow[provider])<br>";
  987.                                 if (socket_sendto($socket,$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  988.  
  989.                                     echo ("sendto error");
  990. */
  991.                                 $goipdb[$the][timer]=3;
  992.                                 dolastsend( $goipdb[$the],$len,$msg);
  993.                             }
  994.                             elseif($errortels[$goipnow[provider]]){
  995.                                 foreach($errortels[$goipnow[provider]] as $telthe => $nowrow){
  996.                                     if(!in_array($goipnow[id],$nowrow[error])){
  997.                                         $goipdb[$the][telrow]=$nowrow;
  998.                                         unset($errortels[$goipnow[provider]][$telthe]);        
  999.                                             $goipdb[$the][tel]=$goipdb[$the][telrow][tel]; 
  1000.                                         $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]."')"); 
  1001.                                         $telid=$db->fetch_array($db->query("SELECT LAST_INSERT_ID()"));
  1002.            
  1003.                                         $goipdb[$the][telid]=$telid[0];
  1004.                                         //$goipdb[$the][telid]=$testid++;
  1005.                                         //echo "inser: ".$goipdb[$the][telid]." ".$goipdb[$the][tel]." start<br>";
  1006.                                         $goipdb[$the]['send']="SEND";
  1007. /*
  1008.                                         $buf="SEND ".$goipdb[$the][messageid]." ".$goipdb[$the][telid]." ".$goipdb[$the][tel]."\n";
  1009.                                         echo "$buf ($goipnow[name] $goipnow[prov])<br>";
  1010.                                         if (socket_sendto($socket,$buf, strlen($buf), 0, "127.0.0.1", $port)===false)
  1011.                                             echo ("sendto error");
  1012. */
  1013.  
  1014.                                         $goipdb[$the][timer]=3;
  1015.                                         dolastsend( $goipdb[$the],$len,$msg);
  1016.                                         break;
  1017.                                     }
  1018.                                 }                          
  1019.                             }
  1020.                             if(checkover($goipdb)) break 2;
  1021.                         }
  1022.                         else if($goipnow[timer]>0){
  1023.                             dolastsend( $goipdb[$the],$len,$msg);
  1024.                             $goipdb[$the][timer]--;
  1025.                         }
  1026.                         else //等待它超時吧
  1027.                             continue;
  1028.                     }//elseif($comm[0]=="ERROR"){
  1029.                     $goipdb[$the]['time']=time();
  1030.                 //}//foreach($bufline as $line){
  1031.               }//foreach($read
  1032.                 $i=0;
  1033.                
  1034.                 $nowtime=time();
  1035.                 foreach($goipdb as $the0 => $goipsend){
  1036.                     $flag=1;
  1037.                     //echo "12346789000000000000000000 $goipsend[time]  $nowtime<br>";
  1038.                     if($goipsend['time'] <$nowtime-$timeout && $goipsend[send]!="OK"){//超時了
  1039.                         $goipdb[$the0]['time']=$nowtime;
  1040.                         if($goipsend[timer]>0){//重試
  1041.                                                     if($goipdb[$the0][send]!="RMSG")  //如果其他已結束,將不等待連不上的GOIP
  1042.                                                             $flag=0;//未完成
  1043.                             dolastsend( $goipdb[$the0],$len,$msg);
  1044.                             $goipdb[$the0][timer]--;
  1045.                             //echo("<br>$i timer:".$goipsend[timer]."<br>");
  1046.                             $i++;
  1047.                         }
  1048.                         else{ //累計失敗
  1049.                             if($goipsend[send]=="SEND"){
  1050.                                 echo "<font color='#FF0000'>$goipnow[name] $goipnow[telid] $goipnow[tel] cannot get answer</font><br>";
  1051.                                 //$db->query("dela");  
  1052.                                 foreach($goipdb as $the => $goiptmp){
  1053.                                     if($goiptmp[send]=="OK" && $goiptmp[provider]==$goipsend[provider]){
  1054.                                         if($goipsend[telrow][error] && in_array($goiptmp[id],$goipsend[telrow][error]))
  1055.                                             continue;//已發送錯誤
  1056.                                         $goipdb[$the][send]="SEND";
  1057.                                         $goipdb[$the][tel]=$goipsend[tel];
  1058.                                         $goipdb[$the][telid]=$goipsend[telid];
  1059.                                         $goipdb[$the0][send]=="OK"; //結束
  1060.                                         dolastsend( $goiptmp,$len,$msg);
  1061.                                         $goipdb[$the0][send]="RMSG";//超時的goip,100s後通訊
  1062.                                         $goipdb[$the0][timer]=$re_ask_timer;
  1063.                                         $goipdb[$the0][tel]=0;
  1064.                                         $goipdb[$the0][telid]=0;
  1065.                                         break;
  1066.                                     }
  1067.                                        
  1068.                                 }
  1069.                             }
  1070.                             else{
  1071.                                 echo "<font color='#FFOO00'>2cannot get response from goip:  $goipsend[send] ($goipsend[name] $goipsend[provider])</font><br>";
  1072.                                 /*100s 後重新通訊*/
  1073.                                 $goipdb[$the0][send]="RMSG";
  1074.                                 $goipdb[$the0][timer]=$re_ask_timer;
  1075.                             }
  1076.                             if($goipsend[send]=="SEND"){//沒有找到空閑的goip,把號碼壓回,100s後重新通訊
  1077.                                 if($goipsend[telrow][error])
  1078.                                     array_push($errortels[$goipsend[provider]], $goipsend[telrow]);//壓回出錯數組
  1079.                                 else
  1080.                                     array_push($tels[$goipsend[provider]], $goipsend[telrow]); //壓回
  1081.                                
  1082.                                 /*刪除數據庫*/
  1083.                                 $db->query("delete from sends where id=$goipsend[telid]"); 
  1084.                                 $goipdb[$the0][send]="RMSG";
  1085.                                 $goipdb[$the0][timer]=$re_ask_timer;
  1086.                                 $goipdb[$the0][tel]=0;
  1087.                                 $goipdb[$the0][telid]=0;                       
  1088.                             }  
  1089.                             if(checkover($goipdb)) break 2;
  1090.                         }
  1091.                     }
  1092.                     //$goipdb[$the0]['time']=$nowtime;
  1093.                    
  1094.                     //else $flag++;//完成
  1095.                 }
  1096.             }//else{ //可讀
  1097.             /*檢查超時*/
  1098.         }//for(;;){
  1099.         foreach($socks as $socket){
  1100.             foreach($goipdb as $the => $goipnow){
  1101.                 //echo "$key => $val\n";
  1102.                 if($goipnow[sock]==$socket){
  1103.                     break;
  1104.                 }  
  1105.             }
  1106.             if($goipnow[sock]==$socket)
  1107.                 socket_sendto($socket,"DONE $goipnow[messageid]\n", strlen("DONE $goipnow[messageid]\n"), 0, "127.0.0.1", $port);                      
  1108.         }
  1109.        
  1110.        
  1111.         $i=0;
  1112.         foreach($tels as $provname => $provtels){
  1113.             foreach($provtels as $send){
  1114.                 $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]."')");
  1115.                 $i++;
  1116.             }  
  1117.         }
  1118.         foreach($errortels as $provname => $provtels){
  1119.             foreach($provtels as $send){
  1120.                 $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]."')");   
  1121.                 $i++;
  1122.             }
  1123.         }
  1124.         $db->query("update message set over=2 where id=$sendid");
  1125.         echo "All sendings done! Failure:{$i}";
  1126.         echo "<br><br>";
  1127.         echo "<a href=sendinfo.php?id=$sendid target=main><font size=2'>Click me to check details.</font></a>";
  1128.        
  1129. }
  1130. ?>
  1131. </body>
  1132. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement