invoiceninja/app/libraries/utils.php

465 lines
11 KiB
PHP
Raw Normal View History

2013-12-07 22:33:07 +02:00
<?php
class Utils
{
2014-04-13 11:19:10 +03:00
public static function isRegistered()
{
return Auth::check() && Auth::user()->registered;
}
2014-04-23 16:02:24 +03:00
public static function isConfirmed()
{
return Auth::check() && Auth::user()->confirmed;
}
public static function isProd()
{
return App::environment() == ENV_PRODUCTION;
2014-04-04 00:12:38 +03:00
}
2014-04-18 11:57:31 +03:00
public static function isNinja()
{
return self::isNinjaProd() || self::isNinjaDev();
}
2014-04-04 00:12:38 +03:00
public static function isNinjaProd()
{
2014-05-26 09:24:37 +03:00
return isset($_ENV['NINJA_PROD']) && $_ENV['NINJA_PROD'];
}
public static function isNinjaDev()
{
return isset($_ENV['NINJA_DEV']) && $_ENV['NINJA_DEV'];
}
2014-05-17 22:29:57 +03:00
public static function isPro()
{
return Auth::check() && Auth::user()->isPro();
}
2014-04-18 11:57:31 +03:00
public static function getProLabel($feature)
{
if (Auth::check()
&& !Auth::user()->isPro()
2014-04-29 23:46:40 +03:00
&& $feature == ACCOUNT_ADVANCED_SETTINGS)
2014-04-18 11:57:31 +03:00
{
return '&nbsp;<sup class="pro-label">PRO</sup>';
}
else
{
return '';
}
}
public static function basePath()
{
return substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/') + 1);
}
2014-03-27 14:25:31 +02:00
public static function trans($input)
{
$data = [];
foreach ($input as $field)
{
if ($field == "checkbox")
{
$data[] = $field;
}
else
{
$data[] = trans("texts.$field");
}
}
return $data;
}
2014-01-08 23:22:56 +00:00
public static function fatalError($message = false, $exception = false)
2013-12-31 21:49:54 +02:00
{
2014-01-08 23:22:56 +00:00
if (!$message)
2014-01-02 01:12:33 +02:00
{
2014-04-01 13:30:43 +03:00
$message = "An error occurred, please try again later.";
2014-01-02 01:12:33 +02:00
}
2014-01-08 23:22:56 +00:00
static::logError($message . ' ' . $exception);
2014-01-02 01:12:33 +02:00
2014-04-26 22:35:14 +03:00
$data = [
'showBreadcrumbs' => false
];
return View::make('error', $data)->with('error', $message);
2014-01-08 23:22:56 +00:00
}
public static function logError($error, $context = 'PHP')
{
$count = Session::get('error_count', 0);
Session::put('error_count', ++$count);
2014-01-09 13:44:55 +00:00
if ($count > 100) return 'logged';
2014-01-08 23:22:56 +00:00
$data = [
'context' => $context,
'user_id' => Auth::check() ? Auth::user()->id : 0,
2014-01-31 00:29:09 +02:00
'user_name' => Auth::check() ? Auth::user()->getDisplayName() : '',
2014-01-29 12:41:38 +02:00
'url' => Input::get('url', Request::url()),
2014-01-09 13:44:55 +00:00
'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '',
2014-01-08 23:22:56 +00:00
'ip' => Request::getClientIp(),
2014-03-23 20:01:00 +02:00
'count' => Session::get('error_count', 0)
2014-01-08 23:22:56 +00:00
];
2014-03-23 19:54:02 +02:00
Log::error($error."\n", $data);
2014-01-08 23:22:56 +00:00
/*
Mail::queue('emails.error', ['message'=>$error.' '.json_encode($data)], function($message)
{
$message->to($email)->subject($subject);
});
*/
2013-12-31 21:49:54 +02:00
}
2014-01-14 11:52:56 +00:00
public static function parseFloat($value)
{
$value = preg_replace('/[^0-9\.\-]/', '', $value);
return floatval($value);
}
2013-12-07 22:33:07 +02:00
public static function formatPhoneNumber($phoneNumber)
{
2014-01-09 21:38:18 +00:00
$phoneNumber = preg_replace('/[^0-9a-zA-Z]/','',$phoneNumber);
2013-12-07 22:33:07 +02:00
if (!$phoneNumber) {
return '';
}
if(strlen($phoneNumber) > 10) {
$countryCode = substr($phoneNumber, 0, strlen($phoneNumber)-10);
$areaCode = substr($phoneNumber, -10, 3);
$nextThree = substr($phoneNumber, -7, 3);
$lastFour = substr($phoneNumber, -4, 4);
$phoneNumber = '+'.$countryCode.' ('.$areaCode.') '.$nextThree.'-'.$lastFour;
}
else if(strlen($phoneNumber) == 10 && in_array(substr($phoneNumber, 0, 3), array(653, 656, 658, 659))) {
/**
* SG country code are 653, 656, 658, 659
* US area code consist of 650, 651 and 657
* @see http://en.wikipedia.org/wiki/Telephone_numbers_in_Singapore#Numbering_plan
* @see http://www.bennetyee.org/ucsd-pages/area.html
*/
2014-07-19 11:11:01 +08:00
$countryCode = substr($phoneNumber, 0, 2);
$nextFour = substr($phoneNumber, 2, 4);
$lastFour = substr($phoneNumber, 6, 4);
2014-07-19 11:11:01 +08:00
$phoneNumber = '+'.$countryCode.' '.$nextFour.' '.$lastFour;
}
2013-12-07 22:33:07 +02:00
else if(strlen($phoneNumber) == 10) {
$areaCode = substr($phoneNumber, 0, 3);
$nextThree = substr($phoneNumber, 3, 3);
$lastFour = substr($phoneNumber, 6, 4);
$phoneNumber = '('.$areaCode.') '.$nextThree.'-'.$lastFour;
}
else if(strlen($phoneNumber) == 7) {
$nextThree = substr($phoneNumber, 0, 3);
$lastFour = substr($phoneNumber, 3, 4);
$phoneNumber = $nextThree.'-'.$lastFour;
}
return $phoneNumber;
}
2014-04-23 23:30:54 +03:00
public static function formatMoney($value, $currencyId = false)
2013-12-29 19:40:11 +02:00
{
2014-04-23 23:30:54 +03:00
if (!$currencyId)
{
$currencyId = Session::get(SESSION_CURRENCY);
}
2014-01-09 10:39:20 +00:00
$currency = Currency::remember(DEFAULT_QUERY_CACHE)->find($currencyId);
if (!$currency)
{
$currency = Currency::remember(DEFAULT_QUERY_CACHE)->find(1);
2013-12-29 19:40:11 +02:00
}
2014-01-09 10:39:20 +00:00
2013-12-29 19:40:11 +02:00
return $currency->symbol . number_format($value, $currency->precision, $currency->decimal_separator, $currency->thousand_separator);
}
2013-12-08 15:32:49 +02:00
public static function pluralize($string, $count)
2013-12-07 22:33:07 +02:00
{
2014-04-01 13:30:43 +03:00
$field = $count == 1 ? $string : $string . 's';
2014-04-02 16:10:00 +03:00
$string = trans("texts.$field", ['count' => $count]);
2014-04-01 13:30:43 +03:00
return $string;
2013-12-07 22:33:07 +02:00
}
2013-12-08 15:32:49 +02:00
public static function toArray($data)
2013-12-07 22:33:07 +02:00
{
return json_decode(json_encode((array) $data), true);
}
2013-12-08 15:32:49 +02:00
public static function toSpaceCase($camelStr)
2013-12-07 22:33:07 +02:00
{
return preg_replace('/([a-z])([A-Z])/s','$1 $2', $camelStr);
}
2013-12-08 15:32:49 +02:00
public static function timestampToDateTimeString($timestamp) {
2013-12-15 14:55:50 +02:00
$timezone = Session::get(SESSION_TIMEZONE, DEFAULT_TIMEZONE);
$format = Session::get(SESSION_DATETIME_FORMAT, DEFAULT_DATETIME_FORMAT);
return Utils::timestampToString($timestamp, $timezone, $format);
2013-12-07 22:33:07 +02:00
}
2013-12-08 15:32:49 +02:00
public static function timestampToDateString($timestamp) {
2013-12-15 14:55:50 +02:00
$timezone = Session::get(SESSION_TIMEZONE, DEFAULT_TIMEZONE);
$format = Session::get(SESSION_DATE_FORMAT, DEFAULT_DATE_FORMAT);
return Utils::timestampToString($timestamp, $timezone, $format);
}
2013-12-16 21:27:32 +00:00
public static function dateToString($date) {
$dateTime = new DateTime($date);
$timestamp = $dateTime->getTimestamp();
$format = Session::get(SESSION_DATE_FORMAT, DEFAULT_DATE_FORMAT);
return Utils::timestampToString($timestamp, false, $format);
}
public static function timestampToString($timestamp, $timezone = false, $format)
2013-12-15 14:55:50 +02:00
{
2013-12-24 01:22:16 -05:00
if (!$timestamp) {
return '';
}
2013-12-16 21:27:32 +00:00
$date = Carbon::createFromTimeStamp($timestamp);
if ($timezone) {
$date->tz = $timezone;
}
2013-12-07 22:33:07 +02:00
if ($date->year < 1900) {
return '';
}
2013-12-15 14:55:50 +02:00
return $date->format($format);
2013-12-16 21:27:32 +00:00
}
2013-12-07 22:33:07 +02:00
public static function toSqlDate($date, $formatResult = true)
2013-12-07 22:33:07 +02:00
{
if (!$date)
{
2013-12-09 11:38:49 +02:00
return null;
2013-12-07 22:33:07 +02:00
}
2014-01-01 01:50:13 +02:00
$timezone = Session::get(SESSION_TIMEZONE);
$format = Session::get(SESSION_DATE_FORMAT);
2013-12-25 16:34:42 -05:00
$dateTime = DateTime::createFromFormat($format, $date, new DateTimeZone($timezone));
return $formatResult ? $dateTime->format('Y-m-d') : $dateTime;
2013-12-07 22:33:07 +02:00
}
public static function fromSqlDate($date, $formatResult = true)
2013-12-07 22:33:07 +02:00
{
if (!$date || $date == '0000-00-00')
{
return '';
}
2014-01-01 01:50:13 +02:00
$timezone = Session::get(SESSION_TIMEZONE);
$format = Session::get(SESSION_DATE_FORMAT);
$dateTime = DateTime::createFromFormat('Y-m-d', $date, new DateTimeZone($timezone));
return $formatResult ? $dateTime->format($format) : $dateTime;
2014-01-01 01:50:13 +02:00
}
public static function today($formatResult = true)
{
$timezone = Session::get(SESSION_TIMEZONE);
$format = Session::get(SESSION_DATE_FORMAT);
$date = date_create(null, new DateTimeZone($timezone));
if ($formatResult)
{
return $date->format($format);
}
else
{
return $date;
}
2013-12-07 22:33:07 +02:00
}
2013-12-25 16:34:42 -05:00
public static function trackViewed($name, $type, $url = false)
2013-12-07 22:33:07 +02:00
{
2013-12-25 16:34:42 -05:00
if (!$url)
{
$url = Request::url();
}
2013-12-07 22:33:07 +02:00
$viewed = Session::get(RECENTLY_VIEWED);
if (!$viewed)
{
$viewed = [];
}
$object = new stdClass;
$object->url = $url;
$object->name = ucwords($type) . ': ' . $name;
2014-05-21 00:40:09 +03:00
$data = [];
2013-12-07 22:33:07 +02:00
for ($i=0; $i<count($viewed); $i++)
{
$item = $viewed[$i];
2014-05-21 00:40:09 +03:00
if ($object->url == $item->url || $object->name == $item->name)
2013-12-07 22:33:07 +02:00
{
2014-05-21 00:40:09 +03:00
continue;
}
array_unshift($data, $item);
2013-12-07 22:33:07 +02:00
}
2014-05-21 00:40:09 +03:00
array_unshift($data, $object);
2013-12-07 22:33:07 +02:00
2014-05-21 00:40:09 +03:00
if (count($data) > RECENTLY_VIEWED_LIMIT)
2013-12-07 22:33:07 +02:00
{
2014-05-21 00:40:09 +03:00
array_pop($data);
2013-12-07 22:33:07 +02:00
}
2014-05-21 00:40:09 +03:00
Session::put(RECENTLY_VIEWED, $data);
2013-12-07 22:33:07 +02:00
}
2013-12-10 19:18:35 +02:00
public static function processVariables($str)
{
if (!$str) {
return '';
}
$variables = ['MONTH', 'QUARTER', 'YEAR'];
for ($i=0; $i<count($variables); $i++)
{
$variable = $variables[$i];
2013-12-11 13:11:59 +02:00
$regExp = '/:' . $variable . '[+-]?[\d]*/';
2013-12-10 19:18:35 +02:00
preg_match_all($regExp, $str, $matches);
$matches = $matches[0];
if (count($matches) == 0) {
continue;
}
foreach ($matches as $match) {
$offset = 0;
$addArray = explode('+', $match);
$minArray = explode('-', $match);
if (count($addArray) > 1) {
$offset = intval($addArray[1]);
} else if (count($minArray) > 1) {
$offset = intval($minArray[1]) * -1;
}
$val = Utils::getDatePart($variable, $offset);
$str = str_replace($match, $val, $str);
}
}
return $str;
}
2013-12-07 22:33:07 +02:00
2013-12-10 19:18:35 +02:00
private static function getDatePart($part, $offset)
{
$offset = intval($offset);
if ($part == 'MONTH') {
return Utils::getMonth($offset);
} else if ($part == 'QUARTER') {
return Utils::getQuarter($offset);
} else if ($part == 'YEAR') {
return Utils::getYear($offset);
}
}
private static function getMonth($offset)
{
$months = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
$month = intval(date('n')) - 1;
2014-01-09 10:05:35 +00:00
2013-12-10 19:18:35 +02:00
$month += $offset;
$month = $month % 12;
2014-01-09 10:05:35 +00:00
if ($month < 0)
{
$month += 12;
}
2013-12-10 19:18:35 +02:00
return $months[$month];
}
private static function getQuarter($offset)
{
$month = intval(date('n')) - 1;
$quarter = floor(($month + 3) / 3);
$quarter += $offset;
$quarter = $quarter % 4;
if ($quarter == 0) {
$quarter = 4;
}
return 'Q' . $quarter;
}
private static function getYear($offset)
{
$year = intval(date('Y'));
return $year + $offset;
}
2013-12-11 00:10:43 +02:00
public static function getEntityName($entityType)
{
return ucwords(str_replace('_', ' ', $entityType));
}
2013-12-11 22:33:44 +02:00
2013-12-30 22:17:45 +02:00
public static function getClientDisplayName($model)
{
if ($model->client_name)
{
return $model->client_name;
}
else if ($model->first_name || $model->last_name)
{
return $model->first_name . ' ' . $model->last_name;
}
else
{
return $model->email;
}
}
2014-01-08 20:09:47 +00:00
public static function encodeActivity($person = null, $action, $entity = null, $otherPerson = null)
{
2014-01-08 23:22:56 +00:00
$person = $person ? $person->getDisplayName() : '<i>System</i>';
2014-01-09 10:39:20 +00:00
$entity = $entity ? '[' . $entity->getActivityKey() . ']' : '';
2014-01-08 23:22:56 +00:00
$otherPerson = $otherPerson ? 'to ' . $otherPerson->getDisplayName() : '';
2014-01-08 20:09:47 +00:00
return trim("$person $action $entity $otherPerson");
}
public static function decodeActivity($message)
{
$pattern = '/\[([\w]*):([\d]*):(.*)\]/i';
preg_match($pattern, $message, $matches);
if (count($matches) > 0)
{
$match = $matches[0];
$type = $matches[1];
$publicId = $matches[2];
$name = $matches[3];
$link = link_to($type . 's/' . $publicId, $name);
$message = str_replace($match, "$type $link", $message);
}
return $message;
}
2014-07-15 23:36:40 +03:00
public static function generateLicense() {
$parts = [];
for ($i=0; $i<5; $i++) {
$parts[] = strtoupper(str_random(4));
}
return join('-', $parts);
}
2013-12-07 22:33:07 +02:00
}