return "$nickname-$datestamp-$random.$ext";
}
+ /**
+ * Validation for as-saved base filenames
+ */
+ static function validFilename($filename)
+ {
+ return preg_match('/^[A-Za-z0-9._-]+$/', $filename);
+ }
+
+ /**
+ * @throws ClientException on invalid filename
+ */
static function path($filename)
{
+ if (!self::validFilename($filename)) {
+ throw new ClientException("Invalid filename");
+ }
$dir = common_config('attachments', 'dir');
if ($dir[strlen($dir)-1] != '/') {
static function url($filename)
{
- $path = common_config('attachments', 'path');
-
- if ($path[strlen($path)-1] != '/') {
- $path .= '/';
+ if (!self::validFilename($filename)) {
+ throw new ClientException("Invalid filename");
}
+ if(common_config('site','private')) {
- if ($path[0] != '/') {
- $path = '/'.$path;
- }
+ return common_local_url('getfile',
+ array('filename' => $filename));
- $server = common_config('attachments', 'server');
+ } else {
+ $path = common_config('attachments', 'path');
- if (empty($server)) {
- $server = common_config('site', 'server');
- }
+ if ($path[strlen($path)-1] != '/') {
+ $path .= '/';
+ }
+
+ if ($path[0] != '/') {
+ $path = '/'.$path;
+ }
+
+ $server = common_config('attachments', 'server');
+
+ if (empty($server)) {
+ $server = common_config('site', 'server');
+ }
+
+ $ssl = common_config('attachments', 'ssl');
- // XXX: protocol
+ if (is_null($ssl)) { // null -> guess
+ if (common_config('site', 'ssl') == 'always' &&
+ !common_config('attachments', 'server')) {
+ $ssl = true;
+ } else {
+ $ssl = false;
+ }
+ }
- return 'http://'.$server.$path.$filename;
+ $protocol = ($ssl) ? 'https' : 'http';
+
+ return $protocol.'://'.$server.$path.$filename;
+ }
}
function getEnclosure(){