GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — php-example-applications/php-directory-sync-example/router.php at main · workos/php-example-applications

Expand file tree

/

router.php

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
177 lines (155 loc) · 6.29 KB
/

router.php

Copy path

File metadata and controls

177 lines (155 loc) · 6.29 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
require __DIR__ . "/vendor/autoload.php";
error_reporting(E_ALL ^ E_WARNING);
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
include './variables.php';
// Setup html templating library
$loader = new FilesystemLoader(__DIR__ . '/templates');
$twig = new Environment($loader);
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
//Set API Key, ClientID, and Connection
$WORKOS_API_KEY = $_ENV['WORKOS_API_KEY'];
$WORKOS_CLIENT_ID = $_ENV['WORKOS_CLIENT_ID'];
$WORKOS_WEBHOOKS_SECRET = $_ENV['WORKOS_WEBHOOKS_SECRET'];
// Configure WorkOS with API Key and Client ID
\WorkOS\WorkOS::setApiKey($WORKOS_API_KEY);
\WorkOS\WorkOS::setClientId($WORKOS_CLIENT_ID);
// Convenient function for throwing a 404
function httpNotFound()
{
header($_SERVER["SERVER_PROTOCOL"] . " 404");
return true;
}
// Convenient function to transform an object to an associative array
function objectToArray($d)
{
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
} else {
// Return array
return $d;
}
}
// Routing
switch (strtok($_SERVER["REQUEST_URI"], "?")) {
case (preg_match("/\.css$/", $_SERVER["REQUEST_URI"]) ? true : false):
$path = __DIR__ . "/static/css" .$_SERVER["REQUEST_URI"];
if (is_file($path)) {
header("Content-Type: text/css");
readfile($path);
return true;
}
return httpNotFound();
case (preg_match("/\.png$/", $_SERVER["REQUEST_URI"]) ? true : false):
$path = __DIR__ . "/static/images" .$_SERVER["REQUEST_URI"];
if (is_file($path)) {
header("Content-Type: image/png");
readfile($path);
return true;
}
return httpNotFound();
// home and /login will display the login page
case ("/"):
session_start();
$before = $_GET['before'] ?? "";
$after = $_GET['after'] ?? "";
$directoriesList = new WorkOS\DirectorySync();
[$before, $after, $currentPage] = $directoriesList->listDirectories(
limit: 5,
before: $before,
after: $after,
order: null
);
$parsedDirectories = $currentPage;
echo $twig->render("login.html.twig", ['directories' => $parsedDirectories, 'after' => $after, 'before' => $before]);
return true;
//Directory endpoint
case ("/directory"):
session_start();
$directoryId = htmlspecialchars($_GET["id"]);
$directory = (new \WorkOS\DirectorySync())
->getDirectory(
$directoryId
);
$directoryPayloadArray = objectToArray($directory);
$directoryPayloadArrayRawData = $directoryPayloadArray['raw'];
$parsed_directory = json_encode($directoryPayloadArrayRawData, JSON_PRETTY_PRINT);
$directoryName = $directoryPayloadArrayRawData["name"] ?? "";
$directoryType = $directoryPayloadArrayRawData["type"] ?? "";
$directoryDomain = $directoryPayloadArrayRawData["domain"] ?? "";
$directoryCreated = $directoryPayloadArrayRawData["created_at"] ?? "";
$_SESSION['id'] = $directoryId;
echo $twig->render('directory.html', ['directory' => $parsed_directory, 'id' => $_SESSION['id'], 'name'=> $directoryName, 'type'=>$directoryType, 'domain'=>$directoryDomain, 'created_at'=>$directoryCreated]);
return true;
//Groups & Users endpoint for listGroups & listUsers function
case ("/users"):
session_start();
$directoryId = $_GET["id"];
$directory = (new \WorkOS\DirectorySync())
->getDirectory(
$directoryId
);
$parsed_directory = json_encode($directory, JSON_PRETTY_PRINT);
$directoryPayloadArray = objectToArray($directory);
$directoryPayloadArrayRawData = $directoryPayloadArray['raw'];
$directoryName = $directoryPayloadArrayRawData["name"] ?? "";
$_SESSION['directoryName'] = $directoryName;
[$before, $after, $users] = (new \WorkOS\DirectorySync())
->listUsers(
$directoryId
);
$usersArr = objectToArray($users);
echo $twig->render('users.html.twig', ['users' => $usersArr, 'name' => $_SESSION['directoryName'], 'directory' => $directoryId]);
return true;
case ("/groups"):
session_start();
$directoryId = $_GET["id"];
$directory = (new \WorkOS\DirectorySync())
->getDirectory(
$directoryId
);
$parsed_directory = json_encode($directory, JSON_PRETTY_PRINT);
$directoryPayloadArray = objectToArray($directory);
$directoryPayloadArrayRawData = $directoryPayloadArray['raw'];
$directoryName = $directoryPayloadArrayRawData["name"] ?? "";
$_SESSION['directoryName'] = $directoryName;
[$before, $after, $groups] = (new \WorkOS\DirectorySync())
->listGroups(
$directoryId
);
$groupsArr = objectToArray($groups);
echo $twig->render('groups.html.twig', ['groups' => $groupsArr, 'name' => $_SESSION['directoryName'], 'directory' => $directoryId]);
return true;
//Webhooks endpoint
case ("/webhooks"):
$payload = file_get_contents('php://input');
$sigHeader = $_SERVER["HTTP_WORKOS_SIGNATURE"];
if ($payload && $sigHeader) {
$webhook = (new \WorkOS\Webhook())
->constructEvent($sigHeader, $payload, $WORKOS_WEBHOOKS_SECRET, 180);
error_log(print_r($WORKOS_ASCII, true));
error_log(print_r("↓↓↓↓ PRE-VALIDATION ↓↓↓↓", true));
error_log(print_r($payload, true));
error_log(print_r("↓↓↓↓ POST-VALIDATION ↓↓↓↓", true));
error_log(print_r($webhook, true));
echo $twig->render('webhooks.html.twig', ['webhook' => json_encode($data)]);
return true;
}
echo $twig->render('webhooks.html.twig');
return true;
// Any other endpoint returns a 404
default:
return httpNotFound();
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.