Replacing text within string that contains serialize arrays - php

Thanks for clicking on the question. I am trying to find and replace text within a string that contains serialize arrays. For example :
'fgm2wc_options', 'a:19:{s:15:"automatic_empty";N;s:3:"url";s:25:"http://example.com/store/";s:8:"hostname";s:9:"localhost";s:4:"port";s:4:"3306";s:8:"database";s:22:"apgadmin_store_magento";s:8:"username" ... }
I want to change http://example.com/ to smth else I can do it with str_replace but it will not change the string length indicator ( e.g s:25 ).
This is a function i am using:
function recursive_unserialize_replace( $old_url = '', $new_url = '', $data = '', $serialised = false ) {
$new_url = rtrim( $new_url, '/' );
$data = explode( ', ', $data );
try {
if ( is_string( $data ) && ( $unserialized = #unserialize( $data ) ) !== false ) {
$data = recursive_unserialize_replace( $old_url, $new_url, $unserialized, true );
} elseif ( is_array( $data ) ) {
$_tmp = array( );
foreach ( $data as $key => $value ) {
$_tmp[ $key ] = recursive_unserialize_replace( $old_url, $new_url, $value );
}
$data = $_tmp;
unset( $_tmp );
} else {
if ( is_string( $data ) ) {
$data = str_replace( $old_url, $new_url, $data );
}
}
if ( $serialised ) {
return serialize( $data );
}
} catch( Exception $error ) {
}
return $data;
}
Any ideas ?

For anyone interested this is the solution i came up with:
function unserialize_replace( $old_url = '', $new_url = '', $database_string = '' ) {
if ( substr( $old_url, -1 ) !== '/' ) {
$new_url = rtrim( $new_url, '/' );
}
$serialized_arrays = preg_match_all( "/a:\d+:.*\;\}+/", $database_string, $matches );
if( !empty( $serialized_arrays ) && is_array( $matches ) ) {
foreach ( $matches[ 0 ] as $match ) {
$unserialized = #unserialize( $match );
if ( $unserialized ) {
$buffer = str_replace( $old_url, $new_url, $unserialized );
$buffer = serialize( $buffer );
$database_string = str_replace( $match, $buffer, $database_string );
}
}
}
if ( is_string( $database_string ) ) {
$database_string = str_replace( $old_url, $new_url, $database_string );
}
return $database_string;
}
Thanks for the suggestions. Please let me know if you see anything wrong and anything i can improve

Related

Assign css class to php code for a shortcode

I have a lot of posts which have a particular shortcode like [rest massage=""][/rest] and a text in between, and also the text is unique that mean in all the site its not used, only in this shortcode. the shortcode doesn't have any class or id assigned. now I want to assign a css class to it so that i can design it. how can i do it with php or jquery.
I founded this in the shortcode.php
function rcp_restrict_shortcode( $atts, $content = null ) {
$atts = shortcode_atts( array(
'userlevel' => 'none',
'message' => '',
'paid' => false,
'level' => 0,
'subscription' => ''
), $atts, 'restrict' );
global $rcp_options, $user_ID;
if ( strlen( $atts['message'] ) > 0 ) {
$teaser = $atts['message'];
} else {
$teaser = rcp_get_restricted_content_message( ! empty( $atts['paid'] ) );
}
$subscriptions = array_map( 'trim', explode( ',', $atts['subscription'] ) );
$has_access = false;
$classes = 'rcp_restricted';
$customer = rcp_get_customer(); // currently logged in customer
$is_active = rcp_user_has_active_membership();
$has_access_level = rcp_user_has_access( get_current_user_id(), $atts['level'] );
if( $atts['paid'] ) {
if ( rcp_user_has_paid_membership() && $has_access_level ) {
$has_access = true;
}
$classes = 'rcp_restricted rcp_paid_only';
} elseif ( $has_access_level ) {
$has_access = true;
}
if ( ! empty( $subscriptions ) && ! empty( $subscriptions[0] ) ) {
if ( $is_active && ! empty( $customer ) && count( array_intersect( rcp_get_customer_membership_level_ids( $customer->get_id() ), $subscriptions ) ) ) {
$has_access = true;
} else {
$has_access = false;
}
}
if ( $atts['userlevel'] === 'none' && ! is_user_logged_in() ) {
$has_access = false;
}
if( 'none' != $atts['userlevel'] ) {
$roles = array_map( 'trim', explode( ',', $atts['userlevel'] ) );
foreach ( $roles as $role ) {
if ( current_user_can( strtolower( $role ) ) ) {
$has_access = true;
break;
} else {
$has_access = false;
}
}
}
plz help.

I have used return create_function in my application below. php 7.2

how to win?
PHP 7.2.0, the create_function() is deprecated.
Thanks for your help,
or wait for developers?
Edited and cleared Code
return create_function( '', "
global $chery_core_version;
$path = trailingslashit( dirname( __FILE__ ) ) . 'cherry-core.php';
$data = get_file_data( $path, array(
'version' => 'Version'
) );
if ( isset( $data['version'] ) ) {
$version = $data['version'];
}
$old_versions = null;
if ( null !== $chery_core_version ) {
$old_versions = array_keys( $chery_core_version );
}
if ( is_array( $old_versions ) && isset( $old_versions[0] ) ) {
$compare = version_compare( $old_versions[0], $version, '<' );
if ( $compare ) {
$chery_core_version = array();
$chery_core_version[ $version ] = $path;
}
} else {
$chery_core_version = array();
$chery_core_version[ $version ] = $path;
}
" );
Use anonymous functions instead:
return function () {
global $chery_core_version;
$path = trailingslashit(__DIR__) . 'cherry-core.php';
$data = get_file_data( $path, [
'version' => 'Version'
] );
if ( isset( $data['version'] ) ) {
$version = $data['version'];
}
$old_versions = null;
if ( null !== $chery_core_version ) {
$old_versions = array_keys( $chery_core_version );
}
if ( is_array( $old_versions ) && isset( $old_versions[0] ) ) {
$compare = version_compare( $old_versions[0], $version, '<' );
if ( $compare ) {
$chery_core_version = [];
$chery_core_version[ $version ] = $path;
}
} else {
$chery_core_version = [];
$chery_core_version[ $version ] = $path;
}
};

unable to call static helpers in php

I am using a class that I downloaded; being not too familiar with php classes and helpers, I decided to post it to see if anyone more experience can give some help with the error I am having.
I am calling the function in the script "torrenttest" using this
<?php
require_once 'Torrentmaker.php';
$torrent = new Torrent( 'file', 'http://fr33dom.h33t.com:3310/announce' );
$torrent->announce(array('udp://tracker.openbittorrent.com:80/announce',
if ( ! $error = $torrent->error() ) // error method return the last error message
$torrent->save('test2.torrent'); // save to disk
else
echo '<br>DEBUG: ',$error;
the problem is that when 'file' is on the same server it works fine; however when it is calling an distant server it gives an time-out error or just creates a torrent file with no size (fails to run)
this is the script of Torrentmaker.php
class Torrent {
public function __construct ( $data = null, $meta = array(), $piece_length = 256 ) {
if ( is_null( $data ) )
return false;
if ( $piece_length < 32 || $piece_length > 4096 )
return self::set_error( new Exception( 'Invalid piece lenth, must be between 32 and 4096' ) );
if ( is_string( $meta ) )
$meta = array( 'announce' => $meta );
if ( $this->build( $data, $piece_length * 1024 ) )
$this->touch();
else
$meta = array_merge( $meta, $this->decode( $data ) );
foreach( $meta as $key => $value )
$this->{$key} = $value;
public function content ( $precision = null ) {
$files = array();
if ( isset( $this->info['files'] ) && is_array( $this->info['files'] ) )
foreach ( $this->info['files'] as $file )
$files[self::path( $file['path'], $this->info['name'] )] = $precision ?
self::format( $file['length'], $precision ) :
$file['length'];
elseif ( isset( $this->info['name'] ) )
$files[$this->info['name']] = $precision ?
self::format( $this->info['length'], $precision ) :
$this->info['length'];
return $files;
}
public function save ( $filename = null ) {
return file_put_contents( is_null( $filename ) ? $this->info['name'] . '.torrent' : $filename, $this->encode( $this ) );
}
static public function file_get_contents ( $file, $timeout = self::timeout, $offset = null, $length = null ) {
if ( is_file( $file ) || ini_get( 'allow_url_fopen' ) ) {
$context = ! is_file( $file ) && $timeout ?
stream_context_create( array( 'http' => array( 'timeout' => $timeout ) ) ) :
null;
return ! is_null( $offset ) ? $length ?
#file_get_contents( $file, false, $context, $offset, $length ) :
#file_get_contents( $file, false, $context, $offset ) :
#file_get_contents( $file, false, $context );
} elseif ( ! function_exists( 'curl_init' ) )
return self::set_error( new Exception( 'Install CURL or enable "allow_url_fopen"' ) );
$handle = curl_init( $file );
if ( $timeout )
curl_setopt( $handle, CURLOPT_TIMEOUT, $timeout );
if ( $offset || $length )
curl_setopt( $handle, CURLOPT_RANGE, $offset . '-' . ( $length ? $offset + $length -1 : null ) );
curl_setopt( $handle, CURLOPT_RETURNTRANSFER, 1 );
$content = curl_exec( $handle );
$size = curl_getinfo( $handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD );
curl_close( $handle );
return ( $offset && $size == -1 ) || ( $length && $length != $size ) ? $length ?
substr( $content, $offset, $length) :
substr( $content, $offset) :
$content;
public function errors() {
return empty( self::$errors ) ?
false :
self::$errors;
}
}

Slimmest OpenID Implementation?

Is there an implementation of OpenID that is under 5K or at least under 10K?
Please format the code with http://beta.phpformatter.com/ with the following settings to get the true size:
Indentation:
Indentation style: {K&R (One true brace style)}
Indent with: {Tabs}
Starting indentation: [1]
Indentation: [1]
Common:
[x] Remove all comments
[x] Remove empty lines
[x] Align assignments statements nicely
[ ] Put a comment with the condition after if, while, for, foreach, declare and catch statements
Improvement:
[x] Remove lines with just a semicolon (;)
[x] Make normal comments (//) from perl comments (#)
[x] Make long opening tag (
Brackets:
[x] Space inside brackets- ( )
[x] Space inside empty brackets- ( )
[x] Space inside block brackets- [ ]
[x] Space inside empty block brackets- [ ]
Edit:
Alix Axel's answer seems great, not perfect but is getting there. BTW: this is 6.4K So it is no longer the smallest, but i'm going through it to clean it up lots more.
<?php
class openID {
function __construct( $url = null, $realm = null, $return = null, $redirect = true, $verify = false ) {
$data = array( );
if ( ( $verify !== true ) && ( self::Value( $_REQUEST, 'openid_mode' ) !== false ) ) {
if ( strcmp( 'id_res', self::Value( $_REQUEST, 'openid_mode' ) ) === 0 ) {
$data[ 'openid.sig' ] = $_REQUEST[ 'openid_sig' ];
$data[ 'openid.mode' ] = 'check_authentication';
$data[ 'openid.signed' ] = $_REQUEST[ 'openid_signed' ];
$data[ 'openid.assoc_handle' ] = $_REQUEST[ 'openid_assoc_handle' ];
if ( array_key_exists( 'openid_op_endpoint', $_REQUEST ) === true ) {
$data[ 'openid.ns' ] = 'http://specs.openid.net/auth/2.0';
}
foreach ( explode( ',', self::Value( $_REQUEST, 'openid_signed' ) ) as $value ) {
$data[ 'openid.' . $value ] = $_REQUEST[ 'openid_' . str_replace( '.', '_', $value ) ];
}
if ( preg_match( '~is_valid\s*:\s*true~i', self::CURL( self::__construct( $_REQUEST[ 'openid_identity' ], false, false, false, true ), $data, 'POST' ) ) > 0 ) {
return self::Value( $_REQUEST, 'openid_claimed_id', self::Value( $_REQUEST, 'openid_identity' ) );
}
}
} else if ( ( $result = self::CURL( $url ) ) !== false ) {
$xml = self::XML( $result );
$server = strval( self::XML( $xml, '//xrd/service/uri', 0 ) );
if ( empty( $server ) === true ) {
$server = strval( self::XML( $xml, '//head/link[#rel="openid.server" or #rel="openid2.provider"]/#href', 0 ) );
}
if ( self::URL( $server ) === true ) {
if ( $redirect === true ) {
$realm = ( isset( $realm ) === true ) ? $realm : sprintf( '%s://%s/', $_SERVER[ "HTTPS" ] ? "https" : "http", $_SERVER[ 'HTTP_HOST' ] );
$return = ( isset( $return ) === true ) ? $return : sprintf( '%s://%s', $_SERVER[ "HTTPS" ] ? "https" : "http", $_SERVER[ 'HTTP_HOST' ] . $_SERVER[ 'REQUEST_URI' ] );
$delegate = ( preg_match( '~http://specs[.]openid[.]net/auth/2[.]0/server~', $result ) > 0 ) ? 'http://specs.openid.net/auth/2.0/identifier_select' : $url;
if ( preg_match( '~rel="openid[.]delegate"|<[^>]*Delegate[^>]*>~i', $result ) > 0 ) {
$delegate = parent::Value( ph()->Text->Regex( $result, '<([^>]*)Delegate[^>]*>([^>]+)</\1Delegate>', 1 ), 0 );
if ( empty( $delegate ) === true ) {
$delegate = strval( self::XML( $xml, '//head/link[#rel="openid.delegate"]/#href', 0, $delegate ) );
}
}
if ( preg_match( '~rel="openid2[.]provider"|http://specs[.]openid[.]net/auth/2[.]0~i', $result ) > 0 ) {
$data[ 'openid.ns' ] = 'http://specs.openid.net/auth/2.0';
if ( preg_match( '~rel="openid2[.]local_id"|<(Local|Canonical)ID[^>]*>~i', $result ) > 0 ) {
$delegate = self::Value( self::Regex( $result, '<(Local|Canonical)ID[^>]*>([^>]+)</\1ID>', 1 ), 0 );
if ( empty( $delegate ) === true ) {
$delegate = strval( self::XML( $xml, '//head/link[#rel="openid2.local_id"]/#href', 0, $delegate ) );
}
}
}
$data[ 'openid.mode' ] = 'checkid_setup';
$data[ 'openid.return_to' ] = $return;
$data[ 'openid.claimed_id' ] = $data[ 'openid.identity' ] = $delegate;
$data[ 'openid.' . ( ( array_key_exists( 'openid.ns', $data ) === true ) ? 'realm' : 'trust_root' ) ] = $realm;
self::Redirect( sprintf( '%s%s%s', $server, ( strpos( $server, '?' ) !== false ) ? '&' : '?', http_build_query( $data, '', '&' ) ) );
}
return $server;
}
}
return false;
}
function CURL( $url, $data = null, $method = "GET", $options = array( ) ) {
$result = false;
if ( ( extension_loaded( "curl" ) === true ) && ( $this->URL( $url ) === true ) ) {
$curl = curl_init( $url );
if ( is_resource( $curl ) === true ) {
curl_setopt( $curl, CURLOPT_FAILONERROR, true );
curl_setopt( $curl, CURLOPT_AUTOREFERER, true );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
if ( preg_match( "~^GET$~i", $method ) > 0 ) {
curl_setopt( $curl, CURLOPT_HTTPGET, true );
} else if ( preg_match( "~^POST$~i", $method ) > 0 ) {
curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $data );
}
$result = curl_exec( $curl );
if ( $result !== false ) {
curl_close( $curl );
}
}
}
return $result;
}
function Redirect( $url, $permanent = false ) {
if ( headers_sent() !== true ) {
header( "Location: " . $url, true, ( $permanent === true ) ? 301 : 302 );
}
exit( );
}
function Regex( $string, $pattern, $key = null, $modifiers = null, $flag = PREG_PATTERN_ORDER, $default = false ) {
$matches = array( );
if ( preg_match_all( "~" . $pattern . "~" . $modifiers, $string, $matches, $flag ) > 0 ) {
if ( isset( $key ) === true ) {
return ( $key === true ) ? $matches : Value( $matches, $key, $default );
}
return true;
}
return $default;
}
function URL( $value ) {
return (bool) filter_var( $value, FILTER_VALIDATE_URL );
}
function Value( $data, $key = null, $default = false ) {
if ( isset( $key ) === true ) {
foreach ( (array) $key as $value ) {
if ( is_object( $data ) === true ) {
$data = get_object_vars( $data );
}
if ( array_key_exists( $value, (array) $data ) !== true ) {
return $default;
}
$data = $data[ $value ];
}
}
return $data;
}
function XML( $xml, $xpath = null, $key = null, $default = false ) {
if ( extension_loaded( "SimpleXML" ) === true ) {
libxml_use_internal_errors( true );
if ( ( is_string( $xml ) === true ) && ( class_exists( "DOMDocument" ) === true ) ) {
$dom = new DOMDocument();
if ( $dom->loadHTML( $xml ) === true ) {
return $this->XML( simplexml_import_dom( $dom ), $xpath, $key, $default );
}
} else if ( is_object( $xml ) === true ) {
if ( isset( $xpath ) === true ) {
$xml = $xml->xpath( $xpath );
if ( isset( $key ) === true ) {
$xml = $this->Value( $xml, $key, $default );
}
}
return $xml;
}
}
return false;
}
}
new openID( "https://www.google.com/accounts/o8/id" );
?>
Slim down Lightopenid
First of I really don't get your filesize limitation. I find it kind of b*llshit if you ask me....
You could make a call graph(use some sort of dead code detector) of lightopenid to see which functions(only a few are used) you are using. A lot of these big functions aren't used standard for example I believe you can remove the big function protected function request_streams($url, $method='GET', $params=array()) on line 198. I also removed some other functions which aren't used when using it with example-google.php example. My final lightopenid class looks like this(I bet you can even eliminate more code when using call graph/dead code detector).
But now I got the filesize down to 9.8KB. If you compress it you can get it down even more. I got it down to your desired 10K mark, but now the code can not be read by humans anymore.
Code searches:
You could also have a look at these resources/searches to see if any are lighter(I highly doubt that):
https://github.com/search?langOverride=&language=php&q=openid&repo=&start_value=1&type=Repositories&x=17&y=33
http://code.google.com/hosting/search?q=openid+label:php&btn=Search+projects
Inspired by lightopenid and part of phunction (depends on 5 other methods):
public static function OpenID($id, $realm = null, $return = null, $verify = true)
{
$data = array();
if (($verify === true) && (array_key_exists('openid_mode', $_REQUEST) === true))
{
$result = parent::Value($_REQUEST, 'openid_claimed_id', parent::Value($_REQUEST, 'openid_identity'));
if (strcmp('id_res', parent::Value($_REQUEST, 'openid_mode')) === 0)
{
$data['openid.mode'] = 'check_authentication';
foreach (array('ns', 'sig', 'signed', 'assoc_handle') as $key)
{
$data['openid.' . $key] = parent::Value($_REQUEST, 'openid_' . $key);
if (strcmp($key, 'signed') === 0)
{
foreach (explode(',', parent::Value($_REQUEST, 'openid_signed')) as $value)
{
$data['openid.' . $value] = parent::Value($_REQUEST, 'openid_' . str_replace('.', '_', $value));
}
}
}
return (preg_match('~is_valid\s*:\s*true~', self::CURL(self::OpenID($result, false, false, false), array_filter($data, 'is_string'), 'POST')) > 0) ? $result : false;
}
}
else if (($result = self::XML(self::CURL($id))) !== false)
{
$server = null;
$protocol = array
(
array('specs.openid.net/auth/2.0/server', 'specs.openid.net/auth/2.0/signon', array('openid2.provider', 'openid2.local_id')),
array('openid.net/signon/1.1', 'openid.net/signon/1.0', array('openid.server', 'openid.delegate')),
);
foreach ($protocol as $key => $value)
{
while ($namespace = array_shift($value))
{
if (is_array($namespace) === true)
{
$server = strval(self::XML($result, sprintf('//head/link[contains(#rel, "%s")]/#href', $namespace[0]), 0));
$delegate = strval(self::XML($result, sprintf('//head/link[contains(#rel, "%s")]/#href', $namespace[1]), 0, $id));
}
else if (is_object($xml = self::XML($result, sprintf('//xrd/service[contains(type, "http://%s")]', $namespace), 0)) === true)
{
$server = parent::Value($xml, 'uri');
if ($key === 0)
{
$delegate = 'http://specs.openid.net/auth/2.0/identifier_select';
if (strcmp($namespace, 'specs.openid.net/auth/2.0/server') !== 0)
{
$delegate = parent::Value($xml, 'localid', parent::Value($xml, 'canonicalid', $id));
}
}
else if ($key === 1)
{
$delegate = parent::Value($xml, 'delegate', $id);
}
}
if (ph()->Is->URL($server) === true)
{
if (($realm !== false) && ($return !== false))
{
$data['openid.mode'] = 'checkid_setup';
$data['openid.identity'] = $delegate;
$data['openid.return_to'] = parent::URL($return, null, null);
if ($key === 0)
{
$data['openid.ns'] = 'http://specs.openid.net/auth/2.0';
$data['openid.realm'] = parent::URL($realm, false, false);
$data['openid.claimed_id'] = $delegate;
}
else if ($key === 1)
{
$data['openid.trust_root'] = parent::URL($realm, false, false);
}
parent::Redirect(parent::URL($server, null, $data));
}
return $server;
}
}
}
}
return false;
}
Usage:
OpenID('https://www.google.com/accounts/o8/id');
PS: I've updated the code I had previously posted because it was sub-optimal and prone to some bugs.
The slimmest openid implementation I know is LightOpenId
It has got 30kb with comments, so if you run it through that formatter it gets much smaller
EDIT: I found a smaller one here (just 6.6 kb after reformatting)

Manually write a url query string containing nested array data

I'm looking to manually write a multidimensional $_GET query string, saw this done the other day, but can't quite remember it!
something like:
www.url.com?val1=abc&val2=cde&[val3=fgh&val4=ijk]
http://domain.tld/path/to/script.php?arr[a][b][c]=foo
and
var_dump($_GET);
Since array parameters in URL's are postfixed by brackets, I'd try a function like this:
<?php
function array_map_scope( $callback, array $array, array $arguments = array(), $scope = array() ) {
if( !is_callable( $callback ) ) {
return( false );
}
$output = array();
foreach( $array as $key => $value ) {
if( is_array( $value ) ) {
$output[$key] = array_map_scope( $callback, $value, $arguments, array_push_value( $scope, $key ) );
} else {
$output[$key] = call_user_func_array( $callback, array_merge( array( $value, array_push_value( $scope, $key ) ), $arguments ) );
}
}
return( $output );
}
function array_push_value( $array, $value ) {
$array[] = $value;
return( $array );
}
function urlParam( $value, $key, $name ) {
return( $name.'['.implode( array_map( 'urlencode', $key ), '][' ).']='.urlencode( $value ) );
}
function array_values_recursive( $array ) {
$output = array();
foreach( $array as $value ) {
if( is_array( $value ) ) {
$output = array_merge( $output, array_values_recursive( $value ) );
} else {
$output[] = $value;
}
}
return( $output );
}
function array2URL( $name, $array ) {
$array = array_map_scope( 'urlParam', $array, array( urlencode( $name ) ) );
return( implode( array_values_recursive( $array ), '&' ) );
}
echo array2URL('test', array( 'a'=>'a','b'=>array('ba'=>'ba','bb'=>'bb'),'c'=>'c' ) );
?>
Rely on http_build_query() to perfectly format a query string for you.
Either use it directly in your script to generate the substring after ? in the url or use a sandbox to set up your array data, call the function, then copy-paste the output in your static file.
Code: (Demo)
$array = [
'indexed value',
'foo' => 'first level value',
'bar' => ['baz' => 'second level']
];
echo http_build_query($array);
// 0=indexed+value&foo=first+level+value&bar%5Bbaz%5D=second+level
A fringe case consideration

Categories