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