/** * Determine the location of the CiviCRM source tree. * * @return array * - url: string. ex: "http://example.com/sites/all/modules/civicrm" * - path: string. ex: "/var/www/sites/all/modules/civicrm" */ public function getCiviSourceStorage() { global $civicrm_root; // Don't use $config->userFrameworkBaseURL; it has garbage on it. // More generally, we shouldn't be using $config here. if (!defined('CIVICRM_UF_BASEURL')) { throw new RuntimeException('Undefined constant: CIVICRM_UF_BASEURL'); } $cmsPath = $this->cmsRootPath(); // $config = CRM_Core_Config::singleton(); // overkill? // $cmsUrl = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE); $cmsUrl = CIVICRM_UF_BASEURL; if (CRM_Utils_System::isSSL()) { $cmsUrl = str_replace('http://', 'https://', $cmsUrl); } $civiRelPath = CRM_Utils_File::relativize($civicrm_root, $cmsPath); $civiUrl = rtrim($cmsUrl, '/') . '/' . ltrim($civiRelPath, ' /'); return array('url' => CRM_Utils_File::addTrailingSlash($civiUrl, '/'), 'path' => CRM_Utils_File::addTrailingSlash($civicrm_root)); }
/** * Evaluate a glob pattern in the context of a particular extension. * * @param string $ext * Extension name; use 'civicrm' for core. * @param string|array $patterns * Glob pattern; e.g. "*.html". * @param null|int $flags * See glob(). * @return array * List of matching files, relative to the extension base dir. * @see glob() */ public function glob($ext, $patterns, $flags = NULL) { $path = $this->getPath($ext); $patterns = (array) $patterns; $files = array(); foreach ($patterns as $pattern) { if (CRM_Utils_File::isAbsolute($pattern)) { // Absolute path. $files = array_merge($files, (array) glob($pattern, $flags)); } else { // Relative path. $files = array_merge($files, (array) glob("{$path}/{$pattern}", $flags)); } } sort($files); // Deterministic order. $files = array_unique($files); return array_map(function ($file) use($path) { return CRM_Utils_File::relativize($file, "{$path}/"); }, $files); }
/** * Scan $basedir for a list of extension-keys * * @return array * ($key => $relPath) */ protected function getRelPaths() { if (!is_array($this->relPaths)) { if ($this->cache) { $this->relPaths = $this->cache->get($this->cacheKey); } if (!is_array($this->relPaths)) { $this->relPaths = array(); $infoPaths = CRM_Utils_File::findFiles($this->baseDir, 'info.xml'); foreach ($infoPaths as $infoPath) { $relPath = CRM_Utils_File::relativize(dirname($infoPath), $this->baseDir); try { $info = CRM_Extension_Info::loadFromFile($infoPath); } catch (CRM_Extension_Exception_ParseException $e) { CRM_Core_Session::setStatus(ts('Parse error in extension: %1', array(1 => $e->getMessage())), '', 'error'); CRM_Core_Error::debug_log_message("Parse error in extension: " . $e->getMessage()); continue; } $this->relPaths[$info->key] = $relPath; } if ($this->cache) { $this->cache->set($this->cacheKey, $this->relPaths); } } } return $this->relPaths; }
/** * Search directory tree for files which match a glob pattern. * * Note: Dot-directories (like "..", ".git", or ".svn") will be ignored. * * @param string $dir * base dir. * @param string $pattern * glob pattern, eg "*.txt". * @param bool $relative * TRUE if paths should be made relative to $dir * @return array(string) */ public static function findFiles($dir, $pattern, $relative = FALSE) { if (!is_dir($dir)) { return array(); } $dir = rtrim($dir, '/'); $todos = array($dir); $result = array(); while (!empty($todos)) { $subdir = array_shift($todos); $matches = glob("{$subdir}/{$pattern}"); if (is_array($matches)) { foreach ($matches as $match) { if (!is_dir($match)) { $result[] = $relative ? CRM_Utils_File::relativize($match, "{$dir}/") : $match; } } } if ($dh = opendir($subdir)) { while (FALSE !== ($entry = readdir($dh))) { $path = $subdir . DIRECTORY_SEPARATOR . $entry; if ($entry[0] == '.') { // ignore } elseif (is_dir($path)) { $todos[] = $path; } } closedir($dh); } } return $result; }