Rewrite block rules to calculate their own blocks

Summary:
Following a simple request... T3321

Fixes T3321

Test Plan: Unit tests... eventually

Reviewers: epriestley

CC: aran, Korvin

Maniphest Tasks: T3321

Differential Revision: https://secure.phabricator.com/D6319
Add some block tests for D6319

Summary: See discussion in D6319. These tests cover the behavior of certain weird block edge case which we should preserve.

Test Plan: Ran unit tests.

Reviewers: garoevans

Reviewed By: garoevans

CC: aran

Differential Revision: https://secure.phabricator.com/D6325
Add phutil_exit function

Summary: Triggers an event before `exit`ing

Test Plan: Create a listener to handle the event.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Maniphest Tasks: T3469

Differential Revision: https://secure.phabricator.com/D6326
Shut down active write guard on exit

Summary:
Listen for the exit abruptly event, if it's triggered shut down the active write guard.
Depends on D6326

Test Plan: Use `phutil_exit` with an active write_guard, check no exception is thrown.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Maniphest Tasks: T3469

Differential Revision: https://secure.phabricator.com/D6327
Make inline remarkup rules have explicit priority

Summary:
Currently, inline remarkup rules have implicit priorities (based on the order they're added in). Priority is important; some rules need to apply before other rules.

We just kind of always add them in the right order currently, but this is error prone and creates a concrete problem in Phabricator, where we want applications to be able to add rules with both higher and lower priorities than the hyperlink rule (e.g., "http://example.com/D123" should not link as "D123", so the object rule should run later -- but "https://app.asana.com/..." should link as an Asana object, so the Asana rule should run earlier).

Allow rules to have explicit priorities so the order they're added in doesn't matter.

Test Plan: This just makes the current implicit order explicit. Ran unit tests.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Differential Revision: https://secure.phabricator.com/D6329
Minor, give the monospace rule an earlier priority.
Allow UNION queries to be detected as reads for CRSF purposes

Summary: Currently, we don't correctly detect queries in the form `(SELECT ...) UNION (SELECT ...)` as reads. Detect them.

Test Plan: No more CSRFs when doing UNIONs.

Reviewers: btrahan, vrana

Reviewed By: vrana

CC: aran

Differential Revision: https://secure.phabricator.com/D6342
Make csprintf() return an object, not a string

Summary:
See discussion in D6364. This didn't turn out to be as bad as I thought.

Make csprintf() return an object which implements `__toString()`, but renders in either masked or unmasked modes.

The only downside to this approach is that you must use "%P". It is otherwise completey effective in masking sensitive information, even from logs and traces:

  >>> [25] <exec> $ echo 'xxxxx'
  <<< [25] <exec> 3,246 us
  >>> [26] <exec> $ sh -c 'sh -c '\''sh -c '\''\'\'''\''echo '\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''xxxxx'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\'''\''\'\'''\'''\'''
  <<< [26] <exec> 6,922 us
     PASS   13ms★  testPasswords

This also works in the command-which-runs-a-command case, as above.

Test Plan:
  - Added unit tests.
  - Ran `arc unit --everything` in libphutil, arcanist and phabricator.
  - Browsed around Diffusion.

Reviewers: btrahan, mbishopim3

Reviewed By: btrahan

CC: aran

Differential Revision: https://secure.phabricator.com/D6366
Clean up some old scripts in libphutil

Summary:
  - `envinfo.php` has been obsoleted by:
    - `__init_script__.php` just sets all this stuff to the right value now.
    - `support/debug.php` provides a more general mechanism to get arbitrary environmental data from users.
  - `kill_init.php` facilitated the removal of `__init__.php` files, but we did that a long time ago and I haven't seen any users run into issues in many months.

Test Plan: N/A

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Differential Revision: https://secure.phabricator.com/D6377
Fix remarkup "NOTE" block syntax

Summary: Currently, HTML in NOTE blocks gets escaped, because the "rtrim()" applies to a PhutilSafeHTML object and implicitly converts it to (unsafe) string. Instead, trim first.

Test Plan: Typed `NOTE: **x**` into remarkup, saw bold "x".

Reviewers: btrahan, garoevans, vrana

Reviewed By: btrahan

CC: aran

Differential Revision: https://secure.phabricator.com/D6379
Add test coverage for PhutilErrorHandler and silence operator ("@")

Summary:
Fixes T3351. The situation is actually less dire than the comment implies, I had slightly misrembered context. Basically:

  - PhutilErrorHandler respects the silence operator ("@"); I'm not sure if it did at the time.
  - Part of the issue was stack traces for unrelated things, but PhutilOpaqueEnvelope solved that.

Test Plan:
  - Added test coverage for the beahvior of "@".
  - Hit LDAP exceptions and verified nothing sensitive appeared in the traces/logs.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T3351

Differential Revision: https://secure.phabricator.com/D6390
Add service call profiling to LDAP

Summary: We don't pick up LDAP calls in the service profiler, but should. Replaces some slightly more detailed but nonstandard and invasive debugging code, since LDAP seems stable now.

Test Plan:
  $ ~/src/php-src/sapi/cli/php -f ./bin/auth ldap --trace
  >>> [2] <connect> phabricator2_auth
  <<< [2] <connect> 1,755 us
  >>> [3] <query> SELECT * FROM `auth_providerconfig`  ORDER BY id DESC
  <<< [3] <query> 423 us
  Enter LDAP Credentials

      LDAP Username:  ldapuser
  >>> [4] <exec> $ stty -echo
  <<< [4] <exec> 10,370 us

      LDAP Password:  >>> [5] <exec> $ stty echo
  <<< [5] <exec> 6,844 us

  Connecting to LDAP...
  >>> [6] <ldap> connect (127.0.0.1:389)
  <<< [6] <ldap> 12,932 us
  >>> [7] <ldap> bind (sn=ldapuser,ou=People, dc=aphront, dc=com)
  <<< [7] <ldap> 6,860 us
  >>> [8] <ldap> search (ou=People, dc=aphront, dc=com, sn=ldapuser)
  <<< [8] <ldap> 5,907 us
  Found LDAP Account: ldapuser

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Differential Revision: https://secure.phabricator.com/D6391
Don't apply inline rules in Literal remarkup blocks

Summary:
  - Long ago, we applied HTML escaping in literal block rules (`%%% .. %%%`) by using an HTML inline rule.
    - We no longer need to do this, so literal blocks don't need to apply inline rules at all.
  - I think I imagined that certain block rules might want nonstandard inline rules when building Remarkup originally.
    - They never do and we don't have any use cases on the horizon where they might.

This allows us to simplify remarkup code in Phabricator.

Test Plan: Typed `%%% <strong>!!</strong> %%%` into Remarkup and made sure it was still escaped properly.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Differential Revision: https://secure.phabricator.com/D6395
Improve test for NOTE Remarkup rule

Test Plan:
  lang=diviner
  NOTE: Amazon SES **requires you to verify your "From" address**. Configure which
  "From" address to use by setting "##metamta.default-address##" in your config,
  then follow the Amazon SES verification process to verify it. You won't be able
  to send email until you do this!

Blame Rev: D6319

Reviewers: garoevans, epriestley

Reviewed By: epriestley

CC: aran, epriestley, btrahan, Korvin

Differential Revision: https://secure.phabricator.com/D6380
Allow extracting variables from XHPAST strings

Test Plan: New unit test.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D6405
Let PHP decide whether to use int or float in evalStatic()

Test Plan: `XHPASTTreeTestCase` on 32 bits.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D6412
Let PHP decide whether to use int or float in evalStatic()

Amends D6412.

Auditors: epriestley
Avoid autoloading self and parent in PHP 5.2

Summary: `call_user_func(array('parent', 'insert'))` issues E_STRICT in 5.1.2 <= PHP < 5.3.0.

Test Plan:
  class C extends D {
    function f() {
      call_user_func(array($this, 'self::g'));
    }
    function g() {
      echo "C::g()\n";
    }
  }
  $c = new C;
  $c->f();

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D6427
Respect Content-Type encoding in HTTP response status

Test Plan:
  $future = new HTTPSFuture('http://jakub.vrana.cz/');
  list($status) = $future->resolve();
  echo $status->getMessage() . "\n";

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D6428
Link to a specific cURL error

Test Plan:
  $url = 'http://www.suraski.net/blog/index.php?/feeds/index.rss2';
  $future = new HTTPSFuture($url);
  list($status) = $future->resolve();
  echo $status->getMessage() . "\n";

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D6429