1<?php
2
3namespace Kanboard\Model;
4
5use Exception;
6use Kanboard\Core\Base;
7
8/**
9 * Avatar File
10 *
11 * @package  Kanboard\Model
12 * @author   Frederic Guillot
13 */
14class AvatarFileModel extends Base
15{
16    /**
17     * Path prefix
18     *
19     * @var string
20     */
21    const PATH_PREFIX = 'avatars';
22
23    /**
24     * Get image filename
25     *
26     * @access public
27     * @param  integer $user_id
28     * @return string
29     */
30    public function getFilename($user_id)
31    {
32        return $this->db->table(UserModel::TABLE)->eq('id', $user_id)->findOneColumn('avatar_path');
33    }
34
35    /**
36     * Add avatar in the user profile
37     *
38     * @access public
39     * @param  integer  $user_id    Foreign key
40     * @param  string   $path       Path on the disk
41     * @return bool
42     */
43    public function create($user_id, $path)
44    {
45        $result = $this->db->table(UserModel::TABLE)->eq('id', $user_id)->update(array(
46            'avatar_path' => $path,
47        ));
48
49        $this->userSession->refresh($user_id);
50
51        return $result;
52    }
53
54    /**
55     * Remove avatar from the user profile
56     *
57     * @access public
58     * @param  integer  $user_id    Foreign key
59     * @return bool
60     */
61    public function remove($user_id)
62    {
63        try {
64            $filename = $this->getFilename($user_id);
65
66            if (! empty($filename)) {
67                $this->objectStorage->remove($filename);
68                return $this->db->table(UserModel::TABLE)->eq('id', $user_id)->update(array('avatar_path' => ''));
69            }
70        } catch (Exception $e) {
71            $this->logger->error($e->getMessage());
72            return false;
73        }
74
75        return true;
76    }
77
78    /**
79     * Upload avatar image file
80     *
81     * @access public
82     * @param  integer $user_id
83     * @param  array   $file
84     * @return boolean
85     */
86    public function uploadImageFile($user_id, array $file)
87    {
88        try {
89            if ($file['error'] == UPLOAD_ERR_OK && $file['size'] > 0) {
90                $destinationFilename = $this->generatePath($user_id, $file['name']);
91                $this->objectStorage->moveUploadedFile($file['tmp_name'], $destinationFilename);
92                $this->create($user_id, $destinationFilename);
93            } else {
94                throw new Exception('File not uploaded: '.var_export($file['error'], true));
95            }
96
97        } catch (Exception $e) {
98            $this->logger->error($e->getMessage());
99            return false;
100        }
101
102        return true;
103    }
104
105    /**
106     * Upload avatar image content
107     *
108     * @access public
109     * @param  integer $user_id
110     * @param  string  $blob
111     * @return boolean
112     */
113    public function uploadImageContent($user_id, &$blob)
114    {
115        try {
116            $destinationFilename = $this->generatePath($user_id, 'imageContent');
117            $this->objectStorage->put($destinationFilename, $blob);
118            $this->create($user_id, $destinationFilename);
119        } catch (Exception $e) {
120            $this->logger->error($e->getMessage());
121            return false;
122        }
123
124        return true;
125    }
126
127    /**
128     * Generate the path for a new filename
129     *
130     * @access public
131     * @param  integer   $user_id
132     * @param  string    $filename
133     * @return string
134     */
135    public function generatePath($user_id, $filename)
136    {
137        return implode(DIRECTORY_SEPARATOR, array(self::PATH_PREFIX, $user_id, hash('sha1', $filename.time())));
138    }
139
140    /**
141     * Check if a filename is an image (file types that can be shown as avatar)
142     *
143     * @access public
144     * @param  string   $filename   Filename
145     * @return bool
146     */
147    public function isAvatarImage($filename)
148    {
149        switch (get_file_extension($filename)) {
150            case 'jpeg':
151            case 'jpg':
152            case 'png':
153            case 'gif':
154                return true;
155        }
156
157        return false;
158    }
159}
160