Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 86
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
local
0.00% covered (danger)
0.00%
0 / 86
0.00% covered (danger)
0.00%
0 / 6
702
0.00% covered (danger)
0.00%
0 / 1
 get_data
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 prepare_form
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 1
72
 prepare_form_acp
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 process_form
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
30
 get_template_name
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_avatar_list
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\avatar\driver;
15
16/**
17* Handles avatars selected from the board gallery
18*/
19class local extends \phpbb\avatar\driver\driver
20{
21    /**
22    * {@inheritdoc}
23    */
24    public function get_data($row)
25    {
26        $root_path = $this->path_helper->get_web_root_path();
27
28        return array(
29            'src' => $root_path . $this->config['avatar_gallery_path'] . '/' . $row['avatar'],
30            'width' => $row['avatar_width'],
31            'height' => $row['avatar_height'],
32        );
33    }
34
35    /**
36    * {@inheritdoc}
37    */
38    public function prepare_form($request, $template, $user, $row, &$error)
39    {
40        $avatar_list = $this->get_avatar_list($user);
41        $category = $request->variable('avatar_local_cat', key($avatar_list));
42
43        foreach (array_keys($avatar_list) as $cat)
44        {
45            if (!empty($avatar_list[$cat]))
46            {
47                $template->assign_block_vars('avatar_local_cats', array(
48                    'NAME' => $cat,
49                    'SELECTED' => ($cat == $category),
50                ));
51            }
52
53            if ($cat != $category)
54            {
55                unset($avatar_list[$cat]);
56            }
57        }
58
59        if (!empty($avatar_list[$category]))
60        {
61            $template->assign_vars(array(
62                'AVATAR_LOCAL_SHOW' => true,
63            ));
64
65            $table_cols = isset($row['avatar_gallery_cols']) ? $row['avatar_gallery_cols'] : 4;
66            $col_count = $avatar_pos = 0;
67            $avatar_count = count($avatar_list[$category]);
68
69            reset($avatar_list[$category]);
70
71            while ($avatar_pos < $avatar_count)
72            {
73                $img = current($avatar_list[$category]);
74                next($avatar_list[$category]);
75
76                if ($col_count == 0)
77                {
78                    $template->assign_block_vars('avatar_local_row', array(
79                    ));
80                }
81
82                $template->assign_block_vars('avatar_local_row.avatar_local_col', array(
83                    'AVATAR_IMAGE'  => $this->phpbb_root_path . $this->config['avatar_gallery_path'] . '/' . $img['file'],
84                    'AVATAR_NAME'     => $img['name'],
85                    'AVATAR_FILE'     => $img['filename'],
86                    'CHECKED'            => $img['file'] === $row['avatar'],
87                ));
88
89                $template->assign_block_vars('avatar_local_row.avatar_local_option', array(
90                    'AVATAR_FILE'         => $img['filename'],
91                    'S_OPTIONS_AVATAR'    => $img['filename'],
92                    'CHECKED'            => $img['file'] === $row['avatar'],
93                ));
94
95                $col_count = ($col_count + 1) % $table_cols;
96
97                ++$avatar_pos;
98            }
99        }
100
101        return true;
102    }
103
104    /**
105    * {@inheritdoc}
106    */
107    public function prepare_form_acp($user)
108    {
109        return array(
110            'avatar_gallery_path'    => array('lang' => 'AVATAR_GALLERY_PATH',    'validate' => 'rpath',    'type' => 'text:20:255', 'explain' => true),
111        );
112    }
113
114    /**
115    * {@inheritdoc}
116    */
117    public function process_form($request, $template, $user, $row, &$error)
118    {
119        $avatar_list = $this->get_avatar_list($user);
120        $category = $request->variable('avatar_local_cat', '');
121
122        $file = $request->variable('avatar_local_file', '');
123
124        if (empty($category) || empty($file))
125        {
126            return false;
127        }
128
129        if (!isset($avatar_list[$category][urldecode($file)]))
130        {
131            $error[] = 'AVATAR_URL_NOT_FOUND';
132            return false;
133        }
134
135        return array(
136            'avatar' => ($category != $user->lang['NO_AVATAR_CATEGORY']) ? $category . '/' . $file : $file,
137            'avatar_width' => $avatar_list[$category][urldecode($file)]['width'],
138            'avatar_height' => $avatar_list[$category][urldecode($file)]['height'],
139        );
140    }
141
142    /**
143    * {@inheritdoc}
144    */
145    public function get_template_name()
146    {
147        return 'ucp_avatar_options_local.html';
148    }
149
150    /**
151    * Get a list of avatars that are locally available
152    * Results get cached for 24 hours (86400 seconds)
153    *
154    * @param \phpbb\user $user User object
155    *
156    * @return array Array containing the locally available avatars
157    */
158    protected function get_avatar_list($user)
159    {
160        $avatar_list = ($this->cache == null) ? false : $this->cache->get('_avatar_local_list_' . $user->data['user_lang']);
161
162        if ($avatar_list === false)
163        {
164            $avatar_list = array();
165            $path = $this->phpbb_root_path . $this->config['avatar_gallery_path'];
166
167            $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS), \RecursiveIteratorIterator::SELF_FIRST);
168            foreach ($iterator as $file_info)
169            {
170                $file_path = $file_info->getPath();
171                $image = $file_info->getFilename();
172
173                // Match all images in the gallery folder
174                if (preg_match('#^[^&\'"<>]+\.(?:' . implode('|', $this->allowed_extensions) . ')$#i', $image) && is_file($file_path . '/' . $image))
175                {
176                    $dims = $this->imagesize->getImageSize($file_path . '/' . $image);
177
178                    if ($dims === false)
179                    {
180                        $dims = array(0, 0);
181                    }
182                    else
183                    {
184                        $dims = array($dims['width'], $dims['height']);
185                    }
186                    $cat = ($path == $file_path) ? $user->lang['NO_AVATAR_CATEGORY'] : str_replace("$path/", '', $file_path);
187                    $avatar_list[$cat][$image] = array(
188                        'file'      => ($cat != $user->lang['NO_AVATAR_CATEGORY']) ? str_replace('%2F', '/', rawurlencode($cat)) . '/' . rawurlencode($image) : rawurlencode($image),
189                        'filename'  => rawurlencode($image),
190                        'name'      => ucfirst(str_replace('_', ' ', preg_replace('#^(.*)\..*$#', '\1', $image))),
191                        'width'     => $dims[0],
192                        'height'    => $dims[1],
193                    );
194                }
195            }
196            ksort($avatar_list);
197
198            if ($this->cache != null)
199            {
200                $this->cache->put('_avatar_local_list_' . $user->data['user_lang'], $avatar_list, 86400);
201            }
202        }
203
204        return $avatar_list;
205    }
206}