1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 
<?php
namespace Omeka\Stdlib;

use Doctrine\ORM\EntityManager;
use Omeka\Entity\User;
use Omeka\Entity\PasswordCreation;
use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\Mail\Message;
use Zend\Mail\MessageFactory;
use Zend\Mail\Transport\TransportInterface;
use Zend\View\HelperPluginManager;

class Mailer
{
    /**
     * @var TransportInterface
     */
    protected $transport;

    /**
     * @var HelperPluginManager
     */
    protected $viewHelpers;

    /**
     * @var EntityManager
     */
    protected $entityManager;

    /**
     * @var array
     */
    protected $defaultOptions;

    /**
     * Set the transport and message defaults.
     *
     * @var TransportInterface $transport
     * @var array $defaultOptions
     */
    public function __construct(TransportInterface $transport, HelperPluginManager $viewHelpers,
        EntityManager $entityManager, array $defaultOptions = []
    ) {
        $this->transport = $transport;
        $this->viewHelpers = $viewHelpers;
        $this->entityManager = $entityManager;
        $this->defaultOptions = $defaultOptions;
    }

    /**
     * Get the transport.
     *
     * @return TransportInterface
     */
    public function getTransport()
    {
        return $this->transport;
    }

    /**
     * Return a new message object.
     *
     * Sets default options if not already set.
     *
     * @param array|Traversable $options
     * @return Message
     */
    public function createMessage($options = [])
    {
        if ($options instanceof Traversable) {
            $options = ArrayUtils::iteratorToArray($options);
        }
        $options = array_merge($this->defaultOptions, $options);
        return MessageFactory::getInstance($options);
    }

    /**
     * Send a message using the configured transport.
     *
     * @param array|Traversable|Message $message
     */
    public function send($message)
    {
        if ($message instanceof Message) {
            $this->transport->send($message);
        } else {
            $this->transport->send($this->createMessage($message));
        }
    }

    /**
     * Create and return a password creation entity.
     *
     * @param User $user
     * @param bool $activate Whether to activate the user after setting a new
     *  password
     * @return PasswordCreation
     */
    public function getPasswordCreation(User $user, $activate)
    {
        $passwordCreation = new PasswordCreation;
        $passwordCreation->setId();
        $passwordCreation->setUser($user);
        $passwordCreation->setActivate($activate);

        $this->entityManager->persist($passwordCreation);
        $this->entityManager->flush();
        return $passwordCreation;
    }

    /**
     * Return an absolute URL to the create password page.
     *
     * @param PasswordCreation $passwordCreation
     * @return string
     */
    public function getCreatePasswordUrl(PasswordCreation $passwordCreation)
    {
        $url = $this->viewHelpers->get('url');
        return $url(
                'create-password',
                ['key' => $passwordCreation->getId()],
                ['force_canonical' => true]
            );
    }

    /**
     * Return an absolute URL to the main public page.
     *
     * @return string
     */
    public function getSiteUrl()
    {
        $url = $this->viewHelpers->get('url');
        return $url('top', [], ['force_canonical' => true]);
    }

    /**
     * Return the expiration date.
     *
     * @param PasswordCreation $passwordCreation
     * @return string
     */
    public function getExpiration(PasswordCreation $passwordCreation)
    {
        return $this->viewHelpers
            ->get('i18n')->dateFormat($passwordCreation->getExpiration(), 'medium', 'medium');
    }

    /**
     * Return the title of this Omeka S installation.
     *
     * @return string
     */
    public function getInstallationTitle()
    {
        $setting = $this->viewHelpers->get('setting');
        return $setting('installation_title', 'Omeka S');
    }

    /**
     * Send a reset password email.
     *
     * @param User $user
     */
    public function sendResetPassword(User $user)
    {
        $translate = $this->viewHelpers->get('translate');
        $installationTitle = $this->getInstallationTitle();
        $template = $translate('Greetings, %1$s!

It seems you have forgotten your password for %5$s at %2$s

To reset your password, click this link:
%3$s

Your reset link will expire on %4$s.');

        $passwordCreation = $this->getPasswordCreation($user, false);
        $body = sprintf(
            $template,
            $user->getName(),
            $this->getSiteUrl(),
            $this->getCreatePasswordUrl($passwordCreation),
            $this->getExpiration($passwordCreation),
            $installationTitle
        );

        $message = $this->createMessage();
        $message->addTo($user->getEmail(), $user->getName())
            ->setSubject(sprintf(
                $translate('Reset your password for %s'),
                $installationTitle
            ))
            ->setBody($body);
        $this->send($message);
    }

    /**
     * Send a user activation email.
     *
     * @param User $user
     */
    public function sendUserActivation(User $user)
    {
        $translate = $this->viewHelpers->get('translate');
        $installationTitle = $this->getInstallationTitle();
        $template = $translate('Greetings!

A user has been created for you on %5$s at %1$s

Your username is your email: %2$s

Click this link to set a password and begin using Omeka S:
%3$s

Your activation link will expire on %4$s. If you have not completed the user activation process by the time the link expires, you will need to request another activation email from your site administrator.');

        $passwordCreation = $this->getPasswordCreation($user, true);
        $body = sprintf(
            $template,
            $this->getSiteUrl(),
            $user->getEmail(),
            $this->getCreatePasswordUrl($passwordCreation),
            $this->getExpiration($passwordCreation),
            $installationTitle
        );

        $message = $this->createMessage();
        $message->addTo($user->getEmail(), $user->getName())
            ->setSubject(sprintf(
                $translate('User activation for %s'),
                $installationTitle
            ))
            ->setBody($body);
        $this->send($message);
    }
}