Community discussions

MikroTik App
 
Hassan1
just joined
Topic Author
Posts: 16
Joined: Fri Apr 11, 2014 10:20 pm

How to get userman End time & Till time values using php API

Mon Jan 05, 2015 4:52 pm

Hello everyone ,

can you help me get user manager End time & Till time values using php API

Thanks in advance
You do not have the required permissions to view the files attached to this post.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: How to get userman End time & Till time values using php

Mon Jan 05, 2015 7:57 pm

It's analogous to how you'd take it from the command line.
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

$util = new RouterOS\Util($client = new RouterOS\Client('router', 'username', 'password'));
$items = $util->setMenu('/tool user-manager session')->getAll(array('.proplist' => 'user,till-time'));

foreach ($items as $item) {
   echo 'Username ' . $item('user') . ' will have internet until ' . $item('till-time') . ";\n";
}
However, at least on my router (6.23), I don't see "end-time" value... Is it perhaps dynamically computed somehow? From the screenshot you have, "End time" is the same as "Till time", so I can't figure out how this value would be computed.
 
Hassan1
just joined
Topic Author
Posts: 16
Joined: Fri Apr 11, 2014 10:20 pm

Re: How to get userman End time & Till time values using php

Mon Jan 05, 2015 11:33 pm

The difference between End-time and till-time is that the end-time is the end of the current active profile but till-time give the end time of all user profiles together if he has more than one.

I tried your code it's shows the current session till-time which always is the same date and time I run the code
not users profile till-time as shown in attached screenshot

Thanks Boen robot
You do not have the required permissions to view the files attached to this post.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: How to get userman End time & Till time values using php

Tue Jan 06, 2015 3:40 pm

I don't use user manager honestly, so I have trouble finding the exact thing needed here...

Instead of looking at the GUI, you should look at the command line. In Winbox (well... I think WebFig works too, but it's clunky), click "New Terminal". Go to "/tool user-manager", and press the Tab key to see possibilities. Then do some "print" calls around in there, so see where what you want is.

Here's the key parts I'm seeing (minus the "print" calls, since I don't have any actual data defined)...
[admin@MikroTik] > /tool user-manager
[admin@MikroTik] /tool user-manager> 
customer  history  payment  router   user 
database  log      profile  session  export
[admin@MikroTik] /tool user-manager> user
add      create-and-activate-profile  edit    export  print   reset-counters
comment  disable                      enable  find    remove  set          
[admin@MikroTik] /tool user-manager> user get value-name=
active                       first-name       reg-key          
active-sessions              incomplete       registration-date
actual-profile               ip-address       shared-users    
caller-id                    last-name        upload-used      
caller-id-bind-on-first-use  last-seen        uptime-used      
comment                      location         username         
customer                     name             wireless-enc-algo
disabled                     password         wireless-enc-key 
download-used                phone            wireless-psk     
email                        random-password 
[admin@MikroTik] /tool user-manager> profile            
limitation          add      edit    find   remove
profile-limitation  comment  export  print  set  
[admin@MikroTik] /tool user-manager> profile get value-name=
comment  name-for-users         owner  starts-at
name     override-shared-users  price  validity 
[admin@MikroTik] /tool user-manager> profile profile-limitation 
add  comment  edit  export  find  print  remove  set
[admin@MikroTik] /tool user-manager> profile profile-limitation get value-name=
comment  from-time  limitation  profile  till-time  weekdays
[admin@MikroTik] /tool user-manager>
It seems "end time" is dynamically computed based on what the last till-time is, and the command line does not display it from any menu. As for a user's till-time values, these are apparently characteristics of a profile-limitation, applicable to the user's actual-profile, so...
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

$util = new RouterOS\Util($client = new RouterOS\Client('router', 'username', 'password'));
$userManDateTimeFormat = 'm/d/Y H:i:s';

$users = $util->setMenu('/tool user-manager user')->getAll(array('.proplist' => 'name,actual-profile'));

foreach ($users as $user) {
   echo 'Username ' . $user('name') . ' will have internet until ';

   $profileLimits = $util->setMenu('/tool user-manager profile profile-limitation')->getAll(array('.proplist' => 'till-time'), RouterOS\Query::where('profile', $user('actual-profile')));
   $endTime = DateTime::createFromFormat($userManDateTimeFormat, $profileLimits->current()->getProperty('till-time'));
   while ($profileLimits->next()) {
       $nextEndTime = DateTime::createFromFormat($userManDateTimeFormat, $profileLimits->current()->getProperty('till-time'));
       if ($nextEndTime > $endTime) {
           $endTime = $nextEndTime;
       }
   }

   echo $endTime->format($userManDateTimeFormat);
}
(Or maybe I'm misunderstanding again, but to assist further, I'd need a CLI equivalent to base the rest on...)
 
Hassan1
just joined
Topic Author
Posts: 16
Joined: Fri Apr 11, 2014 10:20 pm

Re: How to get userman End time & Till time values using php

Wed Jan 07, 2015 7:29 am

I did your way to find the right CLI without success I think it's not been implemented yet ؟ :?
[admin@MikroTik] /tool user-manager> user get value-name=
active                               first-name           reg-key          
active-sessions              incomplete          registration-date
actual-profile                  ip-address          shared-users    
caller-id                           last-name            upload-used      
caller-id-bind-on-first-use                          last-seen
uptime-used                  comment             location         
username                      customer              name             
wireless-enc-algo          disabled               password         
wireless-enc-key           download-used   phone            
wireless-psk                   email                    random-password 
My goal is to send SMS using PHP API to remind my client 1 day before the end date of internet
or they can check when subscription will end

I know that my English cause you some confusion, but forgive me this is my best . :D
Thanks for your help
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: How to get userman End time & Till time values using php

Wed Jan 07, 2015 2:50 pm

Your English is fine. It's my lack of understanding user-manager that's the real problem.

While what you want may not be as easy to obtain as a simple "get", it might still be possible to obtain in some roundabout way. That's why I asked you to do some "print" calls around, which is to say, see the output of
/tool user-manager
customer print
user print
session print
profile print
profile profile-limitation print
and see if you can "manually" spot the end time for a particular user. If you can describe how you do it "manually", automating it would be easy.


Looking at the fact that "user" is our base, I'm thinking maybe check out the active sessions? Like:
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

$util = new RouterOS\Util($client = new RouterOS\Client('router', 'username', 'password'));
$userManDateTimeFormat = 'm/d/Y H:i:s';

$users = $util->setMenu('/tool user-manager user')->getAll(array('.proplist' => 'name,active-sessions'));

foreach ($users as $user) {
   echo 'Username ' . $user('name');

   $activeSessions = $util->setMenu('/tool user-manager session')->getAll(array('from' => $user('active-sessions')));
   if (count($activeSessions) === 0) {
       echo ' does not have internet currently.';
   } else {
       $endTime = DateTime::createFromFormat($userManDateTimeFormat, $activeSessions->current()->getProperty('till-time'));
       while ($activeSessions->next()) {
           $nextEndTime = DateTime::createFromFormat($userManDateTimeFormat, $activeSessions->current()->getProperty('till-time'));
           if ($nextEndTime > $endTime) {
               $endTime = $nextEndTime;
           }
       }

       echo ' will have internet until ' . $endTime->format($userManDateTimeFormat);
    }
}
With the above, if the user has multiple active sessions, you should get the time at which the last one ends.

Who is online

Users browsing this forum: mkrtksr and 5 guests