grouplogo.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. $this->elementEnd('li');
  188. }
  189. $this->elementStart('li', array ('id' => 'settings_attach'));
  190. $this->element('input', array('name' => 'MAX_FILE_SIZE',
  191. 'type' => 'hidden',
  192. 'id' => 'MAX_FILE_SIZE',
  193. 'value' => ImageFile::maxFileSizeInt()));
  194. $this->element('input', array('name' => 'avatarfile',
  195. 'type' => 'file',
  196. 'id' => 'avatarfile'));
  197. $this->elementEnd('li');
  198. $this->elementEnd('ul');
  199. $this->elementStart('ul', 'form_actions');
  200. $this->elementStart('li');
  201. // TRANS: Submit button for uploading a group logo.
  202. $this->submit('upload', _('Upload'));
  203. $this->elementEnd('li');
  204. $this->elementEnd('ul');
  205. $this->elementEnd('fieldset');
  206. $this->elementEnd('form');
  207. }
  208. function showCropForm()
  209. {
  210. $this->elementStart('form', array('method' => 'post',
  211. 'id' => 'form_settings_avatar',
  212. 'class' => 'form_settings',
  213. 'action' =>
  214. common_local_url('grouplogo',
  215. array('nickname' => $this->group->nickname))));
  216. $this->elementStart('fieldset');
  217. // TRANS: Legend for group logo settings fieldset.
  218. $this->element('legend', null, _('Avatar settings'));
  219. $this->hidden('token', common_session_token());
  220. $this->elementStart('ul', 'form_data');
  221. $this->elementStart('li',
  222. array('id' => 'avatar_original',
  223. 'class' => 'avatar_view'));
  224. // TRANS: Header for originally uploaded file before a crop on the group logo page.
  225. $this->element('h2', null, _('Original'));
  226. $this->elementStart('div', array('id'=>'avatar_original_view'));
  227. $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
  228. 'width' => $this->filedata['width'],
  229. 'height' => $this->filedata['height'],
  230. 'alt' => $this->group->nickname));
  231. $this->elementEnd('div');
  232. $this->elementEnd('li');
  233. $this->elementStart('li',
  234. array('id' => 'avatar_preview',
  235. 'class' => 'avatar_view'));
  236. // TRANS: Header for the cropped group logo on the group logo page.
  237. $this->element('h2', null, _('Preview'));
  238. $this->elementStart('div', array('id'=>'avatar_preview_view'));
  239. $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
  240. 'width' => AVATAR_PROFILE_SIZE,
  241. 'height' => AVATAR_PROFILE_SIZE,
  242. 'alt' => $this->group->nickname));
  243. $this->elementEnd('div');
  244. foreach (array('avatar_crop_x', 'avatar_crop_y',
  245. 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
  246. $this->element('input', array('name' => $crop_info,
  247. 'type' => 'hidden',
  248. 'id' => $crop_info));
  249. }
  250. // TRANS: Button text for cropping an uploaded group logo.
  251. $this->submit('crop', _('Crop'));
  252. $this->elementEnd('li');
  253. $this->elementEnd('ul');
  254. $this->elementEnd('fieldset');
  255. $this->elementEnd('form');
  256. }
  257. /**
  258. * Handle a post
  259. *
  260. * We mux on the button name to figure out what the user actually wanted.
  261. *
  262. * @return void
  263. */
  264. function handlePost()
  265. {
  266. // CSRF protection
  267. $token = $this->trimmed('token');
  268. if (!$token || $token != common_session_token()) {
  269. // TRANS: Form validation error message.
  270. $this->show_form(_('There was a problem with your session token. '.
  271. 'Try again, please.'));
  272. return;
  273. }
  274. if ($this->arg('upload')) {
  275. $this->uploadLogo();
  276. } else if ($this->arg('crop')) {
  277. $this->cropLogo();
  278. } else {
  279. // TRANS: Form validation error message when an unsupported argument is used.
  280. $this->showForm(_('Unexpected form submission.'));
  281. }
  282. }
  283. /**
  284. * Handle an image upload
  285. *
  286. * Does all the magic for handling an image upload, and crops the
  287. * image by default.
  288. *
  289. * @return void
  290. */
  291. function uploadLogo()
  292. {
  293. try {
  294. $imagefile = ImageFile::fromUpload('avatarfile');
  295. } catch (Exception $e) {
  296. $this->showForm($e->getMessage());
  297. return;
  298. }
  299. $type = $imagefile->preferredType();
  300. $filename = Avatar::filename($this->group->id,
  301. image_type_to_extension($type),
  302. null,
  303. 'group-temp-'.common_timestamp());
  304. $filepath = Avatar::path($filename);
  305. $imagefile->copyTo($filepath);
  306. $filedata = array('filename' => $filename,
  307. 'filepath' => $filepath,
  308. 'width' => $imagefile->width,
  309. 'height' => $imagefile->height,
  310. 'type' => $type);
  311. $_SESSION['FILEDATA'] = $filedata;
  312. $this->filedata = $filedata;
  313. $this->mode = 'crop';
  314. // TRANS: Form instructions on the group logo page.
  315. $this->showForm(_('Pick a square area of the image to be the logo.'),
  316. true);
  317. }
  318. /**
  319. * Handle the results of jcrop.
  320. *
  321. * @return void
  322. */
  323. function cropLogo()
  324. {
  325. $filedata = $_SESSION['FILEDATA'];
  326. if (!$filedata) {
  327. // TRANS: Server error displayed trying to crop an uploaded group logo that is no longer present.
  328. $this->serverError(_('Lost our file data.'));
  329. }
  330. // If image is not being cropped assume pos & dimentions of original
  331. $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
  332. $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
  333. $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$filedata['width'];
  334. $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$filedata['height'];
  335. $size = min($dest_w, $dest_h, common_config('avatar', 'maxsize'));
  336. $box = array('width' => $size, 'height' => $size,
  337. 'x' => $dest_x, 'y' => $dest_y,
  338. 'w' => $dest_w, 'h' => $dest_h);
  339. $profile = $this->group->getProfile();
  340. $imagefile = new ImageFile(null, $filedata['filepath']);
  341. $filename = Avatar::filename($profile->getID(), image_type_to_extension($imagefile->preferredType()),
  342. $size, common_timestamp());
  343. $imagefile->resizeTo(Avatar::path($filename), $box);
  344. if ($profile->setOriginal($filename)) {
  345. @unlink($filedata['filepath']);
  346. unset($_SESSION['FILEDATA']);
  347. $this->mode = 'upload';
  348. // TRANS: Form success message after updating a group logo.
  349. $this->showForm(_('Logo updated.'), true);
  350. } else {
  351. // TRANS: Form failure message after failing to update a group logo.
  352. $this->showForm(_('Failed updating logo.'));
  353. }
  354. }
  355. function showPageNotice()
  356. {
  357. if ($this->msg) {
  358. $this->element('div', ($this->success) ? 'success' : 'error',
  359. $this->msg);
  360. } else {
  361. $inst = $this->getInstructions();
  362. $output = common_markup_to_html($inst);
  363. $this->elementStart('div', 'instructions');
  364. $this->raw($output);
  365. $this->elementEnd('div');
  366. }
  367. }
  368. /**
  369. * Add the jCrop stylesheet
  370. *
  371. * @return void
  372. */
  373. function showStylesheets()
  374. {
  375. parent::showStylesheets();
  376. $this->cssLink('js/extlib/jquery-jcrop/css/jcrop.css','base','screen, projection, tv');
  377. }
  378. /**
  379. * Add the jCrop scripts
  380. *
  381. * @return void
  382. */
  383. function showScripts()
  384. {
  385. parent::showScripts();
  386. if ($this->mode == 'crop') {
  387. $this->script('extlib/jquery-jcrop/jcrop.js');
  388. $this->script('jcrop.go.js');
  389. }
  390. $this->autofocus('avatarfile');
  391. }
  392. }