diff --git a/.gitignore b/.gitignore index 24d0dae..8a043bb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ htaccess -php/keyVariable.php \ No newline at end of file +php/keyVariable.php +php/config_database \ No newline at end of file diff --git a/README.md b/README.md index 5a8ba68..a3d3ec4 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,9 @@ Puis créer un nouveau fichier dans ```/php``` du nom de ```keyVariable.php```, $apiWeather = 'votre clé api pour openweathermap.org'; ``` +Sachez tout de même que vous ne pourrez pas utiliser la fonction linkShortener car elle dépend de [short-links.divlo.fr/](https://short-links.divlo.fr/). +Cependant, vous avez accès à la structure de la base de donnée dans ```/php/short_links.sql```. + Enjoy! =D ## Librairies diff --git a/css/style.css b/css/style.css index 5d5f766..80773e5 100644 --- a/css/style.css +++ b/css/style.css @@ -102,6 +102,14 @@ pre code { line-height: 2; } +.original-link-list { + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; + max-width: 200px; + vertical-align: middle; +} + /* FOOTER */ footer { diff --git a/img/function-image/convertMarkdown.png b/img/function-image/convertMarkdown.png index eb37faf..94f29bc 100644 Binary files a/img/function-image/convertMarkdown.png and b/img/function-image/convertMarkdown.png differ diff --git a/img/function-image/linkShortener.png b/img/function-image/linkShortener.png new file mode 100644 index 0000000..3485286 Binary files /dev/null and b/img/function-image/linkShortener.png differ diff --git a/php/config.php b/php/config.php index f552463..a93d108 100644 --- a/php/config.php +++ b/php/config.php @@ -88,6 +88,16 @@ switch ($currentpage) { $description = "Convertis du Markdown en HTML."; $image = 'https://function.divlo.fr/img/function-image/convertMarkdown.png'; break; + case '/views/function-views/linkShortener.php': + $title = "Raccourcisseurs de liens"; + $description = "Une URL trop longue ? Raccourcissez-là !"; + $image = 'https://function.divlo.fr/img/function-image/linkShortener.png'; + break; + case '/views/short_links-list.php': + $title = 'Liste des liens récemment raccourcit'; + $description = "La liste de vos liens raccourcit les plus récents."; + $image = 'https://function.divlo.fr/img/function-image/linkShortener.png'; + break; default: $title = 'Erreur 404'; $description = "Cette page n'existe pas!"; diff --git a/php/short_links.sql b/php/short_links.sql new file mode 100644 index 0000000..79314df --- /dev/null +++ b/php/short_links.sql @@ -0,0 +1,13 @@ +SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +SET AUTOCOMMIT = 0; +START TRANSACTION; +SET time_zone = "+00:00"; + +DROP TABLE IF EXISTS `short_links`; +CREATE TABLE IF NOT EXISTS `short_links` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `url` text NOT NULL, + `shortcut` text NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +COMMIT; \ No newline at end of file diff --git a/php/shortenLink.php b/php/shortenLink.php new file mode 100644 index 0000000..c498245 --- /dev/null +++ b/php/shortenLink.php @@ -0,0 +1,100 @@ + false, $valueName => ""); + $req = $bdd->prepare('SELECT * FROM short_links WHERE ' . $valueName . ' = ?'); + $req->execute(array($valueToFind)); + while ($result = $req->fetch()) { + $array['isInDatabase'] = true; + if($typeURL) { + $array["shortcut"] = $result["shortcut"]; + } + } + return $array; +} + +$array = array("url" => "", "option"=> "", "message" => ""); + +if ($_SERVER["REQUEST_METHOD"] == "POST") { + + // Si le formulaire est envoyé + if(isset($_POST['url']) && isset($_POST['option'])) { + $array["url"] = $_POST["url"]; + $array["option"] = $_POST["option"]; + + // Si ce n'est pas un lien valide + if(!filter_var($array["url"], FILTER_VALIDATE_URL)) { + $array["message"] = "Veuillez entré une URL valide."; + } else { + // Connexion à la base de donnée + require_once('./config_database/connectDB.php'); + + // URL déjà en base de donnée ? + $urlInDatabase = alreadyExists($bdd, $array["url"], "url", true); + if($urlInDatabase['isInDatabase']) { + $shortcutURL = $linkPath . '?q=' . $urlInDatabase['shortcut']; + $array['message'] = 'Adresse déjà raccourcie : ' . $shortcutURL . ''; + } else { + if($array['option'] == "userShortcut" && isset($_POST['userShortcut']) && !empty($_POST['userShortcut'])) { + // Shortcut choisis par l'utilisateur + $shortcut = camelCase($_POST['userShortcut']); + } else { + // Shortcut unique aléatoire + do { + $shortcut = uniqueId(); + } while(alreadyExists($bdd, $shortcut, "shortcut")['isInDatabase']); + } + $shortcutURL = $linkPath . '?q=' . $shortcut; + if(alreadyExists($bdd, $shortcut, "shortcut")['isInDatabase']) { + $array['message'] = 'Le shortcut "' . $shortcut . '" est déjà pris, veuillez en choisir un autre.'; + } else { + // Envoi de l'URL et du shortcut dans la base de donnée + $req = $bdd->prepare('INSERT INTO short_links(url, shortcut) VALUES (?, ?)'); + $req->execute(array($array['url'], $shortcut)); + $array['message'] = 'URL raccourcie : '. $shortcutURL .''; + + // URL et Shortcut en Cookie + if(isset($_COOKIE['shortcuts']) && !empty($_COOKIE['shortcuts'])) { + $data = json_decode($_COOKIE['shortcuts'], true); + } else { + $data = array(); + } + array_push($data, array("url" => $array['url'], "shortcut" => $shortcutURL)); + setcookie('shortcuts', json_encode($data), time()+3600*24*365, '/'); + } + } + } + } + echo json_encode($array); +} +?> \ No newline at end of file diff --git a/scripts/executeFunction.js b/scripts/executeFunction.js index 0a32771..fb3ff15 100644 --- a/scripts/executeFunction.js +++ b/scripts/executeFunction.js @@ -95,16 +95,6 @@ $(function () { $('.resultsRandomQuote').html(getRandomQuote()); } - /* Permet d'afficher la liste des citations/proverbes */ - if(chemin === "/views/quote-list.php") { - window.onload = $('.totalLengthQuote').html('Total de ' + quotes.length + ' citations.'); - let resultat = ""; - for (index in quotes) { - resultat = resultat + ` ${quotes[index]["source"]} ${quotes[index]["quote"]} `; - } - $(".quote-list").append(resultat); - } - $("#submitConvertCurrency").click(() => { let value = $('#value').val(); const currencyOfTheValue = $("#currencyOfTheValue option:selected").val(); @@ -212,4 +202,21 @@ $(function () { localStorage.setItem("texteMarkdown", textMarkdown); $('.results').html(convertedHTML); }); + + $('#formLinkShortener').submit((e) => { + e.preventDefault(); + const postdata = $('#formLinkShortener').serialize(); + $.ajax({ + type: 'POST', + url: '../../php/shortenLink.php', + data: postdata, + success: (text) => { + try { + $(".results").html(JSON.parse(text).message); + } catch (error) { + $(".results").html("URL invalide."); + } + } + }); + }); }); \ No newline at end of file diff --git a/scripts/fonctions_annexes.js b/scripts/fonctions_annexes.js index 4c92124..4ef254b 100644 --- a/scripts/fonctions_annexes.js +++ b/scripts/fonctions_annexes.js @@ -158,4 +158,26 @@ function isValidDate(verifyDate, currentDate) { */ function createSessionCookie(name, value) { document.cookie = escape(name) + "=" + escape(value) + " ; path=/"; -} \ No newline at end of file +} + +/** + * @function getCookieValue + * @description Récupère la valeur d'un cookie. + * @param {string} name Nom du cookie + * @param {string} value Valeur du cookie + */ +function getCookieValue(cname) { + const name = cname + "="; + const decodedCookie = decodeURIComponent(document.cookie); + const ca = decodedCookie.split(';'); + for(let i = 0; i < ca.length; i++) { + let c = ca[i]; + while (c.charAt(0) == ' ') { + c = c.substring(1); + } + if (c.indexOf(name) == 0) { + return c.substring(name.length, c.length); + } + } + return ""; +} \ No newline at end of file diff --git a/scripts/main.js b/scripts/main.js index 72e59b9..bb05c4f 100644 --- a/scripts/main.js +++ b/scripts/main.js @@ -34,6 +34,29 @@ $(function () { /* Permet d'afficher l'heure en temps réel sur le footer */ realDateTime('realDateTime'); + /* Permet d'afficher la liste des citations/proverbes */ + if(chemin === "/views/quote-list.php") { + window.onload = $('.totalLengthQuote').html('Total de ' + quotes.length + ' citations.'); + let resultat = ""; + for (index in quotes) { + resultat = resultat + ` ${quotes[index]["source"]} ${quotes[index]["quote"]} `; + } + $(".quote-list").append(resultat); + } + + /* Permet d'afficher la liste des liens récemment raccourcit */ + if(chemin === "/views/short_links-list.php") { + try { + const shortcuts = JSON.parse(getCookieValue("shortcuts")); + window.onload = $('.totalLengthLinksList').html(`Total de ${shortcuts.length} lien(s) raccourcit récemment.`); + let resultat = ""; + for (element of shortcuts) { + resultat += ` ${element["url"]} ${element["shortcut"]} `; + } + $(".links-list").append(resultat); + } catch(error) {} + } + /* Window Scroll Top Button */ const $btnScrollTop = $('.scroll-top-arrow'); $btnScrollTop.on('click', function () { @@ -41,6 +64,18 @@ $(function () { return false; }); + // Affiche l'input selon le choix de l'utilisateur sur la page linkShortener + if(chemin === '/views/function-views/linkShortener.php') { + $('.hideUserShortcut').hide(); + $("#option").bind("keyup change", () => { + if ($("#option").val() == "userShortcut") { + $('.hideUserShortcut').show(); + } else { + $('.hideUserShortcut').hide(); + } + }); + } + /* Date Picker */ $.fn.datepicker.dates['fr'] = { days: ["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"], diff --git a/views/function-list.php b/views/function-list.php index 5b55302..3ea07d0 100644 --- a/views/function-list.php +++ b/views/function-list.php @@ -103,7 +103,7 @@

Vérifie si un nombre fait partie des nombres d'Armstrong ou non.

-
+

Heap's algorithm

Heap's algorithm @@ -113,13 +113,19 @@
-
+

Markdown

Markdown

Convertis du Markdown en HTML.

+
+
+

Raccourcisseurs de liens

+ Raccourcisseurs de liens +

Une URL trop longue ? Raccourcissez-là !

+
diff --git a/views/function-views/filterStudents.php b/views/function-views/filterStudents.php index 3984d4f..d766550 100644 --- a/views/function-views/filterStudents.php +++ b/views/function-views/filterStudents.php @@ -12,10 +12,10 @@ Carte étudiant
- +
- +
diff --git a/views/function-views/linkShortener.php b/views/function-views/linkShortener.php new file mode 100644 index 0000000..723ff66 --- /dev/null +++ b/views/function-views/linkShortener.php @@ -0,0 +1,40 @@ + + + + + + + +
+

:

+

+ +

+ Raccourcisseurs de liens +
+
+ +

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

+
+ + + \ No newline at end of file diff --git a/views/quote-list.php b/views/quote-list.php index 5c03490..dd15f39 100644 --- a/views/quote-list.php +++ b/views/quote-list.php @@ -14,16 +14,16 @@

- - - - - - - + + + + + + + - -
SourceCitation/Proverbe
SourceCitation/Proverbe
+ + \ No newline at end of file diff --git a/views/short_links-list.php b/views/short_links-list.php new file mode 100644 index 0000000..6c2b993 --- /dev/null +++ b/views/short_links-list.php @@ -0,0 +1,28 @@ + + + + + + + +
+

:

+

+
+ Revenir au raccourcisseur de liens. +

+ + + + + + + + + + + +
Liens originauxLiens raccourcit
+ + + \ No newline at end of file