adding folders that are empty to disallowed array - php

I am using an array which allows me to prevent particular subfolders from being included in a script that generates an audio playlist. It looks like this:
$disallowed=array('..', '.', 'simon', 'steve');
I would like to find a way of disallowing any folders which are empty, or, perhaps, better still, disallowing folders that do not have any MP3s in them (in case the script interprets folders as not empty if they contain system files which are automatically generated).
I included the full script below so that you can see how the disallowed array fits in.
Would someone be able to help with this?
Thanks,
Nick
<?php
/*
* =====================
* FUNctions
* =====================
*/
/*
* array subvalue sort -- from: http://www.firsttube.com/read/sorting-a-multi-dimensional-array-with-php/
*
* this function lets me sort the multidimensional array containing song/artist information by the file modified time, a subvalue
*/
function subval_sort($a,$subkey) {
foreach($a as $k=>$v) {
$b[$k] = strtolower($v[$subkey]);
}
arsort($b); //change this to 'asort' for ascending
foreach($b as $key=>$val) {
$c[] = $a[$key];
}
return $c;
}
/*
* function written to clean up my messy code (too many slashes ... display '/' as '&raquo' (>>) for user friendliness )
*/
function clean($dirty, $type='general', $debug=false){
//debug
if($debug==true)
echo'<br />value before clean: '.$dirty.' (first character: '.substr($dirty, 0, 1).')';
/*
* General cleaning -- remove '/' at front and end
*/
if(substr($dirty, 0, 1)=='/'){
//echo'<br />found leading /';
$dirty=substr($dirty, 1, strlen($dirty)-1);
}
if(substr($dirty, -1)=='/')
$dirty=substr($dirty, 0, strlen($dirty)-1);
//prepare the subfolder display information by type
if($type=='link')
$dirty=str_replace(array('//','»'), array('/', '/'), $dirty);
else if($type=='display')
$dirty=str_replace(array('///','//','/'), array('»','»', '»'), $dirty);
else
$dirty=str_replace('»', '/', $dirty);
if($debug==true)echo' | after clean: '.$dirty;
//return
return $dirty;
}
function makelink($linkme, $debug=false){
$link=str_replace('»', '/', $linkme);
$link=str_replace('//', '/', $link);
return $link;
}
function recursiveGetSongs($directory, $fileinfo, $useID3, $getID3, $parent=null, $debug, $filtered=null){
/*
* configure function here:
*
* _usage_
* > the disallowed array should include any folders or files you explicitely don't want displayed
* > the allowedfiletypes array should include any file extentions you want to play
*/
$disallowed=array('..', '.', 'simon', 'steve');
$allowedfiletypes=array('mp3');
if($filtered!=null){
$disallowed=array_merge((array)$filtered, (array)$disallowed);
}
//simple error fix
if($directory=='./')
$directory='.';
//debug
if ($debug==true)echo'Dir to open: '.$directory;
//open directory
$dir = opendir($directory);
while ($read = readdir($dir))
{
//if ( !in_array($read, $disallowed) AND ( $filter!=null AND in_array($read, $filter) ) )
if ( !in_array($read, $disallowed) )
{
if($debug==true)echo $read.'<br />';
//if is not dir, handle file
if ( !is_dir($directory.'/'.$read) ){
if($debug==true)echo '^^ not dir | dir: '.$directory.'<br />';
if( in_array(substr($read, -3, 3), $allowedfiletypes) ){
if($useID3==TRUE){
//store id3 info
$FullFileName = realpath($directory.'/'.$read);
if($debug==TRUE)echo'<br />FFN » '.$FullFileName;
$ThisFileInfo = $getID3->analyze($FullFileName);
getid3_lib::CopyTagsToComments($ThisFileInfo);
$fileinfo[$read]['artist']=$ThisFileInfo['comments_html']['artist'][0];
$fileinfo[$read]['title']=$ThisFileInfo['comments_html']['title'][0];
$fileinfo[$read]['album']=$ThisFileInfo['comments_html']['album'][0];
$fileinfo[$read]['filename']=$ThisFileInfo['filename'];
$fileinfo[$read]['modified']=date ("YmdHis", filemtime($directory.'/'.$read));
if($debug==true)
echo "<br />$read was last modified: " . date ("YmdHis", filemtime($directory.'/'.$read));
$fileinfo[$read]['path']=$directory.'/'.$read;
if($debug==true)echo'<span style="margin-left: 10px;">path:'.$fileinfo[$read]['path'].' > fn: '.$fileinfo[$read]['filename'].'</span><br /><br />';
if($parent!=null)
$fileinfo[$read]['from']=str_replace(array('./', '//', '/'), array('', '»', '»'), $directory); // was =$parent
else
$fileinfo[$read]['from']='root'; //testing this
if($debug==true)echo'<br />'.$fileinfo[$read]['from'].'<br />';
//debug
//echo$ThisFileInfo['filename'].'<br />';
}
else{
//store filename
$fileinfo[$fileinfo['count']]['path']=$directory.'/'.$read;
$fileinfo[$fileinfo['count']]['fn']=$read;
if($parent!=null)
$fileinfo[$fileinfo['count']]['from']=str_replace(array('./', '//', '/'), array('', '»', '»'), $directory);
$fileinfo[$fileinfo['count']]['modified']=date ("YmdHis", filemtime($directory.'/'.$read));
//$fileinfo[$fileinfo['count']]=date ("YmdHis", filemtime($directory.'/'.$read));
}
//inc counter
$fileinfo['count']=$fileinfo['count']+1; // had ++ and it didn't work
}
else
;//do nothing
}
//else, must be a folder (as determined above), recurse folder
else{
//debug
if($debug==true)echo '^^ DIR<br />';
//capture subfolders in case they are needed
if($parent!='')$fileinfo['folders'].=$parent.'»'.$read.'|';
else $fileinfo['folders'].=$read.'|';
$fileinfo['folderpaths'].=$directory.'/|';
$fileinfo=recursiveGetSongs($directory.'/'.$read, $fileinfo, $useID3, $getID3, $parent.'/'.$read, $debug, $filtered);
}
}
}
closedir($dir);
return $fileinfo;
}
?>

Use glob('somefolder/*.mp3') to test if there are mp3s in a folder. The function will return an array, so if it's empty the folder does not contain any mp3 files.

You could try glob() since it's flexible and a little bit more secure than building up a 'valid array'.
foreach (glob($dir . "/*.mp3") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}

Related

Removing document root from file path Zend Framework 2

I'm trying to display an image but I am running into the error of Not allowed to load local resource: file:///C:/xampp/htdocs/public/images/profile/jimmy/status/boned.jpg in the browser console. What I am trying to do is use the base path provided by Zend Framework 2 but I'm retrieving the images in the model so (as far as I know), I can't use $this->basePath() like I would in the view.
This is my json string I am returning but would like to just be able to return /images/profile/jimmy/status/boned.jpg and whatever other images are in there.
I'm getting all the files outside of the directory 'status'. I am trying to get the files inside the status directory. When I did a var_dump this is what I get string(43) "C:\xampp\htdocs/public/images/profile/jimmy" I'm unclear why it is omitting the status directory after '/jimmy'
json string being returned:
{"feed":{"username":"Timmy","status":["this is jimmy, test"],"images":["videos","status","sithtoon.jpg","sith.jpg","edited_photos","diploma.jpg","current","albums","Screenshot_2016-08-09_21_28_13_361272.jpg","Screenshot_2016-08-05_17_55_48_500802.jpg","515gIIJ-Imgur.png",".htaccess"]}}
Here is the relevant PHP code (in the model):
public function listFriendsStatus()
{
$user_id = $this->getUserId()['id'];
// get the friend ids based on user id
// and then compare the friend id to the id in status table
$friend_query = new Select('friends');
$friend_query->columns(array('friend_id'))
->where(array('user_id' => $user_id));
$query = $this->sql->getAdapter()->query(
$this->sql->buildSqlString($friend_query),
Adapter::QUERY_MODE_EXECUTE
);
if ($query->count() > 0) {
$friend_id = array();
foreach ($query as $result) {
$friend_id[] = $result['friend_id'];
}
$status = new Select('status');
$status->columns(array('status'))
->where(array('id' => $friend_id));
$status_query = $this->sql->getAdapter()->query(
$this->sql->buildSqlString($status),
Adapter::QUERY_MODE_EXECUTE
);
if ($status_query->count() > 0) {
// check if a image was used
$members = new Select('members');
$members->columns(array('username'))
->where(array('id' => $friend_id));
$image_query = $this->sql->getAdapter()->query(
$this->sql->buildSqlString($members),
Adapter::QUERY_MODE_EXECUTE
);
if ($image_query->count() > 0) {
foreach ($image_query as $value) {
if (is_dir(getcwd() . '/images/profile/' . $value['username'] . '/status/')) {
$status_dir = pathinfo(getcwd() . '/images/profile/' . $value['username'] . '/status/');
}
}
$images = array();
chdir($status_dir['dirname']);
var_dump($status_dir['dirname']);
// retrieve the image inside the status directory
foreach (array_diff(scandir($status_dir['dirname'], 1), array('.', '..')) as $values) {
$images[] = $values;
}
} else {
throw new FeedException("The user does not exist in the user table.");
}
$status = array();
// get all the statuses
foreach ($status_query as $rows) {
$status[] = $rows['status'];
}
return array('username' => ucfirst($value['username']), 'status' => $status, 'images' => $images); // how to just get the basePath path with zf2
} else {
throw new FeedException("No status was found for your friends.");
}
} else {
throw new FeedException(sprintf("Could not locate any friends for %s", $this->user));
}
}
controller code:
public function getfriendstatusAction()
{
$layout = $this->layout();
$layout->setTerminal(true);
$view_model = new ViewModel();
$view_model->setTerminal(true);
try {
echo json_encode(array('feed' => $this->getStatusService()->listFriendsStatus()));
} catch (FeedException $e) {
echo json_encode(array('fail' => $e->getMessage()));
}
return $view_model;
}
jquery code:
$.getJSON('/members/feed/get-friend-status', function(data) {
$.each(data, function(i, item) {
$('.w3-container.w3-card-2.w3-white.w3-round.w3-margin').find('h4').html(data[i].username);
$('.w3-container.w3-card-2.w3-white.w3-round.w3-margin').find('p').html(data[i].status);
$('.w3-container.w3-card-2.w3-white.w3-round.w3-margin').find('img').attr('src', data[i].images);
});
}).fail(function(response) {
console.log(response);
});
I've been trying to use other directory functions provided with PHP but if I try anything, I run into the error directory could not be found. Basically what I am trying to do is use the similiar approach of $this->basePath() but in a model.
I hope that is clear enough..
Thanks!
Here is a screenshot of what I'm getting and how I want to get the status directory, not the directory outside of it.
I have an idea.
In your code is:
$status_dir = pathinfo(getcwd() . '/images/profile/' . $value['username'] . '/status/');
// ..............
chdir($status_dir['dirname']);
var_dump($status_dir['dirname']);
Try:
var_dump($status_dir);
I guess 'status' will be in 'basename' and / or in 'filename'
pathinfo gets last segment of argument string path as 'basename'.
Pathinfo only parses string as path and return array info, don't check it for isDir or isFile. Your correct chdir should looks like chdir($status_dir['dirname'] . '/' . $status_dir['basename'] ); if you need use of pathinfo.
In other words: dirname of 'images/profile/jimmy/status' is 'images/profile/jimmy' and its a reason why you don't see status in var_dump($status_dir['dirname']) and why chdir($status_dir['dirname']) not working correctly.

php listing directory and sub-directories as hyper-links with option for new folder

I am trying to list all folders within the WWW directory of my WAMPserver with the option to create a new folder for each 'group' of sub-directories as a list of hyperlinks.
I know the code below (see bottom of post) doesn't correspond to HTML standards at the moment, I shall add the header and footer after I have the code below functioning as intended.
Currently the following occurs as output of the script below (see bottom of post):
www
applied
images
NEW FOLDER
templates
NEW FOLDER
NEW FOLDER
coob
includes
NEW FOLDER
NEW FOLDER
NEW FOLDER
when I'd like the output to be in the format:
www
applied
images
templates
NEW FOLDER
coob
includes
NEW FOLDER
NEW FOLDER
NEW FOLDER
I understand to list the sub-directories within each directory found you require a recursive function. I have added the 'random' text ">/>N_F-H:L:A-T_T<\<" into the array due to the fact that this contains multiple symbols disallowed within directory names across multiple operating systems. - this is the one occurrence that shouldn't be treated as a directory and display the NEW FOLDER hyperlink.
I don't understand why when you run this script - The NEW FOLDER item comes up for each item within the list of a sub-directory, rather than just once per group of sub-directories. Why is this and how can I achieve the desired output?
<style type="text/css">
ul.dirlist, ul.dirlist li
{
list-style-type: none;
padding-left: 1em;
}
</style>
<?php
function ListFolder($path, $default_dir)
{
$dircontents = scandir($path);
$dircontents[] = ">/>N_F-H:L:A-T_T<\<";
$dirref = str_replace($default_dir . DIRECTORY_SEPARATOR, "", $path);
//Leave only the lastest folder name
$dirpaths = explode(DIRECTORY_SEPARATOR, $path);
$dirname = end($dirpaths);
echo "<ul class='dirlist'>";
echo "<li>".$dirname."";
foreach($dircontents as $key => $dir)
{
if($dir == ">/>N_F-H:L:A-T_T<\<")
{
echo "<li>NEW FOLDER</li>";
}
else
{
if($dir != "." AND $dir != "..")
{
if(is_dir($path . DIRECTORY_SEPARATOR . $dir))
{
Test($path.DIRECTORY_SEPARATOR.$dir, $default_dir);
}
}
}
}
//close list
echo "</li>";
echo "</ul>";
}
$www_dir = dirname(dirname(__FILE__));
$www_dir_parts = explode(DIRECTORY_SEPARATOR , $www_dir);
if(in_array('www', $www_dir_parts))
{
$wwwkey = array_search('www', $www_dir_parts);
}
elseif(in_array('htdocs', $www_dir_parts))
{
$wwwkey = array_search('htdocs', $www_dir_parts);
}
else
{
$cancel = 1;
//do nothing
}
//if the script hasn't been canceled
if(! isset($cancel))
{
$i = 0;
//default_dir as nothing to begin with
$default_dir = "";
$default_dir_www = "";
while($i <= $wwwkey)
{
//create path to absolute directory path of www
$default_dir .= $www_dir_parts[$i] . DIRECTORY_SEPARATOR;
//increment i
$i++;
}
$i = 0;
while($i <= ($wwwkey - 1))
{
//create path to absolute directory path of www
$default_dir_www .= $www_dir_parts[$i] . DIRECTORY_SEPARATOR;
//increment i
$i++;
}
$default_dir_www = rtrim($default_dir_www, DIRECTORY_SEPARATOR);
$default_dir = rtrim($default_dir, DIRECTORY_SEPARATOR);
}
ListFolder($default_dir, $default_dir_www);
?>
Try below function instead
function ListFolder($path, $default_dir){
$dircontents = scandir($path);
$dirref = str_replace($default_dir . DIRECTORY_SEPARATOR, "", $path);
$havesub = false;
echo "<ul class='dirlist'>";
foreach($dircontents as $key => $dir)
{
if($dir != "." AND $dir != "..")
{
if(is_dir($path . DIRECTORY_SEPARATOR . $dir))
{
echo "<li>".$dir."";
ListFolder($path.DIRECTORY_SEPARATOR.$dir, $default_dir);
echo "</li>";
$havesub = true;
}
}
}
echo ($havesub) ? "<li>NEW FOLDER</li>" : '';
echo "</ul>";
}

php breadcrumbs from url and query database

I have a php function that works for what i need but now i've found that there is chance for it to fail. I use mod rewrite to rewrite urls and this function needs urls to be rewritten for it to work. If i have 2 pages that have been rewritten as the same even though they are not the same, it could fail.
The function reads the url and splits it into parts using the / as the separator.
What i want to be able to do is check if the url has the word 'forum' in it and if it does query the database using the following url parts in the query.
Say i have a url like http://www.mydomain.com/forum/1/2/6 i would like to get the 1 and 2 for my query which would be the boardid and topicid. The last bit would be the page number.
This is a rewritten url which would normally look like http://www.mydomain.com/topic.php?boardid=1&topicid=2&pagenum=6 but using the function I wouldn't be able to split becuase there are no /.
The query i can do easy enough, it's just checking the url to make sure that 'forum' is in there then do the query. If it's not in the url then carry on as normal.
here is the code for my php breadcrumb
function breadcrumb(
$home = 'Home', // Name of root link
$division = ' / ', // Divider between links
$hidextra = true, // Toggle hide/show get data and fragment in text
$index = false, // Toggle show/hide link to directory if it does not contain a file
$indexname = 'index.php' // The definition of the file the directory must contain
) {
$breadcrumb="";
// Requested addons...
$extension = '.php'; // Extension to cut off the end of the TEXT links
$ifIndex = 'index.php'; // Filename of index/default/home files to not display
// End requested addons
$whole = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if(stristr($whole,"contact-bidder")) {
$whole = substr($whole,0,strrpos($whole,'/'));
}
$parts = explode('/', $whole);
$parts[0] = 'http://'.$parts[0];
$array = array('-', '%20');
$breadcrumb .= "{$home}{$division}";
$k = 1;
for ($i=1;$i < sizeof($parts);$i++) {
$uri = '/';
while ($k <= $i) {
$uri .= $parts[$k];
if ($k != (sizeof($parts)-1)) $uri .= '/';
$k++;
}
if (($index && is_dir($_SERVER['DOCUMENT_ROOT'].$uri) && is_file($_SERVER['DOCUMENT_ROOT'].$uri.$indexname)
|| !$index
|| !is_dir($_SERVER['DOCUMENT_ROOT'].$uri)) && $parts[$i] != $ifIndex) {
$breadcrumb .= "<a href=\"$uri\">";
if ($hidextra) {
$breadcrumb .= rtrim(preg_replace("/\?.*$/", '', ucwords(str_replace($array," ",$parts[$i]))), $extension);
}
else {
$breadcrumb .= rtrim(ucwords($parts[$i]), $extension);
}
$breadcrumb .= '</a>';
}
else {
$breadcrumb .= ucwords(str_replace($array," ",$parts[$i]));
}
if (isset($parts[($i+1)])) {
$breadcrumb .= $division;
}
$k = 1;
}
return $breadcrumb;
}
If that isn't possible or easy, is there a way i can split on the ? and & and get only what is before the ? and after = for each variable in the url
The string of variables after the URL is called the query string. The best way to check for the existence of variables in the query string is to look in the $_GET array.
So... if you have:
http://google.com/?key=value&another_key=another_value&forum=22
You would check for the variable 'forum' like this:
if (isset($_GET['forum'])){
// 'forum' has been set in the query string, do something...
} else {
// 'forum' has NOT been set.
}
I managed to fix it how i needed and it wasn't that hard
function breadcrumb(
$home = 'Home', // Name of root link
$division = ' / ', // Divider between links
$hidextra = true, // Toggle hide/show get data and fragment in text
$index = false, // Toggle show/hide link to directory if it does not contain a file
$indexname = 'index.php' // The definition of the file the directory must contain
) {
global $host,$dbUser,$dbPass,$dbName;
require_once("php/database/connection.php");
require_once("php/database/MySQL.php");
// Connect to the database and grab the email
$db = & new MySQL($host,$dbUser,$dbPass,$dbName);
$breadcrumb="";
// Requested addons...
$extension = '.php'; // Extension to cut off the end of the TEXT links
$ifIndex = 'index.php'; // Filename of index/default/home files to not display
// End requested addons
$whole = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$parts = explode('/', $whole);
$parts[0] = 'http://'.$parts[0];
$array = array('-', '%20');
$breadcrumb .= "{$home}{$division}";
$k = 1;
for ($i=1;$i < sizeof($parts);$i++) {
$uri = '/';
while ($k <= $i) {
$uri .= $parts[$k];
if ($k != (sizeof($parts)-1)) $uri .= '/';
$k++;
}
if (($index && is_dir($_SERVER['DOCUMENT_ROOT'].$uri) && is_file($_SERVER['DOCUMENT_ROOT'].$uri.$indexname)
|| !$index
|| !is_dir($_SERVER['DOCUMENT_ROOT'].$uri)) && $parts[$i] != $ifIndex) {
$breadcrumb .= "<a href=\"$uri\">";
if ($hidextra) {
if($parts[$i-1]=="forum") {
$board = substr($parts[$i],6);
$sql = "SELECT boardname FROM boards WHERE boardid='".$board."'";
$result = $db->query($sql);
while($row=$result->fetch()) {
extract($row, EXTR_PREFIX_INVALID, '_');
$breadcrumb .= $boardname;
}
}
else if($parts[$i-2]=="forum") {
$topic = substr($parts[$i],10);
$sql = "SELECT topicname FROM topics WHERE topicid='".$topic."'";
$result = $db->query($sql);
while($row=$result->fetch()) {
extract($row, EXTR_PREFIX_INVALID, '_');
$breadcrumb .= $topicname;
}
}
else {
$breadcrumb .= rtrim(preg_replace("/\?.*$/", '', ucwords(str_replace($array," ",$parts[$i]))), $extension);
}
}
else {
$breadcrumb .= rtrim(ucwords($parts[$i]), $extension);
}
$breadcrumb .= '</a>';
}
else {
$breadcrumb .= ucwords(str_replace($array," ",$parts[$i]));
}
if (isset($parts[($i+1)])) {
$breadcrumb .= $division;
}
$k = 1;
}
return $breadcrumb;
}
I had to check on the current part if the previous was 'forum', make a connection to the database, grab the info i needed then replace what is in the breadcrumb at that point. It was the same for the next breadcrumb.
It now works how it should.

How could I parse gettext .mo files in PHP4 without relying on setlocale/locales at all?

I made a couple related threads but this is the one direct question that I'm seeking the answer for. My framework will use Zend_Translate if the php version is 5, otherwise I have to mimic the functionality for 4.
It seems that pretty much every implementation of gettext relies on setlocale or locales, I know there's a LOT of inconsistency across systems which is why I don't want to rely upon it.
I've tried a couple times to get the textdomain, bindtextdomain and gettext functions to work but I've always needed to invoke setlocale.
By the way, all the .mo files will be UTF-8.
Here's some reusable code to parse MO files in PHP, based on Zend_Translate_Adapter_Gettext:
<?php
class MoParser {
private $_bigEndian = false;
private $_file = false;
private $_data = array();
private function _readMOData($bytes)
{
if ($this->_bigEndian === false) {
return unpack('V' . $bytes, fread($this->_file, 4 * $bytes));
} else {
return unpack('N' . $bytes, fread($this->_file, 4 * $bytes));
}
}
public function loadTranslationData($filename, $locale)
{
$this->_data = array();
$this->_bigEndian = false;
$this->_file = #fopen($filename, 'rb');
if (!$this->_file) throw new Exception('Error opening translation file \'' . $filename . '\'.');
if (#filesize($filename) < 10) throw new Exception('\'' . $filename . '\' is not a gettext file');
// get Endian
$input = $this->_readMOData(1);
if (strtolower(substr(dechex($input[1]), -8)) == "950412de") {
$this->_bigEndian = false;
} else if (strtolower(substr(dechex($input[1]), -8)) == "de120495") {
$this->_bigEndian = true;
} else {
throw new Exception('\'' . $filename . '\' is not a gettext file');
}
// read revision - not supported for now
$input = $this->_readMOData(1);
// number of bytes
$input = $this->_readMOData(1);
$total = $input[1];
// number of original strings
$input = $this->_readMOData(1);
$OOffset = $input[1];
// number of translation strings
$input = $this->_readMOData(1);
$TOffset = $input[1];
// fill the original table
fseek($this->_file, $OOffset);
$origtemp = $this->_readMOData(2 * $total);
fseek($this->_file, $TOffset);
$transtemp = $this->_readMOData(2 * $total);
for($count = 0; $count < $total; ++$count) {
if ($origtemp[$count * 2 + 1] != 0) {
fseek($this->_file, $origtemp[$count * 2 + 2]);
$original = #fread($this->_file, $origtemp[$count * 2 + 1]);
$original = explode("\0", $original);
} else {
$original[0] = '';
}
if ($transtemp[$count * 2 + 1] != 0) {
fseek($this->_file, $transtemp[$count * 2 + 2]);
$translate = fread($this->_file, $transtemp[$count * 2 + 1]);
$translate = explode("\0", $translate);
if ((count($original) > 1) && (count($translate) > 1)) {
$this->_data[$locale][$original[0]] = $translate;
array_shift($original);
foreach ($original as $orig) {
$this->_data[$locale][$orig] = '';
}
} else {
$this->_data[$locale][$original[0]] = $translate[0];
}
}
}
$this->_data[$locale][''] = trim($this->_data[$locale]['']);
unset($this->_data[$locale]['']);
return $this->_data;
}
}
Ok, I basically ended up writing a mo file parser based on Zend's Gettext Adapter, as far as I know gettext is pretty much reliant upon the locale, so manually parsing the .mo file would save the hassle of running into odd circumstances with locale issues with setlocale. I also plan on parsing the Zend Locale data provided in the form of xml files.

Function list of php file

How to get list of functions that are declared in a php file
You can get a list of currently defined function by using get_defined_functions():
$arr = get_defined_functions();
var_dump($arr['user']);
Internal functions are at index internal while user-defined function are at index user.
Note that this will output all functions that were declared previous to that call. Which means that if you include() files with functions, those will be in the list as well. There is no way of separating functions per-file other than making sure that you do not include() any file prior to the call to get_defined_functions().
If you must have the a list of functions per file, the following will attempt to retrieve a list of functions by parsing the source.
function get_defined_functions_in_file($file) {
$source = file_get_contents($file);
$tokens = token_get_all($source);
$functions = array();
$nextStringIsFunc = false;
$inClass = false;
$bracesCount = 0;
foreach($tokens as $token) {
switch($token[0]) {
case T_CLASS:
$inClass = true;
break;
case T_FUNCTION:
if(!$inClass) $nextStringIsFunc = true;
break;
case T_STRING:
if($nextStringIsFunc) {
$nextStringIsFunc = false;
$functions[] = $token[1];
}
break;
// Anonymous functions
case '(':
case ';':
$nextStringIsFunc = false;
break;
// Exclude Classes
case '{':
if($inClass) $bracesCount++;
break;
case '}':
if($inClass) {
$bracesCount--;
if($bracesCount === 0) $inClass = false;
}
break;
}
}
return $functions;
}
Use at your own risk.
You can use get_defined_functions() before and after you include the file, and look at what gets added to the array the second time. Since they appear to be in order of definition, you can just use the index like this:
$functions = get_defined_functions();
$last_index = array_pop(array_keys($functions['user']));
// Include your file here.
$functions = get_defined_functions();
$new_functions = array_slice($functions['user'], $last_index);
You can use the get_defined_functions() function to get all defined function in your current script.
See: http://www.php.net/manual/en/function.get-defined-functions.php
If you want to get the functions defined in another file, you can try using http://www.php.net/token_get_all like this:
$arr = token_get_all(file_get_contents('anotherfile.php'));
Then you can loop through to find function tokens with the symbols defined. The list of tokens can be found http://www.php.net/manual/en/tokens.php
If you're not worried about catching some commented out ones, this might be the simplest way:
preg_match_all('/function (\w+)/', file_get_contents(__FILE__), $m);
var_dump($m[1]);
I wrote this small function to return the functions in a file.
https://gist.github.com/tonylegrone/8742453
It returns a simple array of all the function names. If you're calling it in the particular file you want to scan, you can just use the following:
$functions = get_functions_in_file(__FILE__);
I solved this problem with array_diff
$funcs = get_defined_functions()["user"];
require_once 'myFileWithNewFunctions.php'; // define function testFunc() {} here
var_dump( array_values( array_diff(get_defined_functions()["user"], $funcs) ) )
// output: array[ 0 => "test_func"]
Update
To get the "real" functions name try this
foreach($funcsDiff AS $newFunc) {
$func = new \ReflectionFunction($newFunc);
echo $func->getName(); // testFunc
}
Well for what ever reason if you need to do this I show you:
Example file: Functions.php (I just wrote some random shit does not Mather it works with everything)
<?php
// gewoon een ander php script. door het gebruiken van meerdere includes kun je gemakkelijk samen aan één app werken
function johannes($fnaam, $mode, $array){
switch ($mode) {
case 0:
echo "
<center>
<br><br><br><br><br>
he johannes!<br><br>
klik hier voor random text:<br>
<input type='submit' value='superknop' id='btn' action='randomding' level='0' loadloc='randomshit' />
<p id='randomshit'></p>
</center>
";
break;
case 1:
echo "bouw formulier";
break;
case 2:
echo "verwerk formulier";
break;
default:
echo "[Error: geen mode gedefinieerd voor functie '$fnaam'!]";
}
}
function randomding($fnaam, $mode, $array){
$randomar = array('He pipo wat mot je!','bhebhehehehe bheeeee. rara wie ben ik?','poep meloen!','He johannes, wat doeeeeee je? <input type="text" name="doen" class="deze" placeholder="Wat doe je?" /> <input type="button" value="vertellen!" id="btn" action="watdoetjho" level="0" loadloc="hierzo" kinderen="deze"> <p id="hierzo"></p>','knopje de popje opje mopje','abcdefghijklmnopqrstuvwxyz, doe ook nog mee','Appien is een **p!!!!!! hahhahah<br><br><br><br> hahaha','Ik weet eiegelijk niks meer te verzinnen','echt ik weet nu niks meer','nou oke dan[[][(!*($##&*$*^éäåðßð','he kijk een microboat: <br> <img src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcS_n8FH6xzf24kEc31liZF6ULHCn2IINFurlFZ_G0f0_F4sLTi74w" alt="microboat" />');
$tellen = count($randomar);
$tellen--;
$rand = rand(0, $tellen);
echo $randomar[$rand];
}
function watdoetjho($fnaam, $mode, $array){
$dit = $array['doen'];
echo "Johannes is: $dit";
}
?>
We have 3 functions inside here:
johannes
randomding
watdoetjho
But we also call these functions:
count
rand
If we use get_defined_functions we will get all functions inside scope of script. Yes the PHP functions are separated from the user declared ones but still we want from a specific file.
If we use token_get_all we will receive a large amount of data we need to separate first.
I found these number which we need to make some connections inside the array to match the user functions. Otherwise we will have a list including all of php functions which are called upon.
The numbers are:
334 (List of all the user declared functions)
307 (List of all the function names)
If we filter the array to take all the 334 elements we have this:
334 -|- function -|- 5
But we need the function name and all the array elements are in relation with the 3rd value. So now we need to filter all the array elements which match the 3rd value (5) and the constant number 307.
This will give us something like this:
307 -|- johannes -|- 5
Now in PHP it will look like this:
<?php
error_reporting(E_ALL ^ E_NOTICE); // Or we get these undefined index errors otherwise use other method to search array
// Get the file and get all PHP language tokens out of it
$arr = token_get_all(file_get_contents('Functions.php'));
//var_dump($arr); // Take a look if you want
//The array where we will store our functions
$functions = array();
// loop trough
foreach($arr as $key => $value){
//filter all user declared functions
if($value[0] == 334){
//Take a look for debug sake
//echo $value[0] .' -|- '. $value[1] . ' -|- ' . $value[2] . '<br>';
//store third value to get relation
$chekid = $value[2];
}
//now list functions user declared (note: The last check is to ensure we only get the first peace of information about the function which is the function name, else we also list other function header information like preset values)
if($value[2] == $chekid && $value[0] == 307 && $value[2] != $old){
// just to see what happens
echo $value[0] .' -|- '. $value[1] . ' -|- ' . $value[2] . '<br>';
$functions[] = $value[1];
$old = $chekid;
}
}
?>
Result in this case is:
307 -|- johannes -|- 5
307 -|- randomding -|- 31
307 -|- watdoetjho -|- 43
To get list of defined and used functions in code, with small useful file manager, you can use code below. Enjoy!
if ((!function_exists('check_password'))||(!check_password()) ) exit('Access denied.'); //...security!
echo "<html><body>";
if (!$f) echo nl2br(htmlspecialchars('
Useful parameters:
$ext - allowed extensions, for example: ?ext=.php,css
$extI - case-insensitivity, for example: ?extI=1&ext=.php,css
'));
if (($f)&&(is_readable($_SERVER['DOCUMENT_ROOT'].$f))&&(is_file($_SERVER['DOCUMENT_ROOT'].$f))) {
echo "<h3>File <strong>$f</strong></h3>\n";
if(function_exists('get_used_functions_in_code')) {
echo '<h3>Used:</h3>';
$is=get_used_functions_in_code(file_get_contents($_SERVER['DOCUMENT_ROOT'].$f));
sort($is);
$def=get_defined_functions();
$def['internal']=array_merge($def['internal'], array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'finally', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while'));
foreach ($def['user'] as &$e) $e=strtolower($e); unset($e);
foreach ($is as &$e) if (!in_array(strtolower($e), $def['internal'], TRUE)) $e='<span style="color: red">'.$e.'</span>'; unset($e); //user-defined functions will be red
echo implode('<br />'.PHP_EOL,$is);
}
else echo "Error: missing function 'get_used_functions_in_code' !";
if(function_exists('get_defined_functions_in_code')) {
echo '<br /><h3>Defined:</h3>';
$is=get_defined_functions_in_code(file_get_contents($_SERVER['DOCUMENT_ROOT'].$f));
sort($is);
echo implode('<br />',$is);
}
else echo "Error: missing function 'get_defined_functions_in_code' !";
}
/*
File manager
*/
else {
if (!function_exists('select_icon')) {
function select_icon($name) {$name = pathinfo($name); return '['.$name["extension"].']';}
}
if($ext) $extensions=explode(',',strrev($ext));
if(!$f) $f=dirname($_SERVER['PHP_SELF']);
echo "<h3>Dir ".htmlspecialchars($f)."</h3><br />\n<table>";
$name=scandir($_SERVER['DOCUMENT_ROOT'].$f);
foreach($name as $name) {
if (!($fileOK=(!isset($extensions)))) {
foreach($extensions as $is) if (!$fileOK) $fileOK=((strpos(strrev($name),$is)===0)||($extI &&(stripos(strrev($name),$is)===0)));
}
$is=is_dir($fullName=$_SERVER['DOCUMENT_ROOT']."$f/$name");
if ($is || $fileOK) echo '<tr><td>'.select_icon($is ? 'x.folder' : $name).' </td><td> '.($is ? '[' : '').''.htmlspecialchars($name).''.($is ? ']' : '').'</td>';
if ($is) echo '<td> </td><td> </td>';
elseif ($fileOK) echo '<td style="text-align: right"> '.number_format(filesize($fullName),0,"."," ").' </td><td> '.date ("Y.m.d (D) H:i",filemtime($fullName)).'</td>';
if ($is || $fileOK) echo '</tr>'.PHP_EOL;
}
echo "\n</table>\n";
}
echo "<br /><br />".date ("Y.m.d (D) H:i")."</body></html>";
return;
/********************************************************************/
function get_used_functions_in_code($source) {
$tokens = token_get_all($source);
$functions = array();
$thisStringIsFunc = 0;
foreach($tokens as $token) {
if(($token[0]!=T_WHITESPACE)&&((!is_string($token))||($token[0]!='('))) unset($func);
if((is_array($token))&&(in_array($token[0],array(T_EVAL,T_EXIT,T_INCLUDE,T_INCLUDE_ONCE,T_LIST,T_REQUIRE,T_REQUIRE_ONCE,T_RETURN,T_UNSET)))) {$token[0]=T_STRING;$thisStringIsFunc=1;}
switch($token[0]) {
case T_FUNCTION: $thisStringIsFunc=-1;
break;
case T_STRING:
if($thisStringIsFunc>0) {
if (!in_array(strtoupper($token[1]),$functionsUp)) {$functions[]=$token[1];$functionsUp[]=strtoupper($token[1]);}
$thisStringIsFunc = 0;
} elseif ($thisStringIsFunc+1>0) {
$func = $token[1];
} else $thisStringIsFunc = 0;
break;
case '(':if($func) if(!in_array(strtoupper($func),$functionsUp)) {$functions[]=$func;$functionsUp[]=strtoupper($func);}
}
}
return $functions;
}
/********************************************/
function get_defined_functions_in_code($source) {
$tokens = token_get_all($source);
... then Andrew code (get_defined_functions_in_file) (https://stackoverflow.com/a/2197870/9996503)
}
Finding string (eg. Function names) in a file is simple with regex.
Just read the file and parse the content using preg_match_all.
I wrote a simple function to get list of functions in a file.
https://gist.github.com/komputronika/f92397b4f60870131ef52930faf09983
$a = functions_in_file( "mylib.php" );
The simplest thing (after I saw #joachim answer) is to use get_defined_functions and then only watch for the 'user' key (which contains an array of user-defined methods)
This is the code that helped me solved the problem
<?php
//Just to be sure it's empty (as it should be)
$functions = get_defined_functions();
print_r($functions['user']);
//Load the needed file
require_once '/path/to/your/file.php';
//display the functions loaded
$functions2 = get_defined_functions();
print_r($functions2['user']);
To retrieve also the params definition: (Source in comments)
Array
(
[0] => function foo ( &&bar, &big, [$small = 1] )
[1] => function bar ( &foo )
[2] => function noparams ( )
[3] => function byrefandopt ( [&$the = one] )
)
$functions = get_defined_functions();
$functions_list = array();
foreach ($functions['user'] as $func) {
$f = new ReflectionFunction($func);
$args = array();
foreach ($f->getParameters() as $param) {
$tmparg = '';
if ($param->isPassedByReference()) $tmparg = '&';
if ($param->isOptional()) {
$tmparg = '[' . $tmparg . '$' . $param->getName() . ' = ' . $param->getDefaultValue() . ']';
} else {
$tmparg.= '&' . $param->getName();
}
$args[] = $tmparg;
unset ($tmparg);
}
$functions_list[] = 'function ' . $func . ' ( ' . implode(', ', $args) . ' )' . PHP_EOL;
}
print_r($functions_list);
do include to the file and try this :
$functions = get_defined_functions();
print_r($functions['user']);

Categories