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