Skip to content

Commit 23245d3

Browse files
committed
a bunch of updates improving the code making use of self:: and other imprvoements
1 parent 2d31936 commit 23245d3

1 file changed

Lines changed: 56 additions & 19 deletions

File tree

src/Plugin.php

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,66 @@ public static function doEnable(\Service_Order $serviceOrder, $repeatInvoiceId,
4242
$settings = get_module_settings(self::$module);
4343
if ($regexMatch === false) {
4444
$db = get_module_db(self::$module);
45-
$ip = website_get_next_ip($serviceInfo[$settings['PREFIX'].'_server']);
46-
myadmin_log(self::$module, 'info', 'Trying To Give '.$settings['TITLE'].' '.$serviceInfo[$settings['PREFIX'] . '_id'].' Repeat Invoice '.$repeatInvoiceId.' IP ' . ($ip === false ? '<ip allocation failed>' : $ip), __LINE__, __FILE__);
47-
if ($ip) {
48-
$GLOBALS['tf']->history->add(self::$module . 'queue', $serviceInfo[$settings['PREFIX'] . '_id'], 'add_ip', $ip, $serviceInfo[$settings['PREFIX'] . '_custid']);
49-
$description = 'Additional IP ' . $ip . ' for ' . $settings['TBLNAME'] . ' ' . $serviceInfo[$settings['PREFIX'] . '_id'];
50-
$rdescription = '(Repeat Invoice: ' . $repeatInvoiceId . ') ' . $description;
51-
$db->query("update {$settings['PREFIX']}_ips set ips_main=0,ips_used=1,ips_{$settings['PREFIX']}={$serviceInfo[$settings['PREFIX'].'_id']} where ips_ip='{$ip}'", __LINE__, __FILE__);
52-
$db->query("update invoices set invoices_description='{$rdescription}' where invoices_type=1 and invoices_extra='{$repeatInvoiceId}'", __LINE__, __FILE__);
53-
$db->query("update repeat_invoices set repeat_invoices_description='{$description}' where repeat_invoices_id='{$repeatInvoiceId}'", __LINE__, __FILE__);
45+
$id = $serviceInfo[$settings['PREFIX'].'_id'];
46+
$ip = $serviceInfo[$settings['PREFIX'].'_ip'];
47+
$db->query("select * from website_masters where website_id='{$serviceInfo[$settings['PREFIX'].'_server']}'", __LINE__, __FILE__);
48+
$db->next_record(MYSQL_ASSOC);
49+
$serverdata = $db->Record;
50+
$hash = $serverdata[$settings['PREFIX'].'_key'];
51+
$user = 'root';
52+
function_requirements('whm_api');
53+
$whm = new \xmlapi($serverdata[$settings['PREFIX'].'_ip']);
54+
//$whm->set_debug('true');
55+
$whm->set_port('2087');
56+
$whm->set_protocol('https');
57+
$whm->set_output('json');
58+
$whm->set_auth_type('hash');
59+
$whm->set_user($user);
60+
$whm->set_hash($hash);
61+
$accts = json_decode($whm->listips(), true);
62+
$freeips = [];
63+
$shared_ips = [];
64+
foreach ($accts['result'] as $idx => $ipdata) {
65+
if ($ipdata['mainaddr'] == '1')
66+
$main_ip = $ipdata['ip'];
67+
if ($ipdata['used'] == 0 && $ipdata['active'] == 1)
68+
$freeips[] = $ipdata['ip'];
69+
if ($ipdata['dedicated'] == 0)
70+
$shared_ips[] = $ipdata['ip'];
71+
}
72+
// check if ip is main or additional/dedicated. if ip is main, get a new one
73+
if (in_array($ip, $shared_ips)) {
74+
myadmin_log(self::$module, 'info', "IP {$ip} (Shared) Main IP {$main_ip}", __LINE__, __FILE__);
75+
if (sizeof($freeips) > 0) {
76+
// assign new ip
77+
$ip = $freeips[0];
78+
$response = $whm->setsiteip($ip, $serviceInfo[$settings['PREFIX'].'_username']);
79+
myadmin_log(self::$module, 'info', "WHM setsiteip({$ip}, {$serviceInfo[$settings['PREFIX'].'_username']}) Response: {$response}", __LINE__, __FILE__);
80+
$response = json_decode($response);
81+
if ($response->result[0]->status == 1) {
82+
// update db w/ new ip
83+
$db->query("update {$settings['TABLE']} set {$settings['PREFIX']}_ip='$ip' where {$settings['PREFIX']}_id=$id", __LINE__, __FILE__);
84+
myadmin_log(self::$module, 'info', "Gave Website {$id} IP {$ip}", __LINE__, __FILE__);
85+
} else {
86+
myadmin_log(self::$module, 'info', "Error Giving Website {$id} IP {$ip}", __LINE__, __FILE__);
87+
$headers = '';
88+
$headers .= 'MIME-Version: 1.0' . EMAIL_NEWLINE;
89+
$headers .= 'Content-type: text/html; charset=UTF-8' . EMAIL_NEWLINE;
90+
$headers .= 'From: ' . TITLE . ' <' . EMAIL_FROM . '>' . EMAIL_NEWLINE;
91+
$subject = 'Error Setting IP ' . $ip . ' on ' . $settings['TBLNAME'] . ' ' . $serviceInfo[$settings['TITLE_FIELD']];
92+
admin_mail($subject, $subject, $headers, false, 'admin_email_website_no_ips.tpl');
93+
}
94+
} else {
95+
$subject = "0 Free IPs On {$settings['TBLNAME']} Server {$serverdata[$settings['PREFIX'].'_name']}";
96+
admin_mail($subject, "Webserver {$id} Has Pending IPS<br>\n" . $subject, false, false, 'admin_email_website_no_ips.tpl');
97+
myadmin_log(self::$module, 'info', $subject, __LINE__, __FILE__);
98+
}
5499
} else {
55-
$db->query('SELECT * FROM ' . $settings['PREFIX'] . '_masters WHERE ' . $settings['PREFIX'] . '_id=' . $serviceInfo[$settings['PREFIX'] . '_server'], __LINE__, __FILE__);
56-
$db->next_record(MYSQL_ASSOC);
57-
$headers = '';
58-
$headers .= 'MIME-Version: 1.0' . EMAIL_NEWLINE;
59-
$headers .= 'Content-type: text/html; charset=UTF-8' . EMAIL_NEWLINE;
60-
$headers .= 'From: ' . TITLE . ' <' . EMAIL_FROM . '>' . EMAIL_NEWLINE;
61-
$subject = '0 Free IPs On ' . $settings['TBLNAME'] . ' Server ' . $db->Record[$settings['PREFIX'] . '_name'];
62-
admin_mail($subject, $settings['TBLNAME'] . " {$serviceInfo[$settings['PREFIX'].'_id']} Has Pending IPS<br>\n" . $subject, $headers, false, 'admin_email_vps_no_ips.tpl');
100+
myadmin_log(self::$module, 'info', "IP {$ip} (Already Dedicated) Main IP {$main_ip}", __LINE__, __FILE__);
63101
}
64102
} else {
65103
$ip = $regexMatch;
66-
$GLOBALS['tf']->history->add(self::$module . 'queue', $serviceInfo[$settings['PREFIX'] . '_id'], 'ensure_addon_ip', $ip, $serviceInfo[$settings['PREFIX'] . '_custid']);
67-
$db->query("update {$settings['PREFIX']}_ips set ips_main=0,ips_used=1,ips_{$settings['PREFIX']}={$id} where ips_ip='{$ip}'", __LINE__, __FILE__);
104+
myadmin_log(self::$module, 'info', "IP {$ip} (Already Dedicated)", __LINE__, __FILE__);
68105
}
69106
}
70107

0 commit comments

Comments
 (0)