You are here

helpers.inc in Filebrowser 6.2

Misc helper functions.

File

includes/helpers.inc
View source
<?php

/* This file is part of "filebrowser".
 *    Copyright 2009-2011, arNuméral
 *    Author : Yoran Brault
 *    eMail  : yoran.brault@bad_arnumeral.fr (remove bad_ before sending an email)
 *    Site   : http://www.arnumeral.fr
 *
 * "filebrowser" is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * "filebrowser" is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with "filebrowser"; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

/**
 * @file
 *
 * Misc helper functions.
 */

/**
 * Filter non-empty values from formaapi checkboxes.
 *
 * @param $array source array
 * @return formapi checkboxes
 */
function _filebrowser_filter_checkboxes_result($array) {
  $result = array();
  if ($array) {
    foreach ($array as $key => $value) {
      if (!empty($value)) {
        $result[$key] = TRUE;
      }
    }
  }
  return $result;
}

/**
 * Convert an array for formapi checkboxes comatibiility.
 *
 * @param $properties source array
 * @return formapi checkboxes
 */
function _filebrowser_properties_to_checkboxes(&$properties) {
  $result = array();
  if ($properties) {
    foreach ($properties as $key => $value) {
      if ($value) {
        $result[$key] = $key;
      }
    }
  }
  return $result;
}

/**
 * Helper function to match a pattern on the path
 * @param path path to process
 * @param patterns to search (seperated by cr)
 * @return TRUE if at least one pattern is found
 */
function _filebrowser_match_path($path, $patterns) {
  static $regexps = NULL;
  if (!isset($regexps[$patterns])) {
    $regexps[$patterns] = '/^(' . preg_replace(array(
      '/(\\r\\n?|\\n)/',
      '/\\\\\\*/',
    ), array(
      '|',
      '.*',
    ), preg_quote($patterns, '/')) . ')$/';
  }
  $result = preg_match($regexps[$patterns], _filebrowser_safe_basename($path)) == 1;
  return $result;
}
function &_filebrowser_options($source) {
  foreach ($source as $key => &$value) {
    $value = $value['title'];
  }
  return $source;
}

/**  Check the end of a string
 * @param $str source string
 * @param $sub element to search
 * @return return TRUE is a string ends with another string.
 */
function _filebrowser_ends_with($str, $sub) {
  return substr($str, strlen($str) - strlen($sub)) == $sub;
}

/**
 * This function is used to invoke filebrowser_$kind hooks. Difference with a direct
 * module_invoke_all is that result is cached in a static way (for now, real caching
 * perhaps later).
 * @param $kind name of the hook (complete hook will be filebrowser_$kind)
 * @param $name the name of the resource retreived by the hook (used as hook
 * parameter and cache index)
 */
function _filebrowser_externals($kind, $name = NULL) {
  static $externals = array();
  if (!isset($externals[$kind])) {
    $externals[$kind] = module_invoke_all("filebrowser_{$kind}");
  }
  if (!is_null($name)) {
    return isset($externals[$kind][$name]) ? $externals[$kind][$name] : NULL;
  }
  return $externals[$kind];
}

/**
 * Build an URL with current pager settings.
 * @param $href target href
 * @return URL
 */
function _filebrowser_folder_url($href, $options = array()) {
  $query = _filebrowser_url_query($options);
  if ($query != '') {
    $options['query'] = $query;
  }
  return url($href, $options);
}
function _filebrowser_url_query($options = array()) {
  $query = array();
  foreach ($_GET as $key => $value) {
    if ($key != 'q') {
      $query[] = $key . "=" . filter_xss($value);
    }
  }
  if (isset($options['query'])) {
    foreach (explode('&', $options['query']) as $item) {
      $query[] = $item;
    }
  }
  if (count($query)) {
    return implode("&", $query);
  }
  else {
    return '';
  }
}

Functions

Namesort descending Description
_filebrowser_ends_with Check the end of a string
_filebrowser_externals This function is used to invoke filebrowser_$kind hooks. Difference with a direct module_invoke_all is that result is cached in a static way (for now, real caching perhaps later).
_filebrowser_filter_checkboxes_result Filter non-empty values from formaapi checkboxes.
_filebrowser_folder_url Build an URL with current pager settings.
_filebrowser_match_path Helper function to match a pattern on the path
_filebrowser_options
_filebrowser_properties_to_checkboxes Convert an array for formapi checkboxes comatibiility.
_filebrowser_url_query