PageRenderTime 62ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/lib.php

https://github.com/mirabalj/yekuana
PHP | 848 lines | 637 code | 115 blank | 96 comment | 100 complexity | 67cbb418fc8f72981a7fe77a4bf10541 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. global $CFG;
  3. // Set includes dir
  4. $CFG->rootdir = dirname(dirname(__FILE__)) . '/';
  5. $CFG->incdir = $CFG->rootdir . 'includes/';
  6. $CFG->admdir = $CFG->incdir . 'admin/';
  7. $CFG->comdir = $CFG->incdir . 'common/';
  8. $CFG->tpldir = $CFG->rootdir . 'templates/';
  9. // includes
  10. include($CFG->incdir . 'datalib.php');
  11. include($CFG->incdir . 'constants.php');
  12. include($CFG->incdir . 'displaylib.php');
  13. include($CFG->incdir . 'infolib.php');
  14. // l10n/gettext support
  15. include($CFG->incdir . 'php-gettext/streams.php');
  16. include($CFG->incdir . 'php-gettext/gettext.php');
  17. include($CFG->incdir . 'l10n.php');
  18. // run setup script
  19. require($CFG->incdir . 'setup.php');
  20. function set_config($name, $value) {
  21. global $CFG;
  22. $CFG->$name = $value;
  23. if (get_field('datalists', 'name', 'name', $name)) {
  24. return set_field('datalists', 'value', $value, 'name', $name);
  25. } else {
  26. $config = new StdClass;
  27. $config->name = $name;
  28. $config->value = $value;
  29. return insert_record('datalists', $config);
  30. }
  31. }
  32. function get_config($name=NULL) {
  33. global $CFG;
  34. if (!empty($name)) {
  35. return get_record('datalists', 'name', $name);
  36. }
  37. // this was originally in setup.php, duh!
  38. if ($configs = get_records('datalists')) {
  39. $localcfg = (array)$CFG;
  40. foreach ($configs as $config) {
  41. if (empty($localcfg[$config->name])) {
  42. $localcfg[$config->name] = $config->value;
  43. }
  44. }
  45. $localcfg = (object)$localcfg;
  46. return $localcfg;
  47. } else {
  48. // preserve $CFG if db returns nothing or error
  49. return $CFG;
  50. }
  51. }
  52. function get_url($path='') {
  53. global $CFG;
  54. if (empty($CFG->wwwroot)) {
  55. $url = $_SERVER['REQUEST_URI'];
  56. } else {
  57. $url = $CFG->wwwroot;
  58. }
  59. if (!empty($path)) {
  60. // using mod rewrite?
  61. if (empty($CFG->clean_url)) {
  62. $url .= '/?q=' . $path;
  63. } else {
  64. $url .= '/' . $path;
  65. }
  66. }
  67. return $url;
  68. }
  69. function clean_text($text, $format=FORMAT_MOODLE) {
  70. global $ALLOWED_TAGS;
  71. switch ($format) {
  72. case FORMAT_PLAIN:
  73. return $text;
  74. default:
  75. /// Remove tags that are not allowed
  76. // $text = strip_tags($text, $ALLOWED_TAGS);
  77. $text = strip_tags($text);
  78. /// Add some breaks into long strings of &nbsp;
  79. $text = preg_replace('/((&nbsp;){10})&nbsp;/', '\\1 ', $text);
  80. /// Remove script events
  81. $text = eregi_replace("([^a-z])language([[:space:]]*)=", "\\1Xlanguage=", $text);
  82. $text = eregi_replace("([^a-z])on([a-z]+)([[:space:]]*)=", "\\1Xon\\2=", $text);
  83. return $text;
  84. }
  85. }
  86. // returns particular value for the named variable taken from
  87. // POST or GET, otherwise returning a given default.
  88. function optional_param ($varname, $default=NULL, $options=PARAM_CLEAN) {
  89. if (isset($_POST[$varname])) { // POST has precedence
  90. $param = $_POST[$varname];
  91. } else if (isset($_GET[$varname])){
  92. $param = $_GET[$varname];
  93. } else {
  94. return $default;
  95. }
  96. return clean_param($param, $options);
  97. }
  98. // clean the variables and/or cast to specific types, based on
  99. // an options field
  100. function clean_param ($param, $options) {
  101. global $CFG;
  102. if (is_array($param)) { // Let's loop
  103. $newparam = array();
  104. foreach ($param as $key => $value) {
  105. $newparam[$key] = clean_param($value, $options);
  106. }
  107. return $newparam;
  108. }
  109. if (!$options) {
  110. return $param; // Return raw value
  111. }
  112. //this corrupts data - Sven
  113. //if ((string)$param == (string)(int)$param) { // It's just an integer
  114. // return (int)$param;
  115. //}
  116. if ($options & PARAM_CLEAN) {
  117. // this breaks backslashes in user input
  118. // $param = stripslashes($param); // Needed by kses to work fine
  119. $param = clean_text($param); // Sweep for scripts, etc
  120. // and this unnecessarily escapes quotes, etc in user input
  121. // $param = addslashes($param); // Restore original request parameter slashes
  122. }
  123. if ($options & PARAM_INT) {
  124. $param = (int)$param; // Convert to integer
  125. }
  126. if ($options & PARAM_ALPHA) { // Remove everything not a-z
  127. $param = eregi_replace('[^a-zA-Z]', '', $param);
  128. }
  129. if ($options & PARAM_ALPHANUM) { // Remove everything not a-zA-Z0-9
  130. $param = eregi_replace('[^A-Za-z0-9]', '', $param);
  131. }
  132. if ($options & PARAM_ALPHAEXT) { // Remove everything not a-zA-Z/_-
  133. $param = eregi_replace('[^a-zA-Z/_-]', '', $param);
  134. }
  135. if ($options & PARAM_BOOL) { // Convert to 1 or 0
  136. $tempstr = strtolower($param);
  137. if ($tempstr == 'on') {
  138. $param = 1;
  139. } else if ($tempstr == 'off') {
  140. $param = 0;
  141. } else {
  142. $param = empty($param) ? 0 : 1;
  143. }
  144. }
  145. if ($options & PARAM_NOTAGS) { // Strip all tags completely
  146. $param = strip_tags($param);
  147. }
  148. if ($options & PARAM_SAFEDIR) { // Remove everything not a-zA-Z0-9_-
  149. $param = eregi_replace('[^a-zA-Z0-9_-]', '', $param);
  150. }
  151. if ($options & PARAM_FILE) { // Strip all suspicious characters from filename
  152. $param = ereg_replace('[[:cntrl:]]|[<>"`\|\':\\/]', '', $param);
  153. $param = ereg_replace('\.\.+', '', $param);
  154. if($param == '.') {
  155. $param = '';
  156. }
  157. }
  158. if ($options & PARAM_PATH) { // Strip all suspicious characters from file path
  159. $param = str_replace('\\\'', '\'', $param);
  160. $param = str_replace('\\"', '"', $param);
  161. $param = str_replace('\\', '/', $param);
  162. $param = ereg_replace('[[:cntrl:]]|[<>"`\|\':]', '', $param);
  163. $param = ereg_replace('\.\.+', '', $param);
  164. $param = ereg_replace('//+', '/', $param);
  165. $param = ereg_replace('/(\./)+', '/', $param);
  166. }
  167. if ($options & PARAM_HOST) { // allow FQDN or IPv4 dotted quad
  168. preg_replace('/[^\.\d\w-]/','', $param ); // only allowed chars
  169. // match ipv4 dotted quad
  170. if (preg_match('/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/',$param, $match)){
  171. // confirm values are ok
  172. if ( $match[0] > 255
  173. || $match[1] > 255
  174. || $match[3] > 255
  175. || $match[4] > 255 ) {
  176. // hmmm, what kind of dotted quad is this?
  177. $param = '';
  178. }
  179. } elseif ( preg_match('/^[\w\d\.-]+$/', $param) // dots, hyphens, numbers
  180. && !preg_match('/^[\.-]/', $param) // no leading dots/hyphens
  181. && !preg_match('/[\.-]$/', $param) // no trailing dots/hyphens
  182. ) {
  183. // all is ok - $param is respected
  184. } else {
  185. // all is not ok...
  186. $param='';
  187. }
  188. }
  189. if ($options & PARAM_CLEANHTML) {
  190. // $param = stripslashes($param); // Remove any slashes
  191. $param = clean_text($param); // Sweep for scripts, etc
  192. // $param = trim($param); // Sweep for scripts, etc
  193. }
  194. return $param;
  195. }
  196. // send mail
  197. function send_mail($contactname, $contactemail, $subject, $message, $myname='', $mymail='', $bcc='', $replyto='', $replytoname='') {
  198. global $CFG;
  199. global $release;
  200. // needed for compatibility
  201. $CFG->libdir = dirname(__FILE__);
  202. // include mailer library
  203. include_once(dirname(__FILE__).'/phpmailer/class.phpmailer.php');
  204. $mail = new phpmailer;
  205. $mail->Version = 'Yupana ' . $release;
  206. $mail->PluginDir = $CFG->libdir . '/phpmailer/';
  207. $mail->CharSet = 'UTF-8';
  208. if (empty($CFG->smtp)) {
  209. $mail->IsMail();
  210. } else {
  211. $mail->IsSMTP();
  212. $mail->Host = $CFG->smtp;
  213. if (!empty($CFG->smtpuser) && !empty($CFG->smtppass)) {
  214. $mail->SMTPAuth = true;
  215. $mail->Username = $CFG->smtpuser;
  216. $mail->Password = $CFG->smtppass;
  217. }
  218. }
  219. $mail->Sender = $CFG->adminmail;
  220. if (!empty($myname) && !empty($mymail)) {
  221. $mail->From = $mymail;
  222. $mail->FromName = $myname;
  223. } elseif (!empty($myname)) {
  224. $mail->From = $CFG->adminmail;
  225. $mail->FronNAme = $myname;
  226. } else {
  227. $mail->From = $CFG->general_mail;
  228. $mail->FromName = $CFG->conference_name;
  229. }
  230. if (!empty($replyto) && !empty($replytoname)) {
  231. $mail->AddReplyTo($replyto, $replytoname);
  232. }
  233. $mail->Subject = substr(stripslashes($subject),0,900);
  234. $mail->AddAddress($contactemail, $contactname);
  235. $mail->WordWrap = 79;
  236. $mail->IsHTML(false);
  237. $mail->Body = "\n$message\n";
  238. if ($CFG->send_mail == 1) {
  239. if ($mail->Send()) {
  240. return true;
  241. } else {
  242. print_object($mail->ErrorInfo);
  243. return false;
  244. }
  245. }
  246. if ($CFG->debug > 7) {
  247. print_object($mail);
  248. }
  249. }
  250. function request_password($login, $type) {
  251. global $CFG;
  252. if ($type == 'A') {
  253. $user_type = 'person';
  254. $sUserType = __('asistente');
  255. $table = 'asistente';
  256. } elseif ($type == 'P') {
  257. $user_type = 'speaker';
  258. $sUserType = __('ponente');
  259. $table = 'ponente';
  260. } else {
  261. // duh!
  262. return false;
  263. }
  264. $user = get_record($table, 'login', $login);
  265. if (empty($user)) {
  266. return false;
  267. }
  268. $pwreq = new StdClass;
  269. $pwreq->user_id = $user->id;
  270. $pwreq->user_type = $user_type;
  271. $pwreq->code = 'req' . substr(base_convert(md5(time() . $user->login), 16, 24), 0, 30);
  272. if (!insert_record('password_requests', $pwreq)) {
  273. return false;
  274. } else {
  275. $subject = sprintf(__('%s: Cambio de contraseña %s'), $CFG->conference_name, $sUserType);
  276. $url = get_url('recover_password/'.$pwreq->code);
  277. $message = "";
  278. $message .= sprintf(__("Has solicitado cambio de contraseña para el usuario %s\n"), $user->login);
  279. $message .= __("Para confirmarlo ingrese a la siguiente dirección:\n");
  280. $message .= " {$url}\n\n";
  281. $message .= "--";
  282. $message .= "{$CFG->conference_name}\n";
  283. $message .= "{$CFG->conference_link}\n";
  284. }
  285. return send_mail($user->namep.' '.$user->apellidos, $user->mail, $subject, $message);
  286. }
  287. function generatePassword() {
  288. $salt = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  289. srand((double)microtime()*1000000);
  290. $i = 0;
  291. $pass = '';
  292. while ($i < 15) { // change for other length
  293. $num = rand() % 33;
  294. $tmp = substr($salt, $num, 1);
  295. $pass = $pass . $tmp;
  296. $i++;
  297. }
  298. return $pass;
  299. }
  300. function strftime_caste($formato, $fecha){
  301. // strftime por Marcos A. Botta
  302. // $fromato: como se quiere mostrar la fecha
  303. // $fecha: tiemestamp correspondiente a la fecha y hora que se quiere mostrar
  304. $salida = strftime($formato, $fecha);
  305. // reemplazo meses
  306. $salida = ereg_replace("January",__("Enero"),$salida);
  307. $salida = ereg_replace("February",__("Febrero"),$salida);
  308. $salida = ereg_replace("March",__("Marzo"),$salida);
  309. $salida = ereg_replace("April",__("Abril"),$salida);
  310. $salida = ereg_replace("May",__("Mayo"),$salida);
  311. $salida = ereg_replace("June",__("Junio"),$salida);
  312. $salida = ereg_replace("July",__("Julio"),$salida);
  313. $salida = ereg_replace("August",__("Agosto"),$salida);
  314. $salida = ereg_replace("September",__("Septiembre"),$salida);
  315. $salida = ereg_replace("October",__("Octubre"),$salida);
  316. $salida = ereg_replace("November",__("Noviembre"),$salida);
  317. $salida = ereg_replace("December",__("Diciembre"),$salida);
  318. // reemplazo meses cortos
  319. $salida = ereg_replace("Jan",__("Ene"),$salida);
  320. $salida = ereg_replace("Apr",__("Abr"),$salida);
  321. $salida = ereg_replace("Aug",__("Ago"),$salida);
  322. $salida = ereg_replace("Dec",__("Dic"),$salida);
  323. // reemplazo di'as
  324. $salida = ereg_replace("Monday",__("Lunes"),$salida);
  325. $salida = ereg_replace("Tuesday",__("Martes"),$salida);
  326. $salida = ereg_replace("Wednesday",__("Miércoles"),$salida);
  327. $salida = ereg_replace("Thursday",__("Jueves"),$salida);
  328. $salida = ereg_replace("Friday",__("Viernes"),$salida);
  329. $salida = ereg_replace("Saturday",__("Sábado"),$salida);
  330. $salida = ereg_replace("Sunday",__("Domingo"),$salida);
  331. // reemplazo dias cortos
  332. $salida = ereg_replace("Mon",__("Lun"),$salida);
  333. $salida = ereg_replace("Tue",__("Mar"),$salida);
  334. $salida = ereg_replace("Wed",__("Mie"),$salida);
  335. $salida = ereg_replace("Thu",__("Jue"),$salida);
  336. $salida = ereg_replace("Fri",__("Vie"),$salida);
  337. $salida = ereg_replace("Sat",__("Sab"),$salida);
  338. $salida = ereg_replace("Sun",__("Dom"),$salida);
  339. // reemplazo cuando es 1 de algun mes
  340. $salida = ereg_replace(" 01 de "," 1&deg; de ",$salida);
  341. return $salida;
  342. } // fin strftime_caste
  343. function month2name ($id) {
  344. $result = $id;
  345. $id = (int) $id;
  346. switch ($id) {
  347. case 1: $result = __('Enero'); break;
  348. case 2: $result = __('Febrero'); break;
  349. case 3: $result = __('Marzo'); break;
  350. case 4: $result = __('Abril'); break;
  351. case 5: $result = __('Mayo'); break;
  352. case 6: $result = __('Junio'); break;
  353. case 7: $result = __('Julio'); break;
  354. case 8: $result = __('Agosto'); break;
  355. case 9: $result = __('Septiembre'); break;
  356. case 10: $result = __('Octubre'); break;
  357. case 11: $result = __('Noviembre'); break;
  358. case 12: $result = __('Diciembre'); break;
  359. }
  360. return $result;
  361. }
  362. function beginSession($tipo) {
  363. global $CFG;
  364. @session_start(); //ignore errors
  365. session_register("YACOMASVARS");
  366. switch ($tipo)
  367. {
  368. case 'P':
  369. $login='ponlogin';
  370. $id='ponid';
  371. $last='ponlast';
  372. break;
  373. case 'A':
  374. $login='asilogin';
  375. $id='asiid';
  376. $last='asilast';
  377. break;
  378. case 'R':
  379. $login='rootlogin';
  380. $id='rootid';
  381. $last='rootlast';
  382. $level='rootlevel';
  383. break;
  384. }
  385. // Check if $last index exists, if not set to 0
  386. if (!empty($_SESSION['YACOMASVARS'][$last])) {
  387. $last_time = $_SESSION['YACOMASVARS'][$last];
  388. } else {
  389. $last_time = 0;
  390. }
  391. $t_transcurrido = time() - $last_time;
  392. $hora = 3600;
  393. if ($tipo == 'R')
  394. {
  395. if (empty($_SESSION['YACOMASVARS'][$login]) || empty($_SESSION['YACOMASVARS'][$id]) ||
  396. empty($_SESSION['YACOMASVARS'][$level]) ||
  397. ($t_transcurrido > $hora))
  398. { # 1 hour exp.
  399. //fix: in order to get admin login
  400. @session_unset();
  401. @session_destroy();
  402. header('Location: ' . get_url('admin/login'));
  403. exit;
  404. }
  405. }
  406. else
  407. {
  408. if (empty($_SESSION['YACOMASVARS'][$login]) || empty($_SESSION['YACOMASVARS'][$id]) ||
  409. ($t_transcurrido > $hora))
  410. { # 1 hour exp.
  411. header('Location: ' . get_url('logout'));
  412. exit;
  413. }
  414. }
  415. $_SESSION['YACOMASVARS'][$last] = time();
  416. }
  417. //implement user authentification
  418. //must return user object with all attributes
  419. function user_auth($login, $pass, $context) {
  420. global $CFG;
  421. switch ($context) {
  422. case 'admin':
  423. $user = get_admin(null, $login, $pass);
  424. break;
  425. case 'ponente':
  426. $user = get_speaker(null, $login, $pass);
  427. break;
  428. case 'asistente':
  429. $user = get_person(null, $login, $pass);
  430. break;
  431. default:
  432. return null;
  433. }
  434. //check for external auth
  435. //dont use external auth for main admin
  436. if (empty($user) && !empty($CFG->auth) && $login != 'admin') {
  437. $auth_include = $CFG->incdir . 'auth/' . $CFG->auth . '.php';
  438. if (is_readable($auth_include)) {
  439. include($auth_include);
  440. $auth_func = $CFG->auth . '_user_auth';
  441. // if external auth success
  442. // load user info of context
  443. if (function_exists($auth_func)) {
  444. if ($auth_func($login, $pass, $context)) {
  445. //load user info
  446. switch ($context) {
  447. case 'admin':
  448. $user = get_admin(null, $login);
  449. break;
  450. case 'ponente':
  451. $user = get_speaker(null, $login);
  452. break;
  453. case 'asistente':
  454. $user = get_person(null, $login);
  455. break;
  456. }
  457. }
  458. }
  459. }
  460. }
  461. return $user;
  462. }
  463. //return array with available external auth system
  464. function user_auth_available () {
  465. global $CFG;
  466. $auths = array();
  467. $auth_scripts = $CFG->incdir . 'auth/*.php';
  468. foreach (glob($auth_scripts) as $auth) {
  469. preg_match('#auth/(.+)\.php$#', $auth, $matches);
  470. $auths[] = $matches[1];
  471. }
  472. return $auths;
  473. }
  474. //initial gettext code
  475. //compatibility code
  476. //gettext workaround
  477. if (!function_exists('__gettext')) {
  478. function __gettext($s) {
  479. return __($s);
  480. }
  481. }
  482. // required functions for uploadlig.php, taked from elgglib.php
  483. /**
  484. * Converts numbers like 10M into bytes.
  485. *
  486. * @param mixed $size The size to be converted
  487. * @return mixed
  488. */
  489. function get_real_size($size=0) {
  490. if (!$size) {
  491. return 0;
  492. }
  493. $scan['GB'] = 1073741824;
  494. $scan['Gb'] = 1073741824;
  495. $scan['G'] = 1073741824;
  496. $scan['g'] = 1073741824;
  497. $scan['MB'] = 1048576;
  498. $scan['Mb'] = 1048576;
  499. $scan['M'] = 1048576;
  500. $scan['m'] = 1048576;
  501. $scan['KB'] = 1024;
  502. $scan['Kb'] = 1024;
  503. $scan['K'] = 1024;
  504. $scan['k'] = 1024;
  505. while (list($key) = each($scan)) {
  506. if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) {
  507. $size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key];
  508. break;
  509. }
  510. }
  511. return $size;
  512. }
  513. /**
  514. * Returns the maximum size for uploading files.
  515. *
  516. * There are five possible upload limits:
  517. * 1. in Apache using LimitRequestBody (no way of checking or changing this)
  518. * 2. in php.ini for 'upload_max_filesize' (can not be changed inside PHP)
  519. * 3. in .htaccess for 'upload_max_filesize' (can not be changed inside PHP)
  520. * 4. in php.ini for 'post_max_size' (can not be changed inside PHP)
  521. * 5. by the limitations on the current situation (eg file quota)
  522. *
  523. * The last one is passed to this function as an argument (in bytes).
  524. * Anything defined as 0 is ignored.
  525. * The smallest of all the non-zero numbers is returned.
  526. *
  527. * @param int $maxbytes Current maxbytes (in bytes)
  528. * @return int The maximum size for uploading files.
  529. * @todo Finish documenting this function
  530. */
  531. function get_max_upload_file_size($maxbytes=0) {
  532. global $CFG;
  533. if (! $filesize = ini_get('upload_max_filesize')) {
  534. if (!empty($CFG->absmaxuploadsize)) {
  535. $filesize = $CFG->absmaxuploadsize;
  536. } else {
  537. $filesize = '5M';
  538. }
  539. }
  540. $minimumsize = get_real_size($filesize);
  541. if ($postsize = ini_get('post_max_size')) {
  542. $postsize = get_real_size($postsize);
  543. if ($postsize < $minimumsize) {
  544. $minimumsize = $postsize;
  545. }
  546. }
  547. if ($maxbytes and $maxbytes < $minimumsize) {
  548. $minimumsize = $maxbytes;
  549. }
  550. return $minimumsize;
  551. }
  552. /*
  553. * Convert high ascii characters into low ascii
  554. * This code is from http://kalsey.com/2004/07/dirify_in_php/
  555. *
  556. */
  557. function convert_high_ascii($s) {
  558. $HighASCII = array(
  559. "!\xc0!" => 'A', # A`
  560. "!\xe0!" => 'a', # a`
  561. "!\xc1!" => 'A', # A'
  562. "!\xe1!" => 'a', # a'
  563. "!\xc2!" => 'A', # A^
  564. "!\xe2!" => 'a', # a^
  565. "!\xc4!" => 'Ae', # A:
  566. "!\xe4!" => 'ae', # a:
  567. "!\xc3!" => 'A', # A~
  568. "!\xe3!" => 'a', # a~
  569. "!\xc8!" => 'E', # E`
  570. "!\xe8!" => 'e', # e`
  571. "!\xc9!" => 'E', # E'
  572. "!\xe9!" => 'e', # e'
  573. "!\xca!" => 'E', # E^
  574. "!\xea!" => 'e', # e^
  575. "!\xcb!" => 'Ee', # E:
  576. "!\xeb!" => 'ee', # e:
  577. "!\xcc!" => 'I', # I`
  578. "!\xec!" => 'i', # i`
  579. "!\xcd!" => 'I', # I'
  580. "!\xed!" => 'i', # i'
  581. "!\xce!" => 'I', # I^
  582. "!\xee!" => 'i', # i^
  583. "!\xcf!" => 'Ie', # I:
  584. "!\xef!" => 'ie', # i:
  585. "!\xd2!" => 'O', # O`
  586. "!\xf2!" => 'o', # o`
  587. "!\xd3!" => 'O', # O'
  588. "!\xf3!" => 'o', # o'
  589. "!\xd4!" => 'O', # O^
  590. "!\xf4!" => 'o', # o^
  591. "!\xd6!" => 'Oe', # O:
  592. "!\xf6!" => 'oe', # o:
  593. "!\xd5!" => 'O', # O~
  594. "!\xf5!" => 'o', # o~
  595. "!\xd8!" => 'Oe', # O/
  596. "!\xf8!" => 'oe', # o/
  597. "!\xd9!" => 'U', # U`
  598. "!\xf9!" => 'u', # u`
  599. "!\xda!" => 'U', # U'
  600. "!\xfa!" => 'u', # u'
  601. "!\xdb!" => 'U', # U^
  602. "!\xfb!" => 'u', # u^
  603. "!\xdc!" => 'Ue', # U:
  604. "!\xfc!" => 'ue', # u:
  605. "!\xc7!" => 'C', # ,C
  606. "!\xe7!" => 'c', # ,c
  607. "!\xd1!" => 'N', # N~
  608. "!\xf1!" => 'n', # n~
  609. "!\xdf!" => 'ss'
  610. );
  611. $find = array_keys($HighASCII);
  612. $replace = array_values($HighASCII);
  613. $s = preg_replace($find,$replace,$s);
  614. return $s;
  615. }
  616. /*
  617. * Cleans a given filename by removing suspicious or troublesome characters
  618. * Only these are allowed:
  619. * alphanumeric _ - .
  620. *
  621. * @param string $string ?
  622. * @return string
  623. */
  624. function clean_filename($string) {
  625. $string = remove_accents($string);
  626. $string = convert_high_ascii($string);
  627. $string = eregi_replace("\.\.+", '', $string);
  628. $string = preg_replace('/[^\.a-zA-Z\d\_-]/','_', $string ); // only allowed chars
  629. $string = eregi_replace("_+", '_', $string);
  630. return $string;
  631. }
  632. function remove_accents($string) {
  633. $chars = array(
  634. // Decompositions for Latin-1 Supplement
  635. chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
  636. chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
  637. chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
  638. chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
  639. chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
  640. chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
  641. chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
  642. chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
  643. chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
  644. chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
  645. chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
  646. chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
  647. chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
  648. chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
  649. chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
  650. chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
  651. chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
  652. chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
  653. chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
  654. chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
  655. chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
  656. chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
  657. chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
  658. chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
  659. chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
  660. chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
  661. chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
  662. chr(195).chr(191) => 'y',
  663. // Decompositions for Latin Extended-A
  664. chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
  665. chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
  666. chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
  667. chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
  668. chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
  669. chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
  670. chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
  671. chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
  672. chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
  673. chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
  674. chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
  675. chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
  676. chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
  677. chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
  678. chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
  679. chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
  680. chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
  681. chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
  682. chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
  683. chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
  684. chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
  685. chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
  686. chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
  687. chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
  688. chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
  689. chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
  690. chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
  691. chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
  692. chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
  693. chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
  694. chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
  695. chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
  696. chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
  697. chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
  698. chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
  699. chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
  700. chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
  701. chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
  702. chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
  703. chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
  704. chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
  705. chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
  706. chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
  707. chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
  708. chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
  709. chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
  710. chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
  711. chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
  712. chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
  713. chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
  714. chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
  715. chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
  716. chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
  717. chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
  718. chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
  719. chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
  720. chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
  721. chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
  722. chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
  723. chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
  724. chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
  725. chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
  726. chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
  727. chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
  728. // Euro Sign
  729. chr(226).chr(130).chr(172) => 'E');
  730. $string = strtr($string, $chars);
  731. return $string;
  732. }
  733. ?>