View Full Version : Some help with PHP
gjoellee
March 5th, 2009, 09:05 PM
Hi, I am writing a program named Kubus in PHP (it is some sort of an offline website), and I am going to make Kubus be able to have addons. I don't know how to write the code I need!
OK, here are the facts:
All addons are installed in ./addons/<addon name>
All addons will have an index file, called "addindx.php", which is located in ./addons/<addon name>/addindx.php
I must have a php/css/html/js script that can search that folder(s) for the "addindx.php" and create a link to each addindx.php that exists (just simple <a href="URL">name</a>).
Note: The user might, install or remove the addons
If you need any more info, please tell...
jimi_hendrix
March 5th, 2009, 09:31 PM
for the actual addon part you could use eval() but that compromises security
Tibuda
March 5th, 2009, 10:06 PM
I believe this is what you are looking for.
<?php
$addins = glob('addins/**/addindx.php');
foreach ($addins as $addin) {
printf('<a href="%s">%s</a>', $addin, $addin);
}
?>You just need to find a way to get the addin name.
Peter76
March 6th, 2009, 12:27 PM
Hi, having flue and so being chained to my bed and learning PHP, I came up with the following solution:
<?php
echo '<ul id="addons">'.PHP_EOL;
define( ADDONDIR, 'addons/' ); //define addon directory
define( AINDEXNAME, 'addindx.php' ); //define name of addon index files
$addon_contents = scandir( ADDONDIR ); // list contents in addon directory in array
foreach( $addon_contents as $addon_item ) {
$exclude = array( '.', '..' );
if ( !in_array( $addon_item, $exclude ) && is_dir( ADDONDIR.$addon_item ) ) { // walk through addon dir and exclude .. and . and check if item is a dir. !NB is_dir needs absolute path to work.
$addon_item_contents = scandir( ADDONDIR.$addon_item );
if ( in_array( AINDEXNAME, $addon_item_contents ) ) { // Check if index file exists
echo '<li><a href="'.ADDONDIR.$addon_item.'/'.AINDEXNAME.'">'.$addon_item.'</a></li>'.PHP_EOL;
}
}
}
echo '</ul>'.PHP_EOL;
?>
As said, I'm quite new to PHP, so if somebody has a better idea to this or some other tips I would like to hear it very much.
Hope this helps the OP as well.
Reiger
March 6th, 2009, 01:00 PM
I believe this is what you are looking for.
<?php
$addins = glob('addins/**/addindx.php');
foreach ($addins as $addin) {
printf('<a href="%s">%s</a>', $addin, $addin);
}
?>You just need to find a way to get the addin name.
Expanding this approach so it does the full magic:
$addons = glob('addons/**/addindx.php');
echo "<ul>";
foreach ($addons as $addon) {
$bits = explode('/',$addon);
$name = $bits[1]; // as per OP's specification of dir structure/names
echo "<li><a href="$addon">$name</a></li>";
}
echo "</ul>";
gjoellee
March 6th, 2009, 04:07 PM
Expanding this approach so it does the full magic:
<?php
$addons = glob('addons/**/addindx.php');
echo "<ul>";
foreach ($addons as $addon) {
$bits = explode('/',$addon);
$name = $bits[1]; // as per OP's specification of dir structure/names
echo "<li><a href="$addon">$name</a></li>";
}
echo "</ul>";
?>
Gives,
Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /path/to/file.php on line 9this is line 9
echo "<li><a href="$addon">$name</a></li>";and I need one more thing. I don't have clean URL, so I must include a "?c=" in the URL so it reads of "index.php". That way I don't have to copy the theme files, and the content of my index.php again.
And one more thing: The name of the addon is stored in "addons/**/info/name.inc"
Peter76
March 6th, 2009, 04:24 PM
echo "<li><a href="$addon">$name</a></li>";
You should either escape the double qoutes like this:
echo "<li><a href=\"$addon\">$name</a></li>";
Or write it like this with single quotes ( Better IMHO )
echo '<li><a href="'.$addon.'">'.$name.'</a></li>';
Could you be a bit more elaborate on the not having a clean url thing? I haven't got a clue what you mean :-)
Good luck
Reiger
March 6th, 2009, 04:40 PM
Ah I'm typing faster than I think or mistype what I think...
echo "<li><a href='$addon'>$name</a></li>";
[/echo]
Now with the unclean URL you presumably meant:
echo "<li><a href='index.php?c=$addon'>$name</a></li>"
That still leave the name to be resolved properly, though. Did you mean that the name of the dot inc file [name].inc is the name of the addon or that you can only know the name by reading a file always called name.inc?
If the latter:
[php]
$bits = explode('/',$addon);
$bits[2] = 'info/name.inc';
$path = implode('/',$bits);
/*
Now add code which reads & parses the file refered to by $path.
The name of the addon should be stored in $name.
*/
echo "<li><a href='index.php?c=$addon'>$name</a></li>";
gjoellee
March 6th, 2009, 05:36 PM
Ah I'm typing faster than I think or mistype what I think...
echo "<li><a href='$addon'>$name</a></li>";
[/echo]
Now with the unclean URL you presumably meant:
echo "<li><a href='index.php?c=$addon'>$name</a></li>"
That still leave the name to be resolved properly, though. Did you mean that the name of the dot inc file [name].inc is the name of the addon or that you can only know the name by reading a file always called name.inc?
If the latter:
[php]
$bits = explode('/',$addon);
$bits[2] = 'info/name.inc';
$path = implode('/',$bits);
/*
Now add code which reads & parses the file refered to by $path.
The name of the addon should be stored in $name.
*/
echo "<li><a href='index.php?c=$addon'>$name</a></li>";
Yes. Inside the "name.inc" there is one line that contains the name. In this case the name.inc looks like this:
Random Tux"Random Tux" is written on line #1, and name.inc does not include any more than that.
However your code, does not give any errors now, but all I can see now is the "dot" from the <ul><li> tags. And after that, nothing. The name does not appear!
I can change the name to any file type, so if you need it to by as a .txt file, I will make the name in a .txt file.
gjoellee
March 7th, 2009, 07:53 PM
BUMPedibum
gjoellee
March 8th, 2009, 05:09 PM
pmub <-- Read backwards!
Tibuda
March 9th, 2009, 12:31 AM
http://www.php.net/manual/en/function.file-get-contents.php
<?php
$addins = glob('addins/**/addindx.php');
foreach ($addins as $addin) {
$name = file_get_contents(dirname($addin) . '/name.inc');
printf('<a href="%s">%s</a>', $addin, $name);
}
?>
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.