$value) { $lowerFrom[ strtolower($fieldName) ] = $value; } return V::get(strtolower($name), $default, $lowerFrom, $type, $filterCallback); } /** * Get variable from array or object. */ public static function get($name, $default, $from, $type = '', $filterCallback = null) { if (empty($name)) return null; $ret = null; if (is_bool($name)) $name = (int)$name; if (!is_string($name) && !is_numeric($name)) { //var_dump($name); //echo'
';print_r(debug_backtrace());echo''; throw new Exception("Error name is not scalar! '{$name}'"); } if (is_array($from)) { if (array_key_exists($name, $from)) { $ret = $from[$name]; } } else if (is_object($from)) { if (isset($from->$name)) { $ret = $from->$name; } } if (isset($ret) && $type != '') { $ret = V::convert($ret, $type); } if (!empty($filterCallback)) { if ($type == 'array' && is_array($ret) && !empty($ret)) { $ret = V::filter($ret, $filterCallback); } } $ret = (null !== $ret)? $ret : $default; return $ret; } /** * Convert variable type. * @usage: V::convert($from, 'url'); */ public static function convert($from, $type = 'string') { switch ($type) { case 'minOccurs': case 'maxOccurs': // The default values for minOccurs and maxOccurs are 1 - @return int or 'unbounded', default is 1 if (!is_scalar($from)) return null; if (!strlen($from)) return 1; if ("unbounded" === $from) return "unbounded"; return (int)$from; } $type = strtolower($type); // is_scalar($from) - return TRUE if int,float,string,bool, FALSE if array,object,resource, ... $ret = null; switch ($type) { case 'string': if (is_scalar($from)) { $ret = $from; settype($ret, $type); } return $ret; case 'word': if (is_scalar($from)) { $ret = $from; settype($ret, 'string'); $ret = trim($ret); if (false !== ($pos = strpos($ret, ' '))) { $ret = substr($ret, 0, $pos); } } return $ret; case 'login':// [a-zA-Z.-_] if (is_scalar($from)) { $ret = $from; settype($ret, 'string'); $ret = trim($ret); if (!preg_match("/^[a-zA-Z.-_]*$/", $ret, $matches)) { $ret = null; } } return $ret; case 'url':// [a-zA-Z0-9_-] if (is_scalar($from)) { $ret = $from; settype($ret, 'string'); $ret = trim($ret); $pl_letters = array('ą', 'ć', 'ę', 'ł', 'ń', 'ó', 'ś', 'ź', 'ż', 'Ą', 'Ć', 'Ę', 'Ł', 'Ń', 'Ó', 'Ś', 'Ź', 'Ż'); $en_letters = array('a', 'c', 'e', 'l', 'n', 'o', 's', 'z', 'z', 'A', 'C', 'E', 'L', 'N', 'O', 'S', 'Z', 'Z'); $ret = str_replace($pl_letters, $en_letters, $ret); $ret = preg_replace('/[^a-zA-Z0-9_-]+/', '_', $ret); } return $ret; case 'int': case 'integer': if (is_scalar($from)) { $ret = $from; settype($ret, $type); } return $ret; case 'float': case 'double': if (is_scalar($from)) { $ret = str_replace(',', '.', $from); settype($ret, $type); } return $ret; case 'price':// 0.00 - decimal(n, 2) if (is_scalar($from)) { $ret = str_replace(',', '.', $from); settype($ret, 'float'); $ret = round($ret, 2); } return $ret; case 'object': case 'array': if (is_scalar($from) || is_array($from) || is_object($from)) { $ret = $from; settype($ret, $type); } return $ret; case 'int_array': if (is_scalar($from) || is_array($from) || is_object($from)) { $ret = array(); $arr = $from; settype($arr, 'array'); foreach ($arr as $v) { $v = V::convert($v, 'int'); $ret[] = $v; } } return $ret; case 'uint_array':// unsigned int array if (is_scalar($from) || is_array($from) || is_object($from)) { $ret = array(); $arr = $from; settype($arr, 'array'); foreach ($arr as $v) { $v = V::convert($v, 'int'); if ($v <= 0) continue; $ret[] = $v; } } return $ret; case 'float_array':// uncigned int array if (is_scalar($from) || is_array($from) || is_object($from)) { $ret = array(); $arr = $from; settype($arr, 'array'); foreach ($arr as $v) { $v = V::convert($v, 'float'); $ret[] = $v; } } return $ret; case 'bool': case 'boolean': return (bool)$from; default: $fun = 'func_type_convert_'.$type; return (function_exists($fun)) ? $fun($from) : null; } } /** * Merge the contents of two objects/array. * * array V::extend(mixed $defaults, mixed $params); * @see http://api.jquery.com/jQuery.extend/ * is_scalar($from) - return TRUE if int,float,string,bool, FALSE if array,object,resource, ... */ public static function extend($defaults, $params) { $ret = array(); $d = (is_array($defaults))? $defaults : (array)$defaults; $p = (is_array($params))? $params : (array)$params; foreach ($d as $k => $v) { $ret[$k] = $v; } foreach ($p as $k => $v) { if (array_key_exists($k, $ret) && (is_array($ret[$k]) || is_object($ret[$k])) && (is_array($v) || is_object($v))) { $ret[$k] = V::extend($ret[$k], $v); } else { $ret[$k] = $v; } } return $ret; } public static function json_encode_latin2($o, $force_object = false) { if ($o === '') { return '""'; } else if (!$o) { return 'null'; } else if (is_array($o)) { $arr = ''; if ($force_object) { foreach ($o as $k => $v) { $arr[] = '"'.$k.'":'.V::json_encode_latin2($v, $force_object); } return '{'.implode(',',$arr).'}'; } else { foreach ($o as $k => $v) { if (is_string($k)) $arr[] = '"'.$k.'":'.V::json_encode_latin2($v, $force_object); else $arr[] = V::json_encode_latin2($v); } return '['.implode(',',$arr).']'; } } else if (is_object($o)) { $arr = ''; foreach (get_object_vars($o) as $k => $v) { $arr[] = '"'.$k.'":'.V::json_encode_latin2($v, $force_object); } return '{'.implode(',',$arr).'}'; } else if (is_string($o)) { return '"'.addslashes(str_replace(array("\n","\r"), array('\n',''), $o)).'"'; } else if (is_numeric($o)) { return ''.$o.''; } else if (is_bool($o)) { return ''.(($o)? 'true' : 'false').''; } } static function safePrintPrettyJson($json) { if (empty($json)) return '{}'; try { $decoded = json_decode($json); if (NULL === $decoded) { throw new Exception(json_last_error()); } return json_encode($decoded, $options = JSON_PRETTY_PRINT); } catch (Exception $e) { DBG::log($e); return 'JSON DECODE ERROR: ' . $e->getMessage(); } } public static function copy($o) { $null = null; if (!$o) { return $null; } else if (is_array($o)) { $ret = array(); foreach ($o as $k => $v) { $ret[$k] = $v; } return $ret; } else if (is_object($o)) { $ret = new stdClass(); foreach (get_object_vars($o) as $k => $v) { $ret->$k = $v; } return $ret; } else { $ret = $o; return $ret; } } public static function make_link($prefix = '', $params = array()) { $ret = ''; if ($prefix) { $ret = $prefix; } if (!empty($params)) { $ret_arr = array(); foreach ($params as $k => $v) { $ret_arr[] = $k . "=" . $v; } $ret .= "?" . implode("&", $ret_arr); } return $ret; } public static function strShort($label, $maxLength = 10, $suffix = ' ...') { if (strlen($label) > $maxLength) { $pos = strpos($label, ' - '); if ($pos > $maxLength || $pos < 5) { $label = substr($label, 0, $maxLength) . $suffix; } else { $label = substr($label, 0, $pos); } } return $label; } public static function strShortUtf8($label, $maxLength = 10, $suffix = ' ...') { if (mb_strlen($label, 'utf-8') > $maxLength) { $pos = mb_strpos($label, ' - ', 0, 'utf-8'); if ($pos > $maxLength || $pos < 5) { $label = mb_substr($label, 0, $maxLength, 'utf-8') . $suffix; } else { $label = mb_substr($label, 0, $pos, 'utf-8'); } } return $label; } public static function filter($array, $filterCallback) { if (!is_callable($filterCallback)) { throw new Exception("callback is not callable '" . ((is_array($filterCallback))? implode('.', $filterCallback) : $filterCallback) . "'"); } return array_filter($array, $filterCallback); } public static function filterNotEmpty($value) { return !empty($value); } public static function filterNotEmptyString($value) { if ('0' === $value) return true; return !empty($value); } public static function filterInteger($value) {// An integer or string with integer value if (is_int($value)) { return true; } else if (is_string($value)) { if ((string)(int)$value === $value) { return true; } } return false; } public static function filterNegativeInteger($value) {// An integer containing only negative values (..,-2,-1) if (V::filterInteger($value)) { if (intval($value) < 0) { return true; } } return false; } public static function filterNonNegativeInteger($value) {// An integer containing only non-negative values (0,1,2,..) if (V::filterInteger($value)) { if (intval($value) >= 0) { return true; } } return false; } public static function filterNonPositiveInteger($value) {// An integer containing only non-positive values (..,-2,-1,0) if (V::filterInteger($value)) { if (intval($value) <= 0) { return true; } } return false; } public static function filterPositiveInteger($value) {// An integer containing only positive values (1,2,..) if (V::filterInteger($value)) { if (intval($value) > 0) { return true; } } return false; } public static function validate($argName, $args, $params) { //$what = V::validate('what', $args, array('type'=>'word', 'not_empty'=>true, 'max_length'=>'255', 'values'=>$when_values)); $argValue = V::get($argName, null, $args); $fldLabel = V::get('fld_label', $argName, $params); if (array_key_exists('not_empty', $params) && true == $params['not_empty']) { if (!array_key_exists($argName, $args) || empty($args[$argName])) throw new Exception("Field {$fldLabel} not set."); } $params['fld_label'] = $fldLabel; return V::validateValue($argValue, $params); } public static function validateValue($value, $params) { $fldLabel = V::get('fld_label', '', $params); $maxLength = V::get('max_length', 0, $params); if ($maxLength > 0) { if (strlen($value) > $maxLength) throw new Exception("'{$fldLabel}' cannot be longer then {$maxLength}."); } $allowedValues = V::get('values', null, $params); if (is_array($allowedValues) && !empty($allowedValues)) { if (!in_array($value, $allowedValues)) throw new Exception("'{$fldLabel}' value is not allowed"); } $type = V::get('type', null, $params); if ($type != null) { if ('word' == $type) { if (!is_scalar($value) || !preg_match('/^[a-zA-Z_-]*$/', $value)) throw new Exception("required type '{$type}' ({$fldLabel})"); } else if ('login' == $type) { if (!is_scalar($value) || !preg_match('/^[a-zA-Z\._-]*$/', $value)) throw new Exception("required type '{$type}' ({$fldLabel})"); } else { throw new Exception("Unimplemented type to validate: '{$type}'"); } } if (array_key_exists('equal', $params)) { if ($value != $params['equal']) throw new Exception(V::get('error_msg_equal', "'{$fldLabel}' must be equal to '{$params['equal']}'", $params)); } if (array_key_exists('equalStrict', $params)) { if ($value !== $params['equalStrict']) throw new Exception(V::get('error_msg_equalStrict', "'{$fldLabel}' must be strict equal to '{$params['equal']}'", $params)); } return $value; } public static function exec($cmd, &$out, &$ret) { $out = null; $ret = null; // NOTE: SourceGuardian requires file: "${HOME}/.config/SourceGuardian/" $cmd = implode("\n", [ "PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/local/bin:/opt/local/lib/mysql55/bin:/Applications/Server.app/Contents/ServerRoot/usr/bin:/Applications/Server.app/Contents/ServerRoot/usr/sbin:/Users/pl/programy/bin", "export HOME='/Library/WebServer'", $cmd ]); exec($cmd, $out, $ret); return $ret; } static function shell_exec($cmd) { $out = null; $ret = null; // NOTE: SourceGuardian requires file: "${HOME}/.config/SourceGuardian/" $cmd = implode("\n", [ "PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/local/bin:/opt/local/lib/mysql55/bin:/Applications/Server.app/Contents/ServerRoot/usr/bin:/Applications/Server.app/Contents/ServerRoot/usr/sbin:/Users/pl/programy/bin", "export HOME='/Library/WebServer'", $cmd ]); return shell_exec($cmd); } public static function execRemote($host, $login, $password, $command, &$out, &$ret, $port = 22) { $out = null; $ret = null; $pass = $password; $pass = str_replace('!', '\!', $pass); $sshPort = (22 != $port)? "-p {$port}" : ''; $cmd = '/opt/local/bin/sshpass -p ' . $pass . ' ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ConnectTimeout=99999 ' . $sshPort . ' ' . $login . '@' . $host . ' -t <