|
| 1 | +/* |
| 2 | + * Copyright (c) 2021-present Fabien Potencier <fabien@symfony.com> |
| 3 | + * |
| 4 | + * This file is part of Symfony CLI project |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as |
| 8 | + * published by the Free Software Foundation, either version 3 of the |
| 9 | + * License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +package php |
| 21 | + |
| 22 | +import ( |
| 23 | +"fmt" |
| 24 | +"github.com/pkg/errors" |
| 25 | +"github.com/rs/zerolog" |
| 26 | +"github.com/symfony-cli/symfony-cli/util" |
| 27 | +"io" |
| 28 | +"net/http" |
| 29 | +"os" |
| 30 | +"path/filepath" |
| 31 | +"strings" |
| 32 | +) |
| 33 | + |
| 34 | +typePieResultstruct { |
| 35 | +codeint |
| 36 | +errorerror |
| 37 | +} |
| 38 | + |
| 39 | +func (pPieResult)Error()string { |
| 40 | +ifp.error!=nil { |
| 41 | +returnp.error.Error() |
| 42 | +} |
| 43 | + |
| 44 | +return"" |
| 45 | +} |
| 46 | + |
| 47 | +func (pPieResult)ExitCode()int { |
| 48 | +returnp.code |
| 49 | +} |
| 50 | + |
| 51 | +funcPie(dirstring,args,env []string,stdout,stderr,logger io.Writer,debugLogger zerolog.Logger)PieResult { |
| 52 | +e:=&Executor{ |
| 53 | +Dir:dir, |
| 54 | +BinName:"php", |
| 55 | +Stdout:stdout, |
| 56 | +Stderr:stderr, |
| 57 | +SkipNbArgs:-1, |
| 58 | +ExtraEnv:env, |
| 59 | +Logger:debugLogger, |
| 60 | +} |
| 61 | + |
| 62 | +ifpiePath:=os.Getenv("SYMFONY_PIE_PATH");piePath!="" { |
| 63 | +debugLogger.Debug().Str("SYMFONY_PIE_PATH",piePath).Msg("SYMFONY_PIE_PATH has been defined. User is taking control over PIE detection and execution.") |
| 64 | +e.Args=append([]string{piePath},args...) |
| 65 | +}elseifpath,err:=e.findPie();err==nil&&assertIsPHPScript(path) { |
| 66 | +e.Args=append([]string{"php",path},args...) |
| 67 | +}else { |
| 68 | +reason:="No PIE installation found." |
| 69 | +ifpath!="" { |
| 70 | +reason=fmt.Sprintf("Detected PIE file (%s) is not a valid PHAR or PHP script.",path) |
| 71 | +} |
| 72 | +fmt.Fprintln(logger," WARNING:",reason) |
| 73 | +fmt.Fprintln(logger," Downloading PIE for you, but it is recommended to install PIE yourself, instructions available at https://github.com/php/pie") |
| 74 | +// we don't store it under bin/ to avoid it being found by findPie as we want to only use it as a fallback |
| 75 | +binDir:=filepath.Join(util.GetHomeDir(),"pie") |
| 76 | +ifpath,err=downloadPie(binDir);err!=nil { |
| 77 | +returnPieResult{ |
| 78 | +code:1, |
| 79 | +error:errors.Wrap(err,"unable to find pie, get it at https://github.com/php/pie"), |
| 80 | +} |
| 81 | +} |
| 82 | +e.Args=append([]string{"php",path},args...) |
| 83 | +fmt.Fprintf(logger," (running %s)\n\n",e.CommandLine()) |
| 84 | +} |
| 85 | + |
| 86 | +ret:=e.Execute(false) |
| 87 | +ifret!=0 { |
| 88 | +returnPieResult{ |
| 89 | +code:ret, |
| 90 | +error:errors.Errorf("unable to run %s",e.CommandLine()), |
| 91 | +} |
| 92 | +} |
| 93 | +returnPieResult{} |
| 94 | +} |
| 95 | + |
| 96 | +funcfindPie(logger zerolog.Logger) (string,error) { |
| 97 | +for_,file:=range []string{"pie","pie.phar"} { |
| 98 | +logger.Debug().Str("source","PIE").Msgf(`Looking for PIE in the PATH as "%s"`,file) |
| 99 | +ifpharPath,_:=LookPath(file);pharPath!="" { |
| 100 | +// On Windows, we don't want the .bat, but the real pie phar/PHP file |
| 101 | +ifstrings.HasSuffix(pharPath,".bat") { |
| 102 | +pharPath=pharPath[:len(pharPath)-4]+".phar" |
| 103 | +} |
| 104 | +logger.Debug().Str("source","PIE").Msgf(`Found potential PIE as "%s"`,pharPath) |
| 105 | +returnpharPath,nil |
| 106 | +} |
| 107 | +} |
| 108 | + |
| 109 | +return"",os.ErrNotExist |
| 110 | +} |
| 111 | + |
| 112 | +funcdownloadPie(dirstring) (string,error) { |
| 113 | +iferr:=os.MkdirAll(dir,0755);err!=nil { |
| 114 | +return"",err |
| 115 | +} |
| 116 | +path:=filepath.Join(dir,"pie.phar") |
| 117 | +if_,err:=os.Stat(path);err==nil { |
| 118 | +returnpath,nil |
| 119 | +} |
| 120 | + |
| 121 | +piePhar,err:=downloadPiePhar() |
| 122 | +iferr!=nil { |
| 123 | +return"",err |
| 124 | +} |
| 125 | + |
| 126 | +err=os.WriteFile(path,piePhar,0755) |
| 127 | +iferr!=nil { |
| 128 | +return"",err |
| 129 | +} |
| 130 | + |
| 131 | +returnpath,nil |
| 132 | +} |
| 133 | + |
| 134 | +funcdownloadPiePhar() ([]byte,error) { |
| 135 | +resp,err:=http.Get("https://github.com/php/pie/releases/latest/download/pie.phar") |
| 136 | +iferr!=nil { |
| 137 | +returnnil,err |
| 138 | +} |
| 139 | +deferresp.Body.Close() |
| 140 | +returnio.ReadAll(resp.Body) |
| 141 | +} |