Problem with ISPC Api

Discussion in 'Programming/Scripts' started by sjau, Sep 22, 2015.

Thread Status:
Not open for further replies.
  1. sjau

    sjau Local Meanie Moderator

    Hi there

    I run a small apache server on my notebook to test different things and stuff. A long time ago I created a php script that lets me easily add new email aliases to ISPC (I use for ever website a different alias, so I can find out spammers quickly) and I have also added some of the blacklisting / content filter/checking stuff.
    Well, ISPC got updated lately and the script not working anymore, so I thought I convert it nicely to using the API. I managed to get the email alias adding done but I'm struggling with the domain blacklisting.

    Here's what I have this far:
    Code:
    <?php
    
    ini_set('default_socket_timeout', 600); // or whatever new value you want
    
    # Remote User Details; make sure, you have sufficient for according manipulations
    define('USERNAME_',             'xxx');
    define('PASSWORD_',             'yyyyyyyyy');
    
    # Give Server Details
    define('SOAP_LOCATION_',        'https://domain.tld:8080/remote/index.php');
    define('SOAP_URI_',             'https://domnain.tld:8080/remote/');
    
    # Settings for adding email alias; for client id check your id in ISPConfig
    define('CLIENT_ID_',            1);
    define('EMAIL_DESTINATION_',    '[email protected]');
    define('EMAIL_DOMAIN_',         'mydomain.tld');
    
    # Settings for adding filters/blacklist on postfix
    define('PRIMARY_ID_',           1); # ID of the server - change if you have more than one
    
    
    
    
    
    
    /**************************************************************************************************************
    *                                                                                                             *
    *                                         HERE BE DRAGONS                                                     *
    *                                                                                                             *
    **************************************************************************************************************/
    
    $action = $_POST['pg'];
    switch($action) {
        case "emailBlacklistDomainAdd":
            $output = emailDomainBlockAdd();
            break;
        case "headerContent":
            echo "headerContent";
            break;
        case "bodyContent":
            echo "bodyContent";
            break;
        case "emailAliasAdd":
            $output = emailAliasAdd();
            break;
    }
    
    $pg = $_GET['pg'];
    switch($pg) {
        case "emailBlacklistDomainForm":
            $output .= emailBlacklistDomainForm();;
            break;
        case "fheaderContent":
            echo "fheaderContent";
            break;
        case "fbodyContent":
            echo "fbodyContent";
            break;
        default:
            $output .= emailAliasForm();
    }
    
    
    
    
    
    
    function headerForm() {
            $output = "<html>\n<head>\n<title>Email Service</title>\n</head>\n
                            <body>\n
                            <a href='" . $_SERVER['PHP_SELF'] . "?pg=femail'>Add Email Alias</a> | <a href='" . $_SERVER['PHP_SELF'] . "?pg=emailBlacklistDomainForm'>Blacklist Domain</a> | <a href='" . $_SERVER['PHP_SELF'] . "?pg=fheaderContent'>Block Header content </a> | <a href='" . $_SERVER['PHP_SELF'] . "?pg=fbodyContent'>Block Body content </a><br><br>
                            <form name='input' action='" . $_SERVER['PHP_SELF'] . "' method='POST'>\n";
            return($output);
    }
    
    
    
    function footerForm() {
            $output = "</form>\n</body>\n</html>";
            return($output);
    }
    
    
    
    function buildSOAPConnection() {
        $client = new SoapClient(
            null,
            array(
                    'location' => SOAP_LOCATION_,
                    'uri'      => SOAP_URI_,
                    'trace' => 1,
                    'stream_context'=> stream_context_create(
                            array(
                                    'ssl'=> array(
                                            'verify_peer'=>false,
                                            'verify_peer_name'=>false
                                    )
                            )
                    ),
                    'exceptions' => 1
            )
        );
        return($client);
    }
    
    
    
    
    
    
    function emailAliasForm ($email) {
        $output = "     <input type='text' name='emailAlias' value='$email' size='30'>\n
                        <input type='hidden' name='pg' value='emailAliasAdd' size='30'>\n
                        <input type='submit' name='submit' value='Add New Email Alias'>\n";
        return($output);
    }
    
    
    
    function emailAliasAdd () {
        $client = buildSOAPConnection();
        $emailAlias = $_POST['emailAlias'];
        try {
            $session_id = $client->login(USERNAME_, PASSWORD_);
            //* Parameters
            $params = array(    'active'            => 'y',
                                'destination'       => EMAIL_DESTINATION_,
                                'type'              => 'alias',
                                'source'            => $emailAlias . '@' . EMAIL_DOMAIN_
                            );
            $client->mail_alias_add($session_id, CLIENT_ID_, $params);
            $output = "Added alias $emailAlias" . '@' . EMAIL_DOMAIN_ . "pointing to " . EMAIL_DESTINATION_ . "<br><br>";
            $client->logout($session_id);
        } catch (SoapFault $e) {
            echo $client->__getLastResponse();
            die('SOAP Error: '.$e->getMessage());
        }
        return($output);
    }
    
    
    
    
    
    
    function emailBlacklistDomainForm () {
        $client = buildSOAPConnection();
        try {
            $session_id = $client->login(USERNAME_, PASSWORD_);
            $blacklistList = $client->mail_spamfilter_blacklist_get($session_id, PRIMARY_ID_);
            $client->logout($session_id);
        } catch (SoapFault $e) {
            echo $client->__getLastResponse();
            die('SOAP Error: '.$e->getMessage());
        }
        $output = "     <input type='text' name='emailBlacklistDomain' value='$source' size='30'>\n
                        <input type='hidden' name='pg' value='emailBlacklistDomainAdd' size='30'>\n
                        <input type='submit' name='submit' value='Add Domain to Sender and Client Blacklist'><br>\n
                        <br>\n
                        <br>\n
                        $blacklistList";
        return($output);
    }
    
    function emailDomainBlockAdd () {
        $client = buildSOAPConnection();
        $emailBlacklistDomain = $_POST['emailBlacklistDomain'];
        try {
            $session_id = $client->login(USERNAME_, PASSWORD_);
            //* Parameters
            $params = array(    'active'            => 'y',
                                'access'            => 'REJECT_',
                                'type'              => 'sender',
                                'source'            => $emailBlacklistDomain
                            );
            $client->mail_spamfilter_blacklist_add($session_id, PRIMARY_ID_, $params);
            unset($params);
            //* Parameters
            $params = array(    'active'            => 'y',
                                'access'            => 'REJECT_',
                                'type'              => 'client',
                                'source'            => $emailBlacklistDomain
                            );
            $client->mail_spamfilter_blacklist_add($session_id, PRIMARY_ID_, $params);
            $output = "Added $emailBlacklistDomain to the Sender and Client Blacklist.<br>";
            $client->logout($session_id);
        } catch (SoapFault $e) {
            echo $client->__getLastResponse();
            die('SOAP Error: '.$e->getMessage());
        }
        return($output);
    }
    
    
    
    $header = headerForm();
    $foot = footerForm();
    
    echo "$header $output $footer";
    
    
    ?>
    
    I get this error:

    data_processing_error
    email_error_notempty
     
  2. till

    till Super Moderator Staff Member ISPConfig Developer

    The error means that yo pass a $params array with an empty email address to a insert or update function and this email address is mandatory and may not be empty.
     
  3. sjau

    sjau Local Meanie Moderator

    which is weird. The mail_blacklist.tform.php form doesn't even have an email address field there. The fields are:

    server_id
    source
    access
    type
    active

    And I'm trying to add a domain to the global blacklist as sender/client. So why is email field needed there?
     
  4. till

    till Super Moderator Staff Member ISPConfig Developer

    Check that source is not empty. Source is an email address.
     
  5. sjau

    sjau Local Meanie Moderator

    if "source" needs to be an email address, then as what would I provide e.g. "domain.tld" that should be added to the blacklist?

    server_id -> is the server id
    access -> "REJECT"
    type -> sender/recipient/client
    active -> y/n

    So the only other field left is "source"
     
  6. till

    till Super Moderator Staff Member ISPConfig Developer

    From ISPConfig manual:

    "Email: Specify the email address whose emails should be blacklisted. You can even blacklist a whole
    domain by leaving out the local part of the email address - i.e., if you want to blacklist emails from the
    domain example.com, type @example.com in this field."
     
  7. sjau

    sjau Local Meanie Moderator

    using the webform, I can blacklist an entry like "xxxxxxxxxxxxxxxx" not even the @ is required

    And even if I provide "@xxxxxxx.xxx" I still get the same error.
     
  8. sjau

    sjau Local Meanie Moderator

    my mistake...

    mail_blacklist_add != mail_spamfilter_blacklist_add
     
  9. sjau

    sjau Local Meanie Moderator

    Edit: works..... REJECT instead of REJECT_ :) typo there
     
    Last edited: Sep 25, 2015
  10. sjau

    sjau Local Meanie Moderator

    Got it finally working - yey.... again easy to add stuff to the blacklist or do header/body filtering.
     
Thread Status:
Not open for further replies.

Share This Page