*/ /** * Klasa stWebApi * * @package stWebApiPlugin */ class stWebApi { /** * Sprawdzanie zablokowania API * * @return true/false - zablokowane/odblokowane */ public static function isEnabled() { $webapiConfig = stConfig::getInstance(sfContext::getInstance(), 'stWebApiBackend'); return $webapiConfig->get('webapi_on'); } public static function getLogin($hash = '', $perm) { $c = new Criteria(); $c->add(WebApiSessionPeer::HASH, $hash); $webapiConfig = stConfig::getInstance(sfContext::getInstance(), 'stWebApiBackend'); $timeLimit = $webapiConfig->get('session_time'); $updatedAt = time() - $timeLimit; // $c->add(WebApiSessionPeer::UPDATED_AT, $updatedAt, Criteria::GREATER_THAN); $session = WebApiSessionPeer::doSelectOne($c); if (!$session || strtotime($session->getUpdatedAt()) < $updatedAt) { throw new SoapFault("255", sfContext::getInstance()->getI18n()->__("Proszę się zalogować", '', 'stWebApiBackend')); } else { if (!$session->getSfGuardUser()->hasPermission($perm) && $perm != '') { if ($perm == 'webapi_read') throw new SoapFault("255", sfContext::getInstance()->getI18n()->__("Użytkownik nie ma praw odczytu.", '', 'stWebApiBackend')); if ($perm == 'webapi_write') throw new SoapFault("255", sfContext::getInstance()->getI18n()->__("Użytkownik nie ma praw zapisu.", '', 'stWebApiBackend')); } $session->setActive(1); $session->setUpdatedAt(time()); $session->save(); sfContext::getInstance()->getUser()->signIn($session->getSfGuardUser()); } } public static function formatData($data, $type = 'string') { $result = null; switch ($type) { case "string": $data = htmlspecialchars_decode($data); $data = preg_replace( '/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]' . '|[\x00-\x7F][\x80-\xBF]+' . '|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*' . '|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})' . '|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/S', '', $data ); //reject overly long 3 byte sequences and UTF-16 surrogates and replace with ? $data = preg_replace('/\xE0[\x80-\x9F][\x80-\xBF]' . '|\xED[\xA0-\xBF][\x80-\xBF]/S', '', $data); $data = iconv("utf-8", "utf-8//ignore", $data); $result = $data; break; case "boolean": $result = boolval($data); break; case "integer": $result = intval($data); break; case "double": $result = floatval($data); break; case "dateTime": $result = !empty($data) ? date_format(date_create($data), DATE_ATOM) : null; break; default: $result = (string)$data; break; } return $result; } }