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

/wp-content/plugins/duplicator/installer/build/classes/class.utils.php

https://gitlab.com/sokeara/Tribal_Education
PHP | 351 lines | 202 code | 35 blank | 114 comment | 36 complexity | 2957d633ae1c7221486e3e5236c291c1 MD5 | raw file
  1. <?php
  2. // Exit if accessed directly
  3. if (! defined('DUPLICATOR_INIT')) {
  4. $_baseURL = "http://" . strlen($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'];
  5. header("HTTP/1.1 301 Moved Permanently");
  6. header("Location: $_baseURL");
  7. exit;
  8. }
  9. /** * *****************************************************
  10. * Various Static Utility methods for working with the installer */
  11. class DUPX_Util
  12. {
  13. /**
  14. * Get current microtime as a float. Can be used for simple profiling.
  15. */
  16. static public function get_microtime() {
  17. return microtime(true);
  18. }
  19. /**
  20. * Return a string with the elapsed time.
  21. * Order of $end and $start can be switched.
  22. */
  23. static public function elapsed_time($end, $start) {
  24. return sprintf("%.4f sec.", abs($end - $start));
  25. }
  26. /**
  27. * @param string $string Thing that needs escaping
  28. * @param bool $echo Do we echo or return?
  29. * @return string Escaped string.
  30. */
  31. static public function esc_html_attr($string = '', $echo = false) {
  32. $output = htmlentities($string, ENT_QUOTES, 'UTF-8');
  33. if ($echo)
  34. echo $output;
  35. else
  36. return $output;
  37. }
  38. /**
  39. * Count the tables in a given database
  40. * @param string $_POST['dbname'] Database to count tables in
  41. */
  42. static public function dbtable_count($conn, $dbname) {
  43. $res = mysqli_query($conn, "SELECT COUNT(*) AS count FROM information_schema.tables WHERE table_schema = '{$dbname}' ");
  44. $row = mysqli_fetch_row($res);
  45. return is_null($row) ? 0 : $row[0];
  46. }
  47. /**
  48. * Returns the table count
  49. * @param string $conn A valid link resource
  50. * @param string $table_name A valid table name
  51. */
  52. static public function table_row_count($conn, $table_name) {
  53. $total = mysqli_query($conn, "SELECT COUNT(*) FROM `$table_name`");
  54. if ($total) {
  55. $total = @mysqli_fetch_array($total);
  56. return $total[0];
  57. } else {
  58. return 0;
  59. }
  60. }
  61. /**
  62. * Adds a slash to the end of a path
  63. * @param string $path A path
  64. */
  65. static public function add_slash($path) {
  66. $last_char = substr($path, strlen($path) - 1, 1);
  67. if ($last_char != '/') {
  68. $path .= '/';
  69. }
  70. return $path;
  71. }
  72. /**
  73. * Makes path safe for any OS
  74. * Paths should ALWAYS READ be "/"
  75. * uni: /home/path/file.xt
  76. * win: D:/home/path/file.txt
  77. * @param string $path The path to make safe
  78. */
  79. static public function set_safe_path($path) {
  80. return str_replace("\\", "/", $path);
  81. }
  82. static public function unset_safe_path($path) {
  83. return str_replace("/", "\\", $path);
  84. }
  85. /**
  86. * PHP_SAPI for fcgi requires a data flush of at least 256
  87. * bytes every 40 seconds or else it forces a script hault
  88. */
  89. static public function fcgi_flush() {
  90. echo(str_repeat(' ', 256));
  91. @flush();
  92. }
  93. /**
  94. * A safe method used to copy larger files
  95. * @param string $source The path to the file being copied
  96. * @param string $destination The path to the file being made
  97. */
  98. static public function copy_file($source, $destination) {
  99. $sp = fopen($source, 'r');
  100. $op = fopen($destination, 'w');
  101. while (!feof($sp)) {
  102. $buffer = fread($sp, 512); // use a buffer of 512 bytes
  103. fwrite($op, $buffer);
  104. }
  105. // close handles
  106. fclose($op);
  107. fclose($sp);
  108. }
  109. /**
  110. * Finds a string in a file and returns true if found
  111. * @param array $list A list of strings to search for
  112. * @param string $haystack The string to search in
  113. */
  114. static public function string_has_value($list, $file) {
  115. foreach ($list as $var) {
  116. if (strstr($file, $var) !== false) {
  117. return true;
  118. }
  119. }
  120. return false;
  121. }
  122. /** METHOD: get_active_plugins
  123. * Returns the active plugins for a package
  124. * @param conn $dbh A database connection handle
  125. * @return array $list A list of active plugins
  126. */
  127. static public function get_active_plugins($dbh) {
  128. $query = @mysqli_query($dbh, "SELECT option_value FROM `{$GLOBALS['FW_TABLEPREFIX']}options` WHERE option_name = 'active_plugins' ");
  129. if ($query) {
  130. $row = @mysqli_fetch_array($query);
  131. $all_plugins = unserialize($row[0]);
  132. if (is_array($all_plugins)) {
  133. return $all_plugins;
  134. }
  135. }
  136. return array();
  137. }
  138. /**
  139. * Returns the tables for a database
  140. * @param conn $dbh A database connection handle
  141. * @return array $list A list of all table names
  142. */
  143. static public function get_database_tables($dbh) {
  144. $query = @mysqli_query($dbh, 'SHOW TABLES');
  145. if ($query) {
  146. while ($table = @mysqli_fetch_array($query)) {
  147. $all_tables[] = $table[0];
  148. }
  149. if (isset($all_tables) && is_array($all_tables)) {
  150. return $all_tables;
  151. }
  152. }
  153. return array();
  154. }
  155. /**
  156. * MySQL connection support for sock
  157. * @param same as mysqli_connect
  158. * @return database connection handle
  159. */
  160. static public function db_connect( $host, $username, $password, $dbname = '', $port = null ) {
  161. //sock connections
  162. if ( 'sock' === substr( $host, -4 ) )
  163. {
  164. $url_parts = parse_url( $host );
  165. $dbh = @mysqli_connect( 'localhost', $username, $password, $dbname, null, $url_parts['path'] );
  166. }
  167. else
  168. {
  169. $dbh = @mysqli_connect( $host, $username, $password, $dbname, $port );
  170. }
  171. return $dbh;
  172. }
  173. /**
  174. * MySQL database version number
  175. * @param conn $dbh Database connection handle
  176. * @return false|string false on failure, version number on success
  177. */
  178. static public function mysql_version($dbh) {
  179. if (function_exists('mysqli_get_server_info')) {
  180. return preg_replace('/[^0-9.].*/', '', mysqli_get_server_info($dbh));
  181. } else {
  182. return 0;
  183. }
  184. }
  185. /**
  186. * MySQL server variable
  187. * @param conn $dbh Database connection handle
  188. * @return string the server variable to query for
  189. */
  190. static public function mysql_variable_value($dbh, $variable) {
  191. $result = @mysqli_query($dbh, "SHOW VARIABLES LIKE '{$variable}'");
  192. $row = @mysqli_fetch_array($result);
  193. @mysqli_free_result($result);
  194. return isset($row[1]) ? $row[1] : null;
  195. }
  196. /**
  197. * Determine if a MySQL database supports a particular feature
  198. * @param conn $dbh Database connection handle
  199. * @param string $feature the feature to check for
  200. * @return bool
  201. */
  202. static public function mysql_has_ability($dbh, $feature) {
  203. $version = self::mysql_version($dbh);
  204. switch (strtolower($feature)) {
  205. case 'collation' :
  206. case 'group_concat' :
  207. case 'subqueries' :
  208. return version_compare($version, '4.1', '>=');
  209. case 'set_charset' :
  210. return version_compare($version, '5.0.7', '>=');
  211. };
  212. return false;
  213. }
  214. /**
  215. * Sets the MySQL connection's character set.
  216. * @param resource $dbh The resource given by mysqli_connect
  217. * @param string $charset The character set (optional)
  218. * @param string $collate The collation (optional)
  219. */
  220. static public function mysql_set_charset($dbh, $charset = null, $collate = null) {
  221. $charset = (!isset($charset) ) ? $GLOBALS['DBCHARSET_DEFAULT'] : $charset;
  222. $collate = (!isset($collate) ) ? $GLOBALS['DBCOLLATE_DEFAULT'] : $collate;
  223. if (self::mysql_has_ability($dbh, 'collation') && !empty($charset)) {
  224. if (function_exists('mysqli_set_charset') && self::mysql_has_ability($dbh, 'set_charset')) {
  225. return mysqli_set_charset($dbh, $charset);
  226. } else {
  227. $sql = " SET NAMES {$charset}";
  228. if (!empty($collate))
  229. $sql .= " COLLATE {$collate}";
  230. return mysqli_query($dbh, $sql);
  231. }
  232. }
  233. }
  234. /**
  235. * Display human readable byte sizes
  236. * @param string $size The size in bytes
  237. */
  238. static public function readable_bytesize($size) {
  239. try {
  240. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  241. for ($i = 0; $size >= 1024 && $i < 4; $i++)
  242. $size /= 1024;
  243. return round($size, 2) . $units[$i];
  244. } catch (Exception $e) {
  245. return "n/a";
  246. }
  247. }
  248. /**
  249. * The characters that are special in the replacement value of preg_replace are not the
  250. * same characters that are special in the pattern
  251. * @param string $str The string to replace on
  252. */
  253. static public function preg_replacement_quote($str) {
  254. return preg_replace('/(\$|\\\\)(?=\d)/', '\\\\\1', $str);
  255. }
  256. /**
  257. * Check to see if the internet is accessable
  258. * NOTE: fsocketopen on windows doesn't seem to honor $timeout setting.
  259. *
  260. * @param string $url A url e.g without prefix "ajax.googleapis.com"
  261. * @param string $port A valid port number
  262. * @return bool
  263. */
  264. public static function is_url_active($url, $port, $timeout=5)
  265. {
  266. if (function_exists('fsockopen'))
  267. {
  268. @ini_set("default_socket_timeout", 5);
  269. $port = isset($port) && is_integer($port) ? $port : 80;
  270. $connected = @fsockopen($url, $port, $errno, $errstr, $timeout); //website and port
  271. if ($connected){
  272. @fclose($connected);
  273. return true;
  274. }
  275. return false;
  276. } else {
  277. return false;
  278. }
  279. }
  280. /**
  281. * Returns an array of zip files found in the current directory
  282. * @return array of zip files
  283. */
  284. static public function get_zip_files() {
  285. $files = array();
  286. foreach (glob("*.zip") as $name) {
  287. if (file_exists($name)) {
  288. $files[] = $name;
  289. }
  290. }
  291. if (count($files) > 0) {
  292. return $files;
  293. }
  294. //FALL BACK: Windows XP has bug with glob,
  295. //add secondary check for PHP lameness
  296. if ($dh = opendir('.'))
  297. {
  298. while (false !== ($name = readdir($dh))) {
  299. $ext = substr($name, strrpos($name, '.') + 1);
  300. if(in_array($ext, array("zip"))) {
  301. $files[] = $name;
  302. }
  303. }
  304. closedir($dh);
  305. }
  306. return $files;
  307. }
  308. /**
  309. * Does a string have non ascii characters
  310. */
  311. public static function is_non_ascii($string)
  312. {
  313. return preg_match('/[^\x20-\x7f]/', $string);
  314. }
  315. }
  316. ?>