]> git.mxchange.org Git - friendica.git/blob - mod/profiles.php
Merge remote branch 'mike/master'
[friendica.git] / mod / profiles.php
1 <?php
2
3
4 function profiles_post(&$a) {
5
6         if(! local_user()) {
7                 notice( t('Permission denied.') . EOL);
8                 return;
9         }
10
11         $namechanged = false;
12
13         call_hooks('profile_post', $_POST);
14
15         if(($a->argc > 1) && ($a->argv[1] !== "new") && intval($a->argv[1])) {
16                 $orig = q("SELECT * FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
17                         intval($a->argv[1]),
18                         intval(local_user())
19                 );
20                 if(! count($orig)) {
21                         notice( t('Profile not found.') . EOL);
22                         return;
23                 }
24                 $is_default = (($orig[0]['is-default']) ? 1 : 0);
25
26                 $profile_name = notags(trim($_POST['profile_name']));
27                 if(! strlen($profile_name)) {
28                         notify( t('Profile Name is required.') . EOL);
29                         return;
30                 }
31         
32                 $year = intval($_POST['year']);
33                 if($year < 1900 || $year > 2100 || $year < 0)
34                         $year = 0;
35                 $month = intval($_POST['month']);
36                         if(($month > 12) || ($month < 0))
37                                 $month = 0;
38                 $mtab = array(0,31,29,31,30,31,30,31,31,30,31,30,31);
39                 $day = intval($_POST['day']);
40                         if(($day > $mtab[$month]) || ($day < 0))
41                                 $day = 0;
42                 $dob = '0000-00-00';
43                 $dob = sprintf('%04d-%02d-%02d',$year,$month,$day);
44
45                         
46                 $name = notags(trim($_POST['name']));
47
48                 if($orig[0]['name'] != $name)
49                         $namechanged = true;
50
51                 $pdesc = notags(trim($_POST['pdesc']));
52                 $gender = notags(trim($_POST['gender']));
53                 $address = notags(trim($_POST['address']));
54                 $locality = notags(trim($_POST['locality']));
55                 $region = notags(trim($_POST['region']));
56                 $postal_code = notags(trim($_POST['postal_code']));
57                 $country_name = notags(trim($_POST['country_name']));
58                 $keywords = notags(trim($_POST['keywords']));
59                 $marital = notags(trim($_POST['marital']));
60                 if($marital != $orig[0]['marital'])
61                         $maritalchanged = true;
62
63                 $with = ((x($_POST,'with')) ? notags(trim($_POST['with'])) : '');
64
65                 // linkify the relationship target if applicable
66
67                 if(strlen($with)) {
68                         if($with != strip_tags($orig[0]['with'])) {
69                                 $prf = '';
70                                 $lookup = $with;
71                                 if(strpos($lookup,'@') === 0)
72                                         $lookup = substr($lookup,1);
73                                 $lookup = str_replace('_',' ', $lookup);
74                                 if(strpos($lookup,'@') || (strpos($lookup,'http://'))) {
75                                         $newname = $lookup;
76                                         $links = @lrdd($lookup);
77                                         if(count($links)) {
78                                                 foreach($links as $link) {
79                                                         if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page') {
80                                         $prf = $link['@attributes']['href'];
81                                                         }
82                                                 }
83                                         }
84                                 }
85                                 else {
86                                         $newname = $lookup;
87                                         if(strstr($lookup,' ')) {
88                                                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
89                                                         dbesc($newname),
90                                                         intval(local_user())
91                                                 );
92                                         }
93                                         else {
94                                                 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
95                                                         dbesc($lookup),
96                                                         intval(local_user())
97                                                 );
98                                         }
99                                         if(count($r)) {
100                                                 $prf = $r[0]['url'];
101                                                 $newname = $r[0]['name'];
102                                         }
103                                 }
104         
105                                 if($prf) {
106                                         $with = str_replace($lookup,'<a href="' . $prf . '">' . $newname        . '</a>', $with);
107                                         if(strpos($with,'@') === 0)
108                                                 $with = substr($with,1);
109                                 }
110                         }
111                         else
112                                 $with = $orig[0]['with'];
113                 }
114
115                 $sexual = notags(trim($_POST['sexual']));
116                 $homepage = notags(trim($_POST['homepage']));
117                 $politic = notags(trim($_POST['politic']));
118                 $religion = notags(trim($_POST['religion']));
119
120                 $about = escape_tags(trim($_POST['about']));
121                 $interest = escape_tags(trim($_POST['interest']));
122                 $contact = escape_tags(trim($_POST['contact']));
123                 $music = escape_tags(trim($_POST['music']));
124                 $book = escape_tags(trim($_POST['book']));
125                 $tv = escape_tags(trim($_POST['tv']));
126                 $film = escape_tags(trim($_POST['film']));
127                 $romance = escape_tags(trim($_POST['romance']));
128                 $work = escape_tags(trim($_POST['work']));
129                 $education = escape_tags(trim($_POST['education']));
130                 $hide_friends = (($_POST['hide-friends'] == 1) ? 1: 0);
131
132
133                 $r = q("UPDATE `profile` 
134                         SET `profile-name` = '%s',
135                         `name` = '%s',
136                         `pdesc` = '%s',
137                         `gender` = '%s',
138                         `dob` = '%s',
139                         `address` = '%s',
140                         `locality` = '%s',
141                         `region` = '%s',
142                         `postal-code` = '%s',
143                         `country-name` = '%s',
144                         `marital` = '%s',
145                         `with` = '%s',
146                         `sexual` = '%s',
147                         `homepage` = '%s',
148                         `politic` = '%s',
149                         `religion` = '%s',
150                         `keywords` = '%s',
151                         `about` = '%s',
152                         `interest` = '%s',
153                         `contact` = '%s',
154                         `music` = '%s',
155                         `book` = '%s',
156                         `tv` = '%s',
157                         `film` = '%s',
158                         `romance` = '%s',
159                         `work` = '%s',
160                         `education` = '%s',
161                         `hide-friends` = %d
162                         WHERE `id` = %d AND `uid` = %d LIMIT 1",
163                         dbesc($profile_name),
164                         dbesc($name),
165                         dbesc($pdesc),
166                         dbesc($gender),
167                         dbesc($dob),
168                         dbesc($address),
169                         dbesc($locality),
170                         dbesc($region),
171                         dbesc($postal_code),
172                         dbesc($country_name),
173                         dbesc($marital),
174                         dbesc($with),
175                         dbesc($sexual),
176                         dbesc($homepage),
177                         dbesc($politic),
178                         dbesc($religion),
179                         dbesc($keywords),
180                         dbesc($about),
181                         dbesc($interest),
182                         dbesc($contact),
183                         dbesc($music),
184                         dbesc($book),
185                         dbesc($tv),
186                         dbesc($film),
187                         dbesc($romance),
188                         dbesc($work),
189                         dbesc($education),
190                         intval($hide_friends),
191                         intval($a->argv[1]),
192                         intval($_SESSION['uid'])
193                 );
194
195                 if($r)
196                         notice( t('Profile updated.') . EOL);
197
198
199                 if($namechanged && $is_default) {
200                         $r = q("UPDATE `contact` SET `name-date` = '%s' WHERE `self` = 1 AND `uid` = %d LIMIT 1",
201                                 dbesc(datetime_convert()),
202                                 intval(local_user())
203                         );
204                 }
205
206                 if($is_default) {
207                         // Update global directory in background
208                         $url = $_SESSION['my_url'];
209                         if($url && strlen(get_config('system','directory_submit_url')))
210                                 proc_run('php',"include/directory.php","$url");
211                 }
212         }
213 }
214
215
216
217
218 function profiles_content(&$a) {
219
220         $o = '';
221         $o .= '<script> $(document).ready(function() { $(\'#nav-profiles-link\').addClass(\'nav-selected\'); });</script>';
222
223         if(! local_user()) {
224                 notice( t('Permission denied.') . EOL);
225                 return;
226         }
227
228         if(($a->argc > 2) && ($a->argv[1] === "drop") && intval($a->argv[2])) {
229                 $r = q("SELECT * FROM `profile` WHERE `id` = %d AND `uid` = %d AND `is-default` = 0 LIMIT 1",
230                         intval($a->argv[2]),
231                         intval(local_user())
232                 );
233                 if(! count($r)) {
234                         notice( t('Profile not found.') . EOL);
235                         goaway($a->get_baseurl() . '/profiles');
236                         return; // NOTREACHED
237                 }
238
239                 // move every contact using this profile as their default to the user default
240
241                 $r = q("UPDATE `contact` SET `profile-id` = (SELECT `profile`.`id` AS `profile-id` FROM `profile` WHERE `profile`.`is-default` = 1 AND `profile`.`uid` = %d LIMIT 1) WHERE `profile-id` = %d AND `uid` = %d ",
242                         intval(local_user()),
243                         intval($a->argv[2]),
244                         intval(local_user())
245                 );
246                 $r = q("DELETE FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
247                         intval($a->argv[2]),
248                         intval(local_user())
249                 );
250                 if($r)
251                         notice( t('Profile deleted.') . EOL);
252
253                 goaway($a->get_baseurl() . '/profiles');
254                 return; // NOTREACHED
255         }
256
257
258
259
260
261         if(($a->argc > 1) && ($a->argv[1] === 'new')) {
262
263                 $r0 = q("SELECT `id` FROM `profile` WHERE `uid` = %d",
264                         intval(local_user()));
265                 $num_profiles = count($r0);
266
267                 $name = t('Profile-') . ($num_profiles + 1);
268
269                 $r1 = q("SELECT `name`, `photo`, `thumb` FROM `profile` WHERE `uid` = %d AND `is-default` = 1 LIMIT 1",
270                         intval(local_user()));
271                 
272                 $r2 = q("INSERT INTO `profile` (`uid` , `profile-name` , `name`, `photo`, `thumb`)
273                         VALUES ( %d, '%s', '%s', '%s', '%s' )",
274                         intval(local_user()),
275                         dbesc($name),
276                         dbesc($r1[0]['name']),
277                         dbesc($r1[0]['photo']),
278                         dbesc($r1[0]['thumb'])
279                 );
280
281                 $r3 = q("SELECT `id` FROM `profile` WHERE `uid` = %d AND `profile-name` = '%s' LIMIT 1",
282                         intval(local_user()),
283                         dbesc($name)
284                 );
285
286                 notice( t('New profile created.') . EOL);
287                 if(count($r3) == 1)
288                         goaway($a->get_baseurl() . '/profiles/' . $r3[0]['id']);
289                 goaway($a->get_baseurl() . '/profiles');
290         }                
291
292         if(($a->argc > 2) && ($a->argv[1] === 'clone')) {
293
294                 $r0 = q("SELECT `id` FROM `profile` WHERE `uid` = %d",
295                         intval(local_user()));
296                 $num_profiles = count($r0);
297
298                 $name = t('Profile-') . ($num_profiles + 1);
299                 $r1 = q("SELECT * FROM `profile` WHERE `uid` = %d AND `id` = %d LIMIT 1",
300                         intval(local_user()),
301                         intval($a->argv[2])
302                 );
303                 if(! count($r1)) {
304                         notice( t('Profile unavailable to clone.') . EOL);
305                         return;
306                 }
307                 unset($r1[0]['id']);
308                 $r1[0]['is-default'] = 0;
309                 $r1[0]['publish'] = 0;  
310                 $r1[0]['net-publish'] = 0;      
311                 $r1[0]['profile-name'] = dbesc($name);
312
313                 dbesc_array($r1[0]);
314
315                 $r2 = dbq("INSERT INTO `profile` (`" 
316                         . implode("`, `", array_keys($r1[0])) 
317                         . "`) VALUES ('" 
318                         . implode("', '", array_values($r1[0])) 
319                         . "')" );
320
321                 $r3 = q("SELECT `id` FROM `profile` WHERE `uid` = %d AND `profile-name` = '%s' LIMIT 1",
322                         intval(local_user()),
323                         dbesc($name)
324                 );
325                 notice( t('New profile created.') . EOL);
326                 if(count($r3) == 1)
327                         goaway($a->get_baseurl() . '/profiles/' . $r3[0]['id']);
328         goaway($a->get_baseurl() . '/profiles');
329         return; // NOTREACHED
330         }                
331
332
333         if(($a->argc > 1) && (intval($a->argv[1]))) {
334                 $r = q("SELECT * FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
335                         intval($a->argv[1]),
336                         intval(local_user())
337                 );
338                 if(! count($r)) {
339                         notice( t('Profile not found.') . EOL);
340                         return;
341                 }
342
343                 profile_load($a,$a->user['nickname'],$r[0]['id']);
344
345                 require_once('include/profile_selectors.php');
346
347                 $tpl = load_view_file('view/profed_head.tpl');
348
349                 $opt_tpl = load_view_file("view/profile-hide-friends.tpl");
350                 $hide_friends = replace_macros($opt_tpl,array(
351                         '$yes_selected' => (($r[0]['hide-friends']) ? " checked=\"checked\" " : ""),
352                         '$no_selected' => (($r[0]['hide-friends'] == 0) ? " checked=\"checked\" " : "")
353                 ));
354
355
356                 $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
357                 $a->page['htmlhead'] .= "<script type=\"text/javascript\" src=\"include/country.js\" ></script>";
358
359
360                 $is_default = (($r[0]['is-default']) ? 1 : 0);
361                 $tpl = load_view_file("view/profile_edit.tpl");
362                 $o .= replace_macros($tpl,array(
363                         '$disabled' => (($is_default) ? 'onclick="return false;" style="color: #BBBBFF;"' : ''),
364                         '$baseurl' => $a->get_baseurl(),
365                         '$profile_id' => $r[0]['id'],
366                         '$profile_name' => $r[0]['profile-name'],
367                         '$default' => (($is_default) ? '<p id="profile-edit-default-desc">' . t('This is your <strong>public</strong> profile.<br />It <strong>may</strong> be visible to anybody using the internet.') . '</p>' : ""),
368                         '$name' => $r[0]['name'],
369                         '$pdesc' => $r[0]['pdesc'],
370                         '$dob' => dob($r[0]['dob']),
371                         '$hide_friends' => $hide_friends,
372                         '$address' => $r[0]['address'],
373                         '$locality' => $r[0]['locality'],
374                         '$region' => $r[0]['region'],
375                         '$postal_code' => $r[0]['postal-code'],
376                         '$country_name' => $r[0]['country-name'],
377                         '$age' => ((intval($r[0]['dob'])) ? '(' . t('Age: ') . age($r[0]['dob'],$a->user['timezone'],$a->user['timezone']) . ')' : ''),
378                         '$gender' => gender_selector($r[0]['gender']),
379                         '$marital' => marital_selector($r[0]['marital']),
380                         '$with' => strip_tags($r[0]['with']),
381                         '$sexual' => sexpref_selector($r[0]['sexual']),
382                         '$about' => $r[0]['about'],
383                         '$homepage' => $r[0]['homepage'],
384                         '$politic' => $r[0]['politic'],
385                         '$religion' => $r[0]['religion'],
386                         '$keywords' => $r[0]['keywords'],
387                         '$music' => $r[0]['music'],
388                         '$book' => $r[0]['book'],
389                         '$tv' => $r[0]['tv'],
390                         '$film' => $r[0]['film'],
391                         '$interest' => $r[0]['interest'],
392                         '$romance' => $r[0]['romance'],
393                         '$work' => $r[0]['work'],
394                         '$education' => $r[0]['education'],
395                         '$contact' => $r[0]['contact']
396                 ));
397
398                 $arr = array('profile' => $r[0], 'entry' => $o);
399                 call_hooks('profile_edit', $arr);
400
401                 return $o;
402         }
403         else {
404
405                 $r = q("SELECT * FROM `profile` WHERE `uid` = %d",
406                         local_user());
407                 if(count($r)) {
408
409                         $o .= load_view_file('view/profile_listing_header.tpl');
410                         $tpl_default = load_view_file('view/profile_entry_default.tpl');
411                         $tpl = load_view_file('view/profile_entry.tpl');
412
413                         foreach($r as $rr) {
414                                 $template = (($rr['is-default']) ? $tpl_default : $tpl);
415                                 $o .= replace_macros($template, array(
416                                         '$photo' => $rr['thumb'],
417                                         '$id' => $rr['id'],
418                                         '$alt' => t('Profile Image'),
419                                         '$profile_name' => $rr['profile-name']
420                                 ));
421                         }
422                 }
423                 return $o;
424         }
425
426 }