From 9a4e741d1ae4fdd0e172c6ee572568ed326793ba Mon Sep 17 00:00:00 2001
From: Adam Magness <adam.magness@gmail.com>
Date: Fri, 1 Dec 2017 21:05:06 -0500
Subject: [PATCH] Review Changes

renamed some functions and adjusted corresponding calls.
---
 include/dbstructure.php | 24 ++-----------------
 mod/invite.php          |  2 +-
 mod/item.php            |  4 ++--
 mod/settings.php        |  2 +-
 src/Network/Probe.php   |  6 ++---
 src/Protocol/Email.php  | 51 +++++++++++------------------------------
 src/Util/Emailer.php    |  4 ++--
 src/Worker/Delivery.php | 10 ++++----
 src/Worker/OnePoll.php  | 11 ++++-----
 9 files changed, 34 insertions(+), 80 deletions(-)

diff --git a/include/dbstructure.php b/include/dbstructure.php
index f7a2219683..cb2883d40d 100644
--- a/include/dbstructure.php
+++ b/include/dbstructure.php
@@ -79,30 +79,10 @@ function update_fail($update_id, $error_message) {
 			'to_email' => $admin['email'],
 			'preamble' => $preamble,
 			'body' => $body,
-			'language' => $lang,
-		));
+			'language' => $lang)
+		);
 	}
 
-
-
-
-	/*
-	 @TODO deprecated code?
-	$email_tpl = get_intltext_template("update_fail_eml.tpl");
-	$email_msg = replace_macros($email_tpl, array(
-		'$sitename' => $a->config['sitename'],
-		'$siteurl' =>  System::baseUrl(),
-		'$update' => DB_UPDATE_VERSION,
-		'$error' => sprintf(t('Update %s failed. See error logs.'), DB_UPDATE_VERSION)
-	));
-	$subject=sprintf(t('Update Error at %s'), System::baseUrl());
-	
-	$subject = Email::emailHeaderEncode($subject,'UTF-8'); // use Friendica\Protocol\Email;
-	mail($a->config['admin_email'], $subject, $email_msg,
-		'From: ' . 'Administrator' . '@' . $_SERVER['SERVER_NAME']."\n"
-		.'Content-type: text/plain; charset=UTF-8'."\n"
-		.'Content-transfer-encoding: 8bit');
-	*/
 	//try the logger
 	logger("CRITICAL: Database structure update failed: ".$retval);
 }
diff --git a/mod/invite.php b/mod/invite.php
index 2571bff927..06c0df3b84 100644
--- a/mod/invite.php
+++ b/mod/invite.php
@@ -75,7 +75,7 @@ function invite_post(App $a) {
 			$nmessage = $message;
 		}
 
-		$res = mail($recip, Email::emailHeaderEncode(t('Please join us on Friendica'),'UTF-8'),
+		$res = mail($recip, Email::encodeHeader(t('Please join us on Friendica'),'UTF-8'),
 			$nmessage,
 			"From: " . $a->user['email'] . "\n"
 			. 'Content-type: text/plain; charset=UTF-8' . "\n"
diff --git a/mod/item.php b/mod/item.php
index d8ecf50a4a..ffac1c8e44 100644
--- a/mod/item.php
+++ b/mod/item.php
@@ -1032,9 +1032,9 @@ function item_post(App $a) {
 				$disclaimer .= sprintf( t('You may visit them online at %s'), System::baseUrl() . '/profile/' . $a->user['nickname']) . EOL;
 				$disclaimer .= t('Please contact the sender by replying to this post if you do not wish to receive these messages.') . EOL;
 				if (!$datarray['title']=='') {
-					$subject = Email::emailHeaderEncode($datarray['title'], 'UTF-8');
+					$subject = Email::encodeHeader($datarray['title'], 'UTF-8');
 				} else {
-					$subject = Email::emailHeaderEncode('[Friendica]' . ' ' . sprintf( t('%s posted an update.'), $a->user['username']), 'UTF-8');
+					$subject = Email::encodeHeader('[Friendica]' . ' ' . sprintf( t('%s posted an update.'), $a->user['username']), 'UTF-8');
 				}
 				$link = '<a href="' . System::baseUrl() . '/profile/' . $a->user['nickname'] . '"><img src="' . $author['thumb'] . '" alt="' . $a->user['username'] . '" /></a><br /><br />';
 				$html    = prepare_body($datarray);
diff --git a/mod/settings.php b/mod/settings.php
index 1d7ac91782..af514e3a9f 100644
--- a/mod/settings.php
+++ b/mod/settings.php
@@ -265,7 +265,7 @@ function settings_post(App $a) {
 					if (strlen($eacct['server'])) {
 						$dcrpass = '';
 						openssl_private_decrypt(hex2bin($eacct['pass']), $dcrpass, $a->user['prvkey']);
-						$mbox = Email::emailConnect($mb, $mail_user, $dcrpass);
+						$mbox = Email::connect($mb, $mail_user, $dcrpass);
 						unset($dcrpass);
 						if (!$mbox) {
 							$failed = true;
diff --git a/src/Network/Probe.php b/src/Network/Probe.php
index 2216d7dcce..500aa7323f 100644
--- a/src/Network/Probe.php
+++ b/src/Network/Probe.php
@@ -1520,13 +1520,13 @@ class Probe
 				$mailbox = Email::constructMailboxName($r[0]);
 				$password = '';
 				openssl_private_decrypt(hex2bin($r[0]['pass']), $password, $x[0]['prvkey']);
-				$mbox = Email::emailConnect($mailbox, $r[0]['user'], $password);
+				$mbox = Email::connect($mailbox, $r[0]['user'], $password);
 				if (!mbox) {
 					return false;
 				}
 			}
 
-			$msgs = Email::emailPoll($mbox, $uri);
+			$msgs = Email::poll($mbox, $uri);
 			logger('searching '.$uri.', '.count($msgs).' messages found.', LOGGER_DEBUG);
 
 			if (!count($msgs)) {
@@ -1546,7 +1546,7 @@ class Probe
 		$data["notify"]  = 'smtp '.random_string();
 		$data["poll"]    = 'email '.random_string();
 
-		$x = Email::emailMsgMeta($mbox, $msgs[0]);
+		$x = Email::messageMeta($mbox, $msgs[0]);
 		if (stristr($x[0]->from, $uri)) {
 			$adr = imap_rfc822_parse_adrlist($x[0]->from, '');
 		} elseif (stristr($x[0]->to, $uri)) {
diff --git a/src/Protocol/Email.php b/src/Protocol/Email.php
index ceca79cb88..c962de725e 100644
--- a/src/Protocol/Email.php
+++ b/src/Protocol/Email.php
@@ -19,7 +19,7 @@ class Email
 	 * @param string $password The password
 	 * @return object
 	 */
-	public static function emailConnect($mailbox, $username, $password)
+	public static function connect($mailbox, $username, $password)
 	{
 		if (! function_exists('imap_open')) {
 			return false;
@@ -35,7 +35,7 @@ class Email
 	 * @param string $email_addr email
 	 * @return array
 	 */
-	public static function emailPoll($mbox, $email_addr)
+	public static function poll($mbox, $email_addr)
 	{
 		if (! ($mbox && $email_addr))
 			return array();
@@ -83,44 +83,19 @@ class Email
 	 * @param integer $uid  user id
 	 * @return mixed
 	 */
-	public static function emailMsgMeta($mbox, $uid)
+	public static function messageMeta($mbox, $uid)
 	{
 		$ret = (($mbox && $uid) ? @imap_fetch_overview($mbox, $uid, FT_UID) : array(array())); // POSSIBLE CLEANUP --> array(array()) is probably redundant now
 		return (count($ret)) ? $ret : array();
 	}
 
-	/**
-	 * @brief Check addons, not called from main friendica project
-	 * I don't see it in addons either
-	 */
-	function email_msg_headers($mbox, $uid) {
-		$raw_header = (($mbox && $uid) ? @imap_fetchheader($mbox,$uid,FT_UID) : '');
-		$raw_header = str_replace("\r",'',$raw_header);
-		$ret = array();
-		$h = explode("\n",$raw_header);
-		if (count($h))
-		foreach ($h as $line ) {
-			if (preg_match("/^[a-zA-Z]/", $line)) {
-				$key = substr($line,0,strpos($line,':'));
-				$value = substr($line,strpos($line,':')+1);
-
-				$last_entry = strtolower($key);
-				$ret[$last_entry] = trim($value);
-			}
-			else {
-				$ret[$last_entry] .= ' ' . trim($line);
-			}
-		}
-		return $ret;
-	}
-
 	/**
 	 * @param object  $mbox  mailbox
 	 * @param integer $uid   user id
 	 * @param string  $reply reply
 	 * @return array
 	 */
-	public static function emailGetMsg($mbox, $uid, $reply)
+	public static function getMessage($mbox, $uid, $reply)
 	{
 		$ret = array();
 
@@ -131,11 +106,11 @@ class Email
 		}
 
 		if (! $struc->parts) {
-			$ret['body'] = self::emailGetPart($mbox, $uid, $struc, 0, 'html');
+			$ret['body'] = self::messageGetPart($mbox, $uid, $struc, 0, 'html');
 			$html = $ret['body'];
 
 			if (trim($ret['body']) == '') {
-				$ret['body'] = self::emailGetPart($mbox, $uid, $struc, 0, 'plain');
+				$ret['body'] = self::messageGetPart($mbox, $uid, $struc, 0, 'plain');
 			} else {
 				$ret['body'] = html2bbcode($ret['body']);
 			}
@@ -143,12 +118,12 @@ class Email
 			$text = '';
 			$html = '';
 			foreach ($struc->parts as $ptop => $p) {
-				$x = self::emailGetPart($mbox, $uid, $p, $ptop + 1, 'plain');
+				$x = self::messageGetPart($mbox, $uid, $p, $ptop + 1, 'plain');
 				if ($x) {
 					$text .= $x;
 				}
 
-				$x = self::emailGetPart($mbox, $uid, $p, $ptop + 1, 'html');
+				$x = self::messageGetPart($mbox, $uid, $p, $ptop + 1, 'html');
 				if ($x) {
 					$html .= $x;
 				}
@@ -184,7 +159,7 @@ class Email
 	 * @param string  $subtype sub type
 	 * @return string
 	 */
-	private static function emailGetPart($mbox, $uid, $p, $partno, $subtype)
+	private static function messageGetPart($mbox, $uid, $p, $partno, $subtype)
 	{
 		// $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
 		global $htmlmsg,$plainmsg,$charset,$attachments;
@@ -257,7 +232,7 @@ class Email
 		if (isset($p->parts) && $p->parts) {
 			$x = "";
 			foreach ($p->parts as $partno0 => $p2) {
-				$x .=  self::emailGetPart($mbox, $uid, $p2, $partno . '.' . ($partno0+1), $subtype);  // 1.2, 1.2.1, etc.
+				$x .=  self::messageGetPart($mbox, $uid, $p2, $partno . '.' . ($partno0+1), $subtype);  // 1.2, 1.2.1, etc.
 				//if ($x) {
 				//	return $x;
 				//}
@@ -271,7 +246,7 @@ class Email
 	 * @param string $charset character set
 	 * @return string
 	 */
-	public static function emailHeaderEncode($in_str, $charset)
+	public static function encodeHeader($in_str, $charset)
 	{
 		$out_str = $in_str;
 		$need_to_convert = false;
@@ -324,7 +299,7 @@ class Email
 	}
 
 	/**
-	 * Function emailSend is used by NETWORK_EMAIL and NETWORK_EMAIL2 code
+	 * Function send is used by NETWORK_EMAIL and NETWORK_EMAIL2 code
 	 * (not to notify the user, but to send items to email contacts)
 	 *
 	 * @param string $addr    address
@@ -336,7 +311,7 @@ class Email
 	 *
 	 * @todo This could be changed to use the Emailer class
 	 */
-	public static function emailSend($addr, $subject, $headers, $item)
+	public static function send($addr, $subject, $headers, $item)
 	{
 		//$headers .= 'MIME-Version: 1.0' . "\n";
 		//$headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
diff --git a/src/Util/Emailer.php b/src/Util/Emailer.php
index ec37204000..7d4205ac33 100644
--- a/src/Util/Emailer.php
+++ b/src/Util/Emailer.php
@@ -37,8 +37,8 @@ class Emailer
 			$email_textonly = PConfig::get($params['uid'], "system", "email_textonly");
 		}
 
-		$fromName = Email::emailHeaderEncode(html_entity_decode($params['fromName'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
-		$messageSubject = Email::emailHeaderEncode(html_entity_decode($params['messageSubject'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
+		$fromName = Email::encodeHeader(html_entity_decode($params['fromName'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
+		$messageSubject = Email::encodeHeader(html_entity_decode($params['messageSubject'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
 
 		// generate a mime boundary
 		$mimeBoundary   =rand(0, 9)."-"
diff --git a/src/Worker/Delivery.php b/src/Worker/Delivery.php
index 7d800f54f7..f17aa5507b 100644
--- a/src/Worker/Delivery.php
+++ b/src/Worker/Delivery.php
@@ -418,19 +418,19 @@ class Delivery {
 					if ($r1 && $r1[0]['reply_to'])
 						$reply_to = $r1[0]['reply_to'];
 
-					$subject  = (($it['title']) ? Email::emailHeaderEncode($it['title'],'UTF-8') : t("\x28no subject\x29")) ;
+					$subject  = (($it['title']) ? Email::encodeHeader($it['title'],'UTF-8') : t("\x28no subject\x29")) ;
 
 					// only expose our real email address to true friends
 
 					if (($contact['rel'] == CONTACT_IS_FRIEND) && !$contact['blocked']) {
 						if ($reply_to) {
-							$headers  = 'From: '.Email::emailHeaderEncode($local_user[0]['username'],'UTF-8').' <'.$reply_to.'>'."\n";
+							$headers  = 'From: '.Email::encodeHeader($local_user[0]['username'],'UTF-8').' <'.$reply_to.'>'."\n";
 							$headers .= 'Sender: '.$local_user[0]['email']."\n";
 						} else {
-							$headers  = 'From: '.Email::emailHeaderEncode($local_user[0]['username'],'UTF-8').' <'.$local_user[0]['email'].'>'."\n";
+							$headers  = 'From: '.Email::encodeHeader($local_user[0]['username'],'UTF-8').' <'.$local_user[0]['email'].'>'."\n";
 						}
 					} else {
-						$headers  = 'From: '. Email::emailHeaderEncode($local_user[0]['username'],'UTF-8') .' <'. t('noreply') .'@'.$a->get_hostname() .'>'. "\n";
+						$headers  = 'From: '. Email::encodeHeader($local_user[0]['username'],'UTF-8') .' <'. t('noreply') .'@'.$a->get_hostname() .'>'. "\n";
 					}
 
 					//if ($reply_to)
@@ -469,7 +469,7 @@ class Delivery {
 						if (strncasecmp($subject,'RE:',3))
 							$subject = 'Re: '.$subject;
 					}
-					Email::emailSend($addr, $subject, $headers, $it);
+					Email::send($addr, $subject, $headers, $it);
 				}
 				break;
 
diff --git a/src/Worker/OnePoll.php b/src/Worker/OnePoll.php
index 3f00454b2a..f7f7bcccf8 100644
--- a/src/Worker/OnePoll.php
+++ b/src/Worker/OnePoll.php
@@ -331,7 +331,7 @@ Class OnePoll
 				$mailbox = Email::constructMailboxName($mailconf);
 				$password = '';
 				openssl_private_decrypt(hex2bin($mailconf['pass']), $password, $x['prvkey']);
-				$mbox = Email::emailConnect($mailbox, $mailconf['user'], $password);
+				$mbox = Email::connect($mailbox, $mailconf['user'], $password);
 				unset($password);
 				logger("Mail: Connect to " . $mailconf['user']);
 				if ($mbox) {
@@ -344,12 +344,12 @@ Class OnePoll
 			}
 
 			if ($mbox) {
-				$msgs = Email::emailPoll($mbox, $contact['addr']);
+				$msgs = Email::poll($mbox, $contact['addr']);
 
 				if (count($msgs)) {
 					logger("Mail: Parsing ".count($msgs)." mails from ".$contact['addr']." for ".$mailconf['user'], LOGGER_DEBUG);
 
-					$metas = Email::emailMsgMeta($mbox,implode(',', $msgs));
+					$metas = Email::messageMeta($mbox, implode(',', $msgs));
 					if (count($metas) != count($msgs)) {
 						logger("onepoll: for " . $mailconf['user'] . " there are ". count($msgs) . " messages but received " . count($metas) . " metas", LOGGER_DEBUG);
 					} else {
@@ -361,8 +361,7 @@ Class OnePoll
 							$datarray = array();
 							$datarray['verb'] = ACTIVITY_POST;
 							$datarray['object-type'] = ACTIVITY_OBJ_NOTE;
-							// $meta = Email::emailMsgMeta($mbox, $msg_uid);
-							// $headers = email_msg_headers($mbox, $msg_uid);
+							// $meta = Email::messageMeta($mbox, $msg_uid);
 
 							$datarray['uri'] = Email::msgid2iri(trim($meta->message_id, '<>'));
 
@@ -466,7 +465,7 @@ Class OnePoll
 								$datarray['parent-uri'] = $datarray['uri'];
 							}
 
-							$r = Email::emailGetMsg($mbox, $msg_uid, $reply);
+							$r = Email::getMessage($mbox, $msg_uid, $reply);
 							if (!$r) {
 								logger("Mail: can't fetch msg ".$msg_uid." for ".$mailconf['user']);
 								continue;