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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
| <?php
class UssdNode {
private $name = '';
private $parent = '';
private $title = '';
private $address = '';
private $children = array();
private $index = 0;
function __construct($title, $name, $parent) {
$this->name = $name;
$this->parent = $parent;
$this->title = $title;
}
function addChildNode(UssdNode $childNode) {
$this->children[] = $childNode;
}
function setIndex($index) {
$this->index = $index;
}
function hasChildren() {
return count($this->children) > 0;
}
function setAddress($address) {
$this->address = $address;
}
function getName() {
return $this->name;
}
function getParent() {
return $this->parent;
}
function getAddress() {
return $this->address;
}
function getTitle() {
return $this->title;
}
function getChildren() {
return $this->children;
}
function getNameFromIndex($index) {
return $this->children[$index - 1]->getName();
}
function toString() {
$objectString = '';
$items = $this->children;
$bufferLimit = (count($items) == 0) ? 1 : $this->getBufferLimit() + 1;
do {
$bufferLimit -= 1;
$objectString = $this->recurseMenu($items, $bufferLimit);
} while (strlen($objectString) > 160);
$this->index = $bufferLimit;
return $objectString;
}
function getBufferLimit() {
$len = count($this->children);
$margin = $len - $this->index;
if ($margin < 5)
return $this->index + $margin;
else
return $this->index + 5; // LIMITE LE NOMBRE DE MENU A AFFICHER
}
function recurseMenu($items, $bufferLimit) {
$objectString = $this->getTitle() . PHP_EOL;
$lastMenu = false;
if (count($items) > 0) {
for ($i = $this->index; $i < $bufferLimit; $i++) {
$item = $items[$i];
$num = $i + 1;
// get node by name
$userSessions = $_SESSION['userSessions'];
$currUserSession = $userSessions[$this->address];
$node = $currUserSession->getNode($item->getName());
$title = $node->getTitle();
$objectString = $objectString . PHP_EOL . $num . '. ' . $title;
}
} else {
$objectString = $objectString . PHP_EOL . 'NO DATA AVAILABLE, TRY AGAIN LATER';
}
$lastMenu = $bufferLimit == count($items);
$objectString = $objectString . PHP_EOL . PHP_EOL . '0. Exit';
if ($this->getParent() != '0') {
$objectString = $objectString . PHP_EOL . '#. Back';
}
if ($lastMenu === false) {
$rem = count($items) - $this->getBufferLimit();
$objectString = $objectString . PHP_EOL . '77. Next (' . $rem . ')';
}
return $objectString;
}
}
class MyTree {
private $rootNode;
function __construct() {
$this->initTree();
}
function initTree() {
$rootNode = new UssdNode('Welcome to Cheggen USSD Menu', 'root', '0');
$childNode1 = new UssdNode('Option 1', 'option1', 'root');
$childNode1->addChildNode(new UssdNode('Sub-option 1', 'suboption1', 'option1'));
$childNode1->addChildNode(new UssdNode('Sub-option 2', 'suboption2', 'option1'));
$childNode2 = new UssdNode('Option 2', 'option2', 'root');
$childNode2->addChildNode(new UssdNode('Sub-option 3', 'suboption3', 'option2'));
$childNode2->addChildNode(new UssdNode('Sub-option 4', 'suboption4', 'option2'));
$childNode3 = new UssdNode('Option 3', 'option3', 'root');
$childNode3->addChildNode(new UssdNode('Sub-option 4', 'suboption4', 'option3'));
$childNode3->addChildNode(new UssdNode('Sub-option 5', 'suboption5', 'option3'));
$childNode4 = new UssdNode('Option 4', 'option4', 'root');
$childNode4->addChildNode(new UssdNode('Sub-option 6', 'suboption6', 'option4'));
$childNode4->addChildNode(new UssdNode('Sub-option 7', 'suboption7', 'option4'));
$childNode5 = new UssdNode('Option 5', 'option5', 'root');
$childNode5->addChildNode(new UssdNode('Sub-option 8', 'suboption8', 'option5'));
$childNode5->addChildNode(new UssdNode('Sub-option 9', 'suboption9', 'option5'));
$childNode6 = new UssdNode('Option 6', 'option6', 'root');
$childNode6->addChildNode(new UssdNode('Sub-option 10', 'suboption10', 'option6'));
$childNode6->addChildNode(new UssdNode('Sub-option 11', 'suboption11', 'option6'));
$childNode7 = new UssdNode('Option 7', 'option7', 'root');
$childNode7->addChildNode(new UssdNode('Sub-option 12', 'suboption12', 'option7'));
$childNode7->addChildNode(new UssdNode('Sub-option 13', 'suboption13', 'option7'));
$rootNode->addChildNode($childNode1);
$rootNode->addChildNode($childNode2);
$rootNode->addChildNode($childNode3);
$rootNode->addChildNode($childNode4);
$rootNode->addChildNode($childNode5);
$rootNode->addChildNode($childNode6);
$rootNode->addChildNode($childNode7);
$this->rootNode = $rootNode;
}
function getRootNode() {
return $this->rootNode;
}
}
class MyUssdUserSession {
private $ussdTree;
private $currentNode;
private $address;
function __construct($address, $ussdTree) {
$this->address = $address;
$this->ussdTree = $ussdTree;
$this->currentNode = $ussdTree->getRootNode();
}
function getNode($input) {
$children = $this->currentNode->getChildren();
foreach ($children as $index => $child) {
if ((is_int($input) ? $index : $child->getName()) == $input) {
return $child;
}
}
return null;
}
function processUssdInput($input) {
// Check if Detect A Page Refresh Or New Page Entered
$is_page_refreshed = (isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] == 'max-age=0');
// var_dump($is_page_refreshed);
if(session_status() === PHP_SESSION_ACTIVE) { // Check if Session is Enabled (Active)
if($is_page_refreshed) { // If this Page has been Refresh (true)
// Exit
session_destroy();
}
}
if ($input === '') {
// Initial menu
return $this->currentNode->toString();
} elseif ($input === '0' or $input === 0) {
// Exit
return 'Thanks, end of session';
session_destroy();
} elseif ($input === '#') { // 'Invalid input';
// Back
$parentName = $this->currentNode->getParent();
$parentNode = $this->getNode($parentName);
if ($parentNode !== null) {
$this->currentNode = $parentNode;
return $this->currentNode->toString();
} else {
return 'Invalid input';
}
} elseif ($input === '77' || $input === 77) {
// Next
$index = $this->currentNode->getIndex() + 5;
$this->currentNode->setIndex($index);
return $this->currentNode->toString();
} else {
// Check if the input matches any child node
$childNode = $this->getNode($input);
if ($childNode !== null) {
$this->currentNode = $childNode;
return $this->currentNode->toString();
} else {
return 'Invalid input';
}
}
}
}
// Start the USSD session
session_start();
// Create the USSD tree
$ussdTree = new MyTree();
// Get the user's address (phone number)
$address = $_POST['address'] ?? '';
// Create or retrieve the user session
$userSessions = $_SESSION['userSessions'] ?? [];
$userSession = $userSessions[$address] ?? null;
if ($userSession === null) {
$userSession = new MyUssdUserSession($address, $ussdTree);
$userSessions[$address] = $userSession;
$_SESSION['userSessions'] = $userSessions;
}
// Get the USSD input from the request
// $input = '#';
$input = $_POST['ussdInput'] ?? '';
// Process the USSD input and get the response
$response = $userSession->processUssdInput($input);
// Send the response back to the USSD gateway
header('Content-type: text/plain');
echo $response; |
Partager