Anyone want to help me write a new geofencing script?

If you've made a patch to quick fix a bug or to add a new feature not yet in the main tree then post it here so others can try it out.
Post Reply
kennbr34
Posts: 48
Joined: Sat Jan 14, 2017 6:43 pm

Anyone want to help me write a new geofencing script?

Post by kennbr34 »

So I used to use the method outlined here:

viewtopic.php?t=25499

The problem is the authentication method has changed in >= 1.3.4.0

I've figured out that someone would need to get the auth API token in order to authenticate and then curl the runstate.json API change link with the token affixed to the end. I figured out how to do it with bash as the following...

Code: Select all

curl -XGET  http://localhost/zm/api/states/change/Home.json?token="$(curl -XPOST -d "user=user&pass=pass" http://localhost/zm/api/host/login.json | sed 's/{//g' | sed "s/\"access_token\"\:\"//g" | cut -b 1-195)"
To change the runstate to 'Home'

I don't know any PHP and so was wondering if someone wanted to use this information to modify the PHP script in order to authenticate and change the runstate via API. I feel like it's probably pretty elementary to get the access token and then curl with it as suggested, but I figured I'd ask and see if someone can help me out before I end up learning PHP and doing it myself.
kennbr34
Posts: 48
Joined: Sat Jan 14, 2017 6:43 pm

Re: Anyone want to help me write a new geofencing script?

Post by kennbr34 »

Well, I don't like using CGI but I actually know how to use it so I put together this cgi version and stuck it into ZM's cgi-bin. I think I have it set to properly sanitize the input values, since by printing only what matches the regex "runstate=[[:alpha:]]\{1,\}" with grep, it will exclude any other value, and only print the alphabetical part of the string before the '&'. So in other words you can enter '/zm/cgi-bin/setruntime.sh?amigonnatrytohackyou=yes&runstate=Away&didihackyou=no' and it will only grab 'runstate=Away' and apply the 'Away' portion after the equal sign, and just throw the other values away.

Code: Select all

#!/bin/bash

USER=user
PASS=pass
HOST=localhost
PROT=http

echo "Content-type: text/html"
echo ""

echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
echo '<title>Change ZM Runstate</title>'
echo '</head>'
echo '<body>'

echo $QUERY_STRING | grep runstate > /dev/null

if [ $? -gt 0 ]; then
        echo 'Runstate needed'
        exit 1
fi

RUNSTATE=`echo $QUERY_STRING | grep -o "runstate=[[:alpha:]]\{1,\}" | sed "s/runstate=//g"`
ACCESS_TOKEN=`curl -XPOST -d "user=$USER&pass=$PASS" $PROT://$HOST/zm/api/host/login.json | sed 's/{//g' | sed "s/\"access_token\"\:\"//g" | cut -b 1-195`

curl -XGET  $PROT://$HOST/zm/api/states/change/$RUNSTATE.json?token="$ACCESS_TOKEN" > /dev/null

echo '</body>'
echo '</html>'

exit 0
Post Reply