-
Notifications
You must be signed in to change notification settings - Fork 7
/
tools.php
199 lines (171 loc) · 6.19 KB
/
tools.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/*
* RunThisApp allows sharing test builds of iOS apps with testers.
* Copyright (C) 2011 Ludovic Landry & Pascal Cans
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Description of tools
*
* @author Ludovic Landry et pis aussi un peu pcans :p
*/
class Tools {
/**
* @return string current absolute uri
*/
static function current_url() {
$server = $_SERVER['SERVER_NAME'];
if ($_SERVER['SERVER_PORT'] != '80') {
$server .= ':'.$_SERVER['SERVER_PORT'];
}
return 'http://' . $server . $_SERVER['REQUEST_URI'];
}
/**
* Generate a random Apple request id used for web services
* The format returned is XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
* @return string request_id
*/
static function randomAppleRequestId() {
$request_id = md5(rand());
$request_id = substr_replace($request_id, '-', 20, 0);
$request_id = substr_replace($request_id, '-', 16, 0);
$request_id = substr_replace($request_id, '-', 12, 0);
$request_id = substr_replace($request_id, '-', 8, 0);
return strtoupper($request_id);
}
/**
* Return an hexa hash generated randomly with the given length
* @param int $length
* @return string hash
*/
static function randomHashWithSize($length) {
return substr(md5(rand()), 0, $length);
}
/**
* Return the configurtion PLIST file for the application
* identified by the token passed in parameter
* @param string $token
* @return string path
*/
static function getInfoPlistPath($token) {
$dir = __DIR__ . '/app/' . $token . '/app_bundle/Payload/';
$app_folder = NULL;
if (is_dir($dir)) {
if ( ($dh = opendir($dir)) ) {
while (($file = readdir($dh)) !== false) {
if ( filetype($dir . $file) == 'dir' && $file != '.' && $file != '..' ) {
$app_folder = $file;
break;
}
}
closedir($dh);
}
}
$plistPath = NULL;
if ($app_folder != NULL) {
$plistPath = $dir . $app_folder . '/';
// We get the Info.plist of the app if it exists else <app_name>-Info.plist
if ( file_exists($plistPath . 'Info.plist') ) {
$plistFilePath = $plistPath . 'Info.plist';
} else {
$plistFilePath = $plistPath . $app_folder . '-Info.plist';
}
}
return $plistFilePath;
}
static function rel2abs($rel, $base) {
/* return if already absolute URL */
if (parse_url($rel, PHP_URL_SCHEME) != '') return $rel;
/* queries and anchors */
if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel;
/* parse base URL and convert to local variables:
$scheme, $host, $path */
extract(parse_url($base));
/* remove non-directory element from path */
$path = preg_replace('#/[^/]*$#', '', $path);
/* destroy path if relative url points to root */
if ($rel[0] == '/') $path = '';
/* dirty absolute URL */
$abs = "$host$path/$rel";
/* replace '//' or '/./' or '/foo/../' with '/' */
$re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
/* absolute URL is ready! */
return $scheme.'://'.$abs;
}
static function dieError($msg) {
if (!headers_sent()) {
header('HTTP/1.0 500 Internal Server Error');
}
die('Error: ' . $msg);
}
static function display_xml_errors($error, $xml)
{
$return = $xml[$error->line - 1] . "\n";
$return .= str_repeat('-', $error->column) . "^\n";
switch ($error->level) {
case LIBXML_ERR_WARNING:
$return .= "Warning $error->code: ";
break;
case LIBXML_ERR_ERROR:
$return .= "Error $error->code: ";
break;
case LIBXML_ERR_FATAL:
$return .= "Fatal Error $error->code: ";
break;
}
$return .= trim($error->message) .
"\n Line: $error->line" .
"\n Column: $error->column";
if ($error->file) {
$return .= "\n File: $error->file";
}
return "$return\n\n--------------------------------------------\n\n";
}
static function deleteVersionDataFiles($versionToken) {
$path = __DIR__.'/'.UPLOAD_PATH.$versionToken;
//remove profile
$filename = $path.'.mobileprovision';
if (file_exists($filename)) {
unlink($filename);
}
//remove plistcrc
$filename = $path.'.plist';
if (file_exists($filename)) {
unlink($filename);
}
//remove data folder
if (file_exists($path)) {
Tools::rrmdir($path);
}
}
static function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir"){
Tools::rrmdir($dir."/".$object);
} else {
unlink($dir."/".$object);
}
}
}
reset($objects);
rmdir($dir);
}
}
}
?>