Update search.tsx

This commit is contained in:
qwertyforce 2020-12-26 12:56:08 +03:00
parent 911d02f109
commit d6d130dbba

View File

@ -47,9 +47,7 @@ export default function Search(props: any) {
) )
} }
function parse(tokens: string[]) {
function parse(query:string) {
const operators: any = [] const operators: any = []
const operands: any = [] const operands: any = []
const priority: any = { const priority: any = {
@ -58,8 +56,8 @@ function parse(query:string) {
"||": 1 "||": 1
} }
function operate(operator: string, a: any, b: any = 0) { function operate(operator: string, a: any, b: any = 0) {
if(a===undefined || b===undefined){ if (a === undefined || b === undefined) {
return {error:true} return { error: true }
} }
let x = {} let x = {}
switch (operator) { switch (operator) {
@ -122,71 +120,9 @@ function parse(query:string) {
} }
} }
} }
function detect_brackets(str:string) {
const tokens = ["&&", "||", "!", ",","-"]
const tokens2 = ["&&", "||", "!", "(", ")", ",","-"]
const stack=[]
const closing_brackets=[]
const opening_brackets=[]
for(let i=0;i<str.length;i++){
if(tokens2.includes(str[i])){
stack.push({token:str[i],idx:i})
}
if(str[i+1] && tokens2.includes(str[i]+str[i+1])){
stack.push({token:str[i]+str[i+1],idx:i})
}
if(str[i]===")"){
let x:any=stack.pop()
let m=false
while(x.token!=="("){
x=stack.pop()
if(tokens.includes(x.token)){
m=true
}
}
if(!m && (x.idx>0 && (tokens.includes(str[x.idx-1])) || (str[x.idx-2] && tokens.includes(str[x.idx-2]+str[x.idx-1]))) ||
(x.idx===0)){
closing_brackets.push(i)
opening_brackets.push(x.idx)
}else if(m){
closing_brackets.push(i)
opening_brackets.push(x.idx)
}
}
}
const arr=str.split("")
for(let i=0;i<closing_brackets.length;i++){
arr[opening_brackets[i]]="(%"
arr[closing_brackets[i]]="%)"
}
str=arr.join("")
return str
}
for (const token of tokens) {
function split(str:any) { switch (token) {
const tokens = ["&&", "||", "!", "(%", "%)", ",","-"]
const temp_char = "#$#"
str=detect_brackets(str)
console.log(str)
for (const token of tokens) {
str = str.replaceAll(token, temp_char + token + temp_char)
}
const arr = str.split(temp_char).map((el:any)=>el.trim()).filter((el:any) => el !== "")
for(let i=0;i<arr.length;i++){
if(arr[i]==="(%"){
arr[i]="("
}
if(arr[i]==="%)"){
arr[i]=")"
}
}
return arr;
}
const query_splitted = split(query)
for (const word of query_splitted) {
switch (word) {
case "||": case "||":
x("||") x("||")
break; break;
@ -205,32 +141,145 @@ function parse(query:string) {
execute_until_opening_bracket() execute_until_opening_bracket()
break; break;
default: default:
operands.push({ tags: word }) operands.push({ tags: token })
break; break;
} }
} }
execute_remaining() execute_remaining()
if(operands.length!==1 || operators.length!==0){ if (operands.length !== 1 || operators.length !== 0) {
return {error:true} return { error: true }
}else{ } else {
return operands[0] return operands[0]
} }
} }
function tokenize(query: string) {
function detect_brackets(str: string) {
const tokens = ["&&", "||", "#!", ",", "#-"];
const tokens2 = ["&&", "||", "#!", "(", ")", ",", "#-"];
const stack = [];
const closing_brackets = [];
const opening_brackets = [];
for (let i = 0; i < str.length; i++) {
if (tokens2.includes(str[i])) {
stack.push({ token: str[i], idx: i });
}
if (str[i + 1] && tokens2.includes(str[i] + str[i + 1])) {
stack.push({ token: str[i] + str[i + 1], idx: i });
}
if (stack.length > 0 && str[i] === ")") {
let x = stack.pop();
let m = false;
if (!x) {
return "##errror##"
}
while (x.token !== "(") {
x = stack.pop();
if (!x) {
return "##errror##"
}
if (tokens.includes(x.token)) {
m = true;
}
}
if (!m && (x.idx > 0 && (tokens.includes(str[x.idx - 1])) || (str[x.idx - 2] && tokens.includes(str[x.idx - 2] + str[x.idx - 1]))) ||
(x.idx === 0)) {
closing_brackets.push(i);
opening_brackets.push(x.idx);
}
else if (m) {
closing_brackets.push(i);
opening_brackets.push(x.idx);
}
}
}
const arr = str.split("");
for (let i = 0; i < closing_brackets.length; i++) {
arr[opening_brackets[i]] = "(%";
arr[closing_brackets[i]] = "%)";
}
str = arr.join("");
return str;
}
function not_operator_fix(str: string) {
const str_arr = str.split("");
const tokens = ["&&", "||", "!", ",", "-", "(", ")"];
const not_operators = ["!", "-"];
const stack = [];
for (let i = 0; i < str.length; i++) {
if (tokens.includes(str[i])) {
stack.push({ token: str[i], idx: i });
}
if (str[i + 1] && tokens.includes(str[i] + str[i + 1])) {
stack.push({ token: str[i] + str[i + 1], idx: i });
}
}
if (stack[0] && stack[0].idx === 0 && not_operators.includes(stack[0].token)) {
str_arr[0] = "#" + stack[0].token;
}
for (let i = 1; i < stack.length; i++) {
if (not_operators.includes(stack[i].token)) {
if (stack[i - 1].token !== ")" && stack[i - 1].token.length + stack[i - 1].idx === stack[i].idx) {
str_arr[stack[i].idx] = "#" + stack[i].token;
}
}
}
return str_arr.join("");
}
function split(str: string) {
const operators = ["&&", "||", "#!", "(%", "%)", ",", "#-"];
const temp_char = "#$#";
str = not_operator_fix(str);
str = detect_brackets(str);
if (str === "##errror##") {
return []
}
for (const operator of operators) {
str = str.replaceAll(operator, temp_char + operator + temp_char);
}
const arr = str.split(temp_char).map((el) => el.trim()).filter((el) => el !== "");
for (let i = 0; i < arr.length; i++) {
if (arr[i] === "(%") {
arr[i] = "(";
}
if (arr[i] === "#-") {
arr[i] = "-";
}
if (arr[i] === "#!") {
arr[i] = "!";
}
if (arr[i] === "%)") {
arr[i] = ")";
}
}
console.log(arr);
return arr;
}
return split(query)
}
function build_ast(str: string) {
const tokens = tokenize(str)
if (tokens.length === 0) {
return { error: true }
}
const ast = parse(tokens)
return ast
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function getServerSideProps(context: any) { export async function getServerSideProps(context: any) {
if (context.query.q) { if (context.query.q) {
if(context.query.q.includes("$")){ if (context.query.q.includes("$")) { //anti - nosql injection or something
return { return {
props: { err: true } props: { err: true }
} }
} }
console.log(context.query.q) console.log(context.query.q)
const query=parse(context.query.q) const query = build_ast(context.query.q)
if(query.error){ if (query.error) {
return { return {
props: { err: true } props: { err: true }
} }
} }
const images = await db_ops.image_ops.find_images_by_tags(query) const images = await db_ops.image_ops.find_images_by_tags(query)
@ -242,7 +291,7 @@ export async function getServerSideProps(context: any) {
} else { } else {
page = 1 page = 1
} }
page = Math.max(page,1) page = Math.max(page, 1)
if (page <= Math.ceil(images.length / images_on_page)) { if (page <= Math.ceil(images.length / images_on_page)) {
for (let i = (page - 1) * images_on_page; (i < (page) * images_on_page) && (i < images.length); i++) { for (let i = (page - 1) * images_on_page; (i < (page) * images_on_page) && (i < images.length); i++) {
photos.push({ photos.push({
@ -263,6 +312,6 @@ export async function getServerSideProps(context: any) {
} }
} }
return { return {
props: { err: true } props: { err: true }
} }
} }