How can i replace url from css - css

I'm trying to combine multiple css files into a single file (from many different folder in my host, and including external css files). I use the following short code:
Declare array:
$styles=array(
array(
'name'=>'style1.css',
'path'=>'http://mydomain.com/css/'
),
array(
'name'=>'camera.css',
'path'=>'http://mydomain.com/js/camera/'
),
array(
'name'=>'style3.css',
'path'=>'http://external.com/assets/css/'
));
Get content and replace url:
foreach ($styles as $style) {
echo preg_replace('/url\(\s*[\'"]?\/?(.+?)[\'"]?\s*\)/i', 'url('.$style['path'].'$1)', file_get_contents($style['path'].$style['name']));
}
After combined into one css file, i have some css background image url as follows:
url(ttp://mydomain.com/css/../image/background.png) //Internal path - Case 1A
url(http://mydomain.com/js/camera/../../image/background.png) //Internal path - Case 1B
url(http://external.com/assets/css/../../image/background.png) //External path - Case 2
Actual, the internal path (Case 1A, 1B) can display the background image (despite the lack of professionalism), but in the external path (Case 2) cannot display the background image, my question is:
How can i replace wrong path with correct path (REPAIR BASE ON CURRENT RESULTS) as:
url(http://mydomain.com/image/background.png) //Correct internal path
url(http://external.com/image/background.png) //Correct external path
(I understand the problem is if find a keyword containing '../' will remove keyword and remove 1 string before the keyword contain '*/', , but I can't figure out how to do this).
Thanks in advance.
(I have tried find out and tried searching for 1 week before ask new question).

additionally replace /up-level-directory/../ with /
that is '/([^\/]+)/../' with / globally
$correct_url = preg_replace('|/\w+/\.\./|', '/', $url);

Related

How to write lossless WebP files with PerlMagick

I'm trying to convert PNG files to lossless WebP in Perl with Graphics::Magick. Command line for that is:
$ gm convert in.png -define webp:lossless=true out.webp
My Perl code looks something like that:
use Graphics::Magick;
my $image = Graphics::Magick->new();
$image->Read("in.png");
my $image_data = $image->ImageToBlock(magick => "webp");
print $out_fh $image_data;
This code writes lossy WebP files perfectly, but how can I express the "-define" thing in terms of Perl API?
Thanks,
Update: looks like I need to call AddDefiniton API function (http://www.graphicsmagick.org/api/image.html#adddefinition). Looks like it's not exported via Perl API as of now.
I know it is of no help to you, but for those interested in how to do it in PHP, here is how:
$im = new \Gmagick($src);
$im->setimageformat('WEBP');
// Not completely sure if setimageoption() has always been there, so lets check first.
if (method_exists($im, 'setimageoption')) {
$im->setimageoption('webp', 'lossless', 'true');
}
$imageBlob = $im->getImageBlob();
$success = #file_put_contents($destination, $imageBlob);
For more webp options, check out this code

Difference between two files view in HTML using Java or any jar

I want to write a script which compare two files in java and see there difference in html page ( side by side ), can someone help me out how to write ( where to start). I am pulling my hair out for this....
I want to use this script in beanshell postprocessor so that I can compare the standard output files with result files easily
I don't think you should be asking people for writing code for you here, consider hiring a freelancer instead.
Alternatively you can use the following approach:
Add JSR223 Assertion as a child of the request which you would like to fail if files won't be equal
Put the following code into "Script" area:
def file1 = new File('/path/to/file1')
def file2 = new File('/path/to/file2')
def file1Lines = file1.readLines('UTF-8')
def file2Lines = file2.readLines('UTF-8')
if (file1Lines.size() != file2Lines.size()) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('Files size is different, omitting line-by-line compare')
} else {
def differences = new StringBuilder()
file1Lines.eachWithIndex {
String file1Line, int number ->
String file2Line = file2Lines.get(number)
if (!file1Line.equals(file2Line)) {
differences.append('Difference # ').append(number).append('. Expected: ')
.append(file1Line).append('. Actual: ' + file2Line)
differences.append(System.getProperty('line.separator'))
}
}
if (differences.toString().length() > 0) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage(differences.toString())
}
}
If there will be differences in files content you will see them listed one by one in the JSR223 Assertion
See Scripting JMeter Assertions in Groovy - A Tutorial for more details.

SQLite: isolating the file extension from a path

I need to isolate the file extension from a path in SQLite. I've read the post here (SQLite: How to select part of string?), which gets 99% there.
However, the solution:
select distinct replace(column_name, rtrim(column_name, replace(column_name, '.', '' ) ), '') from table_name;
fails if a file has no extension (i.e. no '.' in the filename), for which it should return an empty string. Is there any way to trap this please?
Note the filename in this context is the bit after the final '\'- it shouldn't be searching for'.'s in the full path, as it does at moment too.
I think it should be possible to do it using further nested rtrims and replaces.
Thanks. Yes, you can do it like this:
1) create a scalar function called "extension" in QtScript in SQLiteStudio
2) The code is as follows:
if ( arguments[0].substring(arguments[0].lastIndexOf('\u005C')).lastIndexOf('.') == -1 )
{
return ("");
}
else
{
return arguments[0].substring(arguments[0].lastIndexOf('.'));
}
3) Then, in the SQL query editor you can use
select distinct extension(PATH) from DATA
... to itemise the distinct file extensions from the column called PATH in the table called DATA.
Note that the PATH field must contain a backslash ('\') in this implementation - i.e. it must be a full path.

Remove .js file works but a warning persist

i would like to remove a .js file from a drupal module to override this .js one in my theme.
So, in my MYTHEME_preprocess_page() function I added the following code:
//Delete logintoboggan.js and replace by our own in .info file
$tmpJs = drupal_add_js();
if (isset($tmpJs['module']['sites/all/modules/logintoboggan/logintoboggan.js'])) unset($tmpJs['module']['sites/all/modules/logintoboggan/logintoboggan.js']);
$vars['scripts'] = drupal_get_js($tmpJs);
This for, but a warning message appear on the website:
warning: Illegal offset type in isset or empty in /home/mykitxen/public_html/example.com/includes/common.inc on line 2210.
What is wrong?
You're missing the first parameter of drupal_get_js() ($scope), which should be 'header' or NULL in your case (with NULL just triggering the default 'header'). So the last line of your code should be:
$vars['scripts'] = drupal_get_js('header', $tmpJs);
or
$vars['scripts'] = drupal_get_js(NULL, $tmpJs);
NOTE: This assumes Drupal 6. If you are using Drupal 7, you might want to take a look at hook_js_alter().

Update configSource of XML element in web.config using Powershell by passing in Parameters

I am trying to figure out a way to update my web.config for different environments by updating the configSource for the appSettings element in the web.config.
Here are the way I know how to do it.
$xml.get_DocumentElement().appSettings.configSource = $replaced_test
The problem is that I want one base script where I can pass in different nodes to the script that I want to change and update but I am not sure how to do it.
For example, I want to be able to call a powershell script like this
changeWebConfig.ps1 nodeToChange newValueofNode
I hope this was clear enough.
This is the code I have now.
$webConfigPath = "C:\web.config"
# Get the content of the config file and cast it to XML
$xml = [xml](get-content $webConfigPath)
#this was the trick I had been looking for
$root = $xml.get_DocumentElement()."system.serviceModel".client.configSource = $replace
# Save it
$xml.Save($webConfigPath)
The problem I was having was the configuration node
I had to change it from
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
this to
<configuration>
I am not sure how to find the node with the configuration node in it's orginal state yet, but I'm getting closer.
function Set-ConfigAppSetting
([string]$PathToConfig=$(throw 'Configuration file is required'),
[string]$Key = $(throw 'No Key Specified'),
[string]$Value = $(throw 'No Value Specified'))
{
if (Test-Path $PathToConfig)
{
$x = [xml] (type $PathToConfig)
$node = $x.SelectSingleNode("//client[#configSource]")
$node.configSource = $Value
$x.Save($PathToConfig)
}
}
set-configappsetting "c:\web.config" CurrentTaxYear ".\private$\dinnernoworders" -confirm
Finally figured it out.
$root = $xml.get_DocumentElement().SelectSingleNode("//client[#configSource]").configSource = "test"
of course, I will replace "//client[#configSource]" with a variable so I can pass in different nodes as parameters to create my base script.
Im looking for a way to modify the code as well.
Here is a way you can view whats the node:
$path = 'c:\site\web.config'
$PublishState = (Select-Xml -Path $path -XPath "configuration/appSettings/add[#key='PublishState']/#value").Node.'#text'
$PublishState

Resources