/**
 * set vars for Search Engine Drop Down List
 *
 * @param $selectedEngine
 * @param $autoSubmit
 */
function tmplSetSearchEngineDDL($selectedEngine = 'TorrentSpy', $autoSubmit = false)
{
    global $cfg, $tmpl;
    // set some vars
    $tmpl->setvar('autoSubmit', $autoSubmit);
    $handle = opendir("./inc/searchEngines");
    while ($entry = readdir($handle)) {
        $entrys[] = $entry;
    }
    natcasesort($entrys);
    $Engine_List = array();
    foreach ($entrys as $entry) {
        if ($entry != "." && $entry != ".." && substr($entry, 0, 1) != "." && strpos($entry, "Engine.php")) {
            $tmpEngine = str_replace("Engine", '', substr($entry, 0, strpos($entry, ".")));
            if (array_key_exists($tmpEngine, $cfg['searchEngineLinks'])) {
                $hreflink = $cfg['searchEngineLinks'][$tmpEngine];
                $settings['searchEngineLinks'][$tmpEngine] = $hreflink;
            } else {
                $hreflink = getEngineLink($tmpEngine);
                $settings['searchEngineLinks'][$tmpEngine] = $hreflink;
                $settingsNeedsSaving = true;
            }
            array_push($Engine_List, array('selected' => $selectedEngine == $tmpEngine ? 1 : 0, 'Engine' => $tmpEngine, 'hreflink' => $hreflink));
        }
    }
    return $Engine_List;
}
function buildSearchEngineLinks($selectedEngine = 'TorrentSpy')
{
    global $cfg;
    $settingsNeedsSaving = false;
    $settings['searchEngineLinks'] = array();
    $output = '';
    if (!array_key_exists('searchEngineLinks', $cfg) || !is_array($cfg['searchEngineLinks'])) {
        saveSettings($settings);
    }
    $handle = opendir("./searchEngines");
    while ($entry = readdir($handle)) {
        $entrys[] = $entry;
    }
    natcasesort($entrys);
    foreach ($entrys as $entry) {
        if ($entry != "." && $entry != ".." && substr($entry, 0, 1) != ".") {
            if (strpos($entry, "Engine.php")) {
                $tmpEngine = str_replace("Engine", '', substr($entry, 0, strpos($entry, ".")));
                if (array_key_exists($tmpEngine, $cfg['searchEngineLinks'])) {
                    $hreflink = $cfg['searchEngineLinks'][$tmpEngine];
                    $settings['searchEngineLinks'][$tmpEngine] = $hreflink;
                } else {
                    $hreflink = getEngineLink($tmpEngine);
                    $settings['searchEngineLinks'][$tmpEngine] = $hreflink;
                    $settingsNeedsSaving = true;
                }
                if (strlen($hreflink) > 0) {
                    $output .= "<a href=\"http://" . $hreflink . "/\" target=\"_blank\">";
                    if ($selectedEngine == $tmpEngine) {
                        $output .= "<b>" . $hreflink . "</b>";
                    } else {
                        $output .= $hreflink;
                    }
                    $output .= "</a><br>\n";
                }
            }
        }
    }
    if (count($settings['searchEngineLinks'], COUNT_RECURSIVE) != count($cfg['searchEngineLinks'], COUNT_RECURSIVE)) {
        $settingsNeedsSaving = true;
    }
    if ($settingsNeedsSaving) {
        natcasesort($settings['searchEngineLinks']);
        saveSettings($settings);
    }
    return $output;
}