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