Problem with script connecting to FTP using passive mode

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
webMasterYoda
Forum Newbie
Posts: 1
Joined: Wed Apr 11, 2007 8:20 am

Problem with script connecting to FTP using passive mode

Post by webMasterYoda »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi people. 

I have a problem. 

I am using a PHP script for hot-spot internet accounting. It is connecting to MikroTik's FTP from which it aquires certain file which contains info about hot-spot users internet usage. 
But, after reconstruction of our network sript stopped working. It's still connecting to FTP server, but cannot get directory and file list. BTW it is working in passive mode. I am sure about this, because I tested it with another FTP client from the server which is hosting this script. 
So, the solution is to force script to connect to MikroTiks FTP server in passive mode. But I am unable to get this done due to my poor knowledge of PHP scripting. 





Here is 2 main files which are important for PHP script 


config.php 
------------

Code: Select all

<?php 
//----------------------------------------------------------------------------------- 
$company = "KulaWireless"; 
$website_title = "KulaWireless Hu2T"; 
$website = "www.kulawireless.net"; 
$website_url = "http://".$website; 
$website_logo = "$website_url/themes/ExtraLite/images/logo.gif"; 
//----------------------------------------------------------------------------------- 
$ftp_server = "10.5.53.1"; 
$ftp_user_name = "user"; 
$ftp_user_pass = "password"; 
$ftp_file = "myprotok_kw5.txt"; 
//----------------------------------------------------------------------------------- 
global $status; 
//----------------------------------------------------------------------------------- 
$local_file = "./temp/xuserdata.dat"; 
$local_tmpf = "./temp/xtmp.dat"; 
//----------------------------------------------------------------------------------- 
global $user_name; 
global $user_pass; 
global $user_upload_limit; 
global $user_download_limit; 
global $user_upload; 
global $user_download; 
global $form_user; 
global $form_pass; 
//----------------------------------------------------------------------------------- 
# Messages 
$msg_invalid_user_or_pass = "Invalid username or password!"; 
$msg_both_parameter = "You must type both parameters!"; 
#Don't change $msg_footnote. It is fully forbidden. 
$msg_footnote = "Hu2T v0.0.3 engine's code is Copyright &copy; 2005 by TuXzA. All Rights Reserved. Hu2T is Mikrotik Hostspot user used traffic monitor."; 
$msg_ftp_cnt_error = "There was a problem. Please try later or contact with system administrator." ; 
$msg_ftp_cnt_ok = "Successfully connected to database."; 
//----------------------------------------------------------------------------------- 
?>



main.php
------------

Code: Select all

<?php 
//----------------------------------------------------------------------------------- 

$ftp_con = ftp_connect($ftp_server); 
$ftp_log_res = @ftp_login($ftp_con, $ftp_user_name, $ftp_user_pass); 
if (@ftp_get($ftp_con, $local_file, $ftp_file, FTP_BINARY)) { 
$status = $msg_ftp_cnt_ok; 
} else { 
$status = $msg_ftp_cnt_error; 
} 
@ftp_close($ftp_con); 


//----------------------------------------------------------------------------------- 
$ujsor = 0; 

$filebe = fopen($local_file, "r"); 
$fileki = fopen($local_tmpf, "w"); 

$text=fread($filebe,filesize($local_file)); 

for ($i=1 ; $i<filesize($local_file); $i++) 
if ((($text[$i-1]!=="\x20") && ($text[$i]=="\x20"))||($text[$i]!=="\x20")) 
{ 
if ($ujsor>3) fwrite($fileki,$text[$i]); 
if ($text[$i]=="\x0A") $ujsor++; 
} 

fclose($fileki); 
fclose($filebe); 

//----------------------------------------------------------------------------------- 
$filebe = fopen($local_tmpf, "r"); 

$text=fread($filebe,filesize($local_tmpf)); 

$text.="\x0A\x0A"; 

$i=0; 

do { 
$userdata=''; 
while (!(($text[$i]=="\x0A") && ($text[$i+1]=="\x0A"))) 
{ 
if ($text[$i]=="\x0A") $i++; 
else if ($text[$i]=="\x2D"); 
else $userdata.="$text[$i]"; 
$i++; 
} 
$i++; 


// print ($userdata); 

//-----------xxxxxxxxxxx 
$xm=1; 
$xa=1; 
$xb=1; 
$xc=1; 
$xd=1; 


$userdata1=$userdata; 
$userdata=str_replace("macaddress", "mcaddress", $userdata); 
if (strcmp($userdata1,$userdata)==0) $xm=0; 
$userdata1=$userdata; 
$userdata=str_replace("limitbytesin", "xxxxxa", $userdata); 
if (strcmp($userdata1,$userdata)==0) $xa=0; 
$userdata1=$userdata; 
$userdata=str_replace("limitbytesout", "xxxxxb", $userdata); 
if (strcmp($userdata1,$userdata)==0) $xb=0; 
$userdata1=$userdata; 
$userdata=str_replace("bytesin", "xxxxxc", $userdata); 
if (strcmp($userdata1,$userdata)==0) $xc=0; 
$userdata1=$userdata; 
$userdata=str_replace("bytesout", "xxxxxd", $userdata); 
if (strcmp($userdata1,$userdata)==0) $xd=0; 

// print ($userdata); 

//-----------xxxxxxxxxxx 


$upoz1 = strpos($userdata, 'name'); 
$upoz2 = strpos($userdata, 'password'); 

$uservag= array ('name=','"'); 
$user_name=str_replace($uservag, "", substr($userdata,$upoz1,$upoz2-$upoz1-1)); 

$ppoz1 = strpos($userdata, 'password'); 
if ($xm==1) $ppoz2 = strpos($userdata, 'mcaddress'); 
else $ppoz2 = strpos($userdata, 'profile'); 

$passvag= array ('password=','"'); 
$user_pass=str_replace($passvag, "", substr($userdata,$ppoz1,$ppoz2-$ppoz1-1)); 

$ulpoz1 = strpos($userdata, 'xxxxxa'); 
$ulpoz2 = strpos($userdata, 'xxxxxb'); 

$ullimitvag= array ('xxxxxa','='); 
$user_upload_limit=str_replace($ullimitvag, "", substr($userdata,$ulpoz1,$ulpoz2-$ulpoz1)); 
$user_upload_limit/=1024*1024; 

$dlpoz1 = strpos($userdata, 'xxxxxb'); 
$dlpoz2 = strpos($userdata, 'uptime'); 

$dllimitvag= array ('xxxxxb','='); 
$user_download_limit=str_replace($dllimitvag, "", substr($userdata,$dlpoz1,$dlpoz2-$dlpoz1)); 
$user_download_limit/=1024*1024; 

$bipoz1 = strpos($userdata, 'xxxxxc'); 
$bipoz2 = strpos($userdata, 'xxxxxd'); 

$bivag= array ('xxxxxc','='); 
$user_upload=str_replace($bivag, "", substr($userdata,$bipoz1,$bipoz2-$bipoz1)); 
$user_upload/=1024*1024; 

$bopoz1 = strpos($userdata, 'xxxxxd'); 
$bopoz2 = strpos($userdata, 'packetsin'); 

$bovag= array ('xxxxxd','='); 
$user_download=str_replace($bovag, "", substr($userdata,$bopoz1,$bopoz2-$bopoz1)); 
$user_download/=1024*1024; 
} 

while( !(($user_name==$form_user)|| ($i == filesize($local_tmpf)) ) ); 

fclose($filebe); 
//----------------------------------------------------------------------------------- 

?>



So, can someone help me? I really cant this done without your help.

Thanks inadvance.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply