@@ -106,40 +106,72 @@ public function getPath()
106106 */
107107 public function setPath ($ path , $ verifyFile = true , $ zip = null )
108108 {
109- if ($ verifyFile && preg_match ('~^data:image/[a-z]+;base64,~ ' , $ path ) !== 1 ) {
110- // Check if a URL has been passed. https://stackoverflow.com/a/2058596/1252979
111- if (filter_var ($ path , FILTER_VALIDATE_URL )) {
112- $ this ->path = $ path ;
113- // Implicit that it is a URL, rather store info than running check above on value in other places.
114- $ this ->isUrl = true ;
115- $ imageContents = file_get_contents ($ path );
109+ $ this ->isUrl = false ;
110+ if (preg_match ('~^data:image/[a-z]+;base64,~ ' , $ path ) === 1 ) {
111+ $ this ->path = $ path ;
112+
113+ return $ this ;
114+ }
115+
116+ $ this ->path = '' ;
117+ // Check if a URL has been passed. https://stackoverflow.com/a/2058596/1252979
118+ if (filter_var ($ path , FILTER_VALIDATE_URL )) {
119+ if (!preg_match ('/^(http|https|file|ftp|s3):/ ' , $ path )) {
120+ throw new PhpSpreadsheetException ('Invalid protocol for linked drawing ' );
121+ }
122+ // Implicit that it is a URL, rather store info than running check above on value in other places.
123+ $ this ->isUrl = true ;
124+ $ imageContents = @file_get_contents ($ path );
125+ if ($ imageContents !== false ) {
116126 $ filePath = tempnam (sys_get_temp_dir (), 'Drawing ' );
117127 if ($ filePath ) {
118- file_put_contents ($ filePath , $ imageContents );
119- if (file_exists ($ filePath )) {
120- $ this ->setSizesAndType ($ filePath );
128+ $ put = @file_put_contents ($ filePath , $ imageContents );
129+ if ($ put !== false ) {
130+ if ($ this ->isImage ($ filePath )) {
131+ $ this ->path = $ path ;
132+ $ this ->setSizesAndType ($ filePath );
133+ }
121134 unlink ($ filePath );
122135 }
123136 }
124- } elseif ( file_exists ( $ path )) {
125- $ this -> path = $ path ;
126- $ this -> setSizesAndType ( $ path );
127- } elseif ( $ zip instanceof ZipArchive) {
128- $ zipPath = explode ( ' # ' , $ path )[ 1 ];
129- if ($ zip -> locateName ( $ zipPath ) !== false ) {
137+ }
138+ } elseif ( $ zip instanceof ZipArchive) {
139+ $ zipPath = explode ( ' # ' , $ path )[ 1 ] ;
140+ $ locate = @ $ zip-> locateName ( $ zipPath );
141+ if ( $ locate !== false ) {
142+ if ($ this -> isImage ( $ path ) ) {
130143 $ this ->path = $ path ;
131144 $ this ->setSizesAndType ($ path );
132145 }
133- } else {
134- throw new PhpSpreadsheetException ("File $ path not found! " );
135146 }
136147 } else {
137- $ this ->path = $ path ;
148+ $ exists = @file_exists ($ path );
149+ if ($ exists !== false && $ this ->isImage ($ path )) {
150+ $ this ->path = $ path ;
151+ $ this ->setSizesAndType ($ path );
152+ }
153+ }
154+ if ($ this ->path === '' && $ verifyFile ) {
155+ throw new PhpSpreadsheetException ("File $ path not found! " );
138156 }
139157
140158 return $ this ;
141159 }
142160
161+ private function isImage (string $ path ): bool
162+ {
163+ $ mime = (string ) @mime_content_type ($ path );
164+ $ retVal = false ;
165+ if (str_starts_with ($ mime , 'image/ ' )) {
166+ $ retVal = true ;
167+ } elseif ($ mime === 'application/octet-stream ' ) {
168+ $ extension = pathinfo ($ path , PATHINFO_EXTENSION );
169+ $ retVal = in_array ($ extension , ['bin ' , 'emf ' ], true );
170+ }
171+
172+ return $ retVal ;
173+ }
174+
143175 /**
144176 * Get isURL.
145177 */
0 commit comments