public function testUrl() { $url = 'https://example.com'; $result = TransferValidation::url($url, array('scheme' => 'http')); $this->assertFalse($result); $url = 'http://example.com'; $result = TransferValidation::url($url, array('scheme' => 'http')); $this->assertTrue($result); $url = null; $result = TransferValidation::url($url); $this->assertFalse($result); }
/** * Checks if resource is not blank or empty * * @param mixed $check Array or string * @return boolean */ static function blank($check) { if (empty($check)) { return true; } if (TransferValidation::fileUpload($check) && $check['error'] == UPLOAD_ERR_NO_FILE) { return true; } if (is_string($check) && Validation::blank($check)) { return true; } return false; }
/** * Checks if field contains a transferable resource * * To require a file being uploaded, consider the following validation rule. * {{{ * var $validate = array( * 'file' => array( * 'resource' => array( * 'rule' => 'checkResource', * 'allowEmpty' => false, * 'required' => true * )))); * }}i} * * @see TransferBehavior::_source() * @param Model $Model * @param array $field * @return boolean */ function checkResource(&$Model, $field) { return TransferValidation::resource(current($field)); }
/** * Gather/Return information about a resource * * @param mixed $resource Path to file in local FS, URL or file-upload array * @param string $what scheme, host, port, file, MIME type, size, permission, * dirname, basename, filename, extension or type * @return mixed */ function info(&$Model, $resource, $what = null) { extract($this->settings[$Model->alias], EXTR_SKIP); $defaultResource = array('scheme' => null, 'host' => null, 'port' => null, 'file' => null, 'mimeType' => null, 'size' => null, 'pixels' => null, 'permisssion' => null, 'dirname' => null, 'basename' => null, 'filename' => null, 'extension' => null, 'type' => null); /* Currently HTTP is supported only */ if (TransferValidation::url($resource, array('scheme' => 'http'))) { $resource = array_merge($defaultResource, pathinfo(parse_url($resource, PHP_URL_PATH)), array('scheme' => parse_url($resource, PHP_URL_SCHEME), 'host' => parse_url($resource, PHP_URL_HOST), 'port' => parse_url($resource, PHP_URL_PORT), 'file' => $resource, 'type' => 'http-url-remote')); if (!class_exists('HttpSocket')) { App::import('Core', 'HttpSocket'); } $Socket =& new HttpSocket(array('timeout' => 5)); $Socket->request(array('method' => 'HEAD', 'uri' => $resource['file'])); if (empty($Socket->error) && $Socket->response['status']['code'] == 200) { $resource = array_merge($resource, array('size' => $Socket->response['header']['Content-Length'], 'mimeType' => $trustClient ? $Socket->response['header']['Content-Type'] : null, 'permission' => '0004')); } } elseif (MediaValidation::file($resource, false)) { $resource = array_merge($defaultResource, pathinfo($resource), array('file' => $resource, 'host' => 'localhost', 'mimeType' => MimeType::guessType($resource, array('paranoid' => !$trustClient)))); if (TransferValidation::uploadedFile($resource['file'])) { $resource['type'] = 'uploaded-file-local'; } else { $resource['type'] = 'file-local'; } if (is_readable($resource['file'])) { /* * Because there is not better way to determine if resource is an image * first, we suppress a warning that would be thrown here otherwise. */ list($width, $height) = @getimagesize($resource['file']); $resource = array_merge($resource, array('size' => filesize($resource['file']), 'permission' => substr(sprintf('%o', fileperms($resource['file'])), -4), 'pixels' => $width * $height)); } } elseif (TransferValidation::fileUpload($resource)) { $resource = array_merge($defaultResource, pathinfo($resource['name']), array('file' => $resource['name'], 'host' => env('REMOTE_ADDR'), 'size' => $resource['size'], 'mimeType' => $trustClient ? $resource['type'] : null, 'permission' => '0004', 'type' => 'file-upload-remote')); } else { return null; } if (!isset($resource['filename'])) { /* PHP < 5.2.0 */ $length = isset($resource['extension']) ? strlen($resource['extension']) + 1 : 0; $resource['filename'] = substr($resource['basename'], 0, -$length); } if (is_null($what)) { return $resource; } elseif (array_key_exists($what, $resource)) { return $resource[$what]; } return null; }