Location: PHPKode > projects > PhpGiggle > phpGiggle/includes/phpgiggle-admin-functions.inc.php
<?

/**
    * Copyright (C) 2004 Tony Bierman
    * 
    * This program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public License
    * as published by the Free Software Foundation; either version 2
    * of the License, or (at your option) any later version.
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    * GNU General Public License for more details.
    * You should have received a copy of the GNU General Public License
    * along with this program; if not, write to the Free Software
    * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    *
*/

/*
    
    see README.html for complete documentation
*/

function AddConfValue($strCfgFile,$strCfgVar,$strCfgVal)
{
    $strOldContent = file ($strCfgFile);
    usort($strOldContent, "sort_by_length");
    $strNewContent = "";
    $strCfgVar = str_replace(" ","_",$strCfgVar);
    if(preg_match('/\s/',  $strCfgVal)) { $strCfgVal = "'" . $strCfgVal . "'"; }
    /*
    if(preg_match("/".$valid_key_regexp."/i", $strCfgVar))
    {
	print_error("Keyword $strCfgVar contains illegal characters. Words file $strCfgFile was not revised.");
	$phpgiggle_errors++;
    }
    */
    
    if(!$phpgiggle_errors)
    {
	while (list ($intLineNum, $strLine) = each ($strOldContent)) 
	{
	    if(eregi("^".$strCfgVar,$strLine))
	    {
		$strLineParts=explode(" = ",$strLine);
		// we should determine type of value here! (BOOL, INT or String)
		if($strCfgVar == trim($strLineParts[0])) 
		{
		    $strLineParts[1] = $strCfgVal;
		    $strLine = implode(" = ",$strLineParts)."\r\n";
		}
		$matched_word++;
	    }
	    $strNewContent .= $strLine;
	    $fp = fopen($strCfgFile."_new", "w"); 
	    fputs($fp,$strNewContent);
	    fclose($fp);
	}
	if(!$matched_word)
	{
	    $strNewContent .= $strCfgVar." = ".$strCfgVal."\n";    
	    $fp = fopen($strCfgFile."_new", "w"); 
	    fputs($fp,$strNewContent);
	    fclose($fp);
	}
	
	echo '<div id="content">'."\n";
	echo "<h2>Keyword has been added</h2><br/>\nKeyword <b>".str_replace("_"," ",$strCfgVar)."</b> has been updated to <b>$strCfgVal</b> in file <b>$strCfgFile</b>\n";
	if(!rename($strCfgFile,$strCfgFile.".bak")) echo "<h2>Error adding keyword</h2><br/>\n<b>Error: Could not rename old file!</b>\n";
	if(!rename($strCfgFile."_new",$strCfgFile)) echo "<h2>Error adding keyword</h2><br/>\n<b>Failed to copy file!</b>\n";
    }
}

function DeleteConfValue($strCfgFile,$strCfgVar)
{
    $strOldContent = file ($strCfgFile);
    usort($strOldContent, "sort_by_length");
    $strNewContent = "";
    
    while (list ($intLineNum, $strLine) = each ($strOldContent)) 
    {
        if(eregi("^".preg_quote($strCfgVar),$strLine))
        {
            $strLineParts=explode(" = ",$strLine);
            // we should determine type of value here! (BOOL, INT or String)
            if($strCfgVar == trim($strLineParts[0])) { continue; }
        }
        $strNewContent .= $strLine;
        $fp = fopen($strCfgFile."_new", "w"); 
        fputs($fp,$strNewContent);
        fclose($fp);
    }
    
    echo '<div id="content">'."\n";
    echo "<h2>Keyword has been deleted</h2><br/>\nKeyword <b>".str_replace("_"," ",$strCfgVar)."</b> has been deleted in file <b>$strCfgFile</b>\n";
    if(!rename($strCfgFile,$strCfgFile.".bak")) echo "<h2>Error deleting keyword</h2><br/>\n<b>Error: Could not rename old file!</b>\n";
    if(!rename($strCfgFile."_new",$strCfgFile)) echo "<h2>Error deleting keyword</h2><br/>\n<b>Failed to copy file!</b>\n";
}

function ReplaceConfValue($strCfgFile,$strCfgVar,$strCfgVal)
{
    $strOldContent = file ($strCfgFile);
    usort($strOldContent, "sort_by_length");
    $strNewContent = "";
    if(preg_match('/\s/',  $strCfgVal)) { $strCfgVal = "'" . $strCfgVal . "'"; }
    
    while (list ($intLineNum, $strLine) = each ($strOldContent)) 
    {
        if(eregi("^".$strCfgVar,$strLine))
        {
            $strLineParts=explode(" = ",$strLine);
            // we should determine type of value here! (BOOL, INT or String)
            if($strCfgVar == trim($strLineParts[0])) 
            {
                $strLineParts[1] = $strCfgVal;
                $strLine = implode(" = ",$strLineParts)."\r\n";
            }
        }
        $strNewContent .= $strLine;
        $fp = fopen($strCfgFile."_new", "w"); 
        fputs($fp,$strNewContent);
        fclose($fp);
    }
    
    echo '<div id="content">'."\n";
    echo "<h2>Keyword has been updated</h2><br/>\nKeyword <b>".str_replace("_"," ",$strCfgVar)."</b> has been updated to <b>$strCfgVal</b> in file <b>$strCfgFile</b>\n";
    if(!rename($strCfgFile,$strCfgFile.".bak")) echo "<h2>Error updating keyword</h2><br/>\n<b>Error: Could not rename old file!</b>\n";
    if(!rename($strCfgFile."_new",$strCfgFile)) echo "<h2>Error updating keyword</h2><br/>\n<b>Failed to copy file!</b>\n";
}

function print_error($message)
{
    echo '<h2>phpGiggle encountered an error!</h2>'."\n";
    echo '<p>'.$message.'</p>'."\n";
}

function print_info($title, $message)
{
    echo '<h2>'.$title.'</h2>'."\n";
    echo '<p>'.$message.'</p>'."\n";
}

function sort_by_length($a, $b) 
{
    $a_parts=explode(" = ",$a);
    $b_parts=explode(" = ",$b);
    $a_len=strlen(trim($a_parts[0]));
    $b_len=strlen(trim($b_parts[0]));
    if ($a_len == $b_len)
    {
	return 0;
    }
    return ($a_len  < $b_len) ? 1 : -1;
}

function validate_keywords()
{
    require(phpGiggle_ABSPATH.'includes/phpgiggle-regexp.inc.php');
    // sluuurrp the file to an array
    // RAM is cheap, right?
    $file_lines = file(phpgiggle_wordsfile);
    natcasesort($file_lines);
    reset($file_lines);

    echo "<table>\n";
    // iterate array
    while (list ($line_number, $line) = each ($file_lines)) 
    {
	$line_parts=explode(" = ",$line);
	$case_sensitive_search_word = ltrim(rtrim($line_parts[0]));
	$giggle_link = ltrim(rtrim($line_parts[1]));
	
	echo '<tr><td>'.str_replace("_"," ",$case_sensitive_search_word).'</td>';
        //validate keyword
	if(preg_match("/".$valid_key_regexp."/i", $case_sensitive_search_word))
	{
	    echo '<td>Invalid keyword or phrase!!</td></tr>'."\n";	
	}        
	else
	{
	    switch ($giggle_link) {
		// phpGiggle template
		case function_exists("phpgiggle_".$giggle_link):
		    $giggle_function = "phpgiggle_".$giggle_link;
		    $newtext = $giggle_function(str_replace("_"," ",$case_sensitive_search_word));
		    break;
		// highlight
		case (eregi("$valid_html_color", $giggle_link)?$giggle_link:!$giggle_link):
		    $newtext = "<span title=\"phpGiggle Highlighter !\" style=\"background-color: ".
			$giggle_link.";\">".str_replace("_"," ",$case_sensitive_search_word)."</span>";     
		    break;
		// just link
		case (eregi("$valid_url_regexp", $giggle_link)?$giggle_link:!$giggle_link):
		    $newtext = "<a ".phpgiggle_target." title=\"phpGiggle it!\" href=\"".
			$giggle_link ."\">".str_replace("_"," ",$case_sensitive_search_word)."</a>";
		    break;
		// Acronym or just alt-text
		default:
		    $newtext = '<abbr title="'.$giggle_link.'">'.$case_sensitive_search_word.'</abbr>';
	    } 	    
    	    echo '<td>'.$newtext.'</td></tr>'."\n";
        }
    }
    echo "</table>\n";
}
?>
Return current item: PhpGiggle