PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/inc/public.functions.inc.php

https://github.com/straccio/Vault
PHP | 532 lines | 410 code | 46 blank | 76 comment | 102 complexity | 47058831897ac96bbd4ec062fa8a4840 MD5 | raw file
  1. <?php
  2. function direct_getDirect($className,$appname){
  3. if(is_array($className)){
  4. $classes=$className;
  5. }else{
  6. $appname=$className;
  7. $classes=array($className);
  8. }
  9. session_start();
  10. require_once('inc/ExtDirect/API.php');
  11. require_once('inc/ExtDirect/CacheProvider.php');
  12. $cache = new ExtDirect_CacheProvider('tmp/cache/'.$_SESSION["usr"] . '_' . $appname . 'api_cache.txt');
  13. $api = new ExtDirect_API();
  14. $api->setRouterUrl('?op=getRouter'); // default
  15. $api->setCacheProvider($cache);
  16. $api->setNamespace('Vault');
  17. $api->setDescriptor('Vault.'.$appname. '._APIDesc');
  18. $api->setDefaults(array(
  19. 'autoInclude' => false,
  20. 'basePath' => 'inc'
  21. ));
  22. foreach($classes as $className){
  23. if(strlen($className)<=3){
  24. $className = strtoupper($className);
  25. }else{
  26. $className = ucwords(strtolower($className));
  27. }
  28. // Include ExtDirect PHP Helpers
  29. $api->add(
  30. array(
  31. $className => array('prefix' => 'Direct_')
  32. )
  33. );
  34. }
  35. $api->output();
  36. $_SESSION['ext-direct-state'] = $api->getState();
  37. }
  38. function direct_getRouter($className,$appname){
  39. if(is_array($className)){
  40. $classes=$className;
  41. }else{
  42. $appname=$className;
  43. $classes=array($className);
  44. }
  45. session_start();
  46. require_once('inc/ExtDirect/API.php');
  47. require_once('inc/ExtDirect/Router.php');
  48. if(!isset($_SESSION['ext-direct-state'])) {
  49. direct_getDirect($appname);
  50. }
  51. $api = new ExtDirect_API();
  52. $api->setState($_SESSION['ext-direct-state']);
  53. $router = new ExtDirect_Router($api);
  54. $router->dispatch();
  55. $router->getResponse(true); // true to print the response instantly
  56. }
  57. function getmicrotime(){
  58. list($usec, $sec) = explode(" ",microtime());
  59. return ((float)$usec + (float)$sec);
  60. }
  61. function iif($if,$true,$false){
  62. if($if){
  63. return $true;
  64. }else{
  65. return $false;
  66. }
  67. }
  68. function removeln($str){
  69. return str_replace("\n","",$str);
  70. }
  71. function clientGetVar($name){
  72. global $render;
  73. if($render=="html"){
  74. return $_COOKIE[$name];
  75. }
  76. }
  77. function clientSetVar($name,$value){
  78. global $render;
  79. if($render=="html"){
  80. setcookie($name,$value,null,null,$HTTP_SERVER_VARS['HTTP_HOST'],true);
  81. }
  82. }
  83. function _getopt ( ) {
  84. /* _getopt(): Ver. 1.2 2008/08/19
  85. My page: http://www.ntu.beautifulworldco.com/weblog/?p=526
  86. Usage: _getopt ( [$flag,] $short_option [, $long_option] );
  87. Note that another function split_para() is required, which can be found in the same
  88. page.
  89. _getopt() fully simulates getopt() which is described at
  90. http://us.php.net/manual/en/function.getopt.php , including long options for PHP
  91. version under 5.3.0. (Prior to 5.3.0, long options was only available on few systems)
  92. Besides legacy usage of getopt(), I also added a new option to manipulate your own
  93. argument lists instead of those from command lines. This new option can be a string
  94. or an array such as
  95. $flag = "-f value_f -ab --required 9 --optional=PK --option -v test -k";
  96. or
  97. $flag = array ( "-f", "value_f", "-ab", "--required", "9", "--optional=PK", "--option" );
  98. So there are four ways to work with _getopt(),
  99. 1. _getopt ( $short_option );
  100. it's a legacy usage, same as getopt ( $short_option ).
  101. 2. _getopt ( $short_option, $long_option );
  102. it's a legacy usage, same as getopt ( $short_option, $long_option ).
  103. 3. _getopt ( $flag, $short_option );
  104. use your own argument lists instead of command line arguments.
  105. 4. _getopt ( $flag, $short_option, $long_option );
  106. use your own argument lists instead of command line arguments.
  107. */
  108. if ( func_num_args() == 1 ) {
  109. $flag = $flag_array = $GLOBALS['argv'];
  110. $short_option = func_get_arg ( 0 );
  111. $long_option = array ();
  112. } else if ( func_num_args() == 2 ) {
  113. if ( is_array ( func_get_arg ( 1 ) ) ) {
  114. $flag = $GLOBALS['argv'];
  115. $short_option = func_get_arg ( 0 );
  116. $long_option = func_get_arg ( 1 );
  117. } else {
  118. $flag = func_get_arg ( 0 );
  119. $short_option = func_get_arg ( 1 );
  120. $long_option = array ();
  121. }
  122. } else if ( func_num_args() == 3 ) {
  123. $flag = func_get_arg ( 0 );
  124. $short_option = func_get_arg ( 1 );
  125. $long_option = func_get_arg ( 2 );
  126. } else {
  127. exit ( "wrong options\n" );
  128. }
  129. $short_option = trim ( $short_option );
  130. $short_no_value = array();
  131. $short_required_value = array();
  132. $short_optional_value = array();
  133. $long_no_value = array();
  134. $long_required_value = array();
  135. $long_optional_value = array();
  136. $options = array();
  137. for ( $i = 0; $i < strlen ( $short_option ); ) {
  138. if ( $short_option{$i} != ":" ) {
  139. if ( $short_option{$i+1} != ":" ) {
  140. $short_no_value[] = $short_option{$i};
  141. $i++;
  142. continue;
  143. } else if ( $short_option{$i+1} == ":" && $short_option{$i+2} != ":" ) {
  144. $short_required_value[] = $short_option{$i};
  145. $i += 2;
  146. continue;
  147. } else if ( $short_option{$i+1} == ":" && $short_option{$i+2} == ":" ) {
  148. $short_optional_value[] = $short_option{$i};
  149. $i += 3;
  150. continue;
  151. }
  152. } else {
  153. continue;
  154. }
  155. }
  156. foreach ( $long_option as $a ) {
  157. if ( substr( $a, -2 ) == "::" ) {
  158. $long_optional_value[] = substr( $a, 0, -2);
  159. continue;
  160. } else if ( substr( $a, -1 ) == ":" ) {
  161. $long_required_value[] = substr( $a, 0, -1 );
  162. continue;
  163. } else {
  164. $long_no_value[] = $a;
  165. continue;
  166. }
  167. }
  168. if ( is_array ( $flag ) )
  169. $flag_array = $flag;
  170. else {
  171. $flag = "- $flag";
  172. $flag_array = split_para( $flag );
  173. }
  174. for ( $i = 0; $i < count( $flag_array ); ) {
  175. if ( $i >= count ( $flag_array ) )
  176. break;
  177. if ( ! $flag_array[$i] || $flag_array[$i] == "-" ) {
  178. $i++;
  179. continue;
  180. }
  181. if ( $flag_array[$i]{0} != "-" ) {
  182. $i++;
  183. continue;
  184. }
  185. if ( substr( $flag_array[$i], 0, 2 ) == "--" ) {
  186. if (strpos($flag_array[$i], '=') != false) {
  187. list($key, $value) = explode('=', substr($flag_array[$i], 2), 2);
  188. if ( in_array ( $key, $long_required_value ) || in_array ( $key, $long_optional_value ) )
  189. $options[$key][] = $value;
  190. $i++;
  191. continue;
  192. }
  193. if (strpos($flag_array[$i], '=') == false) {
  194. $key = substr( $flag_array[$i], 2 );
  195. if ( in_array( substr( $flag_array[$i], 2 ), $long_required_value ) ) {
  196. $options[$key][] = $flag_array[$i+1];
  197. $i += 2;
  198. continue;
  199. } else if ( in_array( substr( $flag_array[$i], 2 ), $long_optional_value ) ) {
  200. if ( $flag_array[$i+1] != "" && $flag_array[$i+1]{0} != "-" ) {
  201. $options[$key][] = $flag_array[$i+1];
  202. $i += 2;
  203. } else {
  204. $options[$key][] = FALSE;
  205. $i ++;
  206. }
  207. continue;
  208. } else if ( in_array( substr( $flag_array[$i], 2 ), $long_no_value ) ) {
  209. $options[$key][] = FALSE;
  210. $i++;
  211. continue;
  212. } else {
  213. $i++;
  214. continue;
  215. }
  216. }
  217. } else if ( $flag_array[$i]{0} == "-" && $flag_array[$i]{1} != "-" ) {
  218. for ( $j=1; $j < strlen($flag_array[$i]); $j++ ) {
  219. if ( in_array( $flag_array[$i]{$j}, $short_required_value ) || in_array( $flag_array[$i]{$j}, $short_optional_value )) {
  220. if ( $j == strlen($flag_array[$i]) - 1 ) {
  221. if ( in_array( $flag_array[$i]{$j}, $short_required_value ) ) {
  222. $options[$flag_array[$i]{$j}][] = $flag_array[$i+1];
  223. $i += 2;
  224. } else if ( in_array( $flag_array[$i]{$j}, $short_optional_value ) && $flag_array[$i+1] != "" && $flag_array[$i+1]{0} != "-" ) {
  225. $options[$flag_array[$i]{$j}][] = $flag_array[$i+1];
  226. $i += 2;
  227. } else {
  228. $options[$flag_array[$i]{$j}][] = FALSE;
  229. $i ++;
  230. }
  231. $plus_i = 0;
  232. break;
  233. } else {
  234. $options[$flag_array[$i]{$j}][] = substr ( $flag_array[$i], $j + 1 );
  235. $i ++;
  236. $plus_i = 0;
  237. break;
  238. }
  239. } else if ( in_array ( $flag_array[$i]{$j}, $short_no_value ) ) {
  240. $options[$flag_array[$i]{$j}][] = FALSE;
  241. $plus_i = 1;
  242. continue;
  243. } else {
  244. $plus_i = 1;
  245. break;
  246. }
  247. }
  248. $i += $plus_i;
  249. continue;
  250. }
  251. $i++;
  252. continue;
  253. }
  254. foreach ( $options as $key => $value ) {
  255. if ( count ( $value ) == 1 ) {
  256. $options[ $key ] = $value[0];
  257. }
  258. }
  259. return $options;
  260. }
  261. function split_para ( $pattern ) {
  262. /* split_para() version 1.0 2008/08/19
  263. My page: http://www.ntu.beautifulworldco.com/weblog/?p=526
  264. This function is to parse parameters and split them into smaller pieces.
  265. preg_split() does similar thing but in our function, besides "space", we
  266. also take the three symbols " (double quote), '(single quote),
  267. and \ (backslash) into consideration because things in a pair of " or '
  268. should be grouped together.
  269. As an example, this parameter list
  270. -f "test 2" -ab --required "t\"est 1" --optional="te'st 3" --option -v 'test 4'
  271. will be splited into
  272. -f
  273. t"est 2
  274. -ab
  275. --required
  276. test 1
  277. --optional=te'st 3
  278. --option
  279. -v
  280. test 4
  281. see the code below,
  282. $pattern = "-f \"test 2\" -ab --required \"t\\\"est 1\" --optional=\"te'st 3\" --option -v 'test 4'";
  283. $result = split_para( $pattern );
  284. echo "ORIGINAL PATTERN: $pattern\n\n";
  285. var_dump( $result );
  286. */
  287. $begin=0;
  288. $backslash = 0;
  289. $quote = "";
  290. $quote_mark = array();
  291. $result = array();
  292. $pattern = trim ( $pattern );
  293. for ( $end = 0; $end < strlen ( $pattern ) ; ) {
  294. if ( ! in_array ( $pattern{$end}, array ( " ", "\"", "'", "\\" ) ) ) {
  295. $backslash = 0;
  296. $end ++;
  297. continue;
  298. }
  299. if ( $pattern{$end} == "\\" ) {
  300. $backslash++;
  301. $end ++;
  302. continue;
  303. } else if ( $pattern{$end} == "\"" ) {
  304. if ( $backslash % 2 == 1 || $quote == "'" ) {
  305. $backslash = 0;
  306. $end ++;
  307. continue;
  308. }
  309. if ( $quote == "" ) {
  310. $quote_mark[] = $end - $begin;
  311. $quote = "\"";
  312. } else if ( $quote == "\"" ) {
  313. $quote_mark[] = $end - $begin;
  314. $quote = "";
  315. }
  316. $backslash = 0;
  317. $end ++;
  318. continue;
  319. } else if ( $pattern{$end} == "'" ) {
  320. if ( $backslash % 2 == 1 || $quote == "\"" ) {
  321. $backslash = 0;
  322. $end ++;
  323. continue;
  324. }
  325. if ( $quote == "" ) {
  326. $quote_mark[] = $end - $begin;
  327. $quote = "'";
  328. } else if ( $quote == "'" ) {
  329. $quote_mark[] = $end - $begin;
  330. $quote = "";
  331. }
  332. $backslash = 0;
  333. $end ++;
  334. continue;
  335. } else if ( $pattern{$end} == " " ) {
  336. if ( $quote != "" ) {
  337. $backslash = 0;
  338. $end ++;
  339. continue;
  340. } else {
  341. $backslash = 0;
  342. $cand = substr( $pattern, $begin, $end-$begin );
  343. for ( $j = 0; $j < strlen ( $cand ); $j ++ ) {
  344. if ( in_array ( $j, $quote_mark ) )
  345. continue;
  346. $cand1 .= $cand{$j};
  347. }
  348. if ( $cand1 ) {
  349. eval( "\$cand1 = \"$cand1\";" );
  350. $result[] = $cand1;
  351. }
  352. $quote_mark = array();
  353. $cand1 = "";
  354. $end ++;
  355. $begin = $end;
  356. continue;
  357. }
  358. }
  359. }
  360. $cand = substr( $pattern, $begin, $end-$begin );
  361. for ( $j = 0; $j < strlen ( $cand ); $j ++ ) {
  362. if ( in_array ( $j, $quote_mark ) )
  363. continue;
  364. $cand1 .= $cand{$j};
  365. }
  366. eval( "\$cand1 = \"$cand1\";" );
  367. if ( $cand1 )
  368. $result[] = $cand1;
  369. return $result;
  370. }
  371. function array_mix() {
  372. $array = array();
  373. $arrays = func_get_args();
  374. foreach($arrays as $array_i) if(is_array($array_i))
  375. $array = array_mixer($array, $array_i);
  376. return $array;
  377. }
  378. function array_mix_recursive() {
  379. $array = array();
  380. $arrays = func_get_args();
  381. foreach($arrays as $array_i) if(is_array($array_i))
  382. $array = array_mixer($array, $array_i, true);
  383. return $array;
  384. }
  385. function array_mixer($array_o, $array_i, $recursive=false) {
  386. foreach($array_i as $k => $v) {
  387. if(! isset($array_o[$k])) {
  388. $array_o[$k] = $v;
  389. } else {
  390. if(is_array($array_o[$k])) {
  391. if(is_array($v)) {
  392. if($recursive) $array_o[$k] = array_mixer($array_o[$k], $v);
  393. else $array_o[$k] = $v;
  394. } else $array_o[$k][] = $v;
  395. } else {
  396. if(! isset($array_o[$k])) {
  397. $array_o[$k] = $v;
  398. } else {
  399. $array_o[$k] = array($array_o[$k]);
  400. $array_o[$k][] = $v;
  401. }
  402. }
  403. }
  404. }
  405. return $array_o;
  406. }
  407. function mime_header_encode($string) {
  408. if (preg_match('/[^\x20-\x7E]/', $string)) {
  409. $chunk_size = 47; // floor((75 - strlen("=?UTF-8?B??=")) * 0.75);
  410. $len = strlen($string);
  411. $output = '';
  412. while ($len > 0) {
  413. $chunk = truncate_utf8($string, $chunk_size);
  414. $output .= ' =?UTF-8?B?'. base64_encode($chunk) ."?=\n";
  415. $c = strlen($chunk);
  416. $string = substr($string, $c);
  417. $len -= $c;
  418. }
  419. return trim($output);
  420. }
  421. return $string;
  422. }
  423. //if (!function_exists('str_getcsv')) {
  424. function str_rowgetcsv($input, $delimiter=',', $enclosure='"', $escape=null, $eol=null) {
  425. $temp=fopen("php://memory", "rw");
  426. fwrite($temp, $input);
  427. fseek($temp, 0);
  428. $r = array();
  429. while (($data = fgetcsv($temp, 4096, $delimiter, $enclosure)) !== false) {
  430. $r[] = $data;
  431. }
  432. fclose($temp);
  433. return $r;
  434. }
  435. //}
  436. ?>