PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/transfer_validation.php

http://github.com/davidpersson/media
PHP | 111 lines | 53 code | 6 blank | 52 comment | 20 complexity | df33165ebf57d60468e27bbd652d846d MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * Transfer Validation File
  4. *
  5. * Copyright (c) 2007-2013 David Persson
  6. *
  7. * Distributed under the terms of the MIT License.
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * PHP version 5
  11. * CakePHP version 1.3
  12. *
  13. * @package media
  14. * @subpackage media.libs
  15. * @copyright 2007-2013 David Persson <nperson@gmx.de>
  16. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  17. * @link http://github.com/davidpersson/media
  18. */
  19. App::import('Core', 'Validation');
  20. App::import('Lib', 'Media.MediaValidation');
  21. /**
  22. * Transfer Validation Class
  23. *
  24. * @package media
  25. * @subpackage media.libs
  26. */
  27. class TransferValidation extends MediaValidation {
  28. /**
  29. * Checks if subject is transferable
  30. *
  31. * @param mixed $check Path to file in local FS, URL or file-upload array
  32. * @return boolean
  33. */
  34. public function resource($check) {
  35. if (TransferValidation::fileUpload($check)
  36. || TransferValidation::uploadedFile($check) /* This must appear above file */
  37. || MediaValidation::file($check)
  38. || TransferValidation::url($check)) {
  39. return !TransferValidation::blank($check);
  40. }
  41. return false;
  42. }
  43. /**
  44. * Checks if resource is not blank or empty
  45. *
  46. * @param mixed $check Array or string
  47. * @return boolean
  48. */
  49. public function blank($check) {
  50. if (empty($check)) {
  51. return true;
  52. }
  53. if (TransferValidation::fileUpload($check) && $check['error'] == UPLOAD_ERR_NO_FILE) {
  54. return true;
  55. }
  56. if (is_string($check) && Validation::blank($check)) {
  57. return true;
  58. }
  59. return false;
  60. }
  61. /**
  62. * Identifies a file upload array
  63. *
  64. * @param mixed $check
  65. * @return boolean
  66. */
  67. public function fileUpload($check) {
  68. if (!is_array($check)) {
  69. return false;
  70. }
  71. if (!array_key_exists('name',$check)
  72. || !array_key_exists('type',$check)
  73. || !array_key_exists('tmp_name',$check)
  74. || !array_key_exists('error',$check)
  75. || !array_key_exists('size',$check)) {
  76. return false;
  77. }
  78. return true;
  79. }
  80. /**
  81. * Checks if subject is an uploaded file
  82. *
  83. * @param mixed $check
  84. */
  85. public function uploadedFile($check) {
  86. return MediaValidation::file($check) && is_uploaded_file($check);
  87. }
  88. /**
  89. * Validates url
  90. *
  91. * @param string string to check
  92. * @param array options for allowing different url parts currently only scheme is supported
  93. */
  94. public function url($check, $options = array()) {
  95. if (!is_string($check)) {
  96. return false;
  97. }
  98. if (isset($options['scheme'])) {
  99. if (!preg_match('/^(' . implode('|', (array) $options['scheme']) . ':)+/', $check)) {
  100. return false;
  101. }
  102. }
  103. return Validation::url($check);
  104. }
  105. }