PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/class/ui.class.php

https://gitlab.com/x33n/ampache
PHP | 355 lines | 197 code | 40 blank | 118 comment | 34 complexity | ff7c1f5f4c35be1a482b2d74ab34fc4b MD5 | raw file
  1. <?php
  2. /* vim:set softtabstop=4 shiftwidth=4 expandtab: */
  3. /**
  4. *
  5. * LICENSE: GNU General Public License, version 2 (GPLv2)
  6. * Copyright 2001 - 2015 Ampache.org
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; version 2
  11. * of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. *
  22. */
  23. // A collection of methods related to the user interface
  24. class UI
  25. {
  26. private static $_classes;
  27. private static $_ticker;
  28. private static $_icon_cache;
  29. public function __construct()
  30. {
  31. return false;
  32. }
  33. /**
  34. * access_denied
  35. *
  36. * Throw an error when they try to do something naughty.
  37. */
  38. public static function access_denied($error = 'Access Denied')
  39. {
  40. // Clear any buffered crap
  41. ob_end_clean();
  42. header("HTTP/1.1 403 $error");
  43. require_once AmpConfig::get('prefix') . '/templates/show_denied.inc.php';
  44. exit;
  45. }
  46. /**
  47. * ajax_include
  48. *
  49. * Does some trickery with the output buffer to return the output of a
  50. * template.
  51. */
  52. public static function ajax_include($template)
  53. {
  54. ob_start();
  55. require AmpConfig::get('prefix') . '/templates/' . $template;
  56. $output = ob_get_contents();
  57. ob_end_clean();
  58. return $output;
  59. }
  60. /**
  61. * check_iconv
  62. *
  63. * Checks to see whether iconv is available;
  64. */
  65. public static function check_iconv()
  66. {
  67. if (function_exists('iconv') && function_exists('iconv_substr')) {
  68. return true;
  69. }
  70. return false;
  71. }
  72. /**
  73. * check_ticker
  74. *
  75. * Stupid little cutesie thing to ratelimit output of long-running
  76. * operations.
  77. */
  78. public static function check_ticker()
  79. {
  80. if (!isset(self::$_ticker) || (time() > self::$_ticker + 1)) {
  81. self::$_ticker = time();
  82. return true;
  83. }
  84. return false;
  85. }
  86. /**
  87. * clean_utf8
  88. *
  89. * Removes characters that aren't valid in XML (which is a subset of valid
  90. * UTF-8, but close enough for our purposes.)
  91. * See http://www.w3.org/TR/2006/REC-xml-20060816/#charsets
  92. */
  93. public static function clean_utf8($string)
  94. {
  95. if ($string) {
  96. $clean = preg_replace('/[^\x{9}\x{a}\x{d}\x{20}-\x{d7ff}\x{e000}-\x{fffd}\x{10000}-\x{10ffff}]|[\x{7f}-\x{84}\x{86}-\x{9f}\x{fdd0}-\x{fddf}\x{1fffe}-\x{1ffff}\x{2fffe}-\x{2ffff}\x{3fffe}-\x{3ffff}\x{4fffe}-\x{4ffff}\x{5fffe}-\x{5ffff}\x{6fffe}-\x{6ffff}\x{7fffe}-\x{7ffff}\x{8fffe}-\x{8ffff}\x{9fffe}-\x{9ffff}\x{afffe}-\x{affff}\x{bfffe}-\x{bffff}\x{cfffe}-\x{cffff}\x{dfffe}-\x{dffff}\x{efffe}-\x{effff}\x{ffffe}-\x{fffff}\x{10fffe}-\x{10ffff}]/u', '', $string);
  97. // Other cleanup regex. Takes too long to process.
  98. /*$regex = <<<'END'
  99. /
  100. (
  101. (?: [\x00-\x7F] # single-byte sequences 0xxxxxxx
  102. | [\xC0-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
  103. | [\xE0-\xEF][\x80-\xBF]{2} # triple-byte sequences 1110xxxx 10xxxxxx * 2
  104. | [\xF0-\xF7][\x80-\xBF]{3} # quadruple-byte sequence 11110xxx 10xxxxxx * 3
  105. ){1,100} # ...one or more times
  106. )
  107. | . # anything else
  108. /x
  109. END;
  110. $clean = preg_replace($regex, '$1', $string);*/
  111. if ($clean) {
  112. return $clean;
  113. }
  114. debug_event('UI', 'Charset cleanup failed, something might break', 1);
  115. }
  116. }
  117. /**
  118. * flip_class
  119. *
  120. * First initialised with an array of two class names. Subsequent calls
  121. * reverse the array then return the first element.
  122. */
  123. public static function flip_class($classes = null)
  124. {
  125. if (is_array($classes)) {
  126. self::$_classes = $classes;
  127. } else {
  128. self::$_classes = array_reverse(self::$_classes);
  129. }
  130. return self::$_classes[0];
  131. }
  132. /**
  133. * format_bytes
  134. *
  135. * Turns a size in bytes into the best human-readable value
  136. */
  137. public static function format_bytes($value, $precision = 2)
  138. {
  139. $pass = 0;
  140. while (strlen(floor($value)) > 3) {
  141. $value /= 1024;
  142. $pass++;
  143. }
  144. switch ($pass) {
  145. case 1: $unit = 'kB'; break;
  146. case 2: $unit = 'MB'; break;
  147. case 3: $unit = 'GB'; break;
  148. case 4: $unit = 'TB'; break;
  149. case 5: $unit = 'PB'; break;
  150. default: $unit = 'B'; break;
  151. }
  152. return round($value, $precision) . ' ' . $unit;
  153. }
  154. /**
  155. * unformat_bytes
  156. *
  157. * Parses a human-readable size
  158. */
  159. public static function unformat_bytes($value)
  160. {
  161. if (preg_match('/^([0-9]+) *([[:alpha:]]+)$/', $value, $matches)) {
  162. $value = $matches[1];
  163. $unit = strtolower(substr($matches[2], 0, 1));
  164. } else {
  165. return $value;
  166. }
  167. switch ($unit) {
  168. case 'p':
  169. $value *= 1024;
  170. case 't':
  171. $value *= 1024;
  172. case 'g':
  173. $value *= 1024;
  174. case 'm':
  175. $value *= 1024;
  176. case 'k':
  177. $value *= 1024;
  178. }
  179. return $value;
  180. }
  181. /**
  182. * get_icon
  183. *
  184. * Returns an <img> tag for the specified icon
  185. */
  186. public static function get_icon($name, $title = null, $id = null)
  187. {
  188. $bUseSprite = file_exists(AmpConfig::get('prefix') . AmpConfig::get('theme_path') . '/images/icons.sprite.png');
  189. if (is_array($name)) {
  190. $hover_name = $name[1];
  191. $name = $name[0];
  192. }
  193. $title = $title ?: T_(ucfirst($name));
  194. $icon_url = self::_find_icon($name);
  195. if (isset($hover_name)) {
  196. $hover_url = self::_find_icon($hover_name);
  197. }
  198. if ($bUseSprite) {
  199. $tag = '<span class="sprite sprite-icon_'.$name.'"';
  200. } else {
  201. $tag = '<img src="' . $icon_url . '" ';
  202. }
  203. if ($id) {
  204. $tag .= 'id="' . $id . '" ';
  205. }
  206. $tag .= 'alt="' . $title . '" ';
  207. $tag .= 'title="' . $title . '" ';
  208. if (isset($hover_name) && isset($hover_url)) {
  209. $tag .= 'onmouseover="this.src=\'' . $hover_url . '\'; return true;"';
  210. $tag .= 'onmouseout="this.src=\'' . $icon_url . '\'; return true;" ';
  211. }
  212. if ($bUseSprite) {
  213. $tag .= '></span>';
  214. } else {
  215. $tag .= '/>';
  216. }
  217. return $tag;
  218. }
  219. /**
  220. * _find_icon
  221. *
  222. * Does the finding icon thing
  223. */
  224. private static function _find_icon($name)
  225. {
  226. if (isset(self::$_icon_cache[$name]) && $url = self::$_icon_cache[$name]) {
  227. return $url;
  228. }
  229. $filename = 'icon_' . $name . '.png';
  230. $path = AmpConfig::get('theme_path') . '/images/icons/';
  231. if (!file_exists(AmpConfig::get('prefix') . $path . $filename)) {
  232. $path = '/images/';
  233. }
  234. $url = AmpConfig::get('web_path') . $path . $filename;
  235. self::$_icon_cache[$name] = $url;
  236. return $url;
  237. }
  238. /**
  239. * show_header
  240. *
  241. * For now this just shows the header template
  242. */
  243. public static function show_header()
  244. {
  245. require_once AmpConfig::get('prefix') . '/templates/header.inc.php';
  246. }
  247. /**
  248. * show_footer
  249. *
  250. * Shows the footer template and possibly profiling info.
  251. */
  252. public static function show_footer()
  253. {
  254. if (!defined("TABLE_RENDERED")) {
  255. show_table_render();
  256. }
  257. require_once AmpConfig::get('prefix') . '/templates/footer.inc.php';
  258. if (isset($_REQUEST['profiling'])) {
  259. Dba::show_profile();
  260. }
  261. }
  262. /**
  263. * show_box_top
  264. *
  265. * This shows the top of the box.
  266. */
  267. public static function show_box_top($title = '', $class = '')
  268. {
  269. require AmpConfig::get('prefix') . '/templates/show_box_top.inc.php';
  270. }
  271. /**
  272. * show_box_bottom
  273. *
  274. * This shows the bottom of the box
  275. */
  276. public static function show_box_bottom()
  277. {
  278. require AmpConfig::get('prefix') . '/templates/show_box_bottom.inc.php';
  279. }
  280. /**
  281. * update_text
  282. *
  283. * Convenience function that, if the output is going to a browser,
  284. * blarfs JS to do a fancy update. Otherwise it just outputs the text.
  285. */
  286. public static function update_text($field, $value)
  287. {
  288. if (defined('CLI')) {
  289. echo $value . "\n";
  290. return;
  291. }
  292. static $id = 1;
  293. if (defined('SSE_OUTPUT')) {
  294. echo "id: " . $id . "\n";
  295. echo "data: displayNotification('" . addslashes($value) . "', 5000)\n\n";
  296. } else {
  297. if (!empty($field)) {
  298. echo "<script>updateText('" . $field . "', '" . addslashes($value) ."');</script>\n";
  299. } else {
  300. echo "<br />" . $value . "<br /><br />\n";
  301. }
  302. }
  303. ob_flush();
  304. flush();
  305. $id++;
  306. }
  307. public static function get_logo_url()
  308. {
  309. if (AmpConfig::get('custom_logo')) {
  310. return AmpConfig::get('custom_logo');
  311. } else {
  312. return AmpConfig::get('web_path') . AmpConfig::get('theme_path') . '/images/ampache.png';
  313. }
  314. }
  315. }