grouplogo.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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-2011 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')) {
  31. exit(1);
  32. }
  33. /**
  34. * Upload an avatar
  35. *
  36. * We use jCrop plugin for jQuery to crop the image after upload.
  37. *
  38. * @category Settings
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @author Zach Copley <zach@status.net>
  42. * @author Sarven Capadisli <csarven@status.net>
  43. * @author Alexei Sorokin <sor.alexei@meowr.ru>
  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 GrouplogoAction extends GroupAction
  48. {
  49. public $filedata;
  50. public $mode = null;
  51. public $imagefile = null;
  52. public $filename = null;
  53. public $message = null;
  54. public $success = null;
  55. protected $canPost = true;
  56. /**
  57. * Title of the page
  58. *
  59. * @return string Title of the page
  60. */
  61. public function title()
  62. {
  63. // TRANS: Title for group logo settings page.
  64. return _('Group logo');
  65. }
  66. /**
  67. * Content area of the page
  68. *
  69. * Shows a form for uploading an avatar.
  70. *
  71. * @return void
  72. */
  73. public function showContent()
  74. {
  75. if ($this->mode == 'crop') {
  76. $this->showCropForm();
  77. } else {
  78. $this->showUploadForm();
  79. }
  80. }
  81. public function showCropForm()
  82. {
  83. $this->elementStart('form', array('method' => 'post',
  84. 'id' => 'form_settings_avatar',
  85. 'class' => 'form_settings',
  86. 'action' =>
  87. common_local_url(
  88. 'grouplogo',
  89. array('nickname' => $this->group->nickname)
  90. )));
  91. $this->elementStart('fieldset');
  92. // TRANS: Legend for group logo settings fieldset.
  93. $this->element('legend', null, _('Avatar settings'));
  94. $this->hidden('token', common_session_token());
  95. $this->elementStart('ul', 'form_data');
  96. $this->elementStart(
  97. 'li',
  98. array('id' => 'avatar_original',
  99. 'class' => 'avatar_view')
  100. );
  101. // TRANS: Header for originally uploaded file before a crop on the group logo page.
  102. $this->element('h2', null, _('Original'));
  103. $this->elementStart('div', array('id' => 'avatar_original_view'));
  104. $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
  105. 'width' => $this->filedata['width'],
  106. 'height' => $this->filedata['height'],
  107. 'alt' => $this->group->nickname));
  108. $this->elementEnd('div');
  109. $this->elementEnd('li');
  110. $this->elementStart(
  111. 'li',
  112. array('id' => 'avatar_preview',
  113. 'class' => 'avatar_view')
  114. );
  115. // TRANS: Header for the cropped group logo on the group logo page.
  116. $this->element('h2', null, _('Preview'));
  117. $this->elementStart('div', array('id' => 'avatar_preview_view'));
  118. $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
  119. 'width' => AVATAR_PROFILE_SIZE,
  120. 'height' => AVATAR_PROFILE_SIZE,
  121. 'alt' => $this->group->nickname));
  122. $this->elementEnd('div');
  123. foreach (array('avatar_crop_x', 'avatar_crop_y',
  124. 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
  125. $this->element('input', array('name' => $crop_info,
  126. 'type' => 'hidden',
  127. 'id' => $crop_info));
  128. }
  129. // TRANS: Button text for cropping an uploaded group logo.
  130. $this->submit('crop', _('Crop'));
  131. $this->elementEnd('li');
  132. $this->elementEnd('ul');
  133. $this->elementEnd('fieldset');
  134. $this->elementEnd('form');
  135. }
  136. public function showUploadForm()
  137. {
  138. $user = common_current_user();
  139. $profile = $user->getProfile();
  140. if (!$profile) {
  141. common_log_db_error($user, 'SELECT', __FILE__);
  142. // TRANS: Error message displayed when referring to a user without a profile.
  143. $this->serverError(_('User has no profile.'));
  144. }
  145. $original = $this->group->original_logo;
  146. $this->elementStart('form', array('enctype' => 'multipart/form-data',
  147. 'method' => 'post',
  148. 'id' => 'form_settings_avatar',
  149. 'class' => 'form_settings',
  150. 'action' =>
  151. common_local_url(
  152. 'grouplogo',
  153. array('nickname' => $this->group->nickname)
  154. )));
  155. $this->elementStart('fieldset');
  156. // TRANS: Group logo form legend.
  157. $this->element('legend', null, _('Group logo'));
  158. $this->hidden('token', common_session_token());
  159. $this->elementStart('ul', 'form_data');
  160. if ($original) {
  161. $this->elementStart('li', array('id' => 'avatar_original',
  162. 'class' => 'avatar_view'));
  163. // TRANS: Uploaded original file in group logo form.
  164. $this->element('h2', null, _('Original'));
  165. $this->elementStart('div', array('id' => 'avatar_original_view'));
  166. $this->element('img', array('src' => $this->group->original_logo,
  167. 'alt' => $this->group->nickname));
  168. $this->elementEnd('div');
  169. $this->elementEnd('li');
  170. }
  171. if ($this->group->homepage_logo) {
  172. $this->elementStart('li', array('id' => 'avatar_preview',
  173. 'class' => 'avatar_view'));
  174. // TRANS: Header for preview of to be displayed group logo.
  175. $this->element('h2', null, _('Preview'));
  176. $this->elementStart('div', array('id' => 'avatar_preview_view'));
  177. $this->element('img', array('src' => $this->group->homepage_logo,
  178. 'width' => AVATAR_PROFILE_SIZE,
  179. 'height' => AVATAR_PROFILE_SIZE,
  180. 'alt' => $this->group->nickname));
  181. $this->elementEnd('div');
  182. if (!empty($this->group->homepage_logo)) {
  183. // TRANS: Button on group logo upload page to delete current group logo.
  184. $this->submit('delete', _('Delete'));
  185. }
  186. $this->elementEnd('li');
  187. }
  188. $this->elementStart('li', array('id' => 'settings_attach'));
  189. $this->element('input', array('name' => 'MAX_FILE_SIZE',
  190. 'type' => 'hidden',
  191. 'id' => 'MAX_FILE_SIZE',
  192. 'value' => ImageFile::maxFileSizeInt()));
  193. $this->element('input', array('name' => 'avatarfile',
  194. 'type' => 'file',
  195. 'id' => 'avatarfile'));
  196. $this->elementEnd('li');
  197. $this->elementEnd('ul');
  198. $this->elementStart('ul', 'form_actions');
  199. $this->elementStart('li');
  200. // TRANS: Submit button for uploading a group logo.
  201. $this->submit('upload', _('Upload'));
  202. $this->elementEnd('li');
  203. $this->elementEnd('ul');
  204. $this->elementEnd('fieldset');
  205. $this->elementEnd('form');
  206. }
  207. public function showPageNoticeBlock()
  208. {
  209. parent::showPageNoticeBlock();
  210. if ($this->message) {
  211. $this->element(
  212. 'div',
  213. ($this->success) ? 'success' : 'error',
  214. $this->message
  215. );
  216. } else {
  217. $inst = $this->getInstructions();
  218. $output = common_markup_to_html($inst);
  219. $this->elementStart('div', 'instructions');
  220. $this->raw($output);
  221. $this->elementEnd('div');
  222. }
  223. }
  224. /**
  225. * Instructions for use
  226. *
  227. * @return string instructions for use
  228. */
  229. public function getInstructions()
  230. {
  231. // TRANS: Instructions for group logo page.
  232. // TRANS: %s is the maximum file size for that site.
  233. return sprintf(_('You can upload a logo image for your group. The maximum file size is %s.'), ImageFile::maxFileSize());
  234. }
  235. /**
  236. * Add the jCrop stylesheet
  237. *
  238. * @return void
  239. */
  240. public function showStylesheets()
  241. {
  242. parent::showStylesheets();
  243. $this->cssLink('js/extlib/jquery-jcrop/css/jcrop.css', 'base', 'screen, projection, tv');
  244. }
  245. /**
  246. * Add the jCrop scripts
  247. *
  248. * @return void
  249. */
  250. public function showScripts()
  251. {
  252. parent::showScripts();
  253. if ($this->mode == 'crop') {
  254. $this->script('extlib/jquery-jcrop/jcrop.js');
  255. $this->script('jcrop.go.js');
  256. }
  257. $this->autofocus('avatarfile');
  258. }
  259. /**
  260. * Prepare to run
  261. * @param array $args
  262. * @return bool
  263. * @throws ClientException
  264. * @throws NicknameException
  265. */
  266. protected function prepare(array $args = [])
  267. {
  268. parent::prepare($args);
  269. if (!common_logged_in()) {
  270. // TRANS: Client error displayed when trying to create a group while not logged in.
  271. $this->clientError(_('You must be logged in to create a group.'));
  272. }
  273. $nickname_arg = $this->trimmed('nickname');
  274. $nickname = common_canonical_nickname($nickname_arg);
  275. // Permanent redirect on non-canonical nickname
  276. if ($nickname_arg != $nickname) {
  277. $args = array('nickname' => $nickname);
  278. common_redirect(common_local_url('grouplogo', $args), 301);
  279. }
  280. if (!$nickname) {
  281. // TRANS: Client error displayed when trying to change group logo settings without providing a nickname.
  282. $this->clientError(_('No nickname.'), 404);
  283. }
  284. $groupid = $this->trimmed('groupid');
  285. if ($groupid) {
  286. $this->group = User_group::getKV('id', $groupid);
  287. } else {
  288. $local = Local_group::getKV('nickname', $nickname);
  289. if ($local) {
  290. $this->group = User_group::getKV('id', $local->group_id);
  291. }
  292. }
  293. if (!$this->group) {
  294. // TRANS: Client error displayed when trying to update logo settings for a non-existing group.
  295. $this->clientError(_('No such group.'), 404);
  296. }
  297. $cur = common_current_user();
  298. if (!$cur->isAdmin($this->group)) {
  299. // TRANS: Client error displayed when trying to change group logo settings while not being a group admin.
  300. $this->clientError(_('You must be an admin to edit the group.'), 403);
  301. }
  302. return true;
  303. }
  304. /**
  305. * Handle a post
  306. *
  307. * We mux on the button name to figure out what the user actually wanted.
  308. *
  309. * @return void
  310. * @throws ClientException
  311. * @throws NoResultException
  312. * @throws UnsupportedMediaException
  313. * @throws UseFileAsThumbnailException
  314. */
  315. protected function handlePost()
  316. {
  317. parent::handlePost();
  318. if ($this->arg('upload')) {
  319. $this->uploadLogo();
  320. } elseif ($this->arg('crop')) {
  321. $this->cropLogo();
  322. } elseif ($this->arg('delete')) {
  323. $this->deleteLogo();
  324. } else {
  325. // TRANS: Form validation error message when an unsupported argument is used.
  326. $this->setMessage(_('Unexpected form submission.'), true);
  327. }
  328. }
  329. /**
  330. * Handle an image upload
  331. *
  332. * Does all the magic for handling an image upload, and crops the
  333. * image by default.
  334. *
  335. * @return void
  336. */
  337. public function uploadLogo()
  338. {
  339. try {
  340. $imagefile = ImageFile::fromUpload('avatarfile');
  341. } catch (Exception $e) {
  342. $this->setMessage($e->getMessage(), true);
  343. return;
  344. }
  345. $type = $imagefile->preferredType();
  346. $filename = Avatar::filename(
  347. $this->group->id,
  348. image_type_to_extension($type),
  349. null,
  350. 'group-temp-' . common_timestamp()
  351. );
  352. $filepath = Avatar::path($filename);
  353. $imagefile->copyTo($filepath);
  354. $filedata = array('filename' => $filename,
  355. 'filepath' => $filepath,
  356. 'width' => $imagefile->width,
  357. 'height' => $imagefile->height,
  358. 'type' => $type);
  359. $_SESSION['FILEDATA'] = $filedata;
  360. $this->filedata = $filedata;
  361. $this->mode = 'crop';
  362. // TRANS: Form instructions on the group logo page.
  363. $this->setMessage(_('Pick a square area of the image to be the logo.'));
  364. }
  365. public function setMessage($msg, $error = false)
  366. {
  367. $this->message = $msg;
  368. $this->success = !$error;
  369. }
  370. /**
  371. * Handle the results of jcrop.
  372. *
  373. * @return void
  374. * @throws NoResultException
  375. * @throws UnsupportedMediaException
  376. * @throws UseFileAsThumbnailException
  377. */
  378. public function cropLogo()
  379. {
  380. $filedata = $_SESSION['FILEDATA'];
  381. if (!$filedata) {
  382. // TRANS: Server error displayed trying to crop an uploaded group logo that is no longer present.
  383. $this->serverError(_('Lost our file data.'));
  384. }
  385. // If image is not being cropped assume pos & dimentions of original
  386. $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x') : 0;
  387. $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y') : 0;
  388. $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w') : $filedata['width'];
  389. $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h') : $filedata['height'];
  390. $size = min($dest_w, $dest_h, common_config('avatar', 'maxsize'));
  391. $box = array('width' => $size, 'height' => $size,
  392. 'x' => $dest_x, 'y' => $dest_y,
  393. 'w' => $dest_w, 'h' => $dest_h);
  394. $profile = $this->group->getProfile();
  395. $imagefile = new ImageFile($filedata['filepath']);
  396. $filename = Avatar::filename(
  397. $profile->getID(),
  398. image_type_to_extension($imagefile->preferredType()),
  399. $size,
  400. common_timestamp()
  401. );
  402. $imagefile->resizeTo(Avatar::path($filename), $box);
  403. if ($profile->setOriginal($filename)) {
  404. @unlink($filedata['filepath']);
  405. unset($_SESSION['FILEDATA']);
  406. $this->mode = 'upload';
  407. // TRANS: Form success message after updating a group logo.
  408. $this->setMessage(_('Logo updated.'));
  409. } else {
  410. // TRANS: Form failure message after failing to update a group logo.
  411. $this->setMessage(_('Failed updating logo.'), true);
  412. }
  413. }
  414. /**
  415. * Get rid of the current group logo.
  416. *
  417. * @return void
  418. */
  419. public function deleteLogo()
  420. {
  421. $orig = clone($this->group);
  422. Avatar::deleteFromProfile($this->group->getProfile());
  423. @unlink(Avatar::path(basename($this->group->original_logo)));
  424. @unlink(Avatar::path(basename($this->group->homepage_logo)));
  425. @unlink(Avatar::path(basename($this->group->stream_logo)));
  426. @unlink(Avatar::path(basename($this->group->mini_logo)));
  427. $this->group->original_logo = User_group::defaultLogo(AVATAR_PROFILE_SIZE);
  428. $this->group->homepage_logo = User_group::defaultLogo(AVATAR_PROFILE_SIZE);
  429. $this->group->stream_logo = User_group::defaultLogo(AVATAR_STREAM_SIZE);
  430. $this->group->mini_logo = User_group::defaultLogo(AVATAR_MINI_SIZE);
  431. $this->group->update($orig);
  432. // TRANS: Success message for deleting the group logo.
  433. $this->setMessage(_('Logo deleted.'));
  434. }
  435. }