Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / hacked / src / hackedProjectWebFilesDownloader.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\hacked\hackedProjectWebFilesDownloader.
6  */
7
8 namespace Drupal\hacked;
9
10 use Drupal\Component\Utility\Unicode;
11 use Exception;
12
13 /**
14  * Downloads a project using a standard Drupal method.
15  */
16 class hackedProjectWebFilesDownloader extends hackedProjectWebDownloader {
17   function download_link() {
18     if (!empty($this->project->project_info['releases'][$this->project->existing_version])) {
19       $this_release = $this->project->project_info['releases'][$this->project->existing_version];
20       return $this_release['download_link'];
21     }
22   }
23
24   function download() {
25     $dir = $this->get_destination();
26     if (!($release_url = $this->download_link())) {
27       return FALSE;
28     }
29
30     // If our directory already exists, we can just return the path to this cached version
31     if (file_exists($dir) && count(hacked_file_scan_directory($dir, '/.*/', [
32         '.',
33         '..',
34         'CVS',
35         '.svn',
36         '.git'
37       ]))
38     ) {
39       return $dir;
40     }
41
42     // Build the destination folder tree if it doesn't already exists.
43     if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY) && !mkdir($dir, 0775, TRUE)) {
44       $message = $this->t('Failed to create temp directory: %dir', ['%dir' => $dir]);
45       \Drupal::logger('hacked')->error($message->render());
46       return FALSE;
47     }
48
49     if (!($local_file = $this->file_get($release_url))) {
50       $message = $this->t('Could not download the project: @name from URL: @url', [
51         '@name' => $this->project->title(),
52         '@url'  => $release_url
53       ]);
54       \Drupal::logger('hacked')->error($message->render());
55       return FALSE;
56     }
57     try {
58       $this->archive_extract($local_file, $dir);
59     }
60     catch (Exception $e) {
61       $message = $this->t('Could not extract the project: @name. Error was: !error', [
62         '@name'  => $this->project->title(),
63         '!error' => $e->getMessage()
64       ]);
65       \Drupal::logger('hacked')->error($message->render());
66       return FALSE;
67     }
68
69     return TRUE;
70   }
71
72   /**
73    * Copies a file from $url to the temporary directory for updates.
74    *
75    * If the file has already been downloaded, returns the the local path.
76    *
77    * @param $url
78    *   The URL of the file on the server.
79    *
80    * @return string
81    *   Path to local file.
82    */
83   function file_get($url) {
84     $parsed_url = parse_url($url);
85     $remote_schemes = ['http', 'https', 'ftp', 'ftps', 'smb', 'nfs'];
86     if (!in_array($parsed_url['scheme'], $remote_schemes)) {
87       // This is a local file, just return the path.
88       return \Drupal::service('file_system')->realpath($url);
89     }
90
91     // Check the cache and download the file if needed.
92     $cache_directory = 'temporary://hacked-cache';
93     $local = $cache_directory . '/' . basename($parsed_url['path']);
94
95     if (!file_exists($cache_directory)) {
96       mkdir($cache_directory);
97     }
98
99     return system_retrieve_file($url, $local, FALSE, FILE_EXISTS_REPLACE);
100   }
101
102   /**
103    * Unpack a downloaded archive file.
104    *
105    * @param string $file
106    *   The filename of the archive you wish to extract.
107    * @param string $directory
108    *   The directory you wish to extract the archive into.
109    * @return Archiver
110    *   The Archiver object used to extract the archive.
111    * @throws Exception on failure.
112    */
113   function archive_extract($file, $directory) {
114     $archiver = archiver_get_archiver($file);
115     if (!$archiver) {
116       throw new Exception(t('Cannot extract %file, not a valid archive.', ['%file' => $file]));
117     }
118
119     // Remove the directory if it exists, otherwise it might contain a mixture of
120     // old files mixed with the new files (e.g. in cases where files were removed
121     // from a later release).
122     $files = $archiver->listContents();
123     // Unfortunately, we can only use the directory name for this. :(
124     $project = Unicode::substr($files[0], 0, -1);
125     $extract_location = $directory . '/' . $project;
126     if (file_exists($extract_location)) {
127       file_unmanaged_delete_recursive($extract_location);
128     }
129
130     $archiver->extract($directory);
131     return $archiver;
132   }
133
134
135 }