?> 제거
replace 코드 디버깅 용도로 제거
This commit is contained in:
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"files.exclude": {
|
||||
"**/.git": true,
|
||||
"**/.svn": true,
|
||||
"**/.hg": true,
|
||||
"**/CVS": true,
|
||||
"**/.DS_Store": true,
|
||||
"**/logs/*.txt": true,
|
||||
|
||||
},
|
||||
"search.exclude": {
|
||||
"**/che_close": true,
|
||||
"e_lib/*":true,
|
||||
},
|
||||
}
|
||||
@@ -7,4 +7,3 @@ require_once(ROOT.W.F_CONFIG.W.APP.PHP);
|
||||
require_once(ROOT.W.F_FUNC.W.FUNC.PHP);
|
||||
|
||||
CustomHeader();
|
||||
?>
|
||||
|
||||
@@ -54,4 +54,3 @@ case "락풀기":
|
||||
}
|
||||
|
||||
echo "<script>location.replace('_119.php');</script>";
|
||||
?>
|
||||
|
||||
@@ -176,5 +176,3 @@ switch($btn) {
|
||||
}
|
||||
|
||||
echo "<script>location.replace('_admin1.php');</script>";
|
||||
?>
|
||||
|
||||
|
||||
@@ -428,5 +428,4 @@ switch($btn) {
|
||||
}
|
||||
|
||||
echo "<script>location.replace('_admin2.php');</script>";
|
||||
?>
|
||||
|
||||
|
||||
@@ -130,5 +130,3 @@ switch($btn) {
|
||||
}
|
||||
|
||||
echo "<script>location.replace('_admin3.php');</script>";
|
||||
?>
|
||||
|
||||
|
||||
@@ -103,5 +103,3 @@ switch($btn) {
|
||||
}
|
||||
|
||||
echo "<script>location.replace('_admin4.php');</script>";
|
||||
?>
|
||||
|
||||
|
||||
@@ -26,5 +26,3 @@ switch($btn) {
|
||||
}
|
||||
|
||||
echo "<script>location.replace('_admin5.php');</script>";
|
||||
?>
|
||||
|
||||
|
||||
@@ -1559,4 +1559,4 @@ function printSimul($game, $i) {
|
||||
</tr>";
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -254,4 +254,3 @@ function getHexCode($hex) {
|
||||
case 12: return "C"; case 13: return "D"; case 14: return "E"; case 15: return "F";
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
@@ -335,4 +335,3 @@ function getNewColor($type) {
|
||||
return $color;
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<script type="text/javascript">
|
||||
<!--<script type="text/javascript">
|
||||
|
||||
var _gaq = _gaq || [];
|
||||
_gaq.push(['_setAccount', '']);
|
||||
@@ -11,3 +11,4 @@
|
||||
})();
|
||||
|
||||
</script>
|
||||
-->
|
||||
@@ -213,4 +213,3 @@ if($btn == "판매") {
|
||||
}
|
||||
|
||||
Submit("b_auction.php", $msg, $msg2);
|
||||
?>
|
||||
|
||||
@@ -58,5 +58,3 @@ if($btn == "국가방침") {
|
||||
|
||||
echo "<script>location.replace('b_dipcenter.php');</script>";
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -124,5 +124,3 @@ if($command == 46) {
|
||||
echo "<script>location.replace('main.php');</script>";
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -115,5 +115,3 @@ if($genlist == 9999 && str_replace(" ", "", $msg) != "") {
|
||||
|
||||
echo "<script>location.replace('msglist.php');</script>";
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -252,5 +252,3 @@ if($btn == "추방") {
|
||||
|
||||
echo "<script>location.replace('b_myBossInfo.php');</script>";
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -18,5 +18,3 @@ MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
|
||||
|
||||
echo "<script>location.replace('b_nationrule.php');</script>";
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
include "lib.php";
|
||||
include "func.php";
|
||||
|
||||
//로그인 검사
|
||||
CheckLogin();
|
||||
$connect = dbConn();
|
||||
|
||||
@@ -54,5 +54,3 @@ if($btn == "부 대 창 설" && $name != "" && $me[troop] == 0) {
|
||||
echo "<script>location.replace('b_troop.php');</script>";
|
||||
|
||||
MYDB_close($connect);
|
||||
?>
|
||||
|
||||
|
||||
@@ -16,5 +16,3 @@ $result = MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),""
|
||||
|
||||
echo "<script>location.replace('b_myPage.php');</script>";
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
<?php /*
|
||||
localhost
|
||||
root
|
||||
root
|
||||
test
|
||||
?>
|
||||
*/
|
||||
@@ -116,5 +116,3 @@ pushGenLog($you, $youlog);
|
||||
|
||||
echo "<script>location.replace('msglist.php');</script>";
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -95,5 +95,3 @@ pushHistory($connect, $history);
|
||||
|
||||
echo "<script>location.replace('msglist.php');</script>";
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -100,5 +100,3 @@ pushHistory($connect, $history);
|
||||
|
||||
echo "<script>location.replace('msglist.php');</script>";
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -134,5 +134,3 @@ pushHistory($connect, $history);
|
||||
|
||||
echo "<script>location.replace('msglist.php');</script>";
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -159,6 +159,3 @@ pushAllLog($alllog);
|
||||
pushHistory($connect, $history);
|
||||
|
||||
echo "<script>location.replace('msglist.php');</script>";
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -7,4 +7,3 @@ require_once(ROOT.W.F_CONFIG.W.APP.PHP);
|
||||
require_once(ROOT.W.F_FUNC.W.FUNC.PHP);
|
||||
|
||||
CustomHeader();
|
||||
?>
|
||||
|
||||
@@ -124,5 +124,4 @@ pushHistory($connect, $history);
|
||||
|
||||
echo "<script>location.replace('msglist.php');</script>";
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
<?php/*jwh1807:유기체:13:127.0.0.1:1320151557*/?>
|
||||
<?php/*jwh1807:유기체:13:127.0.0.1:1320151557*/
|
||||
|
||||
@@ -244,5 +244,3 @@ function RegGeneral3($connect,$turnterm,$gencount,$nation,$level,$name,$leader,$
|
||||
) or Error(__LINE__.MYDB_error($connect),"");
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -253,5 +253,3 @@ function RegGeneral3($connect,$turnterm,$gencount,$nation,$level,$name,$leader,$
|
||||
) or Error(__LINE__.MYDB_error($connect),"");
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -114,5 +114,3 @@ function RegGeneral3($connect,$turnterm,$gencount,$nation,$level,$name,$leader,$
|
||||
) or Error(__LINE__.MYDB_error($connect),"");
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -143,5 +143,3 @@ function RegGeneral4($connect,$turnterm,$gencount,$nation,$level,$name,$leader,$
|
||||
) or Error(__LINE__.MYDB_error($connect),"");
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -7416,4 +7416,3 @@ function Submit($url, $msg="", $msg2="") {
|
||||
";
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -345,4 +345,3 @@ function processAuction($connect) {
|
||||
MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -128,4 +128,3 @@ function LogHistory($connect, $isFirst=0) {
|
||||
if(STEP_LOG) pushStepLog(date('Y-m-d H:i:s').', LogHistory Finish');
|
||||
return true;
|
||||
}
|
||||
?>
|
||||
|
||||
@@ -285,4 +285,3 @@ class HTTP{
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
@@ -1861,4 +1861,3 @@ function RiceBillRate($connect, $nation, $rate, $rice_rate, $type, $rice) {
|
||||
MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -6145,4 +6145,3 @@ function process_99($connect, &$general) {
|
||||
pushGenLog($general, $log);
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -301,4 +301,3 @@ class _String {
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -345,4 +345,3 @@ function insertCity($connect, $name, $level, $pop2, $agri2, $comm2, $secu2, $def
|
||||
) or Error(__LINE__.MYDB_error($connect),"");
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -6,5 +6,5 @@ $connect=dbConn();
|
||||
|
||||
LogHistory($connect, 1);
|
||||
|
||||
echo "<script>location.replace('index.php');</script>";
|
||||
?>
|
||||
//echo "<script>location.replace('index.php');</script>";
|
||||
echo "index.php"; //TODO:replace
|
||||
|
||||
@@ -83,7 +83,7 @@ if(!isTable($connect, "history",$dbname)) @MYDB_query($history_schema, $connect)
|
||||
|
||||
// 파일로 DB 정보 저장
|
||||
$file=@fopen("d_setting/set.php","w") or Error("set.php 파일 생성 실패<br><br>디렉토리의 퍼미션을 707로 주십시요","");
|
||||
@fwrite($file,"<?php /*\n$hostname\n$user_id\n$password\n$dbname\n */?>\n") or Error("set.php 파일 생성 실패<br><br>디렉토리의 퍼미션을 707로 주십시요","");
|
||||
@fwrite($file,"<?php /*\n$hostname\n$user_id\n$password\n$dbname\n */\n") or Error("set.php 파일 생성 실패<br><br>디렉토리의 퍼미션을 707로 주십시요","");
|
||||
@fclose($file);
|
||||
@mkdir("data",0707);
|
||||
@chmod("data",0707);
|
||||
@@ -93,5 +93,4 @@ $temp=MYDB_fetch_array(MYDB_query("select count(*) from general where level = '1
|
||||
|
||||
MYDB_close($connect);
|
||||
|
||||
echo "<script>location.replace('install2.php');</script>";
|
||||
?>
|
||||
echo "<!--<script>location.replace('install2.php');</script>-->install2.php"; //TODO:location.replace 살려두기
|
||||
|
||||
@@ -271,5 +271,3 @@ if($id_num) {
|
||||
</script>");
|
||||
echo("<script>location.replace('index.php');</script>");
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -272,4 +272,3 @@ if($id_num) {
|
||||
echo("<script>location.replace('index.php');</script>");
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -54,5 +54,3 @@ $response['intel'] = $abil['intel'];
|
||||
|
||||
usleep(100*1000);
|
||||
echo json_encode($response);
|
||||
|
||||
?>
|
||||
@@ -181,4 +181,3 @@ function PrintElapsedTime() {
|
||||
$_endTime = round(getMicroTime() - $_startTime, 3);
|
||||
echo "<table width=1000 align=center style=font-size:10;><tr><td align=right>경과시간 : {$_endTime}초</td></tr></table>";
|
||||
}
|
||||
?>
|
||||
|
||||
@@ -78,4 +78,3 @@ function DeleteSession() {
|
||||
}
|
||||
|
||||
MYDB_close($connect);
|
||||
?>
|
||||
|
||||
@@ -16,7 +16,7 @@ $id = $me[user_id];
|
||||
$pw = $me[password];
|
||||
$conmsg = $me[conmsg];
|
||||
|
||||
//¼¼¼Çº¯¼ö Á¦°Å
|
||||
//���Ǻ��� ����
|
||||
session_destroy();
|
||||
|
||||
echo "<script>location.replace('start.php');</script>";
|
||||
@@ -33,4 +33,3 @@ a
|
||||
</html>
|
||||
*/
|
||||
|
||||
?>
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
<?php
|
||||
phpinfo();
|
||||
?>
|
||||
@@ -7,4 +7,3 @@ require_once(ROOT.W.F_CONFIG.W.APP.PHP);
|
||||
require_once(ROOT.W.F_FUNC.W.FUNC.PHP);
|
||||
|
||||
CustomHeader();
|
||||
?>
|
||||
|
||||
+1
-1
@@ -8,4 +8,4 @@ smtp.gmail.com
|
||||
aser
|
||||
ewrwer
|
||||
@gmail.com
|
||||
*/?>
|
||||
*/
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
TODO : adobdb5, phpmailer5(?), plates, utilphp
|
||||
@@ -953,4 +953,4 @@ class MeekroDBEval {
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -2470,4 +2470,4 @@ class phpmailerException extends Exception {
|
||||
return $errorMsg;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
|
||||
@@ -407,4 +407,4 @@ class POP3 {
|
||||
|
||||
// End of class
|
||||
}
|
||||
?>
|
||||
|
||||
|
||||
@@ -814,4 +814,4 @@ class SMTP {
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/src/utilphp/util.php';
|
||||
|
||||
/**
|
||||
* Globally namespaced version of the class.
|
||||
*
|
||||
* @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
|
||||
*/
|
||||
class util extends \utilphp\util { }
|
||||
@@ -0,0 +1 @@
|
||||
service_name: travis-ci
|
||||
@@ -0,0 +1,4 @@
|
||||
test.php
|
||||
build/*
|
||||
report/*
|
||||
tests/test*
|
||||
@@ -0,0 +1,22 @@
|
||||
language: php
|
||||
|
||||
php:
|
||||
- 5.3
|
||||
- 5.4
|
||||
- 5.5
|
||||
- 5.6
|
||||
- hhvm
|
||||
|
||||
matrix:
|
||||
allow_failures:
|
||||
- php: hhvm
|
||||
|
||||
before_script:
|
||||
- pyrus install pear/PHP_CodeSniffer
|
||||
- wget -nc http://getcomposer.org/composer.phar
|
||||
- php composer.phar install --dev --no-interaction
|
||||
- mkdir -p build/logs
|
||||
- phpenv rehash
|
||||
|
||||
after_script:
|
||||
- php vendor/bin/coveralls
|
||||
@@ -0,0 +1,86 @@
|
||||
Change Log
|
||||
==========
|
||||
|
||||
1.1.0
|
||||
-----
|
||||
|
||||
This release introduces a deprecation notice for `slugify()`, please update your code.
|
||||
|
||||
* Added a cryptographically secure random string function `secure_random_string`. Thanks to @abhimanyusharma003 via [Pull Request #53](https://github.com/brandonwamboldt/utilphp/pull/53)
|
||||
* Added `limit_characters` and `limit_words` functions. Thanks to @abhimanyusharma003 via [Pull Request #55](https://github.com/brandonwamboldt/utilphp/pull/55)
|
||||
* Added `rmdir` method to recursively delete a directory. Thanks to @ARACOOOL via [Pull Request #56](https://github.com/brandonwamboldt/utilphp/pull/56)
|
||||
* Added `set_executable`, `set_readable`, `set_writable`, `directory_size`, `directory_contents` and `get_user_dir` functions. Thanks to @sergserg via [Pull Request #70](https://github.com/brandonwamboldt/utilphp/pull/70)
|
||||
* Changed parameter ordering for `slugify`, `$css_mode` is now the third argument. For backwards compatibility, the old order will still work but it will generate an `E_USER_DEPRECATED` warning. Thanks to @abhimanyusharma003 via [Pull Request #71](https://github.com/brandonwamboldt/utilphp/pull/71)
|
||||
* Added `match_string` method. Thanks to @abhimanyusharma003 via [Pull Request #72](https://github.com/brandonwamboldt/utilphp/pull/72)
|
||||
* Renamed internal methods (protected ones) for PSR-2 compliance
|
||||
* General performance improvements, code quality improvements, and increased unit test coverage (special thanks to @hopeseekr for getting us near 100% coverage)
|
||||
|
||||
1.0.7
|
||||
-----
|
||||
|
||||
* Added `fix_broken_serialization` to fix broken serialized strings (Thanks to @hopeseekr via [Pull Request #48](https://github.com/brandonwamboldt/utilphp/pull/48))
|
||||
* Fixed `get_current_url` appending port 80 or 443 when not needed (Thanks to @scottchiefbaker via [Pull Request #49](https://github.com/brandonwamboldt/utilphp/pull/49))
|
||||
* `var_dump` can now handle recursive data structures without crashing
|
||||
* `var_dump` code was minified and cleaned up
|
||||
* `array_flatten` was optimized (thanks to @hopeseekr via [Pull Request #47](https://github.com/brandonwamboldt/utilphp/pull/47))
|
||||
* `remove_accents` was completely rewritten and is now ~4x faster
|
||||
|
||||
1.0.6
|
||||
-----
|
||||
|
||||
* Added `start_with` function
|
||||
* Added `ends_with` function
|
||||
* Added `str_contains` function
|
||||
* Added `str_icontains` function
|
||||
* Added `get_file_ext` function
|
||||
* Fixing permissions on the files & directories
|
||||
* Fixing a bug with the include path of util.php
|
||||
|
||||
1.0.5
|
||||
-----
|
||||
|
||||
* [Issue #29](https://github.com/brandonwamboldt/utilphp/issues/29) Fixed error in `var_dump` if mbstring extension wasn't present
|
||||
* Adding Composer support
|
||||
* Updating license from GPL to MIT
|
||||
* Adding Changelog to project
|
||||
* Bumping minimum version to PHP 5.3.3
|
||||
|
||||
1.0.4
|
||||
-----
|
||||
|
||||
* [Issue #22](https://github.com/brandonwamboldt/utilphp/issues/22) Removed all superglobal *_get functions, you can use the modified array_get now
|
||||
* [Issue #22](https://github.com/brandonwamboldt/utilphp/issues/22) Modifed the behaviour of `array_get`, see documentation
|
||||
* [Pull Request #21](https://github.com/brandonwamboldt/utilphp/pull/21) Added multibyte support to html* functions
|
||||
* [Issue #9](https://github.com/brandonwamboldt/utilphp/issues/9) Removed the `str_to_utf8` function
|
||||
* [Issue #3](https://github.com/brandonwamboldt/utilphp/issues/3) Removed the `absint` function
|
||||
* Removed declare() from util.php to avoid errors
|
||||
* Updated PHPUnit tests to use PHPUnit 3.6
|
||||
|
||||
1.0.3
|
||||
-----
|
||||
|
||||
* [Issue #16](https://github.com/brandonwamboldt/utilphp/issues/16) Improved performance of `slugify`
|
||||
* [Issue #14](https://github.com/brandonwamboldt/utilphp/issues/14) Modified the regex for `seems_utf8` to be more accurate
|
||||
* [Issue #13](https://github.com/brandonwamboldt/utilphp/issues/13) Changed `validate_email` to be wrapper for `filter_var`
|
||||
* Added 'ok' to the list of yes words for `str_to_bool`
|
||||
* `str_to_bool` matches no followed by any number of 'o's
|
||||
|
||||
1.0.2
|
||||
-----
|
||||
|
||||
* [Issue #12](https://github.com/brandonwamboldt/utilphp/issues/12) `get_current_url` now includes the port and user/password if required
|
||||
* [Issue #11](https://github.com/brandonwamboldt/utilphp/issues/11) `human_time_diff` now uses the DateTime functions
|
||||
* [Issue #10](https://github.com/brandonwamboldt/utilphp/issues/10) `is_https` no longer checks the port as well
|
||||
|
||||
1.0.1
|
||||
-----
|
||||
|
||||
* [Issue #7](https://github.com/brandonwamboldt/utilphp/issues/7) Added the `$trust_proxy_headers` parameter to `get_client_ip`
|
||||
* [Issue #6](https://github.com/brandonwamboldt/utilphp/issues/6) Removed `is_isset` as the function did not work as intended
|
||||
* [Issue #6](https://github.com/brandonwamboldt/utilphp/issues/6) Removed `is_empty` as it is redundant, use ! with function calls instead
|
||||
* Fixed a bug with `get_gravatar`
|
||||
|
||||
1.0.0
|
||||
-----
|
||||
|
||||
* Initial release of util.php.
|
||||
@@ -0,0 +1,48 @@
|
||||
# CONTRIBUTORS
|
||||
|
||||
Util.php is the combined effort of many people who contributed new functions and features, bug fixes, tests, and documentation.
|
||||
|
||||
# Maintainer / Lead
|
||||
|
||||
* Brandon Wamboldt (http://brandonwamboldt.ca)
|
||||
|
||||
## Third-Party Projects
|
||||
|
||||
The following projects were fully or partially incorporated into util.php (and properly attributed).
|
||||
|
||||
* IntToWords (https://github.com/hemant347/IntToWords)
|
||||
* LinkifyURL (https://github.com/jmrware/LinkifyURL)
|
||||
* URLify.php (https://github.com/jbroadway/urlify/)
|
||||
* Pure PHP http_build_url (https://github.com/jakeasmith/http_build_url)
|
||||
|
||||
## Methods
|
||||
|
||||
The following developers and/or companies helped contribute to building util.php.
|
||||
|
||||
* PHP Experts, Inc. (http://www.phpexperts.pro/)
|
||||
- `array_flatten()`
|
||||
- `fix_broken_serialization()`
|
||||
- Lots of unit tests.
|
||||
* Tony Ferrara (ircmaxwell, http://blog.ircmaxell.com/)
|
||||
- `seemsUtf8Regex()`: http://stackoverflow.com/a/11709412/430062
|
||||
* Askar ([@ARACOOOL](https://github.com/ARACOOOL))
|
||||
- `rmdir()`
|
||||
- `limit_characters()`
|
||||
- `limit_words()`
|
||||
* Anonymous (tmp1000@fastmail.fm)
|
||||
- `htmlspecialentities()`: http://php.timesoft.cc/manual/en/function.htmlentities.php
|
||||
- `htmlspecialchars()`
|
||||
* Laravel Project (https://github.com/laravel/laravel)
|
||||
- `secure_random_string()`: http://trac.allenjb.me.uk/php/browser/tagi/vendor/laravel/framework/src/Illuminate/Support/Str.php?rev=87#L127
|
||||
* Abhimanyu Sharma ([@abhimanyusharma003](https://github.com/abhimanyusharma003))
|
||||
- `limit_characters()`
|
||||
- `limit_words()`
|
||||
- `match_string()`
|
||||
* Scott Baker ([@scottchiefbaker](https://github.com/scottchiefbaker))
|
||||
* [@sergserg](https://github.com/sergserg)
|
||||
- `set_writable()`
|
||||
- `set_executable()`
|
||||
- `set_readable()`
|
||||
- `directory_size()`
|
||||
- `directory_contents()`
|
||||
- `get_user_dir()`
|
||||
@@ -0,0 +1,24 @@
|
||||
The MIT License (MIT)
|
||||
=====================
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 Brandon Wamboldt
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
@@ -0,0 +1,123 @@
|
||||
# Util.php [](https://travis-ci.org/brandonwamboldt/utilphp) [](https://coveralls.io/r/brandonwamboldt/utilphp)
|
||||
|
||||
[UtilPHP](http://brandonwamboldt.github.com/utilphp/) (Aka util.php) is a
|
||||
collection of useful functions and snippets that you need or could use every
|
||||
day. It's implemented as a class with static methods, to avoid conflicts with
|
||||
your existing code-base. Just drop it in and start using it immediately.
|
||||
|
||||
Included are 55+ functions that provide you with the ability to do common
|
||||
tasks much easier and more efficiently, without having to find that one comment
|
||||
on php.net where you know it's been done already. Access superglobals without
|
||||
checking to see if certain indexes are set first and pass default values, use a
|
||||
nicely formatted var dump, validate emails, generate random strings, flatten an
|
||||
array, pull a single column out of a multidimensional array and much more.
|
||||
|
||||
Although it's implemented as one giant class, util.php has extensive
|
||||
documentation and a full suite of unit tests to avoid breaking
|
||||
backwards-compatibility unintentionally.
|
||||
|
||||
## Release Information
|
||||
|
||||
This repo contains in development code for future releases as well as the
|
||||
current stable branch. Development code is contained in the develop branch.
|
||||
|
||||
## Changelog and New Features
|
||||
|
||||
You can find a list of all changes for each release in the
|
||||
[official documentation](http://brandonwamboldt.github.com/utilphp/#changelog)
|
||||
|
||||
## Installation
|
||||
|
||||
### Server Requirements
|
||||
|
||||
* PHP version 5.3.3 or higher.
|
||||
|
||||
### Standalone File
|
||||
|
||||
Simply drop `util.php` in any project and call `include 'util.php';` in your
|
||||
project. You can then access the `Util` class.
|
||||
|
||||
### Composer
|
||||
|
||||
Add the following dependency to your composer.json:
|
||||
|
||||
```
|
||||
"brandonwamboldt/utilphp": "1.0.*"
|
||||
```
|
||||
|
||||
When used with composer, the class is namespaced (`\utilphp\util`) instead of
|
||||
just `util`.
|
||||
|
||||
## Contributing
|
||||
|
||||
UtilPHP is a community driven project and accepts contributions of code and
|
||||
documentation from the community. These contributions are made in the form of
|
||||
Issues or [Pull Requests](http://help.github.com/send-pull-requests/) on the
|
||||
[UtilityPHP repository](https://github.com/brandonwamboldt/utilphp) on GitHub.
|
||||
|
||||
Issues are a quick way to point out a bug. If you find a bug or documentation
|
||||
error in UtilityPHP then please check a few things first:
|
||||
|
||||
* There is not already an open Issue
|
||||
* The issue has already been fixed (check the develop branch, or look for closed Issues)
|
||||
* Is it something really obvious that you fix it yourself?
|
||||
|
||||
Reporting issues is helpful but an even better approach is to send a Pull
|
||||
Request, which is done by "Forking" the main repository and committing to your
|
||||
own copy. This will require you to use the version control system called Git.
|
||||
|
||||
## Guidelines
|
||||
|
||||
Before we look into how, here are the guidelines. If your Pull Requests fail to
|
||||
pass these guidelines it will be declined and you will need to re-submit when
|
||||
you’ve made the changes. This might sound a bit tough, but it is required for
|
||||
me to maintain quality of the code-base.
|
||||
|
||||
### PHP Style
|
||||
|
||||
Please ensure all new contributions match the [PSR-2](http://www.php-fig.org/psr/psr-2/)
|
||||
coding style guide.
|
||||
|
||||
### Documentation
|
||||
|
||||
If you change anything that requires a change to documentation then you will
|
||||
need to add it. New methods, parameters, changing default values, adding
|
||||
constants, etc are all things that will require a change to documentation. The
|
||||
change-log must also be updated for every change. Also PHPDoc blocks must be
|
||||
maintained.
|
||||
|
||||
### PHP Version Compatibility
|
||||
|
||||
UtilityPHP is compatible with PHP 5.3.3 so all code supplied must stick to this
|
||||
requirement.
|
||||
|
||||
Of particular note is avoiding short array notation like this:
|
||||
|
||||
```
|
||||
$var = [];
|
||||
```
|
||||
|
||||
Please use the old notation instead:
|
||||
|
||||
```
|
||||
$var = array();
|
||||
```
|
||||
|
||||
I know it's uglier, but PHP 5.3 while EOL'd, still isn't that old.
|
||||
|
||||
### Branching
|
||||
|
||||
One thing at a time: A pull request should only contain one change. That does
|
||||
not mean only one commit, but one change - however many commits it took. The
|
||||
reason for this is that if you change X and Y but send a pull request for both
|
||||
at the same time, we might really want X but disagree with Y, meaning we cannot
|
||||
merge the request. Using the Git-Flow branching model you can create new
|
||||
branches for both of these features and send two requests.
|
||||
|
||||
## License
|
||||
|
||||
UtilPHP is licensed under the MIT license.
|
||||
|
||||
## Resources
|
||||
|
||||
* [Documentation](http://brandonwamboldt.github.com/utilphp/)
|
||||
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "brandonwamboldt/utilphp",
|
||||
"type": "library",
|
||||
"version": "1.1.0",
|
||||
"description": "util.php is a collection of useful functions and snippets that you need or could use every day, designed to avoid conflicts with existing projects",
|
||||
"keywords": ["php", "utility", "collection", "helpers"],
|
||||
"homepage": "https://github.com/brandonwamboldt/utilphp",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Brandon Wamboldt",
|
||||
"email": "brandon.wamboldt@gmail.com"
|
||||
}
|
||||
],
|
||||
"autoload": {
|
||||
"psr-0": { "utilphp\\": "src/" }
|
||||
},
|
||||
"require-dev": {
|
||||
"satooshi/php-coveralls": "dev-master"
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.3.3"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
body {
|
||||
background: #f4f4f4 url('../images/background.png');
|
||||
color: #333;
|
||||
font: 18px/30px "Droid Serif", "Helvetica Neue", Helvetica, Arial;
|
||||
}
|
||||
|
||||
a {
|
||||
text-decoration: none;
|
||||
color: #f00;
|
||||
padding: 2px;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
background: #277585;
|
||||
color: #fff;
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
h1, h2, h3, h4, h5, h6 {
|
||||
padding-top: 20px;
|
||||
}
|
||||
|
||||
h2 {
|
||||
font-size: 40px;
|
||||
font-weight: normal;
|
||||
margin: 0 0 15px;
|
||||
}
|
||||
|
||||
p {
|
||||
margin: 20px 0;
|
||||
min-width: 550px;
|
||||
max-width: 900px;
|
||||
}
|
||||
|
||||
table, tr, td {
|
||||
margin: 0; padding: 0;
|
||||
}
|
||||
|
||||
td {
|
||||
padding: 2px 12px 2px 0;
|
||||
}
|
||||
|
||||
ul {
|
||||
list-style-type: circle;
|
||||
padding: 0 0 0 20px;
|
||||
}
|
||||
|
||||
li {
|
||||
width: 500px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
code, pre, tt {
|
||||
font-family: Monaco, Consolas, "Lucida Console", monospace;
|
||||
font-size: 12px;
|
||||
line-height: 18px;
|
||||
font-style: normal;
|
||||
}
|
||||
|
||||
tt {
|
||||
padding: 0px 3px;
|
||||
background: #fff;
|
||||
border: 1px solid #ddd;
|
||||
zoom: 1;
|
||||
}
|
||||
|
||||
code {
|
||||
margin-left: 20px;
|
||||
}
|
||||
|
||||
pre {
|
||||
font-size: 12px;
|
||||
padding: 12px;
|
||||
background: #fff;
|
||||
margin: 0px 0 30px;
|
||||
border-radius: 0 0 2px 2px;
|
||||
box-shadow: inset 0 0 0 1px rgba(0,0,0,.15);
|
||||
}
|
||||
|
||||
span.header {
|
||||
font-size: 24px;
|
||||
line-height: 30px;
|
||||
border-left: 10px solid #ddd;
|
||||
padding-left: 10px;
|
||||
}
|
||||
|
||||
.desc-text code {
|
||||
margin-left: 0;
|
||||
font-size: 18px;
|
||||
color: #0E04C8;
|
||||
}
|
||||
|
||||
#sidebar {
|
||||
background: #fff;
|
||||
position: fixed;
|
||||
top: 0; left: 0; bottom: 0;
|
||||
width: 220px;
|
||||
overflow-y: auto;
|
||||
overflow-x: hidden;
|
||||
padding: 5px 0 30px 10px;
|
||||
border-right: 1px solid #bbb;
|
||||
|
||||
box-shadow: 0 0 20px #ccc;
|
||||
-webkit-box-shadow: 0 0 20px #ccc;
|
||||
-moz-box-shadow: 0 0 20px #ccc;
|
||||
-webkit-overflow-scrolling: touch;
|
||||
}
|
||||
|
||||
.toc_title, .toc_title:visited {
|
||||
display: block;
|
||||
color: #000;
|
||||
margin-top: 15px;
|
||||
}
|
||||
|
||||
.toc_title:hover {
|
||||
background: none;
|
||||
color: #f00;
|
||||
}
|
||||
|
||||
.toc_title .version {
|
||||
font-size: 12px;
|
||||
font-weight: normal;
|
||||
}
|
||||
|
||||
.toc_section {
|
||||
font-size: 14px;
|
||||
line-height: 24px;
|
||||
margin: 5px 0 0 0;
|
||||
padding-left: 10px;
|
||||
list-style-type: none;
|
||||
}
|
||||
|
||||
.toc_section li {
|
||||
cursor: pointer;
|
||||
margin: 0 0 3px 0;
|
||||
}
|
||||
|
||||
.toc_section li a {
|
||||
text-decoration: none;
|
||||
color: black;
|
||||
}
|
||||
|
||||
.toc_section li a:hover {
|
||||
background: none;
|
||||
color: #f00;
|
||||
}
|
||||
|
||||
.container {
|
||||
min-width: 550px;
|
||||
max-width: 1000px;
|
||||
margin: 40px 0 50px 260px;
|
||||
}
|
||||
|
||||
#documentation section > article > p > code {
|
||||
color: #999;
|
||||
}
|
||||
|
||||
#documentation section > article > p > code > .util_class {
|
||||
color: #bbb;
|
||||
}
|
||||
|
||||
#documentation section > article > p > code > .func_name {
|
||||
color: #0E04C8;
|
||||
}
|
||||
|
||||
#documentation section > article > p > code > .param {
|
||||
color: #DA72F3;
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 803 B |
Binary file not shown.
|
After Width: | Height: | Size: 40 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.0 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 83 KiB |
@@ -0,0 +1,1234 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
|
||||
<link rel="canonical" href="http://includephp.brandonwamboldt.ca/" />
|
||||
<link rel="shortcut icon" href="favicon.png" type="image/png" />
|
||||
|
||||
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic" />
|
||||
<link rel="stylesheet" href="css/docs.css" />
|
||||
|
||||
<title>util.php - The PHP programmers best friend - UtilityPHP</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="sidebar" class="interface">
|
||||
<a class="toc_title" href="#">util.php <span class="version">(1.1.0)</span></a>
|
||||
|
||||
<a class="toc_title" href="#debug_functions">Debugging</a>
|
||||
|
||||
<ul class="toc_section">
|
||||
<li>- <a href="#var_dump">var_dump</a></li>
|
||||
</ul>
|
||||
|
||||
<a class="toc_title" href="#arrays">Arrays & Objects</a>
|
||||
|
||||
<ul class="toc_section">
|
||||
<li>- <a href="#array_first">array_first</a></li>
|
||||
<li>- <a href="#array_first_key">array_first_key</a></li>
|
||||
<li>- <a href="#array_flatten">array_flatten</a></li>
|
||||
<li>- <a href="#array_get">array_get</a></li>
|
||||
<li>- <a href="#array_last">array_last</a></li>
|
||||
<li>- <a href="#array_last_key">array_last_key</a></li>
|
||||
<li>- <a href="#array_map_deep">array_map_deep</a></li>
|
||||
<li>- <a href="#array_pluck">array_pluck</a></li>
|
||||
<li>- <a href="#array_search_deep">array_search_deep</a></li>
|
||||
<li>- <a href="#array_clean">array_clean</a></li>
|
||||
</ul>
|
||||
|
||||
<a class="toc_title" href="#urls">URLs</a>
|
||||
|
||||
<ul class="toc_section">
|
||||
<li>- <a href="#add_query_arg">add_query_arg</a></li>
|
||||
<li>- <a href="#get_current_url">get_current_url</a></li>
|
||||
<li>- <a href="#http_build_url">http_build_url</a></li>
|
||||
<li>- <a href="#is_https">is_https</a></li>
|
||||
<li>- <a href="#remove_query_arg">remove_query_arg</a></li>
|
||||
<li>- <a href="#slugify">slugify</a></li>
|
||||
</ul>
|
||||
|
||||
<a class="toc_title" href="#strings">Strings</a>
|
||||
|
||||
<ul class="toc_section">
|
||||
<li>- <a href="#htmlentities">htmlentities</a></li>
|
||||
<li>- <a href="#htmlspecialchars">htmlspecialchars</a></li>
|
||||
<li>- <a href="#linkify">linkify</a></li>
|
||||
<li>- <a href="#match_string">match_string</a></li>
|
||||
<li>- <a href="#random_string">random_string</a></li>
|
||||
<li>- <a href="#number_to_word">number_to_word</a></li>
|
||||
<li>- <a href="#ordinal">ordinal</a></li>
|
||||
<li>- <a href="#remove_accents">remove_accents</a></li>
|
||||
<li>- <a href="#secure_random_string">secure_random_string</a></li>
|
||||
<li>- <a href="#seems_utf8">seems_utf8</a></li>
|
||||
<li>- <a href="#safe_truncate">safe_truncate</a></li>
|
||||
<li>- <a href="#size_format">size_format</a></li>
|
||||
<li>- <a href="#str_to_bool">str_to_bool</a></li>
|
||||
<li>- <a href="#zero_pad">zero_pad</a></li>
|
||||
<li>- <a href="#strip_space">strip_space</a></li>
|
||||
<li>- <a href="#sanitize_string">sanitize_string</a></li>
|
||||
</ul>
|
||||
|
||||
|
||||
<a class="toc_title" href="#files">Files</a>
|
||||
|
||||
<ul class="toc_section">
|
||||
<li>- <a href="#full_permissions">full_permissions</a></li>
|
||||
<li>- <a href="#rmdir">rmdir</a></li>
|
||||
</ul>
|
||||
|
||||
<a class="toc_title" href="#serialization">Serialization</a>
|
||||
|
||||
<ul class="toc_section">
|
||||
<li>- <a href="#is_serialized">is_serialized</a></li>
|
||||
<li>- <a href="#maybe_serialize">maybe_serialize</a></li>
|
||||
<li>- <a href="#maybe_unserialize">maybe_unserialize</a></li>
|
||||
<li>- <a href="#fix_broken_serialization">fix_broken_serialization</a></li>
|
||||
</ul>
|
||||
|
||||
<a class="toc_title" href="#other">Other</a>
|
||||
|
||||
<ul class="toc_section">
|
||||
<li>- <a href="#force_download">force_download</a></li>
|
||||
<li>- <a href="#get_client_ip">get_client_ip</a></li>
|
||||
<li>- <a href="#get_gravatar">get_gravatar</a></li>
|
||||
<li>- <a href="#human_time_diff">human_time_diff</a></li>
|
||||
<li>- <a href="#nocache_headers">nocache_headers</a></li>
|
||||
<li>- <a href="#utf8_headers">utf8_headers</a></li>
|
||||
<li>- <a href="#validate_email">validate_email</a></li>
|
||||
</ul>
|
||||
|
||||
<a class="toc_title" href="#constants">Constants</a>
|
||||
|
||||
<ul class="toc_section">
|
||||
<li>- <a href="#SECONDS_IN_A_MINUTE">SECONDS_IN_A_MINUTE</a></li>
|
||||
<li>- <a href="#SECONDS_IN_AN_HOUR">SECONDS_IN_AN_HOUR</a></li>
|
||||
<li>- <a href="#SECONDS_IN_A_DAY">SECONDS_IN_A_DAY</a></li>
|
||||
<li>- <a href="#SECONDS_IN_A_WEEK">SECONDS_IN_A_WEEK</a></li>
|
||||
<li>- <a href="#SECONDS_IN_A_MONTH">SECONDS_IN_A_MONTH</a></li>
|
||||
<li>- <a href="#SECONDS_IN_A_YEAR">SECONDS_IN_A_YEAR</a></li>
|
||||
</ul>
|
||||
|
||||
<a class="toc_title" href="#changelog">Change Log</a>
|
||||
</div>
|
||||
|
||||
<div class="container">
|
||||
<p id="introduction">
|
||||
<img id="logo" src="images/utility_logo.png" alt="Util.php" />
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<a href="http://github.com/brandonwamboldt/utilphp/">util.php</a> is a collection of useful functions and snippets
|
||||
that you need or could use every day. It's implemented as a class with static methods, to avoid conflicts with your
|
||||
existing code-base. Just drop it in and start using it immediately.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Included are 60+ functions that provide you with the ability to do common tasks much easier and more efficiently,
|
||||
without having to find that one comment on php.net where you know its been done already. Access superglobals
|
||||
without checking to see if certain indexes are set first and pass default values, use a nicely formatted var dump,
|
||||
validate emails, generate random strings, flatten an array, pull a single column out of a multidimensional array
|
||||
and much more.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Although it's implemented as one giant class, util.php has extensive documentation and a full suite of unit tests
|
||||
to avoid breaking backwards-compatibility unintentionally.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
The project is <a href="http://github.com/brandonwamboldt/utilphp/">hosted on GitHub</a>.
|
||||
You can report bugs and discuss features on the
|
||||
<a href="https://github.com/brandonwamboldt/utilphp/issues">issues page</a>,
|
||||
or send tweets to <a href="http://twitter.com/brandonwamboldt">@brandonwamboldt</a>.
|
||||
</p>
|
||||
|
||||
<h2>Downloads <i style="padding-left: 12px; font-size:12px;">(Right-click, and use "Save As")</i></h2>
|
||||
|
||||
<p>
|
||||
<a href="https://github.com/brandonwamboldt/utilphp/archive/1.1.0.zip">Stable Version (1.1.0)</a><br>
|
||||
<a href="https://github.com/brandonwamboldt/utilphp/zipball/master">Unstable Version (master)</a>
|
||||
</p>
|
||||
|
||||
<h2>Installation</h2>
|
||||
|
||||
<span class="header">Server Requirements</span><br>
|
||||
|
||||
<ul>
|
||||
<li>PHP 5.3.3+</li>
|
||||
</ul>
|
||||
|
||||
<span class="header">Standalone File</span><br>
|
||||
|
||||
<p class="desc-text">
|
||||
Simply drop <code>util.php</code> in any project and call <code>include 'util.php';</code> in your project.
|
||||
You can then access the <code>Util</code> class.
|
||||
</p>
|
||||
|
||||
<span class="header">Composer</span><br>
|
||||
|
||||
<p class="desc-text">
|
||||
Add the following dependency to your composer.json:
|
||||
|
||||
<pre>"brandonwamboldt/utilphp": "1.1.*"</pre>
|
||||
|
||||
When used with composer, the class is namespaced (<code>\utilphp\util</code>) instead of
|
||||
just <code>util</code>.
|
||||
</p>
|
||||
|
||||
<div id="documentation">
|
||||
<h2 id="debug_functions">Debugging Functions</h2>
|
||||
|
||||
<section>
|
||||
<article id="var_dump">
|
||||
<p>
|
||||
<span class="header">var_dump</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">var_dump</span>(mixed <span class="param">$var</span>[, bool <span class="param">$return</span> = FALSE])
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Nicely prints out the contents of a passed variable. Booleans are properly displayed as 'true' or 'false',
|
||||
null values are shown as 'NULL', variable types are displayed, and arrays and objects are displayed as a
|
||||
collapsible tree powered by JavaScript.
|
||||
</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre class="usage-example">util::var_dump($var);</pre>
|
||||
|
||||
<img src="images/awesome_var_dump.jpg" style="border: 1px solid #ccc;" />
|
||||
</article>
|
||||
</section>
|
||||
|
||||
<h2 id="arrays">Array/Object Functions</h2>
|
||||
|
||||
<section>
|
||||
<article id="array_first">
|
||||
<p>
|
||||
<span class="header">array_first</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">array_first</span>(array <span class="param">$array</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Retrieve the first value from an array. Can be run on function callbacks, and it will not modify the pointer
|
||||
of the source array unlike most other methods of doing this.
|
||||
</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::array_first( ['a', 'b', 'c'] );<br />=> Returns 'a'</pre>
|
||||
</article>
|
||||
|
||||
<article id="array_first_key">
|
||||
<p>
|
||||
<span class="header">array_first_key</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">array_first_key</span>(array <span class="param">$array</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Retrieve the first key from an array. Can be run on function callbacks, and it will not modify the pointer of
|
||||
the source array unlike most other methods of doing this.
|
||||
</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::array_first_key( $users );<br />=> Returns 'brandon'</pre>
|
||||
</article>
|
||||
|
||||
<article id="array_flatten">
|
||||
<p>
|
||||
<span class="header">array_flatten</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">array_flatten</span>(array <span class="param">$array</span>[, bool <span class="param">$preserve_keys</span> = TRUE])
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Flattens a multi-dimensional array into a one dimensional array</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::array_flatten( [ 'a', 'b', [ 'c', 'd', 'e', [ 'f', 'g', [ [ [ [ 'h', 'i', 'j' ] ] ] ] ] ] ] );
|
||||
=> Returns [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' ]</pre>
|
||||
</article>
|
||||
|
||||
<article id="array_get">
|
||||
<p>
|
||||
<span class="header">array_get</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">array_get</span>(<span class="param">$var</span>[, mixed <span class="param">$default</span> = NULL])
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Retrieve a value from an array, or return a given default if the index isn't set</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::array_get($_POST['action'], 'index');
|
||||
=> If $_POST['action'] is set, returns its value, otherwise returns 'index'
|
||||
util::array_get($_POST['action']['do'], 'index');
|
||||
=> Returns the value of $_POST['action']['do'] or 'index'</pre>
|
||||
</article>
|
||||
|
||||
<article id="array_last">
|
||||
<p>
|
||||
<span class="header">array_last</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">array_last</span>(array <span class="param">$array</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Retrieve the last value from an array. Can be run on function callbacks, and it will not modify the pointer
|
||||
of the source array unlike most other methods of doing this.
|
||||
</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::array_last( [ 'a', 'b', 'c'] );
|
||||
=> Returns 'c'</pre>
|
||||
</article>
|
||||
|
||||
<article id="array_last_key">
|
||||
<p>
|
||||
<span class="header">array_last_key</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">array_last_key</span>(array <span class="param">$array</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Retrieve the last key from an array. Can be run on function callbacks, and it will not modify the pointer of
|
||||
the source array unlike most other methods of doing this.
|
||||
</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::array_last_key( $users );
|
||||
=> Returns 'zane'</pre>
|
||||
</article>
|
||||
|
||||
<article id="array_map_deep">
|
||||
<p>
|
||||
<span class="header">array_map_deep</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">array_map_deep</span>(array <span class="param">$array</span>, callable <span class="param">$callback</span>[, bool <span class="param">$on_nonscalar</span> = FALSE])
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Returns an array containing all the elements of $array after applying the callback function to each one recursively.
|
||||
Particularly useful for avoiding errors when calling functions that only accept scalar values on an array that could
|
||||
contain nested arrays, such as $_GET or $_POST.
|
||||
</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::array_map_deep($_POST, 'htmlentities');
|
||||
=> Recursively escapes each scalar value in $_POST
|
||||
util::array_map_deep($_POST, 'htmlentities', TRUE);
|
||||
=> Recursively escapes each value in $_POST regardless of its type</pre>
|
||||
</article>
|
||||
|
||||
<article id="array_pluck">
|
||||
<p>
|
||||
<span class="header">array_pluck</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">array_pluck</span>(array <span class="param">$array</span>, string <span class="param">$field</span>[, bool <span class="param">$preserve_keys</span>, bool <span class="param">$remove_nomatches</span>])
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Replaces each value in an array with the specified field of the array or object that used to be the value. Very
|
||||
useful when you have an array of objects or arrays from a database, and you only want one specific field. For
|
||||
example, you want an array of emails from an array of users.
|
||||
</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::array_pluck([['val' => 1], ['val' => 2], ['val' => 3]], 'val');
|
||||
=> Returns [1, 2, 3]
|
||||
util::array_pluck($users, 'email');
|
||||
=> Returns an array of email addresses for each user</pre>
|
||||
</article>
|
||||
|
||||
<article id="array_search_deep">
|
||||
<p>
|
||||
<span class="header">array_search_deep</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">array_search_deep</span>(array <span class="param">$array</span>, mixed <span class="param">$search</span>[, string <span class="param">$field</span> = FALSE])
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Allows you to search for a value in an array of arrays or an array of objects, and return the key of the value
|
||||
that matches the search criteria. If $field is unspecified, the entire array/object is searched.
|
||||
</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::array_search_deep($_POST, 'delete_post');
|
||||
=> Might return 'do' if $_POST['do']['action'] = 'delete_post'
|
||||
util::array_search_deep($users, 'rogue_coder', 'username');
|
||||
=> Might return 5 if $users[5]->username = 'rogue_coder'</pre>
|
||||
</article>
|
||||
|
||||
<article id="array_clean">
|
||||
<p>
|
||||
<span class="header">array_clean</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">array_clean</span>(array <span class="param">$array</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Remove all null/empty/false values from the given array.
|
||||
</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::array_clean( array( 'a', 'b', '', null, false, 0) );<br />=> Returns array('a', 'b');</pre>
|
||||
</article>
|
||||
|
||||
</section>
|
||||
|
||||
<h2 id="urls">URL Functions</h2>
|
||||
|
||||
<section>
|
||||
<article id="add_query_arg">
|
||||
<p>
|
||||
<span class="header">add_query_arg</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">add_query_arg</span>()
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Adds one ore more query args to the query string of the current URL or a given URL</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::add_query_arg( 'user', '5', '/admin/users?action=edit' );
|
||||
=> Returns '/admin/users?action=edit&user=5'
|
||||
util::add_query_arg( [ 'user' => 5, 'action' => 'edit' ], '/admin/users' );
|
||||
=> Returns '/admin/users?user=5&action=edit'</pre>
|
||||
</article>
|
||||
|
||||
<article id="get_current_url">
|
||||
<p>
|
||||
<span class="header">get_current_url</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">get_current_url</span>()
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Returns the current URL with hostname</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::get_current_url();
|
||||
=> Returns 'http://brandonwamboldt.ca/utilphp/'</pre>
|
||||
</article>
|
||||
|
||||
<article id="is_https">
|
||||
<p>
|
||||
<span class="header">is_https</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">is_https</span>()
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Returns true if the current page is being loaded over https, false if it isn't</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::is_https();
|
||||
=> Returns false</pre>
|
||||
</article>
|
||||
|
||||
<article id="http_build_url">
|
||||
<p>
|
||||
<span class="header">http_build_url</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">http_build_url</span>([mixed $url[, mixed $parts[, int $flags = HTTP_URL_REPLACE[, array &amo;$new_url]]]])
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Pure PHP implementation/polyfill for <a href="http://php.net/http_build_url">http_build_url</a>
|
||||
which requires pecl_http to be installed.
|
||||
</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>echo util::http_build_url("http://user@www.example.com/pub/index.php?a=b#files",
|
||||
array(
|
||||
"scheme" => "ftp",
|
||||
"host" => "ftp.example.com",
|
||||
"path" => "files/current/",
|
||||
"query" => "a=c"
|
||||
),
|
||||
HTTP_URL_STRIP_AUTH | HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY | HTTP_URL_STRIP_FRAGMENT
|
||||
);</pre>
|
||||
|
||||
<p>The above example will output:</p>
|
||||
|
||||
<pre>ftp://ftp.example.com/pub/files/current/?a=c</pre>
|
||||
</article>
|
||||
|
||||
<article id="is_https">
|
||||
<p>
|
||||
<span class="header">is_https</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">is_https</span>()
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Returns true if the current page is being loaded over https, false if it isn't</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::is_https();
|
||||
=> Returns false</pre>
|
||||
</article>
|
||||
|
||||
<article id="remove_query_arg">
|
||||
<p>
|
||||
<span class="header">remove_query_arg</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">remove_query_arg</span>( array|string <span class="param">$query_args</span>[, string <span class="param">$url</span>])
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Removes one ore more query args from the query string of the current URL or a given URL</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::remove_query_arg( 'action', '/admin/users?action=edit&user=5' );
|
||||
=> Returns '/admin/users?user=5'
|
||||
util::remove_query_arg( [ 'user', 'action'], '/admin/users?action=edit&user=5' );
|
||||
=> Returns '/admin/users'</pre>
|
||||
</article>
|
||||
|
||||
<article id="slugify">
|
||||
<p>
|
||||
<span class="header">slugify</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">slugify</span>(string <span class="param">$string</span>[, string <span class="param">$separator</span> = "-", bool <span class="param">$css_mode</span> = FALSE])
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Generate a string safe for use in URLs from any given string. Converts any accent characters
|
||||
to their equivalent normal characters and converts any other non-alphanumeric characters to
|
||||
dashes, then converts any sequence of two or more dashes to a single dash. This function
|
||||
generates slugs safe for use as URLs, and if you pass TRUE as the second parameter, it will
|
||||
create strings safe for use as CSS classes or IDs.
|
||||
</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::slugify('This is a random --string with an Ãccent');
|
||||
=> Returns 'this-is-a-random-string-with-an-accent'
|
||||
util::slugify('Another String', '.');
|
||||
=> Returns 'another.string'</pre>
|
||||
</article>
|
||||
</section>
|
||||
|
||||
<h2 id="strings">String Functions</h2>
|
||||
|
||||
<section>
|
||||
<article id="htmlentities">
|
||||
<p>
|
||||
<span class="header">htmlentities</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">htmlentities</span>(string <span class="param">$string</span>[, bool <span class="param">$preserve_encoded_entities</span> = FALSE])
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Executes htmlentities with ENT_QUOTES set by default. However, if you pass TRUE as
|
||||
the second parameter, it will not re-encode entities that are already encoded.
|
||||
</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::htmlentities('this string < this string', TRUE);
|
||||
=> Returns 'this string < this string'</pre>
|
||||
</article>
|
||||
|
||||
<article id="htmlspecialchars">
|
||||
<p>
|
||||
<span class="header">htmlspecialchars</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">htmlspecialchars</span>(string <span class="param">$string</span>[, bool <span class="param">$preserve_encoded_entities</span> = FALSE])
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Executes htmlspecialchars with ENT_QUOTES set by default. However, if you pass TRUE as
|
||||
the second parameter, it will not re-encode entities that are already encoded.
|
||||
</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::htmlspecialchars('this string < this string', TRUE);
|
||||
=> Returns 'this string < this string'</pre>
|
||||
</article>
|
||||
|
||||
<article id="linkify">
|
||||
<p>
|
||||
<span class="header">linkify</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">linkify</span>(string <span class="param">$text</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Find's any URLs in the specified text and wraps them with HTML anchor tags</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::linkify('this string has a link to www.google.com');
|
||||
=> Returns 'this string has a link to <a href="http://wwww.google.com/">www.google.com</a>'</pre>
|
||||
</article>
|
||||
|
||||
<article id="match_string">
|
||||
<p>
|
||||
<span class="header">match_string</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">match_string</span>(string <span class="param">$pattern</span>, string <span class="param">$string</span>[, bool <span class="param">$caseSensitive</span> = true])
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Check if a string matches a given pattern. You can use '*' as a wildcard character.
|
||||
</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::match_string("test/*", "test/my/test");
|
||||
=> Returns true
|
||||
util::match_string("test/", "test/my/test");
|
||||
=> Returns false</pre>
|
||||
</article>
|
||||
|
||||
<article id="random_string">
|
||||
<p>
|
||||
<span class="header">random_string</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">random_string</span>(int <span class="param">$length</span>[, bool <span class="param">$human_friendly</span> = TRUE, bool <span class="param">$include_symbols</span> = FALSE, bool <span class="param">$no_duplicate_chars</span> = FALSE])
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Generates a random string of the specified length. Human friendly mode
|
||||
will exclude characters that can be confused with other characters, such
|
||||
as 0 (zero) and O (capital o), 1 (one) and l (lowercase L).
|
||||
</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::random_string(8);
|
||||
=> Returns 'a6BiF4UW'</pre>
|
||||
</article>
|
||||
|
||||
<article id="number_to_word">
|
||||
<p>
|
||||
<span class="header">number_to_word</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">number_to_word</span>(int|float <span class="param">$number</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Converts a number into its text equivalent. For example, 56 becomes fifty-six.</p>
|
||||
|
||||
<pre>util::number_to_word(512.5);
|
||||
=> Returns 'five hundred and twelve point five'</pre>
|
||||
</article>
|
||||
|
||||
<article id="ordinal">
|
||||
<p>
|
||||
<span class="header">ordinal</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">ordinal</span>(int <span class="param">$number</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Returns the ordinal version of a number (appends th, st, nd, rd)</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::ordinal(22);
|
||||
=> Returns '22nd'</pre>
|
||||
</article>
|
||||
|
||||
<article id="remove_accents">
|
||||
<p>
|
||||
<span class="header">remove_accents</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">remove_accents</span>(string <span class="param">$string</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Converts all accent characters to their ASCII equivalents</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::remove_accents('Àccent');
|
||||
=> Returns 'Accent'</pre>
|
||||
</article>
|
||||
|
||||
<article id="secure_random_string">
|
||||
<p>
|
||||
<span class="header">secure_random_string</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">secure_random_string</span>(int <span class="param">$length</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Generate a random string of characters. Will attempt to use a secure source (openssl).</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::secure_random_string(16);
|
||||
=> Returns '5bB1RJH0cQhNjviT'</pre>
|
||||
</article>
|
||||
|
||||
<article id="seems_utf8">
|
||||
<p>
|
||||
<span class="header">seems_utf8</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">seems_utf8</span>(string <span class="param">$string</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Checks to see if a given string is UTF-8 encoded/safe</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::seems_utf8('This is some random string user input');
|
||||
=> Returns true</pre>
|
||||
</article>
|
||||
|
||||
<article id="safe_truncate">
|
||||
<p>
|
||||
<span class="header">safe_truncate</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">safe_truncate</span>(string <span class="param">$string</span>, int <span class="param">$length</span>[, string <span class="param">$append</span> = '...'])
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Truncate a string to a specified length without cutting a word off</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::safe_truncate('The quick brown fox jumps over the lazy dog', 24);
|
||||
=> Returns 'The quick brown fox...'</pre>
|
||||
</article>
|
||||
|
||||
<article id="size_format">
|
||||
<p>
|
||||
<span class="header">size_format</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">size_format</span>(int <span class="param">$bytes</span>[, int <span class="param">$decimals</span> = 0])
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Formats an integer as a human friendly size string, such as 4 KiB.</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::size_format( 25151251, 2 );
|
||||
=> Returns '23.99 MiB'</pre>
|
||||
</article>
|
||||
|
||||
<article id="str_to_bool">
|
||||
<p>
|
||||
<span class="header">str_to_bool</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">str_to_bool</span>(string <span class="param">$string</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Converts a string to boolean, looking for yes/no words like 'yes', 'no', 'true', 'false', 'y', 'n', etc</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::str_to_bool('yes');
|
||||
=> Returns true</pre>
|
||||
</article>
|
||||
|
||||
<article id="zero_pad">
|
||||
<p>
|
||||
<span class="header">zero_pad</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">zero_pad</span>(int <span class="param">$number</span>, int <span class="param">$length</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Pads a given number with zeroes on the left</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::zero_pad(341, 8);
|
||||
=> Returns '00000341'</pre>
|
||||
</article>
|
||||
|
||||
<article id="strip_space">
|
||||
<p>
|
||||
<span class="header">strip_space</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">strip_space</span>(string <span class="param">$string</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Strip all withspace from the given string</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::strip_space(' The quick brown fox jumps over the lazy dog ');
|
||||
=> Returns 'Thequickbrownfoxjumpsoverthelazydog'</pre>
|
||||
</article>
|
||||
|
||||
<article id="sanitize_string">
|
||||
<p>
|
||||
<span class="header">sanitize_string</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">sanitize_string</span>(string <span class="param">$string</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Sanitize a string by performing the following operation :
|
||||
<ul>
|
||||
<li>Remove accents</li>
|
||||
<li>Lower the string</li>
|
||||
<li>Remove punctuation characters</li>
|
||||
<li>Strip whitespaces</li>
|
||||
</ul>
|
||||
|
||||
</p>
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::sanitize_string(' Benoit! à New-York? j’ai perçu 1 % : Qu’as-tu "gagné" chez M. V. Noël? Dix francs.');
|
||||
=> Returns 'benoitanewyorkjaipercu1quastugagnechezmvnoeldixfrancs'</pre>
|
||||
</article>
|
||||
</section>
|
||||
|
||||
<h2 id="files">Files</h2>
|
||||
|
||||
<section>
|
||||
<article id="full_permissions">
|
||||
<p>
|
||||
<span class="header">full_permissions</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">full_permissions</span>(string <span class="param">$file</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Returns the file permissions as a nice string, like -rw-r--r--</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::full_permissions('/etc/passwd');
|
||||
=> Returns '-rw-r--r--'</pre>
|
||||
</article>
|
||||
|
||||
<article id="rmdir">
|
||||
<p>
|
||||
<span class="header">rmdir</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">rmdir</span>(string <span class="param">$dir</span>[, bool <span class="param">$traverseSymlinks</span> = false])
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Recursively deletes a directories (including all contents).</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::rmdir('/tmp/working/dir');
|
||||
=> Returns true</pre>
|
||||
</article>
|
||||
</section>
|
||||
|
||||
<h2 id="serialization">Serialization</h2>
|
||||
|
||||
<section>
|
||||
<article id="is_serialized">
|
||||
<p>
|
||||
<span class="header">is_serialized</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">is_serialized</span>(mixed <span class="param">$data</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Checks to see if a given variable is a seralized data structure</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::is_serialized( 'a:0:{}' );
|
||||
=> Returns true</pre>
|
||||
</article>
|
||||
|
||||
<article id="maybe_serialize">
|
||||
<p>
|
||||
<span class="header">maybe_serialize</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">maybe_serialize</span>(mixed <span class="param">$data</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Serialize data, if needed to store in plaintext (Arrays or objects)</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::maybe_serialize( 5 );
|
||||
=> Returns 5
|
||||
util::maybe_serialize( array() );
|
||||
=> Returns 'a:0:{}'</pre>
|
||||
</article>
|
||||
|
||||
<article id="maybe_unserialize">
|
||||
<p>
|
||||
<span class="header">maybe_unserialize</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">maybe_unserialize</span>(mixed <span class="param">$data</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Unserialize data, if it's a serialized string</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::maybe_unserialize(5);
|
||||
=> Returns 5
|
||||
util::maybe_unserialize("a:0:{}");
|
||||
=> Returns array()</pre>
|
||||
</article>
|
||||
|
||||
<article id="fix_broken_serialization">
|
||||
<p>
|
||||
<span class="header">fix_broken_serialization</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">fix_broken_serialization</span>(mixed <span class="param">$data</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Attempt to fix a broken serialization string (e.g. where string offsets are incorrect). Can fix
|
||||
errors that frequently occur with mismatched character sets or higher-than-ASCII characters.
|
||||
</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::fix_broken_serialization('a:1:{s:4:"test";s:4:"abc";}');
|
||||
=> Returns 'a:1:{s:4:"test";s:3:"abc";}'</pre>
|
||||
</article>
|
||||
</section>
|
||||
|
||||
<h2 id="other">Other</h2>
|
||||
|
||||
<section>
|
||||
<article id="force_download">
|
||||
<p>
|
||||
<span class="header">force_download</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">force_download</span>(string <span class="param">$filename</span>, string <span class="param">$content</span> = FALSE)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Transmit headers that force a browser to display the download file dialog. Cross
|
||||
browser compatible. Only fires if headers have not already been sent.
|
||||
</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::force_download( 'export.csv', file_get_contents( 'securefile.csv' ) );
|
||||
=> The user will be prompted to download the file</pre>
|
||||
</article>
|
||||
|
||||
<article id="get_client_ip">
|
||||
<p>
|
||||
<span class="header">get_client_ip</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">get_client_ip</span>(bool <span class="param">$trust_proxy_headers</span> = FALSE)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Returns the IP address of the client. Only enable $trust_proxy_headers if your server is behind a proxy
|
||||
that sets the HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR headers.
|
||||
</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::get_client_ip();
|
||||
=> Returns '12.123.12.100'</pre>
|
||||
</article>
|
||||
|
||||
<article id="get_gravatar">
|
||||
<p>
|
||||
<span class="header">get_gravatar</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">get_gravatar</span>(string <span class="param">$email</span>, int <span class="param">$size</span> = 32)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Return the URL to a user's gravatar</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::get_gravatar('brandon.wamboldt@gmail.com');
|
||||
=> Returns 'http://www.gravatar.com/avatar/46679faeb6780ecb1ea57527fdc66eb3?s=32'</pre>
|
||||
</article>
|
||||
|
||||
<article id="human_time_diff">
|
||||
<p>
|
||||
<span class="header">human_time_diff</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">human_time_diff</span>(int <span class="param">$from</span>, int <span class="param">$to</span> = '', int <span class="param">$as_text</span> = FALSE, string <span class="param">$suffix</span> = ' ago')
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Converts a unix timestamp to a relative time string, such as "3 days ago" or "2 weeks ago"</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::human_time_diff(time() - 7400);
|
||||
=> Returns '2 hours ago'
|
||||
util::human_time_diff(time() - 7400, '', TRUE);
|
||||
=> Returns 'two hours ago'</pre>
|
||||
</article>
|
||||
|
||||
<article id="nocache_headers">
|
||||
<p>
|
||||
<span class="header">nocache_headers</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">nocache_headers</span>(mixed <span class="param">$data</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Sets the headers to prevent caching for the different browsers</p>
|
||||
|
||||
<p>
|
||||
Different browsers support different nocache headers, so several headers must be sent
|
||||
so that all of them get the point that no caching should occur
|
||||
</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::nocache_headers();</pre>
|
||||
</article>
|
||||
|
||||
<article id="utf8_headers">
|
||||
<p>
|
||||
<span class="header">utf8_headers</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">utf8_headers</span>(mixed <span class="param">$content_type</span> = 'text/html')
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Transmit UTF-8 content headers if the headers haven't already been sent</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::utf8_headers();</pre>
|
||||
</article>
|
||||
|
||||
<article id="validate_email">
|
||||
<p>
|
||||
<span class="header">validate_email</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">validate_email</span>(string <span class="param">$possible_email</span>)
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>Validate an email address</p>
|
||||
|
||||
<p><strong>Usage:</strong></p>
|
||||
|
||||
<pre>util::validate_email( 'brandon.wamboldt@gmail.com' );
|
||||
=> Returns true
|
||||
util::validate_email( 'not an email' );
|
||||
=> Returns false</pre>
|
||||
</article>
|
||||
</section>
|
||||
|
||||
<h2 id="constants">Constants</h2>
|
||||
|
||||
<section>
|
||||
<article id="SECONDS_IN_A_MINUTE">
|
||||
<p>
|
||||
<span class="header">SECONDS_IN_A_MINUTE</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">SECONDS_IN_A_MINUTE</span>
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>The number of seconds in a minute, useful for making code more verbose</p>
|
||||
</article>
|
||||
|
||||
<article id="SECONDS_IN_AN_HOUR">
|
||||
<p>
|
||||
<span class="header">SECONDS_IN_AN_HOUR</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">SECONDS_IN_AN_HOUR</span>
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>The number of seconds in an hour, useful for making code more verbose</p>
|
||||
</article>
|
||||
|
||||
<article id="SECONDS_IN_A_DAY">
|
||||
<p>
|
||||
<span class="header">SECONDS_IN_A_DAY</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">SECONDS_IN_A_DAY</span>
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>The number of seconds in a day, useful for making code more verbose</p>
|
||||
</article>
|
||||
|
||||
<article id="SECONDS_IN_A_WEEK">
|
||||
<p>
|
||||
<span class="header">SECONDS_IN_A_WEEK</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">SECONDS_IN_A_WEEK</span>
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>The number of seconds in a week (7 days), useful for making code more verbose</p>
|
||||
</article>
|
||||
|
||||
<article id="SECONDS_IN_A_MONTH">
|
||||
<p>
|
||||
<span class="header">SECONDS_IN_A_MONTH</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">SECONDS_IN_A_MONTH</span>
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>The number of seconds in a month (30 days), useful for making code more verbose</p>
|
||||
</article>
|
||||
|
||||
<article id="SECONDS_IN_A_YEAR">
|
||||
<p>
|
||||
<span class="header">SECONDS_IN_A_YEAR</span>
|
||||
<code>
|
||||
<span class="util_class">util::</span><span class="func_name">SECONDS_IN_A_YEAR</span>
|
||||
</code>
|
||||
</p>
|
||||
|
||||
<p>The number of seconds in a year (365 days), useful for making code more verbose</p>
|
||||
</article>
|
||||
</section>
|
||||
|
||||
<h2 id="changelog">Change Log</h2>
|
||||
|
||||
<!--
|
||||
Find: `(.*?)`
|
||||
Replace: <code>\1<\/code>
|
||||
|
||||
Find: \[(.*?)\]\((.*?)\)
|
||||
Replace: <a href="\2">\1</a>
|
||||
-->
|
||||
|
||||
<p class="desc-text">
|
||||
<span class="header">1.1.0</span><br>
|
||||
|
||||
This release introduces a deprecation notice for <code>slugify()</code>, please update your code.<br><br>
|
||||
|
||||
- Added a cryptographically secure random string function <code>secure_random_string</code>. Thanks to @abhimanyusharma003 via <a href="https://github.com/brandonwamboldt/utilphp/pull/53">Pull Request #53</a><br>
|
||||
- Added <code>limit_characters</code> and <code>limit_words</code> functions. Thanks to @abhimanyusharma003 via <a href="https://github.com/brandonwamboldt/utilphp/pull/55">Pull Request #55</a><br>
|
||||
- Added <code>rmdir</code> method to recursively delete a directory. Thanks to @ARACOOOL via <a href="https://github.com/brandonwamboldt/utilphp/pull/56">Pull Request #56</a><br>
|
||||
- Added <code>set_executable</code>, <code>set_readable</code>, <code>set_writable</code>, <code>directory_size</code>, <code>directory_contents</code> and <code>get_user_dir</code> functions. Thanks to @sergserg via <a href="https://github.com/brandonwamboldt/utilphp/pull/70">Pull Request #70</a><br>
|
||||
- Changed parameter ordering for <code>slugify</code>, <code>$css_mode</code> is now the third argument. For backwards compatibility, the old order will still work but it will generate an <code>E_USER_DEPRECATED</code> warning. Thanks to @abhimanyusharma003 via <a href="https://github.com/brandonwamboldt/utilphp/pull/71">Pull Request #71</a><br>
|
||||
- Added <code>match_string</code> method. Thanks to @abhimanyusharma003 via <a href="https://github.com/brandonwamboldt/utilphp/pull/72">Pull Request #72</a><br>
|
||||
- Renamed internal methods (protected ones) for PSR-2 compliance<br>
|
||||
- General performance improvements, code quality improvements, and increased unit test coverage (special thanks to @hopeseekr for getting us near 100% coverage)<br>
|
||||
</p>
|
||||
|
||||
<p class="desc-text">
|
||||
<span class="header">1.0.7</span><br>
|
||||
- Added <code>fix_broken_serialization</code> to fix broken serialized strings (Thanks to @hopeseekr via <a href="https://github.com/brandonwamboldt/utilphp/pull/48">Pull Request #48</a>)<br>
|
||||
- Fixed <code>get_current_url</code> appending port 80 or 443 when not needed (Thanks to @scottchiefbaker via <a href="https://github.com/brandonwamboldt/utilphp/pull/49">Pull Request #49</a>)<br>
|
||||
- <code>var_dump</code> can now handle recursive data structures without crashing<br>
|
||||
- <code>var_dump</code> code was minified and cleaned up<br>
|
||||
- <code>array_flatten</code> was optimized (thanks to @hopeseekr via <a href="https://github.com/brandonwamboldt/utilphp/pull/47">Pull Request #47</a>)<br>
|
||||
- <code>remove_accents</code> was completely rewritten and is now ~4x faster<br>
|
||||
</p>
|
||||
|
||||
<p class="desc-text">
|
||||
<span class="header">1.0.6</span><br>
|
||||
- Added <code>start_with</code> function<br>
|
||||
- Added <code>ends_with</code> function<br>
|
||||
- Added <code>str_contains</code> function<br>
|
||||
- Added <code>str_icontains</code> function<br>
|
||||
- Added <code>get_file_ext</code> function<br>
|
||||
- Fixing permissions on the files & directories<br>
|
||||
- Fixing a bug with the include path of util.php<br>
|
||||
</p>
|
||||
|
||||
<p class="desc-text">
|
||||
<span class="header">1.0.5</span><br>
|
||||
- <a href="https://github.com/brandonwamboldt/utilphp/issues/29">Issue #29</a> Fixed error in <code>var_dump</code> if mbstring extension wasn't present<br>
|
||||
- Adding Composer support<br>
|
||||
- Updating license from GPL to MIT<br>
|
||||
- Adding Changelog to project<br>
|
||||
- Bumping minimum version to PHP 5.3.3<br>
|
||||
</p>
|
||||
|
||||
<p class="desc-text">
|
||||
<span class="header">1.0.4</span><br />
|
||||
- <a href="https://github.com/brandonwamboldt/utilphp/issues/22">Issue #22</a> Removed all superglobal *_get functions, you can use the modified <code>array_get</code> now<br />
|
||||
- <a href="https://github.com/brandonwamboldt/utilphp/issues/22">Issue #22</a> Modifed the behaviour of <code>array_get</code>, see documentation<br />
|
||||
- <a href="https://github.com/brandonwamboldt/utilphp/pull/21">Pull Request #21</a> Added multibyte support to html* functions<br />
|
||||
- <a href="https://github.com/brandonwamboldt/utilphp/issues/9">Issue #9</a> Removed the <code>str_to_utf8</code> function<br />
|
||||
- <a href="https://github.com/brandonwamboldt/utilphp/issues/3">Issue #3</a> Removed the absint function<br />
|
||||
- Removed declare() from util.php to avoid errors<br />
|
||||
- Updated PHPUnit tests to use PHPUnit 3.6
|
||||
</p>
|
||||
|
||||
<p class="desc-text">
|
||||
<span class="header">1.0.3</span><br />
|
||||
- <a href="https://github.com/brandonwamboldt/utilphp/issues/16">Issue #16</a> Improved performance of <code>slugify</code><br />
|
||||
- <a href="https://github.com/brandonwamboldt/utilphp/issues/14">Issue #14</a> Modified the regex for <code>seems_utf8</code> to be more accurate<br />
|
||||
- <a href="https://github.com/brandonwamboldt/utilphp/issues/13">Issue #13</a> Changed <code>validate_email</code> to be wrapper for <code>filter_var</code><br />
|
||||
- Added 'ok' to the list of yes words for <code>str_to_bool</code><br />
|
||||
- <code>str_to_bool</code> matches no followed by any number of 'o's
|
||||
</p>
|
||||
|
||||
<p class="desc-text">
|
||||
<span class="header">1.0.2</span><br />
|
||||
- <a href="https://github.com/brandonwamboldt/utilphp/issues/12">Issue #12</a> <code>get_current_url</code> now includes the port and user/password if required<br />
|
||||
- <a href="https://github.com/brandonwamboldt/utilphp/issues/11">Issue #11</a> <code>human_time_diff</code> now uses the DateTime functions<br />
|
||||
- <a href="https://github.com/brandonwamboldt/utilphp/issues/10">Issue #10</a> <code>is_https</code> no longer checks the port as well
|
||||
</p>
|
||||
|
||||
<p class="desc-text">
|
||||
<span class="header">1.0.1</span><br />
|
||||
- <a href="https://github.com/brandonwamboldt/utilphp/issues/7">Issue #7</a> Added the <code style="color:#DA72F3">$trust_proxy_headers</code> parameter to <code>get_client_ip</code><br />
|
||||
- <a href="https://github.com/brandonwamboldt/utilphp/issues/6">Issue #6</a> Removed <code>is_isset</code> as the function did not work as intended<br />
|
||||
- <a href="https://github.com/brandonwamboldt/utilphp/issues/6">Issue #6</a> Removed <code>is_empty</code> as it is redundant, use ! with function calls instead<br />
|
||||
- Fixed a bug with <code>get_gravatar</code>
|
||||
</p>
|
||||
|
||||
<p class="desc-text">
|
||||
<span class="header">1.0.0</span><br />
|
||||
Initial release of util.php.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="false"
|
||||
stopOnFailure="false"
|
||||
syntaxCheck="false"
|
||||
>
|
||||
<testsuites>
|
||||
<testsuite name="Tests suite">
|
||||
<directory>./tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory>./src</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
<logging>
|
||||
<log type="coverage-clover" target="build/logs/clover.xml"/>
|
||||
</logging>
|
||||
</phpunit>
|
||||
@@ -0,0 +1,39 @@
|
||||
# UtilityPHP Unit Tests #
|
||||
|
||||
### Introduction
|
||||
|
||||
This is the testing documentation for util.php. The unit tests require PHPUnit,
|
||||
and can be run in your web browser.
|
||||
|
||||
### Requirements
|
||||
|
||||
PHP Unit >= 3.5.6
|
||||
|
||||
#### Installation of PHPUnit
|
||||
|
||||
# Download the PHPUnit phar.
|
||||
wget https://phar.phpunit.de/phpunit.phar
|
||||
|
||||
# You can just run it locally. (Preferred for Windows)
|
||||
php phpunit.phar --version
|
||||
|
||||
# Or install it system-wide.
|
||||
mv phpunit.phar phpunit
|
||||
chmod +x phpunit
|
||||
sudo mv phpunit /usr/local/bin
|
||||
phpunit --version
|
||||
|
||||
## Test Suites:
|
||||
|
||||
util.php includes a phpunit config file, so just run `phpunit` from the working
|
||||
directory to run the test suite.
|
||||
|
||||
```
|
||||
phpunit
|
||||
```
|
||||
|
||||
If you want code coverage reports, use the following:
|
||||
|
||||
```
|
||||
phpunit --coverage-html ./report
|
||||
```
|
||||
@@ -0,0 +1,1183 @@
|
||||
<?php
|
||||
|
||||
date_default_timezone_set('UTC');
|
||||
|
||||
require_once dirname(__FILE__) . '/../util.php';
|
||||
|
||||
class VarDumpExperiment
|
||||
{
|
||||
public $public = 1;
|
||||
protected $protected = 2;
|
||||
private $private = 3;
|
||||
public $data;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->data = (object)array('a', 'b', 'c');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* PHPUnit test case for the util.php library
|
||||
*
|
||||
* @author Brandon Wamboldt <brandon.wamboldt@gmail.com>
|
||||
* @since 1.0.000
|
||||
*/
|
||||
class UtilityPHPTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Allows for the testing of private and protected methods.
|
||||
*
|
||||
* @param $name
|
||||
* @return \ReflectionMethod
|
||||
*/
|
||||
protected static function getMethod($name)
|
||||
{
|
||||
$class = new \ReflectionClass('util');
|
||||
$method = $class->getMethod($name);
|
||||
$method->setAccessible(true);
|
||||
return $method;
|
||||
}
|
||||
|
||||
public function test_array_get()
|
||||
{
|
||||
$_GET = array();
|
||||
$_GET['abc'] = 'def';
|
||||
$_GET['nested'] = array( 'key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3' );
|
||||
|
||||
// Looks for $array['abc']
|
||||
$this->assertEquals( 'def', util::array_get( $_GET['abc'] ) );
|
||||
|
||||
// Looks for $array['nested']['key2']
|
||||
$this->assertEquals( 'val2', util::array_get( $_GET['nested']['key2'] ) );
|
||||
|
||||
// Looks for $array['doesnotexist']
|
||||
$this->assertEquals( 'defaultval', util::array_get( $_GET['doesnotexist'], 'defaultval' ) );
|
||||
}
|
||||
|
||||
public function test_slugify()
|
||||
{
|
||||
$this->assertEquals( 'a-simple-title', util::slugify( 'A simple title' ) );
|
||||
$this->assertEquals( 'this-post-it-has-a-dash', util::slugify( 'This post -- it has a dash' ) );
|
||||
$this->assertEquals( '123-1251251', util::slugify( '123----1251251' ) );
|
||||
$this->assertEquals( 'one23-1251251', util::slugify( '123----1251251', '-', true ) );
|
||||
|
||||
$this->assertEquals( 'a-simple-title', util::slugify( 'A simple title', '-' ) );
|
||||
$this->assertEquals( 'this-post-it-has-a-dash', util::slugify( 'This post -- it has a dash', '-' ) );
|
||||
$this->assertEquals( '123-1251251', util::slugify( '123----1251251', '-' ) );
|
||||
$this->assertEquals( 'one23-1251251', util::slugify( '123----1251251', '-', true ) );
|
||||
|
||||
$this->assertEquals( 'a_simple_title', util::slugify( 'A simple title', '_' ) );
|
||||
$this->assertEquals( 'this_post_it_has_a_dash', util::slugify( 'This post -- it has a dash', '_' ) );
|
||||
$this->assertEquals( '123_1251251', util::slugify( '123----1251251', '_' ) );
|
||||
$this->assertEquals( 'one23_1251251', util::slugify( '123----1251251', '_', true ) );
|
||||
|
||||
// Blank seperator test
|
||||
$this->assertEquals( 'asimpletitle', util::slugify( 'A simple title', '' ) );
|
||||
$this->assertEquals( 'thispostithasadash', util::slugify( 'This post -- it has a dash', '' ) );
|
||||
$this->assertEquals( '1231251251', util::slugify( '123----1251251', '' ) );
|
||||
$this->assertEquals( 'one231251251', util::slugify( '123----1251251', '', true ) );
|
||||
|
||||
// Test old parameter ordering for backwards compatability
|
||||
error_reporting(E_ALL ^ E_USER_DEPRECATED);
|
||||
$this->assertEquals( 'one23-1251251', util::slugify( '123----1251251', true ) );
|
||||
$this->assertEquals( '123-1251251', util::slugify( '123----1251251', false ) );
|
||||
}
|
||||
|
||||
public function test_seems_utf8()
|
||||
{
|
||||
// Test a valid UTF-8 sequence: "ÜTF-8 Fµñ".
|
||||
$validUTF8 = "\xC3\x9CTF-8 F\xC2\xB5\xC3\xB1";
|
||||
$this->assertTrue( util::seems_utf8( $validUTF8 ) );
|
||||
|
||||
$this->assertTrue( util::seems_utf8( "\xEF\xBF\xBD this has \xEF\xBF\xBD\xEF\xBF\xBD some invalid utf8 \xEF\xBF\xBD" ) );
|
||||
|
||||
// Test invalid UTF-8 sequences
|
||||
$invalidUTF8 = "\xc3 this has \xe6\x9d some invalid utf8 \xe6";
|
||||
$this->assertFalse( util::seems_utf8( $invalidUTF8 ) );
|
||||
|
||||
// And test some plain ASCII
|
||||
$this->assertTrue( util::seems_utf8( 'The quick brown fox jumps over the lazy dog' ) );
|
||||
|
||||
// Test an invalid non-UTF-8 string.
|
||||
if (function_exists('mb_convert_encoding')) {
|
||||
mb_internal_encoding('UTF-8');
|
||||
// Converts the 'ç' UTF-8 character to UCS-2LE
|
||||
$utf8Char = pack('n', 50087);
|
||||
$ucsChar = mb_convert_encoding($utf8Char, 'UCS-2LE', 'UTF-8');
|
||||
|
||||
// Ensure that PHP's internal encoding system isn't malconfigured.
|
||||
$this->assertEquals( $utf8Char, 'ç', 'This PHP system\'s internal character set is not properly set as UTF-8.' );
|
||||
$this->assertEquals( $utf8Char, pack('n', 50087), 'Something is wrong with your ICU unicode library.' );
|
||||
|
||||
// Test for not UTF-8.
|
||||
$this->assertFalse( util::seems_utf8( $ucsChar) );
|
||||
|
||||
// Test the worker method.
|
||||
$method = self::getMethod('seemsUtf8Regex');
|
||||
$this->assertFalse($method->invoke(null, $invalidUTF8), 'util::seemsUtf8Regex did not properly detect invalid UTF-8.');
|
||||
$this->assertTrue($method->invoke(null, $validUTF8), 'util::seemsUtf8Regex did not properly detect valid UTF-8.');
|
||||
}
|
||||
}
|
||||
|
||||
public function test_size_format()
|
||||
{
|
||||
$size = util::size_format( 512, 0 );
|
||||
$this->assertEquals( '512 B', $size );
|
||||
|
||||
$size = util::size_format( 2048, 1 );
|
||||
$this->assertEquals( '2.0 KiB', $size );
|
||||
|
||||
$size = util::size_format( 25151251, 2 );
|
||||
$this->assertEquals( '23.99 MiB', $size );
|
||||
|
||||
$size = util::size_format( 19971597926, 2 );
|
||||
$this->assertEquals( '18.60 GiB', $size );
|
||||
|
||||
$size = util::size_format( 2748779069440, 1 );
|
||||
$this->assertEquals( '2.5 TiB', $size );
|
||||
|
||||
$size = util::size_format( 2.81475e15, 1 );
|
||||
$this->assertEquals( '2.5 PiB', $size );
|
||||
|
||||
$size = util::size_format( 2.81475e19, 1 );
|
||||
$this->assertEquals( '25000.0 PiB', $size );
|
||||
}
|
||||
|
||||
public function test_maybe_serialize()
|
||||
{
|
||||
$obj = new stdClass();
|
||||
$obj->prop1 = 'Hello';
|
||||
$obj->prop2 = 'World';
|
||||
|
||||
$this->assertEquals( 'This is a string', util::maybe_serialize( 'This is a string' ) );
|
||||
$this->assertEquals( 5.81, util::maybe_serialize( 5.81 ) );
|
||||
$this->assertEquals( 'a:0:{}', util::maybe_serialize( array() ) );
|
||||
$this->assertEquals( 'O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}', util::maybe_serialize( $obj ) );
|
||||
$this->assertEquals( 'a:4:{i:0;s:4:"test";i:1;s:4:"blah";s:5:"hello";s:5:"world";s:5:"array";O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}}', util::maybe_serialize( array( 'test', 'blah', 'hello' => 'world', 'array' => $obj ) ) );
|
||||
}
|
||||
|
||||
public function test_maybe_unserialize()
|
||||
{
|
||||
$obj = new stdClass();
|
||||
$obj->prop1 = 'Hello';
|
||||
$obj->prop2 = 'World';
|
||||
|
||||
$this->assertNull(util::maybe_unserialize(serialize(null)));
|
||||
$this->assertFalse(util::maybe_unserialize(serialize(false)));
|
||||
|
||||
$this->assertEquals( 'This is a string', util::maybe_unserialize( 'This is a string' ) );
|
||||
$this->assertEquals( 5.81, util::maybe_unserialize( 5.81 ) );
|
||||
$this->assertEquals( array(), util::maybe_unserialize( 'a:0:{}' ) );
|
||||
$this->assertEquals( $obj, util::maybe_unserialize( 'O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}' ) );
|
||||
$this->assertEquals( array( 'test', 'blah', 'hello' => 'world', 'array' => $obj ), util::maybe_unserialize( 'a:4:{i:0;s:4:"test";i:1;s:4:"blah";s:5:"hello";s:5:"world";s:5:"array";O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}}' ) );
|
||||
|
||||
// Test a broken serialization.
|
||||
$expectedData = array(
|
||||
'Normal',
|
||||
'High-value Char: '.chr(231).'a-va?', // High-value Char: ça-va? [in ISO-8859-1]
|
||||
);
|
||||
|
||||
$brokenSerialization = 'a:2:{i:0;s:6:"Normal";i:1;s:23:"High-value Char: ▒a-va?";}';
|
||||
|
||||
$unserializedData = util::maybe_unserialize($brokenSerialization);
|
||||
$this->assertEquals($expectedData[0], $unserializedData[0], 'Did not properly fix the broken serialized data.');
|
||||
$this->assertEquals(substr($expectedData[1], 0, 10), substr($unserializedData[1], 0, 10), 'Did not properly fix the broken serialized data.');
|
||||
|
||||
// Test unfixable serialization.
|
||||
$unfixableSerialization = 'a:2:{i:0;s:6:"Normal";}';
|
||||
$this->assertEquals($unfixableSerialization, util::maybe_unserialize($unfixableSerialization), 'Somehow the [previously?] impossible happened and utilphp thinks it has unserialized an unfixable serialization.');
|
||||
}
|
||||
|
||||
public function test_is_serialized()
|
||||
{
|
||||
$this->assertFalse( util::is_serialized(1) );
|
||||
$this->assertFalse( util::is_serialized(null) );
|
||||
$this->assertFalse( util::is_serialized( 's:4:"test;' ) );
|
||||
$this->assertFalse( util::is_serialized( 'a:0:{}!' ) );
|
||||
$this->assertFalse( util::is_serialized( 'a:0' ) );
|
||||
$this->assertFalse( util::is_serialized( 'This is a string' ) );
|
||||
$this->assertFalse( util::is_serialized( 'a string' ) );
|
||||
$this->assertFalse( util::is_serialized( 'z:0;' ) );
|
||||
$this->assertTrue( util::is_serialized( 'N;' ) );
|
||||
$this->assertTrue( util::is_serialized( 'b:1;' ) );
|
||||
$this->assertTrue( util::is_serialized( 'a:0:{}' ) );
|
||||
$this->assertTrue( util::is_serialized( 'O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}' ) );
|
||||
}
|
||||
|
||||
public function test_fix_broken_serialization()
|
||||
{
|
||||
$expectedData = array(
|
||||
'Normal',
|
||||
'High-value Char: '.chr(231).'a-va?', // High-value Char: ça-va? [in ISO-8859-1]
|
||||
);
|
||||
|
||||
$brokenSerialization = 'a:2:{i:0;s:6:"Normal";i:1;s:23:"High-value Char: ▒a-va?";}';
|
||||
|
||||
// Temporarily override error handling to ensure that this is, in fact, [still] a broken serialization.
|
||||
{
|
||||
$expectedError = array(
|
||||
'errno' => 8,
|
||||
'errstr' => 'unserialize(): Error at offset 55 of 60 bytes'
|
||||
);
|
||||
|
||||
$reportedError = array();
|
||||
set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) use (&$reportedError) {
|
||||
$reportedError = compact('errno', 'errstr');
|
||||
});
|
||||
|
||||
unserialize($brokenSerialization);
|
||||
|
||||
$this->assertEquals($expectedError['errno'], $reportedError['errno']);
|
||||
// Because HHVM's unserialize() error message does not contain enough info to properly test.
|
||||
if (!defined('HHVM_VERSION')) {
|
||||
$this->assertEquals($expectedError['errstr'], $reportedError['errstr']);
|
||||
}
|
||||
restore_error_handler();
|
||||
}
|
||||
|
||||
$fixedSerialization = util::fix_broken_serialization($brokenSerialization);
|
||||
$unserializedData = unserialize($fixedSerialization);
|
||||
$this->assertEquals($expectedData[0], $unserializedData[0], 'Did not properly fix the broken serialized data.');
|
||||
|
||||
$this->assertEquals(substr($expectedData[1], 0, 10), substr($unserializedData[1], 0, 10), 'Did not properly fix the broken serialized data.');
|
||||
}
|
||||
|
||||
public function test_is_https()
|
||||
{
|
||||
$_SERVER['HTTPS'] = null;
|
||||
|
||||
$this->assertFalse( util::is_https() );
|
||||
|
||||
$_SERVER['HTTPS'] = 'on';
|
||||
|
||||
$this->assertTrue( util::is_https() );
|
||||
}
|
||||
|
||||
public function test_add_query_arg()
|
||||
{
|
||||
// Regular tests
|
||||
$this->assertEquals( '/app/admin/users?user=5', util::add_query_arg( 'user', 5, '/app/admin/users' ) );
|
||||
$this->assertEquals( '/app/admin/users?user=5', util::add_query_arg( array( 'user' => 5 ), '/app/admin/users' ) );
|
||||
$this->assertEquals( '/app/admin/users?user=5', util::add_query_arg( array( 'user' => 5 ), null, '/app/admin/users' ) );
|
||||
$this->assertEquals( '/app/admin/users?action=edit&user=5', util::add_query_arg( 'user', 5, '/app/admin/users?action=edit' ) );
|
||||
$this->assertEquals( '/app/admin/users?action=edit&user=5', util::add_query_arg( array( 'user' => 5 ), '/app/admin/users?action=edit' ) );
|
||||
$this->assertEquals( '/app/admin/users?action=edit&tab=personal&user=5', util::add_query_arg( 'user', 5, '/app/admin/users?action=edit&tab=personal' ) );
|
||||
$this->assertEquals( '/app/admin/users?action=edit&tab=personal&user=5', util::add_query_arg( array( 'user' => 5 ), '/app/admin/users?action=edit&tab=personal' ) );
|
||||
|
||||
// Ensure strips false.
|
||||
$this->assertEquals('/index.php', util::add_query_arg('debug', false, '/index.php'));
|
||||
|
||||
// With a URL fragment
|
||||
$this->assertEquals( '/app/admin/users?user=5#test', util::add_query_arg( 'user', 5, '/app/admin/users#test' ) );
|
||||
|
||||
// Full URL
|
||||
$this->assertEquals( 'http://example.com/?a=b', util::add_query_arg( 'a', 'b', 'http://example.com' ) );
|
||||
|
||||
// Only the query string
|
||||
$this->assertEquals( '?a=b&c=d', util::add_query_arg( 'c', 'd', '?a=b' ) );
|
||||
$this->assertEquals( 'a=b&c=d', util::add_query_arg( 'c', 'd', 'a=b' ) );
|
||||
|
||||
// Url encoding test
|
||||
$this->assertEquals( '/app/admin/users?param=containsa%26sym', util::add_query_arg( 'param', 'containsa&sym', '/app/admin/users' ) );
|
||||
|
||||
// If not provided, grab the URI from the server.
|
||||
$_SERVER['REQUEST_URI'] = '/app/admin/users';
|
||||
$this->assertEquals( '/app/admin/users?user=6', util::add_query_arg( array( 'user' => 6 ) ) );
|
||||
$this->assertEquals( '/app/admin/users?user=7', util::add_query_arg( 'user', 7 ) );
|
||||
}
|
||||
|
||||
public function test_remove_query_arg()
|
||||
{
|
||||
$this->assertEquals( '/app/admin/users', util::remove_query_arg( 'user', '/app/admin/users?user=5' ) );
|
||||
$this->assertEquals( '/app/admin/users?action=edit', util::remove_query_arg( 'user', '/app/admin/users?action=edit&user=5' ) );
|
||||
$this->assertEquals( '/app/admin/users?user=5', util::remove_query_arg( array( 'tab', 'action' ), '/app/admin/users?action=edit&tab=personal&user=5' ) );
|
||||
}
|
||||
|
||||
public function test_http_build_url()
|
||||
{
|
||||
$url = 'http://user:pass@example.com:8080/path/?query#fragment';
|
||||
|
||||
$expected = 'http://example.com/';
|
||||
$actual = util::http_build_url($url, array(), util::HTTP_URL_STRIP_ALL);
|
||||
$this->assertEquals($expected, $actual);
|
||||
|
||||
$expected = 'http://example.com:8080/path/?query#fragment';
|
||||
$actual = util::http_build_url($url, array(), util::HTTP_URL_STRIP_AUTH);
|
||||
$this->assertEquals($expected, $actual);
|
||||
|
||||
$this->assertEquals('https://dev.example.com/', util::http_build_url('http://example.com/', array('scheme' => 'https', 'host' => 'dev.example.com')));
|
||||
$this->assertEquals('http://example.com/#hi', util::http_build_url('http://example.com/', array('fragment' => 'hi'), util::HTTP_URL_REPLACE));
|
||||
$this->assertEquals('http://example.com/page', util::http_build_url('http://example.com/', array('path' => 'page'), util::HTTP_URL_JOIN_PATH));
|
||||
$this->assertEquals('http://example.com/page', util::http_build_url('http://example.com', array('path' => 'page'), util::HTTP_URL_JOIN_PATH));
|
||||
$this->assertEquals('http://example.com/?hi=Bro', util::http_build_url('http://example.com/', array('query' => 'hi=Bro'), util::HTTP_URL_JOIN_QUERY));
|
||||
$this->assertEquals('http://example.com/?show=1&hi=Bro', util::http_build_url('http://example.com/?show=1', array('query' => 'hi=Bro'), util::HTTP_URL_JOIN_QUERY));
|
||||
|
||||
$this->assertEquals('http://admin@example.com/', util::http_build_url('http://example.com/', array('user' => 'admin')));
|
||||
$this->assertEquals('http://admin:1@example.com/', util::http_build_url('http://example.com/', array('user' => 'admin', 'pass' => '1')));
|
||||
}
|
||||
|
||||
public function test_str_to_bool()
|
||||
{
|
||||
$this->assertTrue( util::str_to_bool( 'true' ) );
|
||||
$this->assertTrue( util::str_to_bool( 'yes' ) );
|
||||
$this->assertTrue( util::str_to_bool( 'y' ) );
|
||||
$this->assertTrue( util::str_to_bool( 'oui' ) );
|
||||
$this->assertTrue( util::str_to_bool( 'vrai' ) );
|
||||
|
||||
$this->assertFalse( util::str_to_bool( 'false' ) );
|
||||
$this->assertFalse( util::str_to_bool( 'no' ) );
|
||||
$this->assertFalse( util::str_to_bool( 'n' ) );
|
||||
$this->assertFalse( util::str_to_bool( 'non' ) );
|
||||
$this->assertFalse( util::str_to_bool( 'faux' ) );
|
||||
|
||||
$this->assertFalse( util::str_to_bool( 'test' , false) );
|
||||
|
||||
}
|
||||
|
||||
public function test_array_pluck()
|
||||
{
|
||||
$array = array(
|
||||
array(
|
||||
'name' => 'Bob',
|
||||
'age' => 37
|
||||
),
|
||||
array(
|
||||
'name' => 'Fred',
|
||||
'age' => 37
|
||||
),
|
||||
array(
|
||||
'name' => 'Jane',
|
||||
'age' => 29
|
||||
),
|
||||
array(
|
||||
'name' => 'Brandon',
|
||||
'age' => 20
|
||||
),
|
||||
array(
|
||||
'age' => 41
|
||||
)
|
||||
);
|
||||
|
||||
$obj_array = array(
|
||||
'bob' => (object) array(
|
||||
'name' => 'Bob',
|
||||
'age' => 37
|
||||
),
|
||||
'fred' => (object) array(
|
||||
'name' => 'Fred',
|
||||
'age' => 37
|
||||
),
|
||||
'jane' => (object) array(
|
||||
'name' => 'Jane',
|
||||
'age' => 29
|
||||
),
|
||||
'brandon' => (object) array(
|
||||
'name' => 'Brandon',
|
||||
'age' => 20
|
||||
),
|
||||
'invalid' => (object) array(
|
||||
'age' => 41
|
||||
)
|
||||
);
|
||||
|
||||
$obj_array_expect = array(
|
||||
'bob' => 'Bob',
|
||||
'fred' => 'Fred',
|
||||
'jane' => 'Jane',
|
||||
'brandon' => 'Brandon'
|
||||
);
|
||||
|
||||
$this->assertEquals( array( 'Bob', 'Fred', 'Jane', 'Brandon' ), util::array_pluck( $array, 'name' ) );
|
||||
$this->assertEquals( array( 'Bob', 'Fred', 'Jane', 'Brandon', array( 'age' => 41 ) ), util::array_pluck( $array, 'name', TRUE, FALSE ) );
|
||||
$this->assertEquals( $obj_array_expect, util::array_pluck( $obj_array, 'name' ) );
|
||||
$this->assertEquals( array( 'Bob', 'Fred', 'Jane', 'Brandon' ), util::array_pluck( $obj_array, 'name', FALSE ) );
|
||||
|
||||
$expected = array('Bob', 'Fred', 'Jane', 'Brandon', 'invalid' => (object)array('age' => 41));
|
||||
$this->assertEquals($expected, util::array_pluck($obj_array, 'name', FALSE, FALSE));
|
||||
$expected = array('Bob', 'Fred', 'Jane', 'Brandon', array('age' => 41));
|
||||
$this->assertEquals($expected, util::array_pluck($array, 'name', false, false));
|
||||
}
|
||||
|
||||
public function test_htmlentities()
|
||||
{
|
||||
$this->assertEquals( 'One & Two', util::htmlentities( 'One & Two' ) );
|
||||
$this->assertEquals( 'One & Two', util::htmlentities( 'One & Two', TRUE ) );
|
||||
}
|
||||
|
||||
public function test_htmlspecialchars()
|
||||
{
|
||||
$this->assertEquals( 'One & Two', util::htmlspecialchars( 'One & Two' ) );
|
||||
$this->assertEquals( 'One & Two', util::htmlspecialchars( 'One & Two', TRUE ) );
|
||||
}
|
||||
|
||||
public function test_remove_accents()
|
||||
{
|
||||
$this->assertEquals( 'A', util::remove_accents( "\xC3\x81" ) );
|
||||
$this->assertEquals( 'e', util::remove_accents( "\xC4\x97" ) );
|
||||
$this->assertEquals( 'U', util::remove_accents( "\xC3\x9C" ) );
|
||||
$this->assertEquals( 'Ae', util::remove_accents( "Ä", 'de' ) );
|
||||
$this->assertEquals( 'OEoeAEDHTHssaedhth', util::remove_accents(chr(140) . chr(156) . chr(198) . chr(208) . chr(222) . chr(223) . chr(230) . chr(240) . chr(254)));
|
||||
}
|
||||
|
||||
public function test_zero_pad()
|
||||
{
|
||||
$this->assertEquals( '00000341', util::zero_pad( 341, 8 ) );
|
||||
$this->assertEquals( '341', util::zero_pad( 341, 1 ) );
|
||||
}
|
||||
|
||||
public function test_human_time_diff()
|
||||
{
|
||||
$this->assertEquals( '1 second ago', util::human_time_diff( time() - 1 ) );
|
||||
$this->assertEquals( '30 seconds ago', util::human_time_diff( time() - 30 ) );
|
||||
$this->assertEquals( '1 minute ago', util::human_time_diff( time() - ( util::SECONDS_IN_A_MINUTE * 1.4 ) ) );
|
||||
$this->assertEquals( '5 minutes ago', util::human_time_diff( time() - ( util::SECONDS_IN_A_MINUTE * 5 ) ) );
|
||||
$this->assertEquals( '1 hour ago', util::human_time_diff( time() - ( util::SECONDS_IN_AN_HOUR ) ) );
|
||||
$this->assertEquals( '2 hours ago', util::human_time_diff( time() - ( util::SECONDS_IN_AN_HOUR * 2 ) ) );
|
||||
$this->assertEquals( '1 day ago', util::human_time_diff( time() - ( util::SECONDS_IN_AN_HOUR * 24 ) ) );
|
||||
$this->assertEquals( '5 days ago', util::human_time_diff( time() - ( util::SECONDS_IN_AN_HOUR * 24 * 5 ) ) );
|
||||
$this->assertEquals( '1 week ago', util::human_time_diff( time() - ( util::SECONDS_IN_AN_HOUR * 24 * 7 ) ) );
|
||||
$this->assertEquals( '2 weeks ago', util::human_time_diff( time() - ( util::SECONDS_IN_AN_HOUR * 24 * 14 ) ) );
|
||||
$this->assertEquals( '1 month ago', util::human_time_diff( time() - ( util::SECONDS_IN_A_WEEK * 5 ) ) );
|
||||
$this->assertEquals( '2 months ago', util::human_time_diff( time() - ( util::SECONDS_IN_A_WEEK * 10 ) ) );
|
||||
$this->assertEquals( '1 year ago', util::human_time_diff( time() - ( util::SECONDS_IN_A_MONTH * 15 ) ) );
|
||||
$this->assertEquals( '2 years ago', util::human_time_diff( time() - ( util::SECONDS_IN_A_MONTH * 36 ) ) );
|
||||
$this->assertEquals( '11 years ago', util::human_time_diff( time() - ( util::SECONDS_IN_A_MONTH * 140 ) ) );
|
||||
|
||||
$this->assertEquals( 'fifteen minutes ago', util::human_time_diff( time() - ( util::SECONDS_IN_A_MINUTE * 15 ), '', TRUE ) );
|
||||
}
|
||||
|
||||
public function test_number_to_word()
|
||||
{
|
||||
try {
|
||||
util::number_to_word('junk data');
|
||||
$this->fail('Accepted junk data');
|
||||
} catch(\LogicException $e) {
|
||||
$this->assertEquals('Not a number', $e->getMessage());
|
||||
}
|
||||
|
||||
// Partially numeric.
|
||||
$this->assertEquals('', util::number_to_word('1a'));
|
||||
|
||||
// Decimals
|
||||
$this->assertEquals( 'five point zero five', util::number_to_word('5.05') );
|
||||
$this->assertEquals( 'zero point eight', util::number_to_word( 0.8 ) );
|
||||
|
||||
// Integers
|
||||
$this->assertEquals( 'positive one', util::number_to_word( '+1' ) );
|
||||
$this->assertEquals( 'negative twelve', util::number_to_word( -12 ) );
|
||||
$this->assertEquals( 'one', util::number_to_word( 1 ) );
|
||||
$this->assertEquals( 'five', util::number_to_word( 5 ) );
|
||||
$this->assertEquals( 'fifteen', util::number_to_word( 15 ) );
|
||||
$this->assertEquals( 'twenty-one', util::number_to_word( 21 ) );
|
||||
$this->assertEquals( 'thirty-two', util::number_to_word( 32 ) );
|
||||
$this->assertEquals( 'forty-three', util::number_to_word( 43 ) );
|
||||
$this->assertEquals( 'fifty-four', util::number_to_word( 54 ) );
|
||||
$this->assertEquals( 'sixty-six', util::number_to_word( 66 ) );
|
||||
$this->assertEquals( 'seventy-seven', util::number_to_word( 77 ) );
|
||||
$this->assertEquals( 'eighty-eight', util::number_to_word( 88 ) );
|
||||
$this->assertEquals( 'ninety-nine', util::number_to_word( 99 ) );
|
||||
$this->assertEquals( 'one hundred and thirty-six', util::number_to_word( 136 ) );
|
||||
$this->assertEquals( 'ten', util::number_to_word( 10 ) );
|
||||
$this->assertEquals( 'twenty', util::number_to_word( 20 ) );
|
||||
$this->assertEquals( 'thirty', util::number_to_word( 30 ) );
|
||||
$this->assertEquals( 'forty', util::number_to_word( 40 ) );
|
||||
$this->assertEquals( 'fifty', util::number_to_word( 50 ) );
|
||||
$this->assertEquals( 'sixty', util::number_to_word( 60 ) );
|
||||
$this->assertEquals( 'seventy', util::number_to_word( 70 ) );
|
||||
$this->assertEquals( 'eighty', util::number_to_word( 80 ) );
|
||||
$this->assertEquals( 'ninety', util::number_to_word( 90 ) );
|
||||
$this->assertEquals( 'eleven', util::number_to_word( 11 ) );
|
||||
$this->assertEquals( 'thirteen', util::number_to_word( 13 ) );
|
||||
$this->assertEquals( 'fourteen', util::number_to_word( 14 ) );
|
||||
$this->assertEquals( 'fifteen', util::number_to_word( 15 ) );
|
||||
$this->assertEquals( 'sixteen', util::number_to_word( 16 ) );
|
||||
$this->assertEquals( 'seventeen', util::number_to_word( 17 ) );
|
||||
$this->assertEquals( 'eighteen', util::number_to_word( 18 ) );
|
||||
$this->assertEquals( 'nineteen', util::number_to_word( 19 ) );
|
||||
$this->assertEquals( 'one thousand', util::number_to_word( 1000 ) );
|
||||
$this->assertEquals( 'one million', util::number_to_word( 1000000 ) );
|
||||
$this->assertEquals( 'one billion', util::number_to_word( 1000000000 ) );
|
||||
$this->assertEquals( 'one trillion', util::number_to_word( 1000000000000 ) );
|
||||
$this->assertEquals( 'one quadrillion', util::number_to_word( '1000000000000000' ) );
|
||||
$this->assertEquals( 'one quintrillion', util::number_to_word( '1000000000000000000' ) );
|
||||
$this->assertEquals( 'one sextillion', util::number_to_word( '1000000000000000000000' ) );
|
||||
$this->assertEquals( 'one septillion', util::number_to_word( '1000000000000000000000000' ) );
|
||||
$this->assertEquals( 'one octillion', util::number_to_word( '1000000000000000000000000000' ) );
|
||||
$this->assertEquals( 'one nonillion', util::number_to_word( '1000000000000000000000000000000' ) );
|
||||
$this->assertEquals( 'one decillion', util::number_to_word( '1000000000000000000000000000000000' ) );
|
||||
$this->assertEquals( 'one', util::number_to_word( '1000000000000000000000000000000000000000000' ) );
|
||||
|
||||
}
|
||||
|
||||
public function test_array_search_deep()
|
||||
{
|
||||
$users = array(
|
||||
1 => (object) array( 'username' => 'brandon', 'age' => 20 ),
|
||||
2 => (object) array( 'username' => 'matt', 'age' => 27 ),
|
||||
3 => (object) array( 'username' => 'jane', 'age' => 53 ),
|
||||
4 => (object) array( 'username' => 'john', 'age' => 41 ),
|
||||
5 => (object) array( 'username' => 'steve', 'age' => 11 ),
|
||||
6 => (object) array( 'username' => 'fred', 'age' => 42 ),
|
||||
7 => (object) array( 'username' => 'rasmus', 'age' => 21 ),
|
||||
8 => (object) array( 'username' => 'don', 'age' => 15 ),
|
||||
9 => array( 'username' => 'darcy', 'age' => 33 ),
|
||||
);
|
||||
|
||||
$test = array(
|
||||
1 => 'brandon',
|
||||
2 => 'devon',
|
||||
3 => array( 'troy' ),
|
||||
4 => 'annie'
|
||||
);
|
||||
|
||||
$this->assertFalse( util::array_search_deep( $test, 'bob' ) );
|
||||
$this->assertEquals( 3, util::array_search_deep( $test, 'troy' ) );
|
||||
$this->assertEquals( 4, util::array_search_deep( $test, 'annie' ) );
|
||||
$this->assertEquals( 2, util::array_search_deep( $test, 'devon', 'devon' ) );
|
||||
$this->assertEquals( 7, util::array_search_deep( $users, 'rasmus', 'username' ) );
|
||||
$this->assertEquals( 9, util::array_search_deep( $users, 'darcy', 'username' ) );
|
||||
$this->assertEquals( 1, util::array_search_deep( $users, 'brandon' ) );
|
||||
}
|
||||
|
||||
public function test_array_map_deep()
|
||||
{
|
||||
$input = array(
|
||||
'<',
|
||||
'abc',
|
||||
'>',
|
||||
'def',
|
||||
array( '&', 'test', '123' ),
|
||||
(object) array( 'hey', '<>' )
|
||||
);
|
||||
|
||||
$expect = array(
|
||||
'<',
|
||||
'abc',
|
||||
'>',
|
||||
'def',
|
||||
array( '&', 'test', '123' ),
|
||||
(object) array( 'hey', '<>' )
|
||||
);
|
||||
|
||||
$this->assertEquals( $expect, util::array_map_deep( $input, 'htmlentities' ) );
|
||||
}
|
||||
|
||||
public function test_random_string()
|
||||
{
|
||||
// Make sure the generated string contains only human friendly characters and is 30 characters long
|
||||
$str = util::random_string( 30 );
|
||||
$this->assertTrue( (bool) preg_match( '/^([ABCDEFGHJKLMNPQRSTUVWXYZabcdefhjkmnprstuvwxyz23456789]{30})$/', $str ) );
|
||||
|
||||
// Make sure the generated string is 30 characters long
|
||||
$str = util::random_string( 30, false, true );
|
||||
$this->assertTrue( strlen( $str ) === 30, 'random_string produced an invalid length string' );
|
||||
|
||||
// Make sure the generated string is 120 characters long
|
||||
$str = util::random_string( 120 );
|
||||
$this->assertTrue(strlen( $str ) === 120, 'random_string produced an invalid length string');
|
||||
|
||||
// Make sure the string doesn't contain duplicate letters
|
||||
$str = util::random_string( 53, true, false, true );
|
||||
$this->assertTrue(strlen($str) === 53, 'random_string produced an invalid length string');
|
||||
$this->assertTrue(count(array_unique(str_split($str))) === strlen($str), 'random_string produced a string with duplicate characters');
|
||||
|
||||
// Longer length than characters available
|
||||
try {
|
||||
$str = util::random_string( 55, true, false, true );
|
||||
$this->assertTrue( false );
|
||||
} catch (Exception $e) {
|
||||
$this->assertTrue( true );
|
||||
}
|
||||
|
||||
// Test secure variant
|
||||
$str = util::secure_random_string(16);
|
||||
$this->assertTrue(strlen($str) === 16);
|
||||
|
||||
// Longer length than characters available
|
||||
try {
|
||||
$str = util::secure_random_string(0);
|
||||
$this->assertTrue(false);
|
||||
} catch (Exception $e) {
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_match_string()
|
||||
{
|
||||
$this->assertTrue( util::match_string('a', 'a') );
|
||||
$this->assertFalse( util::match_string('a', ' a') );
|
||||
$this->assertFalse( util::match_string('/', '/something') );
|
||||
$this->assertTrue( util::match_string('test/*', 'test/first/second') );
|
||||
$this->assertTrue( util::match_string('*/test', 'first/second/test') );
|
||||
$this->assertFalse( util::match_string('first/', 'first/second/test') );
|
||||
$this->assertFalse( util::match_string('test', 'TEST') );
|
||||
$this->assertTrue( util::match_string('test', 'TEST', false) );
|
||||
}
|
||||
|
||||
public function test_validate_email()
|
||||
{
|
||||
$this->assertTrue( util::validate_email( 'john.smith@gmail.com' ) );
|
||||
$this->assertTrue( util::validate_email( 'john.smith+label@gmail.com' ) );
|
||||
$this->assertTrue( util::validate_email( 'john.smith@gmail.co.uk' ) );
|
||||
}
|
||||
|
||||
public function test_safe_truncate()
|
||||
{
|
||||
$this->assertEquals( 'The quick brown fox...', util::safe_truncate( 'The quick brown fox jumps over the lazy dog', 24 ) );
|
||||
$this->assertEquals( 'The quick brown fox jumps over the lazy dog', util::safe_truncate( 'The quick brown fox jumps over the lazy dog', 55 ) );
|
||||
$this->assertEquals( 'Th...', util::safe_truncate( 'The quick brown fox jumps over the lazy dog', 2 ) );
|
||||
$this->assertEquals( 'The...', util::safe_truncate( 'The quick brown fox jumps over the lazy dog', 3 ) );
|
||||
$this->assertEquals( 'The...', util::safe_truncate( 'The quick brown fox jumps over the lazy dog', 7 ) );
|
||||
}
|
||||
|
||||
public function test_limit_characters()
|
||||
{
|
||||
$this->assertEquals( 'The quick brown fox jump...', util::limit_characters( 'The quick brown fox jumps over the lazy dog', 24 ) );
|
||||
$this->assertEquals( 'The quick brown fox jumps over the lazy dog', util::limit_characters( 'The quick brown fox jumps over the lazy dog', 55 ) );
|
||||
$this->assertEquals( 'Th...', util::limit_characters( 'The quick brown fox jumps over the lazy dog', 2 ) );
|
||||
$this->assertEquals( 'The...', util::limit_characters( 'The quick brown fox jumps over the lazy dog', 3 ) );
|
||||
$this->assertEquals( 'The qui...', util::limit_characters( 'The quick brown fox jumps over the lazy dog', 7 ) );
|
||||
$this->assertEquals( 'The quick brown fox jumps over the lazy dog', util::limit_characters( 'The quick brown fox jumps over the lazy dog', 150 ) );
|
||||
}
|
||||
|
||||
public function test_limit_words()
|
||||
{
|
||||
$this->assertEquals( 'The quick brown...', util::limit_words( 'The quick brown fox jumps over the lazy dog', 3 ) );
|
||||
$this->assertEquals( 'The quick brown fox jumps...', util::limit_words( 'The quick brown fox jumps over the lazy dog', 5 ) );
|
||||
$this->assertEquals( 'The...', util::limit_words( 'The quick brown fox jumps over the lazy dog', 1 ) );
|
||||
$this->assertEquals( 'The quick brown fox jumps over the lazy dog', util::limit_words( 'The quick brown fox jumps over the lazy dog', 90 ) );
|
||||
$this->assertEquals( 'The quick brown fox jumps over the...', util::limit_words( 'The quick brown fox jumps over the lazy dog', 7 ) );
|
||||
}
|
||||
|
||||
public function test_ordinal()
|
||||
{
|
||||
$this->assertEquals( '1st', util::ordinal( 1 ) );
|
||||
$this->assertEquals( '2nd', util::ordinal( 2 ) );
|
||||
$this->assertEquals( '3rd', util::ordinal( 3 ) );
|
||||
$this->assertEquals( '4th', util::ordinal( 4 ) );
|
||||
$this->assertEquals( '5th', util::ordinal( 5 ) );
|
||||
$this->assertEquals( '6th', util::ordinal( 6 ) );
|
||||
$this->assertEquals( '7th', util::ordinal( 7 ) );
|
||||
$this->assertEquals( '8th', util::ordinal( 8 ) );
|
||||
$this->assertEquals( '9th', util::ordinal( 9 ) );
|
||||
$this->assertEquals( '22nd', util::ordinal( 22 ) );
|
||||
$this->assertEquals( '23rd', util::ordinal( 23 ) );
|
||||
$this->assertEquals( '143rd', util::ordinal( 143 ) );
|
||||
}
|
||||
|
||||
public function test_array_first()
|
||||
{
|
||||
$test = array( 'a' => array( 'a', 'b', 'c' ) );
|
||||
|
||||
$this->assertEquals( 'a', util::array_first( util::array_get( $test['a'] ) ) );
|
||||
}
|
||||
|
||||
public function test_array_first_key()
|
||||
{
|
||||
$test = array( 'a' => array( 'a' => 'b', 'c' => 'd' ) );
|
||||
|
||||
$this->assertEquals( 'a', util::array_first_key( util::array_get( $test['a'] ) ) );
|
||||
}
|
||||
|
||||
public function test_array_last()
|
||||
{
|
||||
$test = array( 'a' => array( 'a', 'b', 'c' ) );
|
||||
|
||||
$this->assertEquals( 'c', util::array_last( util::array_get( $test['a'] ) ) );
|
||||
}
|
||||
|
||||
public function test_array_last_key()
|
||||
{
|
||||
$test = array( 'a' => array( 'a' => 'b', 'c' => 'd' ) );
|
||||
|
||||
$this->assertEquals( 'c', util::array_last_key( util::array_get( $test['a'] ) ) );
|
||||
}
|
||||
|
||||
public function test_array_flatten()
|
||||
{
|
||||
$input = array( 'a', 'b', 'c', 'd', array( 'first' => 'e', 'f', 'second' => 'g', array( 'h', 'third' => 'i', array( array( array( array( 'j', 'k', 'l' ) ) ) ) ) ) );
|
||||
$expectNoKeys = range( 'a', 'l' );
|
||||
$expectWithKeys = array(
|
||||
'a', 'b', 'c', 'd',
|
||||
'first' => 'e',
|
||||
'f',
|
||||
'second' => 'g',
|
||||
'h',
|
||||
'third' => 'i',
|
||||
'j', 'k', 'l'
|
||||
);
|
||||
|
||||
$this->assertEquals( $expectWithKeys, util::array_flatten( $input ) );
|
||||
$this->assertEquals( $expectNoKeys, util::array_flatten( $input, false ) );
|
||||
$this->assertEquals( $expectWithKeys, util::array_flatten( $input, true ) );
|
||||
}
|
||||
|
||||
public function test_strip_space()
|
||||
{
|
||||
$input = ' The quick brown fox jumps over the lazy dog ';
|
||||
$expect = 'Thequickbrownfoxjumpsoverthelazydog';
|
||||
|
||||
$this->assertEquals($expect, Util::strip_space($input));
|
||||
}
|
||||
|
||||
public function test_sanitize_string()
|
||||
{
|
||||
$input = ' Benoit! à New-York? j’ai perçu 1 % : Qu’as-tu "gagné" chez M. V. Noël? Dix francs.';
|
||||
$expect = 'benoitanewyorkjaipercu1quastugagnechezmvnoeldixfrancs';
|
||||
|
||||
$this->assertEquals($expect, Util::sanitize_string($input));
|
||||
}
|
||||
|
||||
public function test_get_gravatar()
|
||||
{
|
||||
$_SERVER['HTTPS'] = 'on';
|
||||
$this->assertEquals('https://secure.gravatar.com/avatar/a4bf5bbb9feaa2713d99a3b52ab80024?s=32', util::get_gravatar('john.doe@example.org'));
|
||||
$this->assertEquals('https://secure.gravatar.com/avatar/a4bf5bbb9feaa2713d99a3b52ab80024?s=128', util::get_gravatar('john.doe@example.org', 128));
|
||||
|
||||
$_SERVER['HTTPS'] = 'off';
|
||||
$this->assertEquals('http://www.gravatar.com/avatar/a4bf5bbb9feaa2713d99a3b52ab80024?s=32', util::get_gravatar('john.doe@example.org'));
|
||||
$this->assertEquals('http://www.gravatar.com/avatar/a4bf5bbb9feaa2713d99a3b52ab80024?s=128', util::get_gravatar('john.doe@example.org', 128));
|
||||
}
|
||||
|
||||
public function test_get_client_ip()
|
||||
{
|
||||
$_SERVER['REMOTE_ADDR'] = '192.168.30.152';
|
||||
$_SERVER['HTTP_CLIENT_IP'] = '192.168.30.153';
|
||||
$_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.30.154';
|
||||
$this->assertEquals('192.168.30.152', util::get_client_ip());
|
||||
$this->assertEquals('192.168.30.153', util::get_client_ip(true));
|
||||
|
||||
unset($_SERVER['HTTP_CLIENT_IP']);
|
||||
$this->assertEquals('192.168.30.154', util::get_client_ip(true));
|
||||
unset($_SERVER['HTTP_X_FORWARDED_FOR']);
|
||||
$this->assertEquals('192.168.30.152', util::get_client_ip(true));
|
||||
}
|
||||
|
||||
public function test_full_permissions()
|
||||
{
|
||||
// Text a non-existant file.
|
||||
$this->assertFalse(util::full_permissions('faker-123.blah'), 'Gave a permission value for a non-existant file.');
|
||||
|
||||
// Test an existing file.
|
||||
$expected = '-rw-rw-rw-';
|
||||
$tempFile = tempnam(sys_get_temp_dir(), 'foo');
|
||||
$this->assertTrue(chmod($tempFile, 0666), 'Oops. Could not change temp file\'s permissions to 0666.');
|
||||
$this->assertEquals($expected, util::full_permissions($tempFile), 'Could not properly obtain permissions of an existing file.');
|
||||
unlink($tempFile);
|
||||
|
||||
$this->assertEquals('lr--r--r--', util::full_permissions('fake-file-222', octdec('120444')));
|
||||
$this->assertEquals('ur--r--r--', util::full_permissions('fake-file-222', octdec('000444')));
|
||||
$this->assertEquals('srwxr-xr-x', util::full_permissions('fake-file-222', octdec('140755')));
|
||||
$this->assertEquals('drwxr-xr-x', util::full_permissions('fake-file-222', octdec('40755')));
|
||||
$this->assertEquals('brw-rw----', util::full_permissions('fake-file-222', octdec('60660')));
|
||||
$this->assertEquals('crw-rw----', util::full_permissions('fake-file-222', octdec('20660')));
|
||||
$this->assertEquals('prw-rw----', util::full_permissions('fake-file-222', octdec('10660')));
|
||||
$this->assertEquals('---x------', util::full_permissions('fake-file-222', octdec('100100')));
|
||||
$this->assertEquals('--w-------', util::full_permissions('fake-file-222', octdec('100200')));
|
||||
$this->assertEquals('--wx------', util::full_permissions('fake-file-222', octdec('100300')));
|
||||
$this->assertEquals('-r--------', util::full_permissions('fake-file-222', octdec('100400')));
|
||||
$this->assertEquals('-r-x------', util::full_permissions('fake-file-222', octdec('100500')));
|
||||
$this->assertEquals('-rw-------', util::full_permissions('fake-file-222', octdec('100600')));
|
||||
$this->assertEquals('-rwx------', util::full_permissions('fake-file-222', octdec('100700')));
|
||||
|
||||
// Windows does not have the concept of /.
|
||||
if (!defined('PHP_WINDOWS_VERSION_MAJOR')) {
|
||||
$this->assertEquals('drwxr-xr-x', util::full_permissions('/'));
|
||||
}
|
||||
}
|
||||
|
||||
public function test_array_clean()
|
||||
{
|
||||
$input = array( 'a', 'b', '', null, false, 0);
|
||||
$expect = array('a', 'b');
|
||||
$this->assertEquals($expect, Util::array_clean($input));
|
||||
}
|
||||
|
||||
public function test_var_dump_plain()
|
||||
{
|
||||
$input = 'var';
|
||||
$expect = '<span style="color:#588bff;">string</span><span style="color:#999;">(</span>3<span style="color:#999;">)</span> <strong>"var"</strong>';
|
||||
$this->assertEquals($expect, Util::var_dump_plain($input, true));
|
||||
|
||||
$input = true;
|
||||
$expect = '<span style="color:#588bff;">bool</span><span style="color:#999;">(</span><strong>true</strong><span style="color:#999;">)</span>';
|
||||
$this->assertEquals($expect, Util::var_dump_plain($input, true));
|
||||
|
||||
$input = 1;
|
||||
$expect = '<span style="color:#588bff;">int</span><span style="color:#999;">(</span><strong>1</strong><span style="color:#999;">)</span>';
|
||||
$this->assertEquals($expect, Util::var_dump_plain($input, true));
|
||||
|
||||
$input = 1.5;
|
||||
$expect = '<span style="color:#588bff;">float</span><span style="color:#999;">(</span><strong>1.5</strong><span style="color:#999;">)</span>';
|
||||
$this->assertEquals($expect, Util::var_dump_plain($input, true));
|
||||
|
||||
$input = null;
|
||||
$expect = '<strong>NULL</strong>';
|
||||
$this->assertEquals($expect, Util::var_dump_plain($input, true));
|
||||
|
||||
$input = fopen('php://memory', 'r');
|
||||
$expect = '<span style="color:#588bff;">resource</span>("stream") <strong>"' . $input . '"</strong>';
|
||||
$this->assertEquals($expect, Util::var_dump_plain($input, -1));
|
||||
fclose($input);
|
||||
|
||||
// Test complex arrays.
|
||||
$input = array(1, 2, 4, 6, 10 => 20, 100 => 200);
|
||||
$actual = util::var_dump_plain($input, true);
|
||||
$this->assertContains('<img id="include-php-', $actual);
|
||||
$this->assertContains('<br /> 100 => <span', $actual);
|
||||
$this->assertContains('(</span><strong>200</strong><span style="color:#999;">)', $actual);
|
||||
|
||||
// Test complex objects.
|
||||
$experiment = new VarDumpExperiment();
|
||||
$actual = util::var_dump_plain($experiment, true);
|
||||
$this->assertContains('1 => <span style="color:#588bff;">string</span><span style="color:#999;">(</span>1<span style="color:#999;">', $actual);
|
||||
}
|
||||
|
||||
public function test_var_dump()
|
||||
{
|
||||
$input = 'var';
|
||||
$expect = '<pre style="margin-bottom: 18px;background: #f7f7f9;border: 1px solid #e1e1e8;padding: 8px;border-radius: 4px;-moz-border-radius: 4px;-webkit-border radius: 4px;display: block;font-size: 12.05px;white-space: pre-wrap;word-wrap: break-word;color: #333;font-family: Menlo,Monaco,Consolas,\'Courier New\',monospace;"><span style="color:#588bff;">string</span><span style="color:#999;">(</span>3<span style="color:#999;">)</span> <strong>"var"</strong></pre>';
|
||||
|
||||
// Ensure the proper output is returned
|
||||
$this->assertEquals($expect, Util::var_dump($input, true));
|
||||
|
||||
// Ensure the proper output is actually outputted
|
||||
$this->expectOutputString($expect);
|
||||
util::var_dump($input);
|
||||
|
||||
// Ensure we avoid infinite recursion on recursive arrays
|
||||
$a = array('a' => 'value a', 'b' => 'value b');
|
||||
$b = array('test' => &$a);
|
||||
$c = array('a' => &$a, 'b' => &$b);
|
||||
$a['c'] = &$c;
|
||||
$b['c'] = &$c;
|
||||
$this->assertContains('*RECURSION DETECTED*', Util::var_dump($c, true));
|
||||
|
||||
// Ensure we avoid infinite recursion on recursive objects
|
||||
$a = (object) array('a' => 'value a', 'b' => 'value b');
|
||||
$b = (object) array('test' => &$a);
|
||||
$c = (object) array('a' => &$a, 'b' => &$b);
|
||||
$a->c = &$c;
|
||||
$b->c = &$c;
|
||||
$this->assertContains('*RECURSION DETECTED*', Util::var_dump($c, true));
|
||||
|
||||
// Test class scoping.
|
||||
$experiment = new VarDumpExperiment();
|
||||
$actual = util::var_dump($experiment, true);
|
||||
|
||||
$snippet = substr($actual, strrpos($actual, 'display:inline'));
|
||||
$this->assertContains('"public"', $snippet);
|
||||
$this->assertContains('"protected:protected"', $snippet);
|
||||
$this->assertContains('"private:VarDumpExperiment:private"', $actual);
|
||||
}
|
||||
|
||||
public function test_linkify()
|
||||
{
|
||||
$input = 'great websites: http://www.google.com?param=test and http://yahoo.com/a/nested/folder';
|
||||
$expect = 'great websites: <a href="http://www.google.com?param=test">http://www.google.com?param=test</a> and <a href="http://yahoo.com/a/nested/folder">http://yahoo.com/a/nested/folder</a>';
|
||||
$this->assertEquals($expect, Util::linkify($input));
|
||||
|
||||
$this->assertEquals($expect, util::linkify($expect), 'linkify() tried to double linkify an href.');
|
||||
}
|
||||
|
||||
public function test_start_with()
|
||||
{
|
||||
$this->assertTrue(Util::starts_with('start a string', 'start a'));
|
||||
$this->assertFalse(Util::starts_with('start a string', 'string'));
|
||||
}
|
||||
|
||||
public function test_end_with()
|
||||
{
|
||||
$this->assertTrue(Util::ends_with('start a string', 'a string'));
|
||||
$this->assertFalse(Util::ends_with('start a string', 'start'));
|
||||
}
|
||||
|
||||
public function test_contains()
|
||||
{
|
||||
$this->assertTrue(Util::str_contains('start a string', 'a string'));
|
||||
$this->assertFalse(Util::str_contains('start a string', 'Start'));
|
||||
}
|
||||
|
||||
public function test_iContains()
|
||||
{
|
||||
$this->assertTrue(Util::str_icontains('start a string', 'a string'));
|
||||
$this->assertTrue(Util::str_icontains('start a string', 'Start'));
|
||||
}
|
||||
|
||||
public function test_get_file_ext()
|
||||
{
|
||||
$input = 'a_pdf_fil.pdf';
|
||||
$expect = 'pdf';
|
||||
$this->assertEquals($expect, Util::get_file_ext($input));
|
||||
}
|
||||
|
||||
public function test_rmdir()
|
||||
{
|
||||
$dirname = dirname(__FILE__);
|
||||
|
||||
// Test deleting a non-existant directory
|
||||
$this->assertFalse(file_exists($dirname . '/test1'));
|
||||
$this->assertTrue(util::rmdir($dirname . '/test1'));
|
||||
|
||||
// Test deleting an empty directory
|
||||
$dir = $dirname . '/test2';
|
||||
mkdir($dir);
|
||||
|
||||
$this->assertTrue(is_dir($dir));
|
||||
|
||||
if (is_dir($dir)) {
|
||||
util::rmdir($dir);
|
||||
$this->assertFalse(is_dir($dir));
|
||||
}
|
||||
|
||||
// Test deleting a non-empty directory
|
||||
$dir = $dirname . '/test3';
|
||||
$file = $dirname . '/test3/test.txt';
|
||||
mkdir($dir);
|
||||
touch($file);
|
||||
|
||||
$this->assertTrue(is_dir($dir));
|
||||
$this->assertTrue(is_file($file));
|
||||
|
||||
if (is_dir($dir)) {
|
||||
util::rmdir($dir);
|
||||
$this->assertFalse(is_dir($dir));
|
||||
$this->assertFalse(is_file($file));
|
||||
}
|
||||
|
||||
// Test deleting a non-directory path
|
||||
$file = $dirname . '/test4.txt';
|
||||
touch($file);
|
||||
|
||||
try {
|
||||
$str = util::rmdir($file);
|
||||
$this->assertTrue(false);
|
||||
} catch (\Exception $e) {
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
unlink($file);
|
||||
|
||||
// Test deleting a nested directory
|
||||
$dir1 = $dirname . '/test5';
|
||||
$dir2 = $dirname . '/test5/nested_dir';
|
||||
$file1 = $dir1 . '/file1.txt';
|
||||
$file2 = $dir2 . '/file2.txt';
|
||||
mkdir($dir1);
|
||||
mkdir($dir2);
|
||||
touch($file1);
|
||||
touch($file2);
|
||||
|
||||
$this->assertTrue(is_dir($dir1));
|
||||
$this->assertTrue(is_dir($dir2));
|
||||
$this->assertTrue(is_file($file1));
|
||||
$this->assertTrue(is_file($file2));
|
||||
|
||||
if (is_dir($dir1)) {
|
||||
util::rmdir($dir1);
|
||||
$this->assertFalse(is_dir($dir1));
|
||||
$this->assertFalse(is_dir($dir2));
|
||||
$this->assertFalse(is_file($file1));
|
||||
$this->assertFalse(is_file($file2));
|
||||
}
|
||||
|
||||
// Test symlink traversal.
|
||||
$dir = $dirname . '/test6';
|
||||
$nestedDir = "$dir/nested";
|
||||
$symlink = "$dir/nested-symlink";
|
||||
|
||||
mkdir($dir);
|
||||
mkdir($nestedDir);
|
||||
|
||||
$symlinkStatus = symlink($nestedDir, $symlink);
|
||||
$this->assertTrue($symlinkStatus, 'The test system does not support making symlinks.');
|
||||
|
||||
if (!$symlink) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->assertTrue(util::rmdir($symlink, true), 'Could not delete a symlinked directory.');
|
||||
$this->assertFalse(file_exists($symlink), 'Could not delete a symlinked directory.');
|
||||
util::rmdir($dir, true);
|
||||
$this->assertFalse(is_dir($dir), 'Could not delete a directory with a symlinked directory inside of it.');
|
||||
}
|
||||
|
||||
public function test_get_current_url()
|
||||
{
|
||||
$expected = 'http://test.dev/test.php?foo=bar';
|
||||
$expectedAuth = 'http://admin:123@test.dev/test.php?foo=bar';
|
||||
$expectedPort = 'http://test.dev:443/test.php?foo=bar';
|
||||
$expectedPort2 = 'https://test.dev:80/test.php?foo=bar';
|
||||
$expectedSSL = 'https://test.dev/test.php?foo=bar';
|
||||
|
||||
$_SERVER['HTTP_HOST'] = 'test.dev';
|
||||
$_SERVER['SERVER_PORT'] = 80;
|
||||
$_SERVER['REQUEST_URI'] = '/test.php?foo=bar';
|
||||
$_SERVER['QUERY_STRING'] = 'foo=bar';
|
||||
$_SERVER['PHP_SELF'] = '/test.php';
|
||||
|
||||
// Test regular.
|
||||
$this->assertEquals($expected, util::get_current_url());
|
||||
|
||||
// Test server auth.
|
||||
$_SERVER['PHP_AUTH_USER'] = 'admin';
|
||||
$_SERVER['PHP_AUTH_PW'] = '123';
|
||||
$this->assertEquals($expectedAuth, util::get_current_url());
|
||||
unset($_SERVER['PHP_AUTH_USER']);
|
||||
unset($_SERVER['PHP_AUTH_PW']);
|
||||
|
||||
// Test port.
|
||||
$_SERVER['SERVER_PORT'] = 443;
|
||||
$this->assertEquals($expectedPort, util::get_current_url());
|
||||
|
||||
// Test SSL.
|
||||
$_SERVER['HTTPS'] = 'on';
|
||||
$this->assertEquals($expectedSSL, util::get_current_url());
|
||||
$_SERVER['SERVER_PORT'] = 80;
|
||||
$this->assertEquals($expectedPort2, util::get_current_url());
|
||||
unset($_SERVER['HTTPS']);
|
||||
|
||||
// Test no $_SERVER['REQUEST_URI'] (e.g., MS IIS).
|
||||
unset($_SERVER['REQUEST_URI']);
|
||||
$this->assertEquals($expected, util::get_current_url());
|
||||
}
|
||||
|
||||
public function test_set_writable()
|
||||
{
|
||||
if (strncasecmp(PHP_OS, 'WIN', 3) === 0) {
|
||||
$this->markTestSkipped('This functionality is not working on Windows.');
|
||||
}
|
||||
|
||||
if (posix_geteuid() === 0) {
|
||||
$this->markTestSkipped('These tests don\'t work when run as root');
|
||||
}
|
||||
|
||||
$this->assertFalse(util::set_writable('/no/such/file'));
|
||||
|
||||
// Create a file to test with
|
||||
$dirname = dirname(__FILE__);
|
||||
$file = $dirname . '/test7';
|
||||
touch($file);
|
||||
chmod($file, 0644);
|
||||
|
||||
// The file is owned by us so it should be writable
|
||||
$this->assertTrue(is_writable($file));
|
||||
$this->assertEquals('-rw-r--r--', util::full_permissions($file));
|
||||
|
||||
// Toggle writable bit off for us
|
||||
util::set_writable($file, false);
|
||||
clearstatcache();
|
||||
$this->assertFalse(is_writable($file));
|
||||
$this->assertEquals('-r--r--r--', util::full_permissions($file));
|
||||
|
||||
// Toggle writable bit back on for us
|
||||
util::set_writable($file, true);
|
||||
clearstatcache();
|
||||
$this->assertTrue(is_writable($file));
|
||||
$this->assertEquals('-rw-r--r--', util::full_permissions($file));
|
||||
|
||||
unlink($file);
|
||||
}
|
||||
|
||||
public function test_set_readable()
|
||||
{
|
||||
if (strncasecmp(PHP_OS, 'WIN', 3) === 0) {
|
||||
$this->markTestSkipped('This functionality is not working on Windows.');
|
||||
}
|
||||
|
||||
if (posix_geteuid() === 0) {
|
||||
$this->markTestSkipped('These tests don\'t work when run as root');
|
||||
}
|
||||
|
||||
$this->assertFalse(util::set_readable('/no/such/file'));
|
||||
|
||||
$dirname = dirname(__FILE__);
|
||||
$file = $dirname . '/test8';
|
||||
touch($file);
|
||||
|
||||
$this->assertTrue(is_readable($file));
|
||||
|
||||
util::set_readable($file, false);
|
||||
clearstatcache();
|
||||
$this->assertFalse(is_readable($file));
|
||||
|
||||
util::set_readable($file, true);
|
||||
clearstatcache();
|
||||
$this->assertTrue(is_readable($file));
|
||||
|
||||
unlink($file);
|
||||
}
|
||||
|
||||
public function test_set_executable()
|
||||
{
|
||||
if (strncasecmp(PHP_OS, 'WIN', 3) === 0) {
|
||||
$this->markTestSkipped('This functionality is not working on Windows.');
|
||||
}
|
||||
|
||||
if (posix_geteuid() === 0) {
|
||||
$this->markTestSkipped('These tests don\'t work when run as root');
|
||||
}
|
||||
|
||||
$this->assertFalse(util::set_executable('/no/such/file'));
|
||||
|
||||
$dirname = dirname(__FILE__);
|
||||
$file = $dirname . '/test9';
|
||||
touch($file);
|
||||
|
||||
$this->assertFalse(is_executable($file));
|
||||
|
||||
util::set_executable($file, true);
|
||||
clearstatcache();
|
||||
$this->assertTrue(is_executable($file));
|
||||
|
||||
util::set_executable($file, false);
|
||||
clearstatcache();
|
||||
$this->assertFalse(is_executable($file));
|
||||
|
||||
unlink($file);
|
||||
}
|
||||
|
||||
public function test_directory_size() {
|
||||
$dirname = dirname(__FILE__);
|
||||
$dir = $dirname .'/dir1';
|
||||
mkdir($dir);
|
||||
$file1 = $dir .'/file1';
|
||||
file_put_contents($file1, '1234567890');
|
||||
$file2 = $dir .'/file2';
|
||||
file_put_contents($file2, range('a', 'z'));
|
||||
$this->assertEquals(10 + 26, util::directory_size($dir));
|
||||
util::rmdir($dir);
|
||||
}
|
||||
|
||||
public function test_get_user_directory() {
|
||||
// Test for OS Default.
|
||||
$this->assertTrue(is_writable(util::get_user_directory()));
|
||||
|
||||
$oldServer = $_SERVER;
|
||||
unset($_SERVER);
|
||||
// Test for UNIX.
|
||||
$_SERVER['HOME'] = '/home/unknown';
|
||||
$this->assertEquals($_SERVER['HOME'], util::get_user_directory(), 'Could not get the user\'s home directory in UNIX.');
|
||||
unset($_SERVER);
|
||||
|
||||
// Test for Windows.
|
||||
$expected = 'X:\Users\ThisUser';
|
||||
$_SERVER['HOMEDRIVE'] = 'X:';
|
||||
$_SERVER['HOMEPATH'] = '\Users\ThisUser';
|
||||
$this->assertEquals($expected, util::get_user_directory(), 'Could not get the user\'s home directory in Windows.');
|
||||
|
||||
// In case the tests are not being run in isolation.
|
||||
$_SERVER = $oldServer;
|
||||
}
|
||||
|
||||
public function test_directory_contents() {
|
||||
$dirname = dirname(__FILE__);
|
||||
$dir = $dirname . DIRECTORY_SEPARATOR .'dir1';
|
||||
mkdir($dir);
|
||||
$file1 = $dir . DIRECTORY_SEPARATOR .'file1';
|
||||
touch($file1);
|
||||
$this->assertEquals(array($file1), util::directory_contents($dir));
|
||||
util::rmdir($dir);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -9,4 +9,4 @@ if($SETTING->IsExist()) {
|
||||
Error('설정 파일이 없습니다. 설정을 먼저 하십시요!');
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
+1
-1
@@ -10,4 +10,4 @@ for($i=0; $i < $_serverCount; $i++) {
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
+1
-1
@@ -9,4 +9,4 @@ if($SETTING->IsExist()) {
|
||||
Error('설정 파일이 없습니다. 설정을 먼저 하십시요!');
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -10,4 +10,4 @@ if($SESSION->IsLoggedIn() == false) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -4,4 +4,4 @@ require_once(ROOT.W.F_FUNC.W.'class._Setting.php');
|
||||
|
||||
$SETTING = new _Setting(ROOT.W.D_SETTING.W.SET.PHP);
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -8,4 +8,4 @@ foreach($_serverDirs as $serverDir) {
|
||||
$i++;
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -7,4 +7,4 @@ require_once(ROOT.W.F_CONFIG.W.APP.PHP);
|
||||
require_once(ROOT.W.F_FUNC.W.FUNC.PHP);
|
||||
|
||||
CustomHeader();
|
||||
?>
|
||||
|
||||
|
||||
+1
-1
@@ -42,4 +42,4 @@ $_serverLevels = array(
|
||||
1
|
||||
);
|
||||
|
||||
?>
|
||||
|
||||
|
||||
+1
-1
@@ -53,4 +53,4 @@ define('SLEEP', 'sleep');
|
||||
//define(BLOCKBODY, '');
|
||||
define('BLOCKBODY', 'oncontextmenu="return false" onselectstart="return false" ondragstart="return false"');
|
||||
|
||||
?>
|
||||
|
||||
|
||||
+1
-1
@@ -7,4 +7,4 @@ require_once(ROOT.W.F_CONFIG.W.APP.PHP);
|
||||
require_once(ROOT.W.F_FUNC.W.FUNC.PHP);
|
||||
|
||||
CustomHeader();
|
||||
?>
|
||||
|
||||
|
||||
@@ -24,4 +24,4 @@ class _Chat {
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -142,4 +142,4 @@ class _DB {
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -52,4 +52,4 @@ class _JSON {
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -70,4 +70,4 @@ class _Lock {
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -45,4 +45,4 @@ class _Log {
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -37,4 +37,4 @@ class _Mail {
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -33,4 +33,4 @@ class _Process {
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -39,4 +39,4 @@ class _Queue {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
|
||||
@@ -72,4 +72,4 @@ class _Session {
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -78,4 +78,4 @@ class _Setting {
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -234,4 +234,3 @@ class _String {
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -44,4 +44,4 @@ class _Time {
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@@ -111,4 +111,4 @@ class _Validation {
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user