Example #1
0
//R::freeze(true);
// ToDo: use this for compression - http://pieroxy.net/blog/pages/lz-string/index.html
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App();
$auth = new \Middleware\Authenticate();
global $app_config;
$app_config = parse_ini_file('config.ini', true);
$app->get('/version', function ($request, $response, $args) {
    $id = array('service' => 'Going Dutch API', 'version' => '0.1', 'uid' => \Middleware\Authenticate::$requestUid);
    $response->write(json_encode($id));
    return $response;
})->add($auth);
$app->get('/groups', function ($request, $response, $args) {
    $member = new \Models\Member();
    $response->write($member->getGroupsBalance(\Middleware\Authenticate::$requestUid));
    $newResponse = $response->withHeader('Content-type', 'application/json');
    return $newResponse;
})->add($auth);
$app->put('/groups', function ($request, $response, $args) {
    $member = new \Models\Group();
    $response->write($member->updateGroupDetails($request->getParsedBody(), \Middleware\Authenticate::$requestUid));
    $newResponse = $response->withHeader('Content-type', 'application/json');
    return $newResponse;
})->add($auth);
$app->get('/users', function ($request, $response, $args) {
    $member = new \Models\Member();
    $response->write($member->getDetailsMembersInGroups(\Middleware\Authenticate::$requestUid));
    $newResponse = $response->withHeader('Content-type', 'application/json');
    return $newResponse;
})->add($auth);
Example #2
0
 private function addExpenseEmail($expense, $eid, $type = 'add', $removedUids = array())
 {
     // error_log("START WITH EID " . $eid);
     $uids = explode(',', $expense['uids']);
     $uidValues = array_values($uids);
     $uid = array_pop($uidValues);
     $member = new \Models\Member();
     $groupsInfo = $member->getGroupsBalance($uid, false);
     // keep track of users that do not want email
     // error_log(print_r($groupsInfo ,1));
     $noMailUsers = array();
     foreach ($groupsInfo[$expense['gid']]['members'] as $member) {
         if ($member['send_mail'] == 0) {
             $noMailUsers[] = $member['uid'];
         }
     }
     $uidDetails = $this->getUserDetails(implode(',', array_keys($groupsInfo[$expense['gid']]['members'])));
     // error_log(print_r($groupsInfo, 1));
     $groupName = $groupsInfo[$expense['gid']]['name'];
     $created = date('l jS \\of F Y', $expense['ecreated']);
     $from = '*****@*****.**';
     // PHP Fatal error:  Class 'Models\NumberFormatter' not found
     // You just need to enable this extension in php.ini by uncommenting this line:
     // extension=ext/php_intl.dll
     $formatter = new \NumberFormatter('nl_NL', \NumberFormatter::CURRENCY);
     $amount = $formatter->formatCurrency($expense['amount'], $groupsInfo[$expense['gid']]['currency']);
     $amountpp = $formatter->formatCurrency($expense['amount'] / count($uids), $groupsInfo[$expense['gid']]['currency']);
     switch ($type) {
         case 'update':
             $subject = "Going Dutch expense updated in group \"{$groupName}\"";
             $messageTemplate = "The expense made on {date} by {eowner} with {amount} and description \"{description}\" has been updated.<br /><br />\n{removed}";
             $messageTemplateEnd = "The costs per person are {amountpp} making your current balance {yourbalance} which comes to position {yourposition} in the group.\n";
             // error_log("Removed UIDS: " + implode(', ', $removedUids));
             break;
         case 'delete':
             $subject = "Going Dutch expense deleted in group \"{$groupName}\"";
             $messageTemplate = "The expense on {date} made by {eowner} with {amount} and description \"{description}\" has been deleted.<br /><br />\n";
             $messageTemplateEnd = "The costs per person were {amountpp} making your current balance {yourbalance} which comes to position {yourposition} in the group.\n";
             break;
         default:
             $subject = "Going Dutch expense booked in group \"{$groupName}\"";
             $messageTemplate = "On {date} {eowner} made an expense of {amount} with description \"{description}\".<br /><br />\n";
             $messageTemplateEnd = "The costs per person are {amountpp} making your current balance {yourbalance} which comes to position {yourposition} in the group.\n";
     }
     if (count($uids) == 1) {
         $messageTemplateOnlyPay = $messageTemplate . "{participants} was listed as the only participant (but you paid).<br /><br />\n";
         $messageTemplate .= "You were listed as the only participant.<br /><br />\n";
     } else {
         $messageTemplateOnlyPay = $messageTemplate . "{participants} were listed as the participants (but you paid).<br /><br />";
         $messageTemplate .= "You were listed as a participant, together with {participants}.<br /><br />\n";
     }
     $messageTemplateEnd .= "The balance list is now: <br /><br />{balancelist}\n<br /><br />\n";
     $messageTemplateEnd .= "This expense is logged with id {eid}.";
     $messageTemplate .= $messageTemplateEnd;
     $messageTemplateOnlyPay .= $messageTemplateEnd;
     //$message .= "<br /><br /><a href=\"".LOGIN_URL."\">Going Dutch</a>";
     $posArray = array();
     $balanceTable = "\n<table>\n";
     $i = 1;
     // error_log(print_r($uidDetails,1));
     // error_log(print_r($groupsInfo[$expense['gid']]['members'],1));
     $this->aasort($groupsInfo[$expense['gid']]['members'], "balance", true);
     //error_log(print_r($groupsInfo[$expense['gid']]['members'],1));
     foreach ($groupsInfo[$expense['gid']]['members'] as $member) {
         $posArray[$member['uid']] = $i;
         $b = $formatter->formatCurrency($member['balance'], $groupsInfo[$expense['gid']]['currency']);
         // $style = $i < 0 ? '<style=\"color: red\">' : '<style = \"\">';
         // $balanceTable .= "<tr><td>{$i}</td><td>{$uidDetails[$member['uid']]['realname']}</td><td>{$style}{$b}</style></td></tr>\n";
         $balanceTable .= "<tr><td>{$i}</td><td>{$uidDetails[$member['uid']]['realname']}</td><td>{$b}</td></tr>\n";
         $i++;
     }
     $balanceTable .= "</table>\n";
     $onlyPay = false;
     if (!in_array($expense['uid'], $uids)) {
         $onlyPay = true;
         $uids[] = $expense['uid'];
     }
     $uids = array_merge($uids, $removedUids);
     foreach ($uids as $uid) {
         if ($onlyPay && $uid == $expense['uid']) {
             $message = str_replace('{date}', $created, $messageTemplateOnlyPay);
         } else {
             $message = str_replace('{date}', $created, $messageTemplate);
         }
         // $style = $groupsInfo[$expense->gid]['members'][$uid]['balance'] < 0 ? '<style=\"color: red\">' : '<style = \"\">';
         $yourBalance = $formatter->formatCurrency($groupsInfo[$expense['gid']]['members'][$uid]['balance'], $groupsInfo[$expense['gid']]['currency']);
         // $yourBalance = $style . $yourBalance . '</style>';
         $message = str_replace('{eowner}', $expense['uid'] == $uid ? "you" : $uidDetails[$expense['uid']]['realname'], $message);
         $message = str_replace('{amount}', $amount, $message);
         $message = str_replace('{amountpp}', $amountpp, $message);
         $message = str_replace('{yourbalance}', $yourBalance, $message);
         $message = str_replace('{description}', $expense['etitle'], $message);
         $message = str_replace('{yourposition}', $posArray[$uid], $message);
         $message = str_replace('{balancelist}', $balanceTable, $message);
         $message = str_replace('{eid}', $eid, $message);
         if (in_array($uid, $removedUids)) {
             $message = str_replace('{removed}', "You are no longer listed as a participant for this expense.<br /><br />\n", $message);
         } else {
             $message = str_replace('{removed}', '', $message);
         }
         $participants = '';
         $count = count($uids) - count($removedUids) - ($onlyPay ? 1 : 0);
         // error_log("EID: " . $eid . " COUNT: " . $count . " Onlypay: " . $onlyPay . " Uids: " . implode(",", $uids) . ' UID: ' . $expense->uid);
         if ($count > 1) {
             // error_log("EID: " .  $eid . " COUNT: " . $count . " Onlypay: " . $onlyPay . " Uids: " . implode(",", $uids) . ' UID: ' . $expense->uid);
             foreach ($uids as $uidP) {
                 if ($uid == $uidP || $onlyPay && $uidP == $expense['uid'] || in_array($uidP, $removedUids)) {
                     continue;
                 }
                 $participants[] = $uidDetails[$uidP]['realname'];
             }
             $last = array_pop($participants);
             $participants = count($participants) ? implode(", ", $participants) . " and " . $last : $last;
         } elseif ($count == 1 && $onlyPay) {
             foreach ($uids as $uidP) {
                 if ($uidP != $expense['uid'] && !in_array($uidP, $removedUids)) {
                     $participants = $uidDetails[$uidP]['realname'];
                 }
             }
         }
         $message = str_replace('{participants}', $participants, $message);
         $to = $uidDetails[$uid]['email'];
         if (in_array($uid, $removedUids)) {
             $message = preg_replace('/You were listed .*<br \\/>/', '', $message);
             $message = preg_replace('/The costs per person .* current balance/', 'Your current balance is now', $message);
         }
         if (!in_array($uid, $noMailUsers)) {
             $sql = "INSERT INTO email (gid , eid, subject, message, toaddress, fromaddress, submitted)\n                    VALUES (:gid, :eid, :subject, :message, :toaddress, :fromaddress, FROM_UNIXTIME(:submitted))";
             $stmt = Db::getInstance()->prepare($sql);
             $stmt->execute(array(':gid' => $expense['gid'], ':eid' => $eid, ':subject' => $subject, ':message' => $message, ':toaddress' => $to, ':fromaddress' => $from, ':submitted' => time()));
         }
     }
     //        $file = 'C:\xampp\htdocs\api.gdutch.nl\sendmail.php';
     //$cmd = "/usr/bin/php5 {$background_mailfile} {$user['email']} {$from} \"{$from_name}\" \"{$subject}\" \"{$body}\" \"{$replyto}\" \"{$sendas}\"";
     //exec("/usr/bin/php {$background_mailfile} {$user['email']} {$from} {$from_name} {$subject} {$body} {$replyto} {$sendas} > {$ouput} &");
     //        $cmd = "C:\\xampp\\php\\php.exe {$file}";
     //        $output = '/dev/null';
     // exec("{$cmd} > {$output} &");
     // exec("{$cmd} ");
     Db::triggerSendMail();
 }