PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

Sunday, October 16, 2022

[FIXED] How to use script in same scripts twice on same page in opencart?

 October 16, 2022     html, opencart, php, script     No comments   

Issue

I am using the following script to show the barcode on the Invoice of my product SKU. But when I use the same script again to show the bar code for the product name{{ product.name }} and SKU barcode disappear, It only shows one barcode ie Product name barcode. How can I use the script to show multiple barcodes, for the product names and SKU?

FYI: I have no knowledge of codes.

<script src="https://cdn.jsdelivr.net/jsbarcode/3.3.20/JsBarcode.all.min.js"></script>
        <h1><img id="upc"/><script> $('#upc').JsBarcode("{{ product.sku }}",{width:1,height:50,lineColor: "#333333" }); </script></p></h1>

Solution

First I would say, learn how to use HTML, script and other code! Second (also said already), use correct code.

  1. add the script to your template (only once!), like:

<script src="//cdn.jsdelivr.net/npm/jsbarcode@3.11.5/dist/JsBarcode.all.min.js"></script>

Do that at the end of the template.

  1. use correct HTML-Code, e.g.

<svg class="barcode" jsbarcode-format="code128" jsbarcode-value="{{ item.model }}"></svg>

beside each position the barcode shall be displayed.

  1. call the script and init it:

<script>JsBarcode(".barcode").init();</script>

at the end of your template.

  1. (optional): style your barcode output, according explained here: JSBarcode

p.s.: what I would do, is to store the jsbarcode script locally and call the local stored script.

I know many users intend to use cdn sources. Nice, but not effectful, imagine you want to diplay your solution locally without any internet connection. Or - think about the massive outfall a few weeks ago, where important sources were not reachable.

What you have, is what you can control!

pps: the difference in using a dot instead a # is, # indicates an ID, where the dot is simply a CSS-Class.



Answered By - OSWorX
Answer Checked By - Marie Seifert (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Sunday, August 28, 2022

[FIXED] How to import passwords and logins data to firefox?

 August 28, 2022     csv, firefox, google-chrome, import, script     No comments   

Issue

I have a csv file with all of my passwords from chrome and want to transfer them all ti firefox. But recently firefox removed the feature to import login data. This feature is currently disabled by default, due to performance issues.

So I was wandering, is there a way to write some kind of a script that would iterate through saved login data and push it to firefox, instead of doing it manually one by one?


Solution

Go to about:config and search for: signon.management.page.fileImport.enabled

set it to true, then navigate to about:logins (if already open refresh the page) and hit the 3 dots, Import from File is now an option, import your csv and be happy.

Image of the added option



Answered By - Kaplan
Answer Checked By - Terry (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Sunday, May 15, 2022

[FIXED] How to Get Shell Script to Change File Permissions?

 May 15, 2022     apache, file-permissions, script, shell, ubuntu     No comments   

Issue

I have a webpage where users can upload a file. After the upload, the webpage calls a shell script to move the uploaded file to another location. I'm certain the "mv" command is failing because of a permissions issue, but I'm uncertain how to overcome this.

First things first. My webpage runs on an Ubuntu 16.04 server and Apache2 2.4.41. When the user uploads a file, that file is saved on the server in directory /var/www/html/uploads:

me@myServer:/var/www/html/uploads$ ls -l
total 44
-rw-r--r-- 1 www-data www-data   761 Feb 21 15:38 UsersUploadedFile.txt
me@myServer:/var/www/html/uploads$

I notice that the file is owned by user www-data.

Once the file is uploaded, the webpage calls this shell script to move the file to another directory:

#!/bin/bash
echo "Attempting to move the uploaded file..."
{
        mv /var/www/html/uploads/UsersUploadedFile.txt  /home/me/UsersUploadedFile.txt
} || {
   echo "Gah, failed to move the file!"
}

When the file is uploaded, you see this on my webpage:

Attempting to move the uploaded file...
Gah, failed to move the file!

So the "mv" command is failing.

My first instinct was that this was a permissions issue. The file-to-be-moved is owned by user www-data, as I mentioned before. The shell script is too:

me@myServer:/var/www/html$ ls -l
total 36
-rwxr-xr-x 1 www-data www-data  593 Feb 21 15:53 moveTheFile.sh
me@myServer:/var/www/html$

But the directory where I want to file to be moved is owned by user me, and I can't change that for other scripting reasons.

What I guess I'd like to do is to is have the shell script change UsersUploadedFile.txt 's ownership to user me, and then move the file. But if I insert a chown command into the shell script, that command fails, too.

It looks like user www-data is running the apache2 service, therefore also running the shell script...? I'm not sure.

me@myServer:/var/www/html$ ps -ef | grep apache
root      14931      1  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  14934  14931  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  14935  14931  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  14936  14931  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  14937  14931  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  14938  14931  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  14942  14931  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
ph9821    15165  14831  0 16:03 pts/2    00:00:00 grep --color=auto apache
me@myServer:/var/www/html$

So what might I be doing wrong here? And how can I get the shell script to change file permissions? I guess that's what I really need done here. Thank you.


Solution

This is a permission issue. User www-data cannot write a file in your home directory.

Assuming your home directory is owned by user "me" and group "me", you will have to set permissions on your home directory so your group can write into it (it should already). Then add user www-data in that group.

Important note

If you put permission 777 on your home directory, then every user on the system can write into that directory! You should put 775 or 770. Only you and your group should be able to write (and maybe read also, depending on your requirement). Not important if this is your own machine, critical if this is a machine with many users.

Remember permissions are: user group others. 777 is rwx for all 3. It can be dangerous.



Answered By - Nic3500
Answer Checked By - David Goodson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, May 12, 2022

[FIXED] How to pull strings from multiple .txt files based on file name and append them to a new file on Powershell?

 May 12, 2022     append, powershell, script     No comments   

Issue

so basically I have a series of .txt files following the same nomenclature, such as:

1111210803_3CE_080977851__006908818__21110300013442021110420211105_20211110_120447_35418862_820

1111210933_3CE_006908818__2111040001442021110520211108_20211110_120447_35418860_820

The naming convention on these all files always starts with the date, ie 111121. Inside these files, you have lines of strings. I am interested in pulling a specific string in the first line from each of these files. Here is an example of the first line:

123456789012345678901234567890123 I             696969CCHKCTX       12345678901   DA 22758287

In particular, I am interested in the 696969CCHKCTX string. All files will have some numbers followed by the CCHKCTX value. I want to pull the 696969 part of the 696969CCHKCTX string from each .txt file and append them all into a new file.

If possible, I would like to sum those strings and add the appropriate decimal place, as they are to actually be dollar values ie 696969 is actually representing 6969.69 and the last two numbers in that string always represent the cent amount. This rule applies to all the .txt files. I want to be able to apply this to all files that are the same date (ie all files starting with 111121)

How would I go about this?


Solution

Try the following, which combines Get-ChildItem, Group-Object, and ForEach-Object, as well as the -replace operator:

Get-ChildItem -File | # get files of interest; add path / filter as needed.
  Group-Object { $_.Name.Substring(0, 6) } | # group by shared date prefix
    ForEach-Object {
      $firstLines = $_.Group | Get-Content -First 1 # get all 1st lines
      # Extract the cents amounts and sum them.
      $sumCents = 0.0
      $firstLines.ForEach({ 
        $sumCents += [double] ($_ -replace '.+\b(\d+)CCHKCTX\b.+', '$1') 
      })
      # Output an object with the date prefix and the sum dollar amount.
      [pscustomobject] @{
        Date = $_.Name
        Sum = $sumCents / 100
      }
    }

The above outputs a table-formatted representation to the display. You can save it to a file with > / Out-File, for instance, though it's better to use a structured text format for later processing, such as Export-Csv.



Answered By - mklement0
Answer Checked By - Katrina (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Wednesday, April 20, 2022

[FIXED] How to replace a line in a file on multiple computers using PowerShell

 April 20, 2022     connection, powershell, script, windows     No comments   

Issue

I'm trying to replace one line in a test.ini file on multiple PCs with Powershell. But I need to replace this line in a test.ini file with different content. For example:

  • PC st1: in test.ini file line 10 I have "hello" and I need to replace that with "hi"
  • PC st2: in test.ini file line 10 I have "hello" and I need to replace that with "bye"

I created a script but don't understand how can I change the same line with different content on multiple PCs (

 $devices = Get-Content "C:\script\device.txt"

foreach ($computer in $devices) {

Invoke-Command -ComputerName $computer -scriptblock {
((Get-Content -path C:\test\test.ini -Raw) -replace 'hello','world') | Set-Content -Path C:\test\test.ini
Get-Content -path C:\test\test.ini | Select -Index 10 }

}

Please help if I can do that perhaps.


Solution

Ok, so here's what I mean.

Instead of the text file with just the computer names in it, make that a CSV file like user2670623 already commented

ComputerName,SearchWord,Replacement
C2712,hello,hi
C1278,hello,bye
C2452,hello,again

Now you have the computer name, the search word AND the replacement for that particular computer all in one and then you can do stuff like this:

$devices = Import-Csv "C:\script\device.csv"

foreach ($computer in $devices) {
    Invoke-Command -ComputerName $computer.ComputerName -ScriptBlock {
        param(
            [string]$findThis,
            [string]$replaceWith
        )
        # -replace uses regex, so the $findThis string needs to be escaped because it may or may not
        # contain characters that have special meaning in Regular Expressions.
        (Get-Content -Path 'C:\test\test.ini' -Raw) -replace [regex]::Escape($findThis), $replaceWith | Set-Content -Path 'C:\test\test.ini'
    } -ArgumentList $computer.SearchWord $computer.Replacement
}

If it is not possible to create the CSV file for some reason, then you're stuck with tedious code like

$devices = Get-Content "C:\script\device.txt"

foreach ($computer in $devices) {
    # for each computer, define what is to be replaced by what
    switch ($computer) {
        'C2712' { $find = 'hello'; $replace = 'hi' }
        'C1278' { $find = 'hello'; $replace = 'bye' }
        'C2452' { $find = 'hello'; $replace = 'again' }
        # etcetera etcetera..
    }
    Invoke-Command -ComputerName $computer -ScriptBlock {
        # -replace uses regex, so the $findThis string needs to be escaped because it may or may not
        # contain characters that have special meaning in Regular Expressions.
        (Get-Content -Path 'C:\test\test.ini' -Raw) -replace [regex]::Escape($using:find), $using:replace | Set-Content -Path 'C:\test\test.ini'
    }
}


Answered By - Theo
Answer Checked By - Willingham (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing