Location: PHPKode > projects > VideoDB > videodb/core/setup.core.php
<?php
/**
 * Functions for config options
 *
 * @package Core
 * @author  Andreas Gohr    <hide@address.com>
 * @author  Andreas Götz    <hide@address.com>
 * @version $Id: setup.core.php,v 1.3 2007/09/08 09:31:07 andig2 Exp $
 */

require_once './core/functions.php';
require_once './core/custom.php';
require_once './core/output.php';

$SETUP_GLOBAL = array('language', 'autoid', 'mediadefault', 'langdefault', 
                      'filterdefault', 'showtv', 'orderallbydisk', 'removearticles',
                      'localnet', 'IMDBage', 'thumbnail', 
                      'castcolumns', 'template', 'languageflags', 'custom1', 
                      'custom2', 'custom3', 'custom4', 'custom1type', 
                      'custom2type', 'custom3type', 'custom4type', 'recompile', 'enginedefault', 
                      'proxy_host', 'proxy_port', 'actorpics', 'thumbAge', 'listcolumns', 
                      'shownew', 'imdbBrowser', 'multiuser', 'denyguest', 'adultgenres',
                      'pageno', 'showtools', 'youtubekey');

$SETUP_QUICK  = array('template');

$SETUP_USER   = array('language', 'mediadefault', 'langdefault', 'filterdefault', 
                      'showtv', 'orderallbydisk', 'template', 'languageflags', 
                      'listcolumns', 'castcolumns', 'shownew', 'pageno', 'removearticles');

/**
 * build config options array
 *
 * returns associative array of config options
 *
 * @param boolean   $isprofile  Determines if user-specific options are to be displayed
 */
function setup_mkOptions($isprofile = false)
{
	global $config, $lang;

    // built list of setup options
	$setup = array();
    
    // isprofile, name, type (text|boolean|dropdown|special|link), data, set, helphl, helptxt
    $setup[] = setup_addSection('opt_general');
	$setup[] = setup_addOption($isprofile, 'language', 'dropdown', setup_getLanguages(), null, $lang['help_langn'], $lang['help_lang']);
    $option  = setup_addOption($isprofile, 'template', 'dropdown', setup_getTemplates($thumbs));
    $option['thumbs'] = $thumbs;
    $setup[] = $option;
    
    $setup[] = setup_addOption($isprofile, 'listcolumns', 'text');
    $setup[] = setup_addOption($isprofile, 'castcolumns', 'text');

    $setup[] = setup_addOption($isprofile, 'autoid', 'boolean');
    $setup[] = setup_addOption($isprofile, 'orderallbydisk', 'boolean');
    
    $setup[] = setup_addOption($isprofile, 'mediadefault', 'dropdown', setup_getMediatypes());
    $setup[] = setup_addOption($isprofile, 'langdefault', 'text');
    $setup[] = setup_addOption($isprofile, 'filterdefault', 'dropdown', array('all'=>$lang['radio_all'], 'unseen'=>$lang['radio_unseen'], 'new'=>$lang['radio_new'], 'wanted'=>$lang['radio_wanted']));
    $setup[] = setup_addOption($isprofile, 'showtv', 'boolean');
    $setup[] = setup_addOption($isprofile, 'shownew', 'text');
    $setup[] = setup_addOption($isprofile, 'pageno', 'text');
    $setup[] = setup_addOption($isprofile, 'languageflags', 'special', out_languageflags($config['languages']));
    $setup[] = setup_addOption($isprofile, 'removearticles', 'boolean');
    $setup[] = setup_addOption($isprofile, 'adultgenres', 'multi', setup_getGenres(), @split('::', $config['adultgenres']));
    $setup[] = setup_addOption($isprofile, 'recompile', 'boolean');
    $setup[] = setup_addOption($isprofile, 'showtools', 'boolean');

    if (!$isprofile) $setup[] = setup_addSection('opt_custom');
    $setup[] = setup_addOption($isprofile, 'custom', 'special', setup_mkCustoms());
    
    if (!$isprofile) $setup[] = setup_addSection('opt_engines');
    $setup[] = setup_addOption($isprofile, 'enginedefault', 'dropdown', setup_getEngines($config['engines']), null , $lang['help_defaultenginen'], $lang['help_defaultengine']);

    foreach ($config['engines'] as $engine => $meta)
    {
        $title      = $meta['name'];
        $enabled    = $config['engine'][$engine];        
        $helptext   = sprintf($lang['help_engine'], $title);
        $helptext  .= ' '.$lang['help_engine'.$engine];
        if (!$meta['stable']) $helptext .= ' '.$lang['help_engexperimental'];
        
        $setup[] = setup_addOption($isprofile, 'engine'.$engine, 'boolean', null, $enabled, $title, $helptext);
    }

    if (!$isprofile) $setup[] = setup_addSection('opt_security');
	$setup[] = setup_addOption($isprofile, 'localnet', 'text');
	$setup[] = setup_addOption($isprofile, 'multiuser', 'boolean');
	$setup[] = setup_addOption($isprofile, 'denyguest', 'boolean');
    $setup[] = setup_addOption($isprofile, 'usermanager', 'link', 'users.php');
	$setup[] = setup_addOption($isprofile, 'proxy_host', 'text');
	$setup[] = setup_addOption($isprofile, 'proxy_port', 'text');

    if (!$isprofile) $setup[] = setup_addSection('opt_caching');
    $setup[] = setup_addOption($isprofile, 'thumbnail', 'boolean');
    $setup[] = setup_addOption($isprofile, 'imdbBrowser', 'boolean');
    $setup[] = setup_addOption($isprofile, 'IMDBage', 'text');
    $setup[] = setup_addOption($isprofile, 'actorpics', 'boolean');
    $setup[] = setup_addOption($isprofile, 'thumbAge', 'text');

    if (!$isprofile) $setup[] = setup_addSection('opt_apikeys');
	// Youtube Intergration follows (Adam Benson - hide@address.com)
    $setup[] = setup_addOption($isprofile, 'youtubekey', 'text');

    // clean empty entries
    for ($i = count($setup); $i > 0; $i--)
    {
        if (empty($setup[$i]['name']) && empty($setup[$i]['group'])) unset($setup[$i]);
    }

	return $setup;
}

/**
 * Add a new section to the config options array
 *
 * @param array   $setup      The config array
 * @param string  $section    Name of the new section
 */
function setup_addSection($section)
{
    $option['group']    = $section;
    return $option;
}

/**
 * Adds an entry for the config option array
 *
 * returns NULL on global options if $isprofile is true 
 * so global options will not be added to user profile settings
 *
 * @param array   $setup      The config array
 * @param boolean $isprofile  Do we prepare a profile array?
 * @param string  $name       Name of the config option
 * @param string  $type       Type of option (text|boolean|dropdown|special|link)
 * @param string  $data       Current value of this option
 * @param string  $set        Default value of this option
 * @param string  $hl         Help text headline
 * @param string  $help       Help text
 */
function setup_addOption($isprofile, $name, $type, 
                         $data='', $set=NULL, $hl=NULL, $help=NULL)
{
	global $config, $lang;
    global $SETUP_USER;

    // user-specific setting?
    $isuser = in_array($name, $SETUP_USER);
    
	if ($isprofile and !$isuser) return;

	$option['isuser']   = $isuser;
	$option['name']     = $name;
	$option['type']     = $type;
	$option['data']     = $data;
    
    $option['set']  = ($set)  ? $set  : $config[$name];
    $option['hl']   = ($hl)   ? $hl   : $lang['help_'.$name.'n'];
    $option['help'] = ($help) ? $help : $lang['help_'.$name];

    return $option;
}

/**
 * Find available languages
 */
function setup_getLanguages()
{
    if ($dh = opendir('language')) 
    {
        while (($file = readdir($dh)) !== false) 
        {
            if (preg_match("/(.*)\.php$/", $file, $matches))
            {
                $languages[$matches[1]] = $matches[1];
            }
        }
        closedir($dh);
    }
    return $languages;
}

/**
 * Find available templates/styles
 * Extended to search for template screenshots
 *
 * @author  Andreas Götz    <hide@address.com>
 */
function setup_getTemplates(&$screenshots)
{
    $screenshots = array();
    
	if ($dh = @opendir('templates'))
	{
		while (($file = readdir($dh)) !== false)
		{
			if (preg_match("/^\./", $file)) continue;
			if (is_dir('templates/'.$file))
			{
                $template = 'templates/'.$file;
				if ($dh2 = opendir($template))
				{
                    $style_name = '';

					while (($style = readdir($dh2)) !== false)
					{
						if (preg_match("/(.*)\.css$/", $style, $matches)) 
                        {
                            $thumb = $template.'/screenshot_'.$matches[1].'.jpg';
                            if (file_exists($thumb))
                            {
                                $screenshots[] = array('name' => "$file::".$matches[1], 'img' => $thumb);
                            }
                            elseif (empty($style_name))
                            {
                                // remember first style found
                                $style_name = $matches[1];
                            }
							$templates[$file.'::'.$matches[1]] = $file.' ('.$matches[1].')';
						}
					}
	    			closedir($dh2);
                    
                    if ($style_name)
                    {
                        $thumb = $template.'/screenshot.jpg';
                        if (file_exists($thumb))
                        {
                            $screenshots[] = array('name' => "$file::$style_name", 'img' => $thumb);
                        }
                    }

				}
			}
		}
		closedir($dh);
	}
	return $templates;
}

/**
 * Mediatypes
 */
function setup_getMediatypes()
{
    $SELECT = 'SELECT id, name
               FROM '.TBL_MEDIATYPES.'
           ORDER BY name';
    $result = runSQL($SELECT);
    
    foreach($result as $row)
    {
        $mediatypes[$row['id']] = $row['name'];
    }
    
    return $mediatypes;
}

/** 
 * Genres
 */
function setup_getGenres()
{
    $SELECT = 'SELECT id, name
                 FROM '.TBL_GENRES.'
             ORDER BY name';
    $result = runSQL($SELECT);
    
    foreach($result as $row)
    {
        $genres[$row['id']] = $row['name'];
    }
    
    return $genres;
}

/**
 * Get list of engines for default engine selection
 */
function setup_getEngines($engines_ar)
{
	$engines = array();
	
	foreach ($engines_ar as $engine => $meta)
    {
        $engines[$engine] = $meta['name'];
    }
    
    return $engines;
}

/**
 * Prepare customfields
 */
function setup_mkCustoms()
{
    global $config;
    global $allcustomtypes;
    
    $setup_custom = '';
    
    for ($i=1; $i<5; $i++)
    {
        $setup_custom .= $i.'. <input type="text" size="20" name="custom'.$i.'" id="custom'.$i.'" value="'.formvar($config['custom'.$i]).'"/>';
        $setup_custom .= '<select name="custom'.$i.'type">';
    
        foreach($allcustomtypes as $ctype)
        {
            $selected       = ($ctype == $config['custom'.$i.'type']) ? ' selected="selected"' : '';
            $setup_custom  .= '<option value="'.$ctype.'"'.$selected.'>'.$ctype.'</option>';
        }
        $setup_custom .= '</select>';
        $setup_custom .= "<br />\n";
    }
    
    return $setup_custom;
}


/**
 * Check cache folder for expired entries
 *
 * @author Andreas Goetz
 * @param   string  $dir  cache folder
 * @param   boolean $all  return list of all files (or outdated files)
 * @return  array   $total, $expired, $files    sum and list of total and expired files
 */
function analyzeCacheFolder($dir, $all = false)
{
    global $config;

    $files = array();
    
    if ($handle = opendir($dir))
    {
        // read cache directory (note syntax, according to docs!)
        while (false !== ($file = readdir($handle)))
        {
            // prevent deletion of hidden files (*nix) or directory references (Windows)
            if (preg_match("/^\./", $file)) continue;
            $cfile = "$dir/$file";

            // file found?
            if (!is_dir($cfile)) 
            {
                $total += filesize($cfile);
                if ($all || !(time()-filemtime($cfile) < $config['IMDBage']))
                {
                    $expired += filesize($cfile);
                    $files[] = $cfile;
                }
            }
            
            // or hierarchical cache directories?
            elseif ($config['hierarchical']) 
            {
                // one-char directory name?
                if (preg_match("/^\w$/", $file)) 
                {
                    list($atotal, $aexpired, $afiles) = analyzeCacheFolder($cfile, $all);
                    $total += $atotal;
                    $expired += $aexpired;
                    $files = array_merge($files, $afiles);
                }
            }
        }
        closedir($handle);
    }

    return array($total, $expired, $files);
}

/**
 * Update session variables with configuration values
 *
 * @author Andreas Goetz
 */
function update_session()
{
    global $listcolumns, $showtv;
    
    if ($listcolumns) $_SESSION['vdb']['listcolumns'] = $listcolumns;
    if ($showtv) $_SESSION['vdb']['showtv'] = $showtv;
}

?>
Return current item: VideoDB