Jump to content

TomT

Members
  • Posts

    48
  • Joined

  • Last visited

Everything posted by TomT

  1. Thanks using fgets has helped and looks like it could work in this scenario.
  2. Ideally I'd like to do this as a self contained script, not calling any external applications. The code I've got works, but does't display the results one by one..
  3. Hi I'm trying to watch a plain text file for any changes. I want to use PHP and read the file line by line as new lines are added. Some times 1 line may be added, other times dozens of lines could be added. I want to read each line and check if it matches any keywords.. This is what I've got so far, but it's not line by line... it's buffer size based. <?php $file='log.txt'; $line = $lastpos = 0; while (true) { usleep(500000); clearstatcache(false, $file); $len = filesize($file); if ($len < $lastpos) { $lastpos = $len; } elseif ($len > $lastpos) { $f = fopen($file, "rb"); if ($f === false) die(); fseek($f, $lastpos); while (!feof($f)) { $buffer = fread($f, 4096); if ($lastpos > 0) { if (strpos($buffer, 'MATCH') !== false) echo "$line\r\n"; } flush(); $line++; } $lastpos = ftell($f); fclose($f); } } ?> Can anyone help point me in the right direction. Ideally I don't want to keep the log in memory as it could grow to a reasonable size. Thanks
  4. Thanks this is now working and returning the results correctly Array ( [5] => gmail-smtp-in.l.google.com [10] => alt1.gmail-smtp-in.l.google.com [20] => alt2.gmail-smtp-in.l.google.com [30] => alt3.gmail-smtp-in.l.google.com [40] => alt4.gmail-smtp-in.l.google.com ) Thanks much appreciated.
  5. Thanks that makes sense !
  6. Thanks I've copied the Net_DNS files across and it is working.. This is the test code I'm using: <?php require_once 'Net/DNS.php'; $resolver = new Net_DNS_Resolver(); $response = $resolver->rawQuery('gmail.com', 'MX'); if ($response) { if (count($response->answer)) { foreach ($response->answer as $rr) { $rr->display(); } } } ?> and it returns: gmail.com. 3524 IN MX 30 alt3.gmail-smtp-in.l.google.com. gmail.com. 3524 IN MX 10 alt1.gmail-smtp-in.l.google.com. gmail.com. 3524 IN MX 40 alt4.gmail-smtp-in.l.google.com. gmail.com. 3524 IN MX 20 alt2.gmail-smtp-in.l.google.com. gmail.com. 3524 IN MX 5 gmail-smtp-in.l.google.com. Which is all good, How can I change this so it only returns the preference and the address ? Doing a var_dump on $rr returns : object(net_dns_rr_mx)( { ["name"]=> string(9) "gmail.com" ["type"]=> string(2) "MX" ["class"]=> string(2) "IN" ["ttl"]=> int(3365) ["rdlength"]=> int(32) ["rdata"]=> string(32) "alt3 gmail-smtp-inlgoogleÀ" ["preference"]=> int(30) ["exchange"]=> string(31) "alt3.gmail-smtp-in.l.google.com" } object(net_dns_rr_mx)( { ["name"]=> string(9) "gmail.com" ["type"]=> string(2) "MX" ["class"]=> string(2) "IN" ["ttl"]=> int(3365) ["rdlength"]=> int(9) ["rdata"]=> string(9) " alt1À." ["preference"]=> int(10) ["exchange"]=> string(31) "alt1.gmail-smtp-in.l.google.com" } object(net_dns_rr_mx)( { ["name"]=> string(9) "gmail.com" ["type"]=> string(2) "MX" ["class"]=> string(2) "IN" ["ttl"]=> int(3365) ["rdlength"]=> int(9) ["rdata"]=> string(9) "(alt4À." ["preference"]=> int(40) ["exchange"]=> string(31) "alt4.gmail-smtp-in.l.google.com" } object(net_dns_rr_mx)( { ["name"]=> string(9) "gmail.com" ["type"]=> string(2) "MX" ["class"]=> string(2) "IN" ["ttl"]=> int(3365) ["rdlength"]=> int(9) ["rdata"]=> string(9) "alt2À." ["preference"]=> int(20) ["exchange"]=> string(31) "alt2.gmail-smtp-in.l.google.com" } object(net_dns_rr_mx)( { ["name"]=> string(9) "gmail.com" ["type"]=> string(2) "MX" ["class"]=> string(2) "IN" ["ttl"]=> int(3365) ["rdlength"]=> int(4) ["rdata"]=> string(4) "À." ["preference"]=> int(5) ["exchange"]=> string(26) "gmail-smtp-in.l.google.com" } I've tried to echo $rr['answer']['preference'] and $rr['preference']but that doesn't return anything. Thank again for all your help
  7. Unfortunately I don't have the ability to add any new packages to this box. I'm still think the original script does get the correct data, but is formatting it wrong.. I'll try to find time to day to test this again.
  8. Hi It's Linux based and I've no idea what. There are no tools on it to compile or make a new version of PHP. I was hoping to enable getmxrr as that should be supported by PHP4, but I can't see how to do that. I've just found this, which from the comments sounds like it should work, but it returns nothing for me. http://stackoverflow.com/questions/24965134/raw-socket-dns-request-in-php# Any ideas ? Thanks all help is very much appreciated.
  9. It's only connecting to the internet to send mail, it will be firewalled and port restricted. I'll try the wireshark option, only issue will be how I decode the resulting packets. can getmxrr() be enabled in php.ini, or does it need to be compiled in ? Thanks
  10. Hi. Thanks for the reply. I've running this on a very old closed source box running 4.3.10 that doesn't support getmxrr. I can dumb the box but I'd like to try and use it for one task.. There is a chr(15) in there with is the MX query. When I added var_dump(substr($this->dns_reply, $this->cIx-1, $bytes)); The output was the correct data as individual strings, it seems to be how this is reassembled is wrong in how it joins the data back up. I'd be grateful for any help with this. Thanks
  11. Hi I have an old php4 box that I'm trying to make some use of. I don't have nslookup, php getmxrr etc. I found on the php.net site a function to return the MX records for a domain. In this example I'm using gmail, but I get different results compared to getmxrr(). getmxrr shows: Array ( [0] => alt1.gmail-smtp-in.l.google.com [1] => alt3.gmail-smtp-in.l.google.com [2] => alt2.gmail-smtp-in.l.google.com [3] => gmail-smtp-in.l.google.com [4] => alt4.gmail-smtp-in.l.google.com ) yet this function returns: Array ( [0] => google.com [1] => alt3.google.com [2] => alt4.google.com [3] => alt2.google.com [4] => alt2.google.com.google.com ) The code below shows how I've called getmxrr and the function, can anyone advise if it's possible to get the results to match getmxrr ?? It's querying the same server on port 53. <?php getmxrr ( "gmail.com", $mxhosts); echo "<pre>"; print_r($mxhosts); echo "</pre>"; class mxlookup { var $dns_socket = NULL; var $QNAME = ""; var $dns_packet= NULL; var $ANCOUNT = 0; var $cIx = 0; var $dns_repl_domain; var $arrMX = array(); function mxlookup($domain, $dns="8.8.8.8") { $this->QNAME($domain); $this->pack_dns_packet(); $dns_socket = fsockopen("udp://$dns", 53); fwrite($dns_socket,$this->dns_packet,strlen($this->dns_packet)); $this->dns_reply = fread($dns_socket,1); $bytes = stream_get_meta_data($dns_socket); $this->dns_reply .= fread($dns_socket,$bytes['unread_bytes']); fclose($dns_socket); $this->cIx=6; $this->ANCOUNT = $this->gord(2); $this->cIx+=4; $this->parse_data($this->dns_repl_domain); $this->cIx+=7; for($ic=1;$ic<=$this->ANCOUNT;$ic++) { $QTYPE = ord($this->gdi($this->cIx)); if($QTYPE!==15){print("[MX Record not returned]"); die();} $this->cIx+=9; $mxPref = ord($this->gdi($this->cIx)); $this->parse_data($curmx); $this->arrMX[] = $curmx; $this->cIx+=3; } } function parse_data(&$retval) { $arName = array(); $byte = ord($this->gdi($this->cIx)); while($byte!==0) { if($byte==192) //compressed { $tmpIx = $this->cIx; $this->cIx = ord($this->gdi($cIx)); $tmpName = $retval; $this->parse_data($tmpName); $retval=$retval.".".$tmpName; $this->cIx = $tmpIx+1; return; } $retval=""; $bCount = $byte; for($b=0;$b<$bCount;$b++) { $retval .= $this->gdi($this->cIx); } $arName[]=$retval; $byte = ord($this->gdi($this->cIx)); } $retval=join(".",$arName); } function gdi(&$cIx,$bytes=1) { $this->cIx++; return(substr($this->dns_reply, $this->cIx-1, $bytes)); } function QNAME($domain) { $dot_pos = 0; $temp = ""; while($dot_pos=strpos($domain,".")) { $temp = substr($domain,0,$dot_pos); $domain = substr($domain,$dot_pos+1); $this->QNAME .= chr(strlen($temp)).$temp; } $this->QNAME .= chr(strlen($domain)).$domain.chr(0); } function gord($ln=1) { $reply=""; for($i=0;$i<$ln;$i++){ $reply.=ord(substr($this->dns_reply,$this->cIx,1)); $this->cIx++; } return $reply; } function pack_dns_packet() { $this->dns_packet = chr(0).chr(1). chr(1).chr(0). chr(0).chr(1). chr(0).chr(0). chr(0).chr(0). chr(0).chr(0). $this->QNAME. chr(0).chr(15). chr(0).chr(1); } } /* Exampe of use: */ $mx = new mxlookup("gmail.com"); echo "<pre>"; print_r($mx->arrMX); echo "</pre>"; ?> adding a var_dump to the gdi function is showing the correct DNS entries, so it's something to do with how the script is joining the results back together. function gdi(&$cIx,$bytes=1) { $this->cIx++; var_dump(substr($this->dns_reply, $this->cIx-1, $bytes)); return(substr($this->dns_reply, $this->cIx-1, $bytes)); } anyone any ideas ? Thanks Thanks
  12. Thanks. I'll test that and see what it does for me
  13. Doing this seems to have worked: function custom_sort($a, $b) { return $b['id'] - $a['id']; }
  14. Thanks using usort works perfectly One last question, how easy is it to do the same but sort in reverse order ? Thanks
  15. Hi My array is made using : $group[] = array("ctime"=>$ctime,"name"=>$name that results in : [4]=> array(2) { ["ctime"]=> int(1334597042) ["name"]=> string(7) "John.dat" } [3]=> array(2) { ["ctime"]=> int(1334935095) ["name"]=> string(4) "mark" } [2]=> array(2) { ["ctime"]=> int(1340634768) ["name"]=> string(7) "lol.txt" } [1]=> array(2) { ["ctime"]=> int(1334597041) ["name"]=> string( "dave.txt" } [0]=> array(2) { ["ctime"]=> int(1335193200) ["name"]=> string(4) "paul" } How would I sort this array ascending based on the ctime values ? Thanks
  16. Thanks that works well apart from one issue. My array isn't [0] => Val, [1] => val1, [2] => val2 etc the key is a filectime, how do I access that using $group[$i] ??? Thanks
  17. Hi I'm not using a database. The $group is built from a search of files across multiple locations.
  18. I have a simple foreach loop foreach ($group as $key => $val) { echo $val . "<br/>"; } This can return upto 500 entries. Is there any simple way to show 25 or 50 on a page with next page / previous page links ? Thanks
  19. Hi can anyone help with this ? Thanks
  20. Thanks for the replies. I can create the checkbox and get the value from it.. The problem I have is more with setting and unsetting the session and also showing the state of the checkbox depending how the session is set. Can someone help with this ? Thanks
  21. Hi I'm hoping someone can point me in the right direction. I'm trying to create a single page that contains a couple of checkboxes. When the first checkbox is checked I want to set a session. When the second chechbox is checked I want to set a different session. When the page submits ( to itself ) I'd like the checkboxes to reflect if they are checked or not. I can sort of do that part,but I'm having major issues with unsetting the sessions and updating the checkboxes to reflect this. Does anyone have a simple example on how to do this. Many Thanks
  22. Hi I'm using a couple of queries to search a database. This works well but is slow as I'm cascading/nesting the queries.. Below is an example of what I mean... (LDAP queries) Code: <? $dn = "cn=user"; $filter="(objectclass=user)"; $justthese = array('cn','telephone','guid','ID','email'); $sr=ldap_search($ds, $dn, $filter, $justthese); $info = ldap_get_entries($ds, $sr); for ($n=0; $n<$info['count']; $n++) { $cn = $info[$n]['cn'][0]; $email = $info[$n]['email'][0]; $id= $info[$n]['id'][0]; $dn = "cn=Device"; $filter="(objectclass=device)(userid=$id)"; $justthese = array('cn','system','guid','ID','userid'); $sr=ldap_search($ds, $dn, $filter, $justthese); $info = ldap_get_entries($ds, $sr); for ($n=0; $n<$info['count']; $n++) { $system = $info[$n]['system'][0]; $userid = $info[$n]['userid'][0]; } } ?> I'm sure there's a better way of writing this... that will make it faster. Thanks
  23. Can anyone help me with this ? Thanks
  24. I've amended the script below and it works as expected. What I would like to do is update it so I can move messages from INBOX to various IMAP folders based on specific criteria: ie: Subject contains **SPAM** move to 'Junk' TO contains Tom move to 'Inbox.Tom' FROM contains [email protected] move to Inbox.Ebay think you get the idea This my starting code: Code: <?php /* connect to imap */ $hostname = '{127.0.0.1:143}INBOX'; $username = 'user'; $password = 'pass'; /* try to connect */ $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to $hostname: ' . imap_last_error()); /* grab emails */ $emails = imap_search($inbox,'ALL'); /* if emails are returned, cycle through each... */ if($emails) { /* put the newest emails on top */ rsort($emails); /* for every email... */ foreach($emails as $email_number) { /* get information specific to this email */ $overview = imap_fetch_overview($inbox,$email_number,0); $msg_id=$overview[0]->msgno; $subj=$overview[0]->subject; imap_mail_move($inbox,$msg_id,'INBOX.New'); } } /* close the connection */ imap_close($inbox); ?> Any Ideas ? Thanks
  25. Thanks..problem is I don't know how to do this !! please can some one offer a bit of coding and advice. Thanks
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.