From 8e7efeeafa06e7dfa1aaca7c652f663e1fa456ce Mon Sep 17 00:00:00 2001 From: Banana Date: Mon, 19 Dec 2016 12:04:27 +0100 Subject: [PATCH] searching and adding a new URL --- webroot/index.php | 10 +- webroot/lib/summoner.class.php | 225 +++++++++++++++++++++++++++++++++ webroot/view/_foot.html | 16 --- webroot/view/_foot.php | 44 +++++++ webroot/view/_head.html | 12 -- webroot/view/_head.php | 40 ++++++ webroot/view/home.html | 20 --- webroot/view/home.inc.php | 62 +++++++++ webroot/view/home.php | 153 +++++++++++++++++++++- 9 files changed, 529 insertions(+), 53 deletions(-) create mode 100644 webroot/lib/summoner.class.php delete mode 100644 webroot/view/_foot.html create mode 100644 webroot/view/_foot.php delete mode 100644 webroot/view/_head.html create mode 100644 webroot/view/_head.php delete mode 100644 webroot/view/home.html create mode 100644 webroot/view/home.inc.php diff --git a/webroot/index.php b/webroot/index.php index e487ef3..c8bd963 100644 --- a/webroot/index.php +++ b/webroot/index.php @@ -54,16 +54,18 @@ else { } require('config.php'); +require('lib/summoner.class.php'); ## main vars +$Summoner = new Summoner(); # database object $DB = false; # the template data as an array $TemplateData = array(); # the default view -$View = 'home.html'; +$View = 'home.php'; # the default script -$ViewScript = 'home.php'; +$ViewScript = 'home.inc.php'; ## DB connection mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); # throw exeptions @@ -95,9 +97,9 @@ if(!empty($TemplateData['refresh'])) { # header information header('Content-type: text/html; charset=UTF-8'); -require 'view/_head.html'; +require 'view/_head.php'; require 'view/'.$View; -require 'view/_foot.html'; +require 'view/_foot.php'; $DB->close(); # END \ No newline at end of file diff --git a/webroot/lib/summoner.class.php b/webroot/lib/summoner.class.php new file mode 100644 index 0000000..ce66c6d --- /dev/null +++ b/webroot/lib/summoner.class.php @@ -0,0 +1,225 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0. + * + */ + +/** + * a static helper class + */ + +class Summoner { + + /** + * validate the given string with the given type. Optional check the string + * length + * + * @param string $input The string to check + * @param string $mode How the string should be checked + * @param mixed $limit If int given the string is checked for length + * + * @see http://de.php.net/manual/en/regexp.reference.unicode.php + * http://www.sql-und-xml.de/unicode-database/#pc + * + * the pattern replaces all that is allowed. the correct result after + * the replace should be empty, otherwise are there chars which are not + * allowed + * + */ + static function validate($input,$mode='text',$limit=false) { + // check if we have input + $input = trim($input); + + if($input == "") return false; + + $ret = false; + + switch ($mode) { + case 'mail': + if(filter_var($input,FILTER_VALIDATE_EMAIL) === $input) { + return true; + } + else { + return false; + } + break; + + case 'rights': + return self::isRightsString($input); + break; + + case 'url': + if(filter_var($input,FILTER_VALIDATE_URL) === $input) { + return true; + } + else { + return false; + } + break; + + case 'nospace': + // text without any whitespace and special chars + $pattern = '/[\p{L}\p{N}]/u'; + break; + + case 'nospaceP': + // text without any whitespace and special chars + // but with Punctuation other + # http://www.sql-und-xml.de/unicode-database/po.html + $pattern = '/[\p{L}\p{N}\p{Po}\-]/u'; + break; + + case 'digit': + // only numbers and digit + // warning with negative numbers... + $pattern = '/[\p{N}\-]/'; + break; + + case 'pageTitle': + // text with whitespace and without special chars + // but with Punctuation + $pattern = '/[\p{L}\p{N}\p{Po}\p{Z}\s-]/u'; + break; + + # strange. the \p{M} is needed.. don't know why.. + case 'filename': + $pattern = '/[\p{L}\p{N}\p{M}\-_\.\p{Zs}]/u'; + break; + + case 'text': + default: + $pattern = '/[\p{L}\p{N}\p{P}\p{S}\p{Z}\p{M}\s]/u'; + } + + $value = preg_replace($pattern, '', $input); + + #if($input === $value) { + if($value === "") { + $ret = true; + } + + if(!empty($limit)) { + # isset starts with 0 + if(isset($input[$limit])) { + # too long + $ret = false; + } + } + + return $ret; + } + + /** + * return if the given string is utf8 + * http://php.net/manual/en/function.mb-detect-encoding.php + * + * @param string $string + * @return number + */ + static function is_utf8 ( $string ) { + // From http://w3.org/International/questions/qa-forms-utf-8.html + return preg_match('%^(?: + [\x09\x0A\x0D\x20-\x7E] # ASCII + | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte + | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs + | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte + | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates + | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 + | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 + | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 + )*$%xs', $string); + } + + /** + * execute a curl call to the fiven $url + * @param string $curl The request url + */ + static function curlCall($url,$port=80) { + $ret = false; + + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); + curl_setopt($ch, CURLOPT_PORT, $port); + + $do = curl_exec($ch); + if(is_string($do) === true) { + $ret = $do; + } + else { + $ret = false; + } + + curl_close($ch); + + return $ret; + } + + /** + * check if a string strts with a given string + * + * @param string $haystack + * @param string $needle + * @return boolean + */ + static function startsWith($haystack, $needle) { + $length = strlen($needle); + return (substr($haystack, 0, $length) === $needle); + } + + /** + * check if a string ends with a given string + * + * @param string $haystack + * @param string $needle + * @return boolean + */ + static function endsWith($haystack, $needle) { + $length = strlen($needle); + if ($length == 0) { + return true; + } + + return (substr($haystack, -$length) === $needle); + } + + + /** + * simulate the Null coalescing operator in php5 + * + * this only works with arrays and checking if the key is there and echo/return it. + * + * http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op + */ + + static function ifset($array,$key) { + return isset($array[$key]) ? $array[$key] : false; + } +} + +?> diff --git a/webroot/view/_foot.html b/webroot/view/_foot.html deleted file mode 100644 index 416c434..0000000 --- a/webroot/view/_foot.html +++ /dev/null @@ -1,16 +0,0 @@ -
-
-
-
- © Insipid -
-
-
-
- - - - - - - diff --git a/webroot/view/_foot.php b/webroot/view/_foot.php new file mode 100644 index 0000000..d429468 --- /dev/null +++ b/webroot/view/_foot.php @@ -0,0 +1,44 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0. + * + */ +?> +
+
+
+
+ © Insipid +
+
+
+
+ + + + + + + diff --git a/webroot/view/_head.html b/webroot/view/_head.html deleted file mode 100644 index 1421536..0000000 --- a/webroot/view/_head.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - Foundation for Sites - - - - - \ No newline at end of file diff --git a/webroot/view/_head.php b/webroot/view/_head.php new file mode 100644 index 0000000..3b67ef6 --- /dev/null +++ b/webroot/view/_head.php @@ -0,0 +1,40 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0. + * + */ +?> + + + + + + + Insipid + + + + + \ No newline at end of file diff --git a/webroot/view/home.html b/webroot/view/home.html deleted file mode 100644 index 4deb75e..0000000 --- a/webroot/view/home.html +++ /dev/null @@ -1,20 +0,0 @@ -
-
-

Welcome to your Inspid installation

-
-
- -
-
-
-
- - -
- -
-
-
-
-
- \ No newline at end of file diff --git a/webroot/view/home.inc.php b/webroot/view/home.inc.php new file mode 100644 index 0000000..3f63929 --- /dev/null +++ b/webroot/view/home.inc.php @@ -0,0 +1,62 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0. + * + */ + +$searchValue = false; +$isUrl = false; +$submitFeedback = false; +$queryStr = false; +$searchResult = false; +$showAddForm = false; +$formData = false; + +if(isset($_POST['data']) && !empty($_POST['data']) && isset($_POST['submitsearch'])) { + $searchValue = trim($_POST['data']['searchfield']); + $isUrl = Summoner::validate($searchValue,'url'); + if($isUrl === true) { + # search for URL + $queryStr = "SELECT * FROM"; + } + elseif(Summoner::validate($searchValue,'text')) { + # search for this in more then one field + + } + else { + $submitFeedback['message'] = 'Invalid input'; + $submitFeedback['status'] = 'error'; + } + + if(!empty($queryStr)) { + } + + # new one? + if(empty($searchResult) && $isUrl === true) { + # show the add form + $showAddForm = true; + $formData['url'] = $searchValue; + } +} \ No newline at end of file diff --git a/webroot/view/home.php b/webroot/view/home.php index 24374fc..be4fbd1 100644 --- a/webroot/view/home.php +++ b/webroot/view/home.php @@ -24,4 +24,155 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0. * - */ \ No newline at end of file + */ + ?> +
+
+

Welcome to your Inspid installation

+
+
+ +
+
+
+
+ + +
+ +
+
+
+
+
+ + +
+
+ +
+
Error
+

+
+ +
+
Success
+

+
+ +
+
+ + + +
+
+
+

This URL was not found. Want to add it?

+
+
+
+
+ +
+
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+ + +
+
+
+
+

Last added

+
+ +
+

It has an easy to override visual style, and is appropriately subdued.

+ I'm a button +
+
+
+
+
+ +
+

This is a card.

+

It has an easy to override visual style, and is appropriately subdued.

+
+
+
+
+
+ +
+

This is a card.

+

It has an easy to override visual style, and is appropriately subdued.

+
+
+
+
+
+ +
+

This is a card.

+

It has an easy to override visual style, and is appropriately subdued.

+
+
+
+
+
+ +
+

This is a card.

+

It has an easy to override visual style, and is appropriately subdued.

+
+
+
+
+
+ +
+

This is a card.

+

It has an easy to override visual style, and is appropriately subdued.

+
+
+
+
-- 2.39.5