Skip to content

Commit

Permalink
Webasyst framework v.1.7.8
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonix committed Aug 14, 2017
1 parent b2671d1 commit c6b96ab
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 154 deletions.
46 changes: 37 additions & 9 deletions wa-system/contact/waContact.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,26 @@ public function __construct($id = null, $options = array())
self::$options[$name] = $value;
}

$this->init();

if (is_array($id)) {
if (isset($id['id'])) {
$this->id = $id['id'];
$this->id = (int)$id['id'];
unset($id['id']);
}
} else {
$this->id = (int)$id;
}
if ($this->id && $this->id < 0) {
throw new waException('negative id');
}

$this->init();

if (is_array($id)) {
$this->setCache($id);
foreach ($id as $k => $v) {
if ($k != 'id') {
$this->set($k, $v);
}
$this->set($k, $v);
}
$this->setCache($this->data);
} else {
$this->id = (int)$id;
}
}

Expand Down Expand Up @@ -607,7 +612,7 @@ public function exists()
public function save($data = array(), $validate = false)
{
$add = array();
foreach ($data as $key => $value) {
foreach ($this->removeSpecialFields($data) as $key => $value) {
if (strpos($key, '.')) {
$key_parts = explode('.', $key);
$f = waContactFields::get($key_parts[0]);
Expand Down Expand Up @@ -637,6 +642,8 @@ public function save($data = array(), $validate = false)
$this->data[$key] = $value;
}
}

$this->data = $this->removeSpecialFields($this->data);
$this->data['name'] = $this->get('name');
$this->data['firstname'] = $this->get('firstname');
$this->data['is_company'] = $this->get('is_company');
Expand Down Expand Up @@ -764,6 +771,27 @@ public function save($data = array(), $validate = false)
return $errors ? $errors : 0;
}

// Return a new array, removing fields that are not allowed for save() to modify

This comment has been minimized.

Copy link
@ilyaplot

ilyaplot Feb 6, 2018

It is epic fail.

protected function removeSpecialFields($data)
{
unset(
$data['id'],
$data['last_datetime'],
$data['create_datetime']
);
if (wa()->getEnv() == 'frontend' || waConfig::get('is_template')) {
unset(
$data['login'],
$data['is_user'],
$data['is_company'],
$data['create_method'],
$data['create_app_id'],
$data['create_contact_id']
);
}
return $data;
}

public function clearDisabledFields()
{
if (!$this->id) {
Expand Down
131 changes: 42 additions & 89 deletions wa-system/contact/waContactAddressField.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ public function format($data, $format = null, $ignore_hidden = true)
if (!isset($data['value'])) {
$value = array();
foreach ($this->options['fields'] as $field) {
/**
* @var $field waContactField
*/
$f_id = $field->getId();
if ($ignore_hidden && $field instanceof waContactHiddenField) {
continue;
}
/**
* @var $field waContactField
*/

if (isset($data['data'][$f_id])) {
$tmp = trim($field->format($data['data'][$f_id], 'value', $data['data']));
if ($tmp) {
Expand All @@ -68,90 +69,43 @@ public function format($data, $format = null, $ignore_hidden = true)
return parent::format($data, $format);
}

private function sendGeoCodingRequest($value)
{
$url = 'https://maps.googleapis.com/maps/api/geocode/json';
$params = array(
'address' => $this->format($value, 'forMap'),
'sensor' => 'false'
);
$url = $url.'?'.http_build_query($params);
$timeout = 9;
if (function_exists('curl_init')) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$content = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status == 200) {
return $content;
}
} else {
if (ini_get('allow_url_fopen')) {
$wrappers = stream_get_wrappers();
if (in_array('https', $wrappers)) {
$old_timeout = @ini_set('default_socket_timeout', $timeout);
$response = @file_get_contents($url);
@ini_set('default_socket_timeout', $old_timeout);
return $response;
}
}
}
return null;
}

private function setGeoCoords($value)
{
if (!isset($value['data'])) {
return $value;
}
$sm = new waAppSettingsModel();
$app_id = 'webasyst';
$name = 'geocoding';
$last_geocoding = $sm->get($app_id, $name, 0);
if (time() - $last_geocoding >= 3600) {
$response = $this->sendGeoCodingRequest($value);
if ($response) {
$response = json_decode($response, true);
if ($response['status'] == "OK") {
$sm->del($app_id, $name);
foreach ($response['results'] as $result) {
if (empty($result['partial_match'])) { // address correct, geocoding without errors
$value['data']['lat'] = ifset($result['geometry']['location']['lat'], '');
$value['data']['lng'] = ifset($result['geometry']['location']['lng'], '');
break;
}
}
} else if ($response['status'] == "OVER_QUERY_LIMIT") {
$sm->set($app_id, $name, time());
}
} else {
$sm->set($app_id, $name, time());

try {
$map = wa()->getMap();
$address = $this->format($value, 'forMap');
$data = null;
if (!empty($address['with_street'])) {
$data = $map->geocode($address['with_street']);
}
if (empty($data) && !empty($address['without_street'])) {
$data = $map->geocode($address['without_street']);
}
if ($data) {
$value['data'] = array_merge($value['data'], $data);
}
} catch (waException $ex) {
waLog::log("waContactAddressField->setGeoCoords(): ".$ex->getMessage()."\n".$ex->getFullTraceAsString(), 'geocode.log');
}
return $value;
}

public function set(waContact $contact, $value, $params = array(), $add = false)
public function prepareSave($value, waContact $contact = null)
{
$result = parent::set($contact, $value, $params, $add);
if (true || !empty($this->options['geocoding'])) {
if (isset($result[0])) {
foreach ($result as &$value) {
$value = $this->setGeoCoords($value);
}
unset($value);
} else {
$result = $this->setGeoCoords($result);
if (isset($value[0])) {
foreach ($value as &$v) {
$v = $this->setGeoCoords($v);
}
unset($v);
} else {
$value = $this->setGeoCoords($value);
}
return $result;
return parent::prepareSave($value, $contact);
}

}

class waContactAddressForMapFormatter extends waContactFieldFormatter
Expand All @@ -161,17 +115,19 @@ public function format($data) {
'with_street' => '',
'without_street' => ''
);
foreach ($res as $k => &$r) {
$parts = array();
foreach (waContactFields::get('address')->getFields() as $field) {
/**
* @var waContactField $field
*/
$id = $field->getId();
if (isset($data['data'][$id]) && trim($data['data'][$id])) {
$parts[$id] = $field->format($data['data'][$id], 'value', $data['data']);
}
$parts = array();
foreach (waContactFields::get('address')->getFields() as $field) {
/**
* @var waContactField $field
*/
$id = $field->getId();
if (!in_array($id, array('lat', 'lng')) && isset($data['data'][$id]) && trim($data['data'][$id])) {
$parts[$id] = $field->format($data['data'][$id], 'value', $data['data']);
}
}

foreach ($res as $k => &$r) {

$p = $parts;
$value = array();
if (isset($parts['country'])) {
Expand All @@ -182,11 +138,8 @@ public function format($data) {
$value[] = $p['region'];
unset($p['region']);
}
if (isset($parts['city']))
{
if (!isset($parts['region']) ||
mb_strtolower($parts['region']) != mb_strtolower($parts['city']))
{
if (isset($parts['city'])) {
if (!isset($parts['region']) || (mb_strtolower($parts['region']) != mb_strtolower($parts['city']))) {
$value[] = $p['city'];
}
unset($p['city']);
Expand Down Expand Up @@ -361,7 +314,7 @@ public function format($data)
{
$parts = $this->getParts($data);
$data['value'] = array();
foreach($parts['parts'] + $data['data'] as $key => $value) {
foreach ($parts['parts'] + $data['data'] as $key => $value) {
if (strlen($value)) {
$data['value'][$key] = $value;
}
Expand Down
63 changes: 34 additions & 29 deletions wa-system/file/waNet.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,20 @@ protected function buildHeaders($transport, $raw = true)
{
$this->request_headers['Connection'] = 'close';
$this->request_headers['Date'] = date('c');
switch ($this->options['format']) {
case self::FORMAT_JSON:
$this->request_headers["Accept"] = "application/json";
break;
if (empty($this->request_headers['Accept'])) {
switch ($this->options['format']) {
case self::FORMAT_JSON:
$this->request_headers["Accept"] = "application/json";
break;

case self::FORMAT_XML:
$this->request_headers["Accept"] = "text/xml";
break;
case self::FORMAT_XML:
$this->request_headers["Accept"] = "text/xml";
break;

default:
$this->request_headers['Accept'] = '*/*';
break;
default:
$this->request_headers['Accept'] = '*/*';
break;
}
}

$this->request_headers['Accept-Charset'] = $this->options['charset'];
Expand Down Expand Up @@ -274,20 +276,22 @@ protected function encodeRequest($content)
}

$this->request_headers['Content-Length'] = strlen($content);
switch ($format) {
case self::FORMAT_JSON:
$this->request_headers['Content-Type'] = 'application/json';
break;
if (empty($this->request_headers['Content-Type'])) {
switch ($format) {
case self::FORMAT_JSON:
$this->request_headers['Content-Type'] = 'application/json';
break;

case self::FORMAT_XML:
$this->request_headers['Content-Type'] = 'application/xml';
break;
case self::FORMAT_CUSTOM:
//$this->request_headers['Content-Type'] ='application/'.$this->options['custom_content_type'];
break;
default:
$this->request_headers['Content-Type'] = 'application/x-www-form-urlencoded';
break;
case self::FORMAT_XML:
$this->request_headers['Content-Type'] = 'application/xml';
break;
case self::FORMAT_CUSTOM:
//$this->request_headers['Content-Type'] ='application/'.$this->options['custom_content_type'];
break;
default:
$this->request_headers['Content-Type'] = 'application/x-www-form-urlencoded';
break;
}
}
if (!empty($this->options['md5'])) {
$this->request_headers['Content-MD5'] = base64_encode(md5($content, true));
Expand Down Expand Up @@ -345,12 +349,13 @@ protected function decodeResponse($response)
$this->decoded_response = @simplexml_load_string($this->raw_response);

if ($this->decoded_response === false) {
$error = libxml_get_last_error();
/**
* @var LibXMLError $error
*/
$this->log($error->message);
throw new waException('Error while decode XML response: '.$error->message, $error->code);
if ($error = libxml_get_last_error()) {
/**
* @var LibXMLError $error
*/
$this->log($error->message);
throw new waException('Error while decode XML response: '.$error->message, $error->code);
}
}
break;
default:
Expand Down
Loading

0 comments on commit c6b96ab

Please sign in to comment.