Reputation: 384
I have a Linux file server on the LAN, and wanted to be able to have users who don't know how to use the shell, to search for files on the file server.
I decided to write a PHP program so it could be used from a web browser where the user would be able to put in a search string and it would quickly list the matches. It makes use of the PHP exec function and it uses the Linux find command.
This works fine for most searches, but sometimes doing the text search, it returns nothing. I have debugged this to see what was being passed to 'find' and that always works, so there must be something wrong with the way I'm calling exec and parsing the output. But I don't know what it is. I saw in php.net some discussions about using different methods for executing shell commands and returning the output to the PHP program, but I've not been successful to get it to work all the time for all situations.
I thought it might be a Linux file permission issue, but the files it finds in the text search have the same permissions, ownership and group as the ones it doesn't find. Again, I know those files are there, because if I login to the shell myself and do a search using find I can locate them. I should mention, there is no shell login account, so I've not been able to 'su' to that account and test my find command in the shell as the share user.
Here is the web PHP web page, followed by the .php program.
<html>
<head>
<link rel="stylesheet" type="text/css" href="search-file-server.css">
<title>Search File Server</title>
</head>
<h1>Search File Server - Version 1.1</h1>
<body>
<TABLE>
<TR>
<TD>Enter all or part of the file name to
<form action="do_search.php" method="post" style="display:inline">search: <input type="text" name="findit">
</TD>
<TR>
<TD>
<input type="submit" name="submit_search">
</form>
</TD>
</TR>
<TR><TD> </TD></TR>
<TR>
<TD>List files and directories created or modified within the last
<form action="do_search.php" method="post" style="display:inline">
<select id="hours" name="hours">
<?php
for ($x = 1; $x <= 24; $x++) {
echo '<option value=' . '"' . $x . '"' . '>' . $x . '</option>';
}
?>
</select>
hour(s).
<TR>
</TD>
<TD>
<input type="submit" name="submit_hours">
</form>
</TD>
</TR>
<TR><TD> </TD></TR>
<TR>
<TD>List files and directories created or modified within the last
<form action="do_search.php" method="post" style="display:inline">
<select id="days" name="days">
<?php
for ($x = 1; $x <= 60; $x++) {
echo '<option value=' . '"' . $x . '"' . '>' . $x . '</option>';
}
?>
</select>
day(s).
<TR>
</TD>
<TD>
<input type="submit" name="submit_days">
</form>
</TD>
</TR>
</TABLE>
</body>
</html>
do_search.php:
<?php
$submit_search = $_POST["submit_search"];
$submit_hours = $_POST["submit_hours"];
$submit_days = $_POST["submit_days"];
$hours = $_POST["hours"];
$days = $_POST["days"];
$findit = $_POST["findit"]; // Search string from user.
if ($submit_search == "Submit") {
echo "<h1>Searching for: " . $findit . "</h1>";
$search_string = '"' . '*' . $findit . '*' . '"';
$find_stuff = "find /home/share -iname " . $search_string . " -not -name " . '".*" ';
exec("$find_stuff",$output);
// Remove the pre-appended directory path from the file server for display purposes.
foreach ($output as $i) {
echo str_replace("/home/share/","",$i) . '<BR>';
}
}
if ($submit_hours == "Submit") {
echo "<h1>Files/Directories created/modified within the last $hours hour(s)</h1>";
// echo "Hours: $hours" . '<BR>';
$minutes = $hours * 60;
$find_stuff = "find /home/share -not -name " . '".*" ' . " -mmin -$minutes";
exec("$find_stuff",$output);
// Remove the pre-appended directory path from the file server for display purposes.
foreach ($output as $i) {
echo str_replace("/home/share/","",$i) . '<BR>';
}
}
if ($submit_days == "Submit") {
echo "<h1>Files/Directories created/modified within the last $days day(s)</h1>";
// echo "Days: $days" . '<BR>';
$find_stuff = "find /home/share -not -name " . '".*" ' . " -mtime -$days";
exec("$find_stuff",$output);
// Remove the pre-appended directory path from the file server for display purposes.
foreach ($output as $i) {
echo str_replace("/home/share/","",$i) . '<BR>';
}
}
?>
</body>
</html>
Upvotes: 1
Views: 137
Reputation: 384
I overlooked that the permissions for the directories were being set by Samba to be
directory mode = 0770
when it should be 0775 so user apache can find it. This samba share has its own account. While this might seem low-security, it is but this file server is on the LAN and behind a firewall.
This explains why the exec command in PHP wasn't able to return $output because it was operating as user apache and not the Samba share user, which needed the directories to have a permission of 0775.
Upvotes: 1