PageRenderTime 143ms CodeModel.GetById 29ms RepoModel.GetById 5ms app.codeStats 0ms

/jappixmini/jappix/php/functions.php

https://github.com/chiefdome/friendica-addons
PHP | 1427 lines | 966 code | 247 blank | 214 comment | 164 complexity | f83a8abbe8b634782265978e8d2dc5e5 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-3.0, GPL-2.0
  1. <?php
  2. /*
  3. Jappix - An open social platform
  4. These are the PHP functions for Jappix
  5. -------------------------------------------------
  6. License: AGPL
  7. Authors: Vanaryon, LinkMauve, Mathieui, olivierm
  8. Last revision: 16/01/12
  9. */
  10. // The function to check if Jappix is already installed
  11. function isInstalled() {
  12. if(!file_exists(JAPPIX_BASE.'/store/conf/installed.xml'))
  13. return false;
  14. return true;
  15. }
  16. // The function to check if a static host is defined
  17. function hasStatic() {
  18. if(HOST_STATIC && (HOST_STATIC != '.'))
  19. return true;
  20. return false;
  21. }
  22. // The function to check if this is a static server
  23. function isStatic() {
  24. if(hasStatic() && (parse_url(HOST_STATIC, PHP_URL_HOST) == $_SERVER['HTTP_HOST']))
  25. return true;
  26. return false;
  27. }
  28. // The function to check if this is an upload server
  29. function isUpload() {
  30. if(HOST_UPLOAD && (parse_url(HOST_UPLOAD, PHP_URL_HOST) == $_SERVER['HTTP_HOST']))
  31. return true;
  32. return false;
  33. }
  34. // The function to get the users.xml file hashed name
  35. function usersConfName() {
  36. $conf_dir = JAPPIX_BASE.'/store/conf';
  37. // No conf folder?
  38. if(!is_dir($conf_dir))
  39. return '';
  40. // Read the conf folder
  41. $conf_scan = scandir($conf_dir.'/');
  42. $conf_name = '';
  43. // Loop the XML files
  44. foreach($conf_scan as $current) {
  45. if(preg_match('/(.+)(\.users\.xml)($)/', $current)) {
  46. $conf_name = $current;
  47. break;
  48. }
  49. }
  50. // Return the users file name
  51. return $conf_name;
  52. }
  53. // The function to write a XML file
  54. function writeXML($type, $xmlns, $xml) {
  55. // Generate the file path
  56. $conf_path = JAPPIX_BASE.'/store/'.$type.'/';
  57. $conf_name = $xmlns.'.xml';
  58. // Secured stored file?
  59. if(($type == 'conf') && ($xmlns == 'users')) {
  60. // Get the secured file name
  61. $conf_secured = usersConfName();
  62. // Does this file exist?
  63. if($conf_secured)
  64. $conf_name = $conf_secured;
  65. else
  66. $conf_name = hash('sha256', rand(1, 99999999).time()).'.users.xml';
  67. }
  68. // Generate the file complete path
  69. $conf_file = $conf_path.$conf_name;
  70. // Write the installed marker
  71. $gen_xml = '<?xml version="1.0" encoding="utf-8" ?>
  72. <jappix xmlns="jappix:'.$type.':'.$xmlns.'">
  73. '.trim($xml).'
  74. </jappix>';
  75. file_put_contents($conf_file, $gen_xml);
  76. return true;
  77. }
  78. // The function to read a XML file
  79. function readXML($type, $xmlns) {
  80. // Generate the file path
  81. $conf_path = JAPPIX_BASE.'/store/'.$type.'/';
  82. $conf_name = $xmlns.'.xml';
  83. // Secured stored file?
  84. if(($type == 'conf') && ($xmlns == 'users')) {
  85. // Get the secured file name
  86. $conf_secured = usersConfName();
  87. // Does this file exist?
  88. if($conf_secured)
  89. $conf_name = $conf_secured;
  90. }
  91. // Generate the file complete path
  92. $conf_file = $conf_path.$conf_name;
  93. if(file_exists($conf_file))
  94. return file_get_contents($conf_file);
  95. return false;
  96. }
  97. // The function to read remote URLs
  98. function read_url($url) {
  99. // Any cURL?
  100. if(function_exists('curl_init')) {
  101. $ch = curl_init();
  102. curl_setopt($ch, CURLOPT_URL, $url);
  103. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  104. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  105. $data = curl_exec($ch);
  106. curl_close($ch);
  107. }
  108. // Default method
  109. else
  110. $data = @file_get_contents($url);
  111. return $data;
  112. }
  113. // The function to get the Jappix app. current version
  114. function getVersion() {
  115. $file = file_get_contents(JAPPIX_BASE.'/VERSION');
  116. $version = trim($file);
  117. return $version;
  118. }
  119. // The function to detect the user's language
  120. function checkLanguage() {
  121. // If the user defined a language
  122. if(isset($_GET['l']) && !empty($_GET['l'])) {
  123. // We define some stuffs
  124. $defined_lang = strtolower($_GET['l']);
  125. $lang_file = JAPPIX_BASE.'/lang/'.$defined_lang.'/LC_MESSAGES/main.mo';
  126. if($defined_lang == 'en')
  127. $lang_found = true;
  128. else
  129. $lang_found = file_exists($lang_file);
  130. // We check if the asked translation exists
  131. if($lang_found) {
  132. $lang = $defined_lang;
  133. // Write a cookie
  134. setcookie('jappix_locale', $lang, (time() + 31536000));
  135. return $lang;
  136. }
  137. }
  138. // No language has been defined, but a cookie is stored
  139. if(isset($_COOKIE['jappix_locale'])) {
  140. $check_cookie = $_COOKIE['jappix_locale'];
  141. // The cookie has a value, check this value
  142. if($check_cookie && (file_exists(JAPPIX_BASE.'/lang/'.$check_cookie.'/LC_MESSAGES/main.mo') || ($check_cookie == 'en')))
  143. return $check_cookie;
  144. }
  145. // No cookie defined (or an unsupported value), naturally, we check the browser language
  146. if(!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
  147. return 'en';
  148. // We get the language of the browser
  149. $nav_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
  150. $check_en = strtolower($nav_langs[0]);
  151. // We check if this is not english
  152. if($check_en == 'en')
  153. return 'en';
  154. $order = array();
  155. foreach($nav_langs as $entry) {
  156. $indice = explode('=', $entry);
  157. $lang = strtolower(substr(trim($indice[0]), 0, 2));
  158. if(!isset($indice[1]) || !$indice[1])
  159. $indice = 1;
  160. else
  161. $indice = $indice[1];
  162. $order[$lang] = $indice;
  163. }
  164. arsort($order);
  165. foreach($order as $nav_lang => $val) {
  166. $lang_found = file_exists(JAPPIX_BASE.'/lang/'.$nav_lang.'/LC_MESSAGES/main.mo');
  167. if($lang_found)
  168. return $nav_lang;
  169. }
  170. // If Jappix doen't know that language, we include the english translation
  171. return 'en';
  172. }
  173. // The function to convert a ISO language code to its full name
  174. function getLanguageName($code) {
  175. $code = strtolower($code);
  176. $known = array(
  177. 'aa' => 'Afaraf',
  178. 'ab' => 'Аҧсуа',
  179. 'ae' => 'Avesta',
  180. 'af' => 'Afrikaans',
  181. 'ak' => 'Akan',
  182. 'am' => 'አማርኛ',
  183. 'an' => 'Aragonés',
  184. 'ar' => 'العربية',
  185. 'as' => 'অসমীয়া',
  186. 'av' => 'авар мацӀ',
  187. 'ay' => 'Aymar aru',
  188. 'az' => 'Azərbaycan dili',
  189. 'ba' => 'башҡорт теле',
  190. 'be' => 'Беларуская',
  191. 'bg' => 'български',
  192. 'bh' => 'भोजपुरी',
  193. 'bi' => 'Bislama',
  194. 'bm' => 'Bamanankan',
  195. 'bn' => 'বাংলা',
  196. 'bo' => 'བོད་ཡིག',
  197. 'br' => 'Brezhoneg',
  198. 'bs' => 'Bosanski jezik',
  199. 'ca' => 'Català',
  200. 'ce' => 'нохчийн мотт',
  201. 'ch' => 'Chamoru',
  202. 'co' => 'Corsu',
  203. 'cr' => 'ᓀᐦᐃᔭᐍᐏᐣ',
  204. 'cs' => 'Česky',
  205. 'cu' => 'Словѣньскъ',
  206. 'cv' => 'чӑваш чӗлхи',
  207. 'cy' => 'Cymraeg',
  208. 'da' => 'Dansk',
  209. 'de' => 'Deutsch',
  210. 'dv' => 'ދިވެހި',
  211. 'dz' => 'རྫོང་ཁ',
  212. 'ee' => 'Ɛʋɛgbɛ',
  213. 'el' => 'Ελληνικά',
  214. 'en' => 'English',
  215. 'eo' => 'Esperanto',
  216. 'es' => 'Español',
  217. 'et' => 'Eesti keel',
  218. 'eu' => 'Euskara',
  219. 'fa' => 'فارسی',
  220. 'ff' => 'Fulfulde',
  221. 'fi' => 'Suomen kieli',
  222. 'fj' => 'Vosa Vakaviti',
  223. 'fo' => 'Føroyskt',
  224. 'fr' => 'Français',
  225. 'fy' => 'Frysk',
  226. 'ga' => 'Gaeilge',
  227. 'gd' => 'Gàidhlig',
  228. 'gl' => 'Galego',
  229. 'gn' => 'Avañe\'ẽ',
  230. 'gu' => 'ગુજરાતી',
  231. 'gv' => 'Ghaelg',
  232. 'ha' => 'هَوُسَ',
  233. 'he' => 'עברית',
  234. 'hi' => 'हिन्दी',
  235. 'ho' => 'Hiri Motu',
  236. 'hr' => 'Hrvatski',
  237. 'ht' => 'Kreyòl ayisyen',
  238. 'hu' => 'Magyar',
  239. 'hy' => 'Հայերեն',
  240. 'hz' => 'Otjiherero',
  241. 'ia' => 'Interlingua',
  242. 'id' => 'Bahasa',
  243. 'ie' => 'Interlingue',
  244. 'ig' => 'Igbo',
  245. 'ii' => 'ꆇꉙ',
  246. 'ik' => 'Iñupiaq',
  247. 'io' => 'Ido',
  248. 'is' => 'Íslenska',
  249. 'it' => 'Italiano',
  250. 'iu' => 'ᐃᓄᒃᑎᑐᑦ',
  251. 'ja' => '日本語',
  252. 'jv' => 'Basa Jawa',
  253. 'ka' => 'ქართული',
  254. 'kg' => 'KiKongo',
  255. 'ki' => 'Gĩkũyũ',
  256. 'kj' => 'Kuanyama',
  257. 'kk' => 'Қазақ тілі',
  258. 'kl' => 'Kalaallisut',
  259. 'km' => 'ភាសាខ្មែរ',
  260. 'kn' => 'ಕನ್ನಡ',
  261. 'ko' => '한 국어',
  262. 'kr' => 'Kanuri',
  263. 'ks' => 'कश्मीरी',
  264. 'ku' => 'Kurdî',
  265. 'kv' => 'коми кыв',
  266. 'kw' => 'Kernewek',
  267. 'ky' => 'кыргыз тили',
  268. 'la' => 'Latine',
  269. 'lb' => 'Lëtzebuergesch',
  270. 'lg' => 'Luganda',
  271. 'li' => 'Limburgs',
  272. 'ln' => 'Lingála',
  273. 'lo' => 'ພາສາລາວ',
  274. 'lt' => 'Lietuvių kalba',
  275. 'lu' => 'cilubà',
  276. 'lv' => 'Latviešu valoda',
  277. 'mg' => 'Fiteny malagasy',
  278. 'mh' => 'Kajin M̧ajeļ',
  279. 'mi' => 'Te reo Māori',
  280. 'mk' => 'македонски јазик',
  281. 'ml' => 'മലയാളം',
  282. 'mn' => 'Монгол',
  283. 'mo' => 'лимба молдовеняскэ',
  284. 'mr' => 'मराठी',
  285. 'ms' => 'Bahasa Melayu',
  286. 'mt' => 'Malti',
  287. 'my' => 'ဗမာစာ',
  288. 'na' => 'Ekakairũ Naoero',
  289. 'nb' => 'Norsk bokmål',
  290. 'nd' => 'isiNdebele',
  291. 'ne' => 'नेपाली',
  292. 'ng' => 'Owambo',
  293. 'nl' => 'Nederlands',
  294. 'nn' => 'Norsk nynorsk',
  295. 'no' => 'Norsk',
  296. 'nr' => 'Ndébélé',
  297. 'nv' => 'Diné bizaad',
  298. 'ny' => 'ChiCheŵa',
  299. 'oc' => 'Occitan',
  300. 'oj' => 'ᐊᓂᔑᓈᐯᒧᐎᓐ',
  301. 'om' => 'Afaan Oromoo',
  302. 'or' => 'ଓଡ଼ିଆ',
  303. 'os' => 'Ирон æвзаг',
  304. 'pa' => 'ਪੰਜਾਬੀ',
  305. 'pi' => 'पािऴ',
  306. 'pl' => 'Polski',
  307. 'ps' => 'پښتو',
  308. 'pt' => 'Português',
  309. 'pt-br' => 'Brasileiro',
  310. 'qu' => 'Runa Simi',
  311. 'rm' => 'Rumantsch grischun',
  312. 'rn' => 'kiRundi',
  313. 'ro' => 'Română',
  314. 'ru' => 'Русский',
  315. 'rw' => 'Kinyarwanda',
  316. 'sa' => 'संस्कृतम्',
  317. 'sc' => 'sardu',
  318. 'sd' => 'सिन्धी',
  319. 'se' => 'Davvisámegiella',
  320. 'sg' => 'Yângâ tî sängö',
  321. 'sh' => 'Српскохрватски',
  322. 'si' => 'සිංහල',
  323. 'sk' => 'Slovenčina',
  324. 'sl' => 'Slovenščina',
  325. 'sm' => 'Gagana fa\'a Samoa',
  326. 'sn' => 'chiShona',
  327. 'so' => 'Soomaaliga',
  328. 'sq' => 'Shqip',
  329. 'sr' => 'српски језик',
  330. 'ss' => 'SiSwati',
  331. 'st' => 'seSotho',
  332. 'su' => 'Basa Sunda',
  333. 'sv' => 'Svenska',
  334. 'sw' => 'Kiswahili',
  335. 'ta' => 'தமிழ்',
  336. 'te' => 'తెలుగు',
  337. 'tg' => 'тоҷикӣ',
  338. 'th' => 'ไทย',
  339. 'ti' => 'ትግርኛ',
  340. 'tk' => 'Türkmen',
  341. 'tl' => 'Tagalog',
  342. 'tn' => 'seTswana',
  343. 'to' => 'faka Tonga',
  344. 'tr' => 'Türkçe',
  345. 'ts' => 'xiTsonga',
  346. 'tt' => 'татарча',
  347. 'tw' => 'Twi',
  348. 'ty' => 'Reo Mā`ohi',
  349. 'ug' => 'Uyƣurqə',
  350. 'uk' => 'українська',
  351. 'ur' => 'اردو',
  352. 'uz' => 'O\'zbek',
  353. 've' => 'tshiVenḓa',
  354. 'vi' => 'Tiếng Việt',
  355. 'vo' => 'Volapük',
  356. 'wa' => 'Walon',
  357. 'wo' => 'Wollof',
  358. 'xh' => 'isiXhosa',
  359. 'yi' => 'ייִדיש',
  360. 'yo' => 'Yorùbá',
  361. 'za' => 'Saɯ cueŋƅ',
  362. 'zh' => '中文',
  363. 'zu' => 'isiZulu'
  364. );
  365. if(isset($known[$code]))
  366. return $known[$code];
  367. return null;
  368. }
  369. // The function to know if a language is right-to-left
  370. function isRTL($code) {
  371. switch($code) {
  372. // RTL language
  373. case 'ar':
  374. case 'he':
  375. case 'dv':
  376. case 'ur':
  377. $is_rtl = true;
  378. break;
  379. // LTR language
  380. default:
  381. $is_rtl = false;
  382. break;
  383. }
  384. return $is_rtl;
  385. }
  386. // The function to set the good localized <html /> tag
  387. function htmlTag($locale) {
  388. // Initialize the tag
  389. $html = '<html xml:lang="'.$locale.'" lang="'.$locale.'" dir="';
  390. // Set the good text direction (TODO)
  391. /* if(isRTL($locale))
  392. $html .= 'rtl';
  393. else
  394. $html .= 'ltr'; */
  395. $html .= 'ltr';
  396. // Close the tag
  397. $html .= '">';
  398. echo($html);
  399. }
  400. // The function which generates the available locales list
  401. function availableLocales($active_locale) {
  402. // Initialize
  403. $scan = scandir(JAPPIX_BASE.'/lang/');
  404. $list = array();
  405. // Loop the available languages
  406. foreach($scan as $current_id) {
  407. // Get the current language name
  408. $current_name = getLanguageName($current_id);
  409. // Not valid?
  410. if(($current_id == $active_locale) || ($current_name == null))
  411. continue;
  412. // Add this to the list
  413. $list[$current_id] = $current_name;
  414. }
  415. return $list;
  416. }
  417. // The function which generates the language switcher hidden part
  418. function languageSwitcher($active_locale) {
  419. // Initialize
  420. $keep_get = keepGet('l', false);
  421. $list = availableLocales($active_locale);
  422. $html = '';
  423. // Generate the HTML code
  424. foreach($list as $current_id => $current_name)
  425. $html .= '<a href="./?l='.$current_id.$keep_get.'">'.htmlspecialchars($current_name).'</a>, ';
  426. // Output the HTML code
  427. return $html;
  428. }
  429. // The function to generate a strong hash
  430. function genStrongHash($string) {
  431. // Initialize
  432. $i = 0;
  433. // Loop to generate a incredibly strong hash (can be a bit slow)
  434. while($i < 10) {
  435. $string = hash('sha256', $string);
  436. $i++;
  437. }
  438. return $string;
  439. }
  440. // The function to generate the version hash
  441. function genHash($version) {
  442. // Get the configuration files path
  443. $conf_path = JAPPIX_BASE.'/store/conf/';
  444. $conf_main = $conf_path.'main.xml';
  445. $conf_hosts = $conf_path.'hosts.xml';
  446. $conf_background = $conf_path.'background.xml';
  447. $logos_dir = JAPPIX_BASE.'/store/logos/';
  448. // Get the hash of the main configuration file
  449. if(file_exists($conf_main))
  450. $hash_main = md5_file($conf_main);
  451. else
  452. $hash_main = '0';
  453. // Get the hash of the main configuration file
  454. if(file_exists($conf_hosts))
  455. $hash_hosts = md5_file($conf_hosts);
  456. else
  457. $hash_hosts = '0';
  458. // Get the hash of the background configuration file
  459. if(file_exists($conf_background))
  460. $hash_background = md5_file($conf_background);
  461. else
  462. $hash_background = '0';
  463. // Get the hash of the logos folder
  464. $hash_logos = '';
  465. if(is_dir($logos_dir)) {
  466. $logos_scan = scandir($logos_dir.'/');
  467. foreach($logos_scan as $logos_current) {
  468. if(getFileExt($logos_current) == 'png')
  469. $hash_logos .= md5_file($logos_dir.$logos_current);
  470. }
  471. }
  472. return md5($version.$hash_main.$hash_hosts.$hash_background.$hash_logos);
  473. }
  474. // The function to hide the error messages
  475. function hideErrors() {
  476. // Hide errors if not developer
  477. if(!isDeveloper()) {
  478. ini_set('display_errors', 'off');
  479. ini_set('error_reporting', 0);
  480. }
  481. // Developers need to get error reports!
  482. else {
  483. ini_set('display_errors', 'on');
  484. ini_set('error_reporting', E_ALL);
  485. }
  486. }
  487. // The function to check BOSH proxy is enabled
  488. function BOSHProxy() {
  489. if(BOSH_PROXY == 'on')
  490. return true;
  491. return false;
  492. }
  493. // The function to check compression is enabled
  494. function hasCompression() {
  495. if(COMPRESSION != 'off')
  496. return true;
  497. return false;
  498. }
  499. // The function to check compression is available with the current client
  500. function canCompress() {
  501. // Compression allowed by admin & browser?
  502. if(hasCompression() && (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')))
  503. return true;
  504. return false;
  505. }
  506. // The function to check whether to show manager link or not
  507. function showManagerLink() {
  508. if(MANAGER_LINK != 'off')
  509. return true;
  510. return false;
  511. }
  512. // The function to check HTTPS storage is allowed
  513. function httpsStorage() {
  514. if(HTTPS_STORAGE == 'on')
  515. return true;
  516. return false;
  517. }
  518. // The function to check HTTPS storage must be forced
  519. function httpsForce() {
  520. if((HTTPS_FORCE == 'on') && sslCheck())
  521. return true;
  522. return false;
  523. }
  524. // The function to check we use HTTPS
  525. function useHttps() {
  526. if(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on'))
  527. return true;
  528. return false;
  529. }
  530. // The function to compress the output pages
  531. function compressThis() {
  532. if(canCompress() && !isDeveloper())
  533. ob_start('ob_gzhandler');
  534. }
  535. // The function to choose one file get with get.php or a liste of resources
  536. function multiFiles() {
  537. if(MULTI_FILES == 'on')
  538. return true;
  539. return false;
  540. }
  541. function getFiles($h, $l, $t, $g, $f) {
  542. // Define the good path to the Get API
  543. if(hasStatic())
  544. $path_to = HOST_STATIC.'/';
  545. else
  546. $path_to = JAPPIX_BASE.'/';
  547. if(!multiFiles()) {
  548. $values = array();
  549. if ($h)
  550. $values[] = 'h='.$h;
  551. if ($l)
  552. $values[] = 'l='.$l;
  553. if ($t)
  554. $values[] = 't='.$t;
  555. if ($g)
  556. $values[] = 'g='.$g;
  557. if ($f)
  558. $values[] = 'f='.$f;
  559. return $path_to.'php/get.php?'.implode('&amp;', $values);
  560. }
  561. if($g && !empty($g) && preg_match('/^(\S+)\.xml$/', $g) && preg_match('/^(css|js)$/', $t) && isSafe($g) && file_exists('xml/'.$g)) {
  562. $xml_data = file_get_contents('xml/'.$g);
  563. // Any data?
  564. if($xml_data) {
  565. $xml_read = new SimpleXMLElement($xml_data);
  566. $xml_parse = $xml_read->$t;
  567. // Files were added to the list before (with file var)?
  568. if($f)
  569. $f .= '~'.$xml_parse;
  570. else
  571. $f = $xml_parse;
  572. }
  573. }
  574. // Explode the f string
  575. if(strpos($f, '~') != false)
  576. $array = explode('~', $f);
  577. else
  578. $array = array($f);
  579. $a = array();
  580. foreach($array as $file)
  581. $a[] = $path_to.$t.'/'.$file;
  582. if (count($a) == 1)
  583. return $a[0];
  584. return $a;
  585. }
  586. function echoGetFiles($h, $l, $t, $g, $f) {
  587. if ($t == 'css')
  588. $pattern = '<link rel="stylesheet" href="%s" type="text/css" media="all" />';
  589. else if ($t == 'js')
  590. $pattern = '<script type="text/javascript" src="%s"></script>';
  591. $files = getFiles($h, $l, $t, $g, $f);
  592. if (is_string($files))
  593. printf($pattern, $files);
  594. else {
  595. $c = count($files)-1;
  596. for($i=0; $i<=$c; $i++) {
  597. if ($i)
  598. echo ' ';
  599. printf($pattern, $files[$i]);
  600. if ($i != $c)
  601. echo "\n";
  602. }
  603. }
  604. }
  605. // The function to check if anonymous mode is authorized
  606. function anonymousMode() {
  607. if(isset($_GET['r']) && !empty($_GET['r']) && HOST_ANONYMOUS && (ANONYMOUS == 'on'))
  608. return true;
  609. else
  610. return false;
  611. }
  612. // The function to quickly translate a string
  613. function _e($string) {
  614. echo T_gettext($string);
  615. }
  616. // The function to check the encrypted mode
  617. function sslCheck() {
  618. if(ENCRYPTION == 'on')
  619. return true;
  620. else
  621. return false;
  622. }
  623. // The function to return the encrypted link
  624. function sslLink() {
  625. // Using HTTPS?
  626. if(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on'))
  627. $link = '<a class="home-images unencrypted" href="http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'">'.T_('Unencrypted').'</a>';
  628. // Using HTTP?
  629. else
  630. $link = '<a class="home-images encrypted" href="https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'">'.T_('Encrypted').'</a>';
  631. return $link;
  632. }
  633. // The function to get the Jappix static URL
  634. function staticURL() {
  635. // Check for HTTPS
  636. $protocol = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
  637. // Full URL
  638. $url = $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  639. return $url;
  640. }
  641. // The function to get the Jappix location (only from Get API!)
  642. function staticLocation() {
  643. // Filter the URL
  644. return preg_replace('/((.+)\/)php\/get\.php(\S)+$/', '$1', staticURL());
  645. }
  646. // The function to include a translation file
  647. function includeTranslation($locale, $domain) {
  648. T_setlocale(LC_MESSAGES, $locale);
  649. T_bindtextdomain($domain, JAPPIX_BASE.'/lang');
  650. T_bind_textdomain_codeset($domain, 'UTF-8');
  651. T_textdomain($domain);
  652. }
  653. // The function to check the cache presence
  654. function hasCache($hash) {
  655. if(file_exists(JAPPIX_BASE.'/store/cache/'.$hash.'.cache'))
  656. return true;
  657. else
  658. return false;
  659. }
  660. // The function to check if developer mode is enabled
  661. function isDeveloper() {
  662. if(DEVELOPER == 'on')
  663. return true;
  664. else
  665. return false;
  666. }
  667. // The function to get a file extension
  668. function getFileExt($name) {
  669. return strtolower(preg_replace('/^(.+)(\.)([^\.]+)$/i', '$3', $name));
  670. }
  671. // The function to get a file type
  672. function getFileType($ext) {
  673. switch($ext) {
  674. // Images
  675. case 'jpg':
  676. case 'jpeg':
  677. case 'png':
  678. case 'bmp':
  679. case 'gif':
  680. case 'tif':
  681. case 'svg':
  682. case 'psp':
  683. case 'xcf':
  684. $file_type = 'image';
  685. break;
  686. // Videos
  687. case 'ogv':
  688. case 'mkv':
  689. case 'avi':
  690. case 'mov':
  691. case 'mp4':
  692. case 'm4v':
  693. case 'wmv':
  694. case 'asf':
  695. case 'mpg':
  696. case 'mpeg':
  697. case 'ogm':
  698. case 'rmvb':
  699. case 'rmv':
  700. case 'qt':
  701. case 'flv':
  702. case 'ram':
  703. case '3gp':
  704. case 'avc':
  705. $file_type = 'video';
  706. break;
  707. // Sounds
  708. case 'oga':
  709. case 'ogg':
  710. case 'mka':
  711. case 'flac':
  712. case 'mp3':
  713. case 'wav':
  714. case 'm4a':
  715. case 'wma':
  716. case 'rmab':
  717. case 'rma':
  718. case 'bwf':
  719. case 'aiff':
  720. case 'caf':
  721. case 'cda':
  722. case 'atrac':
  723. case 'vqf':
  724. case 'au':
  725. case 'aac':
  726. case 'm3u':
  727. case 'mid':
  728. case 'mp2':
  729. case 'snd':
  730. case 'voc':
  731. $file_type = 'audio';
  732. break;
  733. // Documents
  734. case 'pdf':
  735. case 'odt':
  736. case 'ott':
  737. case 'sxw':
  738. case 'stw':
  739. case 'ots':
  740. case 'sxc':
  741. case 'stc':
  742. case 'sxi':
  743. case 'sti':
  744. case 'pot':
  745. case 'odp':
  746. case 'ods':
  747. case 'doc':
  748. case 'docx':
  749. case 'docm':
  750. case 'xls':
  751. case 'xlsx':
  752. case 'xlsm':
  753. case 'xlt':
  754. case 'ppt':
  755. case 'pptx':
  756. case 'pptm':
  757. case 'pps':
  758. case 'odg':
  759. case 'otp':
  760. case 'sxd':
  761. case 'std':
  762. case 'std':
  763. case 'rtf':
  764. case 'txt':
  765. case 'htm':
  766. case 'html':
  767. case 'shtml':
  768. case 'dhtml':
  769. case 'mshtml':
  770. $file_type = 'document';
  771. break;
  772. // Packages
  773. case 'tgz':
  774. case 'gz':
  775. case 'tar':
  776. case 'ar':
  777. case 'cbz':
  778. case 'jar':
  779. case 'tar.7z':
  780. case 'tar.bz2':
  781. case 'tar.gz':
  782. case 'tar.lzma':
  783. case 'tar.xz':
  784. case 'zip':
  785. case 'xz':
  786. case 'rar':
  787. case 'bz':
  788. case 'deb':
  789. case 'rpm':
  790. case '7z':
  791. case 'ace':
  792. case 'cab':
  793. case 'arj':
  794. case 'msi':
  795. $file_type = 'package';
  796. break;
  797. // Others
  798. default:
  799. $file_type = 'other';
  800. break;
  801. }
  802. return $file_type;
  803. }
  804. // The function to get the MIME type of a file
  805. function getFileMIME($path) {
  806. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  807. $cmime = finfo_file($finfo, $path);
  808. finfo_close($finfo);
  809. return $cmime;
  810. }
  811. // The function to keep the current GET vars
  812. function keepGet($current, $no_get) {
  813. // Get the HTTP GET vars
  814. $request = $_SERVER['REQUEST_URI'];
  815. if(strrpos($request, '?') === false)
  816. $get = '';
  817. else {
  818. $uri = explode('?', $request);
  819. $get = $uri[1];
  820. }
  821. // Remove the items we don't want here
  822. $proper = str_replace('&', '&amp;', $get);
  823. $proper = preg_replace('/((^)|(&amp;))(('.$current.'=)([^&]+))/i', '', $proper);
  824. // Nothing at the end?
  825. if(!$proper)
  826. return '';
  827. // We have no defined GET var
  828. if($no_get) {
  829. // Remove the first "&" if it appears
  830. if(preg_match('/^(&(amp;)?)/i', $proper))
  831. $proper = preg_replace('/^(&(amp;)?)/i', '', $proper);
  832. // Add the first "?"
  833. $proper = '?'.$proper;
  834. }
  835. // Add a first "&" if there is no one and no defined GET var
  836. else if(!$no_get && (substr($proper, 0, 1) != '&') && (substr($proper, 0, 5) != '&amp;'))
  837. $proper = '&amp;'.$proper;
  838. return $proper;
  839. }
  840. // Escapes regex special characters for in-regex usage
  841. function escapeRegex($string) {
  842. return preg_replace('/[-[\]{}()*+?.,\\^$|#]/', '\\$&', $string);
  843. }
  844. // Generates the security HTML code
  845. function securityHTML() {
  846. return '<!DOCTYPE html>
  847. <html xmlns="http://www.w3.org/1999/xhtml">
  848. <head>
  849. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  850. <title>Jappix - Forbidden</title>
  851. </head>
  852. <body>
  853. <h1>Forbidden</h1>
  854. <h4>This is a private folder</h4>
  855. </body>
  856. </html>';
  857. }
  858. // Checks if a relative server path is safe
  859. function isSafe($path) {
  860. // Mhh, someone is about to nasty stuffs (previous folder, or executable scripts)
  861. if(preg_match('/\.\.\//', $path) || preg_match('/index\.html?$/', $path) || preg_match('/(\.)((php([0-9]+)?)|(aspx?)|(cgi)|(rb)|(py)|(pl)|(jsp)|(ssjs)|(lasso)|(dna)|(tpl)|(smx)|(cfm))$/i', $path))
  862. return false;
  863. return true;
  864. }
  865. // Set the good unity for a size in bytes
  866. function formatBytes($bytes, $precision = 2) {
  867. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  868. $bytes = max($bytes, 0);
  869. $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
  870. $pow = min($pow, count($units) - 1);
  871. $bytes /= pow(1024, $pow);
  872. return round($bytes, $precision) . ' ' . $units[$pow];
  873. }
  874. // Converts a human-readable bytes value to a computer one
  875. function humanToBytes($string) {
  876. // Values array
  877. $values = array(
  878. 'K' => '000',
  879. 'M' => '000000',
  880. 'G' => '000000000',
  881. 'T' => '000000000000',
  882. 'P' => '000000000000000',
  883. 'E' => '000000000000000000',
  884. 'Z' => '000000000000000000000',
  885. 'Y' => '000000000000000000000000'
  886. );
  887. // Filter the string
  888. foreach($values as $key => $zero)
  889. $string = str_replace($key, $zero, $string);
  890. // Converts the string into an integer
  891. $string = intval($string);
  892. return $string;
  893. }
  894. // Get the maximum file upload size
  895. function uploadMaxSize() {
  896. // Not allowed to upload files?
  897. if(ini_get('file_uploads') != 1)
  898. return 0;
  899. // Upload maximum file size
  900. $upload = humanToBytes(ini_get('upload_max_filesize'));
  901. // POST maximum size
  902. $post = humanToBytes(ini_get('post_max_size'));
  903. // Return the lowest value
  904. if($upload <= $post)
  905. return $upload;
  906. return $post;
  907. }
  908. // Normalizes special chars
  909. function normalizeChars($string) {
  910. $table = array(
  911. 'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
  912. 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
  913. 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
  914. 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
  915. 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
  916. 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
  917. 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
  918. 'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r'
  919. );
  920. return strtr($string, $table);
  921. }
  922. // Filters the XML special chars for the SVG drawer
  923. function filterSpecialXML($string) {
  924. // Strange thing: when $string = 'Mises à jour' -> bug! but 'Mise à jour' -> ok!
  925. $string = normalizeChars($string);
  926. // Encodes with HTML special chars
  927. $string = htmlspecialchars($string);
  928. return $string;
  929. }
  930. // Writes the current visit in the total file
  931. function writeTotalVisit() {
  932. // Get the current time stamp
  933. $stamp = time();
  934. // Initialize the defaults
  935. $array = array(
  936. 'total' => 0,
  937. 'stamp' => $stamp
  938. );
  939. // Try to read the saved data
  940. $total_data = readXML('access', 'total');
  941. // Get the XML file values
  942. if($total_data) {
  943. // Initialize the visits reading
  944. $read_xml = new SimpleXMLElement($total_data);
  945. // Loop the visit elements
  946. foreach($read_xml->children() as $current_child)
  947. $array[$current_child->getName()] = intval($current_child);
  948. }
  949. // Increment the total number of visits
  950. $array['total']++;
  951. // Generate the new XML data
  952. $total_xml =
  953. '<total>'.$array['total'].'</total>
  954. <stamp>'.$array['stamp'].'</stamp>'
  955. ;
  956. // Re-write the new values
  957. writeXML('access', 'total', $total_xml);
  958. }
  959. // Writes the current visit in the months file
  960. function writeMonthsVisit() {
  961. // Get the current month
  962. $month = intval(date('m'));
  963. // Define the stats array
  964. $array = array();
  965. // January to August period
  966. if($month <= 8) {
  967. for($i = 1; $i <= 8; $i++)
  968. $array['month_'.$i] = 0;
  969. }
  970. // August to September period
  971. else {
  972. $i = 8;
  973. $j = 1;
  974. while($j <= 3) {
  975. // Last year months
  976. if(($i >= 8) && ($i <= 12))
  977. $array['month_'.$i++] = 0;
  978. // First year months
  979. else
  980. $array['month_'.$j++] = 0;
  981. }
  982. }
  983. // Try to read the saved data
  984. $months_data = readXML('access', 'months');
  985. // Get the XML file values
  986. if($months_data) {
  987. // Initialize the visits reading
  988. $read_xml = new SimpleXMLElement($months_data);
  989. // Loop the visit elements
  990. foreach($read_xml->children() as $current_child) {
  991. $current_month = $current_child->getName();
  992. // Parse the current month id
  993. $current_id = intval(preg_replace('/month_([0-9]+)/i', '$1', $current_month));
  994. // Is this month still valid?
  995. if((($month <= 8) && ($current_id <= $month)) || (($month >= 8) && ($current_id >= 8) && ($current_id <= $month)))
  996. $array[$current_month] = intval($current_child);
  997. }
  998. }
  999. // Increment the current month value
  1000. $array['month_'.$month]++;
  1001. // Generate the new XML data
  1002. $months_xml = '';
  1003. foreach($array as $array_key => $array_value)
  1004. $months_xml .= "\n".' <'.$array_key.'>'.$array_value.'</'.$array_key.'>';
  1005. // Re-write the new values
  1006. writeXML('access', 'months', $months_xml);
  1007. }
  1008. // Writes the current visit to the storage file
  1009. function writeVisit() {
  1010. // Write total visits
  1011. writeTotalVisit();
  1012. // Write months visits
  1013. writeMonthsVisit();
  1014. }
  1015. // Returns the default background array
  1016. function defaultBackground() {
  1017. // Define the default values
  1018. $background_default = array(
  1019. 'type' => 'default',
  1020. 'image_file' => '',
  1021. 'image_repeat' => 'repeat-x',
  1022. 'image_horizontal' => 'center',
  1023. 'image_vertical' => 'top',
  1024. 'image_adapt' => 'off',
  1025. 'image_color' => '#cae1e9',
  1026. 'color_color' => '#cae1e9'
  1027. );
  1028. return $background_default;
  1029. }
  1030. // Reads the notice configuration
  1031. function readNotice() {
  1032. // Read the notice configuration XML
  1033. $notice_data = readXML('conf', 'notice');
  1034. // Define the default values
  1035. $notice_default = array(
  1036. 'type' => 'none',
  1037. 'notice' => ''
  1038. );
  1039. // Stored data array
  1040. $notice_conf = array();
  1041. // Read the stored values
  1042. if($notice_data) {
  1043. // Initialize the notice configuration XML data
  1044. $notice_xml = new SimpleXMLElement($notice_data);
  1045. // Loop the notice configuration elements
  1046. foreach($notice_xml->children() as $notice_child)
  1047. $notice_conf[$notice_child->getName()] = utf8_decode($notice_child);
  1048. }
  1049. // Checks no value is missing in the stored configuration
  1050. foreach($notice_default as $notice_name => $notice_value) {
  1051. if(!isset($notice_conf[$notice_name]) || empty($notice_conf[$notice_name]))
  1052. $notice_conf[$notice_name] = $notice_default[$notice_name];
  1053. }
  1054. return $notice_conf;
  1055. }
  1056. // The function to get the admin users
  1057. function getUsers() {
  1058. // Try to read the XML file
  1059. $data = readXML('conf', 'users');
  1060. $array = array();
  1061. // Any data?
  1062. if($data) {
  1063. $read = new SimpleXMLElement($data);
  1064. // Check the submitted user exists
  1065. foreach($read->children() as $child) {
  1066. // Get the node attributes
  1067. $attributes = $child->attributes();
  1068. // Push the attributes to the global array (converted into strings)
  1069. $array[$attributes['name'].''] = $attributes['password'].'';
  1070. }
  1071. }
  1072. return $array;
  1073. }
  1074. // Manages users
  1075. function manageUsers($action, $array) {
  1076. // Try to read the old XML file
  1077. $users_array = getUsers();
  1078. // What must we do?
  1079. switch($action) {
  1080. // Add some users
  1081. case 'add':
  1082. foreach($array as $array_user => $array_password)
  1083. $users_array[$array_user] = genStrongHash($array_password);
  1084. break;
  1085. // Remove some users
  1086. case 'remove':
  1087. foreach($array as $array_user) {
  1088. // Not the last user?
  1089. if(count($users_array) > 1)
  1090. unset($users_array[$array_user]);
  1091. }
  1092. break;
  1093. }
  1094. // Regenerate the XML
  1095. $users_xml = '';
  1096. foreach($users_array as $users_name => $users_password)
  1097. $users_xml .= "\n".' <user name="'.stripslashes(htmlspecialchars($users_name)).'" password="'.stripslashes($users_password).'" />';
  1098. // Write the main configuration
  1099. writeXML('conf', 'users', $users_xml);
  1100. }
  1101. // Resize an image with GD
  1102. function resizeImage($path, $ext, $width, $height) {
  1103. // No GD?
  1104. if(!function_exists('gd_info'))
  1105. return false;
  1106. try {
  1107. // Initialize GD
  1108. switch($ext) {
  1109. case 'png':
  1110. $img_resize = imagecreatefrompng($path);
  1111. break;
  1112. case 'gif':
  1113. $img_resize = imagecreatefromgif($path);
  1114. break;
  1115. default:
  1116. $img_resize = imagecreatefromjpeg($path);
  1117. }
  1118. // Get the image size
  1119. $img_size = getimagesize($path);
  1120. $img_width = $img_size[0];
  1121. $img_height = $img_size[1];
  1122. // Necessary to change the image width
  1123. if($img_width > $width && ($img_width > $img_height)) {
  1124. // Process the new sizes
  1125. $new_width = $width;
  1126. $img_process = (($new_width * 100) / $img_width);
  1127. $new_height = (($img_height * $img_process) / 100);
  1128. }
  1129. // Necessary to change the image height
  1130. else if($img_height > $height && ($img_width < $img_height)) {
  1131. // Process the new sizes
  1132. $new_height = $height;
  1133. $img_process = (($new_height * 100) / $img_height);
  1134. $new_width = (($img_width * $img_process) / 100);
  1135. }
  1136. // Else, just use the old sizes
  1137. else {
  1138. $new_width = $img_width;
  1139. $new_height = $img_height;
  1140. }
  1141. // Create the new image
  1142. $new_img = imagecreatetruecolor($new_width, $new_height);
  1143. // Must keep alpha pixels?
  1144. if(($ext == 'png') || ($ext == 'gif')){
  1145. imagealphablending($new_img, false);
  1146. imagesavealpha($new_img, true);
  1147. // Set transparent pixels
  1148. $transparent = imagecolorallocatealpha($new_img, 255, 255, 255, 127);
  1149. imagefilledrectangle($new_img, 0, 0, $new_width, $new_height, $transparent);
  1150. }
  1151. // Copy the new image
  1152. imagecopyresampled($new_img, $img_resize, 0, 0, 0, 0, $new_width, $new_height, $img_size[0], $img_size[1]);
  1153. // Destroy the old data
  1154. imagedestroy($img_resize);
  1155. unlink($path);
  1156. // Write the new image
  1157. switch($ext) {
  1158. case 'png':
  1159. imagepng($new_img, $path);
  1160. break;
  1161. case 'gif':
  1162. imagegif($new_img, $path);
  1163. break;
  1164. default:
  1165. imagejpeg($new_img, $path, 85);
  1166. }
  1167. return true;
  1168. }
  1169. catch(Exception $e) {
  1170. return false;
  1171. }
  1172. }
  1173. ?>