Skip to content

Commit

Permalink
initial commit of the module
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-blom committed Apr 2, 2020
0 parents commit 9d90bb5
Show file tree
Hide file tree
Showing 8 changed files with 980 additions and 0 deletions.
161 changes: 161 additions & 0 deletions Core/Email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

namespace PaBlo\MultiOrderMailReceiver\Core;

use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\Domain\Email\EmailValidatorServiceBridgeInterface;

/**
* Class Email
* @package PaBlo\MultiOrderMailReceiver\Core
* @see \OxidEsales\Eshop\Core\Email
*/
class Email extends Email_parent
{
/**
* Array of carbon copy email addresses
*
* @var array
*/
protected $_aCarbonCopies = [];

/**
* @var bool
*/
protected $_blCarbonCopyActiveState = false;

/**
* Sets mailer additional settings and sends ordering mail to shop owner.
* Returns true on success.
*
* @param \OxidEsales\Eshop\Application\Model\Order $order Order object
* @param string $subject user defined subject [optional]
*
* @return bool
*/
public function sendOrderEmailToOwner($order, $subject = null)
{
/*Only activate the carbon copy on owner emails*/
$this->setCarbonCopyActive();

return parent::sendOrderEmailToOwner($order, $subject);
}

/**
* Preventing possible email spam over php mail() exploit (http://www.securephpwiki.com/index.php/Email_Injection)
*
* @param string $address
* @param null $name
* @param bool $auto
*
* @return bool
*/
public function setFrom($address, $name = null, $auto = true)
{
$success = parent::setFrom($address, $name, $auto);

if (true === $this->getCarbonCopyActiveState()) {
$shop = $this->_getShop();

if (!empty($shop->oxshops__pbowneremailreceiver->value)) {
$carbonCopyMails = explode(';', $shop->oxshops__pbowneremailreceiver->value);

if (count($carbonCopyMails) > 0) {
$container = ContainerFactory::getInstance()->getContainer();
$validationService = $container->get(EmailValidatorServiceBridgeInterface::class);

$language = \OxidEsales\Eshop\Core\Registry::getLang();
$name = $language->translateString("order");

foreach ($carbonCopyMails as $carbonCopyMailAddress) {
if ($validationService->isEmailValid($carbonCopyMailAddress)) {
$this->setCarbonCopy($carbonCopyMailAddress, $name);
}
}
}
}
}

return $success;
}

/**
* Sets mail carbon copy to carbon copies array
*
* @param string $address recipient email address
* @param string $name recipient name
*/
public function setCarbonCopy($address = null, $name = null): void
{
try {
$address = $this->idnToAscii($address);

$this->addCC($address, $name);

// copying values as original class does not allow to access recipients array
$this->_aCarbonCopies[] = [$address, $name];
} catch (Exception $exception) {
}
}

/**
* Gets carbon copy array.
* Returns array of recipients
* f.e. array( array('[email protected]', 'user1Name'), array('[email protected]', 'user2Name') )
*
* @return array
*/
public function getCarbonCopy(): array
{
return $this->_aCarbonCopies;
}

/**
* Clears all recipients assigned in the TO, CC and BCC array.
*/
public function clearAllCarbonCopies(): void
{
$this->_aCarbonCopies = [];
$this->clearAllRecipients();
}

/**
* Convert domain name to IDNA ASCII form.
*
* @param string $idn The email address
*
* @return string
*/
protected function idnToAscii($idn)
{
if (function_exists('idn_to_ascii')) {
// for old PHP versions support
// remove it after the PHP 7.1 support is dropped
if (defined('INTL_IDNA_VARIANT_UTS46')) {
return idn_to_ascii($idn, 0, INTL_IDNA_VARIANT_UTS46);
}

return idn_to_ascii($idn);
}

return $idn;
}

/**
* Sets the carbon copy state to active
*/
private function setCarbonCopyActive(): void
{
$this->_blCarbonCopyActiveState = true;
}

/**
* Returns the current carbon copy state
*
* @return bool
*/
private function getCarbonCopyActiveState(): bool
{
return $this->_blCarbonCopyActiveState;
}
}
48 changes: 48 additions & 0 deletions Core/MultiOrderMailReceiver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace PaBlo\MultiOrderMailReceiver\Core;

use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
use OxidEsales\Eshop\Core\DbMetaDataHandler;
use OxidEsales\Eshop\Core\DatabaseProvider;

/**
* Class MultiOrderMailReceiver
* @package PaBlo\MultiOrderMailReceiver\Core
*/
class MultiOrderMailReceiver
{
/**
* Adds a custom field to oxshops table to store the additional mail addresses
*
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseErrorException
*/
public static function onActivate(): void
{
$dbMetaDataHandler = oxNew(DbMetaDataHandler::class);

if (!$dbMetaDataHandler->fieldExists('PBOWNEREMAILRECEIVER', 'oxshops')) {
DatabaseProvider::getDb()->execute(
"ALTER TABLE oxshops ADD PBOWNEREMAILRECEIVER text NOT NULL default '' COMMENT 'Additional recipients for order owner mail';"
);
}
}

/**
* Ensures that the template blocks will be cleared on module deactivation.
*/
public static function onDeactivate(): void
{
$container = ContainerFactory::getInstance()->getContainer();
$queryBuilderFactory = $container->get(QueryBuilderFactoryInterface::class);

$queryBuilder = $queryBuilderFactory->create();
$queryBuilder->delete('oxtplblocks', 'tpl')
->where('tpl.oxmodule = :moduleId')
->setParameters([
'moduleId' => 'multiordermailreceiver'
]);
}
}
Loading

0 comments on commit 9d90bb5

Please sign in to comment.