~ubuntu-branches/ubuntu/hardy/gallery2/hardy-security

« back to all changes in this revision

Viewing changes to lib/tools/creator/create-module.php

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2006-04-16 16:42:35 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060416164235-8uy0u4bfjdxpge2o
Tags: 2.1.1-1
* New upstream release (Closes: #362936)
  + Bugfixes for Postgres7 (Closes: #359000, #362152)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 * $RCSfile: create-module.php,v $
 
4
 *
 
5
 * Gallery - a web based photo album viewer and editor
 
6
 * Copyright (C) 2000-2006 Bharat Mediratta
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published by
 
10
 * the Free Software Foundation; either version 2 of the License, or (at
 
11
 * your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful, but
 
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; if not, write to the Free Software
 
20
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 
21
 */
 
22
ini_set('error_reporting', 2047);
 
23
if (!empty($_SERVER['SERVER_NAME'])) {
 
24
    print "You must run this from the command line\n";
 
25
    exit(1);
 
26
}
 
27
 
 
28
if (!file_exists('modules')) {
 
29
    print "You must run this from the gallery2 directory\n";
 
30
    exit(1);
 
31
}
 
32
 
 
33
$author = '';
 
34
$authorFullName = '';
 
35
if (function_exists('posix_getlogin')) {
 
36
    $author = posix_getlogin();
 
37
    $tmp = posix_getpwnam($author);
 
38
    if (!empty($tmp['gecos'])) {
 
39
        $authorFullName = $tmp['gecos'];
 
40
    }
 
41
}
 
42
 
 
43
require_once(dirname(__FILE__) . '/../../../lib/smarty/Smarty.class.php');
 
44
if (!empty($_ENV['TMP'])) {
 
45
    $tmpdir = $_ENV['TMP'];
 
46
} else {
 
47
    $tmpdir = '/tmp';
 
48
}
 
49
$tmpdir .= "/g2_" . rand(1, 30000);
 
50
if (file_exists($tmpdir)) {
 
51
    print "Tmp dir already exists: $tmpdir\n";
 
52
    exit(1);
 
53
}
 
54
 
 
55
if (!mkdir($tmpdir)) {
 
56
    print "Unable to make tmp dir: $tmpdir\n";
 
57
    exit(1);
 
58
}
 
59
 
 
60
$smarty = new Smarty();
 
61
$smarty->compile_dir = $tmpdir;
 
62
$smarty->error_reporting = error_reporting();
 
63
$smarty->debugging = true;
 
64
$smarty->use_sub_dirs = false;
 
65
$smarty->template_dir = dirname(__FILE__);
 
66
 
 
67
/*
 
68
 * Gather any info we need from the user
 
69
 */
 
70
if (!empty($author)) {
 
71
    $defaultModuleName = 'Hello ' . ucfirst($author);
 
72
} else {
 
73
    $defaultModuleName = 'Hello World';
 
74
}
 
75
 
 
76
while (empty($moduleName)) {
 
77
    $moduleName = ask('What is the name of your module?', $defaultModuleName);
 
78
}
 
79
 
 
80
while (empty($moduleId)) {
 
81
    $moduleId = ask('What is the id of your module?',
 
82
                    strtolower(preg_replace('/ /', '', $moduleName)));
 
83
}
 
84
$moduleId = preg_replace('/\W/', '', $moduleId);
 
85
$ucModuleId = ucfirst($moduleId);
 
86
 
 
87
$smarty->assign('moduleId', $moduleId);
 
88
$smarty->assign('ucModuleId', $ucModuleId);
 
89
$smarty->assign('moduleName', $moduleName);
 
90
$smarty->assign('author', $author);
 
91
$smarty->assign('authorFullName', $authorFullName);
 
92
$smarty->assign('viewName', 'MyPage');
 
93
$smarty->assign('mapName', "${ucModuleId}Map");
 
94
 
 
95
/*
 
96
 * Start building things!
 
97
 */
 
98
 
 
99
/* Make the module directory */
 
100
$modulePath = 'modules/' . $moduleId;
 
101
if (file_exists($modulePath)) {
 
102
    error("$modulePath already exists!");
 
103
} else {
 
104
    mkdir($modulePath) || error("Can't mkdir($modulePath)");
 
105
}
 
106
 
 
107
/* Create module.inc */
 
108
$fd = safe_fopen("$modulePath/module.inc");
 
109
fwrite($fd, $smarty->fetch(dirname(__FILE__) . '/module.inc.tpl'));
 
110
fclose($fd);
 
111
 
 
112
/* Create our sample view and template */
 
113
$fd = safe_fopen("$modulePath/MyPage.inc");
 
114
fwrite($fd, $smarty->fetch(dirname(__FILE__) . '/MyPage.inc.tpl'));
 
115
fclose($fd);
 
116
 
 
117
mkdir("$modulePath/templates");
 
118
$fd = safe_fopen("$modulePath/templates/MyPage.tpl");
 
119
fwrite($fd, $smarty->fetch(dirname(__FILE__) . '/MyPage.tpl.tpl'));
 
120
fclose($fd);
 
121
 
 
122
/*
 
123
 * Create our map
 
124
 */
 
125
mkdir($modulePath . '/classes');
 
126
mkdir($modulePath . '/classes/GalleryStorage');
 
127
 
 
128
$smarty->assign('makefileType', 'classes');
 
129
$fd = safe_fopen("$modulePath/classes/GNUmakefile");
 
130
fwrite($fd, $smarty->fetch(dirname(__FILE__) . '/GNUmakefile.tpl'));
 
131
fclose($fd);
 
132
 
 
133
$smarty->assign('makefileType', 'GalleryStorage');
 
134
$fd = safe_fopen("$modulePath/classes/GalleryStorage/GNUmakefile");
 
135
fwrite($fd, $smarty->fetch(dirname(__FILE__) . '/GNUmakefile.tpl'));
 
136
fclose($fd);
 
137
 
 
138
$fd = safe_fopen("$modulePath/classes/Maps.xml");
 
139
fwrite($fd, $smarty->fetch(dirname(__FILE__) . '/map.tpl'));
 
140
fclose($fd);
 
141
 
 
142
$fd = safe_fopen("$modulePath/classes/MyPageHelper.class");
 
143
fwrite($fd, $smarty->fetch(dirname(__FILE__) . '/MyPageHelper.class.tpl'));
 
144
fclose($fd);
 
145
 
 
146
print "* * * * * * * * * * * * * * * * * * * * * * * * * *\n";
 
147
print "Your module is ready!  You must build it by doing: \n";
 
148
print "\n";
 
149
print "  cd modules/$moduleId/classes \n";
 
150
print "  make && make clean\n";
 
151
print "\n";
 
152
print "Then you can go to the Site Admin -> Modules \n";
 
153
print "page and install and activate your module!\n";
 
154
print "* * * * * * * * * * * * * * * * * * * * * * * * * *\n";
 
155
 
 
156
function ask($prompt, $default='') {
 
157
    print $prompt;
 
158
    if (!empty($default)) {
 
159
        print " [$default]";
 
160
    }
 
161
    print ' ';
 
162
    $line = trim(fgets(STDIN));
 
163
    if (empty($line)) {
 
164
        return $default;
 
165
    }
 
166
    return $line;
 
167
}
 
168
 
 
169
function error($message) {
 
170
    fwrite(STDERR, "$message\n");
 
171
    fwrite(STDERR, "*** Exiting!\n");
 
172
    cleanup();
 
173
    exit(1);
 
174
}
 
175
 
 
176
function cleanup() {
 
177
    global $tmpdir;
 
178
    if (file_exists($tmpdir)) {
 
179
        system("rm -rf $tmpdir");
 
180
    }
 
181
}
 
182
 
 
183
function safe_fopen($path) {
 
184
    ($fd = fopen($path, 'wb')) || error("Can't write to $path");
 
185
    return $fd;
 
186
}
 
 
b'\\ No newline at end of file'