Facebook
From Ronald Ngoda, 1 Year ago, written in PHP.
Embed
Download Paste or View Raw
Hits: 121
  1. <?php // Save this file as 'hooks/replace-appgini-functions.php'
  2.  
  3.         $hooks_dir = dirname(__FILE__);
  4.         include("{$hooks_dir}/../defaultLang.php");
  5.         include("{$hooks_dir}/../language.php");
  6.         include("{$hooks_dir}/../lib.php");
  7.        
  8.         // Step 1: Specify the file containing the function we want to overwrite
  9.         $appgini_file = "{$hooks_dir}/../incCommon.php";
  10.  
  11.         // Step 2: Specify the file containing our version of the function
  12.         $mod_file = "{$hooks_dir}/mod.showNotifications.php";
  13.  
  14.         // Step 3: Specify the name of the function we want to overwrite
  15.         $func_name = 'showNotifications';
  16.  
  17.         echo "<br>{$func_name}: " . replace_function($appgini_file, $func_name, $mod_file);
  18.  
  19.         #######################################
  20.  
  21.         function replace_function($appgini_file, $function_name, $mod_file) {
  22.                 // read the new code from the mod file
  23.                 $new_code = @file($mod_file);
  24.                 if(empty($new_code)) return 'No custom code found.';
  25.  
  26.                 // remove the first line containing PHP opening tag and keep the rest as $new_snippet
  27.                 array_shift($new_code);
  28.                 $new_snippet = implode('', $new_code);
  29.  
  30.                 $pattern1 = '/\s*function\s+' . $function_name . '\s*\(.*\).*(\R.*){200}/';
  31.                 $pattern2 = '/\t#+(.*\R)*/';
  32.  
  33.                 $entire_code = file_get_contents($appgini_file);
  34.                 if(!$entire_code) return 'Invalid AppGini file.';
  35.  
  36.                 $m = [];
  37.                 if(!preg_match_all($pattern1, $entire_code, $m)) return 'Function to replace not found.';
  38.                 $snippet = $m[0][0] . "\n";
  39.  
  40.                 if(!preg_match_all($pattern2, $snippet, $m)) return 'Could not find the end of the function.';
  41.                 $snippet = str_replace($m[0][0], '', $snippet);
  42.  
  43.                 $snippet_nocrlf = str_replace("\r\n", "\n", $snippet);
  44.                 $new_snippet_nocrlf = str_replace("\r\n", "\n", $new_snippet);
  45.                 if(trim($snippet_nocrlf) == trim($new_snippet_nocrlf)) return 'Function already replaced.';
  46.  
  47.                 // back up the file before overwriting
  48.                 if(!@copy(
  49.                         $appgini_file,
  50.                         preg_replace('/\.php$/', '.backup.' . date('Y.m.d.H.i.s') . '.php', $appgini_file)
  51.                 )) return 'Could not make a backup copy of file.';
  52.  
  53.                 $new_code = str_replace(trim($snippet), trim($new_snippet), $entire_code);
  54.                 if(!@file_put_contents($appgini_file, $new_code)) return "Couldn't overwrite file.";
  55.  
  56.                 return 'Function overwritten successfully.';
  57.         }