forked from pocketarc/git-deploy-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-deploy
758 lines (635 loc) · 21.9 KB
/
git-deploy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
#!/usr/bin/php
<?php
/*
* @todo log all output in file
* @todo command to set file
* @todo command to set whether to print or not
* @todo command to ignore arbitrary files
* @todo command to update to specific revision (including previous, see issue #8)
* @todo see if it ends in .ini, and if it does, don't append .ini
*/
define('GIT_DEPLOY_DIR', __DIR__ . '/.gitdeploy');
define('VENDOR_DIR', __DIR__ . '/vendor');
ini_set('max_execution_time', 0);
$opts = getopt("lr:", ["revert", 'env:']);
$list = isset($opts['l']);
$revision = isset($opts['r']) ? $opts['r'] : 'HEAD';
$revert = isset($opts['revert']);
$deploy = isset($opts['env']) ? $opts['env'] : 'deploy.ini';
$deploy = GIT_DEPLOY_DIR . '/' . $deploy;
if (substr($deploy, -4) !== '.ini') {
$deploy .= '.ini';
}
/**
* Get local path to file containing revision deployed to server.
* @global string $deploy
* @return string
*/
function getRemoteRevisionLocalFilepath(): string
{
global $deploy;
return GIT_DEPLOY_DIR . '/REMOTE_REVISION-' . basename($deploy, '.ini');
}
/**
* Add all filepaths from $relPath to $arr
* @param string $relPath
* @param array $arr Array to put file list to
*/
function addFilesFromDirectory(string $relPath, array &$arr): void
{
$absPath = __DIR__ . '/' . $relPath;
foreach (get_recursive_file_list($absPath, $relPath) as $f) {
$arr[$f] = $f;
}
}
function get_recursive_file_list(string $folder, string $prefix = ''): array
{
# Add trailing slash
$folder = (substr($folder, strlen($folder) - 1, 1) == '/') ? $folder : $folder . '/';
$return = [];
foreach (clean_scandir($folder) as $file) {
if (is_dir($folder . $file)) {
$return = array_merge($return, get_recursive_file_list($folder . $file, $prefix . $file . '/'));
} else {
$return[] = $prefix . $file;
}
}
return $return;
}
function clean_scandir(string $folder, array $ignore = []): array
{
$ignore[] = '.';
$ignore[] = '..';
$ignore[] = '.DS_Store';
$return = [];
foreach (scandir($folder) as $file) {
if (!in_array($file, $ignore)) {
$return[] = $file;
}
}
return $return;
}
try {
$git = new Terra_Git();
if (!$list) {
$git->setDeployFile($deploy);
if ($revert) {
$git->revert();
} else {
$git->deploy($revision);
}
} else {
$remoteRevisionLocalFilePath = getRemoteRevisionLocalFilepath();
if (file_exists($remoteRevisionLocalFilePath)) {
$remoteRevision = trim(file_get_contents($remoteRevisionLocalFilePath));
} else {
$remoteRevision = NULL;
}
$git->setDeployFile($deploy);
$servers = $git->getServers();
$serverOptions = reset($servers);
$files = $git->getFilesToUpload($remoteRevision, $revision, $serverOptions);
$git->output("Update from $remoteRevision to $revision");
if (count($files['upload']) === 0 && count($files['delete']) === 0) {
$git->output('No files to upload or delete.');
} else {
if (count($files['upload']) > 0) {
$git->output("Files to upload:");
foreach ($files['upload'] as $fileToUpload) {
$git->output($fileToUpload);
}
}
if (count($files['delete']) > 0) {
$git->output("Files to delete:");
foreach ($files['delete'] as $fileToDelete) {
$git->output($fileToDelete);
}
}
}
}
} catch (Exception $e) {
echo $e->getMessage();
}
class Terra_Git
{
protected array $filesToIgnore = [
'git-deploy',
'.gitignore',
];
protected bool $print = true;
protected string $logBuffer = '';
protected string $logFile = 'git-deploy.log.txt';
protected array $submodules = [];
protected array $servers = [];
protected array $pathsThatExist = [];
function __construct()
{
# Check if there are any submodules in the git repository.
$command = "git submodule status";
$output = [];
exec($command, $output);
foreach ($output as $line) {
$line = explode(' ', $line);
$this->submodules[] = $line[1];
}
}
function setDeployFile(string $deploy): void
{
if (!@file_exists($deploy)) {
throw new Exception("File '$deploy' does not exist.");
} else {
$servers = parse_ini_file($deploy, true);
if (!$servers) {
throw new Exception("File '$deploy' is not a valid .ini file.");
} else {
$this->addServers($servers, basename($deploy, '.ini'));
}
}
$this->ignoreFile($deploy);
}
function getServers(): array
{
return $this->servers;
}
function addServers(array $servers, string $env): void
{
foreach ($servers as $uri => $options) {
if (substr($uri, 0, 6) === 'ftp://') {
$options = array_merge($options, parse_url($uri));
}
# Throw in some default values, in case they're not set.
$options = array_merge([
'skip' => false,
'host' => '',
'user' => '',
'pass' => '',
'port' => 21,
'path' => '/',
'passive' => true,
'maintenanceMode' => true,
'indexPath' => 'web/index.php',
'maintenancePath' => 'web/.maintenance.php',
'gitDeployDirectory' => '',
'clean_directories' => [],
'after' => [],
'file_permissions' => [],
'substitutions' => [],
'env' => $env,
'uploadComposerLibs' => false,
], $options);
if ($options['skip']) {
continue;
} else {
$this->servers[$uri] = $options;
}
}
}
function ignoreFile(string $file): void
{
$this->filesToIgnore[] = $file;
}
/**
* Filter out ignored files & files not placed inside gitDeployDirectory if not empty.
* @param array $files
* @param string $gitDeployDir
* @return array
*/
function filterFiles(array $files, string $gitDeployDir): array
{
foreach ($files as $key => $file) {
if (
in_array($file, $this->filesToIgnore)
// exclude all but gitDeployDirectory files
|| !empty($gitDeployDir && strpos($file, $gitDeployDir) !== 0)
) {
unset($files[$key]);
}
}
return $files;
}
function getFilesToUpload(?string $oldRevision = null, string $newRevision = 'HEAD', array $serverOptions = []): array
{
$gitDeployDir = $serverOptions['gitDeployDirectory'];
# Get the list of files to update.
if (!empty($oldRevision)) {
$command = "git diff --name-status {$oldRevision} {$newRevision}";
} else {
$command = "git ls-files";
}
$output = [];
exec($command, $output);
$filesToUpload = [];
$filesToDelete = [];
if (!empty($oldRevision)) {
foreach ($output as $line) {
$path = trim(substr($line, 1, strlen($line)));
if ($line[0] == 'A' or $line[0] == 'C' or $line[0] == 'M') {
$filesToUpload[] = $path;
} elseif ($line[0] == 'D') {
$filesToDelete[] = $path;
// rename, score 100
} elseif ($line[0] == 'R') {
$chunks = explode("\t", $line);
$filesToDelete[] = $chunks[1];
$filesToUpload[] = $chunks[2];
} else {
throw new Exception("Unknown git-diff status: {$line[0]}");
}
}
} else {
$filesToUpload = $output;
}
$filesToUpload = $this->filterFiles($filesToUpload, $gitDeployDir);
$filesToDelete = $this->filterFiles($filesToDelete, $gitDeployDir);
$substitutions = $serverOptions['substitutions'];
if ($substitutions) {
$filesToUploadRemotePaths = preg_replace(array_keys($substitutions), array_values($substitutions), $filesToUpload);
$filesToDeleteRemotePaths = preg_replace(array_keys($substitutions), array_values($substitutions), $filesToDelete);
} else {
$filesToUploadRemotePaths = $filesToUpload;
$filesToDeleteRemotePaths = $filesToDelete;
}
return [
'upload' => array_combine($filesToUpload, $filesToUploadRemotePaths),
'delete' => array_combine($filesToDelete, $filesToDeleteRemotePaths),
];
}
function output(string $message): void
{
if ($this->print) {
echo $message . "\r\n";
} else {
$this->logBuffer .= $message . "\r\n";
}
}
function __destruct()
{
}
function revert(): void
{
foreach ($this->servers as $server) {
$this->deployGitOverFtp($server, 'HEAD', true);
}
}
function deploy(string $revision = 'HEAD'): void
{
foreach ($this->servers as $server) {
$this->deployGitOverFtp($server, $revision);
}
}
function ftp_mkdir($connection, string $file): bool
{
$dir = explode('/', dirname($file));
$path = '';
for ($i = 0; $i < count($dir); $i++) {
$path .= $dir[$i] . '/';
if (!isset($this->pathsThatExist[$path])) {
$origin = ftp_pwd($connection);
if (!@ftp_chdir($connection, $path)) {
if (!@ftp_mkdir($connection, $path)) {
$this->output("Failed to create '$path'.");
return false;
} else {
$this->output("Created directory '$path'.");
$this->pathsThatExist[$path] = true;
}
} else {
$this->pathsThatExist[$path] = true;
}
ftp_chdir($connection, $origin);
}
}
return true;
}
function deployGitOverFtp(array $server, string $revision = 'HEAD', bool $revert = false): void
{
if ($revision == 'HEAD') {
$revision = exec('git rev-parse HEAD');
}
# Let's make sure the $path ends with a slash.
if (substr($server['path'], -1) !== '/') {
$server['path'] = $server['path'] . '/';
}
# Okay, let's connect to the server.
$connection = @ftp_connect($server['host'], $server['port']);
if (!$connection) {
throw new Exception("Could not connect to {$server['host']}.");
} else {
if (!@ftp_login($connection, $server['user'], $server['pass'])) {
throw new Exception("Could not login to {$server['host']} (Tried to login as {$server['user']}).");
}
ftp_pasv($connection, $server['passive']);
if (ftp_chdir($connection, $server['path'])) {
} else {
throw new Exception("Could not change the FTP directory to {$server['path']}.");
}
$this->output(("----------------------------"));
$this->output("Connected to {$server['host']}{$server['path']}");
$this->output("----------------------------");
# Now that we're logged in to the server, let's get the remote revision.
$remoteRevision = $this->getRemoteRevisionContent($connection, 'REVISION');
$previousRevision = $this->getRemoteRevisionContent($connection, 'PREVIOUS_REVISION');
$isFirstTimeDeploy = $remoteRevision === NULL;
// clean working directory is required for first-time deploy to avoid massive file renaming while uploading (see below)
if ($isFirstTimeDeploy) {
$output = [];
exec('git status --porcelain', $output);
if (!empty($output)) {
$this->output('First-time deployment requires clean working directory! Clean it please and retry!');
exit;
}
}
if ($revert) {
if (!isset($previousRevision)) {
throw new Exception("Could not find previous revision in {$server['host']}, which means that reverting is not possible.");
}
$this->output("WARNING: Composer-managed libraries will NOT be reverted!");
# Set revision to PREVIOUS_REVISION, for reverting.
$revision = $previousRevision;
if ($remoteRevision != $revision) {
$this->output("Reverting from " . substr($remoteRevision, 0, 6) . " to " . substr($revision, 0, 6) . ".");
$this->output("----------------------------");
}
} else {
if ($remoteRevision && $remoteRevision != $revision) {
$this->output("Deploying update from " . substr($remoteRevision, 0, 6) . " to " . substr($revision, 0, 6) . ".");
$this->output("----------------------------");
}
}
$files = $this->getFilesToUpload($remoteRevision, $revision, $server);
$filesToUpload = $files['upload'];
$filesToDelete = $files['delete'];
unset($files);
$composerLockPath = __DIR__ . '/composer.lock';
$envComposerLockPath = GIT_DEPLOY_DIR . '/' . $server['env'] . '.composer.lock';
if ($isFirstTimeDeploy) {
// paths are relative to root
$firstTimeDeployFiles = [
'app/config/user_pass.neon' => 'app/config/user_pass.neon',
];
if ($server['uploadComposerLibs']) {
// upload composer-managed packages
$dirs = ['vendor/'];
foreach ($dirs as $relPath) {
$absPath = __DIR__ . '/' . $relPath;
if (is_dir($absPath)) {
foreach (get_recursive_file_list($absPath, $relPath) as $f) {
// skip git-tracked vendor/others directory
if (strpos($f, 'vendor/others/') !== 0) {
$firstTimeDeployFiles[$f] = $f;
}
}
}
}
}
$filesToUpload = array_merge($filesToUpload, $firstTimeDeployFiles);
} else {
// check if composer-managed libs have changed
// we handle only new/updated libraries (to upload), not removed ones (to remove)
$composerChangedFiles = [];
$composerLockFileName = $server['gitDeployDirectory'] . 'composer.lock';
if (isset($filesToUpload[$composerLockFileName])) {
if (!file_exists($envComposerLockPath)) {
throw new Exception('Env-specific composer.lock file is missing -> cannot update composer-managed files');
}
if (!file_exists($composerLockPath)) {
throw new Exception('`composer.lock` file is missing -> cannot update composer-managed files');
}
// load versioned composer.lock
$command = "git show $revision:$composerLockFileName";
$output = [];
exec($command, $output);
$localComposerLockJson = json_decode(join('', $output));
// var_dump($localComposerLockJson);die;
$workingLocalComposerLockJson = json_decode(file_get_contents($composerLockPath));
if ($workingLocalComposerLockJson->{'content-hash'} !== $localComposerLockJson->{'content-hash'}) {
$this->output('There are uncommitted changes in `composer.lock` file so it is not safe to deploy now (we could upload packages with other versions than those in composer.lock'
. "\nPlease commit it and try again.");
exit;
}
$deployedComposerLockJson = json_decode(file_get_contents($envComposerLockPath));
$localPackages = $localComposerLockJson->packages;
$deployedPackages = $deployedComposerLockJson->packages;
if ($localComposerLockJson->{'content-hash'} !== $deployedComposerLockJson->{'content-hash'}) {
if (!$server['uploadComposerLibs']) {
$this->output('A change in composer managed libraries detected..Please run `composer install` on server.');
} else {
/**
* Has package version changed?
* @param string $name package name
* @param string $version current package version
* @param string $deployedVersion deployed package version to be set
* @return bool
*/
$hasPackageChanged = function ($name, $version, &$deployedVersion = NULL) use ($deployedPackages) {
foreach ($deployedPackages as $p) {
if ($p->name === $name) {
$deployedVersion = $p->version;
if ($p->version === $version) {
return false;
}
}
}
return true;
};
$composerLibChanged = false;
foreach ($localComposerLockJson->packages as $p) {
$deployedVersion = NULL;
if ($hasPackageChanged($p->name, $p->version, $deployedVersion)) {
$composerLibChanged = true;
addFilesFromDirectory('vendor/' . $p->name . '/', $composerChangedFiles);
$this->output("Composer: <$p->name> changed from <$deployedVersion> to <$p->version>");
}
}
// if changed, copy composer/ & autoload.php
if ($composerLibChanged) {
addFilesFromDirectory('vendor/composer/', $composerChangedFiles);
$composerChangedFiles['vendor/autoload.php'] = 'vendor/autoload.php';
}
$filesToUpload = array_merge($filesToUpload, $composerChangedFiles);
}
} else {
$this->output('No change in composer managed libraries detected..');
}
}
}
$maintenanceModeTurnedOn = false;
// turn on "Maintenance mode" before uploading
if (
$server['maintenanceMode'] && !$isFirstTimeDeploy && (count($filesToUpload) > 0 or count($filesToDelete) > 0)
) {
$indexPath = $server['indexPath'];
$maintenancePath = $server['maintenancePath'];
$tmpIndexPath = $indexPath . '.tmp';
// alternatively we could just ftp_put index.php to index.php.tmp to avoid possible error of missing index.php file
// after indexPath renamed to tmpIndexPath but maintenancePath not yet renamed to indexPath
ftp_rename($connection, $indexPath, $tmpIndexPath);
ftp_rename($connection, $maintenancePath, $indexPath);
$this->output('Maintenance mode turned ON');
$maintenanceModeTurnedOn = true;
}
// most of the time $localFile === $remoteFile - $remoteFile just defines path where $localFile gets uploaded
foreach ($filesToUpload as $localFile => $remoteFile) {
# Make sure the folder exists in the FTP server.
$ret = $this->ftp_mkdir($connection, $remoteFile);
if ($ret === false) {
$this->output("A problem occurred while attempting to create a folder. Upload to this server cannot continue.");
return;
}
// first time we upload files as they are (working directory is clean)
// same for composerChangedFiles as they are not tracked with git
if ($isFirstTimeDeploy || isset($composerChangedFiles[$localFile])) {
$unlinkTmpFile = FALSE;
$tmpFileToUpload = $localFile;
// other times we upload tmp file with content of $revision
} else {
$unlinkTmpFile = TRUE;
$tmpFileToUpload = GIT_DEPLOY_DIR . '/tmpFileToUpload';
$command = "git show $revision:$localFile > $tmpFileToUpload";
$output = [];
exec($command, $output);
}
$uploaded = false;
$attempts = 1;
while (!$uploaded) {
if ($attempts == 10) {
$this->output("Tried to upload $localFile 10 times, and failed 10 times. Something is wrong, so I'm going to stop executing now.");
exit;
}
# This makes sure to repeat the upload until it finally succeeds.
# Hopefully helps fix a problem where some files aren't transferred.
# Transfer js [& css] files using ASCII mode for proper end lines for Packer to work properly until the Packer fix https://github.com/tholu/php-packer/issues/15
$ext = strtolower(pathinfo($remoteFile, PATHINFO_EXTENSION));
$ftpMode = in_array($ext, ['js', /*'css', 'html', 'latte', 'php'*/]) ? FTP_ASCII : FTP_BINARY;
$uploaded = @ftp_put($connection, $remoteFile, $tmpFileToUpload, $ftpMode);
if (!$uploaded) {
$attempts = $attempts + 1;
$this->output("Failed to upload {$localFile}. Retrying (attempt $attempts/10)... ");
}
}
$this->output("Uploaded {$localFile}");
if ($unlinkTmpFile) {
unlink($tmpFileToUpload);
}
}
foreach ($filesToDelete as $file) {
@ftp_delete($connection, $file);
$this->output("Deleted {$file}");
}
if (!empty($server['clean_directories'])) {
foreach ($server['clean_directories'] as $dir) {
$this->ftp_rmdirr($connection, $dir);
$this->output("Emptied {$dir}");
}
} else {
$this->output("\r\n*** Don't forget to empty cache if needed! ***\r\n");
}
if (count($filesToUpload) > 0 or count($filesToDelete) > 0) {
$this->output("----------------------------");
$temp = tempnam(sys_get_temp_dir(), 'gitRevision');
file_put_contents($temp, $revision);
ftp_put($connection, 'REVISION', $temp, FTP_ASCII);
unlink($temp);
$this->output("Uploaded REVISION file.");
file_put_contents(getRemoteRevisionLocalFilepath(), $revision);
$this->output("Stored local REMOTE_REVISION file.");
# Upload file containing the current revision, for reverting when necessary.
# But, only if the revision we're deploying is different from the one currently deployed.
if ($remoteRevision != $revision) {
$temp = tempnam(sys_get_temp_dir(), 'gitRevision');
file_put_contents($temp, $remoteRevision);
ftp_put($connection, 'PREVIOUS_REVISION', $temp, FTP_ASCII);
unlink($temp);
$this->output("Uploaded PREVIOUS_REVISION file.");
}
// update env-specific composer.lock file
if (file_exists($composerLockPath)) {
copy($composerLockPath, $envComposerLockPath);
}
} else {
$this->output("No files to upload.");
}
# when uploading done turn off "Maintenance mode"
if ($maintenanceModeTurnedOn) {
ftp_rename($connection, $indexPath, $maintenancePath);
ftp_rename($connection, $tmpIndexPath, $indexPath);
$this->output("Maintenance mode turned OFF");
}
if (!empty($server['file_permissions'])) {
$this->setFilePermissions($connection, $server['file_permissions']);
}
if (!empty($server['after'])) {
foreach ($server['after'] as $url) {
fopen($url, 'r');
$this->output("$url called");
}
}
$this->output("----------------------------");
$this->output("Finished working on {$server['host']}{$server['path']}");
$this->output("----------------------------");
ftp_close($connection);
}
}
/**
* Clean (and optionally remove) whole directory recursively
* @link http://www.php.net/manual/en/function.ftp-nlist.php#62306
*/
function ftp_rmdirr($handle, string $path, bool $rmDir = false, int $level = 1): bool
{
if (!@ftp_delete($handle, $path)) {
$this->output("Cleaning: $path");
$list = @ftp_nlist($handle, $path);
if (!empty($list)) {
foreach ($list as $value) {
if (in_array(basename($value), ['.', '..'])) {
continue;
}
// $this->ftp_rmdirr($handle, $value, true, $level + 1);
$this->ftp_rmdirr($handle, $path . '/' . basename($value), true, $level + 1);
}
}
}
if ($rmDir && $level > 1) {
if (@ftp_rmdir($handle, $path))
return true;
else
return false;
}
return true;
}
/**
* Set 0777 file permissions for $items (create them if not present)
* @param ftp_handle
* @param array of files/directories to chmod
*/
function setFilePermissions($connection, array $items = []): void
{
$mode = 0777;
foreach ($items as $path) {
if (!@ftp_chmod($connection, $mode, $path)) {
ftp_mkdir($connection, $path);
ftp_chmod($connection, $mode, $path);
}
}
$this->output('Permissions set.');
}
/**
* Get content of remote revision file.
* @param ftp_handle
* @param string $filename
* @return ?string
*/
function getRemoteRevisionContent($connection, string $filename = 'REVISION'): ?string
{
$tmpFile = tmpfile();
$ret = NULL;
if (@ftp_fget($connection, $tmpFile, $filename, FTP_ASCII)) {
fseek($tmpFile, 0);
$ret = trim(fread($tmpFile, 1024));
fclose($tmpFile);
} else {
# Couldn't get the file. I assume it's because the file didn't exist.
}
return $ret;
}
}