grouplogo.php 15 KB

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