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.

profile_photo.php 9.6KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. require_once("include/Photo.php");
  3. function profile_photo_init(App $a) {
  4. if (! local_user()) {
  5. return;
  6. }
  7. profile_load($a,$a->user['nickname']);
  8. }
  9. function profile_photo_post(App $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. // unless proven otherwise
  17. $is_default_profile = 1;
  18. if($_REQUEST['profile']) {
  19. $r = q("select id, `is-default` from profile where id = %d and uid = %d limit 1",
  20. intval($_REQUEST['profile']),
  21. intval(local_user())
  22. );
  23. if (dbm::is_result($r) && (! intval($r[0]['is-default'])))
  24. $is_default_profile = 0;
  25. }
  26. // phase 2 - we have finished cropping
  27. if($a->argc != 2) {
  28. notice( t('Image uploaded but image cropping failed.') . EOL );
  29. return;
  30. }
  31. $image_id = $a->argv[1];
  32. if(substr($image_id,-2,1) == '-') {
  33. $scale = substr($image_id,-1,1);
  34. $image_id = substr($image_id,0,-2);
  35. }
  36. $srcX = $_POST['xstart'];
  37. $srcY = $_POST['ystart'];
  38. $srcW = $_POST['xfinal'] - $srcX;
  39. $srcH = $_POST['yfinal'] - $srcY;
  40. $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1",
  41. dbesc($image_id),
  42. dbesc(local_user()),
  43. intval($scale));
  44. if (dbm::is_result($r)) {
  45. $base_image = $r[0];
  46. $im = new Photo($base_image['data'], $base_image['type']);
  47. if($im->is_valid()) {
  48. $im->cropImage(175,$srcX,$srcY,$srcW,$srcH);
  49. $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 4, $is_default_profile);
  50. if ($r === false) {
  51. notice ( sprintf(t('Image size reduction [%s] failed.'),"175") . EOL );
  52. }
  53. $im->scaleImage(80);
  54. $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 5, $is_default_profile);
  55. if ($r === false) {
  56. notice( sprintf(t('Image size reduction [%s] failed.'),"80") . EOL );
  57. }
  58. $im->scaleImage(48);
  59. $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 6, $is_default_profile);
  60. if ($r === false) {
  61. notice( sprintf(t('Image size reduction [%s] failed.'),"48") . EOL );
  62. }
  63. // If setting for the default profile, unset the profile photo flag from any other photos I own
  64. if($is_default_profile) {
  65. $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
  66. dbesc($base_image['resource-id']),
  67. intval(local_user())
  68. );
  69. $r = q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s' WHERE `self` AND `uid` = %d",
  70. dbesc(App::get_baseurl() . '/photo/' . $base_image['resource-id'] . '-4.' . $im->getExt()),
  71. dbesc(App::get_baseurl() . '/photo/' . $base_image['resource-id'] . '-5.' . $im->getExt()),
  72. dbesc(App::get_baseurl() . '/photo/' . $base_image['resource-id'] . '-6.' . $im->getExt()),
  73. intval(local_user())
  74. );
  75. } else {
  76. $r = q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d",
  77. dbesc(App::get_baseurl() . '/photo/' . $base_image['resource-id'] . '-4.' . $im->getExt()),
  78. dbesc(App::get_baseurl() . '/photo/' . $base_image['resource-id'] . '-5.' . $im->getExt()),
  79. intval($_REQUEST['profile']),
  80. intval(local_user())
  81. );
  82. }
  83. // we'll set the updated profile-photo timestamp even if it isn't the default profile,
  84. // so that browsers will do a cache update unconditionally
  85. $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d",
  86. dbesc(datetime_convert()),
  87. intval(local_user())
  88. );
  89. info( t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
  90. // Update global directory in background
  91. $url = App::get_baseurl() . '/profile/' . $a->user['nickname'];
  92. if ($url && strlen(get_config('system','directory'))) {
  93. proc_run(PRIORITY_LOW, "include/directory.php", $url);
  94. }
  95. require_once('include/profile_update.php');
  96. profile_change();
  97. } else {
  98. notice( t('Unable to process image') . EOL);
  99. }
  100. }
  101. goaway(App::get_baseurl() . '/profiles');
  102. return; // NOTREACHED
  103. }
  104. $src = $_FILES['userfile']['tmp_name'];
  105. $filename = basename($_FILES['userfile']['name']);
  106. $filesize = intval($_FILES['userfile']['size']);
  107. $filetype = $_FILES['userfile']['type'];
  108. if ($filetype == "") {
  109. $filetype = guess_image_type($filename);
  110. }
  111. $maximagesize = get_config('system','maximagesize');
  112. if (($maximagesize) && ($filesize > $maximagesize)) {
  113. notice( sprintf(t('Image exceeds size limit of %s'), formatBytes($maximagesize)) . EOL);
  114. @unlink($src);
  115. return;
  116. }
  117. $imagedata = @file_get_contents($src);
  118. $ph = new Photo($imagedata, $filetype);
  119. if (! $ph->is_valid()) {
  120. notice( t('Unable to process image.') . EOL );
  121. @unlink($src);
  122. return;
  123. }
  124. $ph->orient($src);
  125. @unlink($src);
  126. return profile_photo_crop_ui_head($a, $ph);
  127. }
  128. if(! function_exists('profile_photo_content')) {
  129. function profile_photo_content(App $a) {
  130. if (! local_user()) {
  131. notice( t('Permission denied.') . EOL );
  132. return;
  133. }
  134. $newuser = false;
  135. if($a->argc == 2 && $a->argv[1] === 'new')
  136. $newuser = true;
  137. if( $a->argv[1]=='use'){
  138. if ($a->argc<3){
  139. notice( t('Permission denied.') . EOL );
  140. return;
  141. };
  142. // check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
  143. $resource_id = $a->argv[2];
  144. //die(":".local_user());
  145. $r=q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC",
  146. intval(local_user()),
  147. dbesc($resource_id)
  148. );
  149. if (!dbm::is_result($r)){
  150. notice( t('Permission denied.') . EOL );
  151. return;
  152. }
  153. $havescale = false;
  154. foreach ($r as $rr) {
  155. if($rr['scale'] == 5)
  156. $havescale = true;
  157. }
  158. // set an already uloaded photo as profile photo
  159. // if photo is in 'Profile Photos', change it in db
  160. if (($r[0]['album']== t('Profile Photos')) && ($havescale)){
  161. $r=q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d",
  162. intval(local_user()));
  163. $r=q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'",
  164. intval(local_user()),
  165. dbesc($resource_id)
  166. );
  167. $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d",
  168. dbesc(datetime_convert()),
  169. intval(local_user())
  170. );
  171. // Update global directory in background
  172. $url = $_SESSION['my_url'];
  173. if ($url && strlen(get_config('system','directory'))) {
  174. proc_run(PRIORITY_LOW, "include/directory.php", $url);
  175. }
  176. goaway(App::get_baseurl() . '/profiles');
  177. return; // NOTREACHED
  178. }
  179. $ph = new Photo($r[0]['data'], $r[0]['type']);
  180. profile_photo_crop_ui_head($a, $ph);
  181. // go ahead as we have jus uploaded a new photo to crop
  182. }
  183. $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
  184. intval(local_user())
  185. );
  186. if(! x($a->config,'imagecrop')) {
  187. $tpl = get_markup_template('profile_photo.tpl');
  188. $o .= replace_macros($tpl,array(
  189. '$user' => $a->user['nickname'],
  190. '$lbl_upfile' => t('Upload File:'),
  191. '$lbl_profiles' => t('Select a profile:'),
  192. '$title' => t('Upload Profile Photo'),
  193. '$submit' => t('Upload'),
  194. '$profiles' => $profiles,
  195. '$form_security_token' => get_form_security_token("profile_photo"),
  196. '$select' => sprintf('%s %s', t('or'), ($newuser) ? '<a href="' . App::get_baseurl() . '">' . t('skip this step') . '</a>' : '<a href="'. App::get_baseurl() . '/photos/' . $a->user['nickname'] . '">' . t('select a photo from your photo albums') . '</a>')
  197. ));
  198. return $o;
  199. }
  200. else {
  201. $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.'.$a->config['imagecrop_ext'];
  202. $resolution = $a->config['imagecrop_resolution'];
  203. $tpl = get_markup_template("cropbody.tpl");
  204. $o .= replace_macros($tpl,array(
  205. '$filename' => $filename,
  206. '$profile' => intval($_REQUEST['profile']),
  207. '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
  208. '$image_url' => App::get_baseurl() . '/photo/' . $filename,
  209. '$title' => t('Crop Image'),
  210. '$desc' => t('Please adjust the image cropping for optimum viewing.'),
  211. '$form_security_token' => get_form_security_token("profile_photo"),
  212. '$done' => t('Done Editing')
  213. ));
  214. return $o;
  215. }
  216. return; // NOTREACHED
  217. }}
  218. if(! function_exists('profile_photo_crop_ui_head')) {
  219. function profile_photo_crop_ui_head(App $a, $ph) {
  220. $max_length = get_config('system','max_image_length');
  221. if (! $max_length) {
  222. $max_length = MAX_IMAGE_LENGTH;
  223. }
  224. if ($max_length > 0) {
  225. $ph->scaleImage($max_length);
  226. }
  227. $width = $ph->getWidth();
  228. $height = $ph->getHeight();
  229. if ($width < 175 || $height < 175) {
  230. $ph->scaleImageUp(200);
  231. $width = $ph->getWidth();
  232. $height = $ph->getHeight();
  233. }
  234. $hash = photo_new_resource();
  235. $smallest = 0;
  236. $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 0 );
  237. if ($r) {
  238. info( t('Image uploaded successfully.') . EOL );
  239. } else {
  240. notice( t('Image upload failed.') . EOL );
  241. }
  242. if ($width > 640 || $height > 640) {
  243. $ph->scaleImage(640);
  244. $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 1 );
  245. if ($r === false) {
  246. notice( sprintf(t('Image size reduction [%s] failed.'),"640") . EOL );
  247. } else {
  248. $smallest = 1;
  249. }
  250. }
  251. $a->config['imagecrop'] = $hash;
  252. $a->config['imagecrop_resolution'] = $smallest;
  253. $a->config['imagecrop_ext'] = $ph->getExt();
  254. $a->page['htmlhead'] .= replace_macros(get_markup_template("crophead.tpl"), array());
  255. $a->page['end'] .= replace_macros(get_markup_template("cropend.tpl"), array());
  256. return;
  257. }}