grouplogo.php 16 KB

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