aboutsummaryrefslogtreecommitdiff
blob: 611a44cb3d2080945e507483c22b2f7feabc2cb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
*
* @package phpBB3
* @copyright (c) 2011 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/

namespace phpbb\avatar\driver;

/**
* Handles avatars selected from the board gallery
* @package phpBB3
*/
class local extends \phpbb\avatar\driver\driver
{
	/**
	* @inheritdoc
	*/
	public function get_data($row)
	{
		return array(
			'src' => $this->path_helper->get_web_root_path() . $this->config['avatar_gallery_path'] . '/' . $row['avatar'],
			'width' => $row['avatar_width'],
			'height' => $row['avatar_height'],
		);
	}

	/**
	* @inheritdoc
	*/
	public function prepare_form($request, $template, $user, $row, &$error)
	{
		$avatar_list = $this->get_avatar_list($user);
		$category = $request->variable('avatar_local_cat', '');

		foreach ($avatar_list as $cat => $null)
		{
			if (!empty($avatar_list[$cat]))
			{
				$template->assign_block_vars('avatar_local_cats', array(
					'NAME' => $cat,
					'SELECTED' => ($cat == $category),
				));
			}

			if ($cat != $category)
			{
				unset($avatar_list[$cat]);
			}
		}

		if (!empty($avatar_list[$category]))
		{
			$template->assign_vars(array(
				'AVATAR_LOCAL_SHOW' => true,
			));

			$table_cols = isset($row['avatar_gallery_cols']) ? $row['avatar_gallery_cols'] : 4;
			$row_count = $col_count = $avatar_pos = 0;
			$avatar_count = sizeof($avatar_list[$category]);

			reset($avatar_list[$category]);

			while ($avatar_pos < $avatar_count)
			{
				$img = current($avatar_list[$category]);
				next($avatar_list[$category]);

				if ($col_count == 0)
				{
					++$row_count;
					$template->assign_block_vars('avatar_local_row', array(
					));
				}

				$template->assign_block_vars('avatar_local_row.avatar_local_col', array(
					'AVATAR_IMAGE'  => $this->phpbb_root_path . $this->config['avatar_gallery_path'] . '/' . $img['file'],
					'AVATAR_NAME' 	=> $img['name'],
					'AVATAR_FILE' 	=> $img['filename'],
				));

				$template->assign_block_vars('avatar_local_row.avatar_local_option', array(
					'AVATAR_FILE' 		=> $img['filename'],
					'S_OPTIONS_AVATAR'	=> $img['filename']
				));

				$col_count = ($col_count + 1) % $table_cols;

				++$avatar_pos;
			}
		}

		return true;
	}

	/**
	* @inheritdoc
	*/
	public function prepare_form_acp($user)
	{
		return array(
			'avatar_gallery_path'	=> array('lang' => 'AVATAR_GALLERY_PATH',	'validate' => 'rpath',	'type' => 'text:20:255', 'explain' => true),
		);
	}

	/**
	* @inheritdoc
	*/
	public function process_form($request, $template, $user, $row, &$error)
	{
		$avatar_list = $this->get_avatar_list($user);
		$category = $request->variable('avatar_local_cat', '');

		$file = $request->variable('avatar_local_file', '');

		if (empty($category) || empty($file))
		{
			$error[] = 'NO_AVATAR_SELECTED';
			return false;
		}

		if (!isset($avatar_list[$category][urldecode($file)]))
		{
			$error[] = 'AVATAR_URL_NOT_FOUND';
			return false;
		}

		return array(
			'avatar' => ($category != $user->lang['MAIN']) ? $category . '/' . $file : $file,
			'avatar_width' => $avatar_list[$category][urldecode($file)]['width'],
			'avatar_height' => $avatar_list[$category][urldecode($file)]['height'],
		);
	}

	/**
	* @inheritdoc
	*/
	public function get_template_name()
	{
		return 'ucp_avatar_options_local.html';
	}

	/**
	* Get a list of avatars that are locally available
	* Results get cached for 24 hours (86400 seconds)
	*
	* @param \phpbb\user $user User object
	*
	* @return array Array containing the locally available avatars
	*/
	protected function get_avatar_list($user)
	{
		$avatar_list = ($this->cache == null) ? false : $this->cache->get('avatar_local_list');

		if ($avatar_list === false)
		{
			$avatar_list = array();
			$path = $this->phpbb_root_path . $this->config['avatar_gallery_path'];

			$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS), \RecursiveIteratorIterator::SELF_FIRST);
			foreach ($iterator as $file_info)
			{
				$file_path = $file_info->getPath();
				$image = $file_info->getFilename();

				// Match all images in the gallery folder
				if (preg_match('#^[^&\'"<>]+\.(?:' . implode('|', $this->allowed_extensions) . ')$#i', $image) && is_file($file_path . '/' . $image))
				{
					if (function_exists('getimagesize'))
					{
						$dims = getimagesize($file_path . '/' . $image);
					}
					else
					{
						$dims = array(0, 0);
					}
					$cat = ($path == $file_path) ? $user->lang['MAIN'] : str_replace("$path/", '', $file_path);
					$avatar_list[$cat][$image] = array(
						'file'      => ($cat != $user->lang['MAIN']) ? rawurlencode($cat) . '/' . rawurlencode($image) : rawurlencode($image),
						'filename'  => rawurlencode($image),
						'name'      => ucfirst(str_replace('_', ' ', preg_replace('#^(.*)\..*$#', '\1', $image))),
						'width'     => $dims[0],
						'height'    => $dims[1],
					);
				}
			}
			ksort($avatar_list);

			if ($this->cache != null)
			{
				$this->cache->put('avatar_local_list', $avatar_list, 86400);
			}
		}

		return $avatar_list;
	}
}