avatarsettings.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Upload an avatar
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Settings
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @author Zach Copley <zach@status.net>
  26. * @copyright 2008-2009 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET') && !defined('LACONICA')) {
  31. exit(1);
  32. }
  33. define('MAX_ORIGINAL', 480);
  34. /**
  35. * Upload an avatar
  36. *
  37. * We use jCrop plugin for jQuery to crop the image after upload.
  38. *
  39. * @category Settings
  40. * @package StatusNet
  41. * @author Evan Prodromou <evan@status.net>
  42. * @author Zach Copley <zach@status.net>
  43. * @author Sarven Capadisli <csarven@status.net>
  44. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  45. * @link http://status.net/
  46. */
  47. class AvatarsettingsAction extends SettingsAction
  48. {
  49. var $mode = null;
  50. var $imagefile = null;
  51. var $filename = null;
  52. /**
  53. * Title of the page
  54. *
  55. * @return string Title of the page
  56. */
  57. function title()
  58. {
  59. // TRANS: Title for avatar upload page.
  60. return _('Avatar');
  61. }
  62. /**
  63. * Instructions for use
  64. *
  65. * @return instructions for use
  66. */
  67. function getInstructions()
  68. {
  69. // TRANS: Instruction for avatar upload page.
  70. // TRANS: %s is the maximum file size, for example "500b", "10kB" or "2MB".
  71. return sprintf(_('You can upload your personal avatar. The maximum file size is %s.'),
  72. ImageFile::maxFileSize());
  73. }
  74. /**
  75. * Content area of the page
  76. *
  77. * Shows a form for uploading an avatar.
  78. *
  79. * @return void
  80. */
  81. function showContent()
  82. {
  83. if ($this->mode == 'crop') {
  84. $this->showCropForm();
  85. } else {
  86. $this->showUploadForm();
  87. }
  88. }
  89. function showUploadForm()
  90. {
  91. $user = common_current_user();
  92. $profile = $user->getProfile();
  93. if (!$profile) {
  94. common_log_db_error($user, 'SELECT', __FILE__);
  95. // TRANS: Error message displayed when referring to a user without a profile.
  96. $this->serverError(_('User has no profile.'));
  97. }
  98. $this->elementStart('form', array('enctype' => 'multipart/form-data',
  99. 'method' => 'post',
  100. 'id' => 'form_settings_avatar',
  101. 'class' => 'form_settings',
  102. 'action' =>
  103. common_local_url('avatarsettings')));
  104. $this->elementStart('fieldset');
  105. // TRANS: Avatar upload page form legend.
  106. $this->element('legend', null, _('Avatar settings'));
  107. $this->hidden('token', common_session_token());
  108. if (Event::handle('StartAvatarFormData', array($this))) {
  109. $this->elementStart('ul', 'form_data');
  110. try {
  111. $original = Avatar::getUploaded($profile);
  112. $this->elementStart('li', array('id' => 'avatar_original',
  113. 'class' => 'avatar_view'));
  114. // TRANS: Header on avatar upload page for thumbnail of originally uploaded avatar (h2).
  115. $this->element('h2', null, _("Original"));
  116. $this->elementStart('div', array('id'=>'avatar_original_view'));
  117. $this->element('img', array('src' => $original->displayUrl(),
  118. 'width' => $original->width,
  119. 'height' => $original->height,
  120. 'alt' => $user->nickname));
  121. $this->elementEnd('div');
  122. $this->elementEnd('li');
  123. } catch (NoAvatarException $e) {
  124. // No original avatar found!
  125. }
  126. try {
  127. $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
  128. $this->elementStart('li', array('id' => 'avatar_preview',
  129. 'class' => 'avatar_view'));
  130. // TRANS: Header on avatar upload page for thumbnail of to be used rendition of uploaded avatar (h2).
  131. $this->element('h2', null, _("Preview"));
  132. $this->elementStart('div', array('id'=>'avatar_preview_view'));
  133. $this->element('img', array('src' => $avatar->displayUrl(),
  134. 'width' => AVATAR_PROFILE_SIZE,
  135. 'height' => AVATAR_PROFILE_SIZE,
  136. 'alt' => $user->nickname));
  137. $this->elementEnd('div');
  138. if (!empty($avatar->filename)) {
  139. // TRANS: Button on avatar upload page to delete current avatar.
  140. $this->submit('delete', _m('BUTTON','Delete'));
  141. }
  142. $this->elementEnd('li');
  143. } catch (NoAvatarException $e) {
  144. // No previously uploaded avatar to preview.
  145. }
  146. $this->elementStart('li', array ('id' => 'settings_attach'));
  147. $this->element('input', array('name' => 'MAX_FILE_SIZE',
  148. 'type' => 'hidden',
  149. 'id' => 'MAX_FILE_SIZE',
  150. 'value' => ImageFile::maxFileSizeInt()));
  151. $this->element('input', array('name' => 'avatarfile',
  152. 'type' => 'file',
  153. 'id' => 'avatarfile'));
  154. $this->elementEnd('li');
  155. $this->elementEnd('ul');
  156. $this->elementStart('ul', 'form_actions');
  157. $this->elementStart('li');
  158. // TRANS: Button on avatar upload page to upload an avatar.
  159. $this->submit('upload', _m('BUTTON','Upload'));
  160. $this->elementEnd('li');
  161. $this->elementEnd('ul');
  162. }
  163. Event::handle('EndAvatarFormData', array($this));
  164. $this->elementEnd('fieldset');
  165. $this->elementEnd('form');
  166. }
  167. function showCropForm()
  168. {
  169. $user = common_current_user();
  170. $profile = $user->getProfile();
  171. if (!$profile) {
  172. common_log_db_error($user, 'SELECT', __FILE__);
  173. // TRANS: Error message displayed when referring to a user without a profile.
  174. $this->serverError(_('User has no profile.'));
  175. }
  176. $this->elementStart('form', array('method' => 'post',
  177. 'id' => 'form_settings_avatar',
  178. 'class' => 'form_settings',
  179. 'action' =>
  180. common_local_url('avatarsettings')));
  181. $this->elementStart('fieldset');
  182. // TRANS: Avatar upload page crop form legend.
  183. $this->element('legend', null, _('Avatar settings'));
  184. $this->hidden('token', common_session_token());
  185. $this->elementStart('ul', 'form_data');
  186. $this->elementStart('li',
  187. array('id' => 'avatar_original',
  188. 'class' => 'avatar_view'));
  189. // TRANS: Header on avatar upload crop form for thumbnail of originally uploaded avatar (h2).
  190. $this->element('h2', null, _('Original'));
  191. $this->elementStart('div', array('id'=>'avatar_original_view'));
  192. $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
  193. 'width' => $this->filedata['width'],
  194. 'height' => $this->filedata['height'],
  195. 'alt' => $user->nickname));
  196. $this->elementEnd('div');
  197. $this->elementEnd('li');
  198. $this->elementStart('li',
  199. array('id' => 'avatar_preview',
  200. 'class' => 'avatar_view'));
  201. // TRANS: Header on avatar upload crop form for thumbnail of to be used rendition of uploaded avatar (h2).
  202. $this->element('h2', null, _('Preview'));
  203. $this->elementStart('div', array('id'=>'avatar_preview_view'));
  204. $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
  205. 'width' => AVATAR_PROFILE_SIZE,
  206. 'height' => AVATAR_PROFILE_SIZE,
  207. 'alt' => $user->nickname));
  208. $this->elementEnd('div');
  209. foreach (array('avatar_crop_x', 'avatar_crop_y',
  210. 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
  211. $this->element('input', array('name' => $crop_info,
  212. 'type' => 'hidden',
  213. 'id' => $crop_info));
  214. }
  215. // TRANS: Button on avatar upload crop form to confirm a selected crop as avatar.
  216. $this->submit('crop', _m('BUTTON','Crop'));
  217. $this->elementEnd('li');
  218. $this->elementEnd('ul');
  219. $this->elementEnd('fieldset');
  220. $this->elementEnd('form');
  221. }
  222. /**
  223. * Handle a post
  224. *
  225. * We mux on the button name to figure out what the user actually wanted.
  226. *
  227. * @return void
  228. */
  229. function handlePost()
  230. {
  231. // Workaround for PHP returning empty $_POST and $_FILES when POST
  232. // length > post_max_size in php.ini
  233. if (empty($_FILES)
  234. && empty($_POST)
  235. && ($_SERVER['CONTENT_LENGTH'] > 0)
  236. ) {
  237. // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
  238. // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
  239. $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
  240. 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
  241. intval($_SERVER['CONTENT_LENGTH']));
  242. $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
  243. return;
  244. }
  245. // CSRF protection
  246. $token = $this->trimmed('token');
  247. if (!$token || $token != common_session_token()) {
  248. // TRANS: Client error displayed when the session token does not match or is not given.
  249. $this->showForm(_('There was a problem with your session token. '.
  250. 'Try again, please.'));
  251. return;
  252. }
  253. if (Event::handle('StartAvatarSaveForm', array($this))) {
  254. if ($this->arg('upload')) {
  255. $this->uploadAvatar();
  256. } else if ($this->arg('crop')) {
  257. $this->cropAvatar();
  258. } else if ($this->arg('delete')) {
  259. $this->deleteAvatar();
  260. } else {
  261. // TRANS: Unexpected validation error on avatar upload form.
  262. $this->showForm(_('Unexpected form submission.'));
  263. }
  264. Event::handle('EndAvatarSaveForm', array($this));
  265. }
  266. }
  267. /**
  268. * Handle an image upload
  269. *
  270. * Does all the magic for handling an image upload, and crops the
  271. * image by default.
  272. *
  273. * @return void
  274. */
  275. function uploadAvatar()
  276. {
  277. try {
  278. $imagefile = ImageFile::fromUpload('avatarfile');
  279. } catch (Exception $e) {
  280. $this->showForm($e->getMessage());
  281. return;
  282. }
  283. if ($imagefile === null) {
  284. // TRANS: Validation error on avatar upload form when no file was uploaded.
  285. $this->showForm(_('No file uploaded.'));
  286. return;
  287. }
  288. $cur = common_current_user();
  289. $type = $imagefile->preferredType();
  290. $filename = Avatar::filename($cur->id,
  291. image_type_to_extension($type),
  292. null,
  293. 'tmp'.common_timestamp());
  294. $filepath = Avatar::path($filename);
  295. $imagefile = $imagefile->copyTo($filepath);
  296. $filedata = array('filename' => $filename,
  297. 'filepath' => $filepath,
  298. 'width' => $imagefile->width,
  299. 'height' => $imagefile->height,
  300. 'type' => $type);
  301. $_SESSION['FILEDATA'] = $filedata;
  302. $this->filedata = $filedata;
  303. $this->mode = 'crop';
  304. // TRANS: Avatar upload form instruction after uploading a file.
  305. $this->showForm(_('Pick a square area of the image to be your avatar.'),
  306. true);
  307. }
  308. /**
  309. * Handle the results of jcrop.
  310. *
  311. * @return void
  312. */
  313. public function cropAvatar()
  314. {
  315. $filedata = $_SESSION['FILEDATA'];
  316. if (!$filedata) {
  317. // TRANS: Server error displayed if an avatar upload went wrong somehow server side.
  318. $this->serverError(_('Lost our file data.'));
  319. }
  320. $file_d = ($filedata['width'] > $filedata['height'])
  321. ? $filedata['height'] : $filedata['width'];
  322. $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
  323. $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
  324. $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$file_d;
  325. $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$file_d;
  326. $size = intval(min($dest_w, $dest_h, MAX_ORIGINAL));
  327. $user = common_current_user();
  328. $profile = $user->getProfile();
  329. $imagefile = new ImageFile($user->id, $filedata['filepath']);
  330. $filename = $imagefile->resize($size, $dest_x, $dest_y, $dest_w, $dest_h);
  331. if ($profile->setOriginal($filename)) {
  332. @unlink($filedata['filepath']);
  333. unset($_SESSION['FILEDATA']);
  334. $this->mode = 'upload';
  335. // TRANS: Success message for having updated a user avatar.
  336. $this->showForm(_('Avatar updated.'), true);
  337. } else {
  338. // TRANS: Error displayed on the avatar upload page if the avatar could not be updated for an unknown reason.
  339. $this->showForm(_('Failed updating avatar.'));
  340. }
  341. }
  342. /**
  343. * Get rid of the current avatar.
  344. *
  345. * @return void
  346. */
  347. function deleteAvatar()
  348. {
  349. $user = common_current_user();
  350. $profile = $user->getProfile();
  351. Avatar::deleteFromProfile($profile);
  352. // TRANS: Success message for deleting a user avatar.
  353. $this->showForm(_('Avatar deleted.'), true);
  354. }
  355. /**
  356. * Add the jCrop stylesheet
  357. *
  358. * @return void
  359. */
  360. function showStylesheets()
  361. {
  362. parent::showStylesheets();
  363. $this->cssLink('js/extlib/jquery-jcrop/css/jcrop.css','base','screen, projection, tv');
  364. }
  365. /**
  366. * Add the jCrop scripts
  367. *
  368. * @return void
  369. */
  370. function showScripts()
  371. {
  372. parent::showScripts();
  373. if ($this->mode == 'crop') {
  374. $this->script('extlib/jquery-jcrop/jcrop.js');
  375. $this->script('jcrop.go.js');
  376. }
  377. $this->autofocus('avatarfile');
  378. }
  379. }