Full Code of phprouter/main for AI

main 44481ae28d63 cached
10 files
7.2 KB
2.0k tokens
10 symbols
1 requests
Download .txt
Repository: phprouter/main
Branch: main
Commit: 44481ae28d63
Files: 10
Total size: 7.2 KB

Directory structure:
gitextract_s62tl7hk/

├── .htaccess
├── LICENSE
├── README.md
├── api/
│   └── save_user.php
├── router.php
├── routes.php
└── views/
    ├── 404.php
    ├── full_name.php
    ├── index.php
    └── user.php

================================================
FILE CONTENTS
================================================

================================================
FILE: .htaccess
================================================
RewriteEngine On
RewriteCond %{REQUEST_URI}  !(\.png|\.jpg|\.webp|\.gif|\.jpeg|\.zip|\.css|\.svg|\.js|\.pdf)$
RewriteRule (.*) routes.php [QSA,L]


================================================
FILE: LICENSE
================================================
MIT License

Copyright (c) 2021 <info@phprouter.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


================================================
FILE: README.md
================================================
# PHP ROUTER

Secure router with XSS and CSRF

1. Download the file ".htaccess" and place it under the root directory (html, htdocs, or www) of your web server

2. Download the file "router.php" and place it under the root directory (html, htdocs, or www) of your web server

3. Download the file "routes.php" and place it under the root directory (html, htdocs, or www) of your web server

In the browser go to "localhost" or "127.0.0.1" and you should see the word "Index" displayed in the website.

Feel free to delete all the routes in the "routes.php" file and create your own. Most likely you want to keep the last route for "Page not found".

For details about routing, visit https://phprouter.com


================================================
FILE: api/save_user.php
================================================
<?php
echo $_POST['user_name'];
echo 'user saved';

================================================
FILE: router.php
================================================
<?php

function get($route, $path_to_include)
{
	if ($_SERVER['REQUEST_METHOD'] == 'GET') {
		route($route, $path_to_include);
	}
}
function post($route, $path_to_include)
{
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		route($route, $path_to_include);
	}
}
function put($route, $path_to_include)
{
	if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
		route($route, $path_to_include);
	}
}
function patch($route, $path_to_include)
{
	if ($_SERVER['REQUEST_METHOD'] == 'PATCH') {
		route($route, $path_to_include);
	}
}
function delete($route, $path_to_include)
{
	if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
		route($route, $path_to_include);
	}
}
function any($route, $path_to_include)
{
	route($route, $path_to_include);
}
function route($route, $path_to_include)
{
	$callback = $path_to_include;
	if (!is_callable($callback)) {
		if (!strpos($path_to_include, '.php')) {
			$path_to_include .= '.php';
		}
	}
	if ($route == "/404") {
		include_once __DIR__ . "/$path_to_include";
		exit();
	}
	$request_url = filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL);
	$request_url = rtrim($request_url, '/');
	$request_url = strtok($request_url, '?');
	$route_parts = explode('/', $route);
	$request_url_parts = explode('/', $request_url);
	array_shift($route_parts);
	array_shift($request_url_parts);
	if ($route_parts[0] == '' && count($request_url_parts) == 0) {
		// Callback function
		if (is_callable($callback)) {
			call_user_func_array($callback, []);
			exit();
		}
		include_once __DIR__ . "/$path_to_include";
		exit();
	}
	if (count($route_parts) != count($request_url_parts)) {
		return;
	}
	$parameters = [];
	for ($__i__ = 0; $__i__ < count($route_parts); $__i__++) {
		$route_part = $route_parts[$__i__];
		if (preg_match("/^[$]/", $route_part)) {
			$route_part = ltrim($route_part, '$');
			array_push($parameters, $request_url_parts[$__i__]);
			$$route_part = $request_url_parts[$__i__];
		} else if ($route_parts[$__i__] != $request_url_parts[$__i__]) {
			return;
		}
	}
	// Callback function
	if (is_callable($callback)) {
		call_user_func_array($callback, $parameters);
		exit();
	}
	include_once __DIR__ . "/$path_to_include";
	exit();
}
function out($text)
{
	echo htmlspecialchars($text);
}

function set_csrf()
{
	session_start();
	if (!isset($_SESSION["csrf"])) {
		$_SESSION["csrf"] = bin2hex(random_bytes(50));
	}
	echo '<input type="hidden" name="csrf" value="' . $_SESSION["csrf"] . '">';
}

function is_csrf_valid()
{
	session_start();
	if (!isset($_SESSION['csrf']) || !isset($_POST['csrf'])) {
		return false;
	}
	if ($_SESSION['csrf'] != $_POST['csrf']) {
		return false;
	}
	return true;
}


================================================
FILE: routes.php
================================================
<?php

require_once __DIR__.'/router.php';

// ##################################################
// ##################################################
// ##################################################

// Static GET
// In the URL -> http://localhost
// The output -> Index
get('/', 'views/index.php');

// Dynamic GET. Example with 1 variable
// The $id will be available in user.php
get('/user/$id', 'views/user');

// Dynamic GET. Example with 2 variables
// The $name will be available in full_name.php
// The $last_name will be available in full_name.php
// In the browser point to: localhost/user/X/Y
get('/user/$name/$last_name', 'views/full_name.php');

// Dynamic GET. Example with 2 variables with static
// In the URL -> http://localhost/product/shoes/color/blue
// The $type will be available in product.php
// The $color will be available in product.php
get('/product/$type/color/$color', 'product.php');

// A route with a callback
get('/callback', function(){
  echo 'Callback executed';
});

// A route with a callback passing a variable
// To run this route, in the browser type:
// http://localhost/user/A
get('/callback/$name', function($name){
  echo "Callback executed. The name is $name";
});

// Route where the query string happends right after a forward slash
get('/product', '');

// A route with a callback passing 2 variables
// To run this route, in the browser type:
// http://localhost/callback/A/B
get('/callback/$name/$last_name', function($name, $last_name){
  echo "Callback executed. The full name is $name $last_name";
});

// ##################################################
// ##################################################
// ##################################################
// Route that will use POST data
post('/user', '/api/save_user');



// ##################################################
// ##################################################
// ##################################################
// any can be used for GETs or POSTs

// For GET or POST
// The 404.php which is inside the views folder will be called
// The 404.php has access to $_GET and $_POST
any('/404','views/404.php');


================================================
FILE: views/404.php
================================================
<?php
// To call this page, in the browser type a route that doesn't exist like:
// http://localhost/test/route

echo 'PAGE NOT FOUND';

================================================
FILE: views/full_name.php
================================================
<?php
// To call this page, in the browser type:
// http://localhost/user/A/B

echo "USER IN VIEWS: $name $last_name";

================================================
FILE: views/index.php
================================================
<?php
// To call this page, in the browser type:
// http://localhost/

echo 'INDEX';

================================================
FILE: views/user.php
================================================
<?php
// To call this page, in the browser type:
// http://localhost/user/1

echo "USER IN VIEWS WITH ID: $id";
Download .txt
gitextract_s62tl7hk/

├── .htaccess
├── LICENSE
├── README.md
├── api/
│   └── save_user.php
├── router.php
├── routes.php
└── views/
    ├── 404.php
    ├── full_name.php
    ├── index.php
    └── user.php
Download .txt
SYMBOL INDEX (10 symbols across 1 files)

FILE: router.php
  function get (line 3) | function get($route, $path_to_include)
  function post (line 9) | function post($route, $path_to_include)
  function put (line 15) | function put($route, $path_to_include)
  function patch (line 21) | function patch($route, $path_to_include)
  function delete (line 27) | function delete($route, $path_to_include)
  function any (line 33) | function any($route, $path_to_include)
  function route (line 37) | function route($route, $path_to_include)
  function out (line 87) | function out($text)
  function set_csrf (line 92) | function set_csrf()
  function is_csrf_valid (line 101) | function is_csrf_valid()
Condensed preview — 10 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (8K chars).
[
  {
    "path": ".htaccess",
    "chars": 146,
    "preview": "RewriteEngine On\nRewriteCond %{REQUEST_URI}  !(\\.png|\\.jpg|\\.webp|\\.gif|\\.jpeg|\\.zip|\\.css|\\.svg|\\.js|\\.pdf)$\nRewriteRul"
  },
  {
    "path": "LICENSE",
    "chars": 1077,
    "preview": "MIT License\n\nCopyright (c) 2021 <info@phprouter.com>\n\nPermission is hereby granted, free of charge, to any person obtain"
  },
  {
    "path": "README.md",
    "chars": 705,
    "preview": "# PHP ROUTER\n\nSecure router with XSS and CSRF\n\n1. Download the file \".htaccess\" and place it under the root directory (h"
  },
  {
    "path": "api/save_user.php",
    "chars": 52,
    "preview": "<?php\r\necho $_POST['user_name'];\r\necho 'user saved';"
  },
  {
    "path": "router.php",
    "chars": 2739,
    "preview": "<?php\r\n\r\nfunction get($route, $path_to_include)\r\n{\r\n\tif ($_SERVER['REQUEST_METHOD'] == 'GET') {\r\n\t\troute($route, $path_t"
  },
  {
    "path": "routes.php",
    "chars": 2221,
    "preview": "<?php\r\n\r\nrequire_once __DIR__.'/router.php';\r\n\r\n// ##################################################\r\n// ##############"
  },
  {
    "path": "views/404.php",
    "chars": 139,
    "preview": "<?php\r\n// To call this page, in the browser type a route that doesn't exist like:\r\n// http://localhost/test/route\r\n\r\nech"
  },
  {
    "path": "views/full_name.php",
    "chars": 122,
    "preview": "<?php\r\n// To call this page, in the browser type:\r\n// http://localhost/user/A/B\r\n\r\necho \"USER IN VIEWS: $name $last_name"
  },
  {
    "path": "views/index.php",
    "chars": 88,
    "preview": "<?php\r\n// To call this page, in the browser type:\r\n// http://localhost/\r\n\r\necho 'INDEX';"
  },
  {
    "path": "views/user.php",
    "chars": 115,
    "preview": "<?php\r\n// To call this page, in the browser type:\r\n// http://localhost/user/1\r\n\r\necho \"USER IN VIEWS WITH ID: $id\";"
  }
]

About this extraction

This page contains the full source code of the phprouter/main GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 10 files (7.2 KB), approximately 2.0k tokens, and a symbol index with 10 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!