Add remaining project files (exclude ignored folders)

This commit is contained in:
WD - Dev
2025-07-05 15:11:40 +07:00
parent a96eb2b958
commit b440b80882
4697 changed files with 1365702 additions and 0 deletions
+153
View File
@@ -0,0 +1,153 @@
<?php
class PesanWA {
// KirimpesanW('112','PesanKirimTag','202','https://dnynet.my.id/images/image.php')
public function Create_Pesan($IdBS,$pel,$pesan,$tag='')
{
include "config/connect.php";
$ap = $koneksi->query("SELECT $pesan FROM basicsetting WHERE id_admin = '$IdBS'");
$pp = $ap->fetch_assoc();
$dp = $pp[$pesan];
$ambil = $koneksi->query("SELECT id_paket,no_hp,nama_pelanggan,pppoe,alamat_pelanggan,nama_paket FROM pelanggan JOIN paket ON pelanggan.id_paket=paket.id_paket WHERE id_pelanggan = $pel");
$pecah = $ambil->fetch_assoc();
$idp = $pecah['id_paket'];
$phone = $pecah['no_hp'];
$nama = $pecah['nama_pelanggan'];
$pppoe = $pecah['pppoe'];
$address = $pecah['alamat_pelanggan'];
$layanan = $pecah['nama_paket'];
if ($tag !='')
{
$tagihan = $koneksi->query("SELECT * FROM tagihan WHERE id_tagihan = $tag");
$pectag = $tagihan->fetch_assoc();
$keterangan = $pectag['KeteranganTag'];
$limit = $pectag['limit_pembayaran'];
$jumlah = number_format($pectag['JumlahTagihan']);
$ppn = number_format($pectag['PPN']);
$kode = $pectag['kodeunik'];
$total = number_format($pectag['VerifRek']);
}
else
{
$keterangan = ''; $limit = ''; $jumlah = ''; $ppn = ''; $kode = ''; $total = '';
}
if (preg_match("/#usr/i", $dp) == 1)
{
$t1 = str_replace("#usr",$nama,$dp);
}
else { $t1 = $dp; }
if (preg_match("/#ppp/i", $t1) == 1)
{
$t2 = str_replace("#ppp",$pppoe,$t1);
}
else { $t2 = $t1; }
if (preg_match("/#add/i", $t2) == 1)
{
$t3 = str_replace("#add",$address,$t2);
}
else { $t3 = $t2; }
if (preg_match("/#lyn/i", $t3) == 1)
{
$t4 = str_replace("#lyn",$layanan,$t3);
}
else { $t4 = $t3; }
if (preg_match("/#tot/i", $t4) == 1)
{
$t5 = str_replace("#tot",$total,$t4);
}
else { $t5 = $t4; }
if (preg_match("/#ket/i", $t5) == 1)
{
$t6 = str_replace("#ket",$keterangan,$t5);
}
else { $t6 = $t5; }
if (preg_match("/#lmt/i", $t6) == 1)
{
$t7 = str_replace("#lmt",$limit,$t6);
}
else { $t7 = $t6; }
if (preg_match("/#jum/i", $t7) == 1)
{
$t8 = str_replace("#jum",$jumlah,$t7);
}
else { $t8 = $t7; }
if (preg_match("/#ppn/i", $t8) == 1)
{
$t9 = str_replace("#ppn",$ppn,$t8);
}
else { $t9 = $t8; }
if (preg_match("/#unk/i", $t9) == 1)
{
$t10 = str_replace("#unk",$kode,$t9);
}
else { $t10 = $t9; }
$this->PesanWA = $t10;
}
public function Get_Pesan()
{
return $this->PesanWA;
}
public function Send_Pesan($apiurl,$apikey,$nowa,$message,$file_url='')
{
//Perbaiki kode negara ind nomor WA
if (substr($nowa,0,2)=='08')
{
$nohp = '62'.substr($nowa, 1);
}
elseif (substr($nowa,0,3)=='628')
{
$nohp = $nowa;
}
if ($file_url!='')
{
$body = array(
'Apikey' => $apikey,
'Phone' => $nohp,
'Message' => $message,
'File' => $file_url
);
}
else
{
$body = array(
'Apikey' => $apikey,
'Phone' => $nohp,
'Message' => $message
);
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $apiurl.'api/sendpost.php');
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_TIMEOUT,30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
curl_exec($curl);
curl_close($curl);
}
}
?>
+32
View File
@@ -0,0 +1,32 @@
<?php
date_default_timezone_set('Asia/Jakarta');
$host="localhost";
$user="root";
$password="";
$db="db_bilspro";
$con = new mysqli($host,$user,$password,$db);
if (!$con)
{
die("Koneksi gagal:".mysqli_connect_error());
}
$dsn = 'mysql:host=localhost;dbname=db_bilspro';
$username = 'root';
$password1 = '';
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_PERSISTENT => true, // menggunakan koneksi yang persisten
];
try {
$pdo = new PDO($dsn, $username, $password1, $options);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
exit;
}
?>
+1
View File
@@ -0,0 +1 @@
NO ACTION => 2 <-> cc.net Limit_Tgl_5<br>NO ACTION => 3 <-> nsn utama Limit_Tgl_10<br>NO ACTION => 3 <-> ciangsana Limit_Tgl_15<br>NO ACTION => 4 <-> fast.net Limit_Tgl_12<br>NO ACTION => 5 <-> bima net Limit Tagihan Tgl 15<br>NO ACTION => 6 <-> kingnet Limit Tgl 15<br>NO ACTION => 8 <-> diamond.net Limit_Tgl_15<br>NO ACTION => 9 <-> 22 p.net Limit_Tgl_15<br>NO ACTION => 10 <-> nettizen Tagihan Limit tgl 15<br>NO ACTION => 12 <-> Tagihan_Limit_Tgl5<br>NO ACTION => 15 <-> Tag_NTA2 Limit Tgl 5<br>NO ACTION => 16 <-> Tagihan Limit Tgl 5<br>BUAT TAGIHAN => 13 <-> Limit Tanggal 26 (Infinnet) Start : 2025-07-04 - Last : 2025-07-09<br>REMAND => 18 <-> BUM-Limit-Tgl-5<br>NO ACTION => 14 <-> Kh_Limit Tanggal 25<br>KIRIM TAGIHAN => 17 <-> Tagihan Limit Tgl 10<br>NO ACTION => 17 <-> Tagihan Limit Tgl 15<br>NO ACTION => 20 <-> IVVI-10<br>NO ACTION => 20 <-> IVVI-20<br>NO ACTION => 20 <-> IVVI-30<br>BUAT TAGIHAN => 21 <-> QONNET Start : 2025-07-02 - Last : 2025-07-05<br>NO ACTION => 21 <-> QONNET2<br>NO ACTION => 25 <-> Tagihan Limit Tgl 5<br>REMAND => 23 <-> Limit Tanggal 5<br>NO ACTION => 26 <-> Limit Tanggal 10<br>NO ACTION => 24 <-> Tagihan30<br>BUAT TAGIHAN => 23 <-> limit tanggal 19 Start : 2025-06-27 - Last : 2025-07-14<br>KIRIM TAGIHAN => 31 <-> 23-ISOLIR<br>NO ACTION => 33 <-> usolir-10<br>NO ACTION => 37 <-> paket 140 ribu<br>REMAND => 28 <-> PAKET-100K<br>REMAND => 28 <-> PAKET-150K<br>REMAND => 28 <-> PAKET-200K<br>REMAND => 28 <-> PAKET-300K<br>KIRIM TAGIHAN => 42 <-> meldina-20<br>BUAT TAGIHAN => 41 <-> lippy-20 Start : 2025-07-04 - Last : 2025-07-09<br>BUAT TAGIHAN => 43 <-> Tagihan_Limit_tgl_13 Start : 2025-07-01 - Last : 2025-07-06<br>REMAND => 46 <-> Limit_Tgl_5<br>NO ACTION => 35 <-> PAKET 1 DEVICE<br>NO ACTION => 35 <-> PAKET 2 DEVICE<br>NO ACTION => 35 <-> PAKET 3 DEVICE<br>NO ACTION => 35 <-> PAKET 4 DEVICE<br>NO ACTION => 35 <-> PAKET 5 DEVICE [1/80]<br>NO ACTION => 35 <-> PAKET 5 DEVICE [1/70]<br>NO ACTION => 35 <-> PAKET 6 DEVICE [1/80]<br>NO ACTION => 35 <-> PAKET 6 DEVICE [1/70]<br>NO ACTION => 35 <-> PAKET 7 DEVICE<br>NO ACTION => 35 <-> PAKET 8 DEVICE<br>BUAT TAGIHAN => 44 <-> Isolir_20 Start : 2025-07-04 - Last : 2025-07-07<br>BUAT TAGIHAN => 53 <-> PAKET BRONZE Start : 2025-07-01 - Last : 2025-07-06<br>KIRIM TAGIHAN => 54 <-> Muhamad rouf<br>BUAT TAGIHAN => 53 <-> PAKET PLATINUM 5MBs Start : 2025-07-01 - Last : 2025-07-06<br>BUAT TAGIHAN => 53 <-> PAKET GLOD 7 MBs Start : 2025-07-01 - Last : 2025-07-06<br>BUAT TAGIHAN => 53 <-> PAKET GLOD+ 10 MBs Start : 2025-07-01 - Last : 2025-07-06<br>NO ACTION => 59 <-> Isolir_21<br>BUAT TAGIHAN => 46 <-> Limit tgl 30 Start : 2025-06-29 - Last : 2025-07-24<br>NO ACTION => 61 <-> Isolir_20<br>BUAT TAGIHAN => 55 <-> Dcn_CO_Isolir Start : 2025-06-28 - Last : 2025-07-24<br>REMAND => 62 <-> limit tanggal 5<br>NO ACTION => 21 <-> VIP-BAGUS<br>REMAND => 55 <-> Dcn_GB_Isolir<br>BUAT TAGIHAN => 49 <-> TAGIHAN TANGGAL 20 Start : 2025-06-29 - Last : 2025-07-18<br>NO ACTION => 19 <-> TAGIHAN-TGL1<br>NO ACTION => 49 <-> TAGIHAN TANGGAL 1<br>NO ACTION => 66 <-> Tagihan Isolir Tgl 1<br>NO ACTION => 67 <-> Tag Netborhood Limit Tgl 10<br>REMAND => 73 <-> Tagihan01-05<br>
+321
View File
@@ -0,0 +1,321 @@
<?php
include "connect.php";
include "tanggal_indo.php";
include "whatsapp_api.php";
require('routeros_api.class.php');
$time = date('H');
$now = date('d');
try {
// Ambil data setting
$stmt = $pdo->prepare("SELECT * FROM profile_tagihan");
$stmt->execute();
$settings = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($settings as $pset) {
$kirim = $pset['kirim_tagihan'];
$limit = $pset['limit_tagihan'];
$jumhari = $pset['buat_tagihan'];
$sekarang = date('Y-m-d');
$skr2 = strtotime($sekarang);
// Cari Max buat tagihan
$date91 = new DateTime($sekarang);
$date91->add(new DateInterval('P' . $jumhari . 'D'));
$tgl91 = $date91->format('Y-m-d');
$sendtag1 = substr($tgl91, 0, 8) . $kirim;
// Kurangi 1 hari dari $tgl91
$dateKirim = new DateTime($sendtag1);
$dateKirim->modify('-1 day'); // Mengurangi 1 hari
$sendtag = $dateKirim->format('Y-m-d');
$lasttime = strtotime($sendtag);
// Cari Min buat tagihan
$date92 = new DateTime($sendtag);
$date92->sub(new DateInterval('P' . $jumhari . 'D'));
$tgl92 = $date92->format('Y-m-d');
$starttime = strtotime($tgl92);
// echo "BUAT TAGIHAN => " . $pset['id_data_server'] . " <-> " . $pset['nama'] . " Start : " . $tgl92 . " - Last : " . $sendtag . "<br><br>";
// KIRIM TAGIHAN
if ($now == $pset['kirim_tagihan'] AND $time >= '6') {
echo "KIRIM TAGIHAN => " . $pset['id_data_server'] . " <-> " . $pset['nama'] . "<br>";
$stmt = $pdo->prepare("SELECT * FROM v_tagihan WHERE status = 'buat' AND id_profile_tagihan = :id_profile_tagihan LIMIT 20");
$stmt->bindParam(':id_profile_tagihan', $pset['id'], PDO::PARAM_INT);
$stmt->execute();
$tags = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($tags as $psend) {
echo $psend['nama'] . "<br>";
$idp = $psend['id_pelanggan'];
$idtag = $psend['id'];
$phone = array($psend['nomor_whatsapp']);
// KIRIM PESAN WHATSAPP
$API_WA = new PesanWA();
$API_WA->Create_Pesan($psend['id_data_server'], $idtag, 'pesan_kirim_tagihan', 'tag');
$Pesan = $API_WA->Get_Pesan();
$API_WA->Send_Pesan($psend['id_data_server'], 'sendMessage', $phone, $Pesan);
// UPDATE TAGIHAN
$stmt = $pdo->prepare("
UPDATE tagihan SET status = 'pending' WHERE id = :id
");
$stmt->bindParam(':id', $idtag, PDO::PARAM_INT);
$stmt->execute();
}
}
// END KIRIM TAGIHAN
// REMAND TAGIHAN
elseif ($now == $pset['peringatan_tagihan'] AND $time >= '6') {
echo "REMAND => " . $pset['id_data_server'] . " <-> " . $pset['nama'] . "<br>";
$stmt = $pdo->prepare("SELECT * FROM v_tagihan WHERE status = 'pending' AND id_profile_tagihan = :id_profile_tagihan LIMIT 20");
$stmt->bindParam(':id_profile_tagihan', $pset['id'], PDO::PARAM_INT);
$stmt->execute();
$remands = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($remands as $presend) {
echo $presend['nama'] . "<br>";
$idtagre = $presend['id'];
$idpelre = $presend['id_pelanggan'];
$wa_nore = array($presend['nomor_whatsapp']);
// KIRIM PESAN WHATSAPP
$API_WA = new PesanWA();
$API_WA->Create_Pesan($presend['id_data_server'], $idtagre, 'pesan_peringatan_tagihan', 'tag');
$Pesanre = $API_WA->Get_Pesan();
$API_WA->Send_Pesan($presend['id_data_server'], 'sendMessage', $wa_nore, $Pesanre);
// UPDATE TAGIHAN
$stmt = $pdo->prepare("
UPDATE tagihan SET status = 'pending1' WHERE id = :id
");
$stmt->bindParam(':id', $idtagre, PDO::PARAM_INT);
$stmt->execute();
echo "<br>".$Pesanre." => ";
print_r($wa_nore);
}
}
// END REMAND TAGIHAN
// OFF CONNECTION OFF PELANGGAN
elseif ($now == $pset['limit_tagihan']) {
echo "OFF CONNECTION => " . $pset['id_data_server'] . " <-> " . $pset['nama'] . "<br>";
$stmt = $pdo->prepare("SELECT * FROM v_tagihan
WHERE status IN ('pending', 'pending1', 'pending2')
AND id_profile_tagihan = :id_profile_tagihan LIMIT 10");
$stmt->bindParam(':id_profile_tagihan', $pset['id'], PDO::PARAM_INT);
$stmt->execute();
$offs = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($offs as $poff) {
echo $poff['nama'] . $poff['id_setting_mikrotik'] ."<br>";
$idtagihan = $poff['id'];
$idpelanggan = $poff['id_pelanggan'];
$wa_nooff = array($poff['nomor_whatsapp']);
$user = $poff['akun'];
$userhs = $poff['user_hotspot'];
//Get Mikrotik Setting
$Mikro = $pdo->prepare("SELECT ip_address, port_api, username, password FROM setting_mikrotik WHERE id = :idset");
$Mikro->bindParam(':idset', $poff['id_setting_mikrotik'], PDO::PARAM_INT);
$Mikro->execute();
$Mikrot = $Mikro->fetchAll(PDO::FETCH_ASSOC);
print_r($Mikrot);
$m_ip = $Mikrot[0]['ip_address'];
$m_user = $Mikrot[0]['username'];
$m_pass = $Mikrot[0]['password'];
$m_port = $Mikrot[0]['port_api'];
$API = new RouterosAPI();
$API->debug = false;
if ($API->connect2($m_ip, $m_user, $m_pass, $m_port)) {
$cek = 1;
if ($poff['jenis_layanan'] == 'Hotspot') {
$arrIDh = $API->comm("/ip/hotspot/user/getall", [
".proplist" => ".id",
"?name" => $userhs,
]);
if (!empty($arrIDh)) {
$API->comm("/ip/hotspot/user/set", [
".id" => $arrIDh[0][".id"],
"disabled" => "true",
]);
$arrIDha = $API->comm("/ip/hotspot/active/getall", [
".proplist" => ".id",
"?user" => $userhs,
]);
if (!empty($arrIDha)) {
$API->comm("/ip/hotspot/active/remove", [
".id" => $arrIDha[0][".id"],
]);
}
}
} else {
$arrID = $API->comm("/ppp/secret/getall", [
".proplist" => ".id",
"?name" => $user,
]);
if (!empty($arrID)) {
$API->comm("/ppp/secret/set", [
".id" => $arrID[0][".id"],
"disabled" => "true",
]);
$arrID1 = $API->comm("/ppp/active/getall", [
".proplist" => ".id",
"?name" => $user,
]);
if (!empty($arrID1)) {
$API->comm("/ppp/active/remove", [
".id" => $arrID1[0][".id"],
]);
}
}
}
} else { $cek = 0; }
$API->disconnect();
if($cek == 1){
// UPDATE TAGIHAN
$stmt = $pdo->prepare("
UPDATE tagihan SET status = 'lewat' WHERE id = :id_tagihan
");
$stmt->bindParam(':id_tagihan', $idtagihan, PDO::PARAM_INT);
$stmt->execute();
// UPDATE PELANGGAN
$stmt = $pdo->prepare("
UPDATE pelanggan SET status = 'tidak aktif' WHERE id = :id_pelanggan
");
$stmt->bindParam(':id_pelanggan', $idpelanggan, PDO::PARAM_INT);
$stmt->execute();
// KIRIM PESAN WHATSAPP
$API_WA = new PesanWA();
$API_WA->Create_Pesan($poff['id_data_server'], $idtagihan, 'pesan_isolir_pelanggan', 'tag');
$Pesanr = $API_WA->Get_Pesan();
$API_WA->Send_Pesan($poff['id_data_server'], 'sendMessage', $wa_nooff, $Pesanr);
}
else {
echo "Server Not Connect";
}
}
}
// END OFF CONNECTION OFF PELANGGAN
// BUAT TAGIHAN
elseif ($skr2 >= $starttime && $skr2 <= $lasttime) {
echo "BUAT TAGIHAN => " . $pset['id_data_server'] . " <-> " . $pset['nama'] . " Start : " . $tgl92 . " - Last : " . $sendtag . "<br>";
$stmt = $pdo->prepare("SELECT * FROM pelanggan WHERE status = 'aktif' AND id_profile_tagihan = :id");
$stmt->bindParam(':id', $pset['id'], PDO::PARAM_INT);
$stmt->execute();
$pelanggan = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($pelanggan as $pecah1) {
// Cari data paket
$stmt = $pdo->prepare("SELECT harga_paket FROM profile_paket WHERE id = :id_profile_paket ");
$stmt->bindParam(':id_profile_paket', $pecah1['id_profile_paket'], PDO::PARAM_INT);
$stmt->execute();
$tagihan = $stmt->fetchColumn();
// Cari data tagihan di tabel tagihan
$stmt = $pdo->prepare("SELECT * FROM tagihan WHERE status = 'buat' AND id_pelanggan = :id");
$stmt->bindParam(':id', $pecah1['id'], PDO::PARAM_INT);
$stmt->execute();
$adadata = $stmt->rowCount();
if ($adadata == 0) {
echo $pecah1['nama'] . "<br>";
if ($pset['ppn'] == 1 ) {
$nominal_tagihan = $tagihan;
$jumlah_tagihan = $tagihan;
$ppn = 0;
$total_tagihan = $jumlah_tagihan + $ppn;
$kode_unik = 0;
$verifikasi_tagihan = $total_tagihan + $kode_unik;
}
elseif($pset['ppn'] == 2 ) {
$nominal_tagihan = $tagihan;
$jumlah_tagihan = $tagihan;
$ppn = $jumlah_tagihan / 100 * 11;
$total_tagihan = $jumlah_tagihan + $ppn;
$kode_unik = 0;
$verifikasi_tagihan = $total_tagihan + $kode_unik;
}
elseif($pset['ppn'] == 3 ) {
$nominal_tagihan = $tagihan;
$jumlah_tagihan = $tagihan - ($tagihan / 100 * 9.91);
$ppn = $jumlah_tagihan / 100 * 11;
$total_tagihan = $jumlah_tagihan + $ppn;
$kode_unik = 0;
$verifikasi_tagihan = $total_tagihan + $kode_unik;
}
elseif($pset['ppn'] == 4 ) {
$nominal_tagihan = $tagihan;
$jumlah_tagihan = $tagihan;
$ppn = $jumlah_tagihan / 100 * 11;
$total_tagihan = $jumlah_tagihan + $ppn;
$kode_unik = substr($total_tagihan, -3);
$verifikasi_tagihan = $total_tagihan - $kode_unik;
}
$datatag = substr($sendtag1, 0, 8) . $pset['limit_tagihan'];
$dataLim = new DateTime($datatag);
$dataLim->modify('-1 day'); // Mengurangi 1 hari
$limit_tag = $dataLim->format('Y-m-d');
$id_data_server = $pecah1['id_data_server'];
$id_pelanggan = $pecah1['id'];
$id_profile_paket = $pecah1['id_profile_paket'];
$id_profile_tagihan = $pecah1['id_profile_tagihan'];
$keterangan = "Tagihan Layanan Internet bulan " . formatIndo($limit_tag, 'F Y');
$status = 'buat';
$stmt = $pdo->prepare("INSERT INTO tagihan
(id_data_server, id_pelanggan, id_profile_paket, id_profile_tagihan, nominal_tagihan, jumlah_tagihan, ppn, total_tagihan, kode_unik, verifikasi_tagihan, keterangan, limit_pembayaran, status)
VALUES (:id_data_server, :id_pelanggan, :id_profile_paket, :id_profile_tagihan, :nominal_tagihan, :jumlah_tagihan, :ppn, :total_tagihan, :kode_unik, :verifikasi_tagihan, :keterangan, :limit_pembayaran, :status)
");
$stmt->execute([
':id_data_server' => $id_data_server,
':id_pelanggan' => $id_pelanggan,
':id_profile_paket' => $id_profile_paket,
':id_profile_tagihan' => $id_profile_tagihan,
':nominal_tagihan' => $nominal_tagihan,
':jumlah_tagihan' => $jumlah_tagihan,
':ppn' => $ppn,
':total_tagihan' => $total_tagihan,
':kode_unik' => $kode_unik,
':verifikasi_tagihan' => $verifikasi_tagihan,
':keterangan' => $keterangan,
':limit_pembayaran' => $limit_tag,
':status' => $status,
]);
}
}
} else {
echo "NO ACTION => " . $pset['id_data_server'] . " <-> " . $pset['nama'] . "<br>";
}
// END BUAT TAGIHAN
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
+497
View File
@@ -0,0 +1,497 @@
<?php
/*****************************
*
* RouterOS PHP API class v1.6
* Author: Denis Basta
* Contributors:
* Nick Barnes
* Ben Menking (ben [at] infotechsc [dot] com)
* Jeremy Jefferson (http://jeremyj.com)
* Cristian Deluxe (djcristiandeluxe [at] gmail [dot] com)
* Mikhail Moskalev (mmv.rus [at] gmail [dot] com)
*
* http://www.mikrotik.com
* http://wiki.mikrotik.com/wiki/API_PHP_class
*
******************************/
class RouterosAPI
{
var $debug = false; // Show debug information
var $connected = false; // Connection state
var $port = 8728; // Port to connect to (default 8729 for ssl)
var $ssl = false; // Connect using SSL (must enable api-ssl in IP/Services)
var $timeout = 2; // Connection attempt timeout and data read timeout
var $attempts = 2; // Connection attempt count
var $delay = 2; // Delay between connection attempts in seconds
var $socket; // Variable for storing socket resource
var $error_no; // Variable for storing connection error number, if any
var $error_str; // Variable for storing connection error text, if any
/* Check, can be var used in foreach */
public function isIterable($var)
{
return $var !== null
&& (is_array($var)
|| $var instanceof Traversable
|| $var instanceof Iterator
|| $var instanceof IteratorAggregate
);
}
/**
* Print text for debug purposes
*
* @param string $text Text to print
*
* @return void
*/
public function debug($text)
{
if ($this->debug) {
echo $text . "\n";
}
}
/**
*
*
* @param string $length
*
* @return void
*/
public function encodeLength($length)
{
if ($length < 0x80) {
$length = chr($length);
} elseif ($length < 0x4000) {
$length |= 0x8000;
$length = chr(($length >> 8) & 0xFF) . chr($length & 0xFF);
} elseif ($length < 0x200000) {
$length |= 0xC00000;
$length = chr(($length >> 16) & 0xFF) . chr(($length >> 8) & 0xFF) . chr($length & 0xFF);
} elseif ($length < 0x10000000) {
$length |= 0xE0000000;
$length = chr(($length >> 24) & 0xFF) . chr(($length >> 16) & 0xFF) . chr(($length >> 8) & 0xFF) . chr($length & 0xFF);
} elseif ($length >= 0x10000000) {
$length = chr(0xF0) . chr(($length >> 24) & 0xFF) . chr(($length >> 16) & 0xFF) . chr(($length >> 8) & 0xFF) . chr($length & 0xFF);
}
return $length;
}
/**
* Login to RouterOS
*
* @param string $ip Hostname (IP or domain) of the RouterOS server
* @param string $login The RouterOS username
* @param string $password The RouterOS password
*
* @return boolean If we are connected or not
*/
public function connect($ip, $login, $password)
{
for ($ATTEMPT = 1; $ATTEMPT <= $this->attempts; $ATTEMPT++) {
$this->connected = false;
$PROTOCOL = ($this->ssl ? 'ssl://' : '' );
$context = stream_context_create(array('ssl' => array('ciphers' => 'ADH:ALL', 'verify_peer' => false, 'verify_peer_name' => false)));
$this->debug('Connection attempt #' . $ATTEMPT . ' to ' . $PROTOCOL . $ip . ':' . $this->port . '...');
$this->socket = @stream_socket_client($PROTOCOL . $ip.':'. $this->port, $this->error_no, $this->error_str, $this->timeout, STREAM_CLIENT_CONNECT,$context);
if ($this->socket) {
socket_set_timeout($this->socket, $this->timeout);
$this->write('/login', false);
$this->write('=name=' . $login, false);
$this->write('=password=' . $password);
$RESPONSE = $this->read(false);
if (isset($RESPONSE[0])) {
if ($RESPONSE[0] == '!done') {
if (!isset($RESPONSE[1])) {
// Login method post-v6.43
$this->connected = true;
break;
} else {
// Login method pre-v6.43
$MATCHES = array();
if (preg_match_all('/[^=]+/i', $RESPONSE[1], $MATCHES)) {
if ($MATCHES[0][0] == 'ret' && strlen($MATCHES[0][1]) == 32) {
$this->write('/login', false);
$this->write('=name=' . $login, false);
$this->write('=response=00' . md5(chr(0) . $password . pack('H*', $MATCHES[0][1])));
$RESPONSE = $this->read(false);
if (isset($RESPONSE[0]) && $RESPONSE[0] == '!done') {
$this->connected = true;
break;
}
}
}
}
}
}
fclose($this->socket);
}
sleep($this->delay);
}
if ($this->connected) {
$this->debug('Connected...');
} else {
$this->debug('Error...');
}
return $this->connected;
}
/**
* Disconnect from RouterOS
*
* @return void
*/
public function disconnect()
{
// let's make sure this socket is still valid. it may have been closed by something else
if( is_resource($this->socket) ) {
fclose($this->socket);
}
$this->connected = false;
$this->debug('Disconnected...');
}
/**
* Login to RouterOS
*
* @param string $ip Hostname (IP or domain) of the RouterOS server
* @param string $login The RouterOS username
* @param string $password The RouterOS password
* @param string $portx The RouterOS port
*
* @return boolean If we are connected or not
*/
public function connect2($ip, $login, $password, $portx = '8728')
{
for ($ATTEMPT = 1; $ATTEMPT <= $this->attempts; $ATTEMPT++) {
$this->connected = false;
$PROTOCOL = ($this->ssl ? 'ssl://' : '' );
$context = stream_context_create(array('ssl' => array('ciphers' => 'ADH:ALL', 'verify_peer' => false, 'verify_peer_name' => false)));
$this->debug('Connection attempt #' . $ATTEMPT . ' to ' . $PROTOCOL . $ip . ':' . $portx . '...');
$this->socket = @stream_socket_client($PROTOCOL . $ip.':'. $portx, $this->error_no, $this->error_str, $this->timeout, STREAM_CLIENT_CONNECT,$context);
if ($this->socket) {
socket_set_timeout($this->socket, $this->timeout);
$this->write('/login', false);
$this->write('=name=' . $login, false);
$this->write('=password=' . $password);
$RESPONSE = $this->read(false);
if (isset($RESPONSE[0])) {
if ($RESPONSE[0] == '!done') {
if (!isset($RESPONSE[1])) {
// Login method post-v6.43
$this->connected = true;
break;
} else {
// Login method pre-v6.43
$MATCHES = array();
if (preg_match_all('/[^=]+/i', $RESPONSE[1], $MATCHES)) {
if ($MATCHES[0][0] == 'ret' && strlen($MATCHES[0][1]) == 32) {
$this->write('/login', false);
$this->write('=name=' . $login, false);
$this->write('=response=00' . md5(chr(0) . $password . pack('H*', $MATCHES[0][1])));
$RESPONSE = $this->read(false);
if (isset($RESPONSE[0]) && $RESPONSE[0] == '!done') {
$this->connected = true;
break;
}
}
}
}
}
}
fclose($this->socket);
}
sleep($this->delay);
}
if ($this->connected) {
$this->debug('Connected...');
} else {
$this->debug('Error...');
}
return $this->connected;
}
/**
* Parse response from Router OS
*
* @param array $response Response data
*
* @return array Array with parsed data
*/
public function parseResponse($response)
{
if (is_array($response)) {
$PARSED = array();
$CURRENT = null;
$singlevalue = null;
foreach ($response as $x) {
if (in_array($x, array('!fatal','!re','!trap'))) {
if ($x == '!re') {
$CURRENT =& $PARSED[];
} else {
$CURRENT =& $PARSED[$x][];
}
} elseif ($x != '!done') {
$MATCHES = array();
if (preg_match_all('/[^=]+/i', $x, $MATCHES)) {
if ($MATCHES[0][0] == 'ret') {
$singlevalue = $MATCHES[0][1];
}
$CURRENT[$MATCHES[0][0]] = (isset($MATCHES[0][1]) ? $MATCHES[0][1] : '');
}
}
}
if (empty($PARSED) && !is_null($singlevalue)) {
$PARSED = $singlevalue;
}
return $PARSED;
} else {
return array();
}
}
/**
* Parse response from Router OS
*
* @param array $response Response data
*
* @return array Array with parsed data
*/
public function parseResponse4Smarty($response)
{
if (is_array($response)) {
$PARSED = array();
$CURRENT = null;
$singlevalue = null;
foreach ($response as $x) {
if (in_array($x, array('!fatal','!re','!trap'))) {
if ($x == '!re') {
$CURRENT =& $PARSED[];
} else {
$CURRENT =& $PARSED[$x][];
}
} elseif ($x != '!done') {
$MATCHES = array();
if (preg_match_all('/[^=]+/i', $x, $MATCHES)) {
if ($MATCHES[0][0] == 'ret') {
$singlevalue = $MATCHES[0][1];
}
$CURRENT[$MATCHES[0][0]] = (isset($MATCHES[0][1]) ? $MATCHES[0][1] : '');
}
}
}
foreach ($PARSED as $key => $value) {
$PARSED[$key] = $this->arrayChangeKeyName($value);
}
return $PARSED;
if (empty($PARSED) && !is_null($singlevalue)) {
$PARSED = $singlevalue;
}
} else {
return array();
}
}
/**
* Change "-" and "/" from array key to "_"
*
* @param array $array Input array
*
* @return array Array with changed key names
*/
public function arrayChangeKeyName(&$array)
{
if (is_array($array)) {
foreach ($array as $k => $v) {
$tmp = str_replace("-", "_", $k);
$tmp = str_replace("/", "_", $tmp);
if ($tmp) {
$array_new[$tmp] = $v;
} else {
$array_new[$k] = $v;
}
}
return $array_new;
} else {
return $array;
}
}
/**
* Read data from Router OS
*
* @param boolean $parse Parse the data? default: true
*
* @return array Array with parsed or unparsed data
*/
public function read($parse = true)
{
$RESPONSE = array();
$receiveddone = false;
while (true) {
// Read the first byte of input which gives us some or all of the length
// of the remaining reply.
$BYTE = ord(fread($this->socket, 1));
$LENGTH = 0;
// If the first bit is set then we need to remove the first four bits, shift left 8
// and then read another byte in.
// We repeat this for the second and third bits.
// If the fourth bit is set, we need to remove anything left in the first byte
// and then read in yet another byte.
if ($BYTE & 128) {
if (($BYTE & 192) == 128) {
$LENGTH = (($BYTE & 63) << 8) + ord(fread($this->socket, 1));
} else {
if (($BYTE & 224) == 192) {
$LENGTH = (($BYTE & 31) << 8) + ord(fread($this->socket, 1));
$LENGTH = ($LENGTH << 8) + ord(fread($this->socket, 1));
} else {
if (($BYTE & 240) == 224) {
$LENGTH = (($BYTE & 15) << 8) + ord(fread($this->socket, 1));
$LENGTH = ($LENGTH << 8) + ord(fread($this->socket, 1));
$LENGTH = ($LENGTH << 8) + ord(fread($this->socket, 1));
} else {
$LENGTH = ord(fread($this->socket, 1));
$LENGTH = ($LENGTH << 8) + ord(fread($this->socket, 1));
$LENGTH = ($LENGTH << 8) + ord(fread($this->socket, 1));
$LENGTH = ($LENGTH << 8) + ord(fread($this->socket, 1));
}
}
}
} else {
$LENGTH = $BYTE;
}
$_ = "";
// If we have got more characters to read, read them in.
if ($LENGTH > 0) {
$_ = "";
$retlen = 0;
while ($retlen < $LENGTH) {
$toread = $LENGTH - $retlen;
$_ .= fread($this->socket, $toread);
$retlen = strlen($_);
}
$RESPONSE[] = $_;
$this->debug('>>> [' . $retlen . '/' . $LENGTH . '] bytes read.');
}
// If we get a !done, make a note of it.
if ($_ == "!done") {
$receiveddone = true;
}
$STATUS = socket_get_status($this->socket);
if ($LENGTH > 0) {
$this->debug('>>> [' . $LENGTH . ', ' . $STATUS['unread_bytes'] . ']' . $_);
}
if ((!$this->connected && !$STATUS['unread_bytes']) || ($this->connected && !$STATUS['unread_bytes'] && $receiveddone)) {
break;
}
}
if ($parse) {
$RESPONSE = $this->parseResponse($RESPONSE);
}
return $RESPONSE;
}
/**
* Write (send) data to Router OS
*
* @param string $command A string with the command to send
* @param mixed $param2 If we set an integer, the command will send this data as a "tag"
* If we set it to boolean true, the funcion will send the comand and finish
* If we set it to boolean false, the funcion will send the comand and wait for next command
* Default: true
*
* @return boolean Return false if no command especified
*/
public function write($command, $param2 = true)
{
if ($command) {
$data = explode("\n", $command);
foreach ($data as $com) {
$com = trim($com);
fwrite($this->socket, $this->encodeLength(strlen($com)) . $com);
$this->debug('<<< [' . strlen($com) . '] ' . $com);
}
if (gettype($param2) == 'integer') {
fwrite($this->socket, $this->encodeLength(strlen('.tag=' . $param2)) . '.tag=' . $param2 . chr(0));
$this->debug('<<< [' . strlen('.tag=' . $param2) . '] .tag=' . $param2);
} elseif (gettype($param2) == 'boolean') {
fwrite($this->socket, ($param2 ? chr(0) : ''));
}
return true;
} else {
return false;
}
}
/**
* Write (send) data to Router OS
*
* @param string $com A string with the command to send
* @param array $arr An array with arguments or queries
*
* @return array Array with parsed
*/
public function comm($com, $arr = array())
{
$count = count($arr);
$this->write($com, !$arr);
$i = 0;
if ($this->isIterable($arr)) {
foreach ($arr as $k => $v) {
switch ($k[0]) {
case "?":
$el = "$k=$v";
break;
case "~":
$el = "$k~$v";
break;
default:
$el = "=$k=$v";
break;
}
$last = ($i++ == $count - 1);
$this->write($el, $last);
}
}
return $this->read();
}
/**
* Standard destructor
*
* @return void
*/
public function __destruct()
{
$this->disconnect();
}
}
+126
View File
@@ -0,0 +1,126 @@
<?php
function formatIndo($tanggal, $format = 'd F Y') {
// Array nama bulan dalam bahasa Indonesia
$bulan = array(
1 => 'Januari',
2 => 'Februari',
3 => 'Maret',
4 => 'April',
5 => 'Mei',
6 => 'Juni',
7 => 'Juli',
8 => 'Agustus',
9 => 'September',
10 => 'Oktober',
11 => 'November',
12 => 'Desember'
);
// Mengubah string tanggal ke objek DateTime
$date = new DateTime($tanggal);
// Format bulan dalam bahasa Indonesia
$bulanIndo = $bulan[$date->format('n')];
// Format tanggal sesuai parameter format
switch ($format) {
case 'd F Y':
return $date->format('d') . ' ' . $bulanIndo . ' ' . $date->format('Y');
case 'F Y':
return $bulanIndo . ' ' . $date->format('Y');
default:
return $date->format($format);
}
}
function penyebut($nilai) {
$nilai = abs($nilai);
$huruf = array("", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
$temp = "";
if ($nilai < 12) {
$temp = " ". $huruf[$nilai];
} else if ($nilai <20) {
$temp = penyebut($nilai - 10). " belas";
} else if ($nilai < 100) {
$temp = penyebut($nilai/10)." puluh". penyebut($nilai % 10);
} else if ($nilai < 200) {
$temp = " seratus" . penyebut($nilai - 100);
} else if ($nilai < 1000) {
$temp = penyebut($nilai/100) . " ratus" . penyebut($nilai % 100);
} else if ($nilai < 2000) {
$temp = " seribu" . penyebut($nilai - 1000);
} else if ($nilai < 1000000) {
$temp = penyebut($nilai/1000) . " ribu" . penyebut($nilai % 1000);
} else if ($nilai < 1000000000) {
$temp = penyebut($nilai/1000000) . " juta" . penyebut($nilai % 1000000);
} else if ($nilai < 1000000000000) {
$temp = penyebut($nilai/1000000000) . " milyar" . penyebut(fmod($nilai,1000000000));
} else if ($nilai < 1000000000000000) {
$temp = penyebut($nilai/1000000000000) . " trilyun" . penyebut(fmod($nilai,1000000000000));
}
return $temp;
}
function terbilang($nilai) {
if($nilai<0) {
$hasil = "minus ". trim(penyebut($nilai));
} else {
$hasil = trim(penyebut($nilai));
}
return $hasil;
}
function baseurl($path = '') {
// Ambil URL dasar aplikasi
$baseurl = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// Gabungkan dengan path tambahan jika ada
if ($path) {
// Menghapus leading slashes dari path untuk menghindari double slashes
$path = ltrim($path, '/');
// Gabungkan base URL dengan path
$baseurl = rtrim($baseurl, '/') . '/' . $path;
}
return $baseurl;
}
function intervalFormat($input) {
// Menangkap jumlah dan satuan
preg_match('/(\d+)([DHM])/', $input, $matches);
if (count($matches) !== 3) {
return "Format tidak valid";
}
$amount = intval($matches[1]);
$unit = strtolower($matches[2]); // Mengubah satuan menjadi huruf kecil
// Inisialisasi waktu
$days = 0;
$hours = 0;
$minutes = 0;
$seconds = 0;
switch ($unit) {
case 'd':
$days = $amount;
break;
case 'h':
$hours = $amount;
break;
case 'm':
$minutes = $amount;
break;
}
// Format output
if ($days > 0) {
return sprintf("%d%s%02d:%02d:%02d", $days, $unit, $hours, $minutes, $seconds);
} else {
return sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);
}
}
?>