Help with coding (hopefully)

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
nighttraindb
Forum Newbie
Posts: 9
Joined: Sun Mar 23, 2014 9:54 am

Help with coding (hopefully)

Post by nighttraindb »

Hello everyone, I am in need of help here. The only thing I know to do is provide the whole php script
Below is what the script is suppose to do (most of it works minus one part)

1. You can goto the php url and provide a list of user names. It then takes the usernames and grabs the email messages based on the time span you requested.
2. (and this is where the problem is) IF you do not place any names in the box (on the php page) it is suppose to pull from a text file located in the same directory users.txt

Unfortunately, it is not pulling from the users.txt file and for the life of me I can't figure out why (A Complete Novice Here)


Here is a snippet in question and below that is the whole program any help in correcting this would be greatly appreciated.

Code: Select all

if (strlen($users) == 0) 
{

              $users = file('users.txt'); 
             } else {
              $users = explode("\r\n", $users); // This line converts the list to an array
         }
		$users = array_unique($users);
************************************

Code: Select all

<?php
	error_reporting(0);
                                                                                                                                                                  
	date_default_timezone_set('EST');

	function remove_facebook($mes) {
		if (preg_match('/facebook/is', $mes)) {
			if (preg_match('/A lot has happened on Facebook/is', $mes))
				return '';

			$base = $mes;                                                         
			$mes = preg_replace("/(You are receiving this email because.+|\[https?:\/\/www\.facebook\.com.+)?Thanks.+/is", '', $mes);
			$mes = preg_replace("/To reply to this message.+/is", '', $mes);
			$mes = preg_replace("/=+.+=+/is", '', $mes);
			$mes = preg_replace("/https?:\/\/www\.facebook\.com.+/is", '', $mes);
			if ($mes != $base)
				$mes .= "\nThank You for using **********.";
		}

		return $mes;	
	}
	
	$cron = $argv[1] == 'cron=1';	

	if (isset($_POST['submit']) || $cron) {

		$from_ts = $cron ? time() - 3600 * 4 : strtotime($_POST['from']);
		$to_ts = $cron ? time() : strtotime($_POST['to']);
		$from = date('d M Y', $from_ts);
		$to = date('d M Y', $to_ts);

		$users = $_POST['users'];
		$type = $cron ? 'UNSEEN' : $_POST['type'];


	]if (strlen($users) == 0) 
{

              $users = file('users.txt'); 
             } else {
              $users = explode("\r\n", $users); // This line converts the list to an array
         }
		$users = array_unique($users);	
  
		$str = ($from == $to ? "ON \"$from\"" : "SINCE \"$from\" BEFORE \"".date('d M Y', $to_ts + 3600 * 24)."\"")." $type";
		
		$dir_name = "$type From ".date('d-m-Y H:i', $from_ts).' To '.date('d-m-Y H:i', $to_ts);		
		$dir = $cron ? "/home/********/public_html/emails2files_cron/$dir_name" : "/home/******/public_html/emails2files/$dir_name";
		mkdir($dir);

		$cnt_files = 0;
		$cnt_msg = 0;

		foreach ($users as $u) {
			$check = 0;
				
			$imap = "{localhost:143/novalidate-cert}INBOX";
			$mailbox = $u.'@******.com';
			$mbox = imap_open($imap, $mailbox, '*******', OP_READONLY);
			$emails = imap_search($mbox, $str);
			$bodies = array();
			$body = '';
				
			foreach ($emails as $e) {
				$msg = imap_headerinfo($mbox, $e);
				if ($msg->udate > $from_ts && $msg->udate < $to_ts) {	
					$text = preg_replace("/(?:\S{70,}\s*)+/is", 'Image not viewable', remove_facebook(imap_qprint(preg_replace("/textmessage\.org/i", "textmyinmate.com", imap_fetchbody($mbox, $e, 1)))));
					if (!$text)
						continue;

					$subj = 'From: '.$msg->fromaddress."\nSent: ".$msg->date."\nTo: ".$msg->toaddress."\nSubject: ".$msg->subject;               

					if (strlen($body) + strlen($text) + strlen($subj) > 9000) {
						$bodies[] = $body;
						$body = '';	
					}
					$body .= $subj."\n\n".$text."\n".str_repeat('*', 10)."\n";                                                                                                                       
					$cnt_msg++;
					$check = 1;
				}		
			}

			if ($check) {
				$bodies[] = $body;
				$cnt_files += count($bodies);
				foreach ($bodies as $i => $b) {
					file_put_contents("$dir/$mailbox".($i ? $i + 1 : '').'.txt', $b);
					$email = 'v@*******.com'; 
					mail($email, "$u".($i ? " (".($i + 1).")" : '')." messages $dir_name", $b, 'From: Emails2Files'.($cron ? 'Auto' : '').' <emails2files@*******.com>');	
				}	                                                                                                                         
			}
				 	
		}

		$spaces = str_repeat(' ', 10);

		file_put_contents('/home/*******/public_html/emails2files_log/log.txt', date('Y-m-d H:i:s').$spaces.($cron ? 'Auto' : 'Manually').$spaces.$dir_name.$spaces."$cnt_files files $cnt_msg messages\n", FILE_APPEND);

		echo "<p style='font-size:30px;color:green'>Your query: $dir_name<br><b>$cnt_files</b> files was created. Files was sented to <b>$email</b>.<br>Count of messages in files is <b>$cnt_msg</b></p>";
			
	}
		
?>
<form method='POST'>
	From: <input type='text' name='from'> To: <input type='text' name='to'> <b>Format: day-month-year hour:min, example: From: 01-08-2014 10:00 To: 01-08-2014 20:00</b><br><br>
	Type: <select name='type'><option value='ALL'>All<option value='UNSEEN'>Unseen<option value='SEEN'>Seen</select><br><br>
	Users:<br>
	<textarea name='users' cols='30' rows='20'></textarea><br><br>
	<input type='submit' name='submit' value='Start' style='font-size:25px;width:260px'>
</form>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Help with coding (hopefully)

Post by Celauran »

Code: Select all

error_reporting(0);
You're just making life difficult doing that. That's fine for a production environment, but turn error reporting on for development.

Code: Select all

error_reporting(-1);
Much better. Now you should start seeing errors, warnings, and notices when things aren't right, as well as indications where things are going wrong. Makes debugging infinitely easier.

Code: Select all

        ]if (strlen($users) == 0) 
What's up with that ] ? That doesn't belong there.
nighttraindb
Forum Newbie
Posts: 9
Joined: Sun Mar 23, 2014 9:54 am

Re: Help with coding (hopefully)

Post by nighttraindb »

First thank you, ok when I cut the error reporting on the first thing I get is this can't seem to go any further.

Undefined variable: argv in /home/******/public_html/emails2filesb.php on line 23
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Help with coding (hopefully)

Post by Celauran »

What are you trying to do here?

Code: Select all

$cron = $argv[1] == 'cron=1';
What PHP is doing is checking whether $argv[1] is equal to the string 'cron=1' and then assigning the result to $cron. As the error mentioned, though, $argv[1] is undefined, so you'll want to check that first with either isset() or array_key_exists
nighttraindb
Forum Newbie
Posts: 9
Joined: Sun Mar 23, 2014 9:54 am

Re: Help with coding (hopefully)

Post by nighttraindb »

Thank You, I suppose my main question is do you see anything wrong here. See the program is suppose to pull the list from the users.txt file located in /home/*****/public.html however apparently my instructions below does not seem to be correct.

$users = $_POST['users'];
$type = $cron ? 'UNSEEN' : $_POST['type'];


]if (strlen($users) == 0)
{

$users = file('users.txt');
} else {
$users = explode("\r\n", $users); // This line converts the list to an array
}
$users = array_unique($users);
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Help with coding (hopefully)

Post by Celauran »

Have you checked file location? Does the web server have read access to the file? What's the value of $users after the file() call?
nighttraindb
Forum Newbie
Posts: 9
Joined: Sun Mar 23, 2014 9:54 am

Re: Help with coding (hopefully)

Post by nighttraindb »

Ok, this I understand now when I check "rights"

rwxrwxrwx is what I see when I right click I get Octal 0777 for users.txt


/home/******/public_html
folder email2files_log
folder email2files_cron
folder email2files
folder config
folder cgi-bin
......
then files
users.txt
test.txt
email2filesb.phb
email2files.php
postinfo.html...

Thanks again
Post Reply