PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/runtime/ext/std/ext_std_options.php

http://github.com/facebook/hiphop-php
PHP | 524 lines | 259 code | 79 blank | 186 comment | 18 complexity | 502f510ad3140eb1fb38e6d711a357a3 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?hh // partial
  2. namespace {
  3. /* Set the various assert() control options or just query their current
  4. * settings.
  5. */
  6. <<__Native>>
  7. function assert_options(int $what,
  8. mixed $value = null): mixed;
  9. /* assert() will check the given assertion and take appropriate action if its
  10. * result is FALSE. Assertions should be used as a debugging feature only. You
  11. * may use them for sanity-checks that test for conditions that should always
  12. * be TRUE and that indicate some programming errors if not or to check for
  13. * the presence of certain features like extension functions or certain system
  14. * limits and features. Assertions should not be used for normal runtime
  15. * operations like input parameter checks. As a rule of thumb your code should
  16. * always be able to work correctly if assertion checking is not activated.
  17. * The behavior of assert() may be configured by assert_options() or by
  18. * .ini-settings described in that functions manual page.
  19. */
  20. <<__Native>>
  21. function assert(mixed $assertion, mixed $message = null): mixed;
  22. /* Loads the PHP extension given by the parameter library. Use
  23. * extension_loaded() to test whether a given extension is already available
  24. * or not. This works on both built-in extensions and dynamically loaded ones
  25. * (either through php.ini or dl()). Warning: This function has been removed
  26. * from some SAPI's in PHP 5.3.
  27. */
  28. function dl(string $_library): int { return 0; }
  29. /* Finds out whether the extension is loaded.
  30. */
  31. <<__Native>>
  32. function extension_loaded(string $name): bool;
  33. /* This function returns the names of all the modules compiled and loaded in
  34. * the PHP interpreter.
  35. */
  36. <<__Native>>
  37. function get_loaded_extensions(bool $zend_extensions = false): varray;
  38. /* This function returns the names of all the functions defined in the module
  39. * indicated by module_name or false if $module_name is not a valid extension.
  40. */
  41. <<__Native>>
  42. function get_extension_funcs(string $module_name): mixed;
  43. /* Gets the value of a PHP configuration option. This function will not
  44. * return configuration information set when the PHP was compiled, or read
  45. * from an Apache configuration file. To check whether the system is using a
  46. * configuration file, try retrieving the value of the cfg_file_path
  47. * configuration setting. If this is available, a configuration file is being
  48. * used.
  49. */
  50. function get_cfg_var(string $_option): mixed { return false; }
  51. <<__Native>>
  52. function get_current_user(): string;
  53. /* Returns the names and values of all the constants currently defined. This
  54. * includes those created by extensions.
  55. */
  56. <<__Native>>
  57. function get_defined_constants(bool $categorize = false): darray;
  58. <<__Native>>
  59. function get_include_path(): string;
  60. <<__Native>>
  61. function restore_include_path(): void;
  62. /* Sets the include_path configuration option for the duration of the script.
  63. */
  64. <<__Native>>
  65. function set_include_path(mixed $new_include_path): string;
  66. /* Gets the names of all files that have been included using include(),
  67. * include_once(), require() or require_once().
  68. */
  69. <<__Native>>
  70. function get_included_files(): varray;
  71. function get_required_files(): varray {
  72. return get_included_files();
  73. }
  74. <<__Native>>
  75. function getenv(string $varname): mixed;
  76. /* Gets the time of the last modification of the current page. If you're
  77. * interested in getting the last modification time of a different file,
  78. * consider using filemtime().
  79. */
  80. <<__Native>>
  81. function getlastmod(): mixed;
  82. <<__Native>>
  83. function getmygid(): mixed;
  84. /* Gets the inode of the current script.
  85. */
  86. <<__Native>>
  87. function getmyinode(): mixed;
  88. /* Gets the current PHP process ID.
  89. */
  90. <<__Native>>
  91. function getmypid(): mixed;
  92. <<__Native>>
  93. function getmyuid(): mixed;
  94. /* Parses options passed to the script. The options parameter may contain the
  95. * following elements: Individual characters (do not accept values) Characters
  96. * followed by a colon (parameter requires value) Characters followed by two
  97. * colons (optional value) Option values are the first argument after the
  98. * string. It does not matter if a value has leading white space or not.
  99. * Optional values do not accept " " (space) as a separator. The format for
  100. * the options and longopts is almost the same, the only difference is that
  101. * longopts takes an array of options (where each element is the option) where
  102. * as options takes a string (where each character is the option).
  103. */
  104. <<__Native>>
  105. function getopt(string $options,
  106. mixed $longopts = null): darray;
  107. /* This is an interface to getrusage(2). It gets data returned from the system
  108. * call.
  109. */
  110. <<__Native>>
  111. function getrusage(int $who = 0): darray;
  112. /* Gets resolution of system clock. "man 3 clock_getres" for more details.
  113. */
  114. <<__Native>>
  115. function clock_getres(int $clk_id,
  116. <<__OutOnly('KindOfInt64')>>
  117. inout mixed $sec,
  118. <<__OutOnly('KindOfInt64')>>
  119. inout mixed $nsec): bool;
  120. /* Gets time of a system clock. "man 3 clock_gettime" for more details.
  121. */
  122. <<__Native>>
  123. function clock_gettime(int $clk_id,
  124. <<__OutOnly('KindOfInt64')>>
  125. inout mixed $sec,
  126. <<__OutOnly('KindOfInt64')>>
  127. inout mixed $nsec): bool;
  128. /* Same as clock_gettime(), but returns a single integer in nanoseconds.
  129. * Returns -1 if invalid or non-supported clock is specified.
  130. */
  131. <<__Native>>
  132. function clock_gettime_ns(int $clk_id): int;
  133. /* Gets number of processors.
  134. */
  135. <<__Native>>
  136. function cpu_get_count(): int;
  137. /* Gets processor model.
  138. */
  139. <<__Native>>
  140. function cpu_get_model(): string;
  141. /* Returns the value of the configuration option on success.
  142. */
  143. <<__Native>>
  144. function ini_get(string $varname): mixed;
  145. /* Gets all configuration options
  146. */
  147. <<__Native>>
  148. function ini_get_all(string $extension = "",
  149. bool $details = true): array;
  150. /* Restores a given configuration option to its original value.
  151. */
  152. <<__Native>>
  153. function ini_restore(string $varname): void;
  154. /* Sets the value of the given configuration option. The configuration option
  155. * will keep this new value during the script's execution, and will be
  156. * restored at the script's ending.
  157. */
  158. <<__Native>>
  159. function ini_set(string $varname,
  160. mixed $newvalue): mixed;
  161. /* Sets the value of the given configuration option. The configuration option
  162. * will keep this new value during the script's execution, and will be
  163. * restored at the script's ending.
  164. */
  165. <<__Native>>
  166. function ini_alter(string $varname,
  167. mixed $newvalue): mixed;
  168. /* Returns the peak of memory, in bytes, that's been allocated to your PHP
  169. * script.
  170. */
  171. <<__Native>>
  172. function memory_get_peak_usage(bool $real_usage = false): int;
  173. /* Returns the amount of memory, in bytes, that's currently being allocated to
  174. * your PHP script.
  175. */
  176. <<__Native>>
  177. function memory_get_usage(bool $real_usage = false): int;
  178. /* Returns the total memory, in bytes, that your PHP script has allocated.
  179. */
  180. <<__Native, __HipHopSpecific>>
  181. function memory_get_allocation(): int;
  182. /* Returns the request-heap memory currently in use by the script.
  183. * Does not trigger OOM.
  184. */
  185. <<__Native, __HipHopSpecific>>
  186. function hphp_memory_heap_usage(): int;
  187. /* Returns the current total capacity of the request-heap, including
  188. * blocks freed by the script but not returned to the process heap,
  189. * external fragmentation, and heap management overhead.
  190. * Does not trigger OOM.
  191. */
  192. <<__Native, __HipHopSpecific>>
  193. function hphp_memory_heap_capacity(): int;
  194. /* Returns the peak of memory, in bytes, that's been allocated to your PHP
  195. * script since calling memory_start_usage_interval.
  196. */
  197. <<__Native, __HipHopSpecific>>
  198. function hphp_memory_get_interval_peak_usage(bool $real_usage = false): int;
  199. /* Starts per-interval usage tracking to allow peak usage to be tracked
  200. * with more granularity than a per-script basis.
  201. *
  202. * Returns whether the state of interval tracking was changed.
  203. */
  204. <<__Native, __HipHopSpecific>>
  205. function hphp_memory_start_interval(): bool;
  206. /* Stops per-interval usage tracking to allow peak usage to be tracked
  207. * with more granularity than a per-script basis.
  208. *
  209. * Returns whether the state of interval tracking was changed.
  210. */
  211. <<__Native, __HipHopSpecific>>
  212. function hphp_memory_stop_interval(): bool;
  213. /* Retrieve a path to the loaded php.ini file.
  214. */
  215. function php_ini_loaded_file(): mixed {
  216. return false;
  217. }
  218. /* Retrieve a comma-separated list of paths to any additionally loaded ini
  219. * files after php.ini.
  220. */
  221. function php_ini_scanned_files(): mixed {
  222. return false;
  223. }
  224. <<__Native>>
  225. function php_sapi_name(): string;
  226. /* php_uname() returns a description of the operating system PHP is running
  227. * on. This is the same string you see at the very top of the phpinfo()
  228. * output. For the name of just the operating system, consider using the
  229. * PHP_OS constant, but keep in mind this constant will contain the operating
  230. * system PHP was built on. On some older UNIX platforms, it may not be able
  231. * to determine the current OS information in which case it will revert to
  232. * displaying the OS PHP was built on. This will only happen if your uname()
  233. * library call either doesn't exist or doesn't work.
  234. */
  235. <<__Native>>
  236. function php_uname(string $mode = ""): mixed;
  237. /* Outputs a large amount of information about the current state of PHP. This
  238. * includes information about PHP compilation options and extensions, the PHP
  239. * version, server information and environment (if compiled as a module), the
  240. * PHP environment, OS version information, paths, master and local values of
  241. * configuration options, HTTP headers, and the PHP License. Because every
  242. * system is setup differently, phpinfo() is commonly used to check
  243. * configuration settings and for available predefined variables on a given
  244. * system. phpinfo() is also a valuable debugging tool as it contains all
  245. * EGPCS (Environment, GET, POST, Cookie, Server) data.
  246. */
  247. function phpinfo(int $what = 0): bool {
  248. (new \__SystemLib\PhpInfo())->report();
  249. return true;
  250. }
  251. /* Returns a string containing the version of the currently running PHP parser
  252. * or extension.
  253. */
  254. <<__Native>>
  255. function phpversion(string $extension = ""): mixed;
  256. /* Adds setting to the server environment. The environment variable will only
  257. * exist for the duration of the current request. At the end of the request
  258. * the environment is restored to its original state. Setting certain
  259. * environment variables may be a potential security breach. The
  260. * safe_mode_allowed_env_vars directive contains a comma-delimited list of
  261. * prefixes. In Safe Mode, the user may only alter environment variables whose
  262. * names begin with the prefixes supplied by this directive. By default, users
  263. * will only be able to set environment variables that begin with PHP_ (e.g.
  264. * PHP_FOO=BAR). Note: if this directive is empty, PHP will let the user
  265. * modify ANY environment variable! The safe_mode_protected_env_vars
  266. * directive contains a comma-delimited list of environment variables, that
  267. * the end user won't be able to change using putenv(). These variables will
  268. * be protected even if safe_mode_allowed_env_vars is set to allow changing
  269. * them.
  270. */
  271. <<__Native>>
  272. function putenv(string $setting): bool;
  273. /* Set the number of seconds a script is allowed to run. If this is reached,
  274. * the script returns a fatal error. The default limit is 30 seconds or, if it
  275. * exists, the max_execution_time value defined in the php.ini. When called,
  276. * set_time_limit() restarts the timeout counter from zero. In other words, if
  277. * the timeout is the default 30 seconds, and 25 seconds into script execution
  278. * a call such as set_time_limit(20) is made, the script will run for a total
  279. * of 45 seconds before timing out.
  280. */
  281. <<__Native>>
  282. function set_time_limit(int $seconds): void;
  283. /* Set the number of seconds into the request to invoke a callback.
  284. * The callback is invoked only once unless another call to this function is
  285. * made. This can help debugging scripts that take a long time to run.
  286. */
  287. <<__Native>>
  288. function set_pre_timeout_handler(int $seconds, mixed $callback): void;
  289. /* Returns the path of the directory PHP stores temporary files in by default.
  290. */
  291. <<__Native>>
  292. function sys_get_temp_dir(): string;
  293. /* version_compare() compares two "PHP-standardized" version number strings.
  294. * This is useful if you would like to write programs working only on some
  295. * versions of PHP. The function first replaces _, - and + with a dot . in
  296. * the version strings and also inserts dots . before and after any non number
  297. * so that for example '4.3.2RC1' becomes '4.3.2.RC.1'. Then it splits the
  298. * results like if you were using explode('.', $ver). Then it compares the
  299. * parts starting from left to right. If a part contains special version
  300. * strings these are handled in the following order: any string not found in
  301. * this list < dev < alpha = a < beta = b < RC = rc < # < pl = p. This way not
  302. * only versions with different levels like '4.1' and '4.1.2' can be compared
  303. * but also any PHP specific version containing development state.
  304. */
  305. <<__IsFoldable, __Rx, __Native>>
  306. function version_compare(string $version1,
  307. string $version2,
  308. string $sop = ""): mixed;
  309. } // root namespace
  310. namespace __SystemLib {
  311. class PhpInfo {
  312. private \DOMDocument $xml;
  313. private \DOMElement $body;
  314. public function __construct() {
  315. $this->xml = new \DOMDocument('1.0', 'UTF-8');
  316. $this->body = $this->element('body');
  317. }
  318. private function is_cli() { return \php_sapi_name() == 'cli'; }
  319. private function appendChildren(\DOMElement $el, ?varray $children) {
  320. if ($children) {
  321. foreach ($children as $v) {
  322. if ($v === null) {
  323. } else if ($v is \DOMElement) {
  324. $el->appendChild($v);
  325. } else if (\is_array($v)) {
  326. $this->appendChildren($el, $v);
  327. } else {
  328. $el->appendChild($this->xml->createTextNode($v));
  329. }
  330. }
  331. }
  332. }
  333. private function element(string $tag, ?darray $attr = null, ...$children) {
  334. $el = $this->xml->createElement($tag);
  335. if ($attr) {
  336. foreach ($attr as $k => $v) {
  337. $el->setAttribute($k, $v);
  338. }
  339. }
  340. $this->appendChildren($el, $children);
  341. return $el;
  342. }
  343. private function tr(string $l, mixed $d) {
  344. return
  345. $this->element(
  346. 'tr', darray[],
  347. $this->element('td', darray['class' => 'l'], $l),
  348. $this->element('td', darray['class' => 'r'], $d));
  349. }
  350. private function table(string $title, darray $data) {
  351. if ($this->is_cli()) {
  352. echo $title . "\n";
  353. echo "\n";
  354. foreach ($data as $k => $v) {
  355. echo $k . " => " . \print_r($v, true) . "\n";
  356. }
  357. echo "\n";
  358. } else {
  359. $children = darray[];
  360. foreach ($data as $k => $v) {
  361. \array_push(inout $children, $this->tr($k, \print_r($v, true)));
  362. }
  363. return varray[
  364. $this->element('hr'),
  365. $this->element('h2', darray[], $title),
  366. $this->element('table', darray[], $children)
  367. ];
  368. }
  369. }
  370. private function appendHead(\DOMElement $html) {
  371. $style =
  372. 'body { margin: auto; text-align: center; width: 600px; }' .
  373. 'hr { margin-top: 30px; }' .
  374. 'table { border-collapse: collapse; margin: auto; width: 100%; }' .
  375. 'td { border: 1px solid black; padding: 5px; }' .
  376. '.l { background-color: #CCF; }' .
  377. '.r { background-color: #CCC; word-break: break-all; }';
  378. $html->appendChild(
  379. $this->element(
  380. 'head', darray[],
  381. $this->element('title', darray[], "HHVM phpinfo"),
  382. $this->element('style', darray['type' => 'text/css'], $style)));
  383. }
  384. private function reportVersionTitle() {
  385. if ($this->is_cli()) {
  386. echo 'HHVM Version => ' . \HHVM_VERSION . "\n";
  387. } else {
  388. $this->body->appendChild(
  389. $this->element('h1', darray[], 'HHVM Version ' . \HHVM_VERSION));
  390. }
  391. }
  392. private function reportVersions() {
  393. if (!$this->is_cli()) {
  394. $this->body->appendChild($this->element('h2', darray[], 'Version'));
  395. }
  396. $data = darray[
  397. 'Version' => \HHVM_VERSION,
  398. 'Version ID' => \HHVM_VERSION_ID,
  399. 'Debug' => \HHVM_DEBUG,
  400. 'Compiler ID' => \HHVM_COMPILER_ID,
  401. 'Repo Schema' => \HHVM_REPO_SCHEMA,
  402. 'PHP Version' => \phpversion(),
  403. 'uname' => \php_uname()];
  404. $this->appendChildren($this->body, $this->table('Version', $data));
  405. }
  406. private function reportIni() {
  407. $this->appendChildren($this->body,
  408. $this->table('INI', \ini_get_all('', false)));
  409. }
  410. private function reportHeaders() {
  411. if (!\function_exists('getallheaders')) return;
  412. $this->appendChildren($this->body,
  413. $this->table('Headers', \getallheaders()));
  414. }
  415. private function reportMap(string $name, darray $map) {
  416. $data = darray[];
  417. foreach ($map as $k => $v) {
  418. $data[\sprintf("%s['%s']", $name, $k)] = $v;
  419. }
  420. $this->appendChildren($this->body, $this->table($name, $data));
  421. }
  422. public function report() {
  423. $html = $this->element('html');
  424. if (!$this->is_cli()) {
  425. $this->appendHead($html);
  426. $html->appendChild($this->body);
  427. }
  428. $this->reportVersionTitle();
  429. if (!$this->is_cli()) {
  430. $this->body->appendChild($this->element('hr'));
  431. }
  432. $this->reportVersions();
  433. $this->reportIni();
  434. $this->reportHeaders();
  435. $this->reportMap('$_SERVER', $_SERVER);
  436. $this->reportMap('$_ENV', $_ENV);
  437. if (!$this->is_cli()) {
  438. $this->body->appendChild($this->element('br'));
  439. $this->xml->appendChild($html);
  440. \header('content-type: text/html; charset=UTF-8');
  441. echo $this->xml->saveHTML();
  442. }
  443. }
  444. }
  445. }