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