streams/mod/profiles.php

799 lines
24 KiB
PHP
Raw Normal View History

2010-07-01 23:48:07 +00:00
<?php
2013-01-06 21:42:51 +00:00
function profiles_init(&$a) {
nav_set_selected('profiles');
2015-01-29 04:56:04 +00:00
if(! local_channel()) {
2013-01-06 21:42:51 +00:00
return;
}
if((argc() > 2) && (argv(1) === "drop") && intval(argv(2))) {
$r = q("SELECT * FROM `profile` WHERE `id` = %d AND `uid` = %d AND `is_default` = 0 LIMIT 1",
intval(argv(2)),
2015-01-29 04:56:04 +00:00
intval(local_channel())
2013-01-06 21:42:51 +00:00
);
if(! count($r)) {
notice( t('Profile not found.') . EOL);
goaway($a->get_baseurl(true) . '/profiles');
return; // NOTREACHED
}
$profile_guid = $r['profile_guid'];
check_form_security_token_redirectOnErr('/profiles', 'profile_drop', 't');
// move every contact using this profile as their default to the user default
$r = q("UPDATE abook SET abook_profile = (SELECT profile_guid AS FROM profile WHERE is_default = 1 AND uid = %d LIMIT 1) WHERE abook_profile = '%s' AND abook_channel = %d ",
2015-01-29 04:56:04 +00:00
intval(local_channel()),
2013-01-06 21:42:51 +00:00
dbesc($profile_guid),
2015-01-29 04:56:04 +00:00
intval(local_channel())
2013-01-06 21:42:51 +00:00
);
PostgreSQL support initial commit There were 11 main types of changes: - UPDATE's and DELETE's sometimes had LIMIT 1 at the end of them. This is not only non-compliant but it would certainly not do what whoever wrote it thought it would. It is likely this mistake was just copied from Friendica. All of these instances, the LIMIT 1 was simply removed. - Bitwise operations (and even some non-zero int checks) erroneously rely on MySQL implicit integer-boolean conversion in the WHERE clauses. This is non-compliant (and bad programming practice to boot). Proper explicit boolean conversions were added. New queries should use proper conventions. - MySQL has a different operator for bitwise XOR than postgres. Rather than add yet another dba_ func, I converted them to "& ~" ("AND NOT") when turning off, and "|" ("OR") when turning on. There were no true toggles (XOR). New queries should refrain from using XOR when not necessary. - There are several fields which the schema has marked as NOT NULL, but the inserts don't specify them. The reason this works is because mysql totally ignores the constraint and adds an empty text default automatically. Again, non-compliant, obviously. In these cases a default of empty text was added. - Several statements rely on a non-standard MySQL feature (http://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html). These queries can all be rewritten to be standards compliant. Interestingly enough, the newly rewritten standards compliant queries run a zillion times faster, even on MySQL. - A couple of function/operator name translations were needed (RAND/RANDOM, GROUP_CONCAT/STRING_AGG, UTC_NOW, REGEXP/~, ^/#) -- assist functions added in the dba_ - INTERVALs: postgres requires quotes around the value, mysql requires that there are not quotes around the value -- assist functions added in the dba_ - NULL_DATE's -- Postgres does not allow the invalid date '0000-00-00 00:00:00' (there is no such thing as year 0 or month 0 or day 0). We use '0001-01-01 00:00:00' for postgres. Conversions are handled in Zot/item packets automagically by quoting all dates with dbescdate(). - char(##) specifications in the schema creates fields with blank spaces that aren't trimmed in the code. MySQL apparently treats char(##) as varchar(##), again, non-compliant. Since postgres works better with text fields anyway, this ball of bugs was simply side-stepped by using 'text' datatype for all text fields in the postgres schema. varchar was used in a couple of places where it actually seemed appropriate (size constraint), but without rigorously vetting that all of the PHP code actually validates data, new bugs might come out from under the rug. - postgres doesn't store nul bytes and a few other non-printables in text fields, even when quoted. bytea fields were used when storing binary data (photo.data, attach.data). A new dbescbin() function was added to handle this transparently. - postgres does not support LIMIT #,# syntax. All databases support LIMIT # OFFSET # syntax. Statements were updated to be standard. These changes require corresponding changes in the coding standards. Please review those before adding any code going forward. Still on my TODO list: - remove quotes from non-reserved identifiers and make reserved identifiers use dba func for quoting - Rewrite search queries for better results (both MySQL and Postgres)
2014-11-13 20:21:58 +00:00
$r = q("DELETE FROM `profile` WHERE `id` = %d AND `uid` = %d",
2013-01-06 21:42:51 +00:00
intval(argv(2)),
2015-01-29 04:56:04 +00:00
intval(local_channel())
2013-01-06 21:42:51 +00:00
);
if($r)
info( t('Profile deleted.') . EOL);
goaway($a->get_baseurl(true) . '/profiles');
return; // NOTREACHED
}
if((argc() > 1) && (argv(1) === 'new')) {
2013-07-03 09:47:36 +00:00
// check_form_security_token_redirectOnErr('/profiles', 'profile_new', 't');
2013-01-06 21:42:51 +00:00
$r0 = q("SELECT `id` FROM `profile` WHERE `uid` = %d",
2015-01-29 04:56:04 +00:00
intval(local_channel()));
2013-01-06 21:42:51 +00:00
$num_profiles = count($r0);
$name = t('Profile-') . ($num_profiles + 1);
$r1 = q("SELECT `name`, `photo`, `thumb` FROM `profile` WHERE `uid` = %d AND `is_default` = 1 LIMIT 1",
2015-01-29 04:56:04 +00:00
intval(local_channel()));
2013-01-06 21:42:51 +00:00
$r2 = q("INSERT INTO `profile` (`aid`, `uid` , `profile_guid`, `profile_name` , `name`, `photo`, `thumb`)
2013-03-21 17:38:34 +00:00
VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s' )",
2013-01-06 21:42:51 +00:00
intval(get_account_id()),
2015-01-29 04:56:04 +00:00
intval(local_channel()),
2013-01-06 21:42:51 +00:00
dbesc(random_string()),
dbesc($name),
dbesc($r1[0]['name']),
dbesc($r1[0]['photo']),
dbesc($r1[0]['thumb'])
);
$r3 = q("SELECT `id` FROM `profile` WHERE `uid` = %d AND `profile_name` = '%s' LIMIT 1",
2015-01-29 04:56:04 +00:00
intval(local_channel()),
2013-01-06 21:42:51 +00:00
dbesc($name)
);
info( t('New profile created.') . EOL);
if(count($r3) == 1)
goaway($a->get_baseurl(true) . '/profiles/' . $r3[0]['id']);
goaway($a->get_baseurl(true) . '/profiles');
}
if((argc() > 2) && (argv(1) === 'clone')) {
check_form_security_token_redirectOnErr('/profiles', 'profile_clone', 't');
$r0 = q("SELECT `id` FROM `profile` WHERE `uid` = %d",
2015-01-29 04:56:04 +00:00
intval(local_channel()));
2013-01-06 21:42:51 +00:00
$num_profiles = count($r0);
$name = t('Profile-') . ($num_profiles + 1);
$r1 = q("SELECT * FROM `profile` WHERE `uid` = %d AND `id` = %d LIMIT 1",
2015-01-29 04:56:04 +00:00
intval(local_channel()),
2013-01-06 21:42:51 +00:00
intval($a->argv[2])
);
if(! count($r1)) {
notice( t('Profile unavailable to clone.') . EOL);
$a->error = 404;
return;
}
unset($r1[0]['id']);
$r1[0]['is_default'] = 0;
$r1[0]['publish'] = 0;
$r1[0]['profile_name'] = dbesc($name);
$r1[0]['profile_guid'] = dbesc(random_string());
dbesc_array($r1[0]);
$r2 = dbq("INSERT INTO `profile` (`"
. implode("`, `", array_keys($r1[0]))
. "`) VALUES ('"
. implode("', '", array_values($r1[0]))
. "')" );
$r3 = q("SELECT `id` FROM `profile` WHERE `uid` = %d AND `profile_name` = '%s' LIMIT 1",
2015-01-29 04:56:04 +00:00
intval(local_channel()),
2013-01-06 21:42:51 +00:00
dbesc($name)
);
info( t('New profile created.') . EOL);
if(count($r3) == 1)
goaway($a->get_baseurl(true) . '/profiles/' . $r3[0]['id']);
goaway($a->get_baseurl(true) . '/profiles');
return; // NOTREACHED
}
2014-08-04 11:40:45 +00:00
if((argc() > 2) && (argv(1) === 'export')) {
$r1 = q("SELECT * FROM `profile` WHERE `uid` = %d AND `id` = %d LIMIT 1",
2015-01-29 04:56:04 +00:00
intval(local_channel()),
2014-08-04 11:40:45 +00:00
intval(argv(2))
);
if(! $r1) {
notice( t('Profile unavailable to export.') . EOL);
$a->error = 404;
return;
}
header('content-type: application/octet_stream');
header('content-disposition: attachment; filename="' . $r1[0]['profile_name'] . '.json"' );
unset($r1[0]['id']);
unset($r1[0]['aid']);
unset($r1[0]['uid']);
unset($r1[0]['is_default']);
unset($r1[0]['publish']);
unset($r1[0]['profile_name']);
unset($r1[0]['profile_guid']);
echo json_encode($r1[0]);
killme();
}
2013-01-06 21:42:51 +00:00
// Run profile_load() here to make sure the theme is set before
// we start loading content
2015-01-29 04:56:04 +00:00
if(((argc() > 1) && (intval(argv(1)))) || !feature_enabled(local_channel(),'multi_profiles')) {
if(feature_enabled(local_channel(),'multi_profiles'))
$id = $a->argv[1];
else {
$x = q("select id from profile where uid = %d and is_default = 1",
2015-01-29 04:56:04 +00:00
intval(local_channel())
);
if($x)
$id = $x[0]['id'];
}
2013-01-06 21:42:51 +00:00
$r = q("SELECT * FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
intval($id),
2015-01-29 04:56:04 +00:00
intval(local_channel())
2013-01-06 21:42:51 +00:00
);
if(! count($r)) {
notice( t('Profile not found.') . EOL);
$a->error = 404;
return;
}
$chan = $a->get_channel();
profile_load($a,$chan['channel_address'],$r[0]['id']);
}
}
2010-07-01 23:48:07 +00:00
function profiles_post(&$a) {
2015-01-29 04:56:04 +00:00
if(! local_channel()) {
notice( t('Permission denied.') . EOL);
2010-07-01 23:48:07 +00:00
return;
}
2012-10-22 00:23:21 +00:00
require_once('include/activities.php');
$namechanged = false;
2011-01-20 23:30:45 +00:00
call_hooks('profile_post', $_POST);
// import from json export file.
// Only import fields that are allowed on this hub
if(x($_FILES,'userfile')) {
$src = $_FILES['userfile']['tmp_name'];
$filesize = intval($_FILES['userfile']['size']);
if($filesize) {
$j = @json_decode(@file_get_contents($src),true);
@unlink($src);
if($j) {
$fields = get_profile_fields_advanced();
if($fields) {
foreach($j as $jj => $v) {
foreach($fields as $f => $n) {
if($jj == $f) {
$_POST[$f] = $v;
break;
}
}
}
}
}
}
}
if((argc() > 1) && (argv(1) !== "new") && intval(argv(1))) {
$orig = q("SELECT * FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
2010-07-01 23:48:07 +00:00
intval($a->argv[1]),
2015-01-29 04:56:04 +00:00
intval(local_channel())
2010-07-01 23:48:07 +00:00
);
if(! count($orig)) {
notice( t('Profile not found.') . EOL);
2010-07-01 23:48:07 +00:00
return;
}
2012-03-12 20:17:37 +00:00
check_form_security_token_redirectOnErr('/profiles', 'profile_edit');
2012-08-31 01:17:38 +00:00
$is_default = (($orig[0]['is_default']) ? 1 : 0);
2010-07-01 23:48:07 +00:00
$profile_name = notags(trim($_POST['profile_name']));
if(! strlen($profile_name)) {
notify( t('Profile Name is required.') . EOL);
2010-07-01 23:48:07 +00:00
return;
}
2014-10-19 23:12:05 +00:00
$dob = $_POST['dob'] ? escape_tags(trim($_POST['dob'])) : '0000-00-00'; // FIXME: Needs to be validated?
$y = substr($dob,0,4);
if((! ctype_digit($y)) || ($y < 1900))
$ignore_year = true;
else
$ignore_year = false;
if($dob != '0000-00-00') {
if(strpos($dob,'0000-') === 0) {
$ignore_year = true;
$dob = substr($dob,5);
}
$dob = datetime_convert('UTC','UTC',(($ignore_year) ? '1900-' . $dob : $dob),(($ignore_year) ? 'm-d' : 'Y-m-d'));
if($ignore_year)
$dob = '0000-' . $dob;
}
2010-07-10 23:47:10 +00:00
2014-04-21 01:48:21 +00:00
$name = escape_tags(trim($_POST['name']));
if($orig[0]['name'] != $name)
$namechanged = true;
2014-04-21 01:48:21 +00:00
$pdesc = escape_tags(trim($_POST['pdesc']));
$gender = escape_tags(trim($_POST['gender']));
$address = escape_tags(trim($_POST['address']));
$locality = escape_tags(trim($_POST['locality']));
$region = escape_tags(trim($_POST['region']));
$postal_code = escape_tags(trim($_POST['postal_code']));
$country_name = escape_tags(trim($_POST['country_name']));
$keywords = escape_tags(trim($_POST['keywords']));
$marital = escape_tags(trim($_POST['marital']));
$howlong = escape_tags(trim($_POST['howlong']));
$sexual = escape_tags(trim($_POST['sexual']));
$homepage = escape_tags(trim($_POST['homepage']));
$hometown = escape_tags(trim($_POST['hometown']));
$politic = escape_tags(trim($_POST['politic']));
$religion = escape_tags(trim($_POST['religion']));
2012-10-22 00:23:21 +00:00
$likes = fix_mce_lf(escape_tags(trim($_POST['likes'])));
$dislikes = fix_mce_lf(escape_tags(trim($_POST['dislikes'])));
$about = fix_mce_lf(escape_tags(trim($_POST['about'])));
$interest = fix_mce_lf(escape_tags(trim($_POST['interest'])));
$contact = fix_mce_lf(escape_tags(trim($_POST['contact'])));
$channels = fix_mce_lf(escape_tags(trim($_POST['channels'])));
2012-10-22 00:23:21 +00:00
$music = fix_mce_lf(escape_tags(trim($_POST['music'])));
$book = fix_mce_lf(escape_tags(trim($_POST['book'])));
$tv = fix_mce_lf(escape_tags(trim($_POST['tv'])));
$film = fix_mce_lf(escape_tags(trim($_POST['film'])));
$romance = fix_mce_lf(escape_tags(trim($_POST['romance'])));
$work = fix_mce_lf(escape_tags(trim($_POST['work'])));
$education = fix_mce_lf(escape_tags(trim($_POST['education'])));
$hide_friends = ((intval($_POST['hide_friends'])) ? 1: 0);
2010-08-10 05:58:58 +00:00
require_once('include/text.php');
2015-01-29 04:56:04 +00:00
linkify_tags($a, $likes, local_channel());
linkify_tags($a, $dislikes, local_channel());
linkify_tags($a, $about, local_channel());
linkify_tags($a, $interest, local_channel());
linkify_tags($a, $interest, local_channel());
linkify_tags($a, $contact, local_channel());
linkify_tags($a, $channels, local_channel());
linkify_tags($a, $music, local_channel());
linkify_tags($a, $book, local_channel());
linkify_tags($a, $tv, local_channel());
linkify_tags($a, $film, local_channel());
linkify_tags($a, $romance, local_channel());
linkify_tags($a, $work, local_channel());
linkify_tags($a, $education, local_channel());
2010-08-10 05:58:58 +00:00
2014-04-21 01:48:21 +00:00
$with = ((x($_POST,'with')) ? escape_tags(trim($_POST['with'])) : '');
2012-06-02 09:30:26 +00:00
if(! strlen($howlong))
$howlong = NULL_DATE;
2012-06-02 09:30:26 +00:00
else
$howlong = datetime_convert(date_default_timezone_get(),'UTC',$howlong);
// linkify the relationship target if applicable
$withchanged = false;
if(strlen($with)) {
if($with != strip_tags($orig[0]['with'])) {
$withchanged = true;
$prf = '';
$lookup = $with;
if(strpos($lookup,'@') === 0)
$lookup = substr($lookup,1);
$lookup = str_replace('_',' ', $lookup);
2014-01-23 23:43:35 +00:00
$newname = $lookup;
$r = q("SELECT * FROM abook left join xchan on abook_xchan = xchan_hash WHERE xchan_name = '%s' AND abook_channel = %d LIMIT 1",
dbesc($newname),
2015-01-29 04:56:04 +00:00
intval(local_channel())
2014-01-23 23:43:35 +00:00
);
if(! $r) {
$r = q("SELECT * FROM abook left join xchan on abook_xchan = xchan_hash WHERE xchan_addr = '%s' AND abook_channel = %d LIMIT 1",
dbesc($lookup . '@%'),
2015-01-29 04:56:04 +00:00
intval(local_channel())
);
}
2014-01-23 23:43:35 +00:00
if($r) {
$prf = $r[0]['xchan_url'];
$newname = $r[0]['xchan_name'];
}
if($prf) {
$with = str_replace($lookup,'<a href="' . $prf . '">' . $newname . '</a>', $with);
if(strpos($with,'@') === 0)
$with = substr($with,1);
}
}
else
$with = $orig[0]['with'];
}
$profile_fields_basic = get_profile_fields_basic();
$profile_fields_advanced = get_profile_fields_advanced();
2015-01-29 04:56:04 +00:00
$advanced = ((feature_enabled(local_channel(),'advanced_profiles')) ? true : false);
if($advanced)
$fields = $profile_fields_advanced;
else
$fields = $profile_fields_basic;
$z = q("select * from profdef where true");
if($z) {
foreach($z as $zz) {
if(array_key_exists($zz['field_name'],$fields)) {
$w = q("select * from profext where channel_id = %d and hash = '%s' and k = '%s' limit 1",
2015-01-29 04:56:04 +00:00
intval(local_channel()),
dbesc($orig[0]['profile_guid']),
dbesc($zz['field_name'])
);
if($w) {
PostgreSQL support initial commit There were 11 main types of changes: - UPDATE's and DELETE's sometimes had LIMIT 1 at the end of them. This is not only non-compliant but it would certainly not do what whoever wrote it thought it would. It is likely this mistake was just copied from Friendica. All of these instances, the LIMIT 1 was simply removed. - Bitwise operations (and even some non-zero int checks) erroneously rely on MySQL implicit integer-boolean conversion in the WHERE clauses. This is non-compliant (and bad programming practice to boot). Proper explicit boolean conversions were added. New queries should use proper conventions. - MySQL has a different operator for bitwise XOR than postgres. Rather than add yet another dba_ func, I converted them to "& ~" ("AND NOT") when turning off, and "|" ("OR") when turning on. There were no true toggles (XOR). New queries should refrain from using XOR when not necessary. - There are several fields which the schema has marked as NOT NULL, but the inserts don't specify them. The reason this works is because mysql totally ignores the constraint and adds an empty text default automatically. Again, non-compliant, obviously. In these cases a default of empty text was added. - Several statements rely on a non-standard MySQL feature (http://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html). These queries can all be rewritten to be standards compliant. Interestingly enough, the newly rewritten standards compliant queries run a zillion times faster, even on MySQL. - A couple of function/operator name translations were needed (RAND/RANDOM, GROUP_CONCAT/STRING_AGG, UTC_NOW, REGEXP/~, ^/#) -- assist functions added in the dba_ - INTERVALs: postgres requires quotes around the value, mysql requires that there are not quotes around the value -- assist functions added in the dba_ - NULL_DATE's -- Postgres does not allow the invalid date '0000-00-00 00:00:00' (there is no such thing as year 0 or month 0 or day 0). We use '0001-01-01 00:00:00' for postgres. Conversions are handled in Zot/item packets automagically by quoting all dates with dbescdate(). - char(##) specifications in the schema creates fields with blank spaces that aren't trimmed in the code. MySQL apparently treats char(##) as varchar(##), again, non-compliant. Since postgres works better with text fields anyway, this ball of bugs was simply side-stepped by using 'text' datatype for all text fields in the postgres schema. varchar was used in a couple of places where it actually seemed appropriate (size constraint), but without rigorously vetting that all of the PHP code actually validates data, new bugs might come out from under the rug. - postgres doesn't store nul bytes and a few other non-printables in text fields, even when quoted. bytea fields were used when storing binary data (photo.data, attach.data). A new dbescbin() function was added to handle this transparently. - postgres does not support LIMIT #,# syntax. All databases support LIMIT # OFFSET # syntax. Statements were updated to be standard. These changes require corresponding changes in the coding standards. Please review those before adding any code going forward. Still on my TODO list: - remove quotes from non-reserved identifiers and make reserved identifiers use dba func for quoting - Rewrite search queries for better results (both MySQL and Postgres)
2014-11-13 20:21:58 +00:00
q("update profext set v = '%s' where id = %d",
dbesc(escape_tags(trim($_POST[$zz['field_name']]))),
intval($w[0]['id'])
);
}
else {
2014-08-29 05:27:05 +00:00
q("insert into profext ( channel_id, hash, k, v ) values ( %d, '%s', '%s', '%s') ",
2015-01-29 04:56:04 +00:00
intval(local_channel()),
dbesc($orig[0]['profile_guid']),
dbesc($zz['field_name']),
dbesc(escape_tags(trim($_POST[$zz['field_name']])))
);
}
}
}
}
2012-04-13 04:10:32 +00:00
$changes = array();
$value = '';
2012-04-13 04:10:32 +00:00
if($is_default) {
if($marital != $orig[0]['marital']) {
2012-04-29 09:11:33 +00:00
$changes[] = '[color=#ff0000]&hearts;[/color] ' . t('Marital Status');
$value = $marital;
}
if($withchanged) {
2012-05-25 07:42:38 +00:00
$changes[] = '[color=#ff0000]&hearts;[/color] ' . t('Romantic Partner');
$value = strip_tags($with);
2012-06-25 04:16:55 +00:00
}
if($likes != $orig[0]['likes']) {
$changes[] = t('Likes');
$value = $likes;
}
if($dislikes != $orig[0]['dislikes']) {
$changes[] = t('Dislikes');
$value = $dislikes;
}
if($work != $orig[0]['work']) {
$changes[] = t('Work/Employment');
}
if($religion != $orig[0]['religion']) {
$changes[] = t('Religion');
$value = $religion;
}
if($politic != $orig[0]['politic']) {
$changes[] = t('Political Views');
$value = $politic;
}
if($gender != $orig[0]['gender']) {
$changes[] = t('Gender');
$value = $gender;
}
if($sexual != $orig[0]['sexual']) {
$changes[] = t('Sexual Preference');
$value = $sexual;
}
if($homepage != $orig[0]['homepage']) {
$changes[] = t('Homepage');
$value = $homepage;
}
if($interest != $orig[0]['interest']) {
$changes[] = t('Interests');
$value = $interest;
}
2012-05-25 22:56:18 +00:00
if($address != $orig[0]['address']) {
$changes[] = t('Address');
// New address not sent in notifications, potential privacy issues
// in case this leaks to unintended recipients. Yes, it's in the public
// profile but that doesn't mean we have to broadcast it to everybody.
}
if($locality != $orig[0]['locality'] || $region != $orig[0]['region']
2012-08-31 01:17:38 +00:00
|| $country_name != $orig[0]['country_name']) {
$changes[] = t('Location');
2012-05-25 22:56:18 +00:00
$comma1 = ((($locality) && ($region || $country_name)) ? ', ' : ' ');
$comma2 = (($region && $country_name) ? ', ' : '');
$value = $locality . $comma1 . $region . $comma2 . $country_name;
}
2012-04-13 04:10:32 +00:00
profile_activity($changes,$value);
2012-04-13 04:10:32 +00:00
}
2010-07-01 23:48:07 +00:00
$r = q("UPDATE `profile`
2012-08-27 08:22:08 +00:00
SET `profile_name` = '%s',
2010-07-01 23:48:07 +00:00
`name` = '%s',
2011-01-19 03:25:28 +00:00
`pdesc` = '%s',
2010-07-01 23:48:07 +00:00
`gender` = '%s',
2010-07-10 23:47:10 +00:00
`dob` = '%s',
2010-07-01 23:48:07 +00:00
`address` = '%s',
`locality` = '%s',
`region` = '%s',
2012-08-31 01:17:38 +00:00
`postal_code` = '%s',
`country_name` = '%s',
2010-07-01 23:48:07 +00:00
`marital` = '%s',
`with` = '%s',
2012-06-02 09:30:26 +00:00
`howlong` = '%s',
2010-07-10 23:47:10 +00:00
`sexual` = '%s',
2010-07-01 23:48:07 +00:00
`homepage` = '%s',
2012-06-03 03:58:20 +00:00
`hometown` = '%s',
2010-07-10 23:47:10 +00:00
`politic` = '%s',
`religion` = '%s',
`keywords` = '%s',
2012-06-25 04:16:55 +00:00
`likes` = '%s',
`dislikes` = '%s',
2010-07-10 23:47:10 +00:00
`about` = '%s',
`interest` = '%s',
`contact` = '%s',
`channels` = '%s',
2010-07-10 23:47:10 +00:00
`music` = '%s',
`book` = '%s',
`tv` = '%s',
`film` = '%s',
`romance` = '%s',
`work` = '%s',
`education` = '%s',
2012-08-31 01:17:38 +00:00
`hide_friends` = %d
PostgreSQL support initial commit There were 11 main types of changes: - UPDATE's and DELETE's sometimes had LIMIT 1 at the end of them. This is not only non-compliant but it would certainly not do what whoever wrote it thought it would. It is likely this mistake was just copied from Friendica. All of these instances, the LIMIT 1 was simply removed. - Bitwise operations (and even some non-zero int checks) erroneously rely on MySQL implicit integer-boolean conversion in the WHERE clauses. This is non-compliant (and bad programming practice to boot). Proper explicit boolean conversions were added. New queries should use proper conventions. - MySQL has a different operator for bitwise XOR than postgres. Rather than add yet another dba_ func, I converted them to "& ~" ("AND NOT") when turning off, and "|" ("OR") when turning on. There were no true toggles (XOR). New queries should refrain from using XOR when not necessary. - There are several fields which the schema has marked as NOT NULL, but the inserts don't specify them. The reason this works is because mysql totally ignores the constraint and adds an empty text default automatically. Again, non-compliant, obviously. In these cases a default of empty text was added. - Several statements rely on a non-standard MySQL feature (http://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html). These queries can all be rewritten to be standards compliant. Interestingly enough, the newly rewritten standards compliant queries run a zillion times faster, even on MySQL. - A couple of function/operator name translations were needed (RAND/RANDOM, GROUP_CONCAT/STRING_AGG, UTC_NOW, REGEXP/~, ^/#) -- assist functions added in the dba_ - INTERVALs: postgres requires quotes around the value, mysql requires that there are not quotes around the value -- assist functions added in the dba_ - NULL_DATE's -- Postgres does not allow the invalid date '0000-00-00 00:00:00' (there is no such thing as year 0 or month 0 or day 0). We use '0001-01-01 00:00:00' for postgres. Conversions are handled in Zot/item packets automagically by quoting all dates with dbescdate(). - char(##) specifications in the schema creates fields with blank spaces that aren't trimmed in the code. MySQL apparently treats char(##) as varchar(##), again, non-compliant. Since postgres works better with text fields anyway, this ball of bugs was simply side-stepped by using 'text' datatype for all text fields in the postgres schema. varchar was used in a couple of places where it actually seemed appropriate (size constraint), but without rigorously vetting that all of the PHP code actually validates data, new bugs might come out from under the rug. - postgres doesn't store nul bytes and a few other non-printables in text fields, even when quoted. bytea fields were used when storing binary data (photo.data, attach.data). A new dbescbin() function was added to handle this transparently. - postgres does not support LIMIT #,# syntax. All databases support LIMIT # OFFSET # syntax. Statements were updated to be standard. These changes require corresponding changes in the coding standards. Please review those before adding any code going forward. Still on my TODO list: - remove quotes from non-reserved identifiers and make reserved identifiers use dba func for quoting - Rewrite search queries for better results (both MySQL and Postgres)
2014-11-13 20:21:58 +00:00
WHERE `id` = %d AND `uid` = %d",
2010-07-01 23:48:07 +00:00
dbesc($profile_name),
dbesc($name),
2011-01-19 03:25:28 +00:00
dbesc($pdesc),
2010-07-01 23:48:07 +00:00
dbesc($gender),
2010-07-10 23:47:10 +00:00
dbesc($dob),
2010-07-01 23:48:07 +00:00
dbesc($address),
dbesc($locality),
dbesc($region),
dbesc($postal_code),
dbesc($country_name),
dbesc($marital),
dbesc($with),
2012-06-02 09:30:26 +00:00
dbesc($howlong),
2010-07-10 23:47:10 +00:00
dbesc($sexual),
2010-07-01 23:48:07 +00:00
dbesc($homepage),
2012-06-03 03:58:20 +00:00
dbesc($hometown),
2010-07-10 23:47:10 +00:00
dbesc($politic),
dbesc($religion),
dbesc($keywords),
2012-06-25 04:16:55 +00:00
dbesc($likes),
dbesc($dislikes),
2010-07-01 23:48:07 +00:00
dbesc($about),
2010-07-10 23:47:10 +00:00
dbesc($interest),
dbesc($contact),
dbesc($channels),
2010-07-10 23:47:10 +00:00
dbesc($music),
dbesc($book),
dbesc($tv),
dbesc($film),
dbesc($romance),
dbesc($work),
dbesc($education),
intval($hide_friends),
2014-06-18 00:34:51 +00:00
intval(argv(1)),
2015-01-29 04:56:04 +00:00
intval(local_channel())
2010-07-01 23:48:07 +00:00
);
if($r)
info( t('Profile updated.') . EOL);
2010-07-09 10:10:28 +00:00
2014-06-18 00:34:51 +00:00
$r = q("select * from profile where id = %d and uid = %d limit 1",
intval(argv(1)),
2015-01-29 04:56:04 +00:00
intval(local_channel())
2014-06-18 00:34:51 +00:00
);
if($r) {
require_once('include/zot.php');
2015-01-29 04:56:04 +00:00
build_sync_packet(local_channel(),array('profile' => $r));
2014-06-18 00:34:51 +00:00
}
$channel = $a->get_channel();
2010-07-09 10:10:28 +00:00
if($namechanged && $is_default) {
PostgreSQL support initial commit There were 11 main types of changes: - UPDATE's and DELETE's sometimes had LIMIT 1 at the end of them. This is not only non-compliant but it would certainly not do what whoever wrote it thought it would. It is likely this mistake was just copied from Friendica. All of these instances, the LIMIT 1 was simply removed. - Bitwise operations (and even some non-zero int checks) erroneously rely on MySQL implicit integer-boolean conversion in the WHERE clauses. This is non-compliant (and bad programming practice to boot). Proper explicit boolean conversions were added. New queries should use proper conventions. - MySQL has a different operator for bitwise XOR than postgres. Rather than add yet another dba_ func, I converted them to "& ~" ("AND NOT") when turning off, and "|" ("OR") when turning on. There were no true toggles (XOR). New queries should refrain from using XOR when not necessary. - There are several fields which the schema has marked as NOT NULL, but the inserts don't specify them. The reason this works is because mysql totally ignores the constraint and adds an empty text default automatically. Again, non-compliant, obviously. In these cases a default of empty text was added. - Several statements rely on a non-standard MySQL feature (http://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html). These queries can all be rewritten to be standards compliant. Interestingly enough, the newly rewritten standards compliant queries run a zillion times faster, even on MySQL. - A couple of function/operator name translations were needed (RAND/RANDOM, GROUP_CONCAT/STRING_AGG, UTC_NOW, REGEXP/~, ^/#) -- assist functions added in the dba_ - INTERVALs: postgres requires quotes around the value, mysql requires that there are not quotes around the value -- assist functions added in the dba_ - NULL_DATE's -- Postgres does not allow the invalid date '0000-00-00 00:00:00' (there is no such thing as year 0 or month 0 or day 0). We use '0001-01-01 00:00:00' for postgres. Conversions are handled in Zot/item packets automagically by quoting all dates with dbescdate(). - char(##) specifications in the schema creates fields with blank spaces that aren't trimmed in the code. MySQL apparently treats char(##) as varchar(##), again, non-compliant. Since postgres works better with text fields anyway, this ball of bugs was simply side-stepped by using 'text' datatype for all text fields in the postgres schema. varchar was used in a couple of places where it actually seemed appropriate (size constraint), but without rigorously vetting that all of the PHP code actually validates data, new bugs might come out from under the rug. - postgres doesn't store nul bytes and a few other non-printables in text fields, even when quoted. bytea fields were used when storing binary data (photo.data, attach.data). A new dbescbin() function was added to handle this transparently. - postgres does not support LIMIT #,# syntax. All databases support LIMIT # OFFSET # syntax. Statements were updated to be standard. These changes require corresponding changes in the coding standards. Please review those before adding any code going forward. Still on my TODO list: - remove quotes from non-reserved identifiers and make reserved identifiers use dba func for quoting - Rewrite search queries for better results (both MySQL and Postgres)
2014-11-13 20:21:58 +00:00
$r = q("UPDATE xchan SET xchan_name = '%s', xchan_name_date = '%s' WHERE xchan_hash = '%s'",
dbesc($name),
dbesc(datetime_convert()),
dbesc($channel['xchan_hash'])
);
}
if($is_default) {
// reload the info for the sidebar widget - why does this not work?
profile_load($a,$channel['channel_address']);
2015-01-29 04:56:04 +00:00
proc_run('php','include/directory.php',local_channel());
}
2010-07-01 23:48:07 +00:00
}
}
function profiles_content(&$a) {
2011-01-19 03:25:28 +00:00
2010-10-31 23:38:22 +00:00
$o = '';
2010-11-25 00:35:35 +00:00
$channel = $a->get_channel();
2015-01-29 04:56:04 +00:00
if(! local_channel()) {
notice( t('Permission denied.') . EOL);
2010-07-01 23:48:07 +00:00
return;
}
require_once('include/identity.php');
$profile_fields_basic = get_profile_fields_basic();
$profile_fields_advanced = get_profile_fields_advanced();
2015-01-29 04:56:04 +00:00
if(((argc() > 1) && (intval(argv(1)))) || !feature_enabled(local_channel(),'multi_profiles')) {
if(feature_enabled(local_channel(),'multi_profiles'))
$id = $a->argv[1];
else {
$x = q("select id from profile where uid = %d and is_default = 1",
2015-01-29 04:56:04 +00:00
intval(local_channel())
);
if($x)
$id = $x[0]['id'];
}
2010-07-01 23:48:07 +00:00
$r = q("SELECT * FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
intval($id),
2015-01-29 04:56:04 +00:00
intval(local_channel())
2010-07-01 23:48:07 +00:00
);
if(! count($r)) {
notice( t('Profile not found.') . EOL);
2010-07-01 23:48:07 +00:00
return;
}
2010-11-16 05:06:44 +00:00
require_once('include/profile_selectors.php');
2010-07-01 23:48:07 +00:00
2012-04-11 01:08:06 +00:00
$editselect = 'none';
2015-01-29 04:56:04 +00:00
// if(feature_enabled(local_channel(),'richtext'))
// $editselect = 'textareas';
2012-04-11 01:08:06 +00:00
$a->page['htmlhead'] .= replace_macros(get_markup_template('profed_head.tpl'), array(
2012-10-22 00:23:21 +00:00
'$baseurl' => $a->get_baseurl(true),
2012-04-11 01:08:06 +00:00
'$editselect' => $editselect,
));
2015-01-29 04:56:04 +00:00
$advanced = ((feature_enabled(local_channel(),'advanced_profiles')) ? true : false);
if($advanced)
$fields = $profile_fields_advanced;
else
$fields = $profile_fields_basic;
2014-12-13 15:00:00 +00:00
$opt_tpl = get_markup_template("profile_hide_friends.tpl");
$hide_friends = replace_macros($opt_tpl,array('$field' => array(
'hide_friends',
t('Hide your contact/friend list from viewers of this profile?'),
$r[0]['hide_friends'],
'',
)));
2010-07-11 09:52:47 +00:00
$q = q("select * from profdef where true");
if($q) {
$extra_fields = array();
foreach($q as $qq) {
$mine = q("select v from profext where k = '%s' and hash = '%s' and channel_id = %d limit 1",
dbesc($qq['field_name']),
dbesc($r[0]['profile_guid']),
2015-01-29 04:56:04 +00:00
intval(local_channel())
);
if(array_key_exists($qq['field_name'],$fields)) {
$extra_fields[] = array($qq['field_name'],$qq['field_desc'],(($mine) ? $mine[0]['v'] : ''), $qq['field_help']);
}
}
}
//logger('extra_fields: ' . print_r($extra_fields,true));
2012-04-11 01:08:06 +00:00
$f = get_config('system','birthday_input_format');
if(! $f)
$f = 'ymd';
2010-07-10 14:09:57 +00:00
2012-08-31 01:17:38 +00:00
$is_default = (($r[0]['is_default']) ? 1 : 0);
2011-05-11 11:37:13 +00:00
$tpl = get_markup_template("profile_edit.tpl");
2010-07-01 23:48:07 +00:00
$o .= replace_macros($tpl,array(
2012-10-22 00:23:21 +00:00
2012-03-12 20:17:37 +00:00
'$form_security_token' => get_form_security_token("profile_edit"),
2015-01-29 04:56:04 +00:00
'$profile_clone_link' => ((feature_enabled(local_channel(),'multi_profiles')) ? 'profiles/clone/' . $r[0]['id'] . '?t='
. get_form_security_token("profile_clone") : ''),
2012-10-22 00:23:21 +00:00
'$profile_drop_link' => 'profiles/drop/' . $r[0]['id'] . '?t='
. get_form_security_token("profile_drop"),
'$fields' => $fields,
2013-07-03 09:47:36 +00:00
'$guid' => $r[0]['profile_guid'],
2012-10-22 00:23:21 +00:00
'$banner' => t('Edit Profile Details'),
'$submit' => t('Submit'),
'$viewprof' => t('View this profile'),
'$editvis' => t('Edit visibility'),
'$profpic' => t('Change Profile Photo'),
2012-10-22 00:23:21 +00:00
'$cr_prof' => t('Create a new profile using these settings'),
'$cl_prof' => t('Clone this profile'),
'$del_prof' => t('Delete this profile'),
2015-01-29 04:56:04 +00:00
'$exportable' => feature_enabled(local_channel(),'profile_export'),
'$lbl_import' => t('Import profile from file'),
'$lbl_export' => t('Export profile to file'),
'$lbl_profname' => t('Profile Name:'),
'$lbl_fullname' => t('Your Full Name:'),
2012-10-22 00:23:21 +00:00
'$lbl_title' => t('Title/Description:'),
'$lbl_gender' => t('Your Gender:'),
2014-10-19 23:12:05 +00:00
'$lbl_bd' => t("Birthday :"),
2012-10-22 00:23:21 +00:00
'$lbl_address' => t('Street Address:'),
'$lbl_city' => t('Locality/City:'),
'$lbl_zip' => t('Postal/Zip Code:'),
'$lbl_country' => t('Country:'),
'$lbl_region' => t('Region/State:'),
'$lbl_marital' => t('<span class="heart">&hearts;</span> Marital Status:'),
'$lbl_with' => t("Who: \x28if applicable\x29"),
'$lbl_ex1' => t('Examples: cathy123, Cathy Williams, cathy@example.com'),
'$lbl_howlong' => t('Since [date]:'),
'$lbl_sexual' => t('Sexual Preference:'),
'$lbl_homepage' => t('Homepage URL:'),
2012-06-03 03:58:20 +00:00
'$lbl_hometown' => t('Hometown:'),
2012-10-22 00:23:21 +00:00
'$lbl_politic' => t('Political Views:'),
'$lbl_religion' => t('Religious Views:'),
'$lbl_pubkey' => t('Keywords:'),
2012-10-22 00:23:21 +00:00
'$lbl_likes' => t('Likes:'),
2012-06-25 04:16:55 +00:00
'$lbl_dislikes' => t('Dislikes:'),
2012-10-22 00:23:21 +00:00
'$lbl_ex2' => t('Example: fishing photography software'),
'$lbl_pubdsc' => t("Used in directory listings"),
2012-10-22 00:23:21 +00:00
'$lbl_about' => t('Tell us about yourself...'),
'$lbl_hobbies' => t('Hobbies/Interests'),
'$lbl_social' => t('Contact information and Social Networks'),
'$lbl_channels' => t('My other channels'),
2012-10-22 00:23:21 +00:00
'$lbl_music' => t('Musical interests'),
'$lbl_book' => t('Books, literature'),
'$lbl_tv' => t('Television'),
'$lbl_film' => t('Film/dance/culture/entertainment'),
'$lbl_love' => t('Love/romance'),
'$lbl_work' => t('Work/employment'),
'$lbl_school' => t('School/education'),
'$disabled' => (($is_default) ? 'onclick="return false;" style="color: #BBBBFF;"' : ''),
'$baseurl' => $a->get_baseurl(true),
'$profile_id' => $r[0]['id'],
2012-08-27 08:22:08 +00:00
'$profile_name' => $r[0]['profile_name'],
'$is_default' => $is_default,
'$default' => t('This is your default profile.') . EOL . translate_scope(map_scope($channel['channel_r_profile'])),
'$advanced' => $advanced,
2012-10-22 00:23:21 +00:00
'$name' => $r[0]['name'],
'$pdesc' => $r[0]['pdesc'],
'$dob' => dob($r[0]['dob']),
2010-07-11 09:52:47 +00:00
'$hide_friends' => $hide_friends,
2012-10-22 00:23:21 +00:00
'$address' => $r[0]['address'],
'$locality' => $r[0]['locality'],
'$region' => $r[0]['region'],
'$postal_code' => $r[0]['postal_code'],
2012-08-31 01:17:38 +00:00
'$country_name' => $r[0]['country_name'],
2012-10-22 00:23:21 +00:00
'$age' => ((intval($r[0]['dob'])) ? '(' . t('Age: ') . age($r[0]['dob'],$a->user['timezone'],$a->user['timezone']) . ')' : ''),
'$gender' => gender_selector($r[0]['gender']),
'$gender_min' => gender_selector_min($r[0]['gender']),
2012-10-22 00:23:21 +00:00
'$marital' => marital_selector($r[0]['marital']),
'$marital_min' => marital_selector_min($r[0]['marital']),
2014-04-21 01:48:21 +00:00
'$with' => $r[0]['with'],
'$howlong' => ($r[0]['howlong'] === NULL_DATE ? '' : datetime_convert('UTC',date_default_timezone_get(),$r[0]['howlong'])),
2012-10-22 00:23:21 +00:00
'$sexual' => sexpref_selector($r[0]['sexual']),
'$sexual_min' => sexpref_selector_min($r[0]['sexual']),
2012-10-22 00:23:21 +00:00
'$about' => $r[0]['about'],
'$homepage' => $r[0]['homepage'],
'$hometown' => $r[0]['hometown'],
'$politic' => $r[0]['politic'],
'$religion' => $r[0]['religion'],
'$keywords' => $r[0]['keywords'],
2012-10-22 00:23:21 +00:00
'$likes' => $r[0]['likes'],
'$dislikes' => $r[0]['dislikes'],
'$music' => $r[0]['music'],
'$book' => $r[0]['book'],
'$tv' => $r[0]['tv'],
'$film' => $r[0]['film'],
'$interest' => $r[0]['interest'],
'$romance' => $r[0]['romance'],
'$work' => $r[0]['work'],
'$education' => $r[0]['education'],
'$contact' => $r[0]['contact'],
'$channels' => $r[0]['channels'],
'$extra_fields' => $extra_fields,
2010-07-01 23:48:07 +00:00
));
2011-01-20 23:30:45 +00:00
$arr = array('profile' => $r[0], 'entry' => $o);
call_hooks('profile_edit', $arr);
2010-07-01 23:48:07 +00:00
return $o;
}
else {
$r = q("SELECT * FROM `profile` WHERE `uid` = %d",
2015-01-29 04:56:04 +00:00
local_channel());
2010-07-01 23:48:07 +00:00
if(count($r)) {
2011-05-11 11:37:13 +00:00
$tpl_header = get_markup_template('profile_listing_header.tpl');
2011-04-08 01:00:35 +00:00
$o .= replace_macros($tpl_header,array(
2011-11-03 22:14:33 +00:00
'$header' => t('Edit/Manage Profiles'),
2013-12-30 23:41:42 +00:00
'$addstuff' => t('Add profile things'),
'$stuff_desc' => t('Include desirable objects in your profile'),
2011-04-08 01:00:35 +00:00
'$chg_photo' => t('Change profile photo'),
2012-03-12 20:17:37 +00:00
'$cr_new' => t('Create New Profile'),
'$cr_new_link' => 'profiles/new?t=' . get_form_security_token("profile_new")
2011-04-08 01:00:35 +00:00
));
2011-05-11 11:37:13 +00:00
$tpl = get_markup_template('profile_entry.tpl');
2010-07-01 23:48:07 +00:00
foreach($r as $rr) {
2011-04-13 04:21:33 +00:00
$o .= replace_macros($tpl, array(
2013-01-22 08:20:25 +00:00
'$photo' => $rr['thumb'],
2010-07-01 23:48:07 +00:00
'$id' => $rr['id'],
2011-01-12 20:44:22 +00:00
'$alt' => t('Profile Image'),
2012-08-27 08:22:08 +00:00
'$profile_name' => $rr['profile_name'],
2012-10-22 00:23:21 +00:00
'$visible' => (($rr['is_default'])
2014-08-08 01:47:09 +00:00
? '<strong>' . translate_scope(map_scope($channel['channel_r_profile'])) . '</strong>'
2012-03-15 04:20:20 +00:00
: '<a href="' . $a->get_baseurl(true) . '/profperm/' . $rr['id'] . '" />' . t('Edit visibility') . '</a>')
2010-07-01 23:48:07 +00:00
));
}
2010-07-01 23:48:07 +00:00
}
return $o;
}
}