PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/protected/modules/user/components/EPhotoValidator.php

https://github.com/LosYear/FluentCMS
PHP | 284 lines | 130 code | 19 blank | 135 comment | 34 complexity | c6d4ea1b595baba415f573ed501e49bc MD5 | raw file
  1. <?php
  2. Yii::import('application.modules.user.UserModule');
  3. /**
  4. * EPhotoValidator class file.
  5. *
  6. * @author emix
  7. * @link http://www.yiiframework.com/
  8. * @copyright Copyright &copy; 2009 emix
  9. * @license
  10. *
  11. * Copyright (C) 2008 by emix
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without modification,
  15. * are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name of emix nor the names of its contributors may
  23. * be used to endorse or promote products derived from this software without
  24. * specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  32. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  35. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @author vladm
  38. * @version 0.2alpha_vladm revised version. date: February 14, 2010
  39. */
  40. class EPhotoValidator extends CFileValidator {
  41. /**
  42. * @var string Allowed mime type of the image, ie: image/jpeg
  43. */
  44. public $mimeType;
  45. /**
  46. * @var string Mime type error message
  47. */
  48. public $mimeTypeError;
  49. /**
  50. * @var fixed image width required.
  51. */
  52. public $width;
  53. /*
  54. * @var string width error message.
  55. */
  56. public $widthError;
  57. /*
  58. * @var fixed image height required.
  59. */
  60. public $height;
  61. /*
  62. * @var string height error message.
  63. */
  64. public $heightError;
  65. /**
  66. * @var int Minimum width of the image required.
  67. */
  68. public $minWidth;
  69. /**
  70. * @var string Mime type error message
  71. */
  72. public $minWidthError;
  73. /**
  74. * @var int Maximum width of the image allowed
  75. */
  76. public $maxWidth;
  77. /**
  78. * @var string Mime type error message
  79. */
  80. public $maxWidthError;
  81. /**
  82. * @var int Minimum height of the image required.
  83. */
  84. public $minHeight;
  85. /**
  86. * @var string Mime type error message
  87. */
  88. public $minHeightError;
  89. /**
  90. * @var int Maximum height of the image allowed.
  91. */
  92. public $maxHeight;
  93. /**
  94. * @var string Mime type error message
  95. */
  96. public $maxHeightError;
  97. /**
  98. * @var int Maximum height of the image allowed.
  99. */
  100. public $smallSideSize;
  101. /**
  102. * @var string Mime type error message
  103. */
  104. public $smallSideSizeError;
  105. /**
  106. * @var int Maximum height of the image allowed.
  107. */
  108. public $smallSideMinSize;
  109. /**
  110. * @var string Mime type error message
  111. */
  112. public $smallSideMinSizeError;
  113. /**
  114. * @var int Maximum height of the image allowed.
  115. */
  116. public $smallSideMaxSize;
  117. /**
  118. * @var string Mime type error message
  119. */
  120. public $smallSideMaxSizeError;
  121. /**
  122. * @var int Minimum height of the image required.
  123. */
  124. public $bigSideSize;
  125. /**
  126. * @var string Mime type error message
  127. */
  128. public $bigSideSizeError;
  129. /**
  130. * @var int Maximum height of the image allowed.
  131. */
  132. public $bigSideMinSize;
  133. /**
  134. * @var string Mime type error message
  135. */
  136. public $bigSideMinSizeError;
  137. /**
  138. * @var int Maximum height of the image allowed.
  139. */
  140. public $bigSideMaxSize;
  141. /**
  142. * @var string Mime type error message
  143. */
  144. public $bigSideMaxSizeError;
  145. protected function validateAttribute($object, $attribute)
  146. {
  147. parent::validateAttribute($object, $attribute);
  148. }
  149. /**
  150. * Internally validates a file object.
  151. * @param CModel the object being validated
  152. * @param string the attribute being validated
  153. * @param CUploadedFile uploaded file passed to check against a set of rules
  154. */
  155. public function validateFile($object, $attribute, $file) {
  156. // do parent validation
  157. parent::validateFile($object, $attribute, $file);
  158. // prevent next validation if no file, other UPLOAD_ERR_s was checked before
  159. if(null===$file || ($file->getError()) == UPLOAD_ERR_NO_FILE)
  160. return;
  161. /**
  162. * Index 0 and 1 contains respectively the width and the height of the
  163. * image.
  164. * Index 2 is one of the IMAGETYPE_XXX constants indicating the type of
  165. * the image.
  166. * Index 3 is a text string with the correct height="yyy" width="xxx"
  167. * string.
  168. * Key 'mime' is the correspondant MIME type of the image.
  169. * Key 'channels' will be 3 for RGB pictures and 4 for CMYK pictures.
  170. * Key 'bits' is the number of bits for each color.
  171. */
  172. $info = @getimagesize($file->getTempName());
  173. if(!$info){
  174. $message = Yum::t('The file "{file}" is not an image.', array(), 'coreMessages');
  175. $this->addError($object, $attribute, $message, array('{file}'=>$file->getName()));
  176. return;
  177. }
  178. // remove unnecessary values from $info and make it more readable
  179. $info = array(
  180. 'width' => $info[0],
  181. 'height' => $info[1],
  182. 'mime' => $info['mime'],
  183. );
  184. if ($this->width!==null){
  185. if($info['width'] != $this->width) {
  186. $message = $this->widthError ? $this->widthError : Yum::t('The image "{file}" width should be "{width}px".', array(), 'coreMessages');
  187. $this->addError($object, $attribute, $message, array('{file}'=>$file->getName(), '{width}'=>$this->width));
  188. }
  189. }
  190. if ($this->minWidth!==null) {
  191. if ($info['width'] < $this->minWidth) {
  192. $message = $this->minWidthError ? $this->minWidthError : Yum::t('The image "{file}" width should be at least "{width}px".', array(), 'coreMessages');
  193. $this->addError($object, $attribute, $message, array('{file}'=>$file->getName(), '{width}'=>$this->minWidth));
  194. }
  195. }
  196. if ($this->maxWidth!==null) {
  197. if ($info['width'] > $this->maxWidth) {
  198. $message = $this->maxWidthError ? $this->maxWidthError : Yum::t('The image "{file}" width should be at most "{width}px".', array(), 'coreMessages');
  199. $this->addError($object, $attribute, $message, array('{file}'=>$file->getName(), '{width}'=>$this->maxWidth));
  200. }
  201. }
  202. if ($this->height!==null){
  203. if($info['height'] != $this->height) {
  204. $message = $this->heightError ? $this->heightError : Yum::t('The image "{file}" height should be "{height}px".', array(), 'coreMessages');
  205. $this->addError($object, $attribute, $message, array('{file}'=>$file->getName(), '{height}'=>$this->height));
  206. }
  207. }
  208. if ($this->minHeight!==null) {
  209. if ($info['height'] < $this->minHeight) {
  210. $message = $this->minHeightError ? $this->minHeightError : Yum::t('The image "{file}" height should be at least "{height}px".', array(), 'coreMessages');
  211. $this->addError($object, $attribute, $message, array('{file}'=>$file->getName(), '{height}'=>$this->minHeight));
  212. }
  213. }
  214. if ($this->maxHeight!==null) {
  215. if ($info['height'] > $this->maxHeight) {
  216. $message = $this->maxHeightError ? $this->maxHeightError : Yum::t('The image "{file}" height should be at most "{height}px".', array(), 'coreMessages');
  217. $this->addError($object, $attribute, $message, array('{file}'=>$file->getName(), '{height}'=>$this->maxHeight));
  218. }
  219. }
  220. if ($this->smallSideSize!==null) {
  221. if (min($info['height'], $info['width']) == $this->smallSideSize) {
  222. $message = $this->smallSideSizeError ? $this->smallSideSizeError : Yum::t('The image "{file}" small side should be "{side}px".', array(), 'coreMessages');
  223. $this->addError($object, $attribute, $message, array('{file}'=>$file->getName(), '{side}'=>$this->smallSideSize));
  224. }
  225. }
  226. if ($this->smallSideMinSize!==null) {
  227. if (min($info['height'], $info['width']) < $this->smallSideMinSize) {
  228. $message = $this->smallSideMinSizeError ? $this->smallSideMinSizeError : Yum::t('The image "{file}" small side should be at least "{side}px".', array(), 'coreMessages');
  229. $this->addError($object, $attribute, $message, array('{file}'=>$file->getName(), '{side}'=>$this->smallSideMinSize));
  230. }
  231. }
  232. if ($this->smallSideMaxSize!==null) {
  233. if (min($info['height'], $info['width']) > $this->smallSideMaxSize) {
  234. $message = $this->smallSideMaxSizeError ? $this->smallSideMaxSizeError : Yum::t('The image "{file}" small side should be at most "{side}px".', array(), 'coreMessages');
  235. $this->addError($object, $attribute, $message, array('{file}'=>$file->getName(), '{side}'=>$this->smallSideMaxSize));
  236. }
  237. }
  238. if ($this->bigSideSize!==null) {
  239. if (max($info['height'], $info['width']) == $this->bigSideSize) {
  240. $message = $this->bigSideSizeError ? $this->bigSideSizeError : Yum::t('The image "{file}" big side should be "{side}px".', array(), 'coreMessages');
  241. $this->addError($object, $attribute, $message, array('{file}'=>$file->getName(), '{side}'=>$this->bigSideSize));
  242. }
  243. }
  244. if ($this->bigSideMinSize!==null) {
  245. if (max($info['height'], $info['width']) < $this->bigSideMinSize) {
  246. $message = $this->bigSideMinSizeError ? $this->bigSideMinSizeError : Yum::t('The image "{file}" big side should be at least "{side}px".', array(), 'coreMessages');
  247. $this->addError($object, $attribute, $message, array('{file}'=>$file->getName(), '{side}'=>$this->bigSideMinSize));
  248. }
  249. }
  250. if ($this->bigSideMaxSize!==null) {
  251. if (max($info['height'], $info['width']) > $this->bigSideMaxSize) {
  252. $message = $this->bigSideMaxSizeError ? $this->bigSideMaxSizeError : Yum::t('The image "{file}" big side should be at most "{side}px".', array(), 'coreMessages');
  253. $this->addError($object, $attribute, $message, array('{file}'=>$file->getName(), '{side}'=>$this->bigSideMaxSize));
  254. }
  255. }
  256. if ($this->mimeType!==null) {
  257. $mimeTypes = is_scalar($this->mimeType) ? array($this->mimeType) : $this->mimeType;
  258. if (!in_array($info['mime'], $mimeTypes)) {
  259. $message=$this->mimeTypeError ? $this->mimeTypeError : Yum::t('The image "{file}" mime type "{mime}" is not allowed.', array(), 'coreMessages');
  260. $this->addError($object, $attribute, $message, array('{file}'=>$file->getName(), '{mime}'=>$info['mime']));
  261. }
  262. }
  263. }
  264. }
  265. ?>