XOOPS  2.6.0
editavatar.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
13 
25 include dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'mainfile.php';
26 
29 
30 // Get Action type
31 $op = Request::getCmd('op', 'list');
32 
33 // If not a user, redirect
34 if (!$xoops->isUser()) {
35  $xoops->redirect('/index.php', 3, XoopsLocale::E_NO_ACCESS_PERMISSION);
36  exit();
37 }
38 
39 // Call header
40 $xoops->header('module:avatars/avatars_editavatar.tpl');
41 
42 // Get avatar handler
43 $avatar_Handler = $helper->getHandlerAvatar();
44 
45 // Parameters
46 $mimetypes = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png', 'image/png');
47 $upload_size = $helper->getConfig('avatars_imagefilesize');
48 $width = $helper->getConfig('avatars_imagewidth');
49 $height = $helper->getConfig('avatars_imageheight');
50 if ($helper->getConfig('avatars_allowupload') == 1
51  && $xoops->user->getVar('posts') >= $helper->getConfig('avatars_postsrequired')
52 ) {
53  $info_msg = array(
54  sprintf(AvatarsLocale::ALERT_INFO_MIMETYPES, implode(", ", $mimetypes)),
57  );
58 } else {
59  $info_msg = '';
60 }
61 
62 switch ($op) {
63 
64  case 'list':
65  default:
66  $xoops->tpl()->assign('uid', $xoops->user->getVar("uid"));
67  $xoops->tpl()->assign('info_msg', $xoops->alert('info', $info_msg, XoopsLocale::INFORMATION_FOR_UPLOADS));
68  $oldavatar = $xoops->user->getVar('user_avatar');
69  if (!empty($oldavatar) && $oldavatar != 'blank.gif') {
70  $warning_msg = '<p>' . AvatarsLocale::ALERT_WARNING_OLD .'</p>';
72  $warning_msg .= "<img src='" . $xoops_upload_url . '/' . $oldavatar ."' alt='&nbsp;' />";
73  $xoops->tpl()->assign('warning_msg', $xoops->alert('warning', $warning_msg, XoopsLocale::WARNING));
74  }
75 
76  // Create form
77  $obj = $avatar_Handler->create();
78  $form = $xoops->getModuleForm($obj, 'avatar_user');
79  // Assign form
80  $xoops->tpl()->assign('form', $form->render());
81  break;
82 
83  case "save":
84  // Check security
85  if (!$xoops->security()->check()) {
86  $xoops->redirect('/index.php', 3, implode('<br />', $xoops->security()->getErrors()));
87  }
88  $uid = Request::getInt('uid', 0);
89  if (empty($uid) || $xoops->user->getVar('uid') != $uid) {
90  $xoops->redirect('/index.php', 3, XoopsLocale::E_NO_ACCESS_PERMISSION);
91  exit();
92  }
93  $uploader_avatars_img =
94  new XoopsMediaUploader(\XoopsBaseConfig::get('uploads-path') . '/avatars', $mimetypes, $upload_size, $width, $height);
95 
96  $obj = $avatar_Handler->create();
97  $error_msg = '';
98  if ($uploader_avatars_img->fetchMedia('user_avatar')) {
99  $uploader_avatars_img->setPrefix('savt');
100  $uploader_avatars_img->fetchMedia('user_avatar');
101  if (!$uploader_avatars_img->upload()) {
102  $error_msg .= $uploader_avatars_img->getErrors();
103  $obj->setVar('avatar_file', 'avatars/blank.gif');
104  } else {
105  $obj->setVar('avatar_name', $xoops->user->getVar('uname'));
106  $obj->setVar('avatar_mimetype', $uploader_avatars_img->getMediaType());
107  $obj->setVar('avatar_file', 'avatars/' . $uploader_avatars_img->getSavedFileName());
108  $obj->setVar('avatar_display', 1);
109  $obj->setVar('avatar_type', 'C');
110 
111  if ($error_msg == '') {
112  if ($avatar_Handler->insert($obj)) {
113  $oldavatar = $xoops->user->getVar('user_avatar');
114  $criteria = new CriteriaCompo();
115  $criteria->add(new Criteria('avatar_type', 'C'));
116  $criteria->add(new Criteria('avatar_file', $oldavatar));
117  $avatars = $avatar_Handler->getObjects($criteria);
118  if (! empty($avatars) && count($avatars) == 1 && is_object($avatars[0])) {
119  $avatar_Handler->delete($avatars[0]);
120  $xoops_upload_path = \XoopsBaseConfig::get('uploads-path');
121  $oldavatar_path = realpath($xoops_upload_path . '/' . $oldavatar);
122  if (0 === strpos($oldavatar_path, realpath($xoops_upload_path))
123  && is_file($oldavatar_path)
124  ) {
125  unlink($oldavatar_path);
126  }
127  }
128  $sql = $xoops->db()->createXoopsQueryBuilder()
129  ->updatePrefix('users')
130  ->set('user_avatar', ':avatar')
131  ->where('uid = :uid')
132  ->setParameter(':uid', $xoops->user->getVar('uid'), \PDO::PARAM_INT)
133  ->setParameter(
134  ':avatar',
135  'avatars/' . $uploader_avatars_img->getSavedFileName(),
136  \PDO::PARAM_STR
137  )
138  ->execute();
139  $avatar_Handler->addUser($obj->getVar('avatar_id'), $xoops->user->getVar('uid'));
140  $xoops->redirect($xoops->url('userinfo.php?uid=' . $uid), 2, XoopsLocale::S_ITEM_SAVED);
141  }
142  $error_msg .= $obj->getHtmlErrors();
143  }
144  }
145  } else {
146  $user_avatar = Request::getString('user_avatar', 'blank.gif');
147  $oldavatar = $xoops->user->getVar('user_avatar');
148  $xoops->user->setVar('user_avatar', $user_avatar);
149  $member_handler = $xoops->getHandlerMember();
150  if (!$member_handler->insertUser($xoops->user)) {
151  echo $xoops->user->getHtmlErrors();
152  }
153  $criteria = new CriteriaCompo();
154  $criteria->add(new Criteria('avatar_type', 'C'));
155  $criteria->add(new Criteria('avatar_file', $oldavatar));
156  $avatars = $avatar_Handler->getObjects($criteria);
157  if (!empty($avatars) && count($avatars) == 1 && is_object($avatars[0])) {
158  $avatar_Handler->delete($avatars[0]);
159  $xoops_upload_path = \XoopsBaseConfig::get('uploads-path');
160  $oldavatar_path = realpath($xoops_upload_path . '/' . $oldavatar);
161  if (0 === strpos($oldavatar_path, realpath($xoops_upload_path)) && is_file($oldavatar_path)) {
162  unlink($oldavatar_path);
163  }
164  }
165  if ($user_avatar != 'blank.gif') {
166  $avatars = $avatar_Handler->getObjects(new Criteria('avatar_file', $user_avatar));
167  if (is_object($avatars[0])) {
168  $avatar_Handler->addUser($avatars[0]->getVar('avatar_id'), $xoops->user->getVar('uid'));
169  }
170  }
171  $xoops->redirect($xoops->url('userinfo.php?uid=' . $uid), 2, XoopsLocale::S_ITEM_SAVED);
172  }
173  $xoops->tpl()->assign('uid', $xoops->user->getVar("uid"));
174  $xoops->tpl()->assign('info_msg', $xoops->alert('info', $info_msg, XoopsLocale::INFORMATION_FOR_UPLOADS));
175  $xoops->tpl()->assign('error_msg', $xoops->alert('error', $error_msg, XoopsLocale::ERRORS));
176  $form = $xoops->getModuleForm($obj, 'avatar_user');
177  $xoops->tpl()->assign('form', $form->render());
178  break;
179 }
180 $xoops->footer();
$xoops
Definition: editavatar.php:27
$height
Definition: editavatar.php:49
const ALERT_WARNING_OLD
Definition: en_US.php:28
if($uname== ''||$pass== '') $member_handler
Definition: checklogin.php:44
$xoops_upload_path
const ERRORS
Definition: en_US.php:302
$mimetypes
Definition: editavatar.php:46
static getInstance()
Definition: Xoops.php:160
$op
Definition: editavatar.php:31
const INFORMATION_FOR_UPLOADS
Definition: en_US.php:480
const ALERT_INFO_MAXFILE
Definition: en_US.php:25
$form
Definition: xoops_code.php:21
exit
Definition: browse.php:104
$xoops_upload_url
Definition: misc.php:30
defined('DS') or define('DS' DIRECTORY_SEPARATOR
Definition: common.php:41
$upload_size
Definition: editavatar.php:47
static get($name)
const ALERT_INFO_MIMETYPES
Definition: en_US.php:26
if(!$xoops->isUser()) $uid
Definition: index.php:31
const ALERT_INFO_PIXELS
Definition: en_US.php:27
static getInstance()
Definition: helper.php:34
$sql
Definition: pda.php:32
const E_NO_ACCESS_PERMISSION
Definition: en_US.php:351
$criteria
const S_ITEM_SAVED
Definition: en_US.php:1028
$helper
Definition: editavatar.php:28
$avatar_Handler
Definition: editavatar.php:43
$width
Definition: editavatar.php:48
$info_msg