/components/com_jomcomment/libraries/avatar.class.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012 · PHP · 476 lines · 342 code · 91 blank · 43 comment · 89 complexity · f911a276a606683c68e652aa678e0e63 MD5 · raw file

  1. <?php
  2. /**
  3. * @copyright (C) 2007 by Slashes & Dots Sdn Bhd - All rights reserved!
  4. * @license http://www.azrul.com Copyrighted Commercial Software
  5. *
  6. * Rem:
  7. * This file is responsible for getting the avatars and displaying avatars
  8. **/
  9. (defined('_VALID_MOS') OR defined('_JEXEC')) or die('Direct Access to this location is not allowed.');
  10. // Include our custom cmslib if its not defined
  11. if(!defined('CMSLIB_DEFINED'))
  12. include_once ((dirname((dirname(dirname(dirname(__FILE__)))))) . '/components/libraries/cmslib/spframework.php');
  13. if(!class_exists('JCAvatarDB'))
  14. include_once(JC_MODEL_PATH . '/avatar.db.php');
  15. if( class_exists('JCAvatar') || class_exists('JCAvatarCB') ||
  16. class_exists('JCAvatarFireboard') || class_exists('JCAvatar') ||
  17. class_exists('JCAvatarSMF') )
  18. return;
  19. class JCAvatar{
  20. var $cms = '';
  21. var $default = '';
  22. var $model = null;
  23. var $userId = null;
  24. var $email = null;
  25. var $source = null; // Source of the avatar image.
  26. var $_width = null;
  27. var $_height = null;
  28. function JCAvatar($userId, $email = null){
  29. global $_JC_CONFIG;
  30. $this->cms =& cmsInstance('CMSCore');
  31. $this->cms->load('helper','url');
  32. // Instantiate the model object
  33. $this->model = new JCAvatarDB();
  34. // Default avatar if avatar could not be found
  35. $this->default = $this->cms->get_path('live') . '/components/com_jomcomment/smilies/guest.gif';
  36. $this->userId = $userId;
  37. if($email)
  38. $this->email = $email;
  39. $this->_width = $_JC_CONFIG->get('gWidth');
  40. $this->_height = $_JC_CONFIG->get('gHeight');
  41. }
  42. /**
  43. * _link()
  44. * Returns: The link to the avatar component. E.g: fireboard (string)
  45. **/
  46. function link($userId, $userEmail = null, $ItemId = null){
  47. global $_JC_CONFIG;
  48. if($userId == '0')
  49. return false;
  50. switch ($_JC_CONFIG->get('gravatar')) {
  51. case "cb" :
  52. $link = $this->_link($userId, $ItemId);
  53. break;
  54. case "smf":
  55. $link = $this->_link($userId, $userEmail);
  56. default:
  57. $link = false;
  58. }
  59. return $link;
  60. }
  61. /**
  62. * _img()
  63. * Returns: The image of the specific avatar (string)
  64. **/
  65. function img(){
  66. if($this->_init())
  67. return "<img src=\"{$this->source}\" height=\"{$this->_height}\" width=\"{$this->_width}\" border=\"0\" alt=\"{$this->userId}\" />";
  68. else
  69. return "<img src=\"{$this->default}\" height=\"{$this->_height}\" width=\"{$this->_width}\" border=\"0\" alt=\"{$this->userId}\" />";
  70. }
  71. }
  72. class JCAvatarFireboard extends JCAvatar{
  73. // Private properties
  74. var $_config = null;
  75. function JCAvatarFireboard($userId){
  76. parent::JCAvatar($userId);
  77. $this->_config = $this->cms->get_path('root') . '/administrator/components/com_fireboard/fireboard_config.php';
  78. // Test if this is prior to 1.0.5
  79. if(!file_exists( $this->_config) ){
  80. // We assume it might be version 1.0.5 and above
  81. $this->_config = $this->cms->get_path('root') . '/components/com_fireboard/sources/fb_config.class.php';
  82. if( file_exists($this->_config) )
  83. {
  84. require_once( $this->_config );
  85. global $fbConfig;
  86. $this->_config = new fb_config();
  87. $this->_config->load();
  88. }
  89. }
  90. }
  91. function _loadVars()
  92. {
  93. if( !is_object( $this->_config ) )
  94. {
  95. require( $this->_config );
  96. global $fbConfig;
  97. $obj = new stdclass();
  98. $obj->avatar_src = $fbConfig['avatar_src'];
  99. $obj->version = $fbConfig['version'];
  100. $this->_config = $obj;
  101. }
  102. }
  103. function _init(){
  104. if( !is_object($this->_config) && !file_exists($this->_config) ){
  105. return false;
  106. }
  107. $this->_loadVars();
  108. echo '<pre>';
  109. print_r($this->_config);
  110. echo '</pre>';
  111. if($this->_config->avatar_src == 'fb'){
  112. $path = $this->model->getFireboard($this->userId);
  113. if($path){
  114. $fireboardAvatar = '';
  115. if(!is_null($this->_config->version) && isset($this->_config->version) && $this->_config->version == '1.0.1')
  116. {
  117. $fireboardAvatar = '/components/com_fireboard/avatars/' . $path;
  118. } else {
  119. // Newer versions
  120. $fireboardAvatar = '/images/fbfiles/avatars/' . $path;
  121. }
  122. if(file_exists($this->cms->get_path('root') . $fireboardAvatar)){
  123. $this->source = $this->cms->get_path('live') . $fireboardAvatar;
  124. } else {
  125. // Image file doesn't exist.
  126. return false;
  127. }
  128. }else {
  129. // Not set avatar yet.
  130. return false;
  131. }
  132. } else if ($this->_config->avatar_src != 'cb') {
  133. // This is for handling cases when fireboard avatar source is Clexus PM
  134. // we just use the default avatar for this.
  135. return false;
  136. }
  137. return true;
  138. }
  139. }
  140. class JCAvatarSMF extends JCAvatar{
  141. var $_config = null;
  142. var $_path = null;
  143. function JCAvatarSMF($userId, $email){
  144. global $_JC_CONFIG;
  145. parent::JCAvatar($userId, $email);
  146. $this->_path = rtrim(trim($_JC_CONFIG->get('smfPath')), '/');
  147. $this->_config = $this->_path . '/Settings.php';
  148. if(!$this->_path || $this->_path == null || !file_exists($this->_config)){
  149. // If path specified in admin section is invalid or does not exists,
  150. // we assume that the default would be in /forum
  151. $this->_path = $this->cms->get_path('root') . '/forum';
  152. }
  153. // If the folder /forum still does not exists, we need to check with the database.
  154. if(!$this->_path || $this->_path == null || !file_exists($this->_config)){
  155. if($this->_path = $this->model->existsSMF()){
  156. $this->_path = rtrim( str_replace("\\", "/", $this->_path), "/");
  157. }
  158. }
  159. }
  160. function _init(){
  161. // Check if configurations exists.
  162. if(file_exists($this->_config)){
  163. global $context, $txt, $smfSettings, $mainframe, $db_name, $db_prefix;
  164. include($this->_config);
  165. $smfSettings = $this->_settings($db_name, $db_prefix);
  166. // Check if email even exists in SMF database
  167. if($this->model->getSMFEmail($db_name, $db_prefix, $this->email) == 0){
  168. return false;
  169. }
  170. if($user = $this->model->getSMFEmail($db_name, $db_prefix, $this->email, true)){
  171. $user->ID_ATTACH = $this->model->getSMF($db_name, $db_prefix, $user->ID_MEMBER);
  172. $context = array();
  173. if($user->avatar == '' && $user->ID_ATTACH > 0){
  174. $context['member']['avatar'] = array('choice' => 'upload',
  175. 'server_pic'=> 'blank.gif',
  176. 'external' => 'http://'
  177. );
  178. }else if(stristr($user->avatar, 'http://')){
  179. $context['member']['avatar'] = array('choice' => 'external',
  180. 'server_pic'=> 'blank.gif',
  181. 'external' => $user->avatar
  182. );
  183. }else if(file_exists($smfSettings['avatar_directory'] . '/' . $user->avatar)){
  184. $context['member']['avatar'] = array('choice' => 'server_stored',
  185. 'server_pic' => $user->avatar == '' ? 'blank.gif' : $user->avatar,
  186. 'external' => 'http://'
  187. );
  188. }else{
  189. $context['member']['avatar'] = array('choice' => 'server_stored',
  190. 'server_pic'=> 'blank.gif',
  191. 'external' => 'http://'
  192. );
  193. }
  194. if(@$context['member']['avatar']['allow_server_stored']){
  195. $context['avatar_list'] = array();
  196. $context['avatars'] = is_dir($smfSettings['avatar_directory']) ? $this->_lists('', 0) : array();
  197. } else {
  198. $context['avatars'] = array();
  199. }
  200. // Second level selected avatars
  201. $context['avatar_selected'] = substr(strrchr($context['member']['avatar']['server_pic'], '/'), 1);
  202. switch($context['member']['avatar']['choice']){
  203. case 'external':
  204. $this->source = $user->avatar;
  205. break;
  206. case 'upload':
  207. // User could upload their own avatar.
  208. $objData = $this->model->getSMF($db_name, $db_prefix, $user->ID_MEMBER, true);
  209. if($objData->type == '1'){
  210. // Admin specified another directory apart from the default avatars.
  211. $this->source = $smfSettings['custom_avatar_url'] . '/' . $objData->filename;
  212. }
  213. else
  214. $this->source = $boardurl . '/index.php?action=dlattach;attach=' . $user->ID_ATTACH . ';type=avatar';
  215. break;
  216. case 'server_stored':
  217. if(!empty($smfSettings['custom_avatar_url'])){
  218. $path = $this->model->getSMF($db_name, $db_prefix, $user->ID_MEMBER, true);
  219. if(!empty($path)){
  220. if(stristr($smfSettings['custom_avatar_url'], 'http://')){
  221. $this->source = $smfSettings['custom_avatar_url'] . '/' . $path;
  222. }
  223. else
  224. $this->source = rtrim($boardurl, '/') . '/' . $smfSettings['custom_avatar_url'] . '/' . $path;
  225. } else {
  226. $this->source = $smfSettings['avatar_url'] . '/' . $user->avatar;
  227. }
  228. } else {
  229. $this->source = $smfSettings['avatar_url'] . '/' . $user->avatar;
  230. }
  231. break;
  232. default:
  233. return false;
  234. }
  235. } else {
  236. // Email not found.
  237. return false;
  238. }
  239. }else
  240. return false;
  241. return true;
  242. }
  243. function _link($userId, $userEmail){
  244. global $_JC_CONFIG;
  245. if(substr($this->_path, strlen($this->_path) - 1, 1) == "/")
  246. $this->_path = substr($this->_path, 0, strlen($this->_path) - 1);
  247. // Check if configurations exists.
  248. if(file_exists($this->_config)){
  249. include($this->_config);
  250. if($result = $this->model->getSMFId($db_name, $db_prefix, $userEmail)){
  251. $wrap = '';
  252. // Administrator could force wrap to smf profile page in Jom Comment settings.
  253. if($_JC_CONFIG->get('smfWrapped'))
  254. $wrap = true;
  255. if($wrap){
  256. $link = cmsSefAmpReplace('index.php?option=com_smf&action=profile&u=' . $result[0]->ID_MEMBER . '&Itemid=' . $this->model->getSMFItemId());
  257. } else
  258. $link = cmsSefAmpReplace($boardurl . '/index.php?action=profile&u=' . $result[0]->ID_MEMBER);
  259. } else
  260. return false;
  261. } else
  262. return false;
  263. return $link;
  264. }
  265. // Recursive function to retrieve avatar files
  266. function _lists($directory, $level){
  267. global $context, $txt, $smfSettings;
  268. $result = array();
  269. // Open the directory..
  270. $dir = dir($smfSettings['avatar_directory'] . (!empty($directory) ? '/' : '') . $directory);
  271. $dirs = array();
  272. $files = array();
  273. if (!$dir)
  274. return array();
  275. while ($line = $dir->read())
  276. {
  277. if (in_array($line, array('.', '..', 'blank.gif', 'index.php')))
  278. continue;
  279. if (is_dir($smfSettings['avatar_directory'] . '/' . $directory . (!empty($directory) ? '/' : '') . $line))
  280. $dirs[] = $line;
  281. else
  282. $files[] = $line;
  283. }
  284. $dir->close();
  285. // Sort the results...
  286. natcasesort($dirs);
  287. natcasesort($files);
  288. if ($level == 0)
  289. {
  290. $result[] = array(
  291. 'filename' => 'blank.gif',
  292. 'checked' => in_array($context['member']['avatar']['server_pic'], array('', 'blank.gif')),
  293. 'name' => &$txt[422],
  294. 'is_dir' => false
  295. );
  296. }
  297. foreach ($dirs as $line)
  298. {
  299. $tmp = getAvatars($directory . (!empty($directory) ? '/' : '') . $line, $level + 1);
  300. if (!empty($tmp))
  301. $result[] = array(
  302. 'filename' => htmlspecialchars($line),
  303. 'checked' => strpos($context['member']['avatar']['server_pic'], $line . '/') !== false,
  304. 'name' => '[' . htmlspecialchars(str_replace('_', ' ', $line)) . ']',
  305. 'is_dir' => true,
  306. 'files' => $tmp
  307. );
  308. unset($tmp);
  309. }
  310. foreach ($files as $line)
  311. {
  312. $filename = substr($line, 0, (strlen($line) - strlen(strrchr($line, '.'))));
  313. $extension = substr(strrchr($line, '.'), 1);
  314. // Make sure it is an image.
  315. if (strcasecmp($extension, 'gif') != 0 && strcasecmp($extension, 'jpg') != 0 && strcasecmp($extension, 'jpeg') != 0 && strcasecmp($extension, 'png') != 0 && strcasecmp($extension, 'bmp') != 0)
  316. continue;
  317. $result[] = array(
  318. 'filename' => htmlspecialchars($line),
  319. 'checked' => $line == $context['member']['avatar']['server_pic'],
  320. 'name' => htmlspecialchars(str_replace('_', ' ', $filename)),
  321. 'is_dir' => false
  322. );
  323. if ($level == 1)
  324. $context['avatar_list'][] = $directory . '/' . $line;
  325. }
  326. return $result;
  327. }
  328. function _settings($dbName, $dbPrefix){
  329. static $smfSetting = array();
  330. if(count($smfSetting) == 0){
  331. $settings = $this->model->getSMFSettings($dbName, $dbPrefix);
  332. foreach($settings as $setting){
  333. $smfSetting[$setting->variable] = $setting->value;
  334. }
  335. }
  336. return $smfSetting;
  337. }
  338. }
  339. class JCAvatarCB extends JCAvatar{
  340. function JCAvatarCB($userId){
  341. parent::JCAvatar($userId);
  342. }
  343. function _init(){
  344. $path = $this->model->getCB($this->userId);
  345. if($path){
  346. // Community builder might store the images in either 2 folders
  347. if(file_exists($this->cms->get_path('root') . '/components/com_comprofiler/images/' . $path))
  348. $this->source = $this->cms->get_path('live') . '/components/com_comprofiler/images/' . $path;
  349. else if (file_exists($this->cms->get_path('root') . '/images/comprofiler/' . $path))
  350. $this->source = $this->cms->get_path('live') . '/images/comprofiler/' . $path;
  351. else
  352. return false;
  353. }else
  354. return false;
  355. return true;
  356. }
  357. function _link($userId, $Itemid){
  358. return cmsSefAmpReplace("index.php?option=com_comprofiler&task=userProfile&user=$userId&Itemid=$Itemid");
  359. }
  360. }
  361. class JCAvatarGravatar extends JCAvatar{
  362. // Private properties.
  363. var $_width = '40';
  364. function JCAvatarGravatar($userId, $email){
  365. parent::JCAvatar($userId, $email);
  366. }
  367. function _init(){
  368. $this->source = 'http://www.gravatar.com/avatar.php?gravatar_id=' . md5($this->email)
  369. . '&amp;default=' . urlencode($this->default)
  370. . '&amp;size=' . $this->_width;
  371. return true;
  372. }
  373. }
  374. class JCAvatarDefault extends JCAvatar{
  375. function JCAvatarDefault($userId){
  376. parent::JCAvatar($userId);
  377. }
  378. function _init(){
  379. return false;
  380. }
  381. function img(){
  382. return "";
  383. }
  384. }
  385. ?>