Improved SQL scripts

This commit is contained in:
Michael Vogel 2014-03-16 19:13:57 +01:00
parent 0568a5d8bb
commit c4578aafa0
6 changed files with 44 additions and 43 deletions

View File

@ -75,8 +75,8 @@ function communityhome_home(&$a, &$o){
// this query don't work on some mysql versions // this query don't work on some mysql versions
if (get_config('communityhome','showactiveusers')===true){ if (get_config('communityhome','showactiveusers')===true){
$r = q("SELECT `uni`.`contacts`,`uni`.`items`, `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname` FROM $r = q("SELECT `uni`.`contacts`,`uni`.`items`, `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname` FROM
(SELECT COUNT(`id`) as `contacts`, `uid` FROM `contact` WHERE `self`=0 GROUP BY `uid`) AS `con`, (SELECT COUNT(*) as `contacts`, `uid` FROM `contact` WHERE `self`=0 GROUP BY `uid`) AS `con`,
(SELECT COUNT(`id`) as `items`, `uid` FROM `item` WHERE `item`.`changed` > DATE(NOW() - INTERVAL 1 MONTH) AND `item`.`wall` = 1 GROUP BY `uid`) AS `ite`, (SELECT COUNT(*) as `items`, `uid` FROM `item` WHERE `item`.`changed` > DATE(NOW() - INTERVAL 1 MONTH) AND `item`.`wall` = 1 GROUP BY `uid`) AS `ite`,
( (
SELECT `contacts`,`items`,`ite`.`uid` FROM `con` RIGHT OUTER JOIN `ite` ON `con`.`uid`=`ite`.`uid` SELECT `contacts`,`items`,`ite`.`uid` FROM `con` RIGHT OUTER JOIN `ite` ON `con`.`uid`=`ite`.`uid`
UNION ALL UNION ALL

View File

@ -7,40 +7,40 @@
class Diasphp { class Diasphp {
function __construct($pod) { function __construct($pod) {
$this->token_regex = '/content="(.*?)" name="csrf-token/'; $this->token_regex = '/content="(.*?)" name="csrf-token/';
$this->pod = $pod; $this->pod = $pod;
$this->cookiejar = tempnam(sys_get_temp_dir(), 'cookies'); $this->cookiejar = tempnam(sys_get_temp_dir(), 'cookies');
} }
function _fetch_token() { function _fetch_token() {
$ch = curl_init(); $ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $this->pod . "/stream"); curl_setopt ($ch, CURLOPT_URL, $this->pod . "/stream");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookiejar); curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookiejar);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookiejar); curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookiejar);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch); $output = curl_exec ($ch);
curl_close($ch); curl_close($ch);
// Token holen und zurückgeben // Token holen und zurückgeben
preg_match($this->token_regex, $output, $matches); preg_match($this->token_regex, $output, $matches);
return $matches[1]; return $matches[1];
} }
function login($username, $password) { function login($username, $password) {
$datatopost = array( $datatopost = array(
'user[username]' => $username, 'user[username]' => $username,
'user[password]' => $password, 'user[password]' => $password,
'authenticity_token' => $this->_fetch_token() 'authenticity_token' => $this->_fetch_token()
); );
$poststr = http_build_query($datatopost); $poststr = http_build_query($datatopost);
// Adresse per cURL abrufen // Adresse per cURL abrufen
$ch = curl_init(); $ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $this->pod . "/users/sign_in"); curl_setopt ($ch, CURLOPT_URL, $this->pod . "/users/sign_in");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookiejar); curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookiejar);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookiejar); curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookiejar);
@ -48,11 +48,11 @@ class Diasphp {
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_POST, true); curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $poststr); curl_setopt ($ch, CURLOPT_POSTFIELDS, $poststr);
curl_exec ($ch); curl_exec ($ch);
$info = curl_getinfo($ch); $info = curl_getinfo($ch);
curl_close($ch); curl_close($ch);
if($info['http_code'] != 302) { if($info['http_code'] != 302) {
throw new Exception('Login error '.print_r($info, true)); throw new Exception('Login error '.print_r($info, true));
} }
@ -60,24 +60,24 @@ class Diasphp {
// Das Objekt zurückgeben, damit man Aurufe verketten kann. // Das Objekt zurückgeben, damit man Aurufe verketten kann.
return $this; return $this;
} }
function post($text) { function post($text) {
// post-daten vorbereiten // post-daten vorbereiten
$datatopost = json_encode(array( $datatopost = json_encode(array(
'aspect_ids' => 'public', 'aspect_ids' => 'public',
'status_message' => array('text' => $text) 'status_message' => array('text' => $text)
)); ));
// header vorbereiten // header vorbereiten
$headers = array( $headers = array(
'Content-Type: application/json', 'Content-Type: application/json',
'accept: application/json', 'accept: application/json',
'x-csrf-token: '.$this->_fetch_token() 'x-csrf-token: '.$this->_fetch_token()
); );
// Adresse per cURL abrufen // Adresse per cURL abrufen
$ch = curl_init(); $ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $this->pod . "/status_messages"); curl_setopt ($ch, CURLOPT_URL, $this->pod . "/status_messages");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookiejar); curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookiejar);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookiejar); curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookiejar);
@ -86,15 +86,15 @@ class Diasphp {
curl_setopt ($ch, CURLOPT_POST, true); curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost); curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
curl_exec ($ch); curl_exec ($ch);
$info = curl_getinfo($ch); $info = curl_getinfo($ch);
curl_close($ch); curl_close($ch);
if($info['http_code'] != 201) { if($info['http_code'] != 201) {
throw new Exception('Post error '.print_r($info, true)); throw new Exception('Post error '.print_r($info, true));
} }
// Ende der möglichen Kette, gib mal "true" zurück. // Ende der möglichen Kette, gib mal "true" zurück.
return true; return true;
} }

View File

@ -289,7 +289,7 @@ function fb_get_friends_sync_parsecontact($uid, $contact) {
`name-date` = '%s', `name-date` = '%s',
`uri-date` = '%s', `uri-date` = '%s',
`avatar-date` = '%s' `avatar-date` = '%s'
WHERE `id` = %d LIMIT 1 WHERE `id` = %d
", ",
dbesc($photos[0]), dbesc($photos[0]),
dbesc($photos[1]), dbesc($photos[1]),
@ -356,7 +356,7 @@ function fb_get_friends_sync_parsecontact($uid, $contact) {
`name-date` = '%s', `name-date` = '%s',
`uri-date` = '%s', `uri-date` = '%s',
`avatar-date` = '%s' `avatar-date` = '%s'
WHERE `id` = %d LIMIT 1 WHERE `id` = %d
", ",
dbesc($photos[0]), dbesc($photos[0]),
dbesc($photos[1]), dbesc($photos[1]),
@ -742,19 +742,19 @@ function facebook_plugin_admin(&$a, &$o){
$o = '<input type="hidden" name="form_security_token" value="' . get_form_security_token("fbsave") . '">'; $o = '<input type="hidden" name="form_security_token" value="' . get_form_security_token("fbsave") . '">';
$o .= '<h4>' . t('Facebook API Key') . '</h4>'; $o .= '<h4>' . t('Facebook API Key') . '</h4>';
$appid = get_config('facebook', 'appid' ); $appid = get_config('facebook', 'appid' );
$appsecret = get_config('facebook', 'appsecret' ); $appsecret = get_config('facebook', 'appsecret' );
$poll_interval = get_config('facebook', 'poll_interval' ); $poll_interval = get_config('facebook', 'poll_interval' );
$sync_comments = get_config('facebook', 'sync_comments' ); $sync_comments = get_config('facebook', 'sync_comments' );
if (!$poll_interval) $poll_interval = FACEBOOK_DEFAULT_POLL_INTERVAL; if (!$poll_interval) $poll_interval = FACEBOOK_DEFAULT_POLL_INTERVAL;
$ret1 = q("SELECT `v` FROM `config` WHERE `cat` = 'facebook' AND `k` = 'appid' LIMIT 1"); $ret1 = q("SELECT `v` FROM `config` WHERE `cat` = 'facebook' AND `k` = 'appid' LIMIT 1");
$ret2 = q("SELECT `v` FROM `config` WHERE `cat` = 'facebook' AND `k` = 'appsecret' LIMIT 1"); $ret2 = q("SELECT `v` FROM `config` WHERE `cat` = 'facebook' AND `k` = 'appsecret' LIMIT 1");
if ((count($ret1) > 0 && $ret1[0]['v'] != $appid) || (count($ret2) > 0 && $ret2[0]['v'] != $appsecret)) $o .= t('Error: it appears that you have specified the App-ID and -Secret in your .htconfig.php file. As long as they are specified there, they cannot be set using this form.<br><br>'); if ((count($ret1) > 0 && $ret1[0]['v'] != $appid) || (count($ret2) > 0 && $ret2[0]['v'] != $appsecret)) $o .= t('Error: it appears that you have specified the App-ID and -Secret in your .htconfig.php file. As long as they are specified there, they cannot be set using this form.<br><br>');
$working_connection = false; $working_connection = false;
if ($appid && $appsecret) { if ($appid && $appsecret) {
$subs = facebook_subscriptions_get(); $subs = facebook_subscriptions_get();
@ -764,7 +764,7 @@ function facebook_plugin_admin(&$a, &$o){
$working_connection = true; $working_connection = true;
} else $o .= t('The correctness of the API Key could not be detected. Something strange\'s going on.') . '<br>'; } else $o .= t('The correctness of the API Key could not be detected. Something strange\'s going on.') . '<br>';
} }
$o .= '<label for="fb_appid">' . t('App-ID / API-Key') . '</label><input id="fb_appid" name="appid" type="text" value="' . escape_tags($appid ? $appid : "") . '"><br style="clear: both;">'; $o .= '<label for="fb_appid">' . t('App-ID / API-Key') . '</label><input id="fb_appid" name="appid" type="text" value="' . escape_tags($appid ? $appid : "") . '"><br style="clear: both;">';
$o .= '<label for="fb_appsecret">' . t('Application secret') . '</label><input id="fb_appsecret" name="appsecret" type="text" value="' . escape_tags($appsecret ? $appsecret : "") . '"><br style="clear: both;">'; $o .= '<label for="fb_appsecret">' . t('Application secret') . '</label><input id="fb_appsecret" name="appsecret" type="text" value="' . escape_tags($appsecret ? $appsecret : "") . '"><br style="clear: both;">';
$o .= '<label for="fb_poll_interval">' . sprintf(t('Polling Interval in minutes (minimum %1$s minutes)'), FACEBOOK_MIN_POLL_INTERVAL) . '</label><input name="poll_interval" id="fb_poll_interval" type="number" min="' . FACEBOOK_MIN_POLL_INTERVAL . '" value="' . $poll_interval . '"><br style="clear: both;">'; $o .= '<label for="fb_poll_interval">' . sprintf(t('Polling Interval in minutes (minimum %1$s minutes)'), FACEBOOK_MIN_POLL_INTERVAL) . '</label><input name="poll_interval" id="fb_poll_interval" type="number" min="' . FACEBOOK_MIN_POLL_INTERVAL . '" value="' . $poll_interval . '"><br style="clear: both;">';
@ -1171,7 +1171,7 @@ function facebook_post_hook(&$a,&$b) {
$retj = json_decode($x); $retj = json_decode($x);
if($retj->id) { if($retj->id) {
q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1", q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d",
dbesc('fb::' . $retj->id), dbesc('fb::' . $retj->id),
intval($b['id']) intval($b['id'])
); );
@ -1183,14 +1183,14 @@ function facebook_post_hook(&$a,&$b) {
add_to_queue($a->contact,NETWORK_FACEBOOK,$s); add_to_queue($a->contact,NETWORK_FACEBOOK,$s);
notice( t('Facebook post failed. Queued for retry.') . EOL); notice( t('Facebook post failed. Queued for retry.') . EOL);
} }
if (isset($retj->error) && $retj->error->type == "OAuthException" && $retj->error->code == 190) { if (isset($retj->error) && $retj->error->type == "OAuthException" && $retj->error->code == 190) {
logger('Facebook session has expired due to changed password.', LOGGER_DEBUG); logger('Facebook session has expired due to changed password.', LOGGER_DEBUG);
$last_notification = get_pconfig($b['uid'], 'facebook', 'session_expired_mailsent'); $last_notification = get_pconfig($b['uid'], 'facebook', 'session_expired_mailsent');
if (!$last_notification || $last_notification < (time() - FACEBOOK_SESSION_ERR_NOTIFICATION_INTERVAL)) { if (!$last_notification || $last_notification < (time() - FACEBOOK_SESSION_ERR_NOTIFICATION_INTERVAL)) {
require_once('include/enotify.php'); require_once('include/enotify.php');
$r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($b['uid']) ); $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($b['uid']) );
notification(array( notification(array(
'uid' => $b['uid'], 'uid' => $b['uid'],
@ -1203,7 +1203,7 @@ function facebook_post_hook(&$a,&$b) {
'source_link' => $a->config["system"]["url"], 'source_link' => $a->config["system"]["url"],
'source_photo' => $a->config["system"]["url"] . '/images/person-80.jpg', 'source_photo' => $a->config["system"]["url"] . '/images/person-80.jpg',
)); ));
set_pconfig($b['uid'], 'facebook', 'session_expired_mailsent', time()); set_pconfig($b['uid'], 'facebook', 'session_expired_mailsent', time());
} else logger('Facebook: No notification, as the last one was sent on ' . $last_notification, LOGGER_DEBUG); } else logger('Facebook: No notification, as the last one was sent on ' . $last_notification, LOGGER_DEBUG);
} }
@ -1307,11 +1307,11 @@ function fb_queue_hook(&$a,&$b) {
$retj = json_decode($j); $retj = json_decode($j);
if($retj->id) { if($retj->id) {
q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1", q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d",
dbesc('fb::' . $retj->id), dbesc('fb::' . $retj->id),
intval($item) intval($item)
); );
logger('facebook_queue: success: ' . $j); logger('facebook_queue: success: ' . $j);
remove_queue_item($x['id']); remove_queue_item($x['id']);
} }
else { else {

View File

@ -36,7 +36,7 @@ function public_server_register_account($a,$b) {
if(! $days) if(! $days)
return; return;
$r = q("UPDATE user set account_expires_on = '%s', expire = %d where uid = %d limit 1", $r = q("UPDATE user set account_expires_on = '%s', expire = %d where uid = %d",
dbesc(datetime_convert('UTC','UTC','now +' . $days . ' days')), dbesc(datetime_convert('UTC','UTC','now +' . $days . ' days')),
intval($days_posts), intval($days_posts),
intval($uid) intval($uid)
@ -66,7 +66,7 @@ function public_server_cron($a,$b) {
'source_photo' => $a->get_baseurl() . '/images/person-80.jpg', 'source_photo' => $a->get_baseurl() . '/images/person-80.jpg',
)); ));
q("update user set expire_notification_sent = '%s' where uid = %d limit 1", q("update user set expire_notification_sent = '%s' where uid = %d",
dbesc(datetime_convert()), dbesc(datetime_convert()),
intval($rr['uid']) intval($rr['uid'])
); );
@ -85,7 +85,7 @@ function public_server_cron($a,$b) {
$r = q("select uid from user where account_expired = 0 and login_date = '0000-00-00 00:00:00' and register_date < UTC_TIMESTAMP() - INTERVAL %d DAY and account_expires_on = '0000-00-00 00:00:00'",intval($nologin)); $r = q("select uid from user where account_expired = 0 and login_date = '0000-00-00 00:00:00' and register_date < UTC_TIMESTAMP() - INTERVAL %d DAY and account_expires_on = '0000-00-00 00:00:00'",intval($nologin));
if(count($r)) { if(count($r)) {
foreach($r as $rr) foreach($r as $rr)
q("update user set account_expires_on = '%s' where uid = %d limit 1", q("update user set account_expires_on = '%s' where uid = %d",
dbesc(datetime_convert('UTC','UTC','now +' . '6 days')), dbesc(datetime_convert('UTC','UTC','now +' . '6 days')),
intval($rr['uid']) intval($rr['uid'])
); );
@ -98,7 +98,7 @@ function public_server_cron($a,$b) {
$r = q("select uid from user where account_expired = 0 and login_date < UTC_TIMESTAMP() - INTERVAL %d DAY and account_expires_on = '0000-00-00 00:00:00' and `page-flags` = 0",intval($flagusers)); $r = q("select uid from user where account_expired = 0 and login_date < UTC_TIMESTAMP() - INTERVAL %d DAY and account_expires_on = '0000-00-00 00:00:00' and `page-flags` = 0",intval($flagusers));
if(count($r)) { if(count($r)) {
foreach($r as $rr) foreach($r as $rr)
q("update user set account_expires_on = '%s' where uid = %d limit 1", q("update user set account_expires_on = '%s' where uid = %d",
dbesc(datetime_convert('UTC','UTC','now +' . '6 days')), dbesc(datetime_convert('UTC','UTC','now +' . '6 days')),
intval($rr['uid']) intval($rr['uid'])
); );
@ -111,7 +111,7 @@ function public_server_cron($a,$b) {
$r = q("select uid from user where account_expired = 0 and login_date < UTC_TIMESTAMP() - INTERVAL %d DAY and account_expires_on = '0000-00-00 00:00:00' and expire = 0 and `page-flags` = 0",intval($flagposts)); $r = q("select uid from user where account_expired = 0 and login_date < UTC_TIMESTAMP() - INTERVAL %d DAY and account_expires_on = '0000-00-00 00:00:00' and expire = 0 and `page-flags` = 0",intval($flagposts));
if(count($r)) { if(count($r)) {
foreach($r as $rr) foreach($r as $rr)
q("update user set expire = %d where uid = %d limit 1", q("update user set expire = %d where uid = %d",
intval($flagpostsexpire), intval($flagpostsexpire),
intval($rr['uid']) intval($rr['uid'])
); );
@ -136,7 +136,7 @@ function public_server_login($a,$b) {
$days = get_config('public_server','expiredays'); $days = get_config('public_server','expiredays');
if(! $days) if(! $days)
return; return;
$r = q("UPDATE user set account_expires_on = '%s' where uid = %d and account_expires_on > '0000-00-00 00:00:00' limit 1", $r = q("UPDATE user set account_expires_on = '%s' where uid = %d and account_expires_on > '0000-00-00 00:00:00'",
dbesc(datetime_convert('UTC','UTC','now +' . $days . ' days')), dbesc(datetime_convert('UTC','UTC','now +' . $days . ' days')),
local_user() local_user()
); );

View File

@ -1022,6 +1022,7 @@ function pumpio_dodelete(&$a, $uid, $self, $post, $own_id) {
function pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id, $threadcompletion = false) { function pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id, $threadcompletion = false) {
require_once('include/items.php'); require_once('include/items.php');
require_once('include/html2bbcode.php');
if (($post->verb == "like") OR ($post->verb == "favorite")) if (($post->verb == "like") OR ($post->verb == "favorite"))
return pumpio_dolike($a, $uid, $self, $post, $own_id); return pumpio_dolike($a, $uid, $self, $post, $own_id);

View File

@ -41,13 +41,13 @@ function testdrive_register_account($a,$b) {
if(! $days) if(! $days)
return; return;
$r = q("UPDATE user set account_expires_on = '%s' where uid = %d limit 1", $r = q("UPDATE user set account_expires_on = '%s' where uid = %d",
dbesc(datetime_convert('UTC','UTC','now +' . $days . ' days')), dbesc(datetime_convert('UTC','UTC','now +' . $days . ' days')),
intval($uid) intval($uid)
); );
}; };
function testdrive_cron($a,$b) { function testdrive_cron($a,$b) {
require_once('include/enotify.php'); require_once('include/enotify.php');
@ -69,11 +69,11 @@ function testdrive_cron($a,$b) {
'source_photo' => $a->get_baseurl() . '/images/person-80.jpg', 'source_photo' => $a->get_baseurl() . '/images/person-80.jpg',
)); ));
q("update user set expire_notification_sent = '%s' where uid = %d limit 1", q("update user set expire_notification_sent = '%s' where uid = %d",
dbesc(datetime_convert()), dbesc(datetime_convert()),
intval($rr['uid']) intval($rr['uid'])
); );
} }
} }