ktemail.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 575 行 · 第 1/2 页

PHP
575
字号
<?php
/**
 * $Id: KTEmail.php 9237 2008-09-10 18:45:15Z kevin_fourie $
 *
 * KnowledgeTree Community Edition
 * Document Management Made Simple
 * Copyright (C) 2008 KnowledgeTree Inc.
 * Portions copyright The Jam Warehouse Software (Pty) Limited
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 3 as published by the
 * Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco,
 * California 94120-7775, or email info@knowledgetree.com.
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * KnowledgeTree" logo and retain the original copyright notice. If the display of the
 * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
 * must display the words "Powered by KnowledgeTree" and retain the original
 * copyright notice.
 * Contributor( s): ______________________________________
 *
 */

require_once(KT_LIB_DIR . '/actions/actionregistry.inc.php');

require_once(KT_LIB_DIR . '/email/Email.inc');
require_once(KT_LIB_DIR . '/users/User.inc');
require_once(KT_LIB_DIR . '/groups/Group.inc');
require_once(KT_LIB_DIR . '/documentmanagement/DocumentTransaction.inc');
require_once(KT_LIB_DIR . '/documentmanagement/Document.inc');
require_once(KT_DIR . '/ktwebservice/KTDownloadManager.inc.php');

/**
 * Sends emails to the selected groups
 */
function sendGroupEmails($aGroupIDs, &$aUserEmails, &$aEmailErrors) {
	global $default;

    // loop through groups
    for ($i=0; $i<count($aGroupIDs); $i++) {
    	// validate the group id
    	if ($aGroupIDs[$i] > 0) {
		    $oDestGroup = Group::get($aGroupIDs[$i]);

		    $aMemberGroups = $oDestGroup->getMemberGroups();
		    foreach ($aMemberGroups as $member){
		    	$aDestinationGroups[] = $member;
		    }
		    $aDestinationGroups[] = $oDestGroup;

		    $default->log->info('sendingEmail to group ' . $oDestGroup->getName());
		    // for each group, retrieve all the users
		    foreach($aDestinationGroups as $oGroup){
		    	$aUsers = kt_array_merge($aUsers, $oGroup->getUsers());
		    }

		    // FIXME: this should send one email with multiple To: users
		    // The FIX (26-09-2007): create an array of users to email
		    for ($j=0; $j<count($aUsers); $j++) {
	    		$default->log->info('sendingEmail to group-member ' . $aUsers[$j]->getName() . ' with email ' . $aUsers[$j]->getEmail());
			    // the user has an email address and has email notification enabled
				if (strlen($aUsers[$j]->getEmail())>0 && $aUsers[$j]->getEmailNotification()) {
					//if the to address is valid, send the mail
					if (validateEmailAddress($aUsers[$j]->getEmail())) {
					   // use the email address as the index to ensure the user is only sent 1 email
					   $aUserEmails[$aUsers[$j]->getEmail()] = $aUsers[$j]->getName();
					} else {
						$default->log->error('email validation failed for ' . $aUsers[$j]->getEmail());
					}
				} else {
				$default->log->info('either ' . $aUsers[$j]->getUserName() . ' has no email address, or notification is not enabled');
				}
		    }
    	} else {
    		$default->log->info('filtered group id=' . $aGroupIDs[$i]);
    	}
    }
}

/**
 * Sends emails to the selected users
 */
function sendUserEmails($aUserIDs, &$aUserEmails, &$aEmailErrors) {
	global $default;

    // loop through users
    for ($i=0; $i<count($aUserIDs); $i++) {
    	if ($aUserIDs[$i] > 0) {
		    $oDestUser = User::get($aUserIDs[$i]);
	    	$default->log->info('sendingEmail to user ' . $oDestUser->getName() . ' with email ' . $oDestUser->getEmail());
		    // the user has an email address and has email notification enabled
			if (strlen($oDestUser->getEmail())>0 && $oDestUser->getEmailNotification()) {
				//if the to address is valid, send the mail
				if (validateEmailAddress($oDestUser->getEmail())) {
				    // use the email address as the index to ensure the user is only sent 1 email
				    $aUserEmails[$oDestUser->getEmail()] = $oDestUser->getName();
				}
			} else {
				$default->log->info('either ' . $oDestUser->getUserName() . ' has no email address, or notification is not enabled');
			}
    	} else {
    		$default->log->info('filtered user id=' . $aUserIDs[$i]);
    	}
    }
}

/**
 * Sends emails to the manually entered email addresses
 */
function sendManualEmails($aEmailAddresses, &$aUserEmails, &$aEmailErrors) {
	global $default;

    // loop through users
    foreach ($aEmailAddresses as $sEmailAddress) {
        $default->log->info('sendingEmail to address ' .  $sEmailAddress);
        if (validateEmailAddress($sEmailAddress)) {
            // use the email address as the index to ensure the user is only sent 1 email
            $aUserEmails[$sEmailAddress] = '';
        }
    }
}

function sendExternalEmails($aEmailAddresses, $iDocumentID, $sDocumentName, $sComment, &$aEmailErrors){
    global $default;
    $oSendingUser = User::get($_SESSION['userID']);

    // Create email content
    $sMessage = '<font face="arial" size="2">';
	$sMessage .= sprintf(_kt("Your colleague, %s, wishes you to view the document entitled '%s'."), $oSendingUser->getName(), $sDocumentName);
	$sMessage .= " \n";
	$sMessage .= _kt('Click on the hyperlink below to view it.') . '<br><br>';
    $sMsgEnd = '<br><br>' . _kt('Comments') . ':<br>' . $sComment;
	$sMsgEnd .= '</font>';

	$sTitle = sprintf(_kt("Link (ID %s): %s from %s"), $iDocumentID, $sDocumentName, $oSendingUser->getName());

	$sEmail = null;
    $sEmailFrom = null;
    $oConfig =& KTConfig::getSingleton();
    if (!$oConfig->get('email/sendAsSystem')) {
        $sEmail = $oSendingUser->getEmail();
        $sEmailFrom = $oSendingUser->getName();
    }
    $oEmail = new Email($sEmail, $sEmailFrom);

    foreach ($aEmailAddresses as $sAddress){
        if(validateEmailAddress($sAddress)){
            // Add to list of addresses
            $sDestEmails .= (empty($sDestEmails)) ? $sAddress : ', '.$sAddress;

            // Create temporary session id
            $session = 'ktext_'.$iDocumentID.time();

            // Create download link
            $oDownloadManager = new KTDownloadManager();
            $oDownloadManager->set_session($session);
            $link = $oDownloadManager->allow_download($iDocumentID);

            $link = "<a href=\"{$link}\">{$link}</a>";

            $sMsg = $sMessage.$link.$sMsgEnd;
            $res = $oEmail->send(array($sAddress), $sTitle, $sMsg);

            if (PEAR::isError($res)) {
                $default->log->error($res->getMessage());
                $aEmailErrors[] = $res->getMessage();
            } else if ($res === false) {
        		$default->log->error("Error sending email ($sTitle) to $sAddress");
        		$aEmailErrors[] = "Error sending email ($sTitle) to $sAddress";
            }
        }
        $default->log->info("Send email ($sTitle) to external addresses $sDestEmails");

    	// emailed link transaction
    	// need a document to do this.
    	$oDocument =& Document::get($iDocumentID);

        $oDocumentTransaction = new DocumentTransaction($oDocument, sprintf(_kt("Document link emailed to external addresses %s. "), $sDestEmails).$sComment, 'ktcore.transactions.email_link');

        if ($oDocumentTransaction->create()) {
    		$default->log->debug("emailBL.php created email link document transaction for document ID=$iDocumentID");
    	} else {
    		$default->log->error("emailBL.php couldn't create email link document transaction for document ID=$iDocumentID");
    	}
    }
}

/**
 * Constructs the email message text and sends the message
 */
function sendEmail($aDestEmailAddress, $iDocumentID, $sDocumentName, $sComment, $bAttachDocument = false, &$aEmailErrors) {
    if ($bAttachDocument !== true) {
        return sendEmailHyperlink($aDestEmailAddress, $iDocumentID, $sDocumentName, $sComment, $aEmailErrors);
    } else {
        return sendEmailDocument($aDestEmailAddress, $iDocumentID, $sDocumentName, $sComment, $aEmailErrors);
    }
}

function sendEmailDocument($aDestEmailAddress, $iDocumentID, $sDocumentName, $sComment, &$aEmailErrors) {
    global $default;
    // Get the email list as a string for the logs
    $sDestEmails = implode(',', $aDestEmailAddress);
    $oSendingUser = User::get($_SESSION['userID']);

    $sMessage .= sprintf(_kt("Your colleague, %s, wishes you to view the attached document entitled '%s'."), $oSendingUser->getName(), $sDocumentName);
    $sMessage .= "\n\n";
	if (strlen($sComment) > 0) {
		$sMessage .= '<br><br>' . _kt('Comments') . ':<br>' . $sComment;
	}
    $sTitle = sprintf(_kt("Document (ID %s): %s from %s"), $iDocumentID, $sDocumentName, $oSendingUser->getName());

    $sEmail = null;
    $sEmailFrom = null;
    $oConfig =& KTConfig::getSingleton();
    if (!$oConfig->get('email/sendAsSystem')) {
        $sEmail = $oSendingUser->getEmail();
        $sEmailFrom = $oSendingUser->getName();
    }
    $oEmail = new Email($sEmail, $sEmailFrom);
    $oDocument = Document::get($iDocumentID);

    // Request a standard file path so that it can be attached to the
    // email
    $oStorage =& KTStorageManagerUtil::getSingleton();
    $sDocumentPath = $oStorage->temporaryFile($oDocument);

    $sDocumentFileName = $oDocument->getFileName();
    $res = $oEmail->sendAttachment($aDestEmailAddress, $sTitle, $sMessage, $sDocumentPath, $sDocumentFileName);

    // Tell the storage we don't need the temporary file anymore.
    $oStorage->freeTemporaryFile($sDocumentPath);

    if (PEAR::isError($res)) {
        $default->log->error($res->getMessage());
        $aEmailErrors[] = $res->getMessage();
        return $res;
    } else if ($res === false) {
        $default->log->error("Error sending email ($sTitle) to $sDestEmails");
        $aEmailErrors[] = "Error sending email ($sTitle) to $sDestEmails";
        return PEAR::raiseError(sprintf(_kt("Error sending email (%s) to %s"), $sTitle, $sDestEmails));
    } else {
        $default->log->info("Send email ($sTitle) to $sDestEmails");
    }

    // emailed link transaction
	$oDocumentTransaction = new DocumentTransaction($oDocument, sprintf(_kt("Document copy emailed to %s. "), $sDestEmails).$sComment, 'ktcore.transactions.email_attachment');
    if ($oDocumentTransaction->create()) {
        $default->log->debug("emailBL.php created email link document transaction for document ID=$iDocumentID");
    } else {
        $default->log->error("emailBL.php couldn't create email link document transaction for document ID=$iDocumentID");
    }
}

function sendEmailHyperlink($aDestEmailAddress, $iDocumentID, $sDocumentName, $sComment, &$aEmailErrors) {
    global $default;
    // Get the email list as a string for the logs
    $sDestEmails = implode(',', $aDestEmailAddress);
    $oSendingUser = User::get($_SESSION['userID']);

	$sMessage = '<font face="arial" size="2">';
    /*
    if ($sDestUserName) {
        $sMessage .= $sDestUserName . ',<br><br>';
    }
    */
	$sMessage .= sprintf(_kt("Your colleague, %s, wishes you to view the document entitled '%s'."), $oSendingUser->getName(), $sDocumentName);
	$sMessage .= " \n";
	$sMessage .= _kt('Click on the hyperlink below to view it.') . '<br>';
	// add the link to the document to the mail
	$sMessage .= '<br>' . generateControllerLink('viewDocument', "fDocumentID=$iDocumentID", $sDocumentName, true);
	// add optional comment
	if (strlen($sComment) > 0) {
		$sMessage .= '<br><br>' . _kt('Comments') . ':<br>' . $sComment;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?