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.

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