Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
O
operate-php
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
examples
operate-php
Commits
52c43bda
Commit
52c43bda
authored
Sep 14, 2017
by
Paul Bird
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
initial commit
parents
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
373 additions
and
0 deletions
+373
-0
index.php
index.php
+172
-0
oauth2.php
oauth2.php
+201
-0
No files found.
index.php
0 → 100755
View file @
52c43bda
<?php
$url
=
strtok
(
$_SERVER
[
"REQUEST_URI"
],
'?'
);
require_once
getcwd
()
.
'/oauth2.php'
;
$oauth2
=
new
Oauth2
(
array
(
'occupieUrl'
=>
'https://api.occupie.com'
,
'client'
=>
'trustedclient'
,
'clientSecret'
=>
''
,
'scope'
=>
'hubapi'
));
?>
<!DOCTYPE html>
<html>
<head>
<title>
Occupie API sample
</title>
<link
href=
"http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.min.css"
rel=
"stylesheet"
>
</head>
<body
class=
"container"
>
<div
class=
"navbar"
>
<div
class=
"navbar-inner"
>
<ul
class=
"nav"
>
<?php
if
(
$oauth2
->
isUserAuthenticated
())
{
?>
<li><a
href=
"
<?php
echo
$url
;
?>
"
>
API
</a></li>
<?php
}
?>
<!-- <li><a href="/#/oauth2Config">OAuth2 config</a></li> -->
<?php
if
(
!
$oauth2
->
isUserAuthenticated
())
{
?>
<li><a
href=
"
<?php
echo
$url
;
?>
"
>
Login
</a></li>
<?php
}
if
(
$oauth2
->
isUserAuthenticated
())
{
?>
<li><a
href=
"
<?php
echo
$url
;
?>
?logout"
>
Logout
</a></li>
<?php
}
if
(
$oauth2
->
isUserAuthenticated
())
{
?>
<li><a
href=
"
<?php
echo
$url
;
?>
?refreshToken"
>
Refresh token
</a></li>
<?php
}
?>
</ul>
</div>
</div>
<div
class=
"row"
>
<div
class=
"span12"
>
<div
class=
"well"
>
<?php
if
(
!
$oauth2
->
isUserAuthenticated
())
{
?>
<h3>
Log in
</h3>
<p>
Logging in retrieves access token for the provided credentials. This token is then used for all calls to api, so credentials don't have to be kept.
</p>
<form
action=
"
<?php
echo
$url
;
?>
"
method=
"post"
>
<div>
<label
for=
"user"
>
Username:
</label>
<input
type=
"text"
name=
"user"
id=
"user"
value=
"name@domain.com"
>
</div>
<div>
<label
for=
"pass"
>
Password:
</label>
<input
type=
"password"
name=
"pass"
id=
"pass"
value=
"replaceme"
>
</div>
<input
type=
"submit"
name=
"login"
value=
"Log in"
class=
"btn btn-default"
>
</form>
<?php
}
else
{
?>
<div
class=
"navbar"
>
<div
class=
"navbar-inner"
>
<ul
class=
"nav"
>
<li><a
href=
"
<?php
echo
$url
;
?>
"
>
Tracking devices
</a></li>
<li><a
href=
"
<?php
echo
$url
;
?>
?create"
>
Create tracking device
</a></li>
</ul>
</div>
</div>
<?php
if
(
isset
(
$_GET
[
'create'
]))
{
?>
<h3>
Create Tracking Device
</h3>
<form
action=
"
<?php
echo
$url
;
?>
?create"
method=
"post"
>
<div>
<label
for=
"name"
>
Name:
</label>
<input
type=
"text"
name=
"trackingdevicename"
id=
"trackingdevicename"
required
placeholder=
"Some string"
>
</div>
<div>
<label
for=
""
>
Type:
</label>
<input
type=
"text"
name=
"trackingtype"
id=
"trackingtype"
required
placeholder=
"IN, OUT or IN/OUT"
>
</div>
<div>
<label
for=
"uid"
>
Unique id:
</label>
<input
type=
"text"
name=
"uniquedeviceid"
id=
"uniquedeviceid"
required
placeholder=
"Some string"
>
</div>
<div>
<label
for=
"siteid"
>
Site id:
</label>
<input
type=
"text"
name=
"siteid"
id=
"siteid"
required
placeholder=
"Some number"
>
</div>
<div>
<label
for=
"active"
>
Active:
</label>
<input
type=
"text"
name=
"active"
id=
"active"
required
placeholder=
"True or False"
>
</div>
<input
type=
"submit"
name=
"create"
value=
"Create"
class=
"btn btn-default"
>
</form>
<?php
}
elseif
(
isset
(
$_GET
[
'entries'
]))
{
?>
<h3>
Tracking Devices Entries
</h3>
<table
class=
"table table-striped"
>
<thead>
<tr>
<th>
Id
</th>
<th>
Name
</th>
<th>
Type
</th>
<th>
Time
</th>
</tr>
</thead>
<tbody>
<?php
if
(
$oauth2
->
trackingEntries
)
foreach
(
$oauth2
->
trackingEntries
as
$entry
)
{
?>
<tr>
<td>
{{entry.trackingentriesid}}
</td>
<td>
{{entry.trackingentriesname}}
</td>
<td>
{{entry.trackingtype}}
</td>
<td>
{{entry.creationdate}}
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
elseif
(
isset
(
$_GET
[
'add'
]))
{
?>
<h3>
Add Tracking Entry
</h3>
<form
action=
"
<?php
echo
$url
;
?>
?add=
<?php
echo
$_GET
[
'add'
];
?>
"
method=
"post"
>
<div>
<label
for=
"trackingentriesname"
>
Name:
</label>
<input
type=
"text"
name=
"trackingentriesname"
id=
"trackingentriesname"
required
placeholder=
"Some string"
>
</div>
<div>
<label
for=
"trackingtype"
>
Type:
</label>
<input
type=
"text"
name=
"trackingtype"
id=
"trackingtype"
required
placeholder=
"IN or OUT"
>
</div>
<div>
<label
for=
"contactid"
>
Contact id:
</label>
<input
type=
"text"
name=
"contactid"
id=
"contactid"
required
placeholder=
"Some number"
>
</div>
<input
type=
"submit"
name=
"add"
value=
"Create"
class=
"btn btn-default"
>
</form>
<?php
}
else
{
?>
<div>
<h3>
Tracking Devices
</h3>
<?php
if
(
$oauth2
->
trackingDevices
)
{
?>
<table
class=
"table table-striped"
>
<thead>
<tr>
<th>
Id
</th>
<th>
name
</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php
foreach
(
$oauth2
->
trackingDevices
as
$key
=>
$value
)
{
?>
<tr>
<td>
<?php
echo
$value
->
trackingdeviceid
;
?>
</td>
<td>
<?php
echo
$value
->
trackingdevicename
;
?>
</td>
<td>
<a
href=
"
<?php
echo
$url
;
?>
?entries=
<?php
echo
$value
->
trackingdeviceid
;
?>
"
>
show entries
</a>
</td>
<td>
<a
href=
"
<?php
echo
$url
;
?>
?add=
<?php
echo
$value
->
trackingdeviceid
;
?>
"
>
add entry
</a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
else
{
?>
<p>
No data!
</p>
<?php
}
?>
</div>
<?php
}
?>
<?php
}
?>
</div>
</div>
</div>
</body>
</html>
oauth2.php
0 → 100755
View file @
52c43bda
<?php
class
Oauth2
{
private
$occupieUrl
=
false
;
private
$client
=
false
;
private
$clientSecret
=
false
;
private
$scope
=
false
;
private
$accessToken
=
false
;
private
$refreshToken
=
false
;
public
$trackingDevices
=
false
;
public
$trackingEntries
=
false
;
public
function
__construct
(
$config
)
{
if
(
!
is_array
(
$config
))
die
(
'Oauth2 needs configuring.'
);
foreach
(
$config
as
$id
=>
$value
)
{
if
(
property_exists
(
'Oauth2'
,
$id
)
&&
is_string
(
$value
)
&&
$id
!==
'accessToken'
&&
$id
!==
'refreshToken'
)
$this
->
$id
=
$value
;
}
$this
->
session
();
$this
->
postHandler
();
if
(
isset
(
$_GET
[
'logout'
]))
$this
->
logout
();
elseif
(
isset
(
$_GET
[
'refreshToken'
]))
$this
->
refreshAccessToken
(
true
);
elseif
(
isset
(
$_GET
[
'entries'
]))
$this
->
getTrackingDeviceEntries
(
$_GET
[
'entries'
]);
if
(
$this
->
isUserAuthenticated
())
$this
->
fetchAllDevices
();
}
public
function
__destruct
()
{
$_SESSION
[
'refreshToken'
]
=
$this
->
refreshToken
;
}
private
function
session
()
{
$session
=
session_id
();
if
(
empty
(
$session
))
session_start
();
if
(
!
empty
(
$_SESSION
[
'refreshToken'
]))
{
$this
->
refreshToken
=
$_SESSION
[
'refreshToken'
];
$this
->
refreshAccessToken
();
}
}
private
function
postHandler
()
{
if
(
isset
(
$_POST
[
'login'
]))
{
$this
->
login
(
$_POST
[
'user'
],
$_POST
[
'pass'
]);
}
elseif
(
isset
(
$_POST
[
'create'
]))
{
$this
->
createTrackingDevice
(
$_POST
[
'trackingdevicename'
],
$_POST
[
'trackingtype'
],
$_POST
[
'uniquedeviceid'
],
$_POST
[
'siteid'
],
$_POST
[
'active'
]);
}
elseif
(
isset
(
$_POST
[
'add'
]))
{
$this
->
createTrackingDeviceEntry
(
$_POST
[
'trackingentriesname'
],
$_POST
[
'trackingtype'
],
$_POST
[
'contactid'
]);
}
unset
(
$_POST
);
}
private
function
logout
()
{
$this
->
accessToken
=
$this
->
refreshToken
=
false
;
session_unset
();
session_destroy
();
header
(
'Location: '
.
strtok
(
$_SERVER
[
"REQUEST_URI"
],
'?'
));
exit
;
}
private
function
login
(
$user
,
$pass
,
$responseHeaders
=
null
)
{
$query
=
http_build_query
(
array
(
'grant_type'
=>
'password'
,
'response_type'
=>
'access_token'
,
'client_id'
=>
$this
->
client
,
'client_secret'
=>
$this
->
clientSecret
,
'scope'
=>
$this
->
scope
,
'username'
=>
$user
,
'password'
=>
$pass
),
''
,
'&'
);
$response
=
$this
->
httpRequest
(
$this
->
getTokenEndpoint
(),
array
(
'method'
=>
'POST'
,
'header'
=>
array
(
'Content-type: application/x-www-form-urlencoded'
),
'content'
=>
$query
),
$responseHeaders
);
if
(
$response
)
{
$this
->
accessToken
=
$response
->
access_token
;
$this
->
refreshToken
=
$response
->
refresh_token
;
echo
'Login success - Response: <pre>'
.
var_export
(
$response
,
true
)
.
'</pre>'
;
}
else
{
echo
'Login error - Response: <pre>'
.
var_export
(
$responseHeaders
,
true
)
.
'</pre>'
;
}
}
private
function
refreshAccessToken
(
$manual
=
false
)
{
$query
=
http_build_query
(
array
(
'grant_type'
=>
'refresh_token'
,
'client_id'
=>
$this
->
client
,
'client_secret'
=>
$this
->
clientSecret
,
'scope'
=>
$this
->
scope
,
'refresh_token'
=>
$this
->
refreshToken
),
''
,
'&'
);
$response
=
$this
->
httpRequest
(
$this
->
getTokenEndpoint
(),
array
(
'method'
=>
'POST'
,
'header'
=>
array
(
'Content-type: application/x-www-form-urlencoded'
),
'content'
=>
$query
),
$responseHeaders
);
if
(
$response
)
{
$this
->
accessToken
=
$response
->
access_token
;
$this
->
refreshToken
=
$response
->
refresh_token
;
if
(
$manual
)
echo
'Access token refreshed - Response: <pre>'
.
var_export
(
$response
,
true
)
.
'</pre>'
;
}
else
{
echo
'Access token error - Response: <pre>'
.
var_export
(
$responseHeaders
,
true
)
.
'</pre>'
;
}
}
private
function
fetchAllDevices
()
{
$options
=
array
(
'method'
=>
'GET'
,
'header'
=>
array
(
'Authorization: Bearer '
.
$this
->
accessToken
,
'Accept: application/json'
)
);
$response
=
$this
->
httpRequest
(
$this
->
getTrackingDeviceApiUri
(),
$options
,
$responseHeaders
);
if
(
$response
)
{
$this
->
trackingDevices
=
$response
->
response
->
trackingdevices
;
//echo 'Tracking devices fetched - Response: <code>'.var_export($this->trackingDevices, true).'</code>';
}
else
{
echo
'Error fetching tracking devices - Response: <pre>'
.
var_export
(
$responseHeaders
,
true
)
.
'</pre>'
;
}
}
private
function
createTrackingDevice
(
$trackingdevicename
,
$trackingtype
,
$uniquedeviceid
,
$siteid
,
$active
)
{
$data
=
'[{'
.
"trackingdevicename:'
{
$trackingdevicename
}
',"
.
"trackingtype:'
{
$trackingtype
}
',"
.
"uniquedeviceid:'
{
$uniquedeviceid
}
',"
.
"siteid:'
{
$siteid
}
',"
.
"active:'
{
$active
}
'"
.
'}]'
;
$options
=
array
(
'method'
=>
'PUT'
,
'header'
=>
array
(
'Authorization: Bearer '
.
$this
->
accessToken
,
'Content-Type: text/plain'
,
'Accept: application/json'
),
'content'
=>
'data={trackingdevices:'
.
$data
.
'}'
);
$response
=
$this
->
httpRequest
(
$this
->
getTrackingDeviceApiUri
(),
$options
,
$responseHeaders
);
if
(
$response
)
{
echo
'Tracking device created - Response: <pre>'
.
var_export
(
$response
,
true
)
.
'</pre>'
;
}
else
{
echo
'Error creating tracking device - Response: <pre>'
.
var_export
(
$responseHeaders
,
true
)
.
'</pre>'
;
}
}
private
function
getTrackingDeviceEntries
(
$deviceId
)
{
$options
=
array
(
'method'
=>
'GET'
,
'header'
=>
array
(
'Authorization: Bearer '
.
$this
->
accessToken
,
'Accept: application/json'
)
);
$response
=
$this
->
httpRequest
(
$this
->
getTrackingEntryApiUri
()
.
"?where=trackingdeviceid=
{
$deviceId
}
"
,
$options
,
$responseHeaders
);
if
(
$response
)
{
$this
->
trackingEntries
=
$response
->
response
->
trackingentriess
;
echo
'Tracking device entries fetched - Response: <code>'
.
var_export
(
$this
->
trackingEntries
,
true
)
.
'</code>'
;
}
else
{
echo
'Error fetching tracking device entries - Response: <pre>'
.
var_export
(
$responseHeaders
,
true
)
.
'</pre>'
;
}
}
private
function
createTrackingDeviceEntry
(
$trackingentriesname
,
$trackingtype
,
$contactid
)
{
$data
=
'[{'
.
"trackingdeviceid:'
{
$_GET
[
'add'
]
}
',"
.
"trackingentriesname:'
{
$trackingentriesname
}
',"
.
"trackingtype:'
{
$trackingtype
}
',"
.
"contactid:'
{
$contactid
}
'"
.
'}]'
;
$options
=
array
(
'method'
=>
'PUT'
,
'header'
=>
array
(
'Authorization: Bearer '
.
$this
->
accessToken
,
'Content-Type: text/plain'
,
'Accept: application/json'
),
'content'
=>
'data={trackingentries:'
.
$data
.
'}'
);
$response
=
$this
->
httpRequest
(
$this
->
getTrackingEntryApiUri
(),
$options
,
$responseHeaders
);
if
(
$response
)
{
echo
'Entry created - Response: <pre>'
.
var_export
(
$response
,
true
)
.
'</pre>'
;
}
else
{
echo
'Error fetching tracking devices - Response: <pre>'
.
var_export
(
$responseHeaders
,
true
)
.
'</pre>'
;
}
}
private
function
httpRequest
(
$uri
,
$options
=
null
,
&
$responseHeaders
=
null
,
$raw
=
false
)
{
$context
=
null
;
if
(
!
empty
(
$options
)
&&
is_array
(
$options
))
{
$context
=
stream_context_create
(
array
(
'http'
=>
$options
));
}
$content
=
@
file_get_contents
(
$uri
,
false
,
$context
);
$responseHeaders
=
implode
(
"
\r\n
"
,
$http_response_header
);
return
$raw
?
$content
:
json_decode
(
$content
);
}
private
function
getTokenEndpoint
()
{
return
$this
->
occupieUrl
.
'/oauth2/token'
;
}
private
function
getTrackingDeviceApiUri
()
{
return
$this
->
occupieUrl
.
'/api/1.0/trackingdevice'
;
}
private
function
getTrackingEntryApiUri
()
{
return
$this
->
occupieUrl
.
'/api/1.0/trackingentries'
;
}
public
function
isUserAuthenticated
()
{
return
$this
->
accessToken
!==
false
;
}
}
?>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment