I want to build a tab delimited datafile. I am using regular expressions to extract the required info i.e. registrant name, registrant email and domain name. I want the code outputs results for each domain lookup in tab delimited format. Can you please give me hints about how I can achieve this?
<?php
require 'Net/Whois.php';
$server = 'whois.networksolutions.com';
$content=file('query-file.txt');
foreach ($content as $query)
{
$whois = new Net_Whois;
$data = $whois->query($query, $server);
if (preg_match_all('/^registrant name: (.*)/im', $data, $matches, PREG_SET_ORDER))
// Print the matches:
{
echo '<pre>' . $matches[0][1] . '</pre>';
} else {
echo 'not found!</p>';
}
if (preg_match_all('/^registrant email: (.*)/im', $data, $email, PREG_SET_ORDER))
// Print the matches:
{
echo '<pre>' . $email[0][1] . '</pre>';
} else {
echo 'not found!</p>';
}
if (preg_match_all('/^Domain Name: (.*)/im', $data, $email, PREG_SET_ORDER))
// Print the matches:
{
echo '<pre>' . $email[0][1] . '</pre>';
} else {
echo 'not found!</p>';
}
}
?>
output of the code
Dns Admin
dns-admin#google.com
google.com
not found!
not found!
not found!
Domain Name Manager
tmgroup#turner.com
cnn.com
Domain Administrator
domains#microsoft.com
msn.com
Domain Administrator
domains#microsoft.com
hotmail.com
Domain Administrator
domainadmin#yahoo-inc.com
yahoo.com
DNS Admin
gmail-abuse#google.com
gmail.com
DESIRED OUTPUT SHOULD LOOK LIKE
Dns Admin dns-admin#google.com google.com
Domain Name Manager tmgroup#turner.com cnn.com
Domain Administrator domains#microsoft.com msn.com
...
Instead of echo save the data to an array for each of the if statements:
$data[] = $matches[0][1];
else
$data[] = 'not found!';
Then at the end of the loop (after the last if), join the data with tabs:
$lines[] = implode("\t", $data) . "\n";
Then after the loop has finished (}) save the lines to a file:
file_put_contents('path/to/file.txt', $lines);
Related
Below code works great with getting only a bunch IP address and add it to Cacti --description='".$dev."' --ip='".$dev."', now I want to add bunch of IP with their description in cacti forexample(--description='".$hostname."' --ip='".$dev."').
I am newbie to PHP and don't know how to add 2 values instead of 1 value in PHP.
devices.txt
1.1.1.1
add_device_bulk.php
if ($handle) {
while (($line = fgets($handle)) !== false) {
$line = chop($line);
print "[".$line."] \n";
createHost($line);
}
} else {
die("Could not open file $filename!");
}
die;
function createHost($dev)
{
global $community;
global $hosttemplate;
print "================== Creating Node & Graph for $dev =======================\n";
$ret = cmd("/usr/bin/php add_device.php --quiet --description='".$dev."' --ip='".$dev."' --template=$hosttemplate --community='".$community."' --avail=snmp ");
//Get host id from: [RET] Success - new device-id: (20)
if (preg_match("/\((\d+)\)/", $ret, $matches))
{
$deviceID = $matches[1];
print "Device ID: $deviceID \n";
//We got a device - create graphs for device
$ret = cmd("/usr/bin/php add_graphs.php --graph-type=ds --graph-template-id=5 --host-id=".$deviceID." --snmp-query-id=1 --snmp-query-type-id=10 --snmp-field=ifOperStatus --snmp-value-regex=Up --snmp-field=ifDescr --snmp-value-regex='GigabitEthernet'");
//RET: Graph Added - graph-id: (34) - data-source-ids: (37, 37)
if (preg_match("/\((\d+)\)/", $ret, $matches)) {
$graphID = $matches[1];
//We got a graph - add it to default tree
# cmd("/usr/bin/php /cacti/appl/cacti/cli/add_tree.php --type=node --node-type=graph --tree-id=1 --graph-id=".$graphID);
}
}
}
function cmd($cmd)
{
print "[CMD] $cmd\n";
$ret = exec($cmd)."\n";
print "[RET] $ret\n";
return $ret;
}
Now I want to add description instead of IP and have a text file as below:
devices.txt
Link1, 1.1.1.1
I'm trying to fetch the email address from email account. Following script is working fine for me.
$mbox = imap_open("{mail.b******n.com:143/novalidate-cert}", "myEmail#b******n.com", "myPassWord");
$headers = imap_headers($mbox);
print_r($headers);
if ($headers == false) {
echo "Call failed<br />\n";
} else {
foreach ($headers as $val) {
echo $val . "<br />\n";
}
}
imap_close($mbox);
Above code is returning date, email address (if no name) and subject.
Example :
[1118] => 1119)13-May-2016 Facebook You have more friends on (16765 chars)
[1192] => 1193)25-May-2016 John Re: B****c Website Feedba (27152 chars)
[1224] => 1225)30-May-2016 k****n#b***n.c DSR is not submitted prop (83005 chars)
Means above code returning the email if email has no From: Name. So is there any way to fetch the email addresses only? I would like to appreciate if someone guide me regarding this. Thank You
You might want to fetch message information with imap_fetch_overview:
$MC = imap_check($mbox);
$result = imap_fetch_overview($mbox,"1:{$MC->Nmsgs}",0);
foreach ($result as $overview) {
echo "#{$overview->msgno} ({$overview->date}) - From: {$overview->from}
{$overview->subject}\n";
}
imap_close($mbox);
I've a log file that reads as
text message sent to client 1234 and mobileNo: 987654
Message: This is working
Confirmation No: ab123
text message sent to client 4321 and mobileNo: 456789
Message: This is not working
Confirmation No:
I have to get an alert if there is no confirmation number (i.e. Confirmation No: )
I've written the following code:
preg_match('/Confirmation No:\s*$/', $logLine, $match);
$matchLine = implode(" ",$match);
if ($matchLine == NULL) {
echo "There is an empty Confirmation No";
} else {
echo "Confirmation No: " . $matchLine;
}
This works fine and prints "There is an empty Confirmation No", if there is no confirmation.
But I would like to add clientID and MobileNo in the output too, i.e. There is an empty confirmation for client 4321 on MobileNo: 456789.
Can someone please advise, how can I do that?
Thanks in advance
preg_match("/Confirmation\sNo:\s(.*)..*client\s(\d+)\sand\smobileNo:\s(\d+)/ms", $string, $match);
if ($match[1] == "") {
echo "There is an empty Confirmation No for client " . $match[2] . " on MobileNo: " . $match[3];
} else {
echo "Confirmation No: " . $match[1];
}
https://regex101.com/r/qU7aP2/1
EDIT: I just noticed, maybe it's the other way around??
https://regex101.com/r/oY4hD9/1
$pattern = "/client\s(\d+)\sand\smobileNo:\s(\d+)..*Confirmation\sNo:\s(\w+)/ms";
I wasn't sure how the string was built.
Not that if this is the regex pattern you need then the code $match[x] needs to change places.
Here is how I'd do the job:
// within the loop on the log file
if (preg_match('/\b(client \d*) and \b(mobileNo: \d*)/', $logLine, $m)) {
$client = 'There is an empty confirmation for '.$m[1].' on '.$m[2];
}
if (preg_match('/Confirmation No: (\w*)$/', $logLine, $m) {
if (empty($m[1])) {
echo $client;
} else {
echo $logLine;
}
$client = '';
}
I would like to show comments author google profile picture, if he entered a gmail.com email address, else I will show the gravatar in comments.
With my limitation in coding I managed to put sample code to construct further:
function comment_image() {
$email = get_avatar(get_comment_author_email());
$domains = array('gmail.com', 'google.com');
$pattern = "/^[a-z0-9._%+-]+#[a-z0-9.-]*(" . implode('|', $domains) . ")$/i";
if (preg_match($pattern, $email)) {
function email_to_userid() {
// get user id of the email address - xyz#gmail.com
//request google profile image url eg: https://www.googleapis.com/plus/v1/people/123456789?fields=image&key={API_KEY}
// above will retun URL: "url": "https://lh3.googleusercontent.com/-abcdef/bbbbbas/photo.jpg?sz=50"
// return the image URL
}
}
} elseif; {
echo get_avatar($comment, 60);
}
I will call the above function in my comments template to show the image:
<?php echo comments_image(); ?>
Thanks in advance for this great community.
If you problem is purely syntactical, this should help:
function comments_image() {
$email = get_avatar(get_comment_author_email());
$domains = array('gmail.com', 'google.com');
$pattern = "/^[a-z0-9._%+-]+#[a-z0-9.-]*(" . implode('|', $domains) . ")$/i";
if (preg_match($pattern, $email)) {
email_to_userid($email);
} elseif {
echo get_avatar($comment, 60);
}
}
function email_to_userid($email) {
// get user id of the email address - xyz#gmail.com
// request google profile image url eg: https://www.googleapis.com/plus/v1/people/123456789?fields=image&key={API_KEY}
// above will retun URL: "url": "https://lh3.googleusercontent.com/-abcdef/bbbbbas/photo.jpg?sz=50"
// return the image URL
}
I'm going crazy with a little problem with Maildir and PHP.
I need to check the APACHE_RUN_USER's Maildir and parse delivery-status messages.
The problem removing message after reading; i noticed that Zend_Mail_Storage_Maildir->removeMessage() is still a stub.
try {
$mailbox = new Zend_Mail_Storage_Maildir( array('dirname' => '/home/' . $_ENV['APACHE_RUN_USER'] . '/Maildir/') );
foreach ($mailbox as $id => $message) {
// seen flag
if ($message->hasFlag(Zend_Mail_Storage::FLAG_SEEN)) { continue; }
//get the unique id
$uniqueid = $mailbox->getUniqueId($id);
//obtain message headers
$headers = $message->getHeaders();
//check if the original message was sent from this app and is a delivery-status
$result = strpos($message, $id_header);
if($result === false) { echo '1 mail skipped: ' . $uniqueid . '. <br />'; continue; }
$result = strpos($headers['content-type'], 'delivery-status');
//if no skip to the next mail
if($result === false) { echo '1 mail skipped: ' . $uniqueid . '. <br />'; continue; }
// if everything it's ok process it.
// clear results
$data = array();
// foreach line of message
foreach( preg_split('/(\r?\n)/', $message) as $line ){
//clear results
$matches = array();
//perform matches on textlines
if( preg_match('/^(.+)\:\s{0,1}(.+)$/', $line, $matches) ) {
//grab intrested headers
foreach( array('Action', 'Status', 'Remote-MTA', 'Diagnostic-Code', $id_header) as $header) {
if($matches[1] == $header) $data[$header] = $matches[2];
}
}
}
// *** I NEED TO DROP THE MESSAGE HERE ***
// not working code ***
$currentmessageid = $mailbox->getNumberByUniqueId($uniqueid);
$mailbox->removeMessage($currentmessageid);
// *** I NEED TO DROP THE MESSAGE HERE ***
// print out results
echo '<pre class="email">';
print_r( $data );
echo '</pre>';
}
} catch (Exception $e) {
echo $e;
}
How can I remove it by hand? Some workarounds?
Thanks.
Sorry , its not implemented yet !
check out issue tracker http://framework.zend.com/issues/browse/ZF-9574
its open issue till today but some comment might be helpful :
In order to delete an email from a
maildir or mbox storage one must use:
Zend_Mail_Storage_Writable_Maildir or
Zend_Mail_Storage_Writable_Mbox
There are historical reasons for this
and they should be addressed and
standardised. For now the above
classes must be used or an exception
will be thrown with a message that is
a bit misleading.
Please refer to:
http://framework.zend.com/issues/browse/ZF-9574
for more details.
In order of tawfekov answer I solved as follow:
Opening mailbox:
$mailbox = new Zend_Mail_Storage_Writable_Maildir( array('dirname' => '/home/' . $_ENV['APACHE_RUN_USER'] . '/Maildir/') );
Processing mail code:
foreach ($mailbox as $id => $message) {
$uniqueid = $mailbox->getUniqueId($id);
/* ... mail processing code ... */
// mark as read
$currentmessageid = $mailbox->getNumberByUniqueId($uniqueid);
$mailbox->setFlags($currentmessageid, array(Zend_Mail_Storage::FLAG_SEEN));
// or uncomment to delete it
//$mailbox->removeMessage($currentmessageid);
}