@@ -215,40 +215,6 @@ function changingColumn(string $columnName, ...$datatype)
215215 return column (\CHANGER , $ columnName , ...$ datatype );
216216 }
217217
218- /**
219- * Creates self signed certificate
220- *
221- * @param string $privatekeyFile
222- * @param string $certificateFile
223- * @param string $signingFile
224- * // param string $caCertificate
225- * @param string $ssl_path
226- * @param array $details - certificate details
227- *
228- * Example:
229- * array $details = [
230- * "countryName" => '',
231- * "stateOrProvinceName" => '',
232- * "localityName" => '',
233- * "organizationName" => '',
234- * "organizationalUnitName" => '',
235- * "commonName" => '',
236- * "emailAddress" => ''
237- * ];
238- *
239- * @return string certificate path
240- */
241- function create_certificate (
242- string $ privatekeyFile = 'certificate.key ' ,
243- string $ certificateFile = 'certificate.crt ' ,
244- string $ signingFile = 'certificate.csr ' ,
245- // string $caCertificate = null,
246- string $ ssl_path = null ,
247- array $ details = ["commonName " => "localhost " ]
248- ) {
249- return ezQuery::createCertificate ($ privatekeyFile , $ certificateFile , $ signingFile , $ ssl_path , $ details );
250- }
251-
252218 /**
253219 * Creates an equality comparison expression with the given arguments.
254220 *
@@ -600,7 +566,119 @@ function clearInstance()
600566 */
601567 function clean_string (string $ string )
602568 {
603- return ezQuery::clean ($ string );
569+ $ patterns = array ( // strip out:
570+ '@<script[^>]*?>.*?</script>@si ' , // Strip out javascript
571+ '@<[\/\!]*?[^<>]*?>@si ' , // HTML tags
572+ '@<style[^>]*?>.*?</style>@siU ' , // Strip style tags properly
573+ '@<![\s\S]*?--[ \t\n\r]*>@ ' // Strip multi-line comments
574+ );
575+
576+ $ string = \preg_replace ($ patterns , '' , $ string );
577+ $ string = \trim ($ string );
578+ $ string = \stripslashes ($ string );
579+
580+ return \htmlentities ($ string );
581+ }
582+
583+ /**
584+ * Check if path/filename is directory traversal attack.
585+ *
586+ * @param string $basePath base directory to check against
587+ * @param string $filename will be preprocess with `sanitize_path()`
588+ * @return boolean
589+ */
590+ function is_traversal (string $ basePath , string $ filename )
591+ {
592+ if (\strpos (\urldecode ($ filename ), '.. ' ) !== false )
593+ return true ;
594+
595+ $ realBase = \rtrim (\realpath ($ basePath ), _DS );
596+ $ userPath = $ realBase . _DS . sanitize_path ($ filename );
597+ $ realUserPath = \realpath ($ userPath );
598+ // Reassign with un-sanitized if file does not exits
599+ if ($ realUserPath === false )
600+ $ realUserPath = $ filename ;
601+
602+ return (\strpos ($ realUserPath , $ realBase ) !== 0 );
603+ }
604+
605+ /**
606+ * Sanitize path to prevent directory traversal.
607+ *
608+ * Example:
609+ *
610+ * `sanitize_path("../../../../config.php");`
611+ *
612+ * Returns `config.php` without the path traversal
613+ * @param string $path
614+ * @return string
615+ */
616+ function sanitize_path (string $ path )
617+ {
618+ $ file = \preg_replace ("/\.[\.]+/ " , "" , $ path );
619+ $ file = \preg_replace ("/^[\/]+/ " , "" , $ file );
620+ $ file = \preg_replace ("/^[A-Za-z][:\|][\/]?/ " , "" , $ file );
621+ return ($ file );
622+ }
623+
624+ /**
625+ * Creates self signed certificate
626+ *
627+ * @param string $privatekeyFile
628+ * @param string $certificateFile
629+ * @param string $signingFile
630+ * // param string $caCertificate
631+ * @param string $ssl_path
632+ * @param array $details - certificate details
633+ *
634+ * Example:
635+ * array $details = [
636+ * "countryName" => '',
637+ * "stateOrProvinceName" => '',
638+ * "localityName" => '',
639+ * "organizationName" => '',
640+ * "organizationalUnitName" => '',
641+ * "commonName" => '',
642+ * "emailAddress" => ''
643+ * ];
644+ *
645+ * @return string certificate path
646+ */
647+ function create_certificate (
648+ string $ privatekeyFile = 'certificate.key ' ,
649+ string $ certificateFile = 'certificate.crt ' ,
650+ string $ signingFile = 'certificate.csr ' ,
651+ // string $caCertificate = null,
652+ string $ ssl_path = null ,
653+ array $ details = ["commonName " => "localhost " ]
654+ ) {
655+ if (empty ($ ssl_path )) {
656+ $ ssl_path = \getcwd ();
657+ $ ssl_path = \preg_replace ('/ \\\/ ' , \_DS , $ ssl_path ) . \_DS ;
658+ } else
659+ $ ssl_path = $ ssl_path . \_DS ;
660+
661+ $ opensslConfig = array ("config " => $ ssl_path . 'openssl.cnf ' );
662+
663+ // Generate a new private (and public) key pair
664+ $ privatekey = \openssl_pkey_new ($ opensslConfig );
665+
666+ // Generate a certificate signing request
667+ $ csr = \openssl_csr_new ($ details , $ privatekey , $ opensslConfig );
668+
669+ // Create a self-signed certificate valid for 365 days
670+ $ sslcert = \openssl_csr_sign ($ csr , null , $ privatekey , 365 , $ opensslConfig );
671+
672+ // Create key file. Note no passphrase
673+ \openssl_pkey_export_to_file ($ privatekey , $ ssl_path . $ privatekeyFile , null , $ opensslConfig );
674+
675+ // Create server certificate
676+ \openssl_x509_export_to_file ($ sslcert , $ ssl_path . $ certificateFile , false );
677+
678+ // Create a signing request file
679+ \openssl_csr_export_to_file ($ csr , $ ssl_path . $ signingFile );
680+
681+ return $ ssl_path ;
604682 }
605683
606684 /**
0 commit comments