Skin -> Templates functions
| > Module written by Matt Mecham
| > Date started: 15th April 2002
|
| > Module Version Number: 1.0.0
+--------------------------------------------------------------------------
*/
$idx = new ad_settings();
class ad_settings {
var $base_url;
function ad_settings() {
global $IN, $root_path, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
switch($IN['code'])
{
case 'add':
$this->add_templates();
break;
case 'edit':
$this->do_form('edit');
break;
case 'edit2':
$this->show_file();
break;
case 'doedit':
$this->do_edit();
break;
case 'remove':
$this->remove();
break;
case 'tools':
$this->tools();
break;
case 'editinfo':
$this->edit_info();
break;
case 'export':
$this->export();
break;
//-------------------------
default:
$this->list_current();
break;
}
}
function export()
{
global $IN, $root_path, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP;
if ($IN['id'] == "")
{
$ADMIN->error("You must specify an existing template set ID, go back and try again");
}
//+-------------------------------
$DB->query("SELECT * from ibf_tmpl_names WHERE skid='".$IN['id']."'");
if ( ! $row = $DB->fetch_row() )
{
$ADMIN->error("Could not query the information from the database");
}
//+-------------------------------
$archive_dir = $INFO['base_dir']."/archive_out";
$templates_dir = $INFO['base_dir']."/style_templates/".$row['skid'];
require $root_dir."sources/lib/tar.php";
if (!is_dir($archive_dir))
{
$ADMIN->error("Could not locate $archive_dir, is the directory there?");
}
if (!is_writeable($archive_dir))
{
$ADMIN->error("Cannot write in $archive_dir, CHMOD via FTP to 0755 or 0777 to enable this script to write into it. IBF cannot do this for you");
}
if (!is_dir($templates_dir))
{
$ADMIN->error("Could not locate $templates_dir, is the directory there?");
}
//+-------------------------------
// Attempt to copy the files to the
// working directory...
//+-------------------------------
$l_name = preg_replace( "/\s{1,}/", "_", $row['skname'] );
$new_dir = "tmpl-".$l_name;
if ( ! $ADMIN->copy_dir($templates_dir, $archive_dir."/".$new_dir) )
{
$ADMIN->error( $ADMIN->errors );
}
// Generate the config file..
$file_content = " \"".addslashes($row['author'])."\", ".
"'email'=>\"".addslashes($row['email'])."\", ".
"'url'=>\"".addslashes($row['url'])."\")\n\n?".">";
$FH = fopen($archive_dir."/".$new_dir."/"."conf.inc", 'w');
fwrite($FH, $file_content, strlen($file_content));
fclose($FH);
// Add files and write tarball
$tar = new tar();
$tar->new_tar( $archive_dir, $new_dir.".tar" );
$tar->add_directory( $archive_dir."/".$new_dir );
$tar->write_tar();
// Check for errors.
if ($tar->error != "")
{
$ADMIN->error($tar->error);
}
// remove original unarchived directory
$ADMIN->rm_dir($archive_dir."/".$new_dir);
$ADMIN->done_screen("Template Pack Export Created
You can download the tar-chive here", "Manage Template Sets", "act=templ" );
}
//+--------------------------------------------------------------------------------
//+--------------------------------------------------------------------------------
function tools_rebuildphp()
{
global $IN, $root_path, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
if ($IN['id'] == "")
{
$ADMIN->error("You must specify an existing template set ID, go back and try again");
}
//+-------------------------------
$DB->query("SELECT * from ibf_tmpl_names WHERE skid='".$IN['id']."'");
if ( ! $row = $DB->fetch_row() )
{
$ADMIN->error("Could not query the information from the database");
}
$templates_dir = $root_path."style_templates/".$IN['id'];
$skin_dir = $root_path."Skin/s".$IN['id'];
$errors = array();
$flag = 0;
if (! file_exists( $root_path."style_templates/".$IN['id']."/config.php" ) )
{
$ADMIN->error("Could not locate the template set configuration file, make sure 'config.php' resides in the templates directory you are editing. If
it is not there, you can copy one from another directory.");
}
//+-------------------------------
require $root_path."style_templates/".$IN['id']."/config.php";
//+-------------------------------
if ( ! is_writeable($skin_dir) )
{
$ADMIN->error("Cannot write into '$skin_dir', please check the CHMOD value, and if needed, CHMOD to 0777 via FTP. IBF cannot do this for you.");
}
if ( ! is_writeable($templates_dir) )
{
$ADMIN->error("Cannot write into '$templates_dir', please check the CHMOD value, and if needed, CHMOD to 0777 via FTP. IBF cannot do this for you.");
}
if ( is_dir($templates_dir) )
{
if ( $handle = opendir($templates_dir) )
{
while (($filename = readdir($handle)) !== false)
{
if (($filename != ".") && ($filename != ".."))
{
if ( preg_match( "/^index\./", $filename ) )
{
continue;
}
//-------------------------------------------
if ( preg_match( "/\.html$/", $filename ) )
{
$name = preg_replace( "/\.html$/", "", $filename );
if ( file_exists($skin_dir."/".$name.".php") )
{
if (! is_writeable($skin_dir."/".$name.".php") )
{
$errors[] = "Cannot write to $name".".php, skipping file...";
continue;
}
}
if ($FHD = fopen($templates_dir."/".$filename, 'r') )
{
$text = fread($FHD, filesize($templates_dir."/".$filename) );
fclose($FHD);
}
else
{
$errors[] = "Could not open $filename, skipping file...";
continue;
}
//----------------------------------------------------
$need = count($skin[$name]);
$start = 0;
$end = 0;
if ($need < 1)
{
$errors[] = "Error recalling function data for $filename, skipping...";
continue;
}
// check to make sure the splitter tags are intact
foreach($skin[$name] as $func_name => $data)
{
if ( preg_match("//", $text) )
{
$start++;
}
//+-------------------------------
if ( preg_match("//", $text) )
{
$end++;
}
}
if ($start != $end)
{
$errors[] = "Some start or end template splitter comments are missing in $filename, skipping file....";
continue;
}
if ($start != $need)
{
$errors[] = "Some template splitter comments are missing in $filename, skipping file...";
continue;
}
//+-------------------------------
// Convert the tags back to php native
//+-------------------------------
$text = $this->unconvert_tags($text);
//+-------------------------------
// Start parsing the php skin file
//+-------------------------------
$final = "<"."?php\n\n".
"class $name {\n\n";
foreach($skin[$name] as $func_name => $data)
{
$top = "\n\nfunction $func_name($data) {\n".
"global \$ibforums;\n".
"return <<\s*\n/", "$top", $text);
//+-------------------------------
$text = preg_replace("/\s*\s*\n/", "$bum", $text);
}
$end = "\n\n}\n?".">";
$final .= $text.$end;
if ($fh = fopen( $skin_dir."/".$name.".php", 'w' ) )
{
fwrite($fh, $final, strlen($final) );
fclose($fh);
}
else
{
$errors[] = "Could not save information to $phpskin, please ensure that the CHMOD permissions are correct.";
}
$end = "";
$final = "";
$top = "";
} // if *.php
} // if not dir
} // while loop
closedir($handle);
}
else
{
$errors[] = "Could not open directory $templates_dir for reading!";
}
}
else
{
$errors[] = "$templates_dir is not a directory, please check the \$root_path variable in admin.php";
}
if (count($errors > 0))
{
$ADMIN->html .= $SKIN->start_table("Errors and warnings");
$ADMIN->html .= $SKIN->add_td_basic( implode("
", $errors) );
$ADMIN->html .= $SKIN->end_table();
}
$ADMIN->done_screen("Source PHP Skin files updated from the editable templates", "Manage Template sets", "act=templ" );
}
function edit_info()
{
global $IN, $root_path, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
if ($IN['id'] == "")
{
$ADMIN->error("You must specify an existing template set ID, go back and try again");
}
//+-------------------------------
$DB->query("SELECT * from ibf_tmpl_names WHERE skid='".$IN['id']."'");
if ( ! $row = $DB->fetch_row() )
{
$ADMIN->error("Could not query the information from the database");
}
$final['skname'] = stripslashes($HTTP_POST_VARS['skname']);
if (isset($HTTP_POST_VARS['author']))
{
$final['author'] = stripslashes($HTTP_POST_VARS['author']);
$final['email'] = stripslashes($HTTP_POST_VARS['email']);
$final['url'] = stripslashes($HTTP_POST_VARS['url']);
}
$db_string = $DB->compile_db_update_string( $final );
$DB->query("UPDATE ibf_tmpl_names SET $db_string WHERE skid='".$IN['id']."'");
$ADMIN->done_screen("Template information updated", "Manage Template sets", "act=templ" );
}
//+-------------------------------
//+-------------------------------
function do_edit()
{
global $IN, $root_path, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
$text = stripslashes($HTTP_POST_VARS['template']);
if ($IN['id'] == "")
{
$ADMIN->error("You must specify an existing template set ID, go back and try again");
}
//+-------------------------------
$DB->query("SELECT * from ibf_tmpl_names WHERE skid='".$IN['id']."'");
if ( ! $row = $DB->fetch_row() )
{
$ADMIN->error("Could not query the information from the database");
}
//+-------------------------------
$real_name = preg_replace( "/\.html/", "", $IN['name'] );
//+-------------------------------
$template = $root_path."style_templates/".$IN['id']."/".$IN['name'];
$phpskin = $root_path."Skin/s".$IN['id']."/".$real_name.".php";
//+-------------------------------
if (! file_exists( $root_path."style_templates/".$IN['id']."/config.php" ) )
{
$ADMIN->error("Could not locate the template set configuration file, make sure 'config.php' resides in the templates directory you are editing. If
it is not there, you can copy one from another directory.");
}
//+-------------------------------
require $root_path."style_templates/".$IN['id']."/config.php";
//+-------------------------------
if ( ! is_writeable($template) )
{
$ADMIN->error("Cannot write into '$template', please check the CHMOD value, and if needed, CHMOD to 0777 via FTP. IBF cannot do this for you.");
}
if ( ! is_writeable($phpskin) )
{
$ADMIN->error("Cannot write into '$phpskin', please check the CHMOD value, and if needed, CHMOD to 0777 via FTP. IBF cannot do this for you.");
}
//+-------------------------------
// Ok, make sure we actually have
// some info to parse here.
//+-------------------------------
if ($text == "")
{
$ADMIN->error("You can't delete the template in this manner");
}
//+-------------------------------
// Swop back < and >
//+-------------------------------
$text = preg_replace("/</", "<", $text);
$text = preg_replace("/>/", ">", $text);
$text = preg_replace("/&/", "&", $text);
//+-------------------------------
// Convert \r to nowt
//+-------------------------------
$text = preg_replace("/\r/", "", $text);
//+-------------------------------
// Make sure we have enough subs
// left. Check to make sure we've
// not removed and start and end tags
//+-------------------------------
$start = 0;
$end = 0;
$need = count($skin[$real_name]);
//
// check to make sure the splitter tags are intact
foreach($skin[$real_name] as $func_name => $data)
{
if ( preg_match("//", $text) )
{
$start++;
}
//+-------------------------------
if ( preg_match("//", $text) )
{
$end++;
}
}
if ($start != $end)
{
$ADMIN->error("Some start or end template splitter comments are missing, please go back and try again");
}
if ($start != $need)
{
$ADMIN->error("Some template splitter comments are missing, please go back and try again");
}
//+-------------------------------
// Save the template back to disk
//+-------------------------------
if ($fh = fopen( "$template", 'w' ) )
{
fwrite( $fh, $text, strlen($text) );
fclose($fh);
}
else
{
$ADMIN->error("Could not save information to $template, please ensure that the CHMOD permissions are correct.");
}
//+-------------------------------
// Convert the tags back to php native
//+-------------------------------
$text = $this->unconvert_tags($text);
//+-------------------------------
// Start parsing the php skin file
//+-------------------------------
$final = "<"."?php\n\n".
"class $real_name {\n\n";
foreach($skin[$real_name] as $func_name => $data)
{
$top = "\n\nfunction $func_name($data) {\n".
"global \$ibforums;\n".
"return <<\s*\n/", "$top", $text);
//+-------------------------------
$text = preg_replace("/\s*\s*\n/", "$bum", $text);
}
$end = "\n\n}\n?".">";
$final .= $text.$end;
if ($fh = fopen( "$phpskin", 'w' ) )
{
fwrite($fh, $final, strlen($final) );
fclose($fh);
}
else
{
$ADMIN->error("Could not save information to $phpskin, please ensure that the CHMOD permissions are correct.");
}
$ADMIN->done_screen("Template file updated", "Manage Templates in template set: {$row['skname']}", "act=templ&code=edit&id={$IN['id']}" );
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
function tools()
{
global $IN, $root_path, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
if ($IN['id'] == "")
{
$ADMIN->error("You must choose a valid skin file to perform this operation on");
}
if ($IN['tool'] == 'conf')
{
$this->tools_conf();
}
else if ($IN['tool'] == 'tmpl')
{
$this->tools_build_tmpl();
}
else
{
$this->tools_rebuildphp();
}
}
//------------------------------------------------------------------------------------
function tools_build_tmpl()
{
global $IN, $root_path, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $HTTP_POST_VARS;
// Rebuilds the skin.html editable files from the PHP source files
$templates_dir = $root_path."style_templates/".$IN['id'];
$skin_dir = $root_path."Skin/s".$IN['id'];
$errors = array();
$flag = 0;
if ( ! is_writeable($skin_dir) )
{
$ADMIN->error("Cannot write into '$skin_dir', please check the CHMOD value, and if needed, CHMOD to 0777 via FTP. IBF cannot do this for you.");
}
if ( ! is_writeable($templates_dir) )
{
$ADMIN->error("Cannot write into '$templates_dir', please check the CHMOD value, and if needed, CHMOD to 0777 via FTP. IBF cannot do this for you.");
}
if ( is_dir($skin_dir) )
{
if ( $handle = opendir($skin_dir) )
{
while (($filename = readdir($handle)) !== false)
{
if (($filename != ".") && ($filename != ".."))
{
if ( preg_match( "/\.php$/", $filename ) )
{
$name = preg_replace( "/^(\S+)\.(\S+)$/", "\\1", $filename );
if ( file_exists($templates_dir."/".$name.".html") )
{
if (! is_writeable($templates_dir."/".$name.".html") )
{
$errors[] = "Cannot write to $name".".html, skipping file...";
}
}
$farray = file($skin_dir."/".$filename) or die("Could not open $skin_dir/$filename for reading");
//$tfh = fopen($skin_dir."/".$filename , 'r')
//----------------------------------------------------
$functions = array();
foreach($farray as $f)
{
// Skip javascript functions...
if ( preg_match( "/