';
$closing_body.= '
';
$closing_body.= '
';
$closing_body.= '

';
$closing_body.= '
Please wait...
';
$closing_body.= '
This page is about to reload to refresh any changes you have made.
';
$closing_body.= '
When complete, this window will automatically close.
';
$closing_body.= '
';
$closing_body.= '
';
$closing_body.= "\n";
//$closing_body .= "\n";
}
$closing_body .= "";
$source = str_replace("",$closing_body,$source);
// replace the nyx_ tags
$source = str_replace("[nyx_page_label]",$page['label'],$source);
$source = str_replace("[nyx_base_href]",BASE_URL,$source);
$source = str_replace("[nyx_index_id]",$page['index_id'],$source);
$source = str_replace("[nyx_master_parent_id]",$page['master_parent_id'],$source);
parse_inner_template($db,$source);
//return $source;
$cached_data = ob_get_contents();
ob_clean();
return $cached_data;
}
//--------------------------------------------------
?>
//------------------------------------------
class plugin {
var $name;
var $label;
var $template_path;
var $id = 0;
// Hooks is an comma delim list of child plugins which need
// to be hooked into this one
var $hooked_into = "";
// which other plugins does this one require?
// eg page-permissions requires page properties
// add to the requirements with $this->requires("plugin_label");
var $requirements = array();
// $hooks is an array of child plugins which are
// 'hooked' into this one
var $hooks = array();
//-----------------------------
// I um der constructicon
//function plugin($startname,$desc){
function __construct($startname,$desc,$path =""){
$this->label = $startname;
$this->name = $desc;
if (is_dir(DIR_ROOT."/plugins/plugin_".$startname."/templates/")){
$this->template_path = DIR_ROOT."/plugins/plugin_".$startname."/templates/";
}else{
$this->template_path = BASE_PATH."/plugins/plugin_".$startname."/templates/";
}
}//---
//-----------------------------
// Add a required plugin
function requires($plugin_label){
$this->requirements[] = $plugin_label;
}//---
//-----------------------------
// Hook into another plugin
function hook_into($plugin_label){
$this->hooked_into .= $plugin_label.",";
}//---
//------------------------------
// get a list of plugins which need
// to 'hook' into this one
function get_hooks(){
global $plugins;
foreach($plugins as $plugin){
if (is_int(strpos($plugin->hooked_into,$this->label) ) ){
$this->hooks[] = array("label"=>$plugin->label,"name"=>$plugin->name);
}
}
}//---
//------------------------------
// Display the plugins container
//
// Paths that start with a '/'
// are local container paths
//
function get_container($container){
if (substr($container,0,1) == "/"){
// path is absolute so load alocal container
$plugin_type = "local";
$include_path = DIR_ROOT.$container;
}else{
// load a system container
$plugin_type = "system";
$include_path = $this->template_path.$container;
}
if (file_exists($include_path)){
return $include_path;
}else{
error("Could not find the ".$plugin_type." container file '".$container."' for the '".$this->name."' plugin.");
return false;
}
}//---
//------------------------------
// ensure that the plugins required
// for this plugin are available to
// be installed, or are already installed
function check_requirements($db){
foreach($this->requirements as $req){
$sql = "SELECT * FROM plugins WHERE plugin_name='plugin_".$req."'";
$db->query($sql);
if ($db->num_rows() == 0 ){
// the required plugin is not already installed so
// check if it's available to be installed
$plugin_path_base = BASE_PATH."/plugins/plugin_".$req."/plugin_".$req.".inc.php";
$plugin_path_local = DIR_ROOT."/plugins/plugin_".$req."/plugin_".$req.".inc.php";
if (!file_exists($plugin_path_base) && !file_exists($plugin_path_local)){
admin_message("Warning, the '".$this->label."' plugin requires the '".$req."' plugin however '".$req."' cannot be found.");
return false;
}
}
}
return true;
}//---
//------------------------------
// Installs all the plugins dependancies,
// then installs itself
function master_install($db){
global $plugins;
//$master_plugins = $plugins;
//$plugins = array();
if ($this->label == "base"){
$this->install($db);
// load the templates so the base plugins can be installed
check_for_templates($db);
}
// first check that the dependancies are ok
if (!$this->check_requirements($db)){
admin_message("Warning, the '".$this->label."' plugin could not be installed because it's dependancies could not be met.");
return false;
}
// loop through the dependancies and install
$dependancies_installed_ok = true;
foreach($this->requirements as $req){
// check if the required plugin is already installed
$sql = "SELECT * FROM plugins WHERE plugin_name='".$req."'";
$db->query($sql);
if($db->num_rows() == 0){
$plugin_path_base = BASE_PATH."/plugins/plugin_".$req."/plugin_".$req.".inc.php";
$plugin_path_local = DIR_ROOT."/plugins/plugin_".$req."/plugin_".$req.".inc.php";
if (file_exists($plugin_path_local)){
$installed_path = $plugin_path_local;
}else if (file_exists($plugin_path_base) ){
$installed_path = $plugin_path_base;
}else{
admin_message("There was a problem installing the '".$this->label."' plugin. It requires the '".$req."' plugin which could not be found.");
return false;
}
// include the plugin
include_once $installed_path;
//if (method_exists($tmp_plugin,"install")) $tmp_plugin->install($db);
if (method_exists($plugins[$req],"install")){
$install_ok = $plugins[$req]->install($db,true);
}else{
$install_ok = true;
}
if ($install_ok){
$sql = "INSERT INTO plugins SET plugin_name='".$plugins[$req]->label."',importance='".$plugins[$req]->importance."'";
$db->query($sql);
admin_message("The '".$req."' plugin has been successfully installed.");
audit($db,"plugin_installed","
".$req."");
}else{
$dependancies_installed_ok = false;
}
}
}
if ($dependancies_installed_ok){
// install THIS plugin
if (method_exists($this,"install") && $this->label != "base"){
$install_ok = $this->install($db,true);
}else{
$install_ok = true;
}
if($install_ok){
$sql = "INSERT INTO plugins SET plugin_name='".$this->label."',importance='".$this->importance."'";
$db->query($sql);
admin_message("The '".$this->label."' plugin has been successfully installed.");
audit($db,"plugin_installed","
".$this->label."");
}
}else{
admin_message("Error. The '".$this->label."' plugin could not be installed because there was an error installing one of its dependancies.");
}
//$plugins = $master_plugins;
return true;
}
//------------------------------
//-------------------------------------------------
// generic installer used by many stardard plugins
//
// - installs the plugin
// - copies the template
// - installs the template
//
function simple_install($db,$stand_alone_override = false,$auto_create_page = true){
//-------------------------------------------------
// Don't auto-install if we're in a shared environment
if (!STAND_ALONE && !$stand_alone_override) return false;
// run the install sql, if any
if (file_exists($this->template_path."../install.sql")){
$install_sql = file_get_contents($this->template_path."../install.sql");
$install_sql = str_replace("\n","\r",$install_sql);
if ($install_sql !=""){
$queries = explode(";\r",$install_sql);
foreach($queries as $query){
$query = trim($query);
if ($query != "") $db->query($query);
}
}
}
// get the required template_id
$sql = "SELECT template_id FROM templates WHERE path='".$this->label.".tpl.html'";
$db->query($sql);
if($db->num_rows() < 1){
// copy the template from the base install
if (file_exists(BASE_PATH."/templates/".$this->label.".tpl.html")){
copy_template_from_base($this->label);
// install the template
$inst_res = install_template($db,DIR_ROOT."/templates/",$this->label.".tpl.html");
check_for_templates($db);
}
}
// get the required template_id
$sql = "SELECT template_id FROM templates WHERE path='".$this->label.".tpl.html'";
$db->query($sql);
if($db->next_record()){
$target_template_id = $db->f("template_id");
// add the page
if ($auto_create_page) add_page($db,$this->name,$target_template_id,"0",array("public","web_admin" ) );
}
/*
else{
// we can't find the correct template
admin_message("The '".$this->name."' plugin could not be installed because a required template '".$this->label.".tpl.html' could not be found");
return false;
}
*/
// add the page
//add_page($db,$this->name,$target_template_id,"0",array("public","web_admin" ) );
return true;
//---
}
//---
//-------------------------------------------------
// The reverse action to the simple install
function simple_uninstall($db){
//-------------------------------------------------
$sql = "DELETE FROM plugins WHERE plugin_name='".$this->label."'";
$db->query($sql);
// delete the template
$sql = "DELETE FROM templates WHERE path='".$this->label.".tpl.html'";
$db->query($sql);
$sql = "DELETE FROM options WHERE plugin='".$this->label."'";
$db->query($sql);
remove_template($db,$this->label);
admin_message("The '".$this->name."' plugin has been successfully uninstalled.");
audit($db,"uninstalled_plugin",$this->label);
}
//---
}
//------------------------------------------
?>
//-------------------------------------------------
function dbi($in){
$out = addslashes($in);
return $out;
}
//---
//-------------------------------------------------
function dbo($in){
$out = stripslashes($in);
return $out;
}
//---
//-------------------------------------------------
// get the page details from the index_id
function get_page_details($db,$index_id,$page_id = ""){
$page_details = array(
"index_id"=>"",
"parent_id"=>"",
"order_id"=>"",
"url"=>"",
"show_in_menu"=>"",
"new_window"=>"",
"page_id"=>"",
"label"=>"",
"name"=>"",
"template_id"=>"",
"page_title"=>"",
"page_description"=>"",
"page_keywords"=>""
);
if ($page_id == ""){
$sql = "SELECT si.*,p.* FROM site_index si LEFT JOIN pages p ON si.page_id = p.page_id WHERE si.index_id='".$index_id."'";
}else{
$sql = "SELECT si.*,p.* FROM site_index si LEFT JOIN pages p ON si.page_id = p.page_id WHERE si.page_id='".$page_id."'";
}
$db->query($sql);
while($db->next_record()){
$page_details = array(
"index_id"=>$db->f("index_id"),
"parent_id"=>$db->f("parent_id"),
"order_id"=>$db->f("order_id"),
"url"=>$db->f("url"),
"show_in_menu"=>$db->f("show_in_menu"),
"new_window"=>$db->f("new_window"),
"page_id"=>$db->f("page_id"),
"label"=>$db->f("label"),
"name"=>$db->f("name"),
"template_id"=>$db->f("template_id"),
"page_title"=>$db->f("page_title"),
"page_description"=>$db->f("page_description"),
"page_keywords"=>$db->f("page_keywords")
);
}
return $page_details;
}
//--
//-------------------------------------------------
// get the topmost parent ID
function get_master_parent_id($db,$index_id){
$sql = "SELECT parent_id FROM site_index WHERE index_id='".$index_id."'";
$db->query($sql);
$master_id = "";
if ($db->next_record()){
if ($db->f("parent_id") == "0" || $db->f("parent_id") == "-1"){
$master_id = $index_id;
}else{
$master_id = get_master_parent_id($db,$db->f("parent_id"));
}
}
return $master_id;
}
//---
//-------------------------------------------------
// get the topmost parent ID
function get_parent_list($index_id){
$db = new DB;
$sql = "SELECT parent_id FROM site_index WHERE index_id='".$index_id."'";
$db->query($sql);
$master_id = "";
if ($db->next_record()){
if ($db->f("parent_id") == "0" || $db->f("parent_id") == "-1"){
$master_id .= $index_id.",";
}else{
$master_id .= $index_id.",".get_parent_list($db->f("parent_id"));
}
}
return $master_id;
}
//---
//-------------------------------------------------
// get the topmost parent ID
function get_child_array($children,$index_id,$template_id = 0){
$db = new DB;
if ($template_id != 0){
$sql = "SELECT index_id FROM site_index si ";
$sql .=" INNER JOIN pages p ON p.page_id = si.page_id AND p.template_id ='".$template_id."' ";
$sql .="WHERE si.parent_id='".$index_id."' ";
}else{
$sql = "SELECT index_id FROM site_index WHERE parent_id='".$index_id."' ";
}
$db->query($sql);
while ($db->next_record()){
$children[] = $db->f("index_id");
$children = get_child_array($children,$db->f("index_id"),$template_id);
}
return $children;
}
//---
//-------------------------------------------------
function get_perms($db){ // called from /index.php
global $page;
// assign all the permissions for this page
$sql = "SELECT perm_id FROM permissions";
$db->query($sql);
$perms = array();
$db2 = new DB;
if (isset($_SESSION['user_id'])){
$uid = $_SESSION['user_id'];
}else{
$uid = 0;
}
while($db->next_record()){
// if the user is a superadmin just set it all to true
$res = false;
//if ( has_user_perm($db2,$uid, "perm_superadmin") ){
// $res = true;
//}else{
$res = has_perm($db2,$db->f("perm_id"),$page['index_id'] );
//}
//echo "setting page['permissions']['".$db->f("perm_id")."'] = ".$res."
";
$perms[$db->f("perm_id")] = $res;
}
return $perms;
}
//---
//-------------------------------------------------
// Strips all tags and any other nasties from form input
function form_safe($input){
$input = strip_tags($input);
return ($input);
}
//---
//-------------------------------------------------
// Assigns a _REQUEST value to if it exists, otherwise returns an empty string.
function assign_if_set($request_name){
$res = "";
if (isset($_REQUEST[$request_name])){
//$res =$_REQUEST[$request_name];
$res = htmlspecialchars($_REQUEST[$request_name]);
}
return $res;
}
//---
//-------------------------------------------------
// Add a new value into the audit trail. This should
// be used whenever some action is made
function audit($db,$name,$value){
$user_id = "";
if (isset($_SESSION['member_user_id']) ) $user_id = $_SESSION['member_user_id'];
if (isset($_SESSION['user_id']) ) $user_id = $_SESSION['user_id'];
$sql = "INSERT INTO audit SET user_id='".$db->in($user_id)."',name='".$db->in($name)."',value='".$db->in($value)."',date=now()";
$db->query($sql);
}
//---
//-------------------------------------------------
//check for a base system
function check_for_base(&$db){
//global $plugins,$page;
global $page;
$plugins = array();
// If we have no plugin table then
// we need to install the base plugin
$found_plugin_table = false;
$tables = $db->table_names();
foreach($tables as $table){
if ($table['table_name'] == "plugins") $found_plugin_table = true;
}
// if we have a plugins table, check if there are any plugins in it
if ($found_plugin_table){
$sql = "SELECT plugin_id FROM plugins";
$db->query($sql);
$plugins_installed = false;
if ($db->num_rows() >0) $plugins_installed = true;
}
if ($found_plugin_table == false || $plugins_installed == false){
$plugin = "plugin_base";
$filename = BASE_PATH."/plugins/".$plugin."/".$plugin.".inc.php";
if(!file_exists($filename)){
error("Plugins table was not found so I tried to install the base system. However no plugin_base could be found.",true);
}
include_once $filename;
admin_message("Installing base system. Welcome to NYX.");
$temp_plugin = str_replace("plugin_","",$plugin);
if (method_exists($plugins[$temp_plugin],"install")) $plugins[$temp_plugin]->master_install($db);
// Login this user as a admin so we can install everything
$_SESSION['user_id'] = "1";
$_SESSION['perm_webadmin'] = true;
$_SESSION['perm_superadmin'] = true;
}
}
//-------------------------------------------------
//-------------------------------------------------
// Load all of the installed plugins
// into the global $plugins array
function load_plugins(&$db){
global $plugins;
//----------------------------
//now include the plugin list
$sql = "SELECT * FROM plugins ORDER BY importance";
$db->query($sql);
while($db->next_record()){
$temp_plugin = str_replace("plugin_","",$db->f("plugin_name"));
//include the plugin class file
$filename_local = DIR_ROOT."/plugins/plugin_".$db->f("plugin_name")."/plugin_".$db->f("plugin_name").".inc.php";
$filename_base = BASE_PATH."/plugins/plugin_".$db->f("plugin_name")."/plugin_".$db->f("plugin_name").".inc.php";
//df("plugin_path_base",$filename_base);
//df("plugin_path_local",$filename_local);
if (file_exists($filename_local)){
$installed_path = $filename_local;
}else if (file_exists($filename_base) ){
$installed_path = $filename_base;
}else{
error("Could not include plugin '".$db->f("plugin_name")."'. Include not found.");
return false;
}
include_once $installed_path;
}
}
//-------------------------------------------------
//-------------------------------------------------
//check for any new plugins in the user
//is logged in as an admin
function check_for_plugins(&$db,$base_path){
$plugins = array();
if (!is_dir($base_path."/plugins") ) return false;
if (!isset($_SESSION['perm_webadmin']) ) return false;
$d = dir($base_path."/plugins");
//----------------------------------------------------
//loop through all the folders in the plugins/ folder
while (false !== ($plugin = $d->read())) {
if( $plugin != "." && $plugin != ".." && is_dir($base_path."/plugins/".$plugin) && $plugin != "plugin_base"){
//first ensure the plugin file exists
if (!file_exists($base_path."/plugins/".$plugin."/".$plugin.".inc.php")) error("The plugin folder '".$base_path."/plugins/".$plugin."' was found but the plugin class extenstion file of '".$plugin.".inc.php' could not be found in it",true);
$temp_plugin = str_replace("plugin_","",$plugin);
install_master_plugin($db,$temp_plugin);
}
}
//---
$d->close();
}
//---
//-------------------------------------------------
//
//
function install_master_plugin($db,$plugin){
global $plugins;
$sql = "SELECT * FROM plugins WHERE plugin_name = '".$plugin."'";
$db->query($sql);
if ($db->num_rows() < 1){
//$plugins = array();
$filename_local = DIR_ROOT."/plugins/plugin_".$plugin."/plugin_".$plugin.".inc.php";
$filename_base = BASE_PATH."/plugins/plugin_".$plugin."/plugin_".$plugin.".inc.php";
if (file_exists($filename_local)){
$installed_path = $filename_local;
}else if (file_exists($filename_base) ){
$installed_path = $filename_base;
}else{
error("Could not include plugin '".$plugin."'. Include not found.");
return false;
}
include_once $installed_path;
if (method_exists($plugins[$plugin],"install")){
$plugins[$plugin]->master_install($db);
}else{
error("Master install method not found in (".$plugin.")");
}
}
return true;
}
//---
//-------------------------------------------------
// check for any new templates if the user
// is logged in as an admin
function check_for_templates(&$db){
if (isset($_SESSION['perm_webadmin'])){
//echo "checking templates folder:".DIR_ROOT."/templates/"."
";
check_template_folder($db,DIR_ROOT."/templates/");
// also check for theme specific templates
$themes = get_dir_list(DIR_ROOT."/templates/themes/");
foreach($themes as $theme){
$path = DIR_ROOT."/templates/themes/".$theme."/";
//echo "checking theme folder:".$path."
";
check_template_folder($db,$path,$theme);
}
}
}
//---
//-------------------------------------------------
function install_template($db,$path,$template,$theme="all"){
$template_source = get_file_contents($path.$template);
preg_match_all ("/
/", $template_source, $matches);
$template_single_instance_name = "";
if (isset($matches[0][0])){
$template_name = get_inner_string($matches[0][0],'name="','"');
$template_description = get_inner_string($matches[0][0],'description="','"');
$template_enabled = get_inner_string($matches[0][0],'enabled="','"');
$template_single_instance_name = get_inner_string($matches[0][0],'single_instance_name="','"');
if ($template_name == "") error("A new template (".$template.") was found but could not be installed because the 'template_name' tag is missing",true);
if ($template_description == "") error("A new template (".$template.") was found but could not be installed because the 'template_description' tag is missing",true);
if ($template_enabled == "") error("A new template (".$template.") was found but could not be installed because the 'template_enabled' tag is missing",true);
if ($template_enabled == "true"){
$template_enabled = "1";
}else{
$template_enabled = "0";
}
admin_message("The new template '".$template_name."' has been successfully installed.");
$sql = "INSERT INTO templates SET path='".$template."',name='".addslashes($template_name)."',description='".addslashes($template_description)."',enabled='".$template_enabled."',single_instance_name='".$template_single_instance_name."',theme='".$theme."'";
$db->query($sql);
audit($db,"template_installed",$template);
return true;
}else{
error("A new template (".$template.") was found but could not be installed because the 'template' tag is missing",true);
return false;
}
}
//-------------------------------------------------
//
function check_template_folder($db,$path,$theme="all"){
$d = dir($path);
//----------------------------------------------------
//loop through all the folders in the plugins/ folder
while (false !== ($template = $d->read())) {
// dont show include files
if (strpos($template,".inc") === false ) {
$include_file = false;
}else{
$include_file = true;
}
if($include_file == false && $template != "." && $template != ".." && !is_dir(DIR_ROOT."/templates/".$template) && is_int(strpos($template,".tpl.html")) ){
$sql = "SELECT * FROM templates WHERE path = '".$template."'";
//echo $sql."
";
$db->query($sql);
if ($db->num_rows() < 1){
$db->next_record();
//extract the new templates details from the template itself
install_template($db,$path,$template,$theme);
}
}
}
//---
$d->close();
}
//---
//-------------------------------------------------
//gets the contents of a local file (eg template)
function get_file_contents($path){
$filename = $path;
if (file_exists($filename)){
$handle = fopen ($filename, "r");
$contents = fread ($handle, filesize ($filename));
fclose ($handle);
}else{
$contents = "";
debug("error, file doesn't exist:".$filename."
");
}
return $contents;
}
//---
//-------------------------------------------------
function simple_email($emailto,$emailfrom,$subject,$body){
require_once BASE_PATH.'/includes/class.html.mime.mail.inc.php';
$mail = new htmlMimeMail;
$options[] = 'X-Mailer: Html Mime Mail Class';
foreach ($options as $option) {
preg_match('/^([^:]*):(.*)$/', $option, $match);
if (count($match) == 3)
$mail->setHeader(trim($match[1]), trim($match[2]));
}
$mail->setHeader('Reply-To',$emailfrom);
if (isset( $_SERVER['SERVER_NAME'])){
$serverFrom = "noreply@".$_SERVER['SERVER_NAME'];
$serverFrom = str_replace("www.","",$serverFrom);
}else{
$serverFrom = "noreply@wizardinacan.co.nz";
//$serverFrom = $emailfrom;
}
$mail->setReturnPath($serverFrom);
$mail->setFrom(str_replace(array("\r", "\n"), '', '"'.$serverFrom.'" <'.$serverFrom.'>'));
//$mail->setFrom(str_replace(array("\r", "\n"), '', '"'.$emailfrom.'" <'.$emailfrom.'>'));
$mail->setSubject($subject);
$mail->setText($body);
if (is_array($emailto)){
$mail->send($emailto);
}else{
$mail->send(array(str_replace(array("\r", "\n"), '', '"'.$emailto.'" <'.$emailto.'>')));
}
}
//---
//-------------------------------------------------
function html_email($emailto,$emailfrom,$subject,$body_html,$body_text){
require_once BASE_PATH.'/includes/class.html.mime.mail.inc.php';
$mail = new htmlMimeMail;
$options[] = 'X-Mailer: Html Mime Mail Class';
foreach ($options as $option) {
preg_match('/^([^:]*):(.*)$/', $option, $match);
if (count($match) == 3)
$mail->setHeader(trim($match[1]), trim($match[2]));
}
$mail->setTextCharset('utf-8');
$mail->setHtmlCharset('utf-8');
$mail->setHeadCharset('utf-8');
$mail->setHeader('Reply-To',$emailfrom);
if (isset( $_SERVER['SERVER_NAME'])){
$serverFrom = "noreply@".$_SERVER['SERVER_NAME'];
$serverFrom = str_replace("www.","",$serverFrom);
}else{
$serverFrom = "noreply@wizardinacan.co.nz";
//$serverFrom = $emailfrom;
}
$mail->setReturnPath($serverFrom);
$mail->setFrom(str_replace(array("\r", "\n"), '', '"'.$serverFrom.'" <'.$serverFrom.'>'));
//$mail->setFrom(str_replace(array("\r", "\n"), '', '"'.$emailfrom.'" <'.$emailfrom.'>'));
$mail->setSubject($subject);
//$mail->setText($body_text);
$mail->setHtml($body_html,$body_text);
//$mail->send(array(str_replace(array("\r", "\n"), '', '"'.$emailto.'" <'.$emailto.'>')));
if (is_array($emailto)){
$mail->send($emailto);
}else{
$mail->send(array(str_replace(array("\r", "\n"), '', '"'.$emailto.'" <'.$emailto.'>')));
}
}
//---
function create_text_email($html_content){
$text_content = $html_content;
// remove everything between the style tags
$startPos = strpos($text_content,"");
$secondHalf = substr($text_content,$endPos,strlen($text_content));
$text_content = $firstHalf.$secondHalf;
// replace the 's
$text_content = str_replace(" "," ",$text_content);
// parse out the links so they can be read in the text-only version
$regexp = "
]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $html_content, $matches)) {
# $matches[2] = array of link addresses
# $matches[3] = array of link text - including HTML code
//debugr($matches);
for($i = 0 ;$i < count($matches[2]);$i++ ){
$tag_to_replace = $matches[0][$i];
$label = $matches[3][$i];
$link = $matches[2][$i];
//echo "label:".$matches[3][$i]." link:".$matches[2][$i]."
";
$text_content= str_replace($tag_to_replace,$label."\n[".$link."]",$text_content);
}
}
//$style = get_inner_string($text_content,"");
//$style = get_tag_data("style",$text_content,true);
//$text_content = str_replace($style,"",$text_content);
$text_content = strip_tags($text_content);
$text_content = trim($text_content);
return $text_content;
}
//-------------------------------------------------
// Get a list of countries
function get_countries(&$db){
$sql = "SELECT * FROM countries ORDER BY name";
$db->query($sql);
$countries = array();
while($db->next_record()){
$countries[] = array(
"country_id"=>$db->f("country_id"),
"name"=>$db->f("name")
);
}
return $countries;
}
//---
//--------------------------------------------------
// set an option
function set_option($db,$plugin,$code,$value){
$sql ="SELECT * FROM options WHERE plugin='".$plugin."' AND code='".$code."'";
$db->query($sql);
if($db->num_rows() > 0){
$sql ="UPDATE options SET value='".$value."' WHERE plugin='".$plugin."' AND code='".$code."'";
}else{
$sql ="INSERT INTO options SET plugin='".$plugin."' ,code='".$code."',value='".$value."'";
}
audit($db,"option_set","".$plugin."".$code."".$value."");
$db->query($sql);
}
//---
//--------------------------------------------------
// get an option
function get_option($db,$plugin,$code){
$ret = "";
$sql ="SELECT * FROM options WHERE plugin='".$db->in($plugin)."' AND code='".$db->in($code)."'";
$db->query($sql);
while($db->next_record()){
if ($db->f("value") == "")
$ret = $db->f("default");
else
$ret =$db->f("value");
}
return $ret;
}
//---
//--------------------------------------------------
// gets an array of options based on a plugin
function get_options($db,$plugin){
$ret = "";
$sql ="SELECT * FROM options WHERE plugin='".$db->in($plugin)."'";
$db->query($sql);
$res = array();
while($db->next_record()){
if ($db->f("value") == "")
$value = $db->f("default");
else
$value =$db->f("value");
$res[$db->f("code")] = $value;
}
return $res;
}
//---
//--------------------------------------------------
// returns the option from the options array
// checking if the option exists (used in /css/default.css.php)
function show_option($options,$key){
if (isset($options[$key])){
return $options[$key];
}else{
return "";
}
}
//--------------------------------------------------
// get content between two text items
function get_inner_string($string,$startTag,$endTag){
//debug_textarea($string);
$pattern = '#'.$startTag.'(.*?)'.$endTag.'#is';
preg_match_all($pattern, $string, $matches);
//debug_textarea(print_r($matches,true));
if (isset($matches[1][0]) && $matches[1][0] != ""){
$res = $matches[1][0];
return trim($res);
}else{
return "";
}
}
//---
//--------------------------------------------------
// get tag data
function get_tag_data($tagname, $string,$include_tags = false) {
$pattern = '#<'.$tagname.'[^>]*>(.*?)'.$tagname.'>#is';
preg_match_all($pattern, $string, $matches); //Multi-dimensional array
if ($include_tags == true){
if (isset($matches[0][0]) && $matches[0][0] != ""){
return $matches[0][0];
}else{
return "";
}
}else{
if (isset($matches[0][0]) && $matches[0][0] != ""){
$res = $matches[0][0];
$res = str_replace("<".$tagname.">","",$res);
$res = str_replace("".$tagname.">","",$res);
return trim($res);
}else{
return "";
}
}
}
//-----------------------------------------
// check if the user is assigned to a
// specific group. Defaults to the currently
// logged-in user
function is_group_member($db,$group_id,$user_id = -1){
if ($user_id == -1){
/*
if (isset($_SESSION['member_user_id'])){
$user_id = $_SESSION['member_user_id'];
}else if (isset($_SESSION['user_id'])){
$user_id = $_SESSION['user_id'];
}else{
$user_id = 0;
}
*/
$user_id = get_user_id();
}
$sql = "SELECT * FROM user_groups WHERE user_id='".$db->in($user_id)."' AND group_id='".$db->in($group_id)."'";
$db->query($sql);
if ($db->num_rows() > 0 ){
return true;
}else{
return false;
}
}
//---
//-----------------------------
// gets the most relevant user id
function get_user_id(){
if (isset($_SESSION['member_user_id'])){
$user_id = $_SESSION['member_user_id'];
}else if (isset($_SESSION['user_id'])){
$user_id = $_SESSION['user_id'];
}else{
$user_id = "0";
}
return $user_id;
}
//---
//-----------------------------
// check if the current user has a permmission
// against a page eg has_perm($db,"perm_view","1");
function has_perm($db,$perm_type,$index_id="0"){
// superadmins have all permissions
if ($perm_type != "perm_superadmin" && has_perm($db,"perm_superadmin",$index_id) ) return true;
if ($perm_type =="perm_view"){
// first check if the page is a member of public
// if so just return true
$sql = "SELECT * FROM page_groups WHERE index_id='".$db->in($index_id)."' AND group_id = 'public'";
$db->query($sql);
if ($db->num_rows() >0 ){
return(true);
}
}
$user_id = get_user_id();
/*
if (isset($_SESSION['member_user_id'])){
$user_id = $_SESSION['member_user_id'];
}else if (isset($_SESSION['user_id'])){
$user_id = $_SESSION['user_id'];
}else{
$user_id = "0";
}
*/
//----
// perm_webadmin and perm_superadmin is assigned regardless of pages
// so first check for them
//if ($perm_type == "perm_webadmin" || $perm_type == "perm_superadmin"){
if ($perm_type == "perm_superadmin" || $perm_type == "perm_debug" ){
$sql ="SELECT ug.group_id FROM user_groups ug ";
$sql .="INNER JOIN group_permissions perm_groups ON perm_groups.group_id = ug.group_id ";
$sql .="INNER JOIN permissions AS perms ON perms.perm_id = perm_groups.perm_id ";
$sql .="WHERE ug.user_id ='".$db->in($user_id)."' AND perms.perm_id = '".$db->in($perm_type)."' ";
$db->query($sql);
if ($db->num_rows() >0){
return(true);
}else{
return(false);
}
}
//---
// All other perms are based on what page is requested
$sql ="SELECT pg.index_id,perms.* FROM page_groups AS pg ";
$sql .="INNER JOIN user_groups ug ON ug.group_id = pg.group_id ";
$sql .="INNER JOIN group_permissions perm_groups ON perm_groups.group_id = pg.group_id ";
$sql .="INNER JOIN permissions AS perms ON perms.perm_id = perm_groups.perm_id ";
$sql .="WHERE pg.index_id ='".$db->in($index_id)."' AND ug.user_id ='".$db->in($user_id)."' AND perms.perm_id = '".$db->in($perm_type)."' ";
//echo $sql."
";
//if ($index_id == "1") echo $sql."
";
$db->query($sql);
if ($db->num_rows() >0){
return(true);
}else{
return(false);
}
}
//---
//-----------------------------
// check if a specific user has a permission
function has_user_perm($db,$user_id, $perm_type){
$sql ="SELECT perms.* FROM user_groups ug ";
$sql .="INNER JOIN group_permissions perm_groups ON perm_groups.group_id = ug.group_id ";
$sql .="INNER JOIN permissions AS perms ON perms.perm_id = perm_groups.perm_id ";
$sql .="WHERE ug.user_id ='".$db->in($user_id)."' AND perms.perm_id = '".$db->in($perm_type)."' ";
$db->query($sql);
if ($db->num_rows() >0){
return(true);
}else{
return(false);
}
}
//---
//-----------------------------
// gets a list of files from a dir
// (excludes dirs)
function get_file_list($dir){
$d = dir($dir);
$files = array();
while (false !== ($file = $d->read())) {
if( $file != "." && $file != ".." && !is_dir($dir."/".$file)){
$files[] = $file;
}
}
return $files;
}
//---
//-----------------------------
// Gets the amount of users
// which are web_admins (dont
// count the nyx admin)
function get_used_admins($db){
$sql ="SELECT u.user_id FROM users u JOIN user_groups ug ON ug.user_id = u.user_id AND ug.group_id = 'web_admin' AND u.email_address !='info@nyx.co.nz'";
$db->query($sql);
return $db->num_rows();
}
//---
//-----------------------------
// Check if the max admins
// has been exceeded
function exceeded_max_admins($db){
$max_admins = get_option($db,"readonly_settings","max_admins");
$used_admins = get_used_admins($db);
if ($used_admins >= $max_admins ){
return true;
}else{
return false;
}
}
//---
//-----------------------------
// Gets the amount of created pages
// (Pages created which use enabled templates)
function get_used_pages($db){
$sql = "SELECT si.index_id FROM site_index si ";
$sql .=" LEFT JOIN pages p ON si.page_id = p.page_id ";
$sql .=" LEFT JOIN templates t ON p.template_id = t.template_id WHERE t.enabled ='1'";
$db->query($sql);
return $db->num_rows();
}
//---
//-----------------------------
// Check if the max created pages
// has been exceeded
function exceeded_max_pages($db){
$max_pages = get_option($db,"readonly_settings","max_pages");
$used_pages = get_used_pages($db);
if ($used_pages >= $max_pages ){
return true;
}else{
return false;
}
}
//---
//-----------------------------
// get the used space for this account
function get_used_space(){
$used_space = 0;
if (is_dir(DIR_ROOT.FILES_PATH)) $used_space += get_folder_size(DIR_ROOT.FILES_PATH);
if (is_dir(DIR_ROOT.UPLOAD_PATH)) $used_space += get_folder_size(DIR_ROOT.UPLOAD_PATH);
if ($used_space != 0) $used_space = sprintf("%01.2f", $used_space / 1024);
return($used_space);
}
//-----------------------------
// Check if the maximum space
// has been exceeded
function exceeded_max_space($db){
$max_space = get_option($db,"readonly_settings","max_space");
$used_space = get_used_space();
if ($used_space > $max_space){
return true;
}else{
return false;
}
}
//---
//-----------------------------
// get the (recursive) size of a folder
function get_folder_size($d ="." ) {
if(!isset($sf)){$sf = 0;}
$h = @opendir($d);
if($h==0)return 0;
while ($f=readdir($h)){
if ( $f!= "..") {
$sf+=filesize($nd=$d."/".$f);
if($f!="."&&is_dir($nd)){
$sf+=get_folder_size ($nd);
}
}
}
closedir($h);
return $sf ;
}
//---
//-----------------------------
// gets a list of dirs from a dir
// (excls files)
function get_dir_list($source){
// make sure we're trying to get a dir list
// from a path which contains the DOC_ROOT..
// just incase
if (strpos($source,DIR_ROOT)===false && strpos($source,BASE_PATH) === false ){
return array();
}
$d = dir($source);
$dirs = array();
if ($d){
while (false !== ($dir = $d->read())) {
if( $dir != "." && $dir != ".." && is_dir($source."/".$dir)){
$dirs[] = $dir;
}
}
}
return $dirs;
}
//---
//-----------------------------
// Delete a directory
function delete_dir($dir){
// ensure the DIR_ROOT in in the path
if (strpos($dir,DIR_ROOT) === true){
$current_dir = opendir($dir);
while($entryname = readdir($current_dir)){
if(is_dir("$dir/$entryname") and ($entryname != "." and $entryname!="..")){
delete_dir("${dir}/${entryname}");
}elseif($entryname != "." and $entryname!=".."){
@unlink("${dir}/${entryname}");
}
}
closedir($current_dir);
@rmdir(${dir});
audit($db,"deleted_folder",$dir);
}else{
// somebodies is trying to delete something outside the
// DIR_ROOT which is likely dodgy
audit($db,"warning_delete_folder_attempt",$path);
}
}
//---
//-----------------------------
// delete a file with audit
function delete_file(&$db,$path){
// ensure the DIR_ROOT in in the path
if (strpos("x".$path,$_SERVER['DOCUMENT_ROOT']) > 0){
audit($db,"deleted_file",$path);
unlink($path);
}else{
// somebodies is trying to delete something outside the
// DIR_ROOT which is likely dodgy
audit($db,"warning_delete_file_attempt",$path);
}
}
//---
//-------------------------------------------------
function randomPassword($len = 8){
//-------------------------------------------------
$pass = '';
$lchar = 0;
$char = 0;
for($i = 0; $i < $len; $i++){
while($char == $lchar){
$char = rand(48, 109);
if($char > 57) $char += 7;
if($char > 90) $char += 6;
}
$pass .= chr($char);
$lchar = $char;
}
return $pass;
}
//---
//-------------------------------------------------
function get_plugin_parameters($tag){
//-------------------------------------------------
// TODO
// Need to change this so that it allows for
// spaces withen the quotes
//$parameters = split(" ",$tag); - 23/08/2010
//$parameters = split(' ',$tag);
$parameters = explode('" ',$tag);
$params = array();
foreach ($parameters as $param){
//echo "param:".$param."
";
//$keyvalues = split("=",$param);
$keyvalues = explode("=",$param);
if (count($keyvalues) < 2){
//echo "error in tag (".count($keyvalues)."):-
";
//debug_textarea($tag);
//echo "
";
}
if (count($keyvalues) > 1){
$keyvalues[0] = str_replace(" ","",$keyvalues[0]);
$keyvalues[1] = str_replace('"','',$keyvalues[1]);
$keyvalues[1] = str_replace("'","",$keyvalues[1]);
//$keyvalues[1] = str_replace(" ","",$keyvalues[1]); -- 23/08/2010
$params[$keyvalues[0]] = $keyvalues[1];
}
}
return $params;
}
//-------------------------------------------------
//---------------------------------------------
// Create a unique page name
//---------------------------------------------
function get_page_name($db,$title,$index_id=""){
// replace spaces with hyphens so they are
// more easily read by search engines
$title = trim($title);
$title = str_replace(" ","-",$title);
$base = preg_replace('/[^a-z0-9-]/', '', strtolower($title));
// If it's a new page strip out the double/triple dashes
//if ($index_id ==""){
$base = str_replace("----","-",$base);
$base = str_replace("---","-",$base);
$base = str_replace("--","-",$base);
//}
if (!preg_match('/[a-z]/', $base)) {
// name either blank or contains only number, so add prefix
$base = 'x'.$base;
}
$candidate = $base;
$index = 1;
$unique_name = '';
do {
$sql = "SELECT * ";
$sql .= "FROM site_index ";
$sql .= "WHERE label='".$candidate."' ";
$sql .= "AND index_id != '".$index_id."' ";
$db->query($sql);
$unique_name = $candidate;
$candidate = $base.$index++;
}
while ($db->num_rows() > 0);
return $unique_name;
}
//---------------------------------------------
//-------------------------------------------------
function add_page($db,$page_name,$template_id,$parent_id,$groups = array() ,$order_id=""){
// insert a page with template_id and groups
//-------------------------------------------------
$new_page_index_id =-1;
//return $new_page_index_id;
$label = get_page_name($db,$page_name);
$sql = "INSERT INTO pages SET name='".$page_name."',template_id='".$template_id."'";
//echo $sql."
";
$db->query($sql);
$page_id = $db->insert_id();
if ($order_id ==""){
// calculate the next order_id
$sql = "SELECT order_id FROM site_index WHERE parent_id='".$parent_id."' ORDER BY order_id DESC LIMIT 1";
//echo "(".$page_name.")".$sql."
";
$db->query($sql);
if ($db->next_record()){
$order_id = $db->f("order_id");
$order_id++;
}else{
$order_id="1";
}
}
// insert the page index
$sql = "INSERT INTO site_index SET page_id='".$page_id."',parent_id='".$parent_id."',label='".$label."',order_id='".$order_id."',show_in_menu='1'";
//echo $sql."
";
$db->query($sql);
$new_page_index_id = $db->insert_id();
// add the page to the control panel group
foreach($groups as $group){
$sql = "REPLACE INTO `page_groups` VALUES (".$new_page_index_id.", '".$group."');";
//echo $sql."
";
$db->query($sql);
}
// add the audit
$page_details = get_page_details($db,$new_page_index_id);
audit($db,"page_added","".$page_details['index_id']."".$page_details['template_id']."".$page_details['name']."");
return $new_page_index_id;
}
//-------------------------------------------------
//-------------------------------------------------
function get_and_create_page($db,$plugin_name,$target_page_label,$target_template,$target_page_name,$parent_id,$groups = array(),$order_id ="" ) {
//
//-------------------------------------------------
$sql = "SELECT index_id FROM site_index WHERE label='".$target_page_label."'";
$db->query($sql);
$template = str_replace(".tpl.html","",$target_template);
if ($db->num_rows() >0 ){
$db->next_record();
$page_index_id = $db->f("index_id");
}else{
// get the required template_id
$sql = "SELECT template_id FROM templates WHERE path='".$target_template."'";
//---------------
// template not installed, try and grab it from the base
$db->query($sql);
if($db->num_rows() < 1){
copy_template_from_base($template);
install_template($db,DIR_ROOT."/templates/",$target_template);
}
//---------------
$sql = "SELECT template_id FROM templates WHERE path='".$target_template."'";
$db->query($sql);
if($db->next_record()){
$template_id = $db->f("template_id");
$page_index_id = add_page($db,$target_page_name,$template_id,$parent_id,$groups,$order_id);
}else{
// we don't seem to have any templates yet so lets hold
// off on installing this plugin
admin_message("The '".$plugin_name."' plugin could not be installed because a required template '".$target_template."' could not be found");
return false;
}
}
return $page_index_id;
}
//---
//--------------------------------
function is_even($val){
return ($val % 2 == 0);
}
//---
//--------------------------------
function copy_template_from_base($template){
//echo "INSTALL TEMPLATE:".$template."
";
$source_template = BASE_PATH."/templates/".$template.".tpl.html";
if (!file_exists($source_template)){
error("The ".$template." source template could not be found.");
return false;
}
$target = $_SERVER['DOCUMENT_ROOT']."/templates/".$template.".tpl.html";
if(!copy($source_template,$target)){
error("There was an error while trying to transfer the ".$template." template");
return false;
}
return true;
}
//---
//--------------------------------
function remove_template($db,$template){
if (!@unlink($_SERVER['DOCUMENT_ROOT']."/templates/".$template.".tpl.html")){
error("There was an error while trying to delete the ".$template." template.");
return false;
}
$sql = "SELECT template_id FROM templates WHERE path='".$template.".tpl.html'";
$db->query($sql);
if ($db->num_rows() > 0){
$db->next_record();
$sql = "DELETE FROM templates WHERE template_id='".$db->f("template_id")."'";
$db->query($sql);
}
return true;
}
//---
//--------------------------------
function date_sql_to_timestamp($sqldate){
//sql date format is YYYY-MM-DD HH:MM:SS
if (strlen($sqldate) != 19 ) return "";
$year = substr($sqldate,0,4);
$month = substr($sqldate,5,2);
$day = substr($sqldate,8,2);
$hour = substr($sqldate,11,2);
$min = substr($sqldate,14,2);
$sec = substr($sqldate,17,2);
$timestamp = mktime($hour,$min, $sec, $month, $day, $year);
return $timestamp;
}
//---
//--------------------------------
// replace profanities with required replacement
// used in forums etc
function replace_profanity($searchContent,$replaceWith){
$searchContent = str_replace(" cunt ",$replaceWith,$searchContent);
$searchContent = str_replace(" fuck ",$replaceWith,$searchContent);
$searchContent = str_replace(" shit ",$replaceWith,$searchContent);
$searchContent = str_replace(" asshole ",$replaceWith,$searchContent);
$searchContent = str_replace(" wanker ",$replaceWith,$searchContent);
}
//--
//--------------------------------
// check if an email address is valid
function valid_email($email) {
if ($email == "") return false;
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}
//---
//-----------------------------------------------
function money($inp){
return number_format($inp, 2, '.', ',');
}
//---
//-----------------------------------------------
function get_file_extension($file_name) {
return substr(strrchr($file_name,'.'),1);
}
//---
?>