<?php

/* functions.vcab.php
*
* vcab v0.6
* Copyleft (c) 2022 Bailey Moore <[email protected]>
* gopher://sdf.org/1/users/bunz/code/vcab.zip
*
* Free for all. There's not much to steal.
*
*/

global $file, $website;
$tags = array('<i>', '</i>', '<b>', '</b> ', '<div class="ds-single">', '<div class="ds-list">', '</div>', '<span class="illustration">', '</span>');
$n1 = array('1.', '2.', '3.', '4.', '5.', '6.', '7.', '8.', '9.');
$n2 = array(' (1)', '(2)', '(3)', '(4)', '(5)', '(6)', '(7)', '(8)', '(9)');

function add_entry($word, $full) {
       global $main, $file;
       $open = fopen($file, "a+");
       $node = $full == TRUE ? get_entry($word) : 0;
       $true = 0;

       if ($node != 0) {
               foreach ($node as $n) {
                       if (fwrite($open, $n . "\n")) {
                               $true++;
                       }
               }
       }
       else {
               if (!strpos($word, "-")) {
                       $word = $word . " -";
               }

               if (fwrite($open, $word . "\n")) {
                       $true++;
               }
       }
       fclose($open);
       exec('sort ' . $file . ' > ' . $main . 'new.vocabulary.list');
       exec('rename -f ' . $main . 'new.vocabulary.list' . ' ' . $file);

       if ($true == 0) {
               return FALSE;
       }
       return TRUE;
}

function get_entry($word) {
       global $website, $tags, $n1, $n2;
       $definition = array();
       $lines = file($website . $word);
       $html = "";
       $z = 0;

       if ($website == "http://www.thefreedictionary.com/") {
               foreach ($lines as $line) {
                       if (search_string("pseg", $line)) {
                               $html = $line;
                               break;
                       }
               }

               if (!empty($html)) {
                       $html = get_string_between($html, '</span><div class="pseg">', '<hr align="left" class="hmshort">');
                       $pseg = substr_count($html, "pseg") + 1;
                       $node = $html;

                       if ($pseg > 1) {
                               while ($pseg != 0) {
                                       if (search_string("pseg", $html)) {
                                               $explode = explode('<div class="pseg">', $html, 2);
                                               $node = $explode[0];
                                       }

                                       if (search_string('<div class="ds-single">', $node)) {
                                               $definition[$z] = $word . " - " . trim(get_string_between($node, '<i>', '</i>')) . " " .
trim(get_string_between($node, '<div class="ds-single">', '</div></div>'));
                                               $definition[$z] = string_ends_with($definition[$z], ".") ? strtolower(rtrim($definition[$z], ".")) :
strtolower($definition[$z]);
                                       }
                                       elseif (search_string('<div class="ds-list">', $node)) {
                                               $format = str_replace($tags, "", $node);
                                               $format = str_replace($n1, $n2, $format);
                                               $format = strpos($format, ".(") ? str_replace(".(", " (", $format) : $format;
                                               $definition[$z] = string_ends_with($format, ".") ? $word . " - " . strtolower(rtrim($format, ".")) : $word . " - "
strtolower($format);
                                       }
                                       $node = $explode[1];

                                       if (strpos($definition[$z], ":")) {
                                               $format = explode(":", $definition[$z], 2);
                                               $definition[$z] = trim($format[0]);
                                       }
                                       $html = $node;
                                       $pseg--;
                                       $z++;
                               }
                       }
                       else {
                               if (search_string('<div class="ds-single">', $html)) {
                                       $definition[$z] = $word . " - " . trim(get_string_between($html, '<i>', '</i>')) . " " . trim(get_string_between($html,
'<div class="ds-single">', '</div></div>'));
                                       $definition[$z] = string_ends_with($definition[$z], ".") ? strtolower(rtrim($definition[$z], ".")) :
strtolower($definition[$z]);
                               }
                               elseif (search_string('<div class="ds-list">', $html)) {
                                       $format = str_replace($tags, "", $html);
                                       $format = str_replace($n1, $n2, $format);
                                       $format = strpos($format, ".(") ? str_replace(".(", " (", $format) : $format;
                                       $definition[$z] = string_ends_with($format, ".") ? $word . " - " . strtolower(rtrim($format, ".")) : $word . " - " .
strtolower($format);
                               }

                               if (strpos($definition[$z], ":")) {
                                       $format = explode(":", $definition[$z], 2);
                                       $definition[$z] = trim($format[0]);
                               }
                       }
               }
               else {
                       echo "\n[ ERROR ] Unable to retrieve word.\n";
                       return FALSE;
               }
       }
       return $definition;
}

function itemize($string, $full = TRUE) {
       if ($full == TRUE) {
               if (strpos($string, ".")) {
                       $exp1 = explode("-", $string, 2);
                       $exp2 = explode(".", $exp1[1], 2);
                       $word = trim($exp1[0]);
                       $type = trim($exp2[0]);
                       $definition = trim($exp2[1]);

                       return array($word, $type, $definition);
               }
       }
       else {
               if (strpos($string, "-")) {
                       $exp1 = explode("-", $string, 2);
                       $word = trim($exp1[0]);
               }
               else {
                       $word = trim($string);
               }
               return $word;
       }
       return FALSE;
}

function search_file($needle, $haystack, $return = FALSE) {
       $contents = file_get_contents($haystack);
       $pattern = preg_quote($needle, '/');
       $pattern = "/^.*$pattern.*\$/m";

       if (preg_match_all($pattern, $contents, $matches)) {
               if ($return == TRUE) {
                       return $matches;
               }
               return TRUE;
       }
       return FALSE;
}

function search_string($needle, $haystack) {
       if (preg_match_all("/$needle/im", $haystack) || strpos($haystack, $needle)) {
               return TRUE;
       }
       return FALSE;
}

function get_string_between($string, $start, $end) {
       $string = ' ' . $string;
       $ini = strpos($string, $start);

       if ($ini == 0) {
               return '';
       }
       $ini += strlen($start);
       $len = strpos($string, $end, $ini) - $ini;

       return substr($string, $ini, $len);
}

function string_ends_with($haystack, $needle, $case = TRUE) {
   $expected_position = strlen($haystack) - strlen($needle);

       if ($case) {
               return strrpos($haystack, $needle, 0) === $expected_position;
       }
       return strripos($haystack, $needle, 0) === $expected_position;
}

function vcab_configure() {
       global $main, $file;

       if (file_exists($main . ".vcabrc")) {
               $config = fopen($main . ".vcabrc", "w");
       }
       echo "\n\n\t[ vcab Configuration Module ]\n";
       echo "\n\t\t \n";
       echo "\n\t\t1) Vocabulary file: $file\n";
       echo "\n\t\t2) Load config file automatically: no\n";
       echo "\n\t\t3) \n";
       echo "\n\t\t4) \n";
       echo "\n\t\t5) \n";
       echo "\n\t\t6) \n";
       echo "\n\t\t7) \n";
       echo "\n\t\t8) \n";
       echo "\n\t\t9) \n";
}

?>