]> git.mxchange.org Git - friendica.git/blob - mod/profiles.php
string extraction
[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                 $keywords = notags(trim($_POST['keywords']));
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                         `keywords` = '%s',
142                         `about` = '%s',
143                         `interest` = '%s',
144                         `contact` = '%s',
145                         `music` = '%s',
146                         `book` = '%s',
147                         `tv` = '%s',
148                         `film` = '%s',
149                         `romance` = '%s',
150                         `work` = '%s',
151                         `education` = '%s',
152                         `hide-friends` = %d
153                         WHERE `id` = %d AND `uid` = %d LIMIT 1",
154                         dbesc($profile_name),
155                         dbesc($name),
156                         dbesc($gender),
157                         dbesc($dob),
158                         dbesc($address),
159                         dbesc($locality),
160                         dbesc($region),
161                         dbesc($postal_code),
162                         dbesc($country_name),
163                         dbesc($marital),
164                         dbesc($with),
165                         dbesc($sexual),
166                         dbesc($homepage),
167                         dbesc($politic),
168                         dbesc($religion),
169                         dbesc($keywords),
170                         dbesc($about),
171                         dbesc($interest),
172                         dbesc($contact),
173                         dbesc($music),
174                         dbesc($book),
175                         dbesc($tv),
176                         dbesc($film),
177                         dbesc($romance),
178                         dbesc($work),
179                         dbesc($education),
180                         intval($hide_friends),
181                         intval($a->argv[1]),
182                         intval($_SESSION['uid'])
183                 );
184
185                 if($r)
186                         notice( t('Profile updated.') . EOL);
187
188
189                 if($namechanged && $is_default) {
190                         $r = q("UPDATE `contact` SET `name-date` = '%s' WHERE `self` = 1 AND `uid` = %d LIMIT 1",
191                                 dbesc(datetime_convert()),
192                                 intval(local_user())
193                         );
194                 }
195
196                 if($is_default) {
197                         // Update global directory in background
198                         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
199                         $url = $_SESSION['my_url'];
200                         if($url && strlen(get_config('system','directory_submit_url')))
201                                 proc_close(proc_open("\"$php_path\" \"include/directory.php\" \"$url\" &",
202                                         array(),$foo));
203                 }
204         }
205 }
206
207
208
209
210 function profiles_content(&$a) {
211         $o = '';
212         $o .= '<script> $(document).ready(function() { $(\'#nav-profiles-link\').addClass(\'nav-selected\'); });</script>';
213
214         if(! local_user()) {
215                 notice( t('Permission denied.') . EOL);
216                 return;
217         }
218
219         if(($a->argc > 2) && ($a->argv[1] === "drop") && intval($a->argv[2])) {
220                 $r = q("SELECT * FROM `profile` WHERE `id` = %d AND `uid` = %d AND `is-default` = 0 AND `self` = 0 LIMIT 1",
221                         intval($a->argv[2]),
222                         intval(local_user())
223                 );
224                 if(! count($r)) {
225                         notice( t('Profile not found.') . EOL);
226                         goaway($a->get_baseurl() . '/profiles');
227                         return; // NOTREACHED
228                 }
229
230                 // move every contact using this profile as their default to the user default
231
232                 $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 ",
233                         intval(local_user()),
234                         intval($a->argv[2]),
235                         intval(local_user())
236                 );
237                 $r = q("DELETE FROM `profile` WHERE `id` = %d LIMIT 1",
238                         intval($a->argv[2])
239                 );
240                 if($r)
241                         notice( t('Profile deleted.') . EOL);
242
243                 goaway($a->get_baseurl() . '/profiles');
244                 return; // NOTREACHED
245         }
246
247
248
249
250
251         if(($a->argc > 1) && ($a->argv[1] === 'new')) {
252
253                 $r0 = q("SELECT `id` FROM `profile` WHERE `uid` = %d",
254                         intval(local_user()));
255                 $num_profiles = count($r0);
256
257                 $name = t('Profile-') . ($num_profiles + 1);
258
259                 $r1 = q("SELECT `name`, `photo`, `thumb` FROM `profile` WHERE `uid` = %d AND `is-default` = 1 LIMIT 1",
260                         intval(local_user()));
261                 
262                 $r2 = q("INSERT INTO `profile` (`uid` , `profile-name` , `name`, `photo`, `thumb`)
263                         VALUES ( %d, '%s', '%s', '%s', '%s' )",
264                         intval(local_user()),
265                         dbesc($name),
266                         dbesc($r1[0]['name']),
267                         dbesc($r1[0]['photo']),
268                         dbesc($r1[0]['thumb'])
269                 );
270
271                 $r3 = q("SELECT `id` FROM `profile` WHERE `uid` = %d AND `profile-name` = '%s' LIMIT 1",
272                         intval(local_user()),
273                         dbesc($name)
274                 );
275
276                 notice( t('New profile created.') . EOL);
277                 if(count($r3) == 1)
278                         goaway($a->get_baseurl() . '/profiles/' . $r3[0]['id']);
279                 goaway($a->get_baseurl() . '/profiles');
280         }                
281
282         if(($a->argc > 2) && ($a->argv[1] === 'clone')) {
283
284                 $r0 = q("SELECT `id` FROM `profile` WHERE `uid` = %d",
285                         intval(local_user()));
286                 $num_profiles = count($r0);
287
288                 $name = t('Profile-') . ($num_profiles + 1);
289                 $r1 = q("SELECT * FROM `profile` WHERE `uid` = %d AND `id` = %d LIMIT 1",
290                         intval(local_user()),
291                         intval($a->argv[2])
292                 );
293                 if(! count($r1)) {
294                         notice( t('Profile unavailable to clone.') . EOL);
295                         return;
296                 }
297                 unset($r1[0]['id']);
298                 $r1[0]['is-default'] = 0;
299                 $r1[0]['publish'] = 0;  
300                 $r1[0]['net-publish'] = 0;      
301                 $r1[0]['profile-name'] = dbesc($name);
302
303                 dbesc_array($r1[0]);
304
305                 $r2 = dbq("INSERT INTO `profile` (`" 
306                         . implode("`, `", array_keys($r1[0])) 
307                         . "`) VALUES ('" 
308                         . implode("', '", array_values($r1[0])) 
309                         . "')" );
310
311                 $r3 = q("SELECT `id` FROM `profile` WHERE `uid` = %d AND `profile-name` = '%s' LIMIT 1",
312                         intval(local_user()),
313                         dbesc($name)
314                 );
315                 notice( t('New profile created.') . EOL);
316                 if(count($r3) == 1)
317                         goaway($a->get_baseurl() . '/profiles/' . $r3[0]['id']);
318         goaway($a->get_baseurl() . '/profiles');
319         return; // NOTREACHED
320         }                
321
322
323         if(($a->argc > 1) && (intval($a->argv[1]))) {
324                 $r = q("SELECT * FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
325                         intval($a->argv[1]),
326                         intval(local_user())
327                 );
328                 if(! count($r)) {
329                         notice( t('Profile not found.') . EOL);
330                         return;
331                 }
332
333                 profile_load($a,$a->user['nickname'],$r[0]['id']);
334
335                 require_once('include/profile_selectors.php');
336
337                 $tpl = load_view_file('view/profed_head.tpl');
338
339                 $opt_tpl = load_view_file("view/profile-hide-friends.tpl");
340                 $hide_friends = replace_macros($opt_tpl,array(
341                         '$yes_selected' => (($r[0]['hide-friends']) ? " checked=\"checked\" " : ""),
342                         '$no_selected' => (($r[0]['hide-friends'] == 0) ? " checked=\"checked\" " : "")
343                 ));
344
345
346                 $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
347                 $a->page['htmlhead'] .= "<script type=\"text/javascript\" src=\"include/country.js\" ></script>";
348
349
350                 $is_default = (($r[0]['is-default']) ? 1 : 0);
351                 $tpl = load_view_file("view/profile_edit.tpl");
352                 $o .= replace_macros($tpl,array(
353                         '$disabled' => (($is_default) ? 'onclick="return false;" style="color: #BBBBFF;"' : ''),
354                         '$baseurl' => $a->get_baseurl(),
355                         '$profile_id' => $r[0]['id'],
356                         '$profile_name' => $r[0]['profile-name'],
357                         '$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>' : ""),
358                         '$name' => $r[0]['name'],
359                         '$dob' => dob($r[0]['dob']),
360                         '$hide_friends' => $hide_friends,
361                         '$address' => $r[0]['address'],
362                         '$locality' => $r[0]['locality'],
363                         '$region' => $r[0]['region'],
364                         '$postal_code' => $r[0]['postal-code'],
365                         '$country_name' => $r[0]['country-name'],
366                         '$age' => ((intval($r[0]['dob'])) ? '(' . t('Age: ') . age($r[0]['dob'],$a->user['timezone'],$a->user['timezone']) . ')' : ''),
367                         '$gender' => gender_selector($r[0]['gender']),
368                         '$marital' => marital_selector($r[0]['marital']),
369                         '$with' => strip_tags($r[0]['with']),
370                         '$sexual' => sexpref_selector($r[0]['sexual']),
371                         '$about' => $r[0]['about'],
372                         '$homepage' => $r[0]['homepage'],
373                         '$politic' => $r[0]['politic'],
374                         '$religion' => $r[0]['religion'],
375                         '$keywords' => $r[0]['keywords'],
376                         '$music' => $r[0]['music'],
377                         '$book' => $r[0]['book'],
378                         '$tv' => $r[0]['tv'],
379                         '$film' => $r[0]['film'],
380                         '$interest' => $r[0]['interest'],
381                         '$romance' => $r[0]['romance'],
382                         '$work' => $r[0]['work'],
383                         '$education' => $r[0]['education'],
384                         '$contact' => $r[0]['contact']
385                 ));
386
387                 return $o;
388         }
389         else {
390
391                 $r = q("SELECT * FROM `profile` WHERE `uid` = %d",
392                         local_user());
393                 if(count($r)) {
394
395                         $o .= load_view_file('view/profile_listing_header.tpl');
396                         $tpl_default = load_view_file('view/profile_entry_default.tpl');
397                         $tpl = load_view_file('view/profile_entry.tpl');
398
399                         foreach($r as $rr) {
400                                 $template = (($rr['is-default']) ? $tpl_default : $tpl);
401                                 $o .= replace_macros($template, array(
402                                         '$photo' => $rr['thumb'],
403                                         '$id' => $rr['id'],
404                                         '$alt' => t('Profile Image'),
405                                         '$profile_name' => $rr['profile-name']
406                                 ));
407                         }
408                 }
409                 return $o;
410         }
411
412 }