Skip to content

Instantly share code, notes, and snippets.

@EntranceJew
Last active September 23, 2020 07:10
Show Gist options
  • Save EntranceJew/653146fcdd0325a2715a477ed0757fff to your computer and use it in GitHub Desktop.
Save EntranceJew/653146fcdd0325a2715a477ed0757fff to your computer and use it in GitHub Desktop.
for when you're really screwed

ACME PHP Upgrade Dummies

for when you have old, bad php code that you need to move forward in versions / migrate

<?php
// the really old obscure one that only works on php5.6 on Windows and not on linux anymore
class Memcache {
private $keys = [];
public function addServer($server, $port){
// that's nice dear
}
public function set($key, $value, $some_flag_i_dont_care_about, $expire){
$this->keys[$key] = $value;
}
public function get($key){
return $this->keys[$key] ?? null;
}
}
<?php
// replace old code using 'call_user_func_array' using 'bind_param' on an instance
// y'know, the kind that uses an array that leads with the type name?
// no? kids these days...
function preparedQuery($sql,$params) {
for ($i=0; $i<count($params); $i++) {
$sql = preg_replace('/\?/','"'.$params[$i].'"',$sql,1);
}
return $sql;
}
/*
$stmt = $mysqli->stmt_init();
$query = preparedQuery($sql_query, $binds);
$stmt->prepare($query);
*/
// original code stripped from the bowels of php.net
<?php
// if you are unfortunate enough to be in a database w/ register globals on, rip u
foreach (array_merge($_GET, $_POST, $_COOKIE) as $key => $val) {
global $$key;
$$key = (get_magic_quotes_gpc()) ? $val : addslashes($val);
}

find

<\?( )*(?!php)(?!=)(?!xml)(?!mso)

replace

<?php 

notes

  • trailing space in the replace
  • original answer from stackoverflow
  • set your file mask to *.php,*.html because a lot of javascript things will match this pattern
  • some php PDF libs will match this pattern as well
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment