| 
									
										
										
										
											2015-07-18 09:49:19 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Models; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-09 15:53:11 +01:00
										 |  |  | use Auth; | 
					
						
							| 
									
										
										
										
											2016-01-01 21:49:27 +01:00
										 |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2015-07-19 09:37:28 +02:00
										 |  |  | use Crypt; | 
					
						
							| 
									
										
										
										
											2016-01-01 21:49:27 +01:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2015-07-18 09:49:19 +02:00
										 |  |  | use Illuminate\Database\Eloquent\Model; | 
					
						
							|  |  |  | use Illuminate\Database\Eloquent\SoftDeletes; | 
					
						
							| 
									
										
										
										
											2016-01-09 15:53:11 +01:00
										 |  |  | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | 
					
						
							| 
									
										
										
										
											2015-07-18 09:49:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2016-01-01 21:49:27 +01:00
										 |  |  |  * FireflyIII\Models\Attachment | 
					
						
							| 
									
										
										
										
											2015-07-18 09:49:19 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-01-01 21:49:27 +01:00
										 |  |  |  * @property integer         $id | 
					
						
							|  |  |  |  * @property Carbon          $created_at | 
					
						
							|  |  |  |  * @property Carbon          $updated_at | 
					
						
							|  |  |  |  * @property string          $deleted_at | 
					
						
							|  |  |  |  * @property integer         $attachable_id | 
					
						
							|  |  |  |  * @property string          $attachable_type | 
					
						
							|  |  |  |  * @property integer         $user_id | 
					
						
							|  |  |  |  * @property string          $md5 | 
					
						
							|  |  |  |  * @property string          $filename | 
					
						
							|  |  |  |  * @property string          $title | 
					
						
							|  |  |  |  * @property string          $description | 
					
						
							|  |  |  |  * @property string          $notes | 
					
						
							|  |  |  |  * @property string          $mime | 
					
						
							|  |  |  |  * @property integer         $size | 
					
						
							|  |  |  |  * @property boolean         $uploaded | 
					
						
							|  |  |  |  * @property-read Attachment $attachable | 
					
						
							|  |  |  |  * @property-read User       $user | 
					
						
							| 
									
										
										
										
											2015-07-18 09:49:19 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | class Attachment extends Model | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     use SoftDeletes; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-19 09:37:28 +02:00
										 |  |  |     protected $fillable = ['attachable_id', 'attachable_type', 'user_id', 'md5', 'filename', 'mime', 'title', 'notes', 'description', 'size', 'uploaded']; | 
					
						
							| 
									
										
										
										
											2015-07-18 09:49:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get all of the owning imageable models. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function attachable() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->morphTo(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function user() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->belongsTo('FireflyIII\User'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-19 09:37:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return null|string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getFilenameAttribute($value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (is_null($value)) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Crypt::decrypt($value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param string $value | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setFilenameAttribute($value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->attributes['filename'] = Crypt::encrypt($value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return null|string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getMimeAttribute($value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (is_null($value)) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Crypt::decrypt($value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param string $value | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setMimeAttribute($value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->attributes['mime'] = Crypt::encrypt($value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return null|string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getTitleAttribute($value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (is_null($value)) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Crypt::decrypt($value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param string $value | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setTitleAttribute($value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->attributes['title'] = Crypt::encrypt($value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return null|string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getDescriptionAttribute($value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (is_null($value)) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Crypt::decrypt($value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param string $value | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setDescriptionAttribute($value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->attributes['description'] = Crypt::encrypt($value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param $value | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return null|string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getNotesAttribute($value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (is_null($value)) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Crypt::decrypt($value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param string $value | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setNotesAttribute($value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->attributes['notes'] = Crypt::encrypt($value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-15 17:38:09 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Attachment $value | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Attachment | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-09 15:53:11 +01:00
										 |  |  |     public static function routeBinder(Attachment $value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (Auth::check()) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($value->user_id == Auth::user()->id) { | 
					
						
							|  |  |  |                 return $value; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         throw new NotFoundHttpException; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-19 09:38:44 +02:00
										 |  |  | } |