Browse Source

api code from main repo

master
Sergio Álvarez 5 months ago
commit
28e102b5e6
  1. 1
      Procfile
  2. 31
      README.md
  3. 1
      composer.json
  4. 72
      index.php
  5. 218
      preg.php
  6. 5
      redis.php

1
Procfile

@ -0,0 +1 @@
web: $(composer config bin-dir)/heroku-php-nginx -v -C nginx_app.conf build/

31
README.md

@ -0,0 +1,31 @@
Code for [RegExp Live tester](https://xrg.es).
Install: `npm i`
Dev server: `npm start`
Build: `npm run build`
Simple nginx dev server:
```
server {
listen 80;
server_name localhost;
root /var/www/;
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
```
And then:
```bash
$ mkdir /var/www/xrg.es
$ sudo mount --bind api/ /var/www/xrg.es
```

1
composer.json

@ -0,0 +1 @@
{}

72
index.php

@ -0,0 +1,72 @@
<?php
// https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-dynamodb-2012-08-10.html
// https://github.com/aws/aws-sdk-php/blob/master/src/DynamoDb
use Aws\DynamoDb\DynamoDbClient;
use Aws\DynamoDb\Marshaler;
$ddb = DynamoDbClient::factory(array(
'version' => 'latest',
'region' => 'eu-west-3'
));
$m = new Marshaler(['nullify_invalid' => true]);
require_once dirname(__FILE__) ."/preg.php";
function response(array $data): string
{
header("Content-Type: application/json");
// AWS API Gateway expected response
// https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-integration-settings-integration-response.html
return (json_encode([
'statusCode' => 200,
'headers' => [
'Access-Control-Allow-Origin' => 'https://re.xrg.es'
],
'body' => json_encode($data)
]));
}
function main(array $data): string
{
global $ddb, $m;
try {
$post = $data['body']? json_decode($data['body'], true): $data;
if (!is_array($post)) {
return response(['fatal' => 'no data']);
}
if ($post['hash']) {
$key = mb_substr($post['hash'], 1, 10);
$state = $ddb->getItem([
'TableName' => 'xrges',
'Key' => ['hash' => ['S' => $key]],
'ConsistentRead' => true
]);
return response($state['Item']? $m->unmarshalItem($state['Item']): []);
}
$preg = new Preg($post);
$result = $preg->exec();
$key = $preg->hash();
$post['hash'] = $key;
$post['ttl_expire'] = time() + (60*60*24*365*2);
$ddb->putItem([
'TableName' => 'xrges',
'Item' => $m->marshalItem($post)
]);
return response(array_merge((array)$result, ['hash' => $key]));
} catch (Throwable $e) {
return response(['fatal' => $e->getMessage()]);
}
}

218
preg.php

@ -0,0 +1,218 @@
<?php
class Preg
{
const FUNCTIONS = ["preg_match", "preg_match_all", "preg_split", "preg_replace", "preg_quote"];
const FLAGS = [
"preg_match" => ['PREG_OFFSET_CAPTURE', 'PREG_UNMATCHED_AS_NULL'],
"preg_match_all" => ['PREG_SET_ORDER', 'PREG_OFFSET_CAPTURE', 'PREG_UNMATCHED_AS_NULL'],
"preg_split" => ['PREG_SPLIT_NO_EMPTY', 'PREG_SPLIT_DELIM_CAPTURE', 'PREG_SPLIT_OFFSET_CAPTURE'],
"preg_replace" => [],
"preg_quote" => []
];
public function __construct($params)
{
if (!in_array($params["method"], self::FUNCTIONS)) {
throw new Error("Invalid function");
}
ksort($params);
$this->hash = base_convert(crc32(serialize($params)), 10, 36);
$this->raw = $params;
$this->method = $params["method"];
$this->pattern = $params["pattern"];
$this->replacement = $params["replacement"];
$this->subject = $params["subject"];
$this->offset = mb_strlen($params["offset"])? (int)$params["offset"]: 0;
$this->limit = mb_strlen($params["limit"])? (int)$params["limit"]: -1;
$this->delimeter = mb_strlen($params["delimeter"])? $params["delimeter"]: null;
$this->flags = 0;
$this->flags_str = [];
foreach (array_intersect(self::FLAGS[$this->method], array_keys((array)$params)) as $flag) {
if ($params[$flag]) {
$this->flags |= constant($flag);
$this->flags_str[] = $flag;
}
}
}
public function exec()
{
if (!mb_strlen($this->pattern)) {
return [];
}
$func = "exec_". $this->method;
return $this->$func();
}
protected function exec_preg_match()
{
$ret = @preg_match($this->pattern, $this->subject, $matches, $this->flags, $this->offset);
$error = $this->last_error();
return array_merge($this->result($ret), [
"dump" => $error?: $matches,
"error" => (bool)$error,
"code" => $this->syntax()
]);
}
protected function exec_preg_match_all()
{
$ret = @preg_match_all($this->pattern, $this->subject, $matches, $this->flags, $this->offset);
$error = $this->last_error();
return array_merge($this->result($ret), [
"dump" => $error?: $matches,
"error" => (bool)$error,
"code" => $this->syntax()
]);
}
protected function exec_preg_split()
{
$ret = @preg_split($this->pattern, $this->subject, $this->limit, $this->flags);
$error = $this->last_error();
return array_merge($this->result($ret), [
"dump" => $error?: $ret,
"error" => (bool)$error,
"code" => $this->syntax()
]);
}
protected function exec_preg_replace()
{
$ret = @preg_replace($this->pattern, $this->replacement, $this->subject, $this->limit);
$error = $this->last_error();
return array_merge($this->result($ret), [
"dump" => $error?: $ret,
"error" => (bool)$error,
"code" => $this->syntax()
]);
}
protected function exec_preg_quote()
{
$ret = @preg_quote($this->pattern, $this->delimeter);
$error = $this->last_error();
return array_merge($this->result($ret), [
"dump" => $error?: $ret,
"error" => (bool)$error,
"code" => $this->syntax()
]);
}
protected function last_error()
{
/*if (!preg_last_error()) {
return false;
}*/
$ple = preg_last_error();
$e = error_get_last();
$gdc = get_defined_constants(true)['pcre'];
$return = [];
if ($ple and in_array($ple, $gdc)) {
$return["preg_last_error()"] = array_flip($gdc)[preg_last_error()];
}
if ($e['type'] < E_NOTICE) {
$return["php error message"] = $e['message'];
}
return $return;
}
protected function result($var)
{
$gettype = gettype($var);
$return = [
"return_value" => "??",
"return_type" => $gettype
];
if (in_array($gettype, ["boolean", "integer", "NULL"])) {
$return["return_value"] = json_encode($var);
} elseif (in_array($gettype, ["string"])) {
$return["return_value"] = "len(". mb_strlen($var). ")";
} elseif (in_array($gettype, ["array"])) {
$return["return_value"] = "count(". count($var). ")";
}
return $return;
}
protected function syntax()
{
$args = [$this->method .'("'. $this->pattern.'"'];
if (in_array($this->method, ['preg_match', 'preg_match_all'])) {
$args[] = '"'. $this->crop_text($this->subject) .'", $matches';
if (count($this->flags_str)) {
$args[] = implode(" | ", $this->flags_str);
}
if (mb_strlen($this->raw['offset'])) {
$args[] = (!count($this->flags_str)? 'null, ': ''). (string)$this->offset;
}
} elseif (in_array($this->method, ['preg_split'])) {
$args[] = '"'. $this->crop_text($this->subject) .'"';
if (mb_strlen($this->raw['limit'])) {
$args[] = (string)$this->limit;
}
if (count($this->flags_str)) {
$args[] = (!mb_strlen($this->raw['limit'])? 'null, ': ''). implode(" | ", $this->flags_str);
}
} elseif (in_array($this->method, ['preg_replace'])) {
$args[] = '"'. $this->crop_text($this->replacement) .'"';
$args[] = '"'. $this->crop_text($this->subject) .'"';
if (mb_strlen($this->raw['limit'])) {
$args[] = (string)$this->limit;
}
} elseif (in_array($this->method, ['preg_quote'])) {
if (mb_strlen($this->delimeter)) {
$args[] = (string)$this->delimeter;
}
}
$code = implode(", ", $args). ");";
return str_replace(array('?&gt;', '&lt;?php&nbsp;', '&nbsp;', '<code>', '</code>'), array('', '', ' ', '', ''), highlight_string('<?php '. $code .' ?'.'>', true) );
}
protected function crop_text($str, $len=50)
{
if (mb_strlen($str, "utf-8") < $len) {
return $str;
}
return str_replace(array("\r", "\n"), array('\r', '\n'), addslashes(mb_substr($str, 0, $len, "utf-8")). "...");
}
public function hash() {
return $this->hash;
}
}

5
redis.php

@ -0,0 +1,5 @@
<?php
$redis = new Redis();
$redis->connect("localhost");
$redis->select(3);
Loading…
Cancel
Save