/** * Checks if resource has (not) one of given extensions * * @param Model $Model * @param array $field * @param mixed $deny True or * blocks any extension, * an array containing extensions (w/o leading dot) selectively blocks, * false blocks no extension * @param mixed $allow True or * allows any extension, * an array containing extensions (w/o leading dot) selectively allows, * false allows no extension * @return boolean */ function checkExtension(&$Model, $field, $deny = false, $allow = true) { extract($this->runtime[$Model->alias]); foreach (array('source', 'temporary', 'destination') as $type) { if ($type == 'temporary' && empty(${$type}) || !isset(${$type}['extension'])) { continue; } if (!MediaValidation::extension(${$type}['extension'], $deny, $allow)) { return false; } } return true; }
function testExtension() { $check = 'png'; $result = MediaValidation::extension($check); $this->assertTrue($result); $check = 'tar.gz'; $result = MediaValidation::extension($check, false, array('tar', 'gz')); $this->assertTrue($result); $check = 'tar.gz'; $result = MediaValidation::extension($check, false, array('tar.gz')); $this->assertFalse($result); $check = 'png'; $result = MediaValidation::extension($check, array('png')); $this->assertFalse($result); $check = 'png'; $result = MediaValidation::extension($check, array('png'), array('png')); $this->assertFalse($result); $check = 'in.va.lid'; $result = MediaValidation::extension($check); $this->assertFalse($result); $check = '.inva.lid'; $result = MediaValidation::extension($check); $this->assertFalse($result); $check = ''; $result = MediaValidation::extension($check); $this->assertFalse($result); $check = false; $result = MediaValidation::extension($check); $this->assertFalse($result); $check = true; $result = MediaValidation::extension($check); $this->assertFalse($result); $check = true; $result = MediaValidation::extension($check); $this->assertFalse($result); $deny = array('bin', 'class', 'dll', 'dms', 'exe', 'lha'); $allow = array('pdf'); $check = 'tmp'; $result = MediaValidation::extension($check, $deny, $allow); $this->assertFalse($result); $check = 'tmp'; $result = MediaValidation::extension($check); $this->assertTrue($result); $deny = array('bin', 'class', 'dll', 'dms', 'exe', 'lha'); $allow = array('pdf', 'tmp'); $check = 'tmp'; $result = MediaValidation::extension($check); $this->assertTrue($result); $deny = array('bin', 'class', 'dll', 'dms', 'exe', 'lha'); $allow = array('*'); $check = 'tmp'; $result = MediaValidation::extension($check); $this->assertTrue($result); }