grouplogo.php 17 KB

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