avatarsettings.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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('GNUSOCIAL')) { exit(1); }
  31. /**
  32. * Upload an avatar
  33. *
  34. * We use jCrop plugin for jQuery to crop the image after upload.
  35. *
  36. * @category Settings
  37. * @package StatusNet
  38. * @author Evan Prodromou <evan@status.net>
  39. * @author Zach Copley <zach@status.net>
  40. * @author Sarven Capadisli <csarven@status.net>
  41. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  42. * @link http://status.net/
  43. */
  44. class AvatarsettingsAction extends SettingsAction
  45. {
  46. var $mode = null;
  47. var $imagefile = null;
  48. var $filename = null;
  49. /**
  50. * Title of the page
  51. *
  52. * @return string Title of the page
  53. */
  54. function title()
  55. {
  56. // TRANS: Title for avatar upload page.
  57. return _('Avatar');
  58. }
  59. /**
  60. * Instructions for use
  61. *
  62. * @return instructions for use
  63. */
  64. function getInstructions()
  65. {
  66. // TRANS: Instruction for avatar upload page.
  67. // TRANS: %s is the maximum file size, for example "500b", "10kB" or "2MB".
  68. return sprintf(_('You can upload your personal avatar. The maximum file size is %s.'),
  69. ImageFile::maxFileSize());
  70. }
  71. /**
  72. * Content area of the page
  73. *
  74. * Shows a form for uploading an avatar. Currently overrides FormAction's showContent
  75. * since we haven't made classes out of AvatarCropForm and AvatarUploadForm.
  76. *
  77. * @return void
  78. */
  79. function showContent()
  80. {
  81. if ($this->mode == 'crop') {
  82. $this->showCropForm();
  83. } else {
  84. $this->showUploadForm();
  85. }
  86. }
  87. function showUploadForm()
  88. {
  89. $user = common_current_user();
  90. $profile = $user->getProfile();
  91. if (!$profile) {
  92. common_log_db_error($user, 'SELECT', __FILE__);
  93. // TRANS: Error message displayed when referring to a user without a profile.
  94. $this->serverError(_('User has no profile.'));
  95. }
  96. $this->elementStart('form', array('enctype' => 'multipart/form-data',
  97. 'method' => 'post',
  98. 'id' => 'form_settings_avatar',
  99. 'class' => 'form_settings',
  100. 'action' =>
  101. common_local_url('avatarsettings')));
  102. $this->elementStart('fieldset');
  103. // TRANS: Avatar upload page form legend.
  104. $this->element('legend', null, _('Avatar settings'));
  105. $this->hidden('token', common_session_token());
  106. if (Event::handle('StartAvatarFormData', array($this))) {
  107. $this->elementStart('ul', 'form_data');
  108. try {
  109. $original = Avatar::getUploaded($profile);
  110. $this->elementStart('li', array('id' => 'avatar_original',
  111. 'class' => 'avatar_view'));
  112. // TRANS: Header on avatar upload page for thumbnail of originally uploaded avatar (h2).
  113. $this->element('h2', null, _("Original"));
  114. $this->elementStart('div', array('id'=>'avatar_original_view'));
  115. $this->element('img', array('src' => $original->displayUrl(),
  116. 'width' => $original->width,
  117. 'height' => $original->height,
  118. 'alt' => $user->nickname));
  119. $this->elementEnd('div');
  120. $this->elementEnd('li');
  121. } catch (NoAvatarException $e) {
  122. // No original avatar found!
  123. }
  124. try {
  125. $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
  126. $this->elementStart('li', array('id' => 'avatar_preview',
  127. 'class' => 'avatar_view'));
  128. // TRANS: Header on avatar upload page for thumbnail of to be used rendition of uploaded avatar (h2).
  129. $this->element('h2', null, _("Preview"));
  130. $this->elementStart('div', array('id'=>'avatar_preview_view'));
  131. $this->element('img', array('src' => $avatar->displayUrl(),
  132. 'width' => AVATAR_PROFILE_SIZE,
  133. 'height' => AVATAR_PROFILE_SIZE,
  134. 'alt' => $user->nickname));
  135. $this->elementEnd('div');
  136. if (!empty($avatar->filename)) {
  137. // TRANS: Button on avatar upload page to delete current avatar.
  138. $this->submit('delete', _m('BUTTON','Delete'));
  139. }
  140. $this->elementEnd('li');
  141. } catch (NoAvatarException $e) {
  142. // No previously uploaded avatar to preview.
  143. }
  144. $this->elementStart('li', array ('id' => 'settings_attach'));
  145. $this->element('input', array('name' => 'MAX_FILE_SIZE',
  146. 'type' => 'hidden',
  147. 'id' => 'MAX_FILE_SIZE',
  148. 'value' => ImageFile::maxFileSizeInt()));
  149. $this->element('input', array('name' => 'avatarfile',
  150. 'type' => 'file',
  151. 'id' => 'avatarfile'));
  152. $this->elementEnd('li');
  153. $this->elementEnd('ul');
  154. $this->elementStart('ul', 'form_actions');
  155. $this->elementStart('li');
  156. // TRANS: Button on avatar upload page to upload an avatar.
  157. $this->submit('upload', _m('BUTTON','Upload'));
  158. $this->elementEnd('li');
  159. $this->elementEnd('ul');
  160. }
  161. Event::handle('EndAvatarFormData', array($this));
  162. $this->elementEnd('fieldset');
  163. $this->elementEnd('form');
  164. }
  165. function showCropForm()
  166. {
  167. $user = common_current_user();
  168. $profile = $user->getProfile();
  169. if (!$profile) {
  170. common_log_db_error($user, 'SELECT', __FILE__);
  171. // TRANS: Error message displayed when referring to a user without a profile.
  172. $this->serverError(_('User has no profile.'));
  173. }
  174. $this->elementStart('form', array('method' => 'post',
  175. 'id' => 'form_settings_avatar',
  176. 'class' => 'form_settings',
  177. 'action' =>
  178. common_local_url('avatarsettings')));
  179. $this->elementStart('fieldset');
  180. // TRANS: Avatar upload page crop form legend.
  181. $this->element('legend', null, _('Avatar settings'));
  182. $this->hidden('token', common_session_token());
  183. $this->elementStart('ul', 'form_data');
  184. $this->elementStart('li',
  185. array('id' => 'avatar_original',
  186. 'class' => 'avatar_view'));
  187. // TRANS: Header on avatar upload crop form for thumbnail of originally uploaded avatar (h2).
  188. $this->element('h2', null, _('Original'));
  189. $this->elementStart('div', array('id'=>'avatar_original_view'));
  190. $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
  191. 'width' => $this->filedata['width'],
  192. 'height' => $this->filedata['height'],
  193. 'alt' => $user->nickname));
  194. $this->elementEnd('div');
  195. $this->elementEnd('li');
  196. $this->elementStart('li',
  197. array('id' => 'avatar_preview',
  198. 'class' => 'avatar_view'));
  199. // TRANS: Header on avatar upload crop form for thumbnail of to be used rendition of uploaded avatar (h2).
  200. $this->element('h2', null, _('Preview'));
  201. $this->elementStart('div', array('id'=>'avatar_preview_view'));
  202. $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
  203. 'width' => AVATAR_PROFILE_SIZE,
  204. 'height' => AVATAR_PROFILE_SIZE,
  205. 'alt' => $user->nickname));
  206. $this->elementEnd('div');
  207. foreach (array('avatar_crop_x', 'avatar_crop_y',
  208. 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
  209. $this->element('input', array('name' => $crop_info,
  210. 'type' => 'hidden',
  211. 'id' => $crop_info));
  212. }
  213. // TRANS: Button on avatar upload crop form to confirm a selected crop as avatar.
  214. $this->submit('crop', _m('BUTTON','Crop'));
  215. $this->elementEnd('li');
  216. $this->elementEnd('ul');
  217. $this->elementEnd('fieldset');
  218. $this->elementEnd('form');
  219. }
  220. protected function doPost()
  221. {
  222. if (Event::handle('StartAvatarSaveForm', array($this))) {
  223. if ($this->trimmed('upload')) {
  224. return $this->uploadAvatar();
  225. } else if ($this->trimmed('crop')) {
  226. return $this->cropAvatar();
  227. } else if ($this->trimmed('delete')) {
  228. return $this->deleteAvatar();
  229. } else {
  230. // TRANS: Unexpected validation error on avatar upload form.
  231. throw new ClientException(_('Unexpected form submission.'));
  232. }
  233. Event::handle('EndAvatarSaveForm', array($this));
  234. }
  235. }
  236. /**
  237. * Handle an image upload
  238. *
  239. * Does all the magic for handling an image upload, and crops the
  240. * image by default.
  241. *
  242. * @return void
  243. */
  244. function uploadAvatar()
  245. {
  246. // ImageFile throws exception if something goes wrong, which we'll
  247. // pick up and show as an error message above the form.
  248. $imagefile = ImageFile::fromUpload('avatarfile');
  249. $type = $imagefile->preferredType();
  250. $filename = Avatar::filename($this->scoped->getID(),
  251. image_type_to_extension($type),
  252. null,
  253. 'tmp'.common_timestamp());
  254. $filepath = Avatar::path($filename);
  255. $imagefile = $imagefile->copyTo($filepath);
  256. $filedata = array('filename' => $filename,
  257. 'filepath' => $filepath,
  258. 'width' => $imagefile->width,
  259. 'height' => $imagefile->height,
  260. 'type' => $type);
  261. $_SESSION['FILEDATA'] = $filedata;
  262. $this->filedata = $filedata;
  263. $this->mode = 'crop';
  264. // TRANS: Avatar upload form instruction after uploading a file.
  265. return _('Pick a square area of the image to be your avatar.');
  266. }
  267. /**
  268. * Handle the results of jcrop.
  269. *
  270. * @return void
  271. */
  272. public function cropAvatar()
  273. {
  274. $filedata = $_SESSION['FILEDATA'];
  275. if (empty($filedata)) {
  276. // TRANS: Server error displayed if an avatar upload went wrong somehow server side.
  277. throw new ServerException(_('Lost our file data.'));
  278. }
  279. $file_d = min($filedata['width'], $filedata['height']);
  280. $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
  281. $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
  282. $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$file_d;
  283. $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$file_d;
  284. $size = intval(min($dest_w, $dest_h, common_config('avatar', 'maxsize')));
  285. $box = array('width' => $size, 'height' => $size,
  286. 'x' => $dest_x, 'y' => $dest_y,
  287. 'w' => $dest_w, 'h' => $dest_h);
  288. $imagefile = new ImageFile(null, $filedata['filepath']);
  289. $filename = Avatar::filename($this->scoped->getID(), image_type_to_extension($imagefile->preferredType()),
  290. $size, common_timestamp());
  291. try {
  292. $imagefile->resizeTo(Avatar::path($filename), $box);
  293. } catch (UseFileAsThumbnailException $e) {
  294. common_debug('Using uploaded avatar directly without resizing, copying it to: '.$filename);
  295. if (!copy($filedata['filepath'], Avatar::path($filename))) {
  296. common_debug('Tried to copy image file '.$filedata['filepath'].' to destination '.Avatar::path($filename));
  297. throw new ServerException('Could not copy file to destination.');
  298. }
  299. }
  300. if ($this->scoped->setOriginal($filename)) {
  301. @unlink($filedata['filepath']);
  302. unset($_SESSION['FILEDATA']);
  303. $this->mode = 'upload';
  304. // TRANS: Success message for having updated a user avatar.
  305. return _('Avatar updated.');
  306. }
  307. // TRANS: Error displayed on the avatar upload page if the avatar could not be updated for an unknown reason.
  308. throw new ServerException(_('Failed updating avatar.'));
  309. }
  310. /**
  311. * Get rid of the current avatar.
  312. *
  313. * @return void
  314. */
  315. function deleteAvatar()
  316. {
  317. Avatar::deleteFromProfile($this->scoped);
  318. // TRANS: Success message for deleting a user avatar.
  319. return _('Avatar deleted.');
  320. }
  321. /**
  322. * Add the jCrop stylesheet
  323. *
  324. * @return void
  325. */
  326. function showStylesheets()
  327. {
  328. parent::showStylesheets();
  329. $this->cssLink('js/extlib/jquery-jcrop/css/jcrop.css','base','screen, projection, tv');
  330. }
  331. /**
  332. * Add the jCrop scripts
  333. *
  334. * @return void
  335. */
  336. function showScripts()
  337. {
  338. parent::showScripts();
  339. if ($this->mode == 'crop') {
  340. $this->script('extlib/jquery-jcrop/jcrop.js');
  341. $this->script('jcrop.go.js');
  342. }
  343. $this->autofocus('avatarfile');
  344. }
  345. }