PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/mysite/thirdparty/fig2/package.gmi.php

https://bitbucket.org/mwuits/mockup_nachz
PHP | 628 lines | 466 code | 67 blank | 95 comment | 44 complexity | ca42997644a026db640b044f1e595986 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, GPL-2.0, AGPL-1.0, LGPL-2.1, LGPL-2.0, LGPL-3.0, GPL-3.0
  1. <?php
  2. /**
  3. *
  4. * PHP Get Method Command-like Interface
  5. *
  6. * Copyright (C) 2006 Matsuda Shota
  7. * http://sgssweb.com/
  8. * admin@sgssweb.com
  9. *
  10. * ------------------------------------------------------------------------
  11. *
  12. * 2006-4-20 First release.
  13. *
  14. */
  15. /*
  16. Syntax:
  17. Function:
  18. function_name argment1,argment2,...;
  19. Constructor:
  20. {object_name:argment1,argment2,...}
  21. Variable:
  22. {variable_name}
  23. Property of variable:
  24. {variable_name.property_name}
  25. String:
  26. 'characters'
  27. "characters"
  28. Number:
  29. integer_number
  30. floating_number
  31. 0xhexadecimal_number
  32. 0Xhexadecimal_number
  33. #hexadecimal_number
  34. 0bbinary_number
  35. 0Bbinary_number
  36. Constant:
  37. constant_value
  38. */
  39. class GMIExecution
  40. {
  41. var $nameTable = array(); // array
  42. var $commands = array(); // array
  43. var $pluggableSet = null; // GMIPluggableSet
  44. var $isDebugMode = false; // boolean
  45. function GMIExecution() {
  46. error_reporting(E_ERROR|E_WARNING|E_PARSE);
  47. }
  48. // void setPluggableSet(GMIPluggableSet pluggableSet)
  49. function setPluggableSet(&$pluggableSet) {
  50. $this->pluggableSet =& $pluggableSet;
  51. $this->prepareVariables($pluggableSet->getVariables());
  52. }
  53. // void prepareVariables(array anArray)
  54. function prepareVariables($anArray = array()) {
  55. reset($anArray);
  56. while (list($name, $exp) = each($anArray)) {
  57. // split expression into variable name.
  58. $matches = array();
  59. preg_match_all('/^\s*([\w\d]+)\s*$/', $name, $matches);
  60. // stop if expression is not valid
  61. if (count($matches[0]) == 0) {
  62. continue;
  63. }
  64. $name = $matches[1][0];
  65. $this->nameTable[$name] = $this->createValue($exp);
  66. }
  67. }
  68. // GMIValue getVariable(string name)
  69. function getVariable($name) {
  70. return isset($this->nameTable[$name])? $this->nameTable[$name]
  71. : null;
  72. }
  73. // void setDebugMode(boolean isDebugMode) {
  74. function setDebugMode($isDebugMode) {
  75. error_reporting(E_ERROR|E_WARNING|E_PARSE);
  76. $this->isDebugMode = $isDebugMode;
  77. }
  78. // GMIValue createValue(string exp)
  79. function createValue($exp) {
  80. $exp = trim($exp);
  81. if (preg_match('/^\{\s*\w+(?:\w+\.\w+)?\s*\}$/', $exp)) {
  82. $value = new GMIVariable($this, $exp);
  83. }
  84. else if (preg_match('/^\{\s*\w+\:.*\}$/', $exp)) {
  85. $value = new GMIConstructor($this, $exp);
  86. }
  87. else if (preg_match('/^(?:(?:[-+]?\d*\.?\d+)|^(?:0[bB](?:[01]+))|^(?:(?:0[xX]|\#)(?:[\da-fA-F]+)))$/', $exp)) {
  88. $value = new GMINumber($this, $exp);
  89. }
  90. else if (preg_match('/^[a-zA-Z][\w\d\-]+$/', $exp)) {
  91. $value = new GMIConstant($this, $exp);
  92. }
  93. else if (preg_match('/^(?:\".*\"|\\\'.*\\\')$/', $exp)) {
  94. if (substr($exp, 0, 1) == "'") {
  95. $exp = preg_replace('/\\\\\'/', "'", substr($exp, 1, -1));
  96. }
  97. else {
  98. $exp = preg_replace('/\\\\"/', "\"", substr($exp, 1, -1));
  99. }
  100. $value = new GMIString($this, preg_replace('/(?<!\\\\)\\\\n/', "\n", $exp));
  101. }
  102. else {
  103. $value = new GMIString($this, preg_replace('/(?<!\\\\)\\\\n/', "\n", $exp));
  104. }
  105. return $value;
  106. }
  107. // boolean validateArguments(array types, array args);
  108. function validateArguments($types, &$args) {
  109. if (count($types) != count($args)) {
  110. return false;
  111. }
  112. reset($types);
  113. while (list($i) = each($types)) {
  114. if ($types[$i] === null) {
  115. break;
  116. }
  117. // constant
  118. if (is_array($types[$i])) {
  119. if ($args[$i]->getType() != 'constant') {
  120. return false;
  121. }
  122. reset($types[$i]);
  123. while (list($n) = each($types[$i])) {
  124. if ($args[$i]->getValue() == $types[$i][$n]) {
  125. break;
  126. }
  127. if ($n == count($types[$i])) {
  128. return false;
  129. }
  130. }
  131. }
  132. // value
  133. else if ($types[$i] != $args[$i]->getType()) {
  134. return false;
  135. }
  136. }
  137. return true;
  138. }
  139. // array extractArguments(int startIndex, int endIndex, array args)
  140. function extractArguments($startIndex, $endIndex, &$args) {
  141. $newArgs = array();
  142. for ($i = $startIndex; $i <= $endIndex; $i ++) {
  143. $newArgs[$i - $startIndex] =& $args[$i];
  144. }
  145. return $newArgs;
  146. }
  147. // mixed construct(string class, array args)
  148. function construct($class, &$args) {
  149. return null;
  150. }
  151. // GMIValue getProperty(GMIVariable variable, string property)
  152. function getProperty(&$variable, $property) {
  153. return null;
  154. }
  155. // void command(string name, array args)
  156. function command($name, &$args) {
  157. switch ($name) {
  158. case "debug":
  159. $this->setDebugMode(true);
  160. break;
  161. }
  162. }
  163. // void execute(string exp)
  164. function execute($exp = "") {
  165. if ($this->pluggableSet !== null) {
  166. $exp = $this->pluggableSet->getExpression();
  167. }
  168. // add slash before ";" in such quoted value as "...", '...' or {...}.
  169. $lines = preg_replace_callback(
  170. '/(?<!\\\)\\\'(?:\\\\\'|[^\'])*(?:(?<!\\\)\\\')|'.
  171. '(?<!\\\)\"(?:\\\\"|[^"])*(?:(?<!\\\)\")|'.
  172. '(?<=\{)(?:[^{}]*(?:\{[^{}]*\})*[^{}]*)*(?=\})/',
  173. create_function('$matches', 'return preg_replace(\'/([;])/\', "\\\\\\\$1", $matches[0]);'),
  174. $exp
  175. );
  176. // split expression into an array by unexcaped ";".
  177. $lines = preg_split('/\s*(?<!\\\)\;\s*/', $lines);
  178. $index = 0;
  179. reset($lines);
  180. while (list($i) = each($lines)) {
  181. // skip empty line.
  182. if ($lines[$i] == "") {
  183. continue;
  184. }
  185. // remove slashes inserted above.
  186. $lines[$i] = preg_replace_callback(
  187. '/(?<!\\\)\\\'(?:\\\\\'|[^\'])*(?:(?<!\\\)\\\')|'.
  188. '(?<!\\\)\"(?:\\\\"|[^"])*(?:(?<!\\\)\")|'.
  189. '(?<=\{)(?:[^{}]*(?:\{[^{}]*\})*[^{}]*)*(?=\})/',
  190. create_function('$matches', 'return preg_replace(\'/\\\\\([;])/\', "$1", $matches[0]);'),
  191. $lines[$i]
  192. );
  193. // convert and store.
  194. $this->commands[$index] = new GMICommand($this, trim($lines[$i]));
  195. $this->commands[$index]->execute();
  196. $index ++;
  197. }
  198. }
  199. // void debug()
  200. function debug() {
  201. echo "<html><body style=\"margin:0;padding:0;font-size:small;\">";
  202. echo "<br />&nbsp;&nbsp;<strong>Variables</strong><br />";
  203. echo "<table style=\"border-collapse:collapse;border:solid 1px #dedede;font-size:small;\">";
  204. echo "<tr style=\"background:#cccccc;border: solid 1px #aaaaaa;\">";
  205. echo "<td style=\"color:white;border-right: solid 1px #aaaaaa;padding:0 10px;white-space:nowrap;\">Name</td>";
  206. echo "<td style=\"color:white;border-right: solid 1px #aaaaaa;padding:0 10px;white-space:nowrap;text-align:center;\">Type</td>";
  207. echo "<td style=\"color:white;border-right: solid 1px #aaaaaa;padding:0 10px;white-space:nowrap;\">Value (Structured)</td>";
  208. echo "<td style=\"color:white;border-right: solid 1px #aaaaaa;padding:0 10px;white-space:nowrap;\">Expression</td>";
  209. echo "</tr>";
  210. reset($this->nameTable);
  211. $i = 0;
  212. while (list($name) = each($this->nameTable)) {
  213. echo "<tr>";
  214. echo "<td style=\"background:".($i%2==0? "#edf3fe":"#ffffff").";border-right: solid 1px ".($i%2==0? "#ced4dd":"#dedede").";padding:2px 10px;white-space:nowrap;vertical-align:top;\"><strong>$name</strong></td>";
  215. echo "<td style=\"background:".($i%2==0? "#e6ebf6":"#f7f7f7").";border-right: solid 1px ".($i%2==0? "#ced4dd":"#dedede").";padding:2px 10px;white-space:nowrap;text-align:center;vertical-align:top;\">".$this->nameTable[$name]->getType()."</td>";
  216. echo "<td style=\"background:".($i%2==0? "#edf3fe":"#ffffff").";border-right: solid 1px ".($i%2==0? "#ced4dd":"#dedede").";padding:2px 10px;color:gray;\">";
  217. ob_start();
  218. var_dump($this->nameTable[$name]->getValue());
  219. echo preg_replace('/\s\s/m', "&nbsp;&nbsp;&nbsp;&nbsp;", nl2br(ob_get_clean()));
  220. echo "</td>";
  221. echo "<td style=\"background:".($i%2==0? "#e6ebf6":"#f7f7f7").";border-right: solid 1px ".($i%2==0? "#ced4dd":"#dedede").";padding:2px 10px;\">".$this->nameTable[$name]->expression."</td>";
  222. echo "</tr>";
  223. $i ++;
  224. }
  225. echo "</table>";
  226. echo "<br />&nbsp;&nbsp;<strong>Commands</strong><br />";
  227. echo "<table style=\"border-collapse:collapse;border:solid 1px #dedede;font-size:small;\">";
  228. reset($this->commands);
  229. while (list($i) = each($this->commands)) {
  230. echo "<tr style=\"border-bottom:solid 1px #9cbaeb;\">";
  231. echo "<td colspan=\"2\" style=\"background:#3875d7;color:white;padding:2px 5px;\"><strong>&or; ".$this->commands[$i]->name."</strong></td>";
  232. echo "<td colspan=\"2\" style=\"text-align:right;background:#3875d7;color:#9cbaeb;padding:2px 5px;\">".$this->commands[$i]->expression."</span></td>";
  233. echo "</tr>";
  234. echo "<tr style=\"background:#cccccc;border: solid 1px #aaaaaa;\">";
  235. echo "<td style=\"color:white;border-right: solid 1px #aaaaaa;padding:0 10px;white-space:nowrap;\">Argument</td>";
  236. echo "<td style=\"color:white;border-right: solid 1px #aaaaaa;padding:0 10px;white-space:nowrap;text-align:center;\">Type</td>";
  237. echo "<td style=\"color:white;border-right: solid 1px #aaaaaa;padding:0 10px;white-space:nowrap;\">Value (Structured)</td>";
  238. echo "<td style=\"color:white;border-right: solid 1px #aaaaaa;padding:0 10px;white-space:nowrap;\">Expression</td>";
  239. echo "</tr>";
  240. reset($this->commands[$i]->arguments);
  241. while (list($n) = each($this->commands[$i]->arguments)) {
  242. echo "<tr>";
  243. echo "<td style=\"background:".($n%2==0? "#edf3fe":"#ffffff").";border-right: solid 1px ".($n%2==0? "#ced4dd":"#dedede").";padding:2px 10px;white-space:nowrap;vertical-align:top\">Argument $n</td>";
  244. echo "<td style=\"background:".($n%2==0? "#e6ebf6":"#f7f7f7").";border-right: solid 1px ".($n%2==0? "#ced4dd":"#dedede").";padding:2px 10px;white-space:nowrap;text-align:center;vertical-align:top\">".$this->commands[$i]->arguments[$n]->getType()."</td>";
  245. echo "<td style=\"background:".($n%2==0? "#edf3fe":"#ffffff").";border-right: solid 1px ".($n%2==0? "#ced4dd":"#dedede").";padding:2px 10px;vertical-align:top;color:gray;\">";
  246. ob_start();
  247. var_dump($this->commands[$i]->arguments[$n]->getValue());
  248. echo preg_replace('/\s\s/m', "&nbsp;&nbsp;&nbsp;&nbsp;", nl2br(ob_get_clean()));
  249. echo "</td>";
  250. echo "<td style=\"background:".($n%2==0? "#e6ebf6":"#f7f7f7").";border-right: solid 1px ".($n%2==0? "#ced4dd":"#dedede").";padding:2px 10px;vertical-align:top;\">".$this->commands[$i]->arguments[$n]->expression."</td>";
  251. echo "</tr>";
  252. }
  253. echo "</tr>";
  254. }
  255. echo "</table>";
  256. echo "</body></html>";
  257. }
  258. }
  259. class GMIDefaultPluggableSet
  260. extends GMIPluggableSet
  261. {
  262. var $defaultVariables; // array
  263. var $expression; // string
  264. // GMIDefaultPluggableSet(string expression, array defaultVariables)
  265. function GMIDefaultPluggableSet($expression, $defaultVariables) {
  266. parent::GMIPluggableSet();
  267. $this->expression = $expression;
  268. $this->defaultVariables = $defaultVariables;
  269. }
  270. // string getExpression()
  271. function getExpression() {
  272. return $this->expression;
  273. }
  274. // array getVariables()
  275. function getVariables() {
  276. return $this->defaultVariables;
  277. }
  278. }
  279. class GMIPluggableSet
  280. {
  281. // GMIPluggableSet()
  282. function GMIPluggableSet() {
  283. }
  284. // string getExpression()
  285. function getExpression() {
  286. return "";
  287. }
  288. // array getVariables()
  289. function getVariables() {
  290. return array();
  291. }
  292. }
  293. class GMIElement
  294. {
  295. var $execution = null; // GMIExecution
  296. var $expression = null; // string
  297. // GMIElement(GMIExecution execution)
  298. function GMIElement(&$execution, $expression) {
  299. $this->execution =& $execution;
  300. $this->expression = $expression;
  301. }
  302. }
  303. class GMICommand
  304. extends GMIElement
  305. {
  306. var $name = ""; // string
  307. var $arguments = array(); // array
  308. // GMICommand(GMIExecution execution, string expression)
  309. function GMICommand(&$execution, $expression) {
  310. parent::GMIElement($execution, $expression);
  311. // split expression into command name and arguments expression.
  312. $matches = array();
  313. preg_match_all('/^(\w+)(?:\s+(.*)$|$)/', $expression, $matches);
  314. // stop if expression is not valid
  315. if (count($matches[0]) == 0) {
  316. return;
  317. }
  318. $this->name = $matches[1][0];
  319. // stop if argument is empty
  320. if ($matches[2][0] == "") {
  321. return;
  322. }
  323. // add slash before "," in such quoted value as "...", '...' or {...}.
  324. $matches[2][0] = preg_replace_callback(
  325. '/(?<!\\\)\\\'(?:\\\\\'|[^\'])*(?:(?<!\\\)\\\')|'.
  326. '(?<!\\\)\"(?:\\\\"|[^"])*(?:(?<!\\\)\")|'.
  327. '(?<=\{)(?:[^{}]*(?:\{[^{}]*\})*[^{}]*)*(?=\})/',
  328. create_function('$matches', 'return preg_replace(\'/([,])/\', "\\\\\\\$1", $matches[0]);'),
  329. $matches[2][0]
  330. );
  331. // split arguments expression into an array by unexcaped ",".
  332. $this->arguments = preg_split('/\s*(?<!\\\)\,\s*/', $matches[2][0]);
  333. reset($this->arguments);
  334. while (list($i) = each($this->arguments)) {
  335. // remove slashes inserted above.
  336. $this->arguments[$i] = preg_replace_callback(
  337. '/(?<!\\\)\\\'(?:\\\\\'|[^\'])*(?:(?<!\\\)\\\')|'.
  338. '(?<!\\\)\"(?:\\\\"|[^"])*(?:(?<!\\\)\")|'.
  339. '(?<=\{)(?:[^{}]*(?:\{[^{}]*\})*[^{}]*)*(?=\})/',
  340. create_function('$matches', 'return preg_replace(\'/\\\\\([,])/\', "$1", $matches[0]);'),
  341. $this->arguments[$i]
  342. );
  343. // convert and store.
  344. $this->arguments[$i] = $this->execution->createValue($this->arguments[$i]);
  345. }
  346. }
  347. // void execute()
  348. function execute() {
  349. $this->execution->command($this->name, $this->arguments);
  350. }
  351. }
  352. class GMIValue
  353. extends GMIElement
  354. {
  355. var $value = null; // mixed
  356. // GMIValue(GMIExecution execution, string expression)
  357. function GMIValue(&$execution, $expression) {
  358. parent::GMIElement($execution, $expression);
  359. }
  360. // mixed getValue()
  361. function getValue() {
  362. return $this->expression;
  363. }
  364. // string getType()
  365. function getType() {
  366. return null;
  367. }
  368. }
  369. class GMIConstructor
  370. extends GMIValue
  371. {
  372. var $class; // string
  373. var $arguments; // array
  374. // GMIConstructor(GMIExecution execution, string expression)
  375. function GMIConstructor(&$execution, $expression) {
  376. parent::GMIValue($execution, $expression);
  377. // split expression into class name and arguments expression.
  378. $matches = array();
  379. preg_match_all('/^\{\s*(\w+)\s*\:(?:\s*(.*)|\s*)\}$/', $expression, $matches);
  380. // stop if expression is not valid
  381. if (count($matches[0]) == 0) {
  382. return;
  383. }
  384. $this->class = $matches[1][0];
  385. // add slash before "," in such quoted value as "..." or '...'.
  386. $matches[2][0] = preg_replace_callback(
  387. '/(?<!\\\)\\\'(?:\\\\\'|[^\'])*(?:(?<!\\\)\\\')|'.
  388. '(?<!\\\)\"(?:\\\\"|[^"])*(?:(?<!\\\)\")/',
  389. create_function('$matches', 'return preg_replace(\'/([,])/\', "\\\\\\\$1", $matches[0]);'),
  390. $matches[2][0]
  391. );
  392. // split arguments expression into an array by unexcaped ",".
  393. $this->arguments = preg_split('/\s*(?<!\\\)\,\s*/', $matches[2][0]);
  394. reset($this->arguments);
  395. while (list($n) = each($this->arguments)) {
  396. // remove slashes inserted above.
  397. $this->arguments[$n] = preg_replace_callback(
  398. '/(?<!\\\)\\\'(?:\\\\\'|[^\'])*(?:(?<!\\\)\\\')|'.
  399. '(?<!\\\)\"(?:\\\\"|[^"])*(?:(?<!\\\)\")/',
  400. create_function('$matches', 'return preg_replace(\'/\\\\\([,])/\', "$1", $matches[0]);'),
  401. $this->arguments[$n]
  402. );
  403. // convert and store.
  404. $this->arguments[$n] = $this->execution->createValue($this->arguments[$n]);
  405. }
  406. }
  407. // mixed getValue()
  408. function getValue() {
  409. if ($this->value === null) {
  410. $this->value = $this->execution->construct($this->class, $this->arguments);
  411. }
  412. return $this->value;
  413. }
  414. // string getType()
  415. function getType() {
  416. if ($this->value === null) {
  417. $this->value = $this->execution->construct($this->class, $this->arguments);
  418. }
  419. return ($this->value !== null)? $this->class
  420. : 'null';
  421. }
  422. }
  423. class GMIVariable
  424. extends GMIValue
  425. {
  426. var $variableName;
  427. var $propertyName;
  428. // GMIVariable(GMIExecution execution, string expression)
  429. function GMIVariable(&$execution, $expression) {
  430. parent::GMIValue($execution, $expression);
  431. // split expression into variable name.
  432. $matches = array();
  433. preg_match_all('/^\{\s*((\w+)(?:\.(\w+))?)\s*\}$/', $expression, $matches);
  434. // stop if expression is not valid
  435. if (count($matches[0]) == 0) {
  436. return;
  437. }
  438. $this->variableName = $matches[2][0];
  439. $this->propertyName = ($matches[3][0] != "")? $matches[3][0] : null;
  440. }
  441. // mixed getValue()
  442. function getValue() {
  443. if ($this->value === null) {
  444. $this->value =& $this->execution->getVariable($this->variableName);
  445. // acquire property of the variable
  446. if ($this->value !== null && $this->propertyName !== null) {
  447. $value =& $this->value;
  448. unset($this->value);
  449. $this->value = $this->execution->getProperty($value, $this->propertyName);
  450. }
  451. }
  452. return ($this->value !== null)? $this->value->getValue()
  453. : null;
  454. }
  455. // string getType()
  456. function getType() {
  457. if ($this->value === null) {
  458. $this->value =& $this->execution->getVariable($this->variableName);
  459. // acquire property of the variable
  460. if ($this->value !== null && $this->propertyName !== null) {
  461. $value =& $this->value;
  462. unset($this->value);
  463. $this->value = $this->execution->getProperty($value, $this->propertyName);
  464. }
  465. }
  466. return ($this->value !== null)? $this->value->getType()
  467. : 'null';
  468. }
  469. }
  470. class GMIConstant
  471. extends GMIValue
  472. {
  473. // GMIConstant(GMIExecution execution, string expression)
  474. function GMIConstant(&$execution, $expression) {
  475. parent::GMIValue($execution, $expression);
  476. }
  477. // string getType()
  478. function getType() {
  479. return 'constant';
  480. }
  481. }
  482. class GMINumber
  483. extends GMIValue
  484. {
  485. // GMINumber(GMIExecution execution, string expression)
  486. function GMINumber(&$execution, $expression) {
  487. parent::GMIValue($execution, $expression);
  488. }
  489. // mixed getValue()
  490. function getValue() {
  491. if ($this->value === null) {
  492. $this->value = $this->decode($this->expression);
  493. }
  494. return $this->value + 0;
  495. }
  496. // string getType()
  497. function getType() {
  498. return 'number';
  499. }
  500. // int decode(string n)
  501. function decode($n) {
  502. if (preg_match('/^0[xX](?:[0-7][\da-fA-F]{7}|[\da-fA-F]{1,7})$/', $n)) {
  503. $n = hexDec(substr(trim($n), 2));
  504. }
  505. else if (preg_match('/^\#(?:[0-7][\da-fA-F]{7}|[\da-fA-F]{1,7})$/', $n)) {
  506. $n = hexDec(substr(trim($n), 1));
  507. }
  508. else if (preg_match('/^0[Bb][01]{1,31}$/', $n)) {
  509. $n = binDec(substr(trim($n), 2));
  510. }
  511. else {
  512. $n = trim($n);
  513. }
  514. return $n;
  515. }
  516. }
  517. class GMIString
  518. extends GMIValue
  519. {
  520. // GMIString(GMIExecution execution, string expression)
  521. function GMIString(&$execution, $expression) {
  522. parent::GMIValue($execution, $expression);
  523. }
  524. // string getType()
  525. function getType() {
  526. return 'string';
  527. }
  528. }
  529. ?>