Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

webbutv3/word-app/util/

db_connect.php
dict.xml
practice.php
user.php
util.php

util.php

141 lines UTF-8 Windows (CRLF)
<?php
function encase($content$tag$class ""$style "")
{
    return 
"<" $tag .
        
" class='" $class "'" .
        
" style='" $style "'" ">"
        
$content .
        
"</" $tag ">";
}

function 
normalize_spanish_string($str)
{
    
$str strtolower($str);
    for (
$i 0$i strlen($str); $i++) {
        switch (
$str[$i]) {
            case 
"á":
                
$str[$i] = "a";
                break;
            case 
"é":
                
$str[$i] = "e";
                break;
            case 
"í":
                
$str[$i] = "i";
                break;
            case 
"ñ":
                
$str[$i] = "n";
                break;
            case 
"ó":
                
$str[$i] = "o";
                break;
            case 
"ü":
                
$str[$i] = "u";
                break;
            case 
"ú":
                
$str[$i] = "u";
                break;
        }
    }

    return 
$str;
}

function 
path_to_url($path)
{
    
$path str_replace("//""/"str_replace($_SERVER['DOCUMENT_ROOT'], '/'str_replace("\\""/"$path)));
    return 
$path;
}

function 
produce_XML_object_tree($raw_XML)
{
    
libxml_use_internal_errors(true);
    try {
        
$xmlTree = new SimpleXMLElement($raw_XML);
    } catch (
Exception $e) {
        
// Something went wrong.
        
$error_message 'SimpleXMLElement threw an exception.';
        foreach (
libxml_get_errors() as $error_line) {
            
$error_message .= "\t" $error_line->message;
        }
        
trigger_error($error_message);
        return 
false;
    }
    return 
$xmlTree;
}

function 
sort_cmp($a$b)
{
    
setlocale(LC_COLLATE'es_ES.UTF-8');
    return 
strcoll($a["es"], $b["es"]);
}


function 
get_predefined_translation(string $word)
{
    static 
$EN_ES_translations = [];

    if (
$EN_ES_translations == []) {
        
$xml_feed_url path_to_url("http://localhost/" BASE_PATH "/util/dict.xml");
        
$ch curl_init();
        
curl_setopt($chCURLOPT_URL$xml_feed_url);
        
curl_setopt($chCURLOPT_HEADERfalse);
        
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
        
$xml curl_exec($ch);
        
curl_close($ch);
        
$cont produce_XML_object_tree($xml);
        foreach (
$cont->as $letter) {
            foreach (
$letter->as $wordXml) {
                
$EN_ES_translations[] = [
                    
"es" => strtolower($wordXml->c),
                    
"en" => strtolower($wordXml->d),
                ];
            }
        }


        
usort($EN_ES_translations"sort_cmp");
    }

    
// binary search
    
$pointer ceil(count($EN_ES_translations) / 2);
    
$i 2;
    
$found true;
    while (
$EN_ES_translations[$pointer]) {
        if (
$i 100) {
            
$found false;
            break;
        }
        
//echo $pointer."<br>";
        
$cmp strcasecmp($EN_ES_translations[$pointer]["es"], $word);
        if (
$cmp == 0)
            break;
        if (
$cmp 0)
            
$pointer $pointer ceil(count($EN_ES_translations) / pow(2$i));
        else
            
$pointer $pointer ceil(count($EN_ES_translations) / pow(2$i));
        
$i++;
    }

    
// not found in array
    
if (!$found)
        return 
null;

    
// look for multiple definitions
    
$pointers = [$pointer];

    
$pointerR $pointer 1;
    
$pointerL $pointer 1;

    while (
$pointerR count($EN_ES_translations) && $EN_ES_translations[$pointerR]["es"] == $EN_ES_translations[$pointer]["es"])
        
$pointers[] = $pointerR++;
    while (
$pointerL >= && $EN_ES_translations[$pointerL]["es"] == $EN_ES_translations[$pointer]["es"])
        
$pointers[] = $pointerL--;

    
$translations = [];

    foreach (
$pointers as $pointer) {
        
$translations[] = $EN_ES_translations[$pointer]["en"];
    }

    return 
$translations;
}