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