annamaria-daneswood-25102012 /components/com_jomcomment/libraries/avatar.class.php

Language PHP Lines 476
MD5 Hash f911a276a606683c68e652aa678e0e63 Estimated Cost $8,704 (why?)
Repository https://bitbucket.org/dgough/annamaria-daneswood-25102012.git View Raw File View Project SPDX
  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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<?php
/**
 * @copyright (C) 2007 by Slashes & Dots Sdn Bhd - All rights reserved!
 * @license http://www.azrul.com Copyrighted Commercial Software
 *
 * Rem:
 * This file is responsible for getting the avatars and displaying avatars
 **/
(defined('_VALID_MOS') OR defined('_JEXEC')) or die('Direct Access to this location is not allowed.');

// Include our custom cmslib if its not defined
if(!defined('CMSLIB_DEFINED'))
	include_once ((dirname((dirname(dirname(dirname(__FILE__)))))) . '/components/libraries/cmslib/spframework.php');

if(!class_exists('JCAvatarDB'))
	include_once(JC_MODEL_PATH . '/avatar.db.php');

if( class_exists('JCAvatar') || class_exists('JCAvatarCB') ||
	class_exists('JCAvatarFireboard') || class_exists('JCAvatar') ||
	class_exists('JCAvatarSMF') )
	return;

class JCAvatar{

	var $cms    	= '';
	var $default    = '';
	var $model      = null;
	var $userId     = null;
	var $email      = null;
	var $source     = null;     // Source of the avatar image.
	
	var $_width     = null;
	var $_height    = null;
	function JCAvatar($userId, $email = null){
		global $_JC_CONFIG;
		
	    $this->cms  =& cmsInstance('CMSCore');
		$this->cms->load('helper','url');

		// Instantiate the model object
		$this->model    = new JCAvatarDB();
		
	    // Default avatar if avatar could not be found
	    $this->default	= $this->cms->get_path('live') . '/components/com_jomcomment/smilies/guest.gif';
	    
	    $this->userId   = $userId;
	    
	    if($email)
			$this->email    = $email;
			
		$this->_width   = $_JC_CONFIG->get('gWidth');
		$this->_height  = $_JC_CONFIG->get('gHeight');
	}

	/**
	 *  _link()
	 *  Returns: The link to the avatar component. E.g: fireboard (string)
	 **/
	function link($userId, $userEmail = null, $ItemId = null){
	    global $_JC_CONFIG;
		
		if($userId == '0')
			return false;

		switch ($_JC_CONFIG->get('gravatar')) {
			case "cb" :
				$link	= $this->_link($userId, $ItemId);
				break;
			case "smf":
				$link	= $this->_link($userId, $userEmail);
			default:
			    $link   = false;
		}
		return $link;
	}

	/**
	 *  _img()
	 *  Returns: The image of the specific avatar (string)
	 **/
	function img(){
	    if($this->_init())
	        return "<img src=\"{$this->source}\" height=\"{$this->_height}\" width=\"{$this->_width}\" border=\"0\" alt=\"{$this->userId}\" />";
		else
		    return "<img src=\"{$this->default}\" height=\"{$this->_height}\" width=\"{$this->_width}\" border=\"0\" alt=\"{$this->userId}\" />";
	}
}

class JCAvatarFireboard extends JCAvatar{
	// Private properties
	var $_config 	= null;

	function JCAvatarFireboard($userId){
		parent::JCAvatar($userId);

		$this->_config  = $this->cms->get_path('root') . '/administrator/components/com_fireboard/fireboard_config.php';

		// Test if this is prior to 1.0.5
		if(!file_exists( $this->_config) ){
			// We assume it might be version 1.0.5 and above
			$this->_config	= $this->cms->get_path('root') . '/components/com_fireboard/sources/fb_config.class.php';
			
			if( file_exists($this->_config) )
			{
				require_once( $this->_config );
				global $fbConfig;
				
				$this->_config	= new fb_config();
				$this->_config->load();
			}
		}
	}
	
	function _loadVars()
	{
		if( !is_object( $this->_config ) )
		{
			require( $this->_config );
			global $fbConfig;
		
			$obj	= new stdclass();
				
			$obj->avatar_src	= $fbConfig['avatar_src'];
			$obj->version		= $fbConfig['version'];
			
			$this->_config	= $obj;
		}
		
	}
	
	function _init(){
		
	    if( !is_object($this->_config) && !file_exists($this->_config) ){
	        return false;
		}
		
		$this->_loadVars();

		echo '<pre>';
		print_r($this->_config);
		echo '</pre>';
		if($this->_config->avatar_src == 'fb'){
		    $path   = $this->model->getFireboard($this->userId);
		    
		    if($path){
		        $fireboardAvatar    = '';
		        
				if(!is_null($this->_config->version) && isset($this->_config->version) && $this->_config->version == '1.0.1')
				{
				    $fireboardAvatar    = '/components/com_fireboard/avatars/' . $path;
				} else {
				    // Newer versions
				    $fireboardAvatar    = '/images/fbfiles/avatars/' . $path;
				}
				
				if(file_exists($this->cms->get_path('root') . $fireboardAvatar)){
					$this->source   = $this->cms->get_path('live') . $fireboardAvatar;
				} else {
				    // Image file doesn't exist.
				    return false;
				}
			}else {
			    // Not set avatar yet.
			    return false;
			}
		} else if ($this->_config->avatar_src != 'cb') {
			// This is for handling cases when fireboard avatar source is Clexus PM
			// we just use the default avatar for this.
			return false;
        }
		return true;
	}
}

class JCAvatarSMF extends JCAvatar{
	var $_config    = null;
	var $_path      = null;
	
	function JCAvatarSMF($userId, $email){
	    global $_JC_CONFIG;
	    
	    parent::JCAvatar($userId, $email);
	    $this->_path  	= rtrim(trim($_JC_CONFIG->get('smfPath')), '/');
		$this->_config  = $this->_path . '/Settings.php';

		if(!$this->_path || $this->_path == null || !file_exists($this->_config)){
		    // If path specified in admin section is invalid or does not exists,
		    // we assume that the default would be in /forum
		    $this->_path    = $this->cms->get_path('root') . '/forum';
		}

		// If the folder /forum still does not exists, we need to check with the database.
		if(!$this->_path || $this->_path == null || !file_exists($this->_config)){
		    if($this->_path = $this->model->existsSMF()){
		        $this->_path    = rtrim( str_replace("\\", "/", $this->_path), "/");
			}
		}
		
	    
	}
	
	function _init(){
		// Check if configurations exists.
		if(file_exists($this->_config)){
			global $context, $txt, $smfSettings, $mainframe, $db_name, $db_prefix;

			include($this->_config);

			$smfSettings    = $this->_settings($db_name, $db_prefix);

			// Check if email even exists in SMF database
			if($this->model->getSMFEmail($db_name, $db_prefix, $this->email) == 0){
			    return false;
			}
			
			if($user = $this->model->getSMFEmail($db_name, $db_prefix, $this->email, true)){
			     $user->ID_ATTACH   = $this->model->getSMF($db_name, $db_prefix, $user->ID_MEMBER);

			     $context   = array();
			     
			     if($user->avatar == '' && $user->ID_ATTACH > 0){
			        $context['member']['avatar']    = array('choice'    => 'upload',
			                                                'server_pic'=> 'blank.gif',
			                                                'external'  => 'http://'
															);
				 }else if(stristr($user->avatar, 'http://')){
				    $context['member']['avatar']    = array('choice'	=> 'external',
				                                            'server_pic'=> 'blank.gif',
				                                            'external'  => $user->avatar
															);
				 }else if(file_exists($smfSettings['avatar_directory'] . '/' . $user->avatar)){
				    $context['member']['avatar']    = array('choice' => 'server_stored',
															'server_pic' => $user->avatar == '' ? 'blank.gif' : $user->avatar,
															'external' => 'http://'
															);
				 }else{
				    $context['member']['avatar']    = array('choice'	=> 'server_stored',
				                                            'server_pic'=> 'blank.gif',
				                                            'external'  => 'http://'
															);
				 }
				 
				 if(@$context['member']['avatar']['allow_server_stored']){
				    $context['avatar_list'] = array();
				    $context['avatars']     = is_dir($smfSettings['avatar_directory']) ? $this->_lists('', 0) : array();
				 } else {
				    $context['avatars']     = array();
				 }
				 
				 // Second level selected avatars
				 $context['avatar_selected']    = substr(strrchr($context['member']['avatar']['server_pic'], '/'), 1);

				 switch($context['member']['avatar']['choice']){
				    case 'external':
				        $this->source   = $user->avatar;
				        break;
					case 'upload':
					    // User could upload their own avatar.
					    $objData = $this->model->getSMF($db_name, $db_prefix, $user->ID_MEMBER, true);

					    if($objData->type == '1'){
					        // Admin specified another directory apart from the default avatars.
					        $this->source   = $smfSettings['custom_avatar_url'] . '/' . $objData->filename;
						}
						else
					    	$this->source   = $boardurl . '/index.php?action=dlattach;attach=' . $user->ID_ATTACH . ';type=avatar';
					    break;
					case 'server_stored':
					    if(!empty($smfSettings['custom_avatar_url'])){
					        $path	= $this->model->getSMF($db_name, $db_prefix, $user->ID_MEMBER, true);
							if(!empty($path)){
							    if(stristr($smfSettings['custom_avatar_url'], 'http://')){
									$this->source   = $smfSettings['custom_avatar_url'] . '/' . $path;
								}
								else
								    $this->source   = rtrim($boardurl, '/') . '/' . $smfSettings['custom_avatar_url'] . '/' . $path;
							} else {
							    $this->source   = $smfSettings['avatar_url'] . '/' . $user->avatar;
							}
						} else {
						    $this->source   = $smfSettings['avatar_url'] . '/' . $user->avatar;
						}
						break;
					default:
					    return false;
				 }
				 
			} else {
			    // Email not found.
			    return false;
			}
		}else
		    return false;

		return true;
	}

	function _link($userId, $userEmail){
	    global $_JC_CONFIG;
	    
		if(substr($this->_path, strlen($this->_path) - 1, 1) == "/")
				$this->_path = substr($this->_path, 0, strlen($this->_path) - 1);

		// Check if configurations exists.
		if(file_exists($this->_config)){
			include($this->_config);

			if($result = $this->model->getSMFId($db_name, $db_prefix, $userEmail)){

			    $wrap   = '';

				// Administrator could force wrap to smf profile page in Jom Comment settings.
				if($_JC_CONFIG->get('smfWrapped'))
				    $wrap   = true;

				if($wrap){
					$link   = cmsSefAmpReplace('index.php?option=com_smf&action=profile&u=' . $result[0]->ID_MEMBER . '&Itemid=' . $this->model->getSMFItemId());
				} else
				    $link   = cmsSefAmpReplace($boardurl . '/index.php?action=profile&u=' . $result[0]->ID_MEMBER);
			} else
			    return false;
		} else
		    return false;
		    
		return $link;
	}

	// Recursive function to retrieve avatar files
	function _lists($directory, $level){
		global $context, $txt, $smfSettings;

		$result = array();

		// Open the directory..
		$dir = dir($smfSettings['avatar_directory'] . (!empty($directory) ? '/' : '') . $directory);
		$dirs = array();
		$files = array();

		if (!$dir)
			return array();

		while ($line = $dir->read())
		{
			if (in_array($line, array('.', '..', 'blank.gif', 'index.php')))
				continue;

			if (is_dir($smfSettings['avatar_directory'] . '/' . $directory . (!empty($directory) ? '/' : '') . $line))
				$dirs[] = $line;
			else
				$files[] = $line;
		}
		$dir->close();

		// Sort the results...
		natcasesort($dirs);
		natcasesort($files);

		if ($level == 0)
		{
			$result[] = array(
				'filename' => 'blank.gif',
				'checked' => in_array($context['member']['avatar']['server_pic'], array('', 'blank.gif')),
				'name' => &$txt[422],
				'is_dir' => false
			);
		}

		foreach ($dirs as $line)
		{
			$tmp = getAvatars($directory . (!empty($directory) ? '/' : '') . $line, $level + 1);
			if (!empty($tmp))
				$result[] = array(
					'filename' => htmlspecialchars($line),
					'checked' => strpos($context['member']['avatar']['server_pic'], $line . '/') !== false,
					'name' => '[' . htmlspecialchars(str_replace('_', ' ', $line)) . ']',
					'is_dir' => true,
					'files' => $tmp
			);
			unset($tmp);
		}

		foreach ($files as $line)
		{
			$filename = substr($line, 0, (strlen($line) - strlen(strrchr($line, '.'))));
			$extension = substr(strrchr($line, '.'), 1);

			// Make sure it is an image.
			if (strcasecmp($extension, 'gif') != 0 && strcasecmp($extension, 'jpg') != 0 && strcasecmp($extension, 'jpeg') != 0 && strcasecmp($extension, 'png') != 0 && strcasecmp($extension, 'bmp') != 0)
				continue;

			$result[] = array(
				'filename' => htmlspecialchars($line),
				'checked' => $line == $context['member']['avatar']['server_pic'],
				'name' => htmlspecialchars(str_replace('_', ' ', $filename)),
				'is_dir' => false
			);
			if ($level == 1)
				$context['avatar_list'][] = $directory . '/' . $line;
		}

		return $result;
	}
	
	function _settings($dbName, $dbPrefix){
		static $smfSetting  = array();

		if(count($smfSetting) == 0){
			$settings   = $this->model->getSMFSettings($dbName, $dbPrefix);

			foreach($settings as $setting){
			    $smfSetting[$setting->variable] = $setting->value;
			}
		}
		return $smfSetting;
	}
}

class JCAvatarCB extends JCAvatar{

	function JCAvatarCB($userId){
	    parent::JCAvatar($userId);
	}
	
	function _init(){
		$path   = $this->model->getCB($this->userId);

		if($path){
		    // Community builder might store the images in either 2 folders
		    if(file_exists($this->cms->get_path('root') . '/components/com_comprofiler/images/' . $path))
		        $this->source   = $this->cms->get_path('live') . '/components/com_comprofiler/images/' . $path;
			else if (file_exists($this->cms->get_path('root') . '/images/comprofiler/' . $path))
			    $this->source   = $this->cms->get_path('live') . '/images/comprofiler/' . $path;
			else
			    return false;
		}else
		    return false;

		return true;
	}
	
	function _link($userId, $Itemid){
		return cmsSefAmpReplace("index.php?option=com_comprofiler&task=userProfile&user=$userId&Itemid=$Itemid");
	}
}

class JCAvatarGravatar extends JCAvatar{
	// Private properties.
	var $_width 	= '40';

	function JCAvatarGravatar($userId, $email){
	    parent::JCAvatar($userId, $email);
	}
	
	function _init(){
	
	    $this->source   = 'http://www.gravatar.com/avatar.php?gravatar_id=' . md5($this->email)
	                    . '&amp;default=' . urlencode($this->default)
						. '&amp;size=' . $this->_width;
        return true;
	}
}

class JCAvatarDefault extends JCAvatar{
	function JCAvatarDefault($userId){
	    parent::JCAvatar($userId);
	}
	
	function _init(){
	    return false;
	}
	
	function img(){
			 return "";
	}
}
?>
Back to Top