-
Notifications
You must be signed in to change notification settings - Fork 16
/
textfiles.php
executable file
·124 lines (109 loc) · 3.25 KB
/
textfiles.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php /*
ocPortal
Copyright (c) ocProducts, 2004-2012
See text/EN/licence.txt for full licencing information.
NOTE TO PROGRAMMERS:
Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
**** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****
*/
/**
* @license http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
* @copyright ocProducts Ltd
* @package core
*/
/**
* Helper function for *_text_file
*
* @param string The file name (without .txt)
* @param ?LANGUAGE_NAME The language to load from (NULL: none) (blank: search)
* @return string The path to the file
*/
function _find_text_file_path($codename,$lang)
{
if (is_null($lang))
{
$langs=array('');
}
elseif ($lang!='')
{
$langs=array($lang);
} else
{
$langs=array(user_lang());
if (get_site_default_lang()!=user_lang()) $langs[]=get_site_default_lang();
if (fallback_lang()!=get_site_default_lang()) $langs[]=fallback_lang();
}
$i=0;
$path='';
do
{
$lang=$langs[$i];
$path=get_custom_file_base().'/text_custom/'.$lang.'/'.$codename.'.txt';
if (!file_exists($path))
{
$path=get_file_base().'/text/'.$lang.'/'.$codename.'.txt';
}
$i++;
}
while ((!file_exists($path)) && (array_key_exists($i,$langs)));
if (!file_exists($path)) $path='';
return $path;
}
/**
* Read a text file, using the _custom system
*
* @param string The file name (without .txt)
* @param ?LANGUAGE_NAME The language to load from (NULL: none) (blank: search)
* @param boolean Whether to tolerate missing files
* @return string The file contents
*/
function read_text_file($codename,$lang=NULL,$missing_blank=false)
{
$path=_find_text_file_path($codename,$lang);
$tmp=@fopen($path,'rb');
if ($tmp===false)
{
if ($missing_blank) return '';
warn_exit(do_lang_tempcode('MISSING_TEXT_FILE',escape_html($codename)));
}
flock($tmp,LOCK_SH);
$in=@file_get_contents($path);
flock($tmp,LOCK_UN);
fclose($tmp);
$in=unixify_line_format($in);
return $in;
}
/**
* Write a text file, using the _custom system
*
* @param string The file name (without .txt)
* @param ?LANGUAGE_NAME The language to write for (NULL: none) (blank: search)
* @param string The data to write
*/
function write_text_file($codename,$lang,$out)
{
$xpath=_find_text_file_path($codename,$lang);
if ($xpath=='')
{
$xpath=get_file_base().'/text/'.user_lang().'/'.$codename.'.txt';
}
$path=str_replace(get_file_base().'/text/',get_custom_file_base().'/text_custom/',$xpath);
$out=unixify_line_format($out);
$myfile=@fopen($path,'at');
flock($myfile,LOCK_EX);
ftruncate($myfile,0);
if ($myfile===false) intelligent_write_error($path);
if (fwrite($myfile,$out)<strlen($out)) warn_exit(do_lang_tempcode('COULD_NOT_SAVE_FILE'));
flock($myfile,LOCK_UN);
fclose($myfile);
fix_permissions($path);
sync_file($path);
// Backup with a timestamp (useful if for example an addon update replaces changes)
$path.='.'.strval(time());
$myfile=@fopen($path,'at');
if ($myfile===false) intelligent_write_error($path);
if (fwrite($myfile,$out)<strlen($out)) warn_exit(do_lang_tempcode('COULD_NOT_SAVE_FILE'));
fclose($myfile);
fix_permissions($path);
sync_file($path);
}