diff options
Diffstat (limited to 'www/wiki/extensions/SemanticMediaWiki/src/Utils/TempFile.php')
-rw-r--r-- | www/wiki/extensions/SemanticMediaWiki/src/Utils/TempFile.php | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/www/wiki/extensions/SemanticMediaWiki/src/Utils/TempFile.php b/www/wiki/extensions/SemanticMediaWiki/src/Utils/TempFile.php new file mode 100644 index 00000000..518f624d --- /dev/null +++ b/www/wiki/extensions/SemanticMediaWiki/src/Utils/TempFile.php @@ -0,0 +1,69 @@ +<?php + +namespace SMW\Utils; + +use RuntimeException; + +/** + * @license GNU GPL v2+ + * @since 3.0 + * + * @author mwjames + */ +class TempFile extends File { + + /** + * @since 3.0 + * + * @return string + * @throws RuntimeException + */ + public function generate() { + + $args = func_get_args(); + $key = array_shift( $args ); + + if ( $args === [] ) { + $key = ''; + } + + return $this->get( + $key . substr( base_convert( md5( json_encode( $args ) ), 16, 32 ), 0, 12 ) + ); + } + + /** + * @since 3.0 + * + * @param string $file + * + * @return string + * @throws RuntimeException + */ + public function get( $file ) { + + $tmpDir = []; + $path = ''; + + if ( isset( $GLOBALS['wgTmpDirectory'] ) ) { + $tmpDir[] = $GLOBALS['wgTmpDirectory']; + } + + $tmpDir[] = sys_get_temp_dir(); + $tmpDir[] = ini_get( 'upload_tmp_dir' ); + + foreach ( $tmpDir as $tmp ) { + if ( $tmp != '' && is_dir( $tmp ) && is_writable( $tmp ) ) { + $path = $tmp; + break; + } + } + + if ( $path !== '' ) { + return str_replace( [ '\\', '/' ], DIRECTORY_SEPARATOR, $path . '/' . $file ); + } + + throw new RuntimeException( 'No writable temporary directory could be found.' ); + } + +} |