<?php

require_once 'common/mailfunctions.php';

function plugin_fetchticket_variables()
{
    $variables = array(
        'Plugin Name'   => array(
            'type'          => 'hidden',
            'description'   => '',
            'value'         => 'Tickets Generator',
        ),
        'Enabled'       => array(
            'type'          => 'yesno',
            'description'   => 'When enabled, the support E-mail account will be periodically consulted via POP3, and the relevant messages will be imported as tickets. Useful as a complement or replacement to E-mail piping. Only USER authentication mechanism and non-SSL connections are supported. Be aware that messages on the account will get erased after being imported, or bounced if invalid.',
            'value'         => '0',
        ),
        'Run schedule - Minute'  => array(
            'type'          => 'text',
            'description'   => 'Enter number, range, list or steps',
            'value'         => '30',
            'helpid'        => '8',
        ),
        'Run schedule - Hour'  => array(
            'type'          => 'text',
            'description'   => 'Enter number, range, list or steps',
            'value'         => '*',
        ),
        'Run schedule - Day'  => array(
            'type'          => 'text',
            'description'   => 'Enter number, range, list or steps',
            'value'         => '*',
        ),
        'Run schedule - Month'  => array(
            'type'          => 'text',
            'description'   => 'Enter number, range, list or steps',
            'value'         => '*',
        ),
        'Run schedule - Day of the week'  => array(
            'type'          => 'text',
            'description'   => 'Enter number in range 0-7 (0 is Sunday) or a 3 letter shortcut (e.g. sun)',
            'value'         => '*',
        ),
        'E-mail server hostname'   => array(
            'type'          => 'text',
            'description'   => '',
            'value'         => '',
        ),
        'E-mail server port'   => array(
            'type'          => 'text',
            'description'   => '',
            'value'         => '110',
        ),
        'E-mail account username'   => array(
            'type'          => 'text',
            'description'   => '',
            'value'         => '',
        ),
        'E-mail account password'   => array(
            'type'          => 'text',
            'description'   => '',
            'value'         => '',
        ),
    );

    return $variables;
}

function plugin_fetchticket_execute()
{
    global $gSettingsArray;

    require_once 'plugins/services/fetchticket/pop3.php';

    $pop = new pop3_class();
    $pop->hostname  = $gSettingsArray['plugin_fetchticket_E-mail server hostname'];
    $pop->port      = $gSettingsArray['plugin_fetchticket_E-mail server port'];
    $pop->debug     = 0;
    $pop3->join_continuation_header_lines = 1; /* Concatenate headers split in multiple lines */
    $username       = $gSettingsArray['plugin_fetchticket_E-mail account username'];
    $password       = $gSettingsArray['plugin_fetchticket_E-mail account password'];

    if ($error = $pop->open()) {
        return array(new Error_CE(4782, $error));
    }
    if ($error = $pop->login($username, $password)) {
        return array(new Error_CE(4783, $error));
    }
    if (!is_array($result = $pop->listMessages('', 1))) {
        return array(new Error_CE(4784, $result));
    }
    
    $pipeCount = 0;
    
    define('SERVICE_CALL', true);
    require_once 'SupportPipe.php';
    require_once 'classes/troubletickets.php';
    require_once 'classes/emailparser.php';
    require_once 'classes/troubleticket.php';
    require_once 'modules/support/fncSupportFunctions.php';

    for ($i = 1; $i <= count($result); $i++) {
        $headers = array();
        $body = array();
        if ($error = $pop->retrieveMessage($i, $headers, $body, -1)) {
            return array(new Error_CE(4785, $error));
        }

        $email = implode("\r\n", $headers);
        $email .= "\r\n\r\n".implode("\r\n", $body);

        if ($error =$pop->deleteMessage($i)) {
            return array(new Error_CE(4786, $error));
        }

        runPipe($email);
        $pipeCount++;
    }

    if ($error = $pop->close()) {
        return array(new Error_CE(4787, $error));
    }
    return array($pipeCount." ".lang("E-mail(s) piped into tickets"));

}

function plugin_fetchticket_output()
{
}

function plugin_fetchticket_dashboard()
{
}

?>
