PageRenderTime 64ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wordpress-console/common.php

https://gitlab.com/blueprintmrk/bladencountyrecords
PHP | 631 lines | 509 code | 86 blank | 36 comment | 87 complexity | 746b1ed6ba0d426af066e4db94b3c225 MD5 | raw file
  1. <?php
  2. # try to load the wordpress environment from passed in location known by the client
  3. # otherwise, fall back to the mostly (but sometimes not) safe assumption that it is
  4. # up 3 directories from this file.
  5. if ( isset( $_POST["root"] ) ) {
  6. require_once( $_POST["root"] . "/wp-load.php" );
  7. } else {
  8. require_once( dirname( __FILE__ ) . "/../../../wp-load.php" );
  9. }
  10. require_once( ABSPATH . "wp-admin/includes/admin.php" );
  11. if ( !session_id() ) {
  12. session_start();
  13. }
  14. ob_end_clean();
  15. error_reporting( E_ALL ^ E_PARSE );
  16. set_time_limit( 0 );
  17. if ( !function_exists( "json_encode" ) ) {
  18. function json_encode( $value ) {
  19. require_once( "lib/FastJSON.class.php" );
  20. return FastJSON::encode($value);
  21. }
  22. }
  23. function console_error_handler( $errno, $errorstr ) {
  24. error( $errorstr );
  25. }
  26. function error( $error ) {
  27. exit( json_encode( array( "error" => $error ) ) );
  28. }
  29. function logit( $msg ) {
  30. $file = "/tmp/console.log";
  31. $fh = fopen($file,"a");
  32. fwrite($fh,$msg);
  33. fwrite($fh,"\n\n");
  34. fclose($fh);
  35. }
  36. // saves newly defined variables to session.
  37. // somebody please refactor this!
  38. function save_variables( $existing, $current, $ignore ) {
  39. $new_vars = array_diff( array_keys( $current ), array_keys( $existing ) );
  40. $user_vars = array_diff( $new_vars, $ignore );
  41. $save_vars = array();
  42. foreach( $current as $key => $value ) {
  43. if ( in_array( $key, $user_vars ) ) {
  44. $save_vars[$key] = $value;
  45. }
  46. }
  47. $export = var_export( $save_vars, true );
  48. // special consideration for variables that are objects
  49. // see: http://www.thoughtlabs.com/2008/02/02/phps-mystical-__set_state-method/
  50. $export = preg_replace_callback( "/(\w+)::__set_state/Ums", "class_set_state_check", $export );
  51. $_SESSION["console_vars"] = $export;
  52. }
  53. // classes to be restored need to implement __set_state() function.
  54. // if they don't have it, we will convert to stdClass object.
  55. function class_set_state_check($matches) {
  56. if (method_exists($matches[1], "__set_state")) {
  57. return $matches[0];
  58. } else {
  59. return "(object) ";
  60. }
  61. }
  62. // this function was yoinked (and adjusted) from the 'php shell' project. See:
  63. // http://jan.kneschke.de/projects/php-shell
  64. // return int 0 if a executable statement is in the session buffer, non-zero otherwise
  65. function parse( $code ) {
  66. ## remove empty lines
  67. if (trim($code) == "") return 1;
  68. $t = token_get_all("<?php ".$code." ?>");
  69. // logit($code);
  70. $need_semicolon = 1; /* do we need a semicolon to complete the statement ? */
  71. $need_return = 1; /* can we prepend a return to the eval-string ? */
  72. $open_comment = 0; /* a open multi-line comment */
  73. $eval = ""; /* code to be eval()'ed later */
  74. $braces = array(); /* to track if we need more closing braces */
  75. $methods = array(); /* to track duplicate methods in a class declaration */
  76. $ts = array(); /* tokens without whitespaces */
  77. foreach ($t as $ndx => $token) {
  78. if (is_array($token)) {
  79. $ignore = 0;
  80. switch($token[0]) {
  81. case T_WHITESPACE:
  82. case T_OPEN_TAG:
  83. case T_CLOSE_TAG:
  84. $ignore = 1;
  85. break;
  86. case T_FOREACH:
  87. case T_DO:
  88. case T_WHILE:
  89. case T_FOR:
  90. case T_IF:
  91. case T_RETURN:
  92. case T_CLASS:
  93. case T_FUNCTION:
  94. case T_INTERFACE:
  95. case T_PRINT:
  96. case T_ECHO:
  97. case T_COMMENT:
  98. case T_UNSET:
  99. case T_INCLUDE:
  100. case T_REQUIRE:
  101. case T_INCLUDE_ONCE:
  102. case T_REQUIRE_ONCE:
  103. case T_TRY:
  104. case T_SWITCH:
  105. case T_DEFAULT:
  106. case T_CASE:
  107. case T_BREAK:
  108. case T_DOC_COMMENT:
  109. $need_return = 0;
  110. break;
  111. case T_EMPTY:
  112. case T_ISSET:
  113. case T_EVAL:
  114. case T_EXIT:
  115. case T_VARIABLE:
  116. case T_STRING:
  117. case T_NEW:
  118. case T_EXTENDS:
  119. case T_IMPLEMENTS:
  120. case T_OBJECT_OPERATOR:
  121. case T_DOUBLE_COLON:
  122. case T_INSTANCEOF:
  123. case T_CATCH:
  124. case T_THROW:
  125. case T_ELSE:
  126. case T_AS:
  127. case T_LNUMBER:
  128. case T_DNUMBER:
  129. case T_CONSTANT_ENCAPSED_STRING:
  130. case T_ENCAPSED_AND_WHITESPACE:
  131. case T_CHARACTER:
  132. case T_ARRAY:
  133. case T_DOUBLE_ARROW:
  134. case T_CONST:
  135. case T_PUBLIC:
  136. case T_PROTECTED:
  137. case T_PRIVATE:
  138. case T_ABSTRACT:
  139. case T_STATIC:
  140. case T_VAR:
  141. case T_INC:
  142. case T_DEC:
  143. case T_SL:
  144. case T_SL_EQUAL:
  145. case T_SR:
  146. case T_SR_EQUAL:
  147. case T_IS_EQUAL:
  148. case T_IS_IDENTICAL:
  149. case T_IS_GREATER_OR_EQUAL:
  150. case T_IS_SMALLER_OR_EQUAL:
  151. case T_BOOLEAN_OR:
  152. case T_LOGICAL_OR:
  153. case T_BOOLEAN_AND:
  154. case T_LOGICAL_AND:
  155. case T_LOGICAL_XOR:
  156. case T_MINUS_EQUAL:
  157. case T_PLUS_EQUAL:
  158. case T_MUL_EQUAL:
  159. case T_DIV_EQUAL:
  160. case T_MOD_EQUAL:
  161. case T_XOR_EQUAL:
  162. case T_AND_EQUAL:
  163. case T_OR_EQUAL:
  164. case T_FUNC_C:
  165. case T_CLASS_C:
  166. case T_LINE:
  167. case T_FILE:
  168. case T_BOOL_CAST:
  169. case T_INT_CAST:
  170. case T_STRING_CAST:
  171. /* just go on */
  172. break;
  173. default:
  174. /* debug unknown tags*/
  175. error_log(sprintf("unknown tag: %d (%s): %s".PHP_EOL, $token[0], token_name($token[0]), $token[1]));
  176. break;
  177. }
  178. if (!$ignore) {
  179. $eval .= $token[1]." ";
  180. $ts[] = array("token" => $token[0], "value" => $token[1]);
  181. }
  182. } else {
  183. $ts[] = array("token" => $token, "value" => "");
  184. $last = count($ts) - 1;
  185. switch ($token) {
  186. case "(":
  187. /* walk backwards through the tokens */
  188. if ($last >= 4 &&
  189. $ts[$last - 1]["token"] == T_STRING &&
  190. $ts[$last - 2]["token"] == T_OBJECT_OPERATOR &&
  191. $ts[$last - 3]["token"] == ")" ) {
  192. /* func()->method()
  193. *
  194. * we can't know what func() is return, so we can't
  195. * say if the method() exists or not
  196. *
  197. */
  198. } else if ($last >= 3 &&
  199. $ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
  200. $ts[0]["token"] != T_ABSTRACT && /* if we are not in a class definition */
  201. $ts[1]["token"] != T_CLASS && /* if we are not in a class definition */
  202. $ts[$last - 1]["token"] == T_STRING &&
  203. $ts[$last - 2]["token"] == T_OBJECT_OPERATOR &&
  204. $ts[$last - 3]["token"] == T_VARIABLE ) {
  205. /* $object->method( */
  206. /* catch (Exception $e) does not set $e in $GLOBALS[] */
  207. $in_catch = 0;
  208. foreach ($ts as $v) {
  209. if ($v["token"] == T_CATCH) {
  210. $in_catch = 1;
  211. }
  212. }
  213. if (!$in_catch) {
  214. /* $object has to exist and has to be a object */
  215. $objname = $ts[$last - 3]["value"];
  216. if (!isset($GLOBALS[ltrim($objname, "$")])) {
  217. throw new Exception(sprintf('Variable \'%s\' is not set', $objname));
  218. }
  219. $object = $GLOBALS[ltrim($objname, "$")];
  220. if (!is_object($object)) {
  221. throw new Exception(sprintf('Variable \'%s\' is not a class', $objname));
  222. }
  223. $method = $ts[$last - 1]["value"];
  224. /* obj */
  225. if (!method_exists($object, $method)) {
  226. throw new Exception(sprintf("Variable %s (Class '%s') doesn't have a method named '%s'",
  227. $objname, get_class($object), $method));
  228. }
  229. }
  230. } else if ($last >= 3 &&
  231. $ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
  232. $ts[$last - 1]["token"] == T_VARIABLE &&
  233. $ts[$last - 2]["token"] == T_OBJECT_OPERATOR &&
  234. $ts[$last - 3]["token"] == T_VARIABLE ) {
  235. /* $object->$method( */
  236. /* $object has to exist and has to be a object */
  237. $objname = $ts[$last - 3]["value"];
  238. if (!isset($GLOBALS[ltrim($objname, "$")])) {
  239. throw new Exception(sprintf('Variable \'%s\' is not set', $objname));
  240. }
  241. $object = $GLOBALS[ltrim($objname, "$")];
  242. if (!is_object($object)) {
  243. throw new Exception(sprintf('Variable \'%s\' is not a class', $objname));
  244. }
  245. $methodname = $ts[$last - 1]["value"];
  246. if (!isset($GLOBALS[ltrim($methodname, "$")])) {
  247. throw new Exception(sprintf('Variable \'%s\' is not set', $methodname));
  248. }
  249. $method = $GLOBALS[ltrim($methodname, "$")];
  250. /* obj */
  251. if (!method_exists($object, $method)) {
  252. throw new Exception(sprintf("Variable %s (Class '%s') doesn't have a method named '%s'",
  253. $objname, get_class($object), $method));
  254. }
  255. } else if ($last >= 6 &&
  256. $ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
  257. $ts[$last - 1]["token"] == T_STRING &&
  258. $ts[$last - 2]["token"] == T_OBJECT_OPERATOR &&
  259. $ts[$last - 3]["token"] == "]" &&
  260. /* might be anything as index */
  261. $ts[$last - 5]["token"] == "[" &&
  262. $ts[$last - 6]["token"] == T_VARIABLE ) {
  263. /* $object[...]->method( */
  264. /* $object has to exist and has to be a object */
  265. $objname = $ts[$last - 6]["value"];
  266. if (!isset($GLOBALS[ltrim($objname, "$")])) {
  267. throw new Exception(sprintf('Variable \'%s\' is not set', $objname));
  268. }
  269. $array = $GLOBALS[ltrim($objname, "$")];
  270. if (!is_array($array)) {
  271. throw new Exception(sprintf('Variable \'%s\' is not a array', $objname));
  272. }
  273. $andx = $ts[$last - 4]["value"];
  274. if (!isset($array[$andx])) {
  275. throw new Exception(sprintf('%s[\'%s\'] is not set', $objname, $andx));
  276. }
  277. $object = $array[$andx];
  278. if (!is_object($object)) {
  279. throw new Exception(sprintf('Variable \'%s\' is not a class', $objname));
  280. }
  281. $method = $ts[$last - 1]["value"];
  282. /* obj */
  283. if (!method_exists($object, $method)) {
  284. throw new Exception(sprintf("Variable %s (Class '%s') doesn't have a method named '%s'",
  285. $objname, get_class($object), $method));
  286. }
  287. } else if ($last >= 3 &&
  288. $ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
  289. $ts[$last - 1]["token"] == T_STRING &&
  290. $ts[$last - 2]["token"] == T_DOUBLE_COLON &&
  291. $ts[$last - 3]["token"] == T_STRING ) {
  292. /* Class::method() */
  293. /* $object has to exist and has to be a object */
  294. $classname = $ts[$last - 3]["value"];
  295. if (!class_exists($classname)) {
  296. throw new Exception(sprintf('Class \'%s\' doesn\'t exist', $classname));
  297. }
  298. $method = $ts[$last - 1]["value"];
  299. if (!in_array($method, get_class_methods($classname))) {
  300. throw new Exception(sprintf("Class '%s' doesn't have a method named '%s'",
  301. $classname, $method));
  302. }
  303. } else if ($last >= 3 &&
  304. $ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
  305. $ts[$last - 1]["token"] == T_VARIABLE &&
  306. $ts[$last - 2]["token"] == T_DOUBLE_COLON &&
  307. $ts[$last - 3]["token"] == T_STRING ) {
  308. /* $var::method() */
  309. /* $object has to exist and has to be a object */
  310. $classname = $ts[$last - 3]["value"];
  311. if (!class_exists($classname)) {
  312. throw new Exception(sprintf('Class \'%s\' doesn\'t exist', $classname));
  313. }
  314. $methodname = $ts[$last - 1]["value"];
  315. if (!isset($GLOBALS[ltrim($methodname, "$")])) {
  316. throw new Exception(sprintf('Variable \'%s\' is not set', $methodname));
  317. }
  318. $method = $GLOBALS[ltrim($methodname, "$")];
  319. if (!in_array($method, get_class_methods($classname))) {
  320. throw new Exception(sprintf("Class '%s' doesn't have a method named '%s'",
  321. $classname, $method));
  322. }
  323. } else if ($last >= 2 &&
  324. $ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
  325. $ts[$last - 1]["token"] == T_STRING &&
  326. $ts[$last - 2]["token"] == T_NEW ) {
  327. /* new Class() */
  328. /* don't care about this in a class ... { ... } */
  329. $classname = $ts[$last - 1]["value"];
  330. if (!class_exists($classname)) {
  331. throw new Exception(sprintf('Class \'%s\' doesn\'t exist', $classname));
  332. }
  333. $r = new ReflectionClass($classname);
  334. if ($r->isAbstract()) {
  335. throw new Exception(sprintf("Can't instantiate abstract Class '%s'", $classname));
  336. }
  337. if (!$r->isInstantiable()) {
  338. throw new Exception(sprintf('Class \'%s\' can\'t be instantiated. Is the class abstract ?', $classname));
  339. }
  340. } else if ($last >= 2 &&
  341. $ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
  342. $ts[$last - 1]["token"] == T_STRING &&
  343. $ts[$last - 2]["token"] == T_FUNCTION ) {
  344. /* make sure we are not a in class definition */
  345. /* function a() */
  346. $func = $ts[$last - 1]["value"];
  347. if (function_exists($func)) {
  348. throw new Exception(sprintf('Function \'%s\' is already defined', $func));
  349. }
  350. } else if ($last >= 4 &&
  351. $ts[0]["token"] == T_CLASS &&
  352. $ts[1]["token"] == T_STRING &&
  353. $ts[$last - 1]["token"] == T_STRING &&
  354. $ts[$last - 2]["token"] == T_FUNCTION ) {
  355. /* make sure we are not a in class definition */
  356. /* class a { .. function a() ... } */
  357. $func = $ts[$last - 1]["value"];
  358. $classname = $ts[1]["value"];
  359. if (isset($methods[$func])) {
  360. throw new Exception(sprintf("Can't redeclare method '%s' in Class '%s'", $func, $classname));
  361. }
  362. $methods[$func] = 1;
  363. } else if ($last >= 1 &&
  364. $ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
  365. $ts[0]["token"] != T_ABSTRACT && /* if we are not in a class definition */
  366. $ts[1]["token"] != T_CLASS && /* if we are not in a class definition */
  367. $ts[$last - 1]["token"] == T_STRING ) {
  368. /* func() */
  369. $funcname = $ts[$last - 1]["value"];
  370. if (!function_exists($funcname)) {
  371. throw new Exception(sprintf("Function %s() doesn't exist", $funcname));
  372. }
  373. } else if ($last >= 1 &&
  374. $ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
  375. $ts[$last - 1]["token"] == T_VARIABLE ) {
  376. /* $object has to exist and has to be a object */
  377. $funcname = $ts[$last - 1]["value"];
  378. if (!isset($GLOBALS[ltrim($funcname, "$")])) {
  379. throw new Exception(sprintf('Variable \'%s\' is not set', $funcname));
  380. }
  381. $func = $GLOBALS[ltrim($funcname, "$")];
  382. if (!function_exists($func)) {
  383. throw new Exception(sprintf("Function %s() doesn't exist", $func));
  384. }
  385. }
  386. array_push($braces, $token);
  387. break;
  388. case "{":
  389. $need_return = 0;
  390. if ($last >= 2 &&
  391. $ts[$last - 1]["token"] == T_STRING &&
  392. $ts[$last - 2]["token"] == T_CLASS ) {
  393. /* class name { */
  394. $classname = $ts[$last - 1]["value"];
  395. if (class_exists($classname, false)) {
  396. throw new Exception(sprintf("Class '%s' can't be redeclared", $classname));
  397. }
  398. } else if ($last >= 4 &&
  399. $ts[$last - 1]["token"] == T_STRING &&
  400. $ts[$last - 2]["token"] == T_EXTENDS &&
  401. $ts[$last - 3]["token"] == T_STRING &&
  402. $ts[$last - 4]["token"] == T_CLASS ) {
  403. /* class classname extends classname { */
  404. $classname = $ts[$last - 3]["value"];
  405. $extendsname = $ts[$last - 1]["value"];
  406. if (class_exists($classname, false)) {
  407. throw new Exception(sprintf("Class '%s' can't be redeclared",
  408. $classname));
  409. }
  410. if (!class_exists($extendsname, true)) {
  411. throw new Exception(sprintf("Can't extend '%s' ... from not existing Class '%s'",
  412. $classname, $extendsname));
  413. }
  414. } else if ($last >= 4 &&
  415. $ts[$last - 1]["token"] == T_STRING &&
  416. $ts[$last - 2]["token"] == T_IMPLEMENTS &&
  417. $ts[$last - 3]["token"] == T_STRING &&
  418. $ts[$last - 4]["token"] == T_CLASS ) {
  419. /* class name implements interface { */
  420. $classname = $ts[$last - 3]["value"];
  421. $implements = $ts[$last - 1]["value"];
  422. if (class_exists($classname, false)) {
  423. throw new Exception(sprintf("Class '%s' can't be redeclared",
  424. $classname));
  425. }
  426. if (!interface_exists($implements, false)) {
  427. throw new Exception(sprintf("Can't implement not existing Interface '%s' for Class '%s'",
  428. $implements, $classname));
  429. }
  430. }
  431. array_push($braces, $token);
  432. break;
  433. case "}":
  434. $need_return = 0;
  435. case ")":
  436. array_pop($braces);
  437. break;
  438. case "[":
  439. if ($ts[0]["token"] != T_CLASS && /* if we are not in a class definition */
  440. $ts[0]["token"] != T_ABSTRACT && /* if we are not in a class definition */
  441. $ts[1]["token"] != T_CLASS && /* if we are not in a class definition */
  442. $ts[$last - 1]["token"] == T_VARIABLE) {
  443. /* $a[] only works on array and string */
  444. /* $object has to exist and has to be a object */
  445. $objname = $ts[$last - 1]["value"];
  446. if (!isset($GLOBALS[ltrim($objname, "$")])) {
  447. throw new Exception(sprintf('Variable \'%s\' is not set', $objname));
  448. }
  449. $obj = $GLOBALS[ltrim($objname, "$")];
  450. if (is_object($obj)) {
  451. throw new Exception(sprintf('Objects (%s) don\'t support array access operators', $objname));
  452. }
  453. }
  454. break;
  455. }
  456. $eval .= $token;
  457. }
  458. }
  459. $last = count($ts) - 1;
  460. if ($last >= 2 &&
  461. $ts[$last - 0]["token"] == T_STRING &&
  462. $ts[$last - 1]["token"] == T_DOUBLE_COLON &&
  463. $ts[$last - 2]["token"] == T_STRING ) {
  464. /* Class::constant */
  465. /* $object has to exist and has to be a object */
  466. $classname = $ts[$last - 2]["value"];
  467. if (!class_exists($classname)) {
  468. throw new Exception(sprintf('Class \'%s\' doesn\'t exist', $classname));
  469. }
  470. $constname = $ts[$last - 0]["value"];
  471. $c = new ReflectionClass($classname);
  472. if (!$c->hasConstant($constname)) {
  473. throw new Exception(sprintf("Class '%s' doesn't have a constant named '%s'",
  474. $classname, $constname));
  475. }
  476. } else if ($last == 0 &&
  477. $ts[$last - 0]["token"] == T_VARIABLE ) {
  478. /* $var */
  479. $varname = $ts[$last - 0]["value"];
  480. if (!isset($GLOBALS[ltrim($varname, "$")])) {
  481. throw new Exception(sprintf('Variable \'%s\' is not set', $varname));
  482. }
  483. }
  484. $need_more = (count($braces) > 0) || $open_comment;
  485. if ($need_more || ";" === $token) {
  486. $need_semicolon = 0;
  487. }
  488. if ($need_return) {
  489. $eval = "return ".$eval;
  490. }
  491. if ($need_more) {
  492. $_SESSION["partial"] = $eval;
  493. } else {
  494. $_SESSION["code"] = $eval;
  495. }
  496. return $need_more;
  497. }
  498. ?>