Difference between revisions of "API PHP package"

From MikroTik Wiki
Jump to: navigation, search
(Example 2: Changing hotspot user passwords)
(Client)
Line 1: Line 1:
 
= Client =
 
= Client =
 
The examples on this page use the [http://pear2.github.com/Net_RouterOS/ PEAR2_Net_RouterOS] package. You can install it with the PEAR(2) installer, or just download and extract the files from the "src" directory wherever you wish.
 
The examples on this page use the [http://pear2.github.com/Net_RouterOS/ PEAR2_Net_RouterOS] package. You can install it with the PEAR(2) installer, or just download and extract the files from the "src" directory wherever you wish.
 +
 +
If you use the PEAR(2) installer, all dependencies will be installed automatically from the pear2.php.net channel. They are also included in the archive, so if you just extract the archive, there's nothing extra you need to get.
  
 
All examples assume that the files from the "src" folder are somehow directly accessible to your PHP file (either via editing your include_path, or by simply extracting the contents of the "src" folder where your own PHP file is). Also, the router is assumed to be accessible with a local IP to the device PHP runs from. The client itself could work without these restrictions - they are specified here for clarity.
 
All examples assume that the files from the "src" folder are somehow directly accessible to your PHP file (either via editing your include_path, or by simply extracting the contents of the "src" folder where your own PHP file is). Also, the router is assumed to be accessible with a local IP to the device PHP runs from. The client itself could work without these restrictions - they are specified here for clarity.

Revision as of 15:37, 1 February 2012

Client

The examples on this page use the PEAR2_Net_RouterOS package. You can install it with the PEAR(2) installer, or just download and extract the files from the "src" directory wherever you wish.

If you use the PEAR(2) installer, all dependencies will be installed automatically from the pear2.php.net channel. They are also included in the archive, so if you just extract the archive, there's nothing extra you need to get.

All examples assume that the files from the "src" folder are somehow directly accessible to your PHP file (either via editing your include_path, or by simply extracting the contents of the "src" folder where your own PHP file is). Also, the router is assumed to be accessible with a local IP to the device PHP runs from. The client itself could work without these restrictions - they are specified here for clarity.

NOTE: The client requires PHP 5.3.0 or later.

Example 1: An HTML form to make RouterOS ping someone

This example is particularly useful when you want to ping someone from inside the network while browsing the page from outside the network.

<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';

if (isset($_GET['act'])) {//This is merely to ensure the form was submitted.

    //Adjust RouterOS IP, username and password accordingly.
    $client = new Client('192.168.0.1', 'admin', 'password');

    //This is just one approach that allows you to create a multy purpose form,
    //with ping being just one action.
    if ($_GET['act'] === 'Ping' && isset($_GET['address'])) {
        //Ping can run for unlimited time, but for PHP,
        //we need to actually stop it at some point.
        $pingRequest = new Request('/ping count=3');
        $results = $client->sendSync($pingRequest->setArgument('address', $_GET['address']));
    }
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Ping someone</title>
    </head>
    <body>
        <div>
            <form action="" method="get">
                <ul>
                    <li>
                        <label for="address">Address:</label>
                        <input type="text" id="address" name="address" value="<?php
                            if (isset($_GET['address'])) {
                                echo htmlspecialchars($_GET['address']);
                            }
                        ?>" />
                    <li>
                    <li>
                        <input type="submit" id="act" name="act" value="Ping" />
                    </li>
                </ul>
            </form>
        </div>
        <?php
if (isset($_GET['act'])) {//There's no need to execute this if the form was not submitted yet.
    echo '<div>Results:<ul>';
    foreach ($results as $result) {
        //Add whatever you want displayed in this section.
        echo '<li>Time:', $result->getArgument('time'), '</li>';
    }
    echo '</ul></div>';
}
        ?>
    </body>
</html>

Example 2: Changing hotspot user passwords

The script assumes you have already made a hotspot and do NOT make this file accessible in a walled garden, i.e. users must be logged in to access it.

<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';

$errors = array();

try {
    //Adjust RouterOS IP, username and password accordingly.
    $client = new Client('192.168.0.1', 'admin', 'password');
    $activeUserEntry = $client->sendSync(new Request(
        '/ip hotspot active print', null, Query::where('address', $_SERVER['REMOTE_ADDR'])
    ));
} catch(\Exception $e) {
    $errors[] = $e->toString();
}

if (isset($_POST['password']) && isset($_POST['password2'])) {
    if ($_POST['password'] !== $_POST['password2']) {
        $errors[] = 'Passwords do not match.';
    } else {
        //Here's the fun part - actually changing the password
        $setRequest = new Request('/ip hotspot users set');
        $client($setRequest
            ->setArgument('password', $_POST['password'])
            ->setArgument('numbers', $client(new Request(
                '/ip hotspot users print .proplist=.id', null,
                Query::where('name', $activeUserEntry->getArgument('name'))
            ))->getArgument('.id'))
        );
    }
}

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Change your hotspot password</title>
        <style type="text/css">#errors {background-color:darkred;color:white;}</style>
    </head>
    <body>
        <div>
            <?php if (!isset($activeUserEntry)) { ?>
                <h1>We're sorry, but we can't change your password right now.
                Please try again later</h1>
            <?php } else { ?>
                <h1>You are currently logged in as "<?php
                    echo $activeUserEntry->getArgument('name');
                ?>"</h1>
                <?php if(!empty($errors)) { ?>
                    <div id="errors"><ul>
                        <?php foreach ($errors as $error) { ?>
                        <li><?php echo $error; ?></li>
                        <?php } ?>
                    </ul></div>
                <?php } ?>
                <form action="" method="post">
                    <ul>
                        <li>
                            <label for="address">New password:</label>
                            <input type="password" id="password" name="password" value="" />
                        <li>
                        <li>
                            <label for="address">Confirm new password:</label>
                            <input type="password" id="password2" name="password2" value="" />
                        <li>
                        <li>
                            <input type="submit" id="act" name="act" value="Change password" />
                        </li>
                     </ul>
                </form>
            <?php } ?>
        </div>
    </body>
</html>