Example #1
0
function Namecheapssl_hook_sync_2($user, $password, $debugMode = false)
{
    require_once dirname(__FILE__) . "/../../modules/servers/namecheapssl/namecheapapi.php";
    require_once dirname(__FILE__) . "/../../modules/servers/namecheapssl/namecheapssl.php";
    namecheapssl_log('hook.sync', 'sync_hook_started', $user);
    $sync_date_offset = NcSql::sql2cell("SELECT value FROM mod_namecheapssl_settings WHERE name='sync_date_offset'");
    $api = new NamecheapApi($user, $password, $debugMode);
    $iPageSize = 22;
    $requestParams = array("Page" => 1, "PageSize" => $iPageSize);
    try {
        $response = $api->request("namecheap.ssl.getList", $requestParams);
        $result = $api->parseResponse($response);
    } catch (Exception $e) {
        echo $e->getMessage();
        return;
        //exit();
    }
    if ($result) {
        $iTotalPages = (int) ceil($result['Paging']['TotalItems'] / $iPageSize);
    }
    for ($i = 1; $i <= $iTotalPages; $i++) {
        if (1 != $i) {
            try {
                $requestParams = array("Page" => $i, "PageSize" => $iPageSize);
                $response = $api->request("namecheap.ssl.getList", $requestParams);
                $result = $api->parseResponse($response);
            } catch (Exception $e) {
                echo $e->getMessage();
                return;
                //exit();
            }
        }
        foreach ($result["SSLListResult"]["SSL"] as $aCertInfo) {
            if ('active' == $aCertInfo['@attributes']['Status'] || 'replaced' == $aCertInfo['@attributes']['Status']) {
                // synchronize expire date
                list($month, $day, $year) = explode("/", $aCertInfo['@attributes']['ExpireDate']);
                //
                $res = Namecheapssl_hook_sync_mysql_query("SELECT h.id FROM `tblhosting` h INNER JOIN `tblsslorders` s ON s.serviceid=h.id  WHERE s.remoteid='{$aCertInfo['@attributes']['CertificateID']}' AND h.`nextduedate` != '{$year}-{$month}-{$day}'");
                if (mysql_num_rows($res)) {
                    $iHostingId = array_shift(mysql_fetch_array($res));
                    $duedate = "{$year}-{$month}-{$day}";
                    if ($sync_date_offset) {
                        $duedate = date('Y-m-d', strtotime($duedate . "-{$sync_date_offset} days"));
                    }
                    $sql = "update `tblhosting`\n                                   set `nextduedate` = '{$duedate}',\n                                       `nextinvoicedate` = '{$duedate}'\n                                 where `id` = '{$iHostingId}'";
                    Namecheapssl_hook_sync_mysql_query($sql);
                    namecheapssl_log('hook.sync', 'sync_hook_updated_duedate', array("{$duedate}"), $iHostingId);
                }
                // sync domain
                if (!empty($aCertInfo['@attributes']['HostName']) && 'active' == $aCertInfo['@attributes']['Status']) {
                    $domain = mysql_real_escape_string($aCertInfo['@attributes']['HostName']);
                    $res = Namecheapssl_hook_sync_mysql_query("SELECT h.id FROM `tblhosting` h INNER JOIN `tblsslorders` s ON s.serviceid=h.id  WHERE s.remoteid='{$aCertInfo['@attributes']['CertificateID']}' AND h.`domain` != '{$domain}'");
                    if (mysql_num_rows($res)) {
                        $iHostingId = array_shift(mysql_fetch_array($res));
                        $sql = "update `tblhosting`\n                                        set `domain` = '{$domain}'\n                                        where `id` = '{$iHostingId}'";
                        Namecheapssl_hook_sync_mysql_query($sql);
                        namecheapssl_log('hook.sync', 'sync_hook_updated_domain', array($domain), $iHostingId);
                    }
                }
            }
            if ('replaced' == $aCertInfo['@attributes']['Status']) {
                // synchronize reissue state
                $sql = "SELECT * FROM tblsslorders WHERE remoteid='{$aCertInfo['@attributes']['CertificateID']}'";
                $r = Namecheapssl_hook_sync_mysql_query($sql);
                if (mysql_num_rows($r)) {
                    $aWhmcsCert = mysql_fetch_assoc($r);
                    // get replaced certificate info
                    try {
                        $replaced_cert_request_params = array('CertificateID' => (int) $aWhmcsCert['remoteid']);
                        $replaced_cert_response = $api->request("namecheap.ssl.getInfo", $replaced_cert_request_params);
                        $replaced_cert_result = $api->parseResponse($replaced_cert_response);
                        if (!empty($replaced_cert_result["SSLGetInfoResult"]["@attributes"]["ReplacedBy"])) {
                            $replacedBy = (int) $replaced_cert_result["SSLGetInfoResult"]["@attributes"]["ReplacedBy"];
                            if (0 == $replacedBy) {
                                echo 'Wrong "replaced by" attribute: ' . $replaced_cert_result["SSLGetInfoResult"]["@attributes"]["ReplacedBy"];
                                return;
                                //exit();
                            }
                            $sql = "UPDATE tblsslorders SET remoteid='{$replacedBy}' WHERE remoteid='{$aCertInfo['@attributes']['CertificateID']}'";
                            Namecheapssl_hook_sync_mysql_query($sql);
                            $sql = "UPDATE mod_namecheapssl SET certificate_id='{$replacedBy}' WHERE certificate_id='{$aCertInfo['@attributes']['CertificateID']}'";
                            Namecheapssl_hook_sync_mysql_query($sql);
                            namecheapssl_log('hook.sync', 'sync_hook_updated_remoteid', array($aCertInfo['@attributes']['CertificateID'], $replacedBy), $aWhmcsCert['serviceid']);
                        }
                    } catch (Exception $e) {
                        echo $e->getMessage();
                        return;
                        //exit();
                    }
                }
            }
        }
    }
}
Example #2
0
function namecheapssl_log($action, $messageKey, $args = null, $serviceId = 0)
{
    static $_M;
    if (empty($_M)) {
        include_once dirname(__FILE__) . '/namecheaplog.php';
    }
    if (empty($_M[$messageKey])) {
        #####
        //// exit('empty log alias');
        return false;
    }
    // try to detect user
    $username = '******';
    $userid = 0;
    if (!empty($_SESSION['adminid'])) {
        $userid = $_SESSION["adminid"];
        $sql = "SELECT username FROM tbladmins WHERE id='" . (int) $userid . "'";
        $username = NcSql::sql2cell($sql);
    }
    if (!empty($_SESSION['uid'])) {
        $userid = $_SESSION['uid'];
        $username = '******';
    }
    $action = 'mod.' . $action;
    if (is_null($args)) {
        $message = $_M[$messageKey];
    } else {
        if (is_string($args)) {
            $args = array($args);
        }
        $message = vsprintf($_M[$messageKey], $args);
    }
    $sql = "INSERT INTO mod_namecheapssl_log SET date=NOW(), debug='0', action='" . NcSql::e($action) . "', serviceid='" . (int) $serviceId . "', description='" . NcSql::e($message) . "', ipaddr='" . NcSql::e($_SERVER['REMOTE_ADDR']) . "', userid='" . (int) $userid . "', user='******'";
    NcSql::q($sql);
    return NcSql::insertId();
}