V.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. /**
  3. * @see http://pl2.php.net/manual/en/book.var.php
  4. *
  5. * Define Your own convert function: var func_type_convert_{$type}($var);
  6. */
  7. class V {
  8. /**
  9. * Get variable from array or object.
  10. */
  11. public static function get($name, $default, $from, $type = '', $filterCallback = null) {
  12. if (empty($name)) return null;
  13. $ret = null;
  14. if (is_array($from)) {
  15. if (array_key_exists($name, $from)) {
  16. $ret = $from[$name];
  17. }
  18. }
  19. else if (is_object($from)) {
  20. if (isset($from->$name)) {
  21. $ret = $from->$name;
  22. }
  23. }
  24. if (isset($ret) && $type != '') {
  25. $ret = V::convert($ret, $type);
  26. }
  27. if (!empty($filterCallback)) {
  28. if ($type == 'array' && is_array($ret) && !empty($ret)) {
  29. $ret = V::filter($ret, $filterCallback);
  30. }
  31. }
  32. $ret = (null !== $ret)? $ret : $default;
  33. return $ret;
  34. }
  35. /**
  36. * Convert variable type.
  37. */
  38. public static function convert($from, $type = 'string') {
  39. $type = strtolower($type);
  40. // is_scalar($from) - return TRUE if int,float,string,bool, FALSE if array,object,resource, ...
  41. $ret = null;
  42. switch ($type) {
  43. case 'string':
  44. if (is_scalar($from)) {
  45. $ret = $from;
  46. settype($ret, $type);
  47. }
  48. break;
  49. case 'word':
  50. if (is_scalar($from)) {
  51. $ret = $from;
  52. settype($ret, 'string');
  53. $ret = trim($ret);
  54. if (false !== ($pos = strpos($ret, ' '))) {
  55. $ret = substr($ret, 0, $pos);
  56. }
  57. }
  58. break;
  59. case 'login':// [a-zA-Z.-_]
  60. if (is_scalar($from)) {
  61. $ret = $from;
  62. settype($ret, 'string');
  63. $ret = trim($ret);
  64. if (!preg_match("/^[a-zA-Z.-_]*$/", $ret, $matches)) {
  65. $ret = null;
  66. }
  67. }
  68. break;
  69. case 'int':
  70. case 'integer':
  71. if (is_scalar($from)) {
  72. $ret = $from;
  73. settype($ret, $type);
  74. }
  75. break;
  76. case 'float':
  77. case 'double':
  78. if (is_scalar($from)) {
  79. $ret = str_replace(',', '.', $from);
  80. settype($ret, $type);
  81. }
  82. break;
  83. case 'object':
  84. case 'array':
  85. if (is_scalar($from) || is_array($from) || is_object($from)) {
  86. $ret = $from;
  87. settype($ret, $type);
  88. }
  89. break;
  90. case 'int_array':
  91. if (is_scalar($from) || is_array($from) || is_object($from)) {
  92. $ret = array();
  93. $arr = $from;
  94. settype($arr, 'array');
  95. foreach ($arr as $v) {
  96. $v = V::convert($v, 'int');
  97. $ret[] = $v;
  98. }
  99. }
  100. break;
  101. case 'uint_array':// uncigned int array
  102. if (is_scalar($from) || is_array($from) || is_object($from)) {
  103. $ret = array();
  104. $arr = $from;
  105. settype($arr, 'array');
  106. foreach ($arr as $v) {
  107. $v = V::convert($v, 'int');
  108. if ($v <= 0) continue;
  109. $ret[] = $v;
  110. }
  111. }
  112. break;
  113. case 'float_array':// uncigned int array
  114. if (is_scalar($from) || is_array($from) || is_object($from)) {
  115. $ret = array();
  116. $arr = $from;
  117. settype($arr, 'array');
  118. foreach ($arr as $v) {
  119. $v = V::convert($v, 'float');
  120. $ret[] = $v;
  121. }
  122. }
  123. break;
  124. default:
  125. $fun = 'func_type_convert_'.$type;
  126. if (function_exists($fun)) {
  127. $ret = $fun($from);
  128. }
  129. break;
  130. }
  131. return $ret;
  132. }
  133. /**
  134. * Merge the contents of two objects/array.
  135. *
  136. * array V::extend(mixed $defaults, mixed $params);
  137. * @see http://api.jquery.com/jQuery.extend/
  138. * is_scalar($from) - return TRUE if int,float,string,bool, FALSE if array,object,resource, ...
  139. */
  140. public static function extend($defaults, $params) {
  141. $ret = array();
  142. $d = (is_array($defaults))? $defaults : (array)$defaults;
  143. $p = (is_array($params))? $params : (array)$params;
  144. foreach ($d as $k => $v) {
  145. $ret[$k] = $v;
  146. }
  147. foreach ($p as $k => $v) {
  148. if (array_key_exists($k, $ret) && (is_array($ret[$k]) || is_object($ret[$k])) && (is_array($v) || is_object($v))) {
  149. $ret[$k] = V::extend($ret[$k], $v);
  150. } else {
  151. $ret[$k] = $v;
  152. }
  153. }
  154. return $ret;
  155. }
  156. public static function json_encode_latin2($o, $force_object = false) {
  157. if ($o === '') {
  158. return '""';
  159. }
  160. else if (!$o) {
  161. return 'null';
  162. }
  163. else if (is_array($o)) {
  164. $arr = '';
  165. if ($force_object) {
  166. foreach ($o as $k => $v) {
  167. $arr[] = '"'.$k.'":'.V::json_encode_latin2($v, $force_object);
  168. }
  169. return '{'.implode(',',$arr).'}';
  170. }
  171. else {
  172. foreach ($o as $k => $v) {
  173. if (is_string($k)) $arr[] = '"'.$k.'":'.V::json_encode_latin2($v, $force_object);
  174. else $arr[] = V::json_encode_latin2($v);
  175. }
  176. return '['.implode(',',$arr).']';
  177. }
  178. }
  179. else if (is_object($o)) {
  180. $arr = '';
  181. foreach (get_object_vars($o) as $k => $v) {
  182. $arr[] = '"'.$k.'":'.V::json_encode_latin2($v, $force_object);
  183. }
  184. return '{'.implode(',',$arr).'}';
  185. }
  186. else if (is_string($o)) {
  187. return '"'.addslashes(str_replace(array("\n","\r"), array('\n',''), $o)).'"';
  188. }
  189. else if (is_numeric($o)) {
  190. return ''.$o.'';
  191. }
  192. else if (is_bool($o)) {
  193. return ''.(($o)? 'true' : 'false').'';
  194. }
  195. }
  196. public static function copy($o) {
  197. $null = null;
  198. if (!$o) {
  199. return $null;
  200. }
  201. else if (is_array($o)) {
  202. $ret = array();
  203. foreach ($o as $k => $v) {
  204. $ret[$k] = $v;
  205. }
  206. return $ret;
  207. }
  208. else if (is_object($o)) {
  209. $ret = new stdClass();
  210. foreach (get_object_vars($o) as $k => $v) {
  211. $ret->$k = $v;
  212. }
  213. return $ret;
  214. }
  215. else {
  216. $ret = $o;
  217. return $ret;
  218. }
  219. }
  220. public static function make_link($prefix = '', $params = array()) {
  221. $ret = '';
  222. if ($prefix) {
  223. $ret = $prefix;
  224. }
  225. if (!empty($params)) {
  226. $ret_arr = array();
  227. foreach ($params as $k => $v) {
  228. $ret_arr[] = $k . "=" . $v;
  229. }
  230. $ret .= "?" . implode("&", $ret_arr);
  231. }
  232. return $ret;
  233. }
  234. public static function strShort($label, $maxLength = 10, $suffix = ' ...') {
  235. if (strlen($label) > $maxLength) {
  236. $pos = strpos($label, ' - ');
  237. if ($pos > $maxLength || $pos < 5) {
  238. $label = substr($label, 0, $pos) . $suffix;
  239. } else {
  240. $label = substr($label, 0, $pos);
  241. }
  242. }
  243. return $label;
  244. }
  245. public static function strShortUtf8($label, $maxLength = 10, $suffix = ' ...') {
  246. if (mb_strlen($label, 'utf-8') > $maxLength) {
  247. $pos = mb_strpos($label, ' - ', 0, 'utf-8');
  248. if ($pos > $maxLength || $pos < 5) {
  249. $label = mb_substr($label, 0, $maxLength, 'utf-8') . $suffix;
  250. } else {
  251. $label = mb_substr($label, 0, $pos, 'utf-8');
  252. }
  253. }
  254. return $label;
  255. }
  256. public static function filter($array, $filterCallback) {
  257. if (!is_callable($filterCallback)) {
  258. throw new Exception("callback is not callable '" . ((is_array($filterCallback))? implode('.', $filterCallback) : $filterCallback) . "'");
  259. }
  260. return array_filter($array, $filterCallback);
  261. }
  262. public static function filterNotEmpty($value) {
  263. return !empty($value);
  264. }
  265. public static function filterInteger($value) {// An integer or string with integer value
  266. if (is_int($value)) {
  267. return true;
  268. } else if (is_string($value)) {
  269. if ((string)(int)$value === $value) {
  270. return true;
  271. }
  272. }
  273. return false;
  274. }
  275. public static function filterNegativeInteger($value) {// An integer containing only negative values (..,-2,-1)
  276. if (V::filterInteger($value)) {
  277. if (intval($value) < 0) {
  278. return true;
  279. }
  280. }
  281. return false;
  282. }
  283. public static function filterNonNegativeInteger($value) {// An integer containing only non-negative values (0,1,2,..)
  284. if (V::filterInteger($value)) {
  285. if (intval($value) >= 0) {
  286. return true;
  287. }
  288. }
  289. return false;
  290. }
  291. public static function filterNonPositiveInteger($value) {// An integer containing only non-positive values (..,-2,-1,0)
  292. if (V::filterInteger($value)) {
  293. if (intval($value) <= 0) {
  294. return true;
  295. }
  296. }
  297. return false;
  298. }
  299. public static function filterPositiveInteger($value) {// An integer containing only positive values (1,2,..)
  300. if (V::filterInteger($value)) {
  301. if (intval($value) > 0) {
  302. return true;
  303. }
  304. }
  305. return false;
  306. }
  307. public static function validate($argName, $args, $params) {
  308. //$what = V::validate('what', $args, array('type'=>'word', 'not_empty'=>true, 'max_length'=>'255', 'values'=>$when_values));
  309. $argValue = V::get($argName, null, $args);
  310. $fldLabel = V::get('fld_label', $argName, $params);
  311. if (array_key_exists('not_empty', $params) && true == $params['not_empty']) {
  312. if (!array_key_exists($argName, $args) || empty($args[$argName])) throw new Exception("Field {$fldLabel} not set.");
  313. }
  314. $params['fld_label'] = $fldLabel;
  315. return V::validateValue($argValue, $params);
  316. }
  317. public static function validateValue($value, $params) {
  318. $fldLabel = V::get('fld_label', '', $params);
  319. $maxLength = V::get('max_length', 0, $params);
  320. if ($maxLength > 0) {
  321. if (strlen($value) > $maxLength) throw new Exception("'{$fldLabel}' cannot be longer then {$maxLength}.");
  322. }
  323. $allowedValues = V::get('values', null, $params);
  324. if (is_array($allowedValues) && !empty($allowedValues)) {
  325. if (!in_array($value, $allowedValues)) throw new Exception("'{$fldLabel}' value is not allowed");
  326. }
  327. $type = V::get('type', null, $params);
  328. if ($type != null) {
  329. if ('word' == $type) {
  330. if (!is_scalar($value) || !preg_match('/^[a-zA-Z_-]*$/', $value)) throw new Exception("required type '{$type}' ({$fldLabel})");
  331. } else {
  332. throw new Exception("Unimplemented type to validate: '{$type}'");
  333. }
  334. }
  335. if (array_key_exists('equal', $params)) {
  336. if ($value != $params['equal']) throw new Exception(V::get('error_msg_equal', "'{$fldLabel}' must be equal to '{$params['equal']}'", $params));
  337. }
  338. if (array_key_exists('equalStrict', $params)) {
  339. if ($value !== $params['equalStrict']) throw new Exception(V::get('error_msg_equalStrict', "'{$fldLabel}' must be strict equal to '{$params['equal']}'", $params));
  340. }
  341. return $value;
  342. }
  343. public static function exec($cmd, &$out, &$ret) {
  344. $out = null;
  345. $ret = null;
  346. exec($cmd, $out, $ret);
  347. return $ret;
  348. }
  349. public static function execRemote($host, $login, $password, $command, &$out, &$ret, $port = 22) {
  350. $out = null;
  351. $ret = null;
  352. $pass = $password;
  353. $pass = str_replace('!', '\!', $pass);
  354. $sshPort = (22 != $port)? "-p {$port}" : '';
  355. $cmd = '/opt/local/bin/sshpass -p ' . $pass . ' ssh -o StrictHostKeyChecking=no -o ConnectTimeout=99999 ' . $sshPort . ' ' . $login . '@' . $host . ' -t <<EOF
  356. declare PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/local/bin:/opt/local/lib/mysql55/bin/:/Applications/Server.app/Contents/ServerRoot/usr/sbin/
  357. '.$command.'
  358. EOF';
  359. exec($cmd, $out, $ret);
  360. return $ret;
  361. }
  362. public static function execRootRemote($host, $login, $password, $command, &$out, &$ret, $port = 22) {
  363. $out = null;
  364. $ret = null;
  365. $pass = $password;
  366. $pass = str_replace('!', '\!', $pass);
  367. $sshPort = (22 != $port)? "-p {$port}" : '';
  368. $cmd = '/opt/local/bin/sshpass -p ' . $pass . ' ssh -o StrictHostKeyChecking=no -o ConnectTimeout=99999 ' . $sshPort . ' ' . $login . '@' . $host . ' -t <<EOF
  369. sudo -n su -
  370. declare PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/local/bin:/opt/local/lib/mysql55/bin/:/Applications/Server.app/Contents/ServerRoot/usr/sbin/
  371. '.$command.'
  372. EOF';
  373. exec($cmd, $out, $ret);
  374. return $ret;
  375. }
  376. }