$endDate->modify("+{$withdrawalDay} day"); } if ($currentDate < $startDate) { $startDate->modify("-1 month"); $endDate->modify("-1 month"); } $currency = configgetvalue('system', 'main', NULL, 'currency'); $tariffPrice = smoneyf($user->calculateTariffPrice()); $formattedTariffPrice = sprintf(_ngettext("%s {$currency}", "%s {$currency}", $tariffPrice), $tariffPrice); $fenom->display($theme->getTemplateLocation('header.tpl'), $headerData); if (isset($_POST['code'])) { $paySum = $sc->pay($_POST['code'], $user->getId()); if ($paySum) { $currency = configgetvalue('system', 'main', NULL, 'currency'); $formattedFundSum = sprintf(ngettext("%s {$currency}", "%s {$currency}", $paySum), $paySum); $tpl = array("formattedFundSum" => $formattedFundSum); $fenom->display($theme->getTemplateLocation('goodcard.tpl'), $tpl); } else { $fenom->display($theme->getTemplateLocation('badcard.tpl')); } } else { $currentTariff = getCurrentTariff($user->getId()); $selectedTariff = $user->getSelectedTariff(); if (!$currentTariff && $selectedTariff) { $fenom->display($theme->getTemplateLocation('notification.tpl'), array("className" => "tip", "errorText" => sprintf(__('After updating service "%s" from %s to %s will be automatically paid for %s.'), $selectedTariff['name'], $currentDate->format($dateFormat), $endDate->format($dateFormat), sprintf(_ngettext("%s {$currency}", "%s {$currency}", $tariffPrice), $tariffPrice)))); } $fenom->display($theme->getTemplateLocation('paywithcard.tpl')); } $fenom->display($theme->getTemplateLocation('footer.tpl')); } }
function getCurrentTariff() { $tariff = getCurrentTariff($this->getId()); if ($tariff) { $tariffTable = new table('tariff'); $row = $tariffTable->loadById($tariff['detailsid']); return $row; } }
public function update($userid) { if ($this->connected) { $resource = $this->checkConnection(); $majorVersion = intval($resource['version'][0]); $usersTable = new table('user'); $usersRes = $usersTable->load(" WHERE id={$userid}"); if (!$usersRes) { return; } foreach ($usersRes as $devuserrow) { $syncData = array("/ip/firewall/address-list" => array(), "/queue/simple" => array(), "/ppp/secret" => array()); $userId = '' . $devuserrow['id']; $useraddr = json_decode($devuserrow['iplist'], true); // Address list section $currentTariff = getCurrentTariff($devuserrow['id'], $usersTable); if ($currentTariff) { $currentTariff = $currentTariff['detailsid']; } if (empty($useraddr) || $useraddr == NULL) { return true; } $ipList = array(); foreach ($useraddr as $ip => $mac) { $ipList[] = $ip; } if (userIsDisabled($userid, $usersTable)) { $addrlist = 'disabled'; } else { $addrlist = $currentTariff ? 'allow' : 'deny'; } if (count($ipList) === 2) { $syncData["/ip/firewall/address-list"][$userId][] = array("list" => $addrlist, "address" => $ipList[1]); } // Queque section if ($currentTariff) { $tariffTable = new table('tariff'); $utariffres = $tariffTable->load("WHERE id={$currentTariff}"); $utariffrow = $utariffres[0]; if ($utariffrow) { // Select right target addresses index if ($majorVersion === 5) { $addressIndex = 'target-addresses'; } else { $addressIndex = 'target'; } // Normal / day if (pluginExists('night') && ($utariffrow['nightupspeed'] || $utariffrow['nightdownspeed'])) { $dayTime = $this->calculateMikrotikTime(configgetvalue('system', 'tariff', NULL, 'nightHourEnd')); $dayTime .= '-1d'; //$dayTime .= '-'.$this->calculateMikrotikTime ( configgetvalue('system', 'tariff', NULL, 'nightHourStart') ); $dayTime .= ',sun,mon,tue,wed,thu,fri,sat'; } else { $dayTime = '0s-1d,sun,mon,tue,wed,thu,fri,sat'; } // Burst if (pluginExists('burst') && ($utariffrow['downburstlimit'] && $utariffrow['upburstlimit'] && $utariffrow['downburstthreshold'] && $utariffrow['upburstthreshold'] && $utariffrow['downbursttime'] && $utariffrow['upbursttime'])) { $burstLimit = $utariffrow['upburstlimit'] . '/' . $utariffrow['downburstlimit']; $burstThreshold = $utariffrow['upburstthreshold'] . '/' . $utariffrow['downburstthreshold']; $burstTime = $utariffrow['upbursttime'] . '/' . $utariffrow['downbursttime']; } else { $burstLimit = "0/0"; $burstThreshold = "0/0"; $burstTime = "0s/0s"; } $useraddr = json_decode($devuserrow['iplist'], true); $responce = $this->API->comm('/queue/simple/print', array('?name' => $userid)); $ipList = array(); foreach ($useraddr as $ip => $mac) { $ipList[] = $ip . "/32"; } if (count($ipList) === 2) { $syncData["/queue/simple"][$userId][] = array("limit-at" => $utariffrow['upspeed'] . "/" . $utariffrow['downspeed'], "max-limit" => $utariffrow['upspeed'] . "/" . $utariffrow['downspeed'], $addressIndex => $ipList[1], "name" => $userId, "time" => $dayTime, "burst-limit" => $burstLimit, "burst-threshold" => $burstThreshold, "burst-time" => $burstTime); } // Night $responce = $this->API->comm('/queue/simple/print', array('?name' => "{$userid}-night")); if (pluginExists('night') && ($utariffrow['nightupspeed'] || $utariffrow['nightdownspeed'])) { $useraddr = json_decode($devuserrow['iplist'], true); $ipList = array(); foreach ($useraddr as $ip => $mac) { $ipList[] = $ip . "/32"; } //$time = $this->calculateMikrotikTime ( configgetvalue('system', 'tariff', NULL, 'nightHourStart') ); $time = '0s'; $time .= '-' . $this->calculateMikrotikTime(configgetvalue('system', 'tariff', NULL, 'nightHourEnd')); $time .= ',sun,mon,tue,wed,thu,fri,sat'; if (count($ipList) == 2) { $syncData["/queue/simple"][$userId][] = array("limit-at" => $utariffrow['nightupspeed'] . "/" . $utariffrow['nightdownspeed'], "max-limit" => $utariffrow['nightupspeed'] . "/" . $utariffrow['nightdownspeed'], $addressIndex => $ipList[1], "name" => $userId . '-night', "time" => $time, "burst-limit" => $burstLimit, "burst-threshold" => $burstThreshold, "burst-time" => $burstTime); } } else { if (count($responce)) { $this->API->comm("/queue/simple/remove", array(".id" => $responce[0]['.id'])); } } } } // PPP section // $useraddr = json_decode($devuserrow['iplist'], true); $response = $this->API->comm('/ppp/secret/print', array('?comment' => $userid)); $ipList = array(); $disableSecretsForDisabledUsers = configgetvalue('router', 'ppp', $this->id, 'disablePPPSecretsOfBlockedUsers'); $disabledState = $devuserrow['disabled'] == '1' && $disableSecretsForDisabledUsers ? 'yes' : 'no'; foreach ($useraddr as $ip => $mac) { $ipList[] = $ip; } if (count($ipList) === 2) { $syncData["/ppp/secret"][$userId][] = array("service" => "any", "profile" => "default", "local-address" => $ipList[0], "remote-address" => $ipList[1], "disabled" => $disabledState, "name" => $devuserrow['login'], "password" => $devuserrow['password']); } } $this->sync($syncData); return $this->checkConnection(); } }
<?php include_once '../include/ucp.php'; if (!$user->isValid()) { redirect('login.php'); } else { if (!$user->isEnabled()) { redirect('disabled.php'); } else { if ($user->isRestricted()) { redirect("/login.php?userisrestricted=true"); } else { $page = 'info'; $selectedTariff = $user->getSelectedTariff(); $currentTariff = $user->getCurrentTariff(); $currentOrder = getCurrentTariff($user->getId()); if ($currentOrder) { if ($user->getField('credit') || $currentOrder['temp'] == 1) { $state = __('Is on loan'); } else { $state = __('Enabled'); } } else { $state = __('Disabled'); } $discount = $user->getDiscount(); $permittedActions = array(); if (configgetvalue('system', 'ucp', NULL, 'permitTariffChange') && configgetvalue('subscriber', 'main', $user->getId(), 'permitTariffChange')) { $permittedActions['changetariff'] = true; } if (configgetvalue('system', 'ucp', NULL, 'permitPasswordChange') && configgetvalue('subscriber', 'main', $user->getId(), 'permitPasswordChange') && $user->getField('login')) {
function payment($mode, $id = false) { global $db; global $mysqlTimeDateFormat; /* $mode 0 payment: new cash value will be written, 'upd' action on routers 1 shownotification: new cash value will not be writtent, 'shownotify' on routers 2 clearnotifications: 'clearnotify' on all routers */ if ($mode !== 2) { if ($res = $db->query("SELECT id, price FROM " . DB_TABLE_PREFIX . "tariff")->fetchAll()) { $price = array(); foreach ($res as $row) { $price[$row['id']] = $row['price']; } } $typeOfCalculation = configgetvalue('system', 'cash', NULL, 'typeOfCalculation'); $creditMonths = configgetvalue('system', 'cash', NULL, 'creditMonths'); $routerAction = $mode ? "shownotification" : "update"; $usersTable = new table('user'); $usersRes = $usersTable->load($id ? "WHERE id={$id}" : ""); foreach ($usersRes as $row) { if (!isset($price[$row['tariff']])) { continue; } $tariffId = $row['tariff']; $userId = $row['id']; // Calculate amounts $cash = money($row['cash']); $sum = -getCashToPay($userId, $row, $price[$tariffId]); $fullMonthSum = -getCashToPay($userId, $row, $price[$tariffId], true); $newCash = $cash + $sum; $minimumCash = $fullMonthSum * intval($creditMonths); $currentOrder = getCurrentTariff($userId); if ($mode == 0) { if ($row['disabled'] == '0' && (!$currentOrder || $currentOrder['temp'] == 1) && ($newCash >= $minimumCash || $row['credit'] == '1')) { //Add info to payments table $orderTable = new table('order'); $moneyFlowTable = new table('moneyflow'); $currentDate = new DateTime('midnight'); $startDate = new DateTime('first day of this month midnight'); $endDate = new DateTime('first day of next month midnight'); $endDate->modify("-1 sec"); $withdrawalDay = configgetvalue('system', 'cash', NULL, 'withdrawalDay'); $withdrawalDay -= 1; if ($withdrawalDay) { $startDate->modify("+{$withdrawalDay} day"); $endDate->modify("+{$withdrawalDay} day"); } if ($currentDate < $startDate) { $startDate->modify("-1 month"); $endDate->modify("-1 month"); } if ($currentOrder) { $orderTable->edit(array('id' => $currentOrder['id'], 'canceled' => 1, 'enddate' => $currentDate->format($mysqlTimeDateFormat))); } $orderId = $orderTable->add(array('user' => $userId, 'detailsname' => 'tariff', 'detailsid' => $tariffId, 'startdate' => $currentDate->format($mysqlTimeDateFormat), 'enddate' => $endDate->format($mysqlTimeDateFormat))); $moneyFlowTable->add(array('user' => $userId, 'detailsname' => 'order', 'detailsid' => $orderId, 'sum' => money($sum))); } else { controllerRouterQueue($row['router'], "update", $row['id']); } } else { if ($mode == 1) { if ($row['disabled'] == '0' && ($newCash < $minimumCash && $row['credit'] == '0')) { controllerRouterQueue($row['router'], "shownotification", $row['id']); } } } } } else { $routerTable = new table('router'); $routerRes = $routerTable->load($id ? "WHERE id={$id}" : ""); foreach ($routerRes as $row) { controllerRouterQueue($row['id'], 'clearnotification', $id); } } }
public function update($userid) { if ($this->connected) { $resource = $this->checkConnection(); $majorVersion = intval($resource['version'][0]); $usersTable = new table('user'); $usersRes = $usersTable->load(" WHERE id={$userid}"); if (!$usersRes) { return; } $syncData = array("/ip/firewall/address-list" => array(), "/ip/firewall/filter" => array(), "/ip/firewall/mangle" => array(), "/ip/arp" => array(), "/queue/simple" => array(), "/ip/dhcp-server/lease" => array()); foreach ($usersRes as $devuserrow) { $userId = '' . $devuserrow['id']; foreach ($syncData as $key => $value) { $syncData[$key][$userId] = array(); } $useraddr = json_decode($devuserrow['iplist'], true); // Address list section foreach ($useraddr as $ip => $mac) { if (strlen($mac)) { $mac = strtoupper($mac); $useraddr[$ip] = preg_replace('/..(?!$)/', '$0:', strtoupper($mac)); } } $currentTariff = getCurrentTariff($devuserrow['id'], $usersTable); if ($currentTariff) { $currentTariff = $currentTariff['detailsid']; } if (empty($useraddr) || $useraddr == NULL) { return true; } if (userIsDisabled($devuserrow['id'], $usersTable)) { $addrlist = 'disabled'; } else { $addrlist = $currentTariff ? 'allow' : 'deny'; } foreach ($useraddr as $key => $value) { $syncData["/ip/firewall/address-list"][$userId][] = array("list" => $addrlist, "address" => $key); } // MAC-filter section $macFilterType = configgetvalue('router', 'main', $this->id, 'filterType'); switch ($macFilterType) { case 1: $inInterface = configgetvalue('router', 'main', $this->id, 'inInterface'); foreach ($useraddr as $ip => $mac) { if (strlen($mac)) { $syncData["/ip/arp"][$userId][] = array("mac-address" => $mac, "address" => $ip, "interface" => $inInterface); } } break; case 2: foreach ($useraddr as $ip => $mac) { if (strlen($mac)) { $syncData["/ip/firewall/filter"][$userId][] = array("list" => $addrlist, "address" => $key, "action" => "drop", "chain" => "forward", "src-mac-address" => "!" . $mac, "src-address" => $ip); } } break; case 3: foreach ($useraddr as $ip => $mac) { if (strlen($mac)) { $syncData["/ip/firewall/mangle"][$userId][] = array("action" => "mark-connection", "new-connection-mark" => "badmac", "chain" => "prerouting", "src-mac-address" => "!" . $mac, "src-address" => $ip); } } break; } // Queque section // if ($currentTariff) { $tariffTable = new table('tariff'); $utariffres = $tariffTable->load("WHERE id={$currentTariff}"); $utariffrow = $utariffres[0]; if ($utariffrow) { // Normal / day if (pluginExists('night') && ($utariffrow['nightupspeed'] || $utariffrow['nightdownspeed'])) { $dayTime = $this->calculateMikrotikTime(configgetvalue('system', 'tariff', NULL, 'nightHourEnd')); $dayTime .= '-1d'; //$dayTime .= '-'.$this->calculateMikrotikTime ( configgetvalue('system', 'tariff', NULL, 'nightHourStart') ); $dayTime .= ',sun,mon,tue,wed,thu,fri,sat'; } else { $dayTime = '0s-1d,sun,mon,tue,wed,thu,fri,sat'; } // Burst if (pluginExists('burst') && $utariffrow['downburstlimit'] && $utariffrow['upburstlimit'] && $utariffrow['downburstthreshold'] && $utariffrow['upburstthreshold'] && $utariffrow['downbursttime'] && $utariffrow['upbursttime']) { $burstLimit = $utariffrow['upburstlimit'] . '/' . $utariffrow['downburstlimit']; $burstThreshold = $utariffrow['upburstthreshold'] . '/' . $utariffrow['downburstthreshold']; $burstTime = $utariffrow['upbursttime'] . '/' . $utariffrow['downbursttime']; } else { $burstLimit = "0/0"; $burstThreshold = "0/0"; $burstTime = "0s/0s"; } // Speed $speed = $utariffrow['upspeed'] . "/" . $utariffrow['downspeed']; $useraddr = json_decode($devuserrow['iplist'], true); $responce = $this->API->comm('/queue/simple/print', array('?name' => $userid)); //$addresslist=explode(',', $responce[0]['target-addresses']); $ipList = array(); foreach ($useraddr as $ip => $mac) { $ipList[] = $ip . "/32"; } // Select right target addresses index if ($majorVersion === 5) { $addressIndex = 'target-addresses'; } else { $addressIndex = 'target'; } $dbadrr = implode(',', $ipList); if (count($useraddr)) { $syncData["/queue/simple"][$userId][] = array("limit-at" => $utariffrow['upspeed'] . "/" . $utariffrow['downspeed'], "max-limit" => $utariffrow['upspeed'] . "/" . $utariffrow['downspeed'], $addressIndex => $dbadrr, "name" => $userId, "time" => $dayTime, "burst-limit" => $burstLimit, "burst-threshold" => $burstThreshold, "burst-time" => $burstTime); } // Night $responce = $this->API->comm('/queue/simple/print', array('?name' => "{$userid}-night")); if (pluginExists('night') && ($utariffrow['nightupspeed'] || $utariffrow['nightdownspeed'])) { $nightSpeed = $utariffrow['nightupspeed'] . "/" . $utariffrow['nightdownspeed']; $useraddr = json_decode($devuserrow['iplist'], true); $ipList = array(); foreach ($useraddr as $ip => $mac) { $ipList[] = $ip . "/32"; } $dbadrr = implode(',', $ipList); //$time = $this->calculateMikrotikTime ( configgetvalue('system', 'tariff', NULL, 'nightHourStart') ); $time = '0s'; $time .= '-' . $this->calculateMikrotikTime(configgetvalue('system', 'tariff', NULL, 'nightHourEnd')); $time .= ',sun,mon,tue,wed,thu,fri,sat'; if (count($useraddr)) { $syncData["/queue/simple"][$userId][] = array("limit-at" => $nightSpeed, "max-limit" => $nightSpeed, $addressIndex => $dbadrr, "name" => $userId . '-night', "time" => $time, "burst-limit" => $burstLimit, "burst-threshold" => $burstThreshold, "burst-time" => $burstTime); } } } } // DHCP section $useraddr = json_decode($devuserrow['iplist'], true); foreach ($useraddr as $key => $value) { if ($useraddr[$key]) { $useraddr[$key] = preg_replace('/..(?!$)/', '$0:', strtoupper($value)); } } foreach ($useraddr as $ip => $mac) { if (strlen($mac)) { $syncData["/ip/dhcp-server/lease"][$userId][] = array("address" => $ip, "mac-address" => $mac); } } $this->sync($syncData); return $this->checkConnection(); } } }