avatarsettings.php 15 KB

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