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

/lib/general.lib.php

https://gitlab.com/x33n/ampache
PHP | 379 lines | 237 code | 50 blank | 92 comment | 34 complexity | a08d7285f2d81dd4a827930a7e074be2 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 v2
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. *
  21. */
  22. /**
  23. * set_memory_limit
  24. * This function attempts to change the php memory limit using init_set.
  25. * Will never reduce it below the current setting.
  26. */
  27. function set_memory_limit($new_limit)
  28. {
  29. $current_limit = ini_get('memory_limit');
  30. if ($current_limit == -1) {
  31. return;
  32. }
  33. $current_limit = UI::unformat_bytes($current_limit);
  34. $new_limit = UI::unformat_bytes($new_limit);
  35. if ($current_limit < $new_limit) {
  36. ini_set (memory_limit, $new_limit);
  37. }
  38. } // set_memory_limit
  39. /**
  40. * generate_password
  41. * This generates a random password of the specified length
  42. */
  43. function generate_password($length)
  44. {
  45. $vowels = 'aAeEuUyY12345';
  46. $consonants = 'bBdDgGhHjJmMnNpPqQrRsStTvVwWxXzZ6789';
  47. $password = '';
  48. $alt = time() % 2;
  49. for ($i = 0; $i < $length; $i++) {
  50. if ($alt == 1) {
  51. $password .= $consonants[(rand(0,strlen($consonants)-1))];
  52. $alt = 0;
  53. } else {
  54. $password .= $vowels[(rand(0,strlen($vowels)-1))];
  55. $alt = 1;
  56. }
  57. }
  58. return $password;
  59. } // generate_password
  60. /**
  61. * scrub_in
  62. * Run on inputs, stuff that might get stuck in our db
  63. */
  64. function scrub_in($input)
  65. {
  66. if (!is_array($input)) {
  67. return stripslashes(htmlspecialchars(strip_tags($input), ENT_QUOTES, AmpConfig::get('site_charset')));
  68. } else {
  69. $results = array();
  70. foreach ($input as $item) {
  71. $results[] = scrub_in($item);
  72. }
  73. return $results;
  74. }
  75. } // scrub_in
  76. /**
  77. * scrub_out
  78. * This function is used to escape user data that is getting redisplayed
  79. * onto the page, it htmlentities the mojo
  80. * This is the inverse of the scrub_in function
  81. */
  82. function scrub_out($string)
  83. {
  84. return htmlentities($string, ENT_QUOTES, AmpConfig::get('site_charset'));
  85. } // scrub_out
  86. /**
  87. * unhtmlentities
  88. * Undoes htmlentities()
  89. */
  90. function unhtmlentities($string)
  91. {
  92. return html_entity_decode($string, ENT_QUOTES, AmpConfig::get('site_charset'));
  93. } //unhtmlentities
  94. /**
  95. * scrub_arg
  96. *
  97. * This function behaves like escapeshellarg, but isn't broken
  98. */
  99. function scrub_arg($arg)
  100. {
  101. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  102. return '"' . str_replace(array('"', '%'), array('', ''), $arg) . '"';
  103. } else {
  104. return "'" . str_replace("'", "'\\''", $arg) . "'";
  105. }
  106. }
  107. /**
  108. * make_bool
  109. * This takes a value and returns what we consider to be the correct boolean
  110. * value. We need a special function because PHP considers "false" to be true.
  111. */
  112. function make_bool($string)
  113. {
  114. if (strcasecmp($string,'false') == 0 || $string == '0') {
  115. return false;
  116. }
  117. return (bool) $string;
  118. } // make_bool
  119. /**
  120. * invert_bool
  121. * This returns the opposite of what you've got
  122. */
  123. function invert_bool($value)
  124. {
  125. return make_bool($value) ? false : true;
  126. } // invert_bool
  127. /**
  128. * get_languages
  129. * This function does a dir of ./locale and pulls the names of the
  130. * different languages installed, this means that all you have to do
  131. * is drop one in and it will show up on the context menu. It returns
  132. * in the form of an array of names
  133. */
  134. function get_languages()
  135. {
  136. /* Open the locale directory */
  137. $handle = @opendir(AmpConfig::get('prefix') . '/locale');
  138. if (!is_resource($handle)) {
  139. debug_event('language','Error unable to open locale directory','1');
  140. }
  141. $results = array();
  142. while (false !== ($file = readdir($handle))) {
  143. $full_file = AmpConfig::get('prefix') . '/locale/' . $file;
  144. /* Check to see if it's a directory */
  145. if (is_dir($full_file) AND substr($file,0,1) != '.' AND $file != 'base') {
  146. switch ($file) {
  147. case 'af_ZA'; $name = 'Afrikaans'; break; /* Afrikaans */
  148. case 'bg_BG'; $name = '&#x0411;&#x044a;&#x043b;&#x0433;&#x0430;&#x0440;&#x0441;&#x043a;&#x0438;'; break; /* Bulgarian */
  149. case 'ca_ES'; $name = 'Catal&#224;'; break; /* Catalan */
  150. case 'cs_CZ'; $name = '&#x010c;esky'; break; /* Czech */
  151. case 'da_DK'; $name = 'Dansk'; break; /* Danish */
  152. case 'de_DE'; $name = 'Deutsch'; break; /* German */
  153. case 'el_GR'; $name = 'Greek'; break; /* Greek */
  154. case 'en_GB'; $name = 'English (UK)'; break; /* English */
  155. case 'en_US'; $name = 'English (US)'; break; /* English */
  156. case 'es_AR'; $name = 'Espa&#241;ol (AR)'; break; /* Spanish */
  157. case 'es_ES'; $name = 'Espa&#241;ol'; break; /* Spanish */
  158. case 'es_MX'; $name = 'Espa&#241;ol (MX)'; break; /* Spanish */
  159. case 'et_EE'; $name = 'Eesti'; break; /* Estonian */
  160. case 'eu_ES'; $name = 'Euskara'; break; /* Basque */
  161. case 'fi_FI'; $name = 'Suomi'; break; /* Finnish */
  162. case 'fr_FR'; $name = 'Fran&#231;ais'; break; /* French */
  163. case 'ga_IE'; $name = 'Gaeilge'; break; /* Irish */
  164. case 'hu_HU'; $name = 'Magyar'; break; /* Hungarian */
  165. case 'is_IS'; $name = 'Icelandic'; break; /* Icelandic */
  166. case 'it_IT'; $name = 'Italiano'; break; /* Italian */
  167. case 'ja_JP'; $name = '&#x65e5;&#x672c;&#x8a9e;'; break; /* Japanese */
  168. case 'ko_KR'; $name = '&#xd55c;&#xad6d;&#xb9d0;'; break; /* Korean */
  169. case 'lt_LT'; $name = 'Lietuvi&#371;'; break; /* Lithuanian */
  170. case 'lv_LV'; $name = 'Latvie&#353;u'; break; /* Latvian */
  171. case 'nb_NO'; $name = 'Norsk'; break; /* Norwegian */
  172. case 'nl_NL'; $name = 'Nederlands'; break; /* Dutch */
  173. case 'no_NO'; $name = 'Norsk bokm&#229;l'; break; /* Norwegian */
  174. case 'pl_PL'; $name = 'Polski'; break; /* Polish */
  175. case 'pt_BR'; $name = 'Portugu&#234;s Brasileiro'; break; /* Portuguese */
  176. case 'pt_PT'; $name = 'Portugu&#234;s'; break; /* Portuguese */
  177. case 'ro_RO'; $name = 'Rom&#226;n&#259;'; break; /* Romanian */
  178. case 'ru_RU'; $name = '&#1056;&#1091;&#1089;&#1089;&#1082;&#1080;&#1081;'; break; /* Russian */
  179. case 'sk_SK'; $name = 'Sloven&#269;ina'; break; /* Slovak */
  180. case 'sl_SI'; $name = 'Sloven&#353;&#269;ina'; break; /* Slovenian */
  181. case 'sr_CS'; $name = 'Srpski'; break; /* Serbian */
  182. case 'sv_SE'; $name = 'Svenska'; break; /* Swedish */
  183. case 'tr_TR'; $name = 'T&#252;rk&#231;e'; break; /* Turkish */
  184. case 'uk_UA'; $name = 'Українська'; break; /* Ukrainian */
  185. case 'vi_VN'; $name = 'Ti&#7871;ng Vi&#7879;t'; break; /* Vietnamese */
  186. case 'zh_CN'; $name = '&#31616;&#20307;&#20013;&#25991;'; break; /* Chinese */
  187. case 'zn_TW'; $name = '&#32321;&#39636;&#20013;&#25991;'; break; /* Chinese */
  188. /* These languages are right to left. */
  189. case 'ar_SA'; $name = '&#1575;&#1604;&#1593;&#1585;&#1576;&#1610;&#1577;'; break; /* Arabic */
  190. case 'he_IL'; $name = '&#1506;&#1489;&#1512;&#1497;&#1514;'; break; /* Hebrew */
  191. case 'fa_IR'; $name = '&#1601;&#1575;&#1585;&#1587;&#1610;'; break; /* Farsi */
  192. default: $name = T_('Unknown'); break;
  193. } // end switch
  194. $results[$file] = $name;
  195. }
  196. } // end while
  197. // Sort the list of languages by country code
  198. ksort($results);
  199. // Prepend English (US)
  200. $results = array( "en_US" => "English (US)" ) + $results;
  201. return $results;
  202. } // get_languages
  203. /**
  204. * is_rtl
  205. * This checks whether to be a rtl language.
  206. */
  207. function is_rtl($locale)
  208. {
  209. return in_array($locale, array("he_IL", "fa_IR", "ar_SA"));
  210. }
  211. /**
  212. * translate_pattern_code
  213. * This just contains a keyed array which it checks against to give you the
  214. * 'tag' name that said pattern code corrasponds to. It returns false if nothing
  215. * is found.
  216. */
  217. function translate_pattern_code($code)
  218. {
  219. $code_array = array('%A'=>'album',
  220. '%a'=>'artist',
  221. '%c'=>'comment',
  222. '%g'=>'genre',
  223. '%T'=>'track',
  224. '%t'=>'title',
  225. '%y'=>'year',
  226. '%o'=>'zz_other');
  227. if (isset($code_array[$code])) {
  228. return $code_array[$code];
  229. }
  230. return false;
  231. } // translate_pattern_code
  232. /**
  233. * generate_config
  234. *
  235. * This takes an array of results and re-generates the config file
  236. * this is used by the installer and by the admin/system page
  237. */
  238. function generate_config($current)
  239. {
  240. // Start building the new config file
  241. $distfile = AmpConfig::get('prefix') . '/config/ampache.cfg.php.dist';
  242. $handle = fopen($distfile,'r');
  243. $dist = fread($handle,filesize($distfile));
  244. fclose($handle);
  245. $data = explode("\n",$dist);
  246. $final = "";
  247. foreach ($data as $line) {
  248. if (preg_match("/^;?([\w\d]+)\s+=\s+[\"]{1}(.*?)[\"]{1}$/",$line,$matches)
  249. || preg_match("/^;?([\w\d]+)\s+=\s+[\']{1}(.*?)[\']{1}$/", $line, $matches)
  250. || preg_match("/^;?([\w\d]+)\s+=\s+[\'\"]{0}(.*)[\'\"]{0}$/",$line,$matches)) {
  251. $key = $matches[1];
  252. $value = $matches[2];
  253. // Put in the current value
  254. if ($key == 'config_version') {
  255. $line = $key . ' = ' . escape_ini($value);
  256. } elseif (isset($current[$key])) {
  257. $line = $key . ' = "' . escape_ini($current[$key]) . '"';
  258. unset($current[$key]);
  259. }
  260. }
  261. $final .= $line . "\n";
  262. }
  263. return $final;
  264. }
  265. /**
  266. * write_config
  267. *
  268. * Write new configuration into the current configuration file by keeping old values.
  269. */
  270. function write_config($current_file_path)
  271. {
  272. $new_data = generate_config(parse_ini_file($current_file_path));
  273. // Start writing into the current config file
  274. $handle = fopen($current_file_path, 'w+');
  275. fwrite($handle, $new_data, strlen($new_data));
  276. fclose($handle);
  277. }
  278. /**
  279. * escape_ini
  280. *
  281. * Escape a value used for inserting into an ini file.
  282. * Won't quote ', like addslashes does.
  283. */
  284. function escape_ini($str)
  285. {
  286. return str_replace('"', '\"', $str);
  287. }
  288. // Declare apache_request_headers and getallheaders if it don't exists (PHP <= 5.3 + FastCGI)
  289. if (!function_exists('apache_request_headers')) {
  290. function apache_request_headers()
  291. {
  292. $headers = array();
  293. foreach ($_SERVER as $name => $value) {
  294. if (substr($name, 0, 5) == 'HTTP_') {
  295. $name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))));
  296. $headers[$name] = $value;
  297. } else if ($name == "CONTENT_TYPE") {
  298. $headers["Content-Type"] = $value;
  299. } else if ($name == "CONTENT_LENGTH") {
  300. $headers["Content-Length"] = $value;
  301. }
  302. }
  303. return $headers;
  304. }
  305. function getallheaders()
  306. {
  307. return apache_request_headers();
  308. }
  309. }
  310. function get_current_path()
  311. {
  312. if (strlen($_SERVER['PHP_SELF'])) {
  313. $root = $_SERVER['PHP_SELF'];
  314. } else {
  315. $root = $_SERVER['REQUEST_URI'];
  316. }
  317. return $root;
  318. }
  319. function get_web_path()
  320. {
  321. $root = get_current_path();
  322. //$root = rtrim(dirname($root),"/\\");
  323. $root = preg_replace('#(.*)/(\w+\.php)$#', '$1', $root);
  324. return $root;
  325. }