<commit_before><?php

/**
 * MIT License
 * Copyright (c) 2018 Electronic Student Services @ Appalachian State University
 * 
 * See LICENSE file in root directory for copyright and distribution permissions.
 * 
 * @author <NAME> <<EMAIL>>
 * @license https://opensource.org/licenses/MIT
 */

namespace stories\Factory;

use stories\Resource\GuestResource as Resource;
use phpws2\Database;
use Canopy\Request;
use phpws2\Variable;
use phpws2\Template;

if (!defined('STORIES_SENDMAIL')) {
    define('STORIES_SENDMAIL', '/usr/sbin/sendmail -bs');
}

class GuestFactory extends BaseFactory
{

    /**
     * @param array $data
     * @return stories\Resource\GuestResource
     */
    public function build($data = null)
    {
        $resource = new Resource;
        if ($data) {
            $resource->setVars($data);
        }
        return $resource;
    }

    public function getCurrentGuests()
    {
        $db = Database::getDB();
        $tbl = $db->addTable('storiesguest');
        $tbl->addFieldConditional('status', 1);
        $tbl->addOrderBy('siteName');
        return $db->select();
    }

    public function getGuestRequests()
    {
        $db = Database::getDB();
        $tbl = $db->addTable('storiesguest');
        $tbl->addFieldConditional('status', 0);
        $tbl->addOrderBy('siteName');
        return $db->select();
    }

    public function newGuestRequest(Request $request)
    {
        $guest = $this->build();
        $guest->siteName = $request->pullPostString('siteName');
        $guest->url = $request->pullPostString('url');
        $guest->email = $request->pullPostString('email');
        $guest->submitDate = time();
        return self::saveResource($guest);
    }

    public function acceptRequest($id)
    {
        $guest = $this->load($id);
        $guest->status = 1;
        $guest->acceptDate = time();
        $this->createAuthkey($guest);
        self::saveResource($guest);
        $this->emailAcceptance($guest);
    }

    private function emailAcceptance($guest)
    {
        $transport = \Swift_SendmailTransport::newInstance(STORIES_SENDMAIL);

        $subject = 'Sharing accepted for site: ' . $guest->siteName;
        $from = 'noreply@' . \Canopy\Server::getSiteUrl(false, false, false);
        $vars['authkey'] = $guest->authkey;
        $vars['siteName'] = $guest->siteName;
        $vars['url'] = $guest->url;
        $template = new Template($vars);
        $template->setModuleTemplate('stories', 'Email/Acceptance.html');
        $content = $template->get();

        $message = \Swift_Message::newInstance();
        $message->setSubject($subject);
        $message->setFrom($from);
        $message->setTo($guest->email);
        $message->setBody($content, 'text/html');
        $mailer = new \Swift_Mailer($transport);
        $mailer->send($message);
    }

    private function createAuthKey(Resource $guest)
    {
        $guest->authkey = sha1(microtime());
    }

    public function denyGuest($id)
    {
        $guest = $this->load($id);
        $guest->status = 2;
        self::saveResource($guest);
    }

}<commit_msg>Added getByAuthKey to load guests<commit_after><?php

/**
 * MIT License
 * Copyright (c) 2018 Electronic Student Services @ Appalachian State University
 * 
 * See LICENSE file in root directory for copyright and distribution permissions.
 * 
 * @author <NAME> <<EMAIL>>
 * @license https://opensource.org/licenses/MIT
 */

namespace stories\Factory;

use stories\Resource\GuestResource as Resource;
use phpws2\Database;
use Canopy\Request;
use phpws2\Variable;
use phpws2\Template;
use stories\Factory\ShareFactory;

if (!defined('STORIES_SENDMAIL')) {
    define('STORIES_SENDMAIL', '/usr/sbin/sendmail -bs');
}

class GuestFactory extends BaseFactory
{

    /**
     * @param array $data
     * @return stories\Resource\GuestResource
     */
    public function build($data = null)
    {
        $resource = new Resource;
        if ($data) {
            $resource->setVars($data);
        }
        return $resource;
    }

    public function getCurrentGuests()
    {
        $db = Database::getDB();
        $tbl = $db->addTable('storiesguest');
        $tbl->addFieldConditional('status', 1);
        $tbl->addOrderBy('siteName');
        return $db->select();
    }

    public function getGuestRequests()
    {
        $db = Database::getDB();
        $tbl = $db->addTable('storiesguest');
        $tbl->addFieldConditional('status', 0);
        $tbl->addOrderBy('siteName');
        return $db->select();
    }

    public function newGuestRequest(Request $request)
    {
        $guest = $this->build();
        $guest->siteName = $request->pullPostString('siteName');
        $guest->url = $request->pullPostString('url');
        $guest->email = $request->pullPostString('email');
        $guest->submitDate = time();
        return self::saveResource($guest);
    }

    public function acceptRequest($id)
    {
        $guest = $this->load($id);
        $guest->status = 1;
        $guest->acceptDate = time();
        $this->createAuthkey($guest);
        self::saveResource($guest);
        $this->emailAcceptance($guest);
    }

    private function emailAcceptance($guest)
    {
        $transport = \Swift_SendmailTransport::newInstance(STORIES_SENDMAIL);

        $subject = 'Sharing accepted for site: ' . $guest->siteName;
        $from = 'noreply@' . \Canopy\Server::getSiteUrl(false, false, false);
        $vars['authkey'] = $guest->authkey;
        $vars['siteName'] = $guest->siteName;
        $vars['url'] = $guest->url;
        $template = new Template($vars);
        $template->setModuleTemplate('stories', 'Email/Acceptance.html');
        $content = $template->get();

        $message = \Swift_Message::newInstance();
        $message->setSubject($subject);
        $message->setFrom($from);
        $message->setTo($guest->email);
        $message->setBody($content, 'text/html');
        $mailer = new \Swift_Mailer($transport);
        $mailer->send($message);
    }

    private function createAuthKey(Resource $guest)
    {
        $guest->authkey = sha1(microtime());
    }

    public function denyGuest($id)
    {
        $guest = $this->load($id);
        $guest->status = 2;
        self::saveResource($guest);
    }

    private function getByAuthkey($authkey)
    {
        $db = Database::getDB();
        $tbl = $db->addTable('storiesguest');
        $tbl->addFieldConditional('authkey', $authkey);
        $row = $db->selectOneRow();
        if (empty($row)) {
            return null;
        } else {
            return $this->build($row);
        }
    }

    public function shareRequest(Request $request)
    {
        $authkey = $request->pullGetString('authkey');
        $entryId = $request->pullGetInteger('entryId');
        $guest = $this->getByAuthkey($authkey);
        if (!$guest) {
            return ['error' => 'Authorization not recognized.'];
        }
        try {
            $shareFactory = new ShareFactory;
            $shareFactory->create($guest, $entryId);
        } catch (\Exception $e) {
            if ($e->getCode() === '23000') {
                return ['error' => 'Duplicate request.'];
            }
        }

        return ['success' => true];
    }

}
{'fraction_non_alphanumeric': 0.1578439481665288, 'fraction_numerical': 0.003997794320374966, 'mean_word_length': 2.582716049382716, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 95, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6061899', 'n_tokens_mistral': 2238, 'n_tokens_neox': 2172, 'n_words': 408}