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

/libs/class.Utils.php

http://github.com/ginatrapani/isosceles
PHP | 201 lines | 113 code | 15 blank | 73 comment | 47 complexity | 5cc5edaeadd1cc7d5e4c8a5b028b501e MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, LGPL-3.0, Apache-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * LICENSE:
  4. *
  5. * This file is part of Isosceles (http://ginatrapani.github.io/isosceles/).
  6. *
  7. * Isosceles is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  9. * later version.
  10. *
  11. * Isosceles is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  12. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with Isosceles. If not, see
  16. * <http://www.gnu.org/licenses/>.
  17. *
  18. * @license http://www.gnu.org/licenses/gpl.html
  19. */
  20. class Utils {
  21. /**
  22. * Indents a flat JSON string to make it more human-readable.
  23. *
  24. * @author http://recursive-design.com/blog/2008/03/11/format-json-with-php/
  25. * @param string $json The original JSON string to process.
  26. * @return string Indented version of the original JSON string.
  27. */
  28. public static function indentJSON($json) {
  29. $result = '';
  30. $pos = 0;
  31. $str_len = strlen($json);
  32. $indent_str = ' ';
  33. $new_line = "\n";
  34. $prev_char = '';
  35. $prev_prev_char = '';
  36. $out_of_quotes = true;
  37. for ($i = 0; $i <= $str_len; $i++) {
  38. // Grab the next character in the string.
  39. $char = substr($json, $i, 1);
  40. // Are we inside a quoted string?
  41. if ($char == '"') {
  42. if ( $prev_char != "\\") {
  43. $out_of_quotes = !$out_of_quotes;
  44. } elseif ($prev_prev_char == "\\") {
  45. $out_of_quotes = !$out_of_quotes;
  46. }
  47. // If this character is the end of an element,
  48. // output a new line and indent the next line.
  49. } else if (($char == '}' || $char == ']') && $out_of_quotes) {
  50. $result .= $new_line;
  51. $pos--;
  52. for ($j = 0; $j < $pos; $j++) {
  53. $result .= $indent_str;
  54. }
  55. }
  56. // Add the character to the result string.
  57. $result .= $char;
  58. // If the last character was the beginning of an element,
  59. // output a new line and indent the next line.
  60. if (($char == ',' || $char == '{' || $char == '[') && $out_of_quotes) {
  61. $result .= $new_line;
  62. if ($char == '{' || $char == '[') {
  63. $pos++;
  64. }
  65. for ($j = 0; $j < $pos; $j++) {
  66. $result .= $indent_str;
  67. }
  68. }
  69. $prev_prev_char = $prev_char;
  70. $prev_char = $char;
  71. }
  72. return $result;
  73. }
  74. /**
  75. * Becuse PHP doesn't have a data type large enough to hold some of the
  76. * numbers that Twitter deals with, this function strips the double
  77. * quotes off every string that contains only numbers inside the double
  78. * quotes.
  79. *
  80. * @param string $encoded_json JSON formatted string.
  81. * @return string Encoded JSON with numeric strings converted to numbers.
  82. */
  83. public static function convertNumericStrings($encoded_json) {
  84. return preg_replace('/\"((?:-)?[0-9]+(\.[0-9]+)?)\"/', '$1', $encoded_json);
  85. }
  86. /**
  87. * If date.timezone is not set in php.ini, default to America/Los_Angeles to avoid date() warning about
  88. * using system settings.
  89. * This method exists to avoid the warning which Smarty triggers in views that don't have access to a
  90. * THINKUP_CFG timezone setting yet, like during installation, or when a config file doesn't exist.
  91. */
  92. public static function setDefaultTimezonePHPini() {
  93. if (ini_get('date.timezone') == false) {
  94. // supress the date_default_timezone_get() warn as php 5.3.* doesn't like when date.timezone is not set in
  95. // php.ini, but many systems comment it out by default, or have no php.ini by default
  96. $error_reporting = error_reporting(); // save old reporting setting
  97. error_reporting( E_ERROR | E_USER_ERROR ); // turn off warning messages
  98. $tz = date_default_timezone_get(); // get tz if we can
  99. error_reporting( $error_reporting ); // reset error reporting
  100. if(! $tz) { // if no $tz defined, use UTC
  101. $tz = 'UTC';
  102. }
  103. ini_set('date.timezone',$tz);
  104. }
  105. }
  106. /**
  107. * Generate var dump to string.
  108. * @return str
  109. */
  110. public static function varDumpToString($mixed = null) {
  111. ob_start();
  112. var_dump($mixed);
  113. $content = ob_get_contents();
  114. ob_end_clean();
  115. return $content;
  116. }
  117. /**
  118. * Get the application's host name or server name, i.e., example.com.
  119. * @return str Host name either set by PHP global vars or stored in the database
  120. */
  121. public static function getApplicationHostName() {
  122. //First attempt to get the host name without querying the database
  123. //Try SERVER_NAME
  124. $server = empty($_SERVER['SERVER_NAME']) ? '' : $_SERVER['SERVER_NAME'];
  125. //Second, try HTTP_HOST
  126. if ($server == '' ) {
  127. $server = empty($_SERVER['HTTP_HOST']) ? '' : $_SERVER['HTTP_HOST'];
  128. }
  129. //Finally fall back to defined application setting in config
  130. if ($server == '') {
  131. $config = Config::getInstance();
  132. $server = $config->getValue('application_host');
  133. }
  134. //domain name is always lowercase
  135. $server = strtolower($server);
  136. return $server;
  137. }
  138. /**
  139. * Get application URL
  140. * @param bool $replace_localhost_with_ip
  141. * @param bool $use_filesystem_path Use filesystem path instead of path specified in config.inc.php
  142. * @return str application URL
  143. */
  144. public static function getApplicationURL($replace_localhost_with_ip = false, $use_filesystem_path = true,
  145. $should_url_encode = true) {
  146. $server = self::getApplicationHostName();
  147. if ($replace_localhost_with_ip) {
  148. $server = ($server == 'localhost')?'127.0.0.1':$server;
  149. }
  150. if ($use_filesystem_path) {
  151. $site_root_path = Utils::getSiteRootPathFromFileSystem();
  152. } else {
  153. $cfg = Config::getInstance();
  154. $site_root_path = $cfg->getValue('site_root_path');
  155. }
  156. if ($should_url_encode) {
  157. //URLencode everything except spaces in site_root_path
  158. $site_root_path = str_replace('%2f', '/', strtolower(urlencode($site_root_path)));
  159. }
  160. if (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != '80') { //non-standard port
  161. if (isset($_SERVER['HTTPS']) && $_SERVER['SERVER_PORT'] == '443') { //account for standard https port
  162. $port = '';
  163. } else {
  164. $port = ':'.$_SERVER['SERVER_PORT'];
  165. }
  166. } else {
  167. $port = '';
  168. }
  169. return 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$server.$port.$site_root_path;
  170. }
  171. /**
  172. * Get site root path from filesystem.
  173. * @return str
  174. */
  175. public static function getSiteRootPathFromFileSystem() {
  176. if (isset($_SERVER['PHP_SELF'])) {
  177. $current_script_path = explode('/', $_SERVER['PHP_SELF']);
  178. } else {
  179. $current_script_path = array();
  180. }
  181. array_pop($current_script_path);
  182. $current_script_path = implode('/', $current_script_path) . '/';
  183. echo $current_script_path;
  184. return $current_script_path;
  185. }
  186. }