}
}
-/**
- * Merges two variables into a single array
- *
- * Similar to PHP array_merge function, but provides same
- * functionality as array_merge without losing array values
- * with same key names. If the values under identical array
- * keys are both strings and $concat_strings is TRUE, those
- * values are concatenated together, otherwise they are placed
- * in a sub-array and are merged (recursively) in the same manner.
- *
- * If either of the elements being merged is not an array,
- * it will simply be added to the returned array.
- *
- * If both values are strings and $concat_strings is TRUE,
- * a concatenated string is returned instead of an array.
- *
- * @param mixed $a First element to be merged
- * @param mixed $b Second element to be merged
- * @param boolean $concat_strings Whether or not string values
- * should be concatenated instead
- * of added to different array
- * keys (default TRUE)
- *
- * @return array The merged $a and $b in one array
- *
- * @since 1.5.2
- *
- */
-function sq_array_merge($a, $b, $concat_strings=true) {
-
- $ret = array();
-
- if (is_array($a)) {
- $ret = $a;
- } else {
- if (is_string($a) && is_string($b) && $concat_strings) {
- return $a . $b;
- }
- $ret[] = $a;
- }
-
-
- if (is_array($b)) {
- foreach ($b as $key => $value) {
- if (isset($ret[$key])) {
- $ret[$key] = sq_array_merge($ret[$key], $value, $concat_strings);
- } else {
- $ret[$key] = $value;
- }
- }
- } else {
- $ret[] = $b;
- }
-
- return $ret;
-
-}
-
/**
* Add a variable to the session.
* @param mixed $var the variable to register
return '';
}
+
+
+/**
+ * Find files in a given directory optionally limited to only
+ * those with the given file extension. If the directory is
+ * not found or cannot be opened, no error is generated; only
+ * an empty file list is returned.
+FIXME: do we WANT to throw an error or a notice or... or return FALSE?
+ *
+ * @param string $directory_path The path (relative or absolute)
+ * to the desired directory.
+ * @param string $extension The file extension filter (optional;
+ * default is to return all files.
+ * @param boolean $return_filenames_only When TRUE, only file names
+ * are returned, otherwise the
+ * $directory_path string is
+ * prepended to each file in
+ * the returned list (optional;
+ * default is filename only)
+ *
+ * @return array The requested file list.
+ *
+ * @since 1.5.2
+ *
+ */
+function list_files($directory_path, $extension='', $return_filenames_only=TRUE) {
+
+ $files = array();
+
+//FIXME: do we want to place security restrictions here like only allowing
+// directories under SM_PATH?
+ // validate given directory
+ //
+ if (empty($directory_path)
+ || !is_dir($directory_path)
+ || !is_readable($directory_path)
+ || !($DIR = opendir($directory_path))) {
+ return $files;
+ }
+
+ // parse through the files
+ //
+ $extension = empty($extension) ? '' : '.' . trim($extension, '.');
+ while (($file = readdir($DIR)) !== false) {
+
+ if ($file == '.' || $file == '..') continue;
+
+ if (empty($extension)
+ || strrpos($file, $extension) === (strlen($file) - strlen($extension))) {
+ $files[] = ($return_filenames_only
+ ? $file
+ : $directory_path . '/' . $file);
+ }
+
+ }
+ closedir($DIR);
+
+ return $files;
+
+}
+
+
+/**
+ * Print variable
+ *
+ * sm_print_r($some_variable, [$some_other_variable [, ...]]);
+ *
+ * Debugging function - does the same as print_r, but makes sure special
+ * characters are converted to htmlentities first. This will allow
+ * values like <some@email.address> to be displayed.
+ * The output is wrapped in <<pre>> and <</pre>> tags.
+ * Since 1.4.2 accepts unlimited number of arguments.
+ * @since 1.4.1
+ * @return void
+ */
+function sm_print_r() {
+ ob_start(); // Buffer output
+ foreach(func_get_args() as $var) {
+ print_r($var);
+ echo "\n";
+ // php has get_class_methods function that can print class methods
+ if (is_object($var)) {
+ // get class methods if $var is object
+ $aMethods=get_class_methods(get_class($var));
+ // make sure that $aMethods is array and array is not empty
+ if (is_array($aMethods) && $aMethods!=array()) {
+ echo "Object methods:\n";
+ foreach($aMethods as $method) {
+ echo '* ' . $method . "\n";
+ }
+ }
+ echo "\n";
+ }
+ }
+ $buffer = ob_get_contents(); // Grab the print_r output
+ ob_end_clean(); // Silently discard the output & stop buffering
+ print '<div align="left"><pre>';
+ print htmlentities($buffer);
+ print '</pre></div>';
+}
+
+