Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
7.3 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. <?php
  2. require_once("Photo.php");
  3. function profile_photo_init(&$a) {
  4. if(! local_user()) {
  5. return;
  6. }
  7. profile_load($a,$a->user['nickname']);
  8. }
  9. function profile_photo_post(&$a) {
  10. if(! local_user()) {
  11. notice ( t('Permission denied.') . EOL );
  12. return;
  13. }
  14. check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
  15. if((x($_POST,'cropfinal')) && ($_POST['cropfinal'] == 1)) {
  16. // phase 2 - we have finished cropping
  17. if($a->argc != 2) {
  18. notice( t('Image uploaded but image cropping failed.') . EOL );
  19. return;
  20. }
  21. $image_id = $a->argv[1];
  22. if(substr($image_id,-2,1) == '-') {
  23. $scale = substr($image_id,-1,1);
  24. $image_id = substr($image_id,0,-2);
  25. }
  26. $srcX = $_POST['xstart'];
  27. $srcY = $_POST['ystart'];
  28. $srcW = $_POST['xfinal'] - $srcX;
  29. $srcH = $_POST['yfinal'] - $srcY;
  30. $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1",
  31. dbesc($image_id),
  32. dbesc(local_user()),
  33. intval($scale));
  34. if(count($r)) {
  35. $base_image = $r[0];
  36. $im = new Photo($base_image['data']);
  37. if($im->is_valid()) {
  38. $im->cropImage(175,$srcX,$srcY,$srcW,$srcH);
  39. $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 4, 1);
  40. if($r === false)
  41. notice ( sprintf(t('Image size reduction [%s] failed.'),"175") . EOL );
  42. $im->scaleImage(80);
  43. $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 5, 1);
  44. if($r === false)
  45. notice( sprintf(t('Image size reduction [%s] failed.'),"80") . EOL );
  46. $im->scaleImage(48);
  47. $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 6, 1);
  48. if($r === false)
  49. notice( sprintf(t('Image size reduction [%s] failed.'),"48") . EOL );
  50. // Unset the profile photo flag from any other photos I own
  51. $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
  52. dbesc($base_image['resource-id']),
  53. intval(local_user())
  54. );
  55. $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d LIMIT 1",
  56. dbesc(datetime_convert()),
  57. intval(local_user())
  58. );
  59. info( t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
  60. // Update global directory in background
  61. $url = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
  62. if($url && strlen(get_config('system','directory_submit_url')))
  63. proc_run('php',"include/directory.php","$url");
  64. require_once('include/profile_update.php');
  65. profile_change();
  66. }
  67. else
  68. notice( t('Unable to process image') . EOL);
  69. }
  70. goaway($a->get_baseurl() . '/profiles');
  71. return; // NOTREACHED
  72. }
  73. $src = $_FILES['userfile']['tmp_name'];
  74. $filename = basename($_FILES['userfile']['name']);
  75. $filesize = intval($_FILES['userfile']['size']);
  76. $maximagesize = get_config('system','maximagesize');
  77. if(($maximagesize) && ($filesize > $maximagesize)) {
  78. notice( sprintf(t('Image exceeds size limit of %d'), $maximagesize) . EOL);
  79. @unlink($src);
  80. return;
  81. }
  82. $imagedata = @file_get_contents($src);
  83. $ph = new Photo($imagedata);
  84. if(! $ph->is_valid()) {
  85. notice( t('Unable to process image.') . EOL );
  86. @unlink($src);
  87. return;
  88. }
  89. @unlink($src);
  90. return profile_photo_crop_ui_head($a, $ph);
  91. }
  92. if(! function_exists('profile_photo_content')) {
  93. function profile_photo_content(&$a) {
  94. if(! local_user()) {
  95. notice( t('Permission denied.') . EOL );
  96. return;
  97. }
  98. $newuser = false;
  99. if($a->argc == 2 && $a->argv[1] === 'new')
  100. $newuser = true;
  101. if( $a->argv[1]=='use'){
  102. if ($a->argc<3){
  103. notice( t('Permission denied.') . EOL );
  104. return;
  105. };
  106. // check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
  107. $resource_id = $a->argv[2];
  108. //die(":".local_user());
  109. $r=q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC",
  110. intval(local_user()),
  111. dbesc($resource_id)
  112. );
  113. if (!count($r)){
  114. notice( t('Permission denied.') . EOL );
  115. return;
  116. }
  117. $havescale = false;
  118. foreach($r as $rr) {
  119. if($rr['scale'] == 5)
  120. $havescale = true;
  121. }
  122. // set an already uloaded photo as profile photo
  123. // if photo is in 'Profile Photos', change it in db
  124. if (($r[0]['album']== t('Profile Photos')) && ($havescale)){
  125. $r=q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d",
  126. intval(local_user()));
  127. $r=q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'",
  128. intval(local_user()),
  129. dbesc($resource_id)
  130. );
  131. $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d LIMIT 1",
  132. dbesc(datetime_convert()),
  133. intval(local_user())
  134. );
  135. // Update global directory in background
  136. $url = $_SESSION['my_url'];
  137. if($url && strlen(get_config('system','directory_submit_url')))
  138. proc_run('php',"include/directory.php","$url");
  139. goaway($a->get_baseurl() . '/profiles');
  140. return; // NOTREACHED
  141. }
  142. $ph = new Photo($r[0]['data']);
  143. profile_photo_crop_ui_head($a, $ph);
  144. // go ahead as we have jus uploaded a new photo to crop
  145. }
  146. if(! x($a->config,'imagecrop')) {
  147. $tpl = get_markup_template('profile_photo.tpl');
  148. $o .= replace_macros($tpl,array(
  149. '$user' => $a->user['nickname'],
  150. '$lbl_upfile' => t('Upload File:'),
  151. '$title' => t('Upload Profile Photo'),
  152. '$submit' => t('Upload'),
  153. '$form_security_token' => get_form_security_token("profile_photo"),
  154. '$select' => sprintf('%s %s', t('or'), ($newuser) ? '<a href="' . $a->get_baseurl() . '">' . t('skip this step') . '</a>' : '<a href="'. $a->get_baseurl() . '/photos/' . $a->user['nickname'] . '">' . t('select a photo from your photo albums') . '</a>')
  155. ));
  156. return $o;
  157. }
  158. else {
  159. $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.jpg';
  160. $resolution = $a->config['imagecrop_resolution'];
  161. $tpl = get_markup_template("cropbody.tpl");
  162. $o .= replace_macros($tpl,array(
  163. '$filename' => $filename,
  164. '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
  165. '$image_url' => $a->get_baseurl() . '/photo/' . $filename,
  166. '$title' => t('Crop Image'),
  167. '$desc' => t('Please adjust the image cropping for optimum viewing.'),
  168. '$form_security_token' => get_form_security_token("profile_photo"),
  169. '$done' => t('Done Editing')
  170. ));
  171. return $o;
  172. }
  173. return; // NOTREACHED
  174. }}
  175. if(! function_exists('_crop_ui_head')) {
  176. function profile_photo_crop_ui_head(&$a, $ph){
  177. $width = $ph->getWidth();
  178. $height = $ph->getHeight();
  179. if($width < 175 || $height < 175) {
  180. $ph->scaleImageUp(200);
  181. $width = $ph->getWidth();
  182. $height = $ph->getHeight();
  183. }
  184. $hash = photo_new_resource();
  185. $smallest = 0;
  186. $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 0 );
  187. if($r)
  188. info( t('Image uploaded successfully.') . EOL );
  189. else
  190. notice( t('Image upload failed.') . EOL );
  191. if($width > 640 || $height > 640) {
  192. $ph->scaleImage(640);
  193. $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 1 );
  194. if($r === false)
  195. notice( sprintf(t('Image size reduction [%s] failed.'),"640") . EOL );
  196. else
  197. $smallest = 1;
  198. }
  199. $a->config['imagecrop'] = $hash;
  200. $a->config['imagecrop_resolution'] = $smallest;
  201. $a->page['htmlhead'] .= get_markup_template("crophead.tpl");
  202. return;
  203. }}