<?php
/*
* author: pavemen
* vesion: 1.1
*/
define("IN_MYBB", 1);
define('THIS_SCRIPT', 'plugin_hook_update.php');
require_once "./global.php";
/*
set $dryrun to 1 to output list of files names being searched, list of matches and found searches and replaces but do NOT modify files
set $dryrun to 0 to just do it
*/
$dryrun = 1;
$plugin_folder = MYBB_ROOT.'inc/plugins'; //NO TRAILING SLASH!!!!!
$hooks_for_return = array();
$hooks_for_return[] = 'admin_config_menu';
$hooks_for_return[] = 'admin_config_action_handler';
$hooks_for_return[] = 'admin_config_permissions';
$hooks_for_return[] = 'admin_home_menu';
$hooks_for_return[] = 'admin_home_action_handler';
$hooks_for_return[] = 'admin_home_menu_quick_access';
$hooks_for_return[] = 'admin_user_menu';
$hooks_for_return[] = 'admin_user_action_handler';
$hooks_for_return[] = 'admin_user_permissions';
$hooks_for_return[] = 'admin_user_groups_edit_graph_tabs';
$hooks_for_return[] = 'admin_forum_management_permission_groups';
$hooks_for_return[] = 'admin_forum_menu';
$hooks_for_return[] = 'admin_forum_action_handler';
$hooks_for_return[] = 'admin_forum_permissions';
$hooks_for_return[] = 'admin_tools_menu';
$hooks_for_return[] = 'admin_tools_action_handler';
$hooks_for_return[] = 'admin_tools_menu_logs';
$hooks_for_return[] = 'admin_tools_get_admin_log_action';
$hooks_for_return[] = 'admin_tools_permissions';
$hooks_for_return[] = 'admin_style_menu';
$hooks_for_return[] = 'admin_style_action_handler';
$hooks_for_return[] = 'admin_style_permissions';
$hooks_for_return[] = 'admin_page_output_nav_tabs_start';
$hooks_for_return[] = 'admin_page_output_tab_control_start';
$hooks_for_return[] = 'admin_page_output_nav_tabs_start';
$hooks_for_return[] = 'admin_form_output_submit_wrapper';
$hooks_for_return[] = 'admin_formcontainer_output_row';
$hooks_for_return[] = 'admin_tabs';
$hooks_for_return[] = 'upload_avatar_end';
$hooks_for_return[] = 'upload_attachment_do_insert';
$hooks_for_return[] = 'upload_file_end';
$hooks_for_return[] = 'my_date';
$hooks_for_return[] = 'error';
$hooks_for_return[] = 'redirect';
$hooks_for_return[] = 'mycode_add_codebuttons';
$hooks_for_return[] = 'functions_fetch_ban_times';
$hooks_for_return[] = 'fetch_wol_activity_end';
$hooks_for_return[] = 'build_friendly_wol_location_end';
$hooks_for_return[] = 'parse_quoted_message';
$hooks_for_return[] = 'postbit_prev';
$hooks_for_return[] = 'postbit_pm';
$hooks_for_return[] = 'postbit_announcement';
$hooks_for_return[] = 'postbit';
$hooks_for_return[] = 'build_forumbits_forum';
$hooks_for_return[] = 'class_moderation_delete_post_start';
$hooks_for_ref = array();
$hooks_for_ref[] = 'admin_form_end';
$hooks_for_ref[] = 'datahandler_event_validate';
$hooks_for_ref[] = 'datahandler_event_insert';
$hooks_for_ref[] = 'datahandler_event_update';
$hooks_for_ref[] = 'datahandler_user_validate';
$hooks_for_ref[] = 'datahandler_user_insert';
$hooks_for_ref[] = 'datahandler_user_update';
$hooks_for_ref[] = 'datahandler_pm_validate';
$hooks_for_ref[] = 'datahandler_pm_insert_updatedraft';
$hooks_for_ref[] = 'datahandler_pm_insert';
$hooks_for_ref[] = 'datahandler_pm_insert_savedcopy';
$hooks_for_ref[] = 'datahandler_post_validate_post';
$hooks_for_ref[] = 'datahandler_post_insert_post';
$hooks_for_ref[] = 'datahandler_post_validate_thread';
$hooks_for_ref[] = 'datahandler_post_insert_thread_post';
$hooks_for_ref[] = 'datahandler_post_insert_thread';
$hooks_for_ref[] = 'datahandler_post_update_thread';
$hooks_for_ref[] = 'datahandler_post_update';
$edits = array();
$hook_function = array();
echo "Starting search for hooks requiring verification";
echo '<br />';
check_hooks($plugin_folder, $hook_function);
ksort($hook_function);
if($dryrun)
{
echo 'Possible functions found<br />';
echo '<pre>';
print_r($hook_function);
echo '</pre>';
echo "<br />";
}
echo "Verifying hooked functions that need updating";
echo '<br />';
check_functions($hook_function, $edits);
ksort($edits);
if($dryrun)
{
echo 'Found functions that need to be updated<br />';
echo '<pre>';
print_r($edits);
echo '</pre>';
echo "<br />";
}
echo "<br />";
echo "Done searching for needed updates";
echo "<br />";
echo "Starting updates";
echo "<br />";
fix_files($edits);
echo "<br />";
echo "Done fixing";
echo "<br />";
function check_hooks($dir, &$hook_function)
{
global $hooks_for_return, $hooks_for_ref;
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$ext = pathinfo($file, PATHINFO_EXTENSION);
//if we have a php file
if($ext == 'php')
{
if($dryrun)
{
echo "Searching <strong>".$dir.'/'.$file."</strong><br />";
}
$filehandle = @fopen($dir.'/'.$file, "r");
if ($filehandle)
{
while (($buffer = fgets($filehandle)) !== false)
{
foreach($hooks_for_return as $hook)
{
if(stristr($buffer, "add_hook") && stristr($buffer, $hook))
{
$split = preg_split("/[\s]*[,][\s]*/i", $buffer);
$split = array_merge(array($split[0]), explode(";", $split[1]));
echo "<pre>";
print_r($split);
echo "</pre>";
if(count($split) > 0)
{
$cleanhook = trim(trim(substr($split[0], 20, strlen($split[0])-21), "'"), '"');
$function = trim(trim(substr($split[1], 1, strlen($split[1])-3), "'"), '"');
$hook_function[$dir][$file][$function] = 'return';
}
}
}
foreach($hooks_for_ref as $hook)
{
if(stristr($buffer, "add_hook") && (stristr($buffer, '"'.$hook.'"') || stristr($buffer, "'".$hook."'")))
{
$split = preg_split("/[\s]*[,][\s]*/i", $buffer);
if(count($split) > 0)
{
$cleanhook = trim(trim(substr($split[0], 20, strlen($split[0])-21), "'"), '"');
$function = trim(trim(substr($split[1], 1, strlen($split[1])-5), "'"), '"');
$hook_function[$dir][$file][$function] = 'ref';
}
}
}
}
fclose($filehandle);
}
}
else
{
//recurse into directories
if(is_dir($dir.'/'.$file))
{
check_hooks($dir.'/'.$file, $hook_function);
}
}
}
}
closedir($handle);
}
}
function check_functions($hook_function, &$edits)
{
global $dryrun;
if(count($hook_function) > 0)
{
foreach($hook_function as $dir=>$files)
{
foreach($files as $filename=>$functions)
{
$searches = array();
$replaces = array();
foreach($functions as $function_name=>$type)
{
$searches[] = "/function ".$function_name."[ ]*\([ ]*\\\$/i";
$replaces[] = "function ".$function_name."(&$";
}
$edits[$dir.'/'.$filename] = array($searches, $replaces);
}
}
}
else
{
echo "Nothing to search";
}
}
function fix_files($edits)
{
global $dryrun;
if(count($edits) > 0)
{
foreach($edits as $file=>$updates)
{
if($dryrun)
{
echo "Looking in ".$file.'<br />';
echo '<pre>';
print_r($updates);
echo '</pre>';
}
$orig_text = file_get_contents($file);
$modified = false;
//foreach($updates as $update)
{
echo $search."<br />";
$text = preg_replace($updates[0], $updates[1], $orig_text, -1, $count);
if($orig_text != $text)
{
if($dryrun) echo "Updating ".$count." lines in ".$file."<br /><br />";
$modified = true;
}
}
if(!$dryrun && $modified)
{
if(!file_put_contents($file, $text))
{
echo "Unable to update ".$file."<br />";
}
else
{
echo "Updated ".$file."<br />";
}
}
}
}
else
{
echo "Nothing to fix";
}
}
?>