| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* |
| 5 |
* This class contains helper methods for handling files. |
| 6 |
* |
| 7 |
* @since 1.0.7 |
| 8 |
* @package Socketlabs |
| 9 |
* @subpackage Socketlabs/includes |
| 10 |
* @author SocketLabs Dev Team <support@socketlabs.com> |
| 11 |
*/ |
| 12 |
class Socketlabs_IO |
| 13 |
{ |
| 14 |
/** |
| 15 |
* Map a file name to a MIME type. |
| 16 |
* Defaults to 'application/octet-stream', i.e.. arbitrary binary data. |
| 17 |
* @since 1.0.7 |
| 18 |
* @param string $filename A file name or full path, does not need to exist as a file |
| 19 |
* @return string |
| 20 |
* @static |
| 21 |
*/ |
| 22 |
public static function filenameToType($filename) |
| 23 |
{ |
| 24 |
// In case the path is a URL, strip any query string before getting extension |
| 25 |
$qpos = strpos($filename, '?'); |
| 26 |
if (false !== $qpos) { |
| 27 |
$filename = substr($filename, 0, $qpos); |
| 28 |
} |
| 29 |
$pathinfo = self::mb_pathinfo($filename); |
| 30 |
return self::_mime_types($pathinfo['extension']); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Multi-byte-safe pathinfo replacement. |
| 35 |
* Drop-in replacement for pathinfo(), but multibyte-safe, cross-platform-safe, old-version-safe. |
| 36 |
* Works similarly to the one in PHP >= 5.2.0 |
| 37 |
* @since 1.0.7 |
| 38 |
* @link http://www.php.net/manual/en/function.pathinfo.php#107461 |
| 39 |
* @param string $path A filename or path, does not need to exist as a file |
| 40 |
* @param integer|string $options Either a PATHINFO_* constant, |
| 41 |
* or a string name to return only the specified piece, allows 'filename' to work on PHP < 5.2 |
| 42 |
* @return string|array |
| 43 |
* @static |
| 44 |
*/ |
| 45 |
public static function mb_pathinfo($path, $options = null) |
| 46 |
{ |
| 47 |
$ret = array('dirname' => '', 'basename' => '', 'extension' => '', 'filename' => ''); |
| 48 |
$pathinfo = array(); |
| 49 |
if (preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im', $path, $pathinfo)) { |
| 50 |
if (array_key_exists(1, $pathinfo)) { |
| 51 |
$ret['dirname'] = $pathinfo[1]; |
| 52 |
} |
| 53 |
if (array_key_exists(2, $pathinfo)) { |
| 54 |
$ret['basename'] = $pathinfo[2]; |
| 55 |
} |
| 56 |
if (array_key_exists(5, $pathinfo)) { |
| 57 |
$ret['extension'] = $pathinfo[5]; |
| 58 |
} |
| 59 |
if (array_key_exists(3, $pathinfo)) { |
| 60 |
$ret['filename'] = $pathinfo[3]; |
| 61 |
} |
| 62 |
} |
| 63 |
switch ($options) { |
| 64 |
case PATHINFO_DIRNAME: |
| 65 |
case 'dirname': |
| 66 |
return $ret['dirname']; |
| 67 |
case PATHINFO_BASENAME: |
| 68 |
case 'basename': |
| 69 |
return $ret['basename']; |
| 70 |
case PATHINFO_EXTENSION: |
| 71 |
case 'extension': |
| 72 |
return $ret['extension']; |
| 73 |
case PATHINFO_FILENAME: |
| 74 |
case 'filename': |
| 75 |
return $ret['filename']; |
| 76 |
default: |
| 77 |
return $ret; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Get the MIME type for a file extension. |
| 83 |
* @since 1.0.7 |
| 84 |
* @param string $ext File extension |
| 85 |
* @access public |
| 86 |
* @return string MIME type of file. |
| 87 |
* @static |
| 88 |
*/ |
| 89 |
public static function _mime_types($ext = '') |
| 90 |
{ |
| 91 |
$mimes = array( |
| 92 |
'xl' => 'application/excel', |
| 93 |
'js' => 'application/javascript', |
| 94 |
'hqx' => 'application/mac-binhex40', |
| 95 |
'cpt' => 'application/mac-compactpro', |
| 96 |
'bin' => 'application/macbinary', |
| 97 |
'doc' => 'application/msword', |
| 98 |
'word' => 'application/msword', |
| 99 |
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
| 100 |
'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', |
| 101 |
'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template', |
| 102 |
'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', |
| 103 |
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
| 104 |
'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide', |
| 105 |
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
| 106 |
'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', |
| 107 |
'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12', |
| 108 |
'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', |
| 109 |
'class' => 'application/octet-stream', |
| 110 |
'dll' => 'application/octet-stream', |
| 111 |
'dms' => 'application/octet-stream', |
| 112 |
'exe' => 'application/octet-stream', |
| 113 |
'lha' => 'application/octet-stream', |
| 114 |
'lzh' => 'application/octet-stream', |
| 115 |
'psd' => 'application/octet-stream', |
| 116 |
'sea' => 'application/octet-stream', |
| 117 |
'so' => 'application/octet-stream', |
| 118 |
'oda' => 'application/oda', |
| 119 |
'pdf' => 'application/pdf', |
| 120 |
'ai' => 'application/postscript', |
| 121 |
'eps' => 'application/postscript', |
| 122 |
'ps' => 'application/postscript', |
| 123 |
'smi' => 'application/smil', |
| 124 |
'smil' => 'application/smil', |
| 125 |
'mif' => 'application/vnd.mif', |
| 126 |
'xls' => 'application/vnd.ms-excel', |
| 127 |
'ppt' => 'application/vnd.ms-powerpoint', |
| 128 |
'wbxml' => 'application/vnd.wap.wbxml', |
| 129 |
'wmlc' => 'application/vnd.wap.wmlc', |
| 130 |
'dcr' => 'application/x-director', |
| 131 |
'dir' => 'application/x-director', |
| 132 |
'dxr' => 'application/x-director', |
| 133 |
'dvi' => 'application/x-dvi', |
| 134 |
'gtar' => 'application/x-gtar', |
| 135 |
'php3' => 'application/x-httpd-php', |
| 136 |
'php4' => 'application/x-httpd-php', |
| 137 |
'php' => 'application/x-httpd-php', |
| 138 |
'phtml' => 'application/x-httpd-php', |
| 139 |
'phps' => 'application/x-httpd-php-source', |
| 140 |
'swf' => 'application/x-shockwave-flash', |
| 141 |
'sit' => 'application/x-stuffit', |
| 142 |
'tar' => 'application/x-tar', |
| 143 |
'tgz' => 'application/x-tar', |
| 144 |
'xht' => 'application/xhtml+xml', |
| 145 |
'xhtml' => 'application/xhtml+xml', |
| 146 |
'zip' => 'application/zip', |
| 147 |
'mid' => 'audio/midi', |
| 148 |
'midi' => 'audio/midi', |
| 149 |
'mp2' => 'audio/mpeg', |
| 150 |
'mp3' => 'audio/mpeg', |
| 151 |
'mpga' => 'audio/mpeg', |
| 152 |
'aif' => 'audio/x-aiff', |
| 153 |
'aifc' => 'audio/x-aiff', |
| 154 |
'aiff' => 'audio/x-aiff', |
| 155 |
'ram' => 'audio/x-pn-realaudio', |
| 156 |
'rm' => 'audio/x-pn-realaudio', |
| 157 |
'rpm' => 'audio/x-pn-realaudio-plugin', |
| 158 |
'ra' => 'audio/x-realaudio', |
| 159 |
'wav' => 'audio/x-wav', |
| 160 |
'bmp' => 'image/bmp', |
| 161 |
'gif' => 'image/gif', |
| 162 |
'jpeg' => 'image/jpeg', |
| 163 |
'jpe' => 'image/jpeg', |
| 164 |
'jpg' => 'image/jpeg', |
| 165 |
'png' => 'image/png', |
| 166 |
'tiff' => 'image/tiff', |
| 167 |
'tif' => 'image/tiff', |
| 168 |
'eml' => 'message/rfc822', |
| 169 |
'css' => 'text/css', |
| 170 |
'html' => 'text/html', |
| 171 |
'htm' => 'text/html', |
| 172 |
'shtml' => 'text/html', |
| 173 |
'log' => 'text/plain', |
| 174 |
'text' => 'text/plain', |
| 175 |
'txt' => 'text/plain', |
| 176 |
'rtx' => 'text/richtext', |
| 177 |
'rtf' => 'text/rtf', |
| 178 |
'vcf' => 'text/vcard', |
| 179 |
'vcard' => 'text/vcard', |
| 180 |
'xml' => 'text/xml', |
| 181 |
'xsl' => 'text/xml', |
| 182 |
'mpeg' => 'video/mpeg', |
| 183 |
'mpe' => 'video/mpeg', |
| 184 |
'mpg' => 'video/mpeg', |
| 185 |
'mov' => 'video/quicktime', |
| 186 |
'qt' => 'video/quicktime', |
| 187 |
'rv' => 'video/vnd.rn-realvideo', |
| 188 |
'avi' => 'video/x-msvideo', |
| 189 |
'movie' => 'video/x-sgi-movie' |
| 190 |
); |
| 191 |
if (array_key_exists(strtolower($ext), $mimes)) { |
| 192 |
return $mimes[strtolower($ext)]; |
| 193 |
} |
| 194 |
return 'application/octet-stream'; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Encode a file attachment in requested format. |
| 199 |
* Returns an empty string on failure. |
| 200 |
* @since 1.0.7 |
| 201 |
* @param string $path The full path to the file |
| 202 |
* @return string |
| 203 |
* @static |
| 204 |
*/ |
| 205 |
public static function encodeFile($path) |
| 206 |
{ |
| 207 |
try { |
| 208 |
if (!is_readable($path)) { |
| 209 |
throw new Exception("Cannot open file: " . $path); |
| 210 |
} |
| 211 |
//PHP versions greater than 5.3.0 should always have magic_quotes_runtime disabled. |
| 212 |
//Magic quotes have been deprecated in PHP and removed as of PHP 5.4. So there is |
| 213 |
//no reason to diable the feature. |
| 214 |
//get_magic_quotes_runtime() deprecated and removed in PHP 7.4 |
| 215 |
//https://www.php.net/manual/en/function.get-magic-quotes-runtime.php |
| 216 |
ini_set('magic_quotes_runtime', false); |
| 217 |
|
| 218 |
$file_buffer = file_get_contents($path); |
| 219 |
$file_buffer = base64_encode($file_buffer); |
| 220 |
|
| 221 |
return $file_buffer; |
| 222 |
} catch (Exception $exc) { |
| 223 |
return ''; |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
|