]> git.mxchange.org Git - friendica.git/blob - mod/profiles.php
850a4b8ce4aa70f9baef5375a65814f53f86ed23
[friendica.git] / mod / profiles.php
1 <?php
2
3
4 function profiles_post(&$a) {
5
6         if(! local_user()) {
7                 notice( "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($_SESSION['uid'])
17                 );
18                 if(! count($orig)) {
19                         $_SESSION['sysmsg'] .= "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                         $a->$_SESSION['sysmsg'] .= "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(implode(', ',$_POST['marital'])));
57                 if($marital != $orig[0]['marital'])
58                         $maritalchanged = true;
59
60                 $sexual = notags(trim($_POST['sexual']));
61                 $homepage = notags(trim($_POST['homepage']));
62                 $politic = notags(trim($_POST['politic']));
63                 $religion = notags(trim($_POST['religion']));
64
65                 $about = escape_tags(trim($_POST['about']));
66                 $interest = escape_tags(trim($_POST['interest']));
67                 $contact = escape_tags(trim($_POST['contact']));
68                 $music = escape_tags(trim($_POST['music']));
69                 $book = escape_tags(trim($_POST['book']));
70                 $tv = escape_tags(trim($_POST['tv']));
71                 $film = escape_tags(trim($_POST['film']));
72                 $romance = escape_tags(trim($_POST['romance']));
73                 $work = escape_tags(trim($_POST['work']));
74                 $education = escape_tags(trim($_POST['education']));
75                 if(x($_POST,'profile_in_directory'))
76                         $publish = (($_POST['profile_in_directory'] == 1) ? 1: 0);
77
78                 $r = q("UPDATE `profile` 
79                         SET `profile-name` = '%s',
80                         `name` = '%s',
81                         `gender` = '%s',
82                         `dob` = '%s',
83                         `address` = '%s',
84                         `locality` = '%s',
85                         `region` = '%s',
86                         `postal-code` = '%s',
87                         `country-name` = '%s',
88                         `marital` = '%s',
89                         `sexual` = '%s',
90                         `homepage` = '%s',
91                         `politic` = '%s',
92                         `religion` = '%s',
93                         `about` = '%s',
94                         `interest` = '%s',
95                         `contact` = '%s',
96                         `music` = '%s',
97                         `book` = '%s',
98                         `tv` = '%s',
99                         `film` = '%s',
100                         `romance` = '%s',
101                         `work` = '%s',
102                         `education` = '%s'
103                         WHERE `id` = %d AND `uid` = %d LIMIT 1",
104                         dbesc($profile_name),
105                         dbesc($name),
106                         dbesc($gender),
107                         dbesc($dob),
108                         dbesc($address),
109                         dbesc($locality),
110                         dbesc($region),
111                         dbesc($postal_code),
112                         dbesc($country_name),
113                         dbesc($marital),
114                         dbesc($sexual),
115                         dbesc($homepage),
116                         dbesc($politic),
117                         dbesc($religion),
118                         dbesc($about),
119                         dbesc($interest),
120                         dbesc($contact),
121                         dbesc($music),
122                         dbesc($book),
123                         dbesc($tv),
124                         dbesc($film),
125                         dbesc($romance),
126                         dbesc($work),
127                         dbesc($education),
128                         intval($a->argv[1]),
129                         intval($_SESSION['uid'])
130                 );
131
132                 if($r)
133                         $_SESSION['sysmsg'] .= "Profile updated." . EOL;
134
135
136                 if($is_default) {
137                         $r = q("UPDATE `profile` 
138                         SET `publish` = %d
139                         WHERE `id` = %d AND `uid` = %d LIMIT 1",
140                         intval($publish),
141                         intval($a->argv[1]),
142                         intval($_SESSION['uid'])
143
144                         );
145                 }
146                 if($namechanged && $is_default) {
147                         $r = q("UPDATE `contact` SET `name-date` = '%s' WHERE `self` = 1 AND `uid` = %d LIMIT 1",
148                                 dbesc(datetime_convert()),
149                                 intval($_SESSION['uid'])
150                         );
151                 }
152
153         }
154
155
156
157 }
158
159
160
161
162 function profiles_content(&$a) {
163         if(! local_user()) {
164                 $_SESSION['sysmsg'] .= "Unauthorised." . EOL;
165                 return;
166         }
167
168         if(($a->argc > 2) && ($a->argv[1] == "drop") && intval($a->argv[2])) {
169                 $r = q("SELECT * FROM `profile` WHERE `id` = %d AND `uid` = %d AND `is-default` = 0 LIMIT 1",
170                         intval($a->argv[2]),
171                         intval($_SESSION['uid'])
172                 );
173                 if(! count($r)) {
174                         $_SESSION['sysmsg'] .= "Profile not found." . EOL;
175                         goaway($a->get_baseurl() . '/profiles');
176                         return; // NOTREACHED
177                 }
178
179                 // move every contact using this profile as their default to the user default
180
181                 $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 ",
182                         intval($_SESSION['uid']),
183                         intval($a->argv[2]),
184                         intval($_SESSION['uid'])
185                 );
186                 $r = q("DELETE FROM `profile` WHERE `id` = %d LIMIT 1",
187                         intval($a->argv[2])
188                 );
189                 if($r)
190                         notice("Profile deleted." . EOL);
191
192                 goaway($a->get_baseurl() . '/profiles');
193                 return; // NOTREACHED
194         }
195
196
197
198
199
200         if(($a->argc > 1) && ($a->argv[1] == 'new')) {
201
202                 $r0 = q("SELECT `id` FROM `profile` WHERE `uid` = %d",
203                         intval($_SESSION['uid']));
204                 $num_profiles = count($r0);
205
206                 $name = "Profile-" . ($num_profiles + 1);
207
208                 $r1 = q("SELECT `name`, `photo`, `thumb` FROM `profile` WHERE `uid` = %d AND `is-default` = 1 LIMIT 1",
209                         intval($_SESSION['uid']));
210                 
211                 $r2 = q("INSERT INTO `profile` (`uid` , `profile-name` , `name`, `photo`, `thumb`)
212                         VALUES ( %d, '%s', '%s', '%s', '%s' )",
213                         intval($_SESSION['uid']),
214                         dbesc($name),
215                         dbesc($r1[0]['name']),
216                         dbesc($r1[0]['photo']),
217                         dbesc($ra[0]['thumb'])
218                 );
219
220                 $r3 = q("SELECT `id` FROM `profile` WHERE `uid` = %d AND `profile-name` = '%s' LIMIT 1",
221                         intval($_SESSION['uid']),
222                         dbesc($name)
223                 );
224                 $_SESSION['sysmsg'] .= "New profile created." . EOL;
225                 if(count($r3) == 1)
226                         goaway($a->get_baseurl() . '/profiles/' . $r3[0]['id']);
227                 goaway($a->get_baseurl() . '/profiles');
228         }                
229
230         if(($a->argc > 2) && ($a->argv[1] == 'clone')) {
231
232                 $r0 = q("SELECT `id` FROM `profile` WHERE `uid` = %d",
233                         intval($_SESSION['uid']));
234                 $num_profiles = count($r0);
235
236                 $name = "Profile-" . ($num_profiles + 1);
237                 $r1 = q("SELECT * FROM `profile` WHERE `uid` = %d AND `id` = %d LIMIT 1",
238                         intval($_SESSION['uid']),
239                         intval($a->argv[2])
240                 );
241                 if(! count($r1)) {
242                         notice("Profile unavailable to clone." . EOL);
243                         return;
244                 }
245                 unset($r1[0]['id']);
246                 $r1[0]['is-default'] = 0;
247                 $r1[0]['publish'] = 0;  
248                 $r1[0]['profile-name'] = dbesc($name);
249
250                 dbesc_array($r1[0]);
251
252                 $r2 = q("INSERT INTO `profile` (`" 
253                         . implode("`, `", array_keys($r1[0])) 
254                         . "`) VALUES ('" 
255                         . implode("', '", array_values($r1[0])) 
256                         . "')" );
257
258                 $r3 = q("SELECT `id` FROM `profile` WHERE `uid` = %d AND `profile-name` = '%s' LIMIT 1",
259                         intval($_SESSION['uid']),
260                         dbesc($name)
261                 );
262                 $_SESSION['sysmsg'] .= "New profile created." . EOL;
263                 if(count($r3) == 1)
264                         goaway($a->get_baseurl() . '/profiles/' . $r3[0]['id']);
265         goaway($a->get_baseurl() . '/profiles');
266         return; // NOTREACHED
267         }                
268
269
270         if(intval($a->argv[1])) {
271                 $r = q("SELECT * FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
272                         intval($a->argv[1]),
273                         intval($_SESSION['uid'])
274                 );
275                 if(! count($r)) {
276                         $_SESSION['sysmsg'] .= "Profile not found." . EOL;
277                         return;
278                 }
279
280                 require_once('mod/profile.php');
281                 profile_load($a,$a->user['nickname'],$r[0]['id']);
282
283                 require_once('view/profile_selectors.php');
284
285                 $tpl = file_get_contents('view/profed_head.tpl');
286                 $opt_tpl = file_get_contents("view/profile-in-directory.tpl");
287                 $profile_in_dir = replace_macros($opt_tpl,array(
288                         '$yes_selected' => (($r[0]['publish']) ? " checked=\"checked\" " : ""),
289                         '$no_selected' => (($r[0]['publish'] == 0) ? " checked=\"checked\" " : "")
290                 ));
291
292                 $opt_tpl = file_get_contents("view/profile-hide-friends.tpl");
293                 $hide_friends = replace_macros($opt_tpl,array(
294                         '$yes_selected' => (($r[0]['hide-friends']) ? " checked=\"checked\" " : ""),
295                         '$no_selected' => (($r[0]['hide-friends'] == 0) ? " checked=\"checked\" " : "")
296                 ));
297
298
299                 $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
300                 $a->page['htmlhead'] .= "<script type=\"text/javascript\" src=\"include/country.js\" ></script>";
301
302
303
304         
305
306                 $is_default = (($r[0]['is-default']) ? 1 : 0);
307                 $tpl = file_get_contents("view/profile_edit.tpl");
308                 $o .= replace_macros($tpl,array(
309                         '$disabled' => (($is_default) ? 'onclick="return false;" style="color: #BBBBFF;"' : ''),
310                         '$baseurl' => $a->get_baseurl(),
311                         '$profile_id' => $r[0]['id'],
312                         '$profile_name' => $r[0]['profile-name'],
313                         '$default' => (($is_default) ? "<p id=\"profile-edit-default-desc\">This is your <strong>public</strong> profile.<br />It <strong>may</strong> be visible to anybody using the internet.</p>" : ""),
314                         '$name' => $r[0]['name'],
315                         '$dob' => dob($r[0]['dob']),
316                         '$hide_friends' => $hide_friends,
317                         '$address' => $r[0]['address'],
318                         '$locality' => $r[0]['locality'],
319                         '$region' => $r[0]['region'],
320                         '$postal_code' => $r[0]['postal-code'],
321                         '$country_name' => $r[0]['country-name'],
322                         '$age' => ((intval($r[0]['dob'])) ? '(Age: '. age($r[0]['dob'],$a->user['timezone'],$a->user['timezone']) . ')' : ''),
323                         '$gender' => gender_selector($r[0]['gender']),
324                         '$marital' => marital_selector($r[0]['marital']),
325                         '$sexual' => sexpref_selector($r[0]['sexual']),
326                         '$about' => $r[0]['about'],
327                         '$homepage' => $r[0]['homepage'],
328                         '$politic' => $r[0]['politic'],
329                         '$religion' => $r[0]['religion'],
330                         '$music' => $r[0]['music'],
331                         '$book' => $r[0]['book'],
332                         '$tv' => $r[0]['tv'],
333                         '$film' => $r[0]['film'],
334                         '$interest' => $r[0]['interest'],
335                         '$romance' => $r[0]['romance'],
336                         '$work' => $r[0]['work'],
337                         '$education' => $r[0]['education'],
338                         '$contact' => $r[0]['contact'],
339                         '$profile_in_dir' => (($is_default) ? $profile_in_dir : '')
340                 ));
341
342                 return $o;
343
344
345         }
346         else {
347
348                 $r = q("SELECT * FROM `profile` WHERE `uid` = %d",
349                         $_SESSION['uid']);
350                 if(count($r)) {
351
352                         $o .= file_get_contents('view/profile_listing_header.tpl');
353                         $tpl_default = file_get_contents('view/profile_entry_default.tpl');
354                         $tpl = file_get_contents('view/profile_entry.tpl');
355
356                         foreach($r as $rr) {
357                                 $template = (($rr['is-default']) ? $tpl_default : $tpl);
358                                 $o .= replace_macros($template, array(
359                                         '$photo' => $rr['thumb'],
360                                         '$id' => $rr['id'],
361                                         '$profile_name' => $rr['profile-name']
362                                 ));
363                         }
364                 }
365                 return $o;
366         }
367
368 }