Commit 51c69370 authored by Nick Sellen's avatar Nick Sellen

Merge branch 'master' into extract-database-gateways

parents fef0c2b2 fa8c8830
......@@ -14,6 +14,7 @@
- Fix newsletter should only be sent to activated accounts @NerdyProjects
## Refactoring
- Consolidate remaining functions and modules !269 @NerdyProjects
- Remove old user registration code !246 @NerdyProjects
## Dev/Test/CI stuff
......
......@@ -5,7 +5,6 @@ use Foodsharing\Lib\Db\ManualDb;
require __DIR__ . '/includes/setup.php';
require_once 'config.inc.php';
require_once 'lib/func.inc.php';
require_once 'lang/DE/de.php';
......
<?php
use Foodsharing\Modules\Message\MessageModel;
class BetrieblistsControl extends ConsoleControl
{
private $model;
public function __construct()
{
error_reporting(E_ALL);
ini_set('display_errors', '1');
$this->model = new BetrieblistsModel();
}
private function queryBezirk($address)
{
$URL = 'http://open.mapquestapi.com/nominatim/v1/search?format=json&addressdetails=1&limit=1';
$valid_query_elements = array('country', 'city', 'street');
if (!in_array('country', $address)) {
$address['country'] = 'Germany';
}
foreach ($address as $k => $v) {
if (in_array($k, $valid_query_elements)) {
$URL .= "&$k=" . urlencode($v);
}
}
$res = file_get_contents($URL);
$obj = json_decode($res, true);
foreach (array('suburb', 'city_district', 'city') as $field) {
$bids = $this->model->q('SELECT b.id, b.`name` FROM fs_bezirk b LEFT JOIN fs_bezirk_closure bc ON b.id = bc.bezirk_id WHERE bc.`ancestor_id` = 741 AND `name` LIKE "%' . $this->model->safe($obj[0]['address'][$field]) . '%" ORDER BY bc.depth DESC, b.`name`');
if ($bids) {
break;
}
}
return $bids;
}
private function implode_r($a)
{
$r = array();
foreach ($a as $e) {
$r[] = implode(', ', $e);
}
return implode('; ', $r);
}
public function checkRegion()
{
$betriebe = $this->model->q('SELECT `id`, `bezirk_id`, `stadt`, `name`, `str`, `hsnr` FROM fs_betrieb WHERE `bezirk_id` IN (SELECT `bezirk_id` FROM fs_bezirk_closure WHERE `ancestor_id` = 31)');
foreach ($betriebe as $betrieb) {
$bezirke = array((array('id' => 31, 'name' => 'Hamburg')));
$address = $betrieb['str'] . ' ' . $betrieb['hsnr'];
$bezirke = $this->queryBezirk(array('city' => $betrieb['stadt'], 'street' => $address));
if (!$bezirke || $bezirke[0]['id'] != $betrieb['bezirk_id']) {
$guess = $this->implode_r($bezirke);
echo $betrieb['id'] . ', ' . $betrieb['name'] . ', ' . $address . ', ' . $betrieb['stadt'] . ', ' . $betrieb['bezirk_id'] . ', "' . $guess . "\"\n";
}
sleep(1);
}
}
public function updateconversations()
{
$betriebe = $this->model->q('SELECT id, `name`, team_conversation_id, springer_conversation_id FROM fs_betrieb');
$msg = new MessageModel();
foreach ($betriebe as $betrieb) {
$cid = $betrieb['team_conversation_id'];
if ((int)($betrieb['team_conversation_id']) == 0) {
$cid = $this->model->insert('INSERT INTO fs_conversation (`name`, `locked`) VALUES(' . $this->model->strval('Team ' . $betrieb['name']) . ', 1)');
if ($cid > 0) {
$this->model->update('UPDATE fs_betrieb SET team_conversation_id = ' . $cid . ' WHERE id = ' . $betrieb['id']);
}
}
$sid = $betrieb['springer_conversation_id'];
if ((int)($betrieb['springer_conversation_id']) == 0) {
$sid = $this->model->insert('INSERT INTO fs_conversation (`name`, `locked`) VALUES(' . $this->model->strval('Springer ' . $betrieb['name']) . ', 1)');
if ($sid > 0) {
$this->model->update('UPDATE fs_betrieb SET springer_conversation_id = ' . $sid . ' WHERE id = ' . $betrieb['id']);
}
}
info('Updating ' . $betrieb['name'] . " (C: $cid, S: $sid)");
$team = $this->model->getBetriebTeam($betrieb['id']);
$springer = $this->model->getBetriebSpringer($betrieb['id']);
$teamIds = array();
if ($team) {
foreach ($team as $user) {
$teamIds[] = $user['id'];
}
}
$springerIds = array();
if ($springer) {
foreach ($springer as $user) {
$springerIds[] = $user['id'];
}
}
$msg->setConversationMembers($cid, $teamIds);
$msg->setConversationMembers($sid, $springerIds);
}
}
}
<?php
class BetrieblistsModel extends ConsoleModel
{
public function __construct()
{
parent::__construct();
}
}
<?php
use Foodsharing\Lib\Db\ManualDb;
use Foodsharing\Lib\Mail\AsyncMail;
class ConsoleControl
{
public function __construct()
{
}
public function progressbar($count)
{
return new \ProgressBar\Manager(0, $count);
}
public function calcDuration($start_ts, $current_item, $total_count)
{
$duration = (time() - $start_ts);
$duration_one = ($duration / $current_item);
$time_left = $duration_one * ($total_count - $current_item);
return 'duration: ' . $this->secs_to_h($duration) . ' time left: ' . $this->secs_to_h($time_left);
}
public function secs_to_h($secs)
{
$units = array(
'week' => 7 * 24 * 3600,
'day' => 24 * 3600,
'hour' => 3600,
'minute' => 60,
'second' => 1,
);
// specifically handle zero
if ($secs == 0) {
return '0 seconds';
}
$s = '';
foreach ($units as $name => $divisor) {
if ($quot = intval($secs / $divisor)) {
$s .= "$quot $name";
$s .= (abs($quot) > 1 ? 's' : '') . ', ';
$secs -= $quot * $divisor;
}
}
return substr($s, 0, -2);
}
public function tplMail($tpl_id, $to, $var = array())
{
global $db;
if (!is_object($db)) {
$db = new ManualDb();
}
if ($message = $db->getOne_message_tpl($tpl_id)) {
$search = array();
$replace = array();
foreach ($var as $key => $v) {
$search[] = '{' . strtoupper($key) . '}';
$replace[] = $v;
}
$message['body'] = str_replace($search, $replace, $message['body']);
$message['subject'] = str_replace($search, $replace, $message['subject']);
$email = new AsyncMail();
$email->setFrom(DEFAULT_EMAIL, DEFAULT_EMAIL_NAME);
$email->addRecipient($to);
$email->setSubject($message['subject']);
$email->setHTMLBody(emailBodyTpl($message['body']));
$email->setBody($message['body']);
$email->send();
}
}
}
function validEmail($email)
{
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
return true;
} else {
return false;
}
}
function s($id)
{
global $g_lang;
if (isset($g_lang[$id])) {
return $g_lang[$id];
} else {
return $id;
}
}
function rolleWrapInt($roleInt)
{
$roles = array(
0 => 'user',
1 => 'fs',
2 => 'bieb',
3 => 'bot',
4 => 'orga',
5 => 'admin'
);
return $roles[$roleInt];
}
function rolleWrap($roleStr)
{
$roles = array(
'user' => 0,
'fs' => 1,
'bieb' => 2,
'bot' => 3,
'orga' => 4,
'admin' => 5
);
return $roles[$roleStr];
}
function loadApp($app)
{
$app = strtolower($app);
if (file_exists(ROOT_DIR . 'app/console/' . $app . '/' . $app . '.control.php') && file_exists(ROOT_DIR . 'app/console/' . $app . '/' . $app . '.model.php')) {
require_once ROOT_DIR . 'app/console/' . $app . '/' . $app . '.control.php';
require_once ROOT_DIR . 'app/console/' . $app . '/' . $app . '.model.php';
$mod = ucfirst($app) . 'Control';
return new $mod();
}
return false;
}
function error($msg)
{
if (QUIET) {
return false;
}
echo "\033[31m" . cliTime() . " [ERROR]\t" . $msg . " \033[0m\n";
}
function info($msg)
{
if (QUIET) {
return false;
}
//echo "\033[37m[INFO]\t" . $msg." \033[0m\n";
echo '' . cliTime() . " [INFO]\t" . $msg . "\n";
}
function success($msg)
{
if (QUIET) {
return false;
}
echo "\033[32m" . cliTime() . " [INFO]\t" . $msg . " \033[0m\n";
}
function cliTime()
{
return date('Y-m-d H:i:s');
}
function qs($str)
{
return $str;
}
function autolink($str, $attributes = array())
{
$attributes['target'] = '_blank';
$attrs = '';
foreach ($attributes as $attribute => $value) {
$attrs .= " {$attribute}=\"{$value}\"";
}
$str = ' ' . $str;
$str = preg_replace(
'`([^"=\'>])(((http|https|ftp)://|www.)[^\s<]+[^\s<\.)])`i',
'$1<a href="$2"' . $attrs . '>$2</a>',
$str
);
$str = substr($str, 1);
$str = preg_replace('`href=\"www`', 'href="http://www', $str);
// fügt http:// hinzu, wenn nicht vorhanden
return $str;
}
function emailBodyTpl($message, $email = false, $token = false)
{
$unsubscribe = '
<tr>
<td height="20" valign="top" style="background-color:#FAF7E5">
<div style="text-align:center;padding-top:10px;font-size:11px;font-family:Arial;padding:15px;color:#594129;">
Willst Du diese Art von Benachrichtigungen nicht mehr bekommen? Du kannst unter <a style="color:#F36933" href="' . BASE_URL . '/?page=settings&sub=info" target="_blank">Benachrichtigungen</a> einstellen, welche Mails Du erhältst.
</div>
</td>
</tr>';
if ($email !== false && $token !== false) {
$unsubscribe = '
<tr>
<td height="20" valign="top" style="background-color:#FAF7E5">
<div style="text-align:center;padding-top:10px;font-size:11px;font-family:Arial;padding:15px;color:#594129;">
Möchtest Du keinen Newsletter mehr erhalten? <a style="color:#F36933" href="http://www.lebensmittelretten.de/unsubscribe/' . $token . '-' . $email . '" target="_blank">Klicke hier zum Abbestellen!</a> Du kannst unter <a style="color:#F36933" href="http://www.lebensmittelretten.de/freiwillige/?page=settings&sub=info" target="_blank">Benachrichtigungen</a> einstellen, welche Mails Du erhältst.
</div>
</td>
</tr>';
}
$message = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $message);
$search = array('<a', '<td', '<li');
$replace = array('<a style="color:#F36933"', '<td style="font-size:13px;font-family:Arial;color:#31210C;"', '<li style="margin-bottom:11px"');
return '<html><head><style type="text/css">a{text-decoration:none;}a:hover{text-decoration:underline;}a.button{display:inline-block;padding:6px 16px;border:1px solid #FFFFFF;background-color:#4A3520;color:#FFFFFF !important;font-weight:bold;border-radius:8px;}a.button:hover{border:1px solid #4A3520;background-color:#ffffff;color:#4A3520 !important;text-decoration:none !important;}.border{padding:10px;border-top:1px solid #4A3520;border-bottom:1px solid #4A3520;background-color:#FFFFFF;}</style></head>
<body style="margin:0;padding:0;">
<div style="background-color:#F1E7C9;border:1px solid #628043;border-top:0px;padding:2%;padding-top:0;margin-top:0px;">
<table width="100%" style="margin-bottom:10px;margin-top:-2px;">
<tr>
<td valign="top" height="30" style="background-color:#4A3520">
<div style="padding:5px;font-size:13px;font-family:Arial;color:#FAF7E5;overflow:hidden;" align="left">
<a style="display:block;color:#FAF7E5;text-decoration:none;" href="http://www.lebensmittelretten.de/" target="_blank">
<span style="margin-left:10px;font-size:20px;font-family:Arial Black, Arial;font-weight:bold;color:#FAF7E5;letter-spacing:-1px;">food</span><span style="margin-right:10px;font-size:20px;font-family:Arial Black, Arial;font-weight:bold;color:#4D971E;letter-spacing:-1px">sharing</span> <span style="font-style:italic">Lebensmittelretten<span style="color:#F36933">.</span>de</span>
</a>
</div>
</td></tr>
</table>
<table height="100%" width="100%">
<tr>
<td valign="top" style="background-color:#FAF7E5">
<div style="padding:5px;font-size:13px;font-family:Arial;padding:15px;color:#31210C;">
' . str_replace($search, $replace, $message) . '
</div>
</td>
</tr>
' . $unsubscribe . '
</table>
</div>
</body>
</html>';
}
This diff is collapsed.
<?php
class MigrateModel extends ConsoleModel
{
public function __construct()
{
parent::__construct();
global $g_dbclean;
$g_dbclean['mysqli']->set_charset('utf8');
}
public function getOldConvDates($recip1, $recip2)
{
return $this->qRow('
SELECT
MIN( time ) AS min,
MAX( time ) AS max
FROM
fs_message
WHERE (
recip_id = ' . $recip1 . '
AND sender_id = ' . $recip2 . '
)
OR (
recip_id = ' . $recip2 . '
AND sender_id = ' . $recip1 . '
)
');
}
public function addMsg($conversation_id, $user_id, $body, $time)
{
return $this->insert('INSERT INTO `fs_msg`(`conversation_id`, `foodsaver_id`, `body`, `time`) VALUES (' . (int)$conversation_id . ',' . (int)$user_id . ',' . $this->strval($body) . ',' . $this->dateval($time) . ')');
}
public function connectUser($conversation_id, $recip1, $recip2, $unread)
{
return $this->insert('
REPLACE INTO `fs_foodsaver_has_conversation`(`foodsaver_id`, `conversation_id`, `unread`)
VALUES
(' . (int)$recip1 . ',' . (int)$conversation_id . ',' . (int)$unread . '),
(' . (int)$recip2 . ',' . (int)$conversation_id . ',' . (int)$unread . ')');
}
public function listOldMessages($recip1, $recip2)
{
return $this->q('
SELECT
id,
msg,
sender_id,
`time`,
unread
FROM
`fs_message`
WHERE (
recip_id = ' . $recip1 . '
AND sender_id = ' . $recip2 . '
)
OR (
recip_id = ' . $recip2 . '
AND sender_id = ' . $recip1 . '
)
ORDER BY id
');
}
public function listOldConversations()
{
if ($convs = $this->q('
SELECT
DISTINCT CONCAT( `sender_id` ,":", `recip_id` ),
sender_id,
recip_id
FROM
fs_message
WHERE
sender_id != 0
AND
recip_id != 0
AND
recip_id != sender_id
ORDER BY
sender_id, recip_id
')
) {
$out = array();
foreach ($convs as $c) {
$recip = array(
$c['sender_id'] => $c['sender_id'],
$c['recip_id'] => $c['recip_id']
);
ksort($recip);
$out[implode(':', $recip)] = $recip;
}
return $out;
}
}
public function getConversationId($recip1, $recip2)
{
$recips = array(
$recip1 => $recip1,
$recip2 => $recip2
);
/*
* make sure the order of this array
*/
ksort($recips);
$conversation_id = false;
/*
* First we want to check is there allready an conversation with exacly those user_ids stored in $recips array
*/
if ($conv = $this->qRow('
SELECT
conversation_id,
GROUP_CONCAT(foodsaver_id ORDER BY foodsaver_id SEPARATOR ":") AS idstring
FROM
fs_foodsaver_has_conversation
GROUP BY
conversation_id
HAVING
idstring = "' . implode(':', $recips) . '"')
) {
$conversation_id = $conv['conversation_id'];
}
/*
* If we dont have an existing conversation create a new one
*/
if (!$conversation_id) {
$conversation_id = $this->insertConversation($recips);
}
$mid = 0;
$member = $this->listConversationMembers($conversation_id);
/*
* UPDATE conversation
*/
$this->update('
UPDATE
`' . PREFIX . 'conversation`
SET
`member` = ' . $this->strval(serialize($member)) . '
WHERE
`id` = ' . (int)$conversation_id . '
');
return $conversation_id;
}
public function updateConversation($conversation_id, $start, $last, $last_foodsaver_id, $last_message, $last_message_id)
{
$this->update('
UPDATE
`' . PREFIX . 'conversation`
SET
`start` = ' . $this->dateval($start) . ',
`last`= ' . $this->dateval($last) . ',
`last_message` = ' . $this->strval($last_message) . ',
`last_message_id` = ' . (int)$last_message_id . '
WHERE
`id` = ' . (int)$conversation_id . '
');
}
public function listConversationMembers($conversation_id)
{
return $this->q('
SELECT
fs.id,
fs.name,
fs.photo,
fs.email,
fs.geschlecht
FROM
`' . PREFIX . 'foodsaver_has_conversation` hc,
`' . PREFIX . 'foodsaver` fs
WHERE
hc.foodsaver_id = fs.id
AND
hc.conversation_id = ' . (int)$conversation_id . '
');
}
public function insertConversation($recipients)
{
/*
* first get one new conversation
*/
$sql = 'INSERT INTO `' . PREFIX . 'conversation`
(
`start`,
`last`,
`last_foodsaver_id`,
`start_foodsaver_id`