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
<?php

/* iPhone Installer Repository 2.3 */


/* Config */

define('__PLISTS_PATH__', 'plists');
define('__REDIRECT_URL__', '/iphone-apps/info/');


/* Start */
if (!debug_set()) {
    if (!isiPhone() && !isCFNetwork()) { redirectToInfo(); }
}

header('Content-type: application/x-apptapp-repository');
ob_start('ob_gzhandler');

generateIndex(__PLISTS_PATH__);


/* Functions */

function redirectToInfo() {
    die(header('Location: ' . __REDIRECT_URL__)
}

function debugSet() {
    return (isset($_GET['debug']));
}

function isiPhone() {
    return (isset($_SERVER['HTTP_USER_AGENT']) && strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone'));
}

function isCFNetwork() {
    return (isset($_SERVER['HTTP_USER_AGENT']) && strstr($_SERVER['HTTP_USER_AGENT'], 'CFNetwork'));
}

function generateIndex($path) {
        global $index, $indexPackagesArray;

        $index = new DOMDocument();
        $index->load('repository.plist');
        $indexPackagesArray = $index->getElementsByTagName('array')->item(0);

        findPackages($path);
        //$index->normalizeDocument();
        return $index->saveXML();
}

function findPackages($path) {
        $packages = dir($path);

        while($entry = $packages->read()) {
                if($entry != "." && $entry != "..") {
                        $entryPath = $path . '/' . $entry;
                        if(is_dir($entryPath)) {
                                findPackages($entryPath);
                        } else if(stristr($entry, ".plist")){
                                if($path != __PLISTS_PATH__) $category = basename($path);
                                else $category = NULL;

                                addPackage($entryPath, $category);
                        }
                }
        }

        $packages->close();
}

function addPackage($path, $category) {
        global $index, $indexPackagesArray;

        $package = new DOMDocument();
        if($package->load($path)) {
                $dict = $package->getElementsByTagName('dict')->item(0);

                if($category != NULL) {
                        $dict->appendChild($package->createElement('key', 'category'));
                        $dict->appendChild($package->createElement('string', htmlentities($category)));
                }
                $dict->appendChild($package->createElement('key', 'date'));
                $dict->appendChild($package->createElement('string', filemtime($path)));

                $child = $index->importNode($dict, true);
                $indexPackagesArray->appendChild($child);
        }
}

?>