123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869 |
- <?php
- function libraries_flush_caches() {
-
-
-
-
- foreach (array('libraries_get_path', 'libraries_info') as $name) {
- drupal_static_reset($name);
- }
-
-
-
- if (db_table_exists('cache_libraries')) {
- return array('cache_libraries');
- }
- }
- function libraries_get_path($name, $base_path = FALSE) {
- $libraries = &drupal_static(__FUNCTION__);
- if (!isset($libraries)) {
- $libraries = libraries_get_libraries();
- }
- $path = ($base_path ? base_path() : '');
- if (!isset($libraries[$name])) {
- return FALSE;
- }
- else {
- $path .= $libraries[$name];
- }
- return $path;
- }
- function libraries_get_libraries() {
- $searchdir = array();
- $profile = drupal_get_path('profile', drupal_get_profile());
- $config = conf_path();
-
-
-
- $searchdir[] = 'libraries';
-
-
-
- $searchdir[] = "$profile/libraries";
-
- $searchdir[] = 'sites/all/libraries';
-
- $searchdir[] = "$config/libraries";
-
- $directories = array();
- $nomask = array('CVS');
- foreach ($searchdir as $dir) {
- if (is_dir($dir) && $handle = opendir($dir)) {
- while (FALSE !== ($file = readdir($handle))) {
- if (!in_array($file, $nomask) && $file[0] != '.') {
- if (is_dir("$dir/$file")) {
- $directories[$file] = "$dir/$file";
- }
- }
- }
- closedir($handle);
- }
- }
- return $directories;
- }
- function libraries_scan_info_files() {
- $profile = drupal_get_path('profile', drupal_get_profile());
- $config = conf_path();
-
- $directories = module_invoke_all('libraries_info_file_paths');
- $directories[] = 'libraries';
- $directories[] = "$profile/libraries";
- $directories[] = 'sites/all/libraries';
- $directories[] = "$config/libraries";
-
- $files = array();
- foreach ($directories as $dir) {
- if (file_exists($dir)) {
- $files = array_merge($files, file_scan_directory($dir, '@^[A-Za-z0-9._-]+\.libraries\.info$@', array(
- 'key' => 'name',
- 'recurse' => FALSE,
- )));
- }
- }
- foreach ($files as $filename => $file) {
- $files[basename($filename, '.libraries')] = $file;
- unset($files[$filename]);
- }
- return $files;
- }
- function libraries_invoke($group, &$library) {
-
-
-
- if (empty($library['callbacks'][$group])) {
- return;
- }
- foreach ($library['callbacks'][$group] as $callback) {
- libraries_traverse_library($library, $callback);
- }
- }
- function libraries_traverse_library(&$library, $callback) {
-
- $callback($library, NULL, NULL);
-
- if (isset($library['versions'])) {
- foreach ($library['versions'] as $version_string => &$version) {
- $callback($version, $version_string, NULL);
-
- if (isset($version['variants'])) {
- foreach ($version['variants'] as $version_variant_name => &$version_variant) {
- $callback($version_variant, $version_string, $version_variant_name);
- }
- }
- }
- }
-
- if (isset($library['variants'])) {
- foreach ($library['variants'] as $variant_name => &$variant) {
- $callback($variant, NULL, $variant_name);
- }
- }
- }
- function libraries_prepare_files(&$library, $version = NULL, $variant = NULL) {
-
-
- $file_types = array();
- if (isset($library['files'])) {
- $file_types[] = &$library['files'];
- }
- if (isset($library['integration files'])) {
-
- foreach ($library['integration files'] as &$integration_files) {
- $file_types[] = &$integration_files;
- }
- }
- foreach ($file_types as &$files) {
-
- foreach (array('js', 'css', 'php') as $type) {
- if (isset($files[$type])) {
- foreach ($files[$type] as $key => $value) {
-
- if (is_numeric($key)) {
- $files[$type][$value] = array();
- unset($files[$type][$key]);
- }
- }
- }
- }
- }
- }
- function libraries_detect_dependencies(&$library, $version = NULL, $variant = NULL) {
- if (isset($library['dependencies'])) {
- foreach ($library['dependencies'] as &$dependency_string) {
- $dependency_info = drupal_parse_dependency($dependency_string);
- $dependency = libraries_detect($dependency_info['name']);
- if (!$dependency['installed']) {
- $library['installed'] = FALSE;
- $library['error'] = 'missing dependency';
- $library['error message'] = t('The %dependency library, which the %library library depends on, is not installed.', array(
- '%dependency' => $dependency['name'],
- '%library' => $library['name'],
- ));
- }
- elseif (drupal_check_incompatibility($dependency_info, $dependency['version'])) {
- $library['installed'] = FALSE;
- $library['error'] = 'incompatible dependency';
- $library['error message'] = t('The version %dependency_version of the %dependency library is not compatible with the %library library.', array(
- '%dependency_version' => $dependency['version'],
- '%dependency' => $dependency['name'],
- '%library' => $library['name'],
- ));
- }
-
-
- $dependency_string = $dependency_info['name'];
- }
- }
- }
- function &libraries_info($name = NULL) {
-
- $libraries = &drupal_static(__FUNCTION__);
- if (!isset($libraries)) {
- $libraries = array();
-
- foreach (module_implements('libraries_info') as $module) {
- foreach (module_invoke($module, 'libraries_info') as $machine_name => $properties) {
- $properties['info type'] = 'module';
- $properties['module'] = $module;
- $libraries[$machine_name] = $properties;
- }
- }
-
- $themes = array();
- foreach (list_themes() as $theme_name => $theme_info) {
- if ($theme_info->status && file_exists(drupal_get_path('theme', $theme_name) . '/template.php')) {
-
-
- $themes[] = $theme_name;
- include_once drupal_get_path('theme', $theme_name) . '/template.php';
- $function = $theme_name . '_libraries_info';
- if (function_exists($function)) {
- foreach ($function() as $machine_name => $properties) {
- $properties['info type'] = 'theme';
- $properties['theme'] = $theme_name;
- $libraries[$machine_name] = $properties;
- }
- }
- }
- }
-
-
- foreach (libraries_scan_info_files() as $machine_name => $file) {
- $properties = drupal_parse_info_file($file->uri);
- $properties['info type'] = 'info file';
- $properties['info file'] = $file->uri;
- $libraries[$machine_name] = $properties;
- }
-
- foreach ($libraries as $machine_name => &$properties) {
- libraries_info_defaults($properties, $machine_name);
- }
-
-
-
- foreach (module_implements('libraries_info_alter') as $module) {
- $function = $module . '_libraries_info_alter';
- $function($libraries);
- }
- foreach ($themes as $theme) {
- $function = $theme . '_libraries_info_alter';
-
- if (function_exists($function)) {
- $function($libraries);
- }
- }
-
- foreach ($libraries as &$properties) {
- libraries_invoke('info', $properties);
- }
- }
- if (isset($name)) {
- if (!empty($libraries[$name])) {
- return $libraries[$name];
- }
- else {
- $false = FALSE;
- return $false;
- }
- }
- return $libraries;
- }
- function libraries_info_defaults(&$library, $name) {
- $library += array(
- 'machine name' => $name,
- 'name' => $name,
- 'vendor url' => '',
- 'download url' => '',
- 'path' => '',
- 'library path' => NULL,
- 'version callback' => 'libraries_get_version',
- 'version arguments' => array(),
- 'files' => array(),
- 'dependencies' => array(),
- 'variants' => array(),
- 'versions' => array(),
- 'integration files' => array(),
- 'callbacks' => array(),
-
- 'post-load integration files' => FALSE,
- );
- $library['callbacks'] += array(
- 'info' => array(),
- 'pre-detect' => array(),
- 'post-detect' => array(),
- 'pre-dependencies-load' => array(),
- 'pre-load' => array(),
- 'post-load' => array(),
- );
-
- array_unshift($library['callbacks']['info'], 'libraries_prepare_files');
- array_unshift($library['callbacks']['post-detect'], 'libraries_detect_dependencies');
- return $library;
- }
- function libraries_detect($name) {
-
- $library = &libraries_info($name);
-
- if ($library === FALSE) {
- return $library;
- }
-
- if (isset($library['installed'])) {
- return $library;
- }
- $library['installed'] = FALSE;
-
- if (!isset($library['library path'])) {
- $library['library path'] = libraries_get_path($library['machine name']);
- }
- if ($library['library path'] === FALSE || !file_exists($library['library path'])) {
- $library['error'] = 'not found';
- $library['error message'] = t('The %library library could not be found.', array(
- '%library' => $library['name'],
- ));
- return $library;
- }
-
- libraries_invoke('pre-detect', $library);
-
- if (!isset($library['version'])) {
-
-
- if (isset($library['version arguments'][0])) {
-
- $library['version'] = call_user_func_array($library['version callback'], array_merge(array($library), $library['version arguments']));
- }
- else {
- $library['version'] = $library['version callback']($library, $library['version arguments']);
- }
- if (empty($library['version'])) {
- $library['error'] = 'not detected';
- $library['error message'] = t('The version of the %library library could not be detected.', array(
- '%library' => $library['name'],
- ));
- return $library;
- }
- }
-
- if (!empty($library['versions'])) {
- ksort($library['versions']);
- $version = 0;
- foreach ($library['versions'] as $supported_version => $version_properties) {
- if (version_compare($library['version'], $supported_version, '>=')) {
- $version = $supported_version;
- }
- }
- if (!$version) {
- $library['error'] = 'not supported';
- $library['error message'] = t('The installed version %version of the %library library is not supported.', array(
- '%version' => $library['version'],
- '%library' => $library['name'],
- ));
- return $library;
- }
-
- $library = array_merge($library, $library['versions'][$version]);
- unset($library['versions']);
- }
-
- if (!empty($library['variants'])) {
- foreach ($library['variants'] as $variant_name => &$variant) {
-
-
- if (!isset($variant['variant callback'])) {
- $variant['installed'] = TRUE;
- }
- else {
-
-
- if (isset($variant['variant arguments'][0])) {
-
- $variant['installed'] = call_user_func_array($variant['variant callback'], array_merge(array($library, $variant_name), $variant['variant arguments']));
- }
- else {
- $variant['installed'] = $variant['variant callback']($library, $variant_name, $variant['variant arguments']);
- }
- if (!$variant['installed']) {
- $variant['error'] = 'not found';
- $variant['error message'] = t('The %variant variant of the %library library could not be found.', array(
- '%variant' => $variant_name,
- '%library' => $library['name'],
- ));
- }
- }
- }
- }
-
- $library['installed'] = TRUE;
-
- libraries_invoke('post-detect', $library);
- return $library;
- }
- function libraries_load($name, $variant = NULL) {
- $loaded = &drupal_static(__FUNCTION__, array());
- if (!isset($loaded[$name])) {
- $library = cache_get($name, 'cache_libraries');
- if ($library) {
- $library = $library->data;
- }
- else {
- $library = libraries_detect($name);
- cache_set($name, $library, 'cache_libraries');
- }
-
- if ($library === FALSE) {
- $loaded[$name] = $library;
- return $loaded[$name];
- }
-
-
- if (isset($variant)) {
-
-
-
- $library['variants'] += array($variant => array('installed' => FALSE));
- $library = array_merge($library, $library['variants'][$variant]);
- }
-
-
- unset($library['variants']);
-
- libraries_invoke('pre-dependencies-load', $library);
-
- $library['loaded'] = FALSE;
- if ($library['installed']) {
-
- if (isset($library['dependencies'])) {
- foreach ($library['dependencies'] as $dependency) {
- libraries_load($dependency);
- }
- }
-
- libraries_invoke('pre-load', $library);
-
- $library['loaded'] = libraries_load_files($library);
-
- libraries_invoke('post-load', $library);
- }
- $loaded[$name] = $library;
- }
- return $loaded[$name];
- }
- function libraries_load_files($library) {
-
- if (!$library['post-load integration files'] && !empty($library['integration files'])) {
- $enabled_themes = array();
- foreach (list_themes() as $theme_name => $theme) {
- if ($theme->status) {
- $enabled_themes[] = $theme_name;
- }
- }
- foreach ($library['integration files'] as $provider => $files) {
- if (module_exists($provider)) {
- libraries_load_files(array(
- 'files' => $files,
- 'path' => '',
- 'library path' => drupal_get_path('module', $provider),
- 'post-load integration files' => FALSE,
- ));
- }
- elseif (in_array($provider, $enabled_themes)) {
- libraries_load_files(array(
- 'files' => $files,
- 'path' => '',
- 'library path' => drupal_get_path('theme', $provider),
- 'post-load integration files' => FALSE,
- ));
- }
- }
- }
-
- $path = $library['library path'];
- $path = ($library['path'] !== '' ? $path . '/' . $library['path'] : $path);
-
- $count = 0;
-
-
-
-
- foreach (array('js', 'css') as $type) {
- if (!empty($library['files'][$type])) {
- foreach ($library['files'][$type] as $data => $options) {
-
-
- if (!is_array($options)) {
- $data = $options;
- $options = array();
- }
-
-
-
- if (is_numeric($data)) {
- $data = $options['data'];
- unset($options['data']);
- }
-
- $data = "$path/$data";
-
- if (!isset($options['group'])) {
- $options['group'] = ($type == 'js') ? JS_DEFAULT : CSS_DEFAULT;
- }
- call_user_func('drupal_add_' . $type, $data, $options);
- $count++;
- }
- }
- }
-
- if (!empty($library['files']['php'])) {
- foreach ($library['files']['php'] as $file => $array) {
- $file_path = DRUPAL_ROOT . '/' . $path . '/' . $file;
- if (file_exists($file_path)) {
- _libraries_require_once($file_path);
- $count++;
- }
- }
- }
-
- if ($library['post-load integration files'] && !empty($library['integration files'])) {
- $enabled_themes = array();
- foreach (list_themes() as $theme_name => $theme) {
- if ($theme->status) {
- $enabled_themes[] = $theme_name;
- }
- }
- foreach ($library['integration files'] as $provider => $files) {
- if (module_exists($provider)) {
- libraries_load_files(array(
- 'files' => $files,
- 'path' => '',
- 'library path' => drupal_get_path('module', $provider),
- 'post-load integration files' => FALSE,
- ));
- }
- elseif (in_array($provider, $enabled_themes)) {
- libraries_load_files(array(
- 'files' => $files,
- 'path' => '',
- 'library path' => drupal_get_path('theme', $provider),
- 'post-load integration files' => FALSE,
- ));
- }
- }
- }
- return $count;
- }
- function _libraries_require_once($file_path) {
- require_once $file_path;
- }
- function libraries_get_version($library, $options) {
-
- $options += array(
- 'file' => '',
- 'pattern' => '',
- 'lines' => 20,
- 'cols' => 200,
- );
- $file = DRUPAL_ROOT . '/' . $library['library path'] . '/' . $options['file'];
- if (empty($options['file']) || !file_exists($file)) {
- return;
- }
- $file = fopen($file, 'r');
- while ($options['lines'] && $line = fgets($file, $options['cols'])) {
- if (preg_match($options['pattern'], $line, $version)) {
- fclose($file);
- return $version[1];
- }
- $options['lines']--;
- }
- fclose($file);
- }
|