1
|
<?php
|
2
|
/**
|
3
|
*
|
4
|
* @category module
|
5
|
* @package droplet
|
6
|
* @author Ruud Eisinga (Ruud) John (PCWacht)
|
7
|
* @author WebsiteBaker Project
|
8
|
* @copyright WebsiteBaker Org. e.V.
|
9
|
* @link http://websitebaker.org/
|
10
|
* @license http://www.gnu.org/licenses/gpl.html
|
11
|
* @platform WebsiteBaker 2.8.3
|
12
|
* @requirements PHP 5.3.6 and higher
|
13
|
* @version $Id: droplets.functions.php 2 2017-07-02 15:14:29Z Manuela $
|
14
|
* @filesource $HeadURL: svn://isteam.dynxs.de/wb/2.10.x/trunk/modules/droplets/droplets.functions.php $
|
15
|
* @lastmodified $Date: 2017-07-02 17:14:29 +0200 (Sun, 02 Jul 2017) $
|
16
|
*
|
17
|
*/
|
18
|
/* -------------------------------------------------------- */
|
19
|
// Must include code to stop this file being accessed directly
|
20
|
if(!defined('WB_PATH')) {
|
21
|
|
22
|
require_once(dirname(dirname(dirname(__FILE__))).'/framework/globalExceptionHandler.php');
|
23
|
throw new IllegalFileException();
|
24
|
}
|
25
|
/* -------------------------------------------------------- */
|
26
|
|
27
|
function prepareDropletToFile($aDroplet) {
|
28
|
$retVal = '';
|
29
|
$aComment = array();
|
30
|
$sDescription = '//:'.(($aDroplet['description']!='') ? $aDroplet['description']: 'Add a desription');
|
31
|
$sComments = '';
|
32
|
$aComment = explode("\n", $aDroplet['comments']);
|
33
|
if( (sizeof($aComment)) ){
|
34
|
foreach($aComment as $isComments) {
|
35
|
if( trim($isComments) !='') {
|
36
|
$sComments .= '//:'.$isComments."\n";
|
37
|
}
|
38
|
}
|
39
|
}
|
40
|
if( !$sComments ){
|
41
|
$sComments .= '//:use [['.$aDroplet['name'].']]'."\n";
|
42
|
}
|
43
|
$sCode = '';
|
44
|
$aCode = explode("\n",$aDroplet['code']);
|
45
|
if( (sizeof($aCode)) ){
|
46
|
foreach($aCode AS $isCode) {
|
47
|
if( $isCode!='') {
|
48
|
$sCode .= $isCode."\n";
|
49
|
}
|
50
|
}
|
51
|
}
|
52
|
$retVal = $sDescription."\n".$sComments.rtrim($sCode,"\n");
|
53
|
return $retVal;
|
54
|
}
|
55
|
|
56
|
function backupDropletFromDatabase( $sTmpDir, $FilesInDB='*', $oDb) {
|
57
|
if (!class_exists('PclZip',false) ) { require( WB_PATH.'/include/pclzip/Constants.php'); }
|
58
|
$retVal = array();
|
59
|
$sDescription = '';
|
60
|
$FilesInDB = rtrim($FilesInDB, ',');
|
61
|
$sqlWhere = ( ($FilesInDB=='*') ? '': 'WHERE `name` IN ('.$FilesInDB.') ');
|
62
|
$sql = 'SELECT `name`,`description`,`comments`,`code` FROM `'.TABLE_PREFIX.'mod_droplets` '
|
63
|
. $sqlWhere
|
64
|
. 'ORDER BY `modified_when` DESC';
|
65
|
if( $oRes = $oDb->query($sql) ) {
|
66
|
while($aDroplet = $oRes->fetchRow(MYSQLI_ASSOC)) {
|
67
|
$sData = prepareDropletToFile($aDroplet);
|
68
|
$sFileName = $sTmpDir.$aDroplet['name'].'.php';
|
69
|
if(file_put_contents($sFileName,$sData)) {
|
70
|
$sDescription = ($aDroplet['description']);
|
71
|
$retVal[] = array(
|
72
|
PCLZIP_ATT_FILE_NAME => $sFileName,
|
73
|
PCLZIP_ATT_FILE_COMMENT => $sDescription
|
74
|
);
|
75
|
}
|
76
|
}
|
77
|
}
|
78
|
return $retVal;
|
79
|
}
|
80
|
|
81
|
function getUniqueName($oDb, $sName)
|
82
|
{
|
83
|
$sBaseName = preg_replace('/^(.*?)(\_[0-9]+)?$/', '$1', $sName);
|
84
|
$sql = 'SELECT `name` FROM `'.TABLE_PREFIX.'mod_droplets` '
|
85
|
. 'WHERE `name` RLIKE \'^'.$sBaseName.'(\_[0-9]+)?$\' '
|
86
|
. 'ORDER BY `name` DESC';
|
87
|
if (($sMaxName = $oDb->get_one($sql))) {
|
88
|
$iCount = intval(preg_replace('/^'.$sBaseName.'\_([0-9]+)$/', '$1', $sMaxName));
|
89
|
$sName = $sBaseName.sprintf('_%03d', ++$iCount);
|
90
|
}
|
91
|
return $sName;
|
92
|
}
|
93
|
/**
|
94
|
* importDropletToDB()
|
95
|
*
|
96
|
* @param mixed $aDroplet
|
97
|
* @param mixed $msg
|
98
|
* @param mixed $bOverwriteDroplets
|
99
|
* @return
|
100
|
*/
|
101
|
function insertDroplet( array $aDroplet, $oDb, $oApp, $bUpdateDroplets = false )
|
102
|
{
|
103
|
$sImportDroplets = '';
|
104
|
$extraSql = '';
|
105
|
$sPattern = "#//:#im";
|
106
|
$sDropletFile = $aDroplet['name'];
|
107
|
$sDropletFile = preg_replace('/^\xEF\xBB\xBF/', '', $sDropletFile);
|
108
|
$sDropletName = pathinfo ($sDropletFile, PATHINFO_FILENAME);
|
109
|
// get right $aFileData a) from Zip or b) from File
|
110
|
if( isset($aDroplet['content']) ) {
|
111
|
$aFileData = $aDroplet['content'];
|
112
|
$sFileData = $aFileData[0];
|
113
|
$bRetval = (bool)preg_match_all($sPattern, $sFileData, $matches, PREG_SET_ORDER);
|
114
|
if ( $bRetval == false ) { return $bRetval; }
|
115
|
}
|
116
|
if( isset($aDroplet['output']) ) { $aFileData = file($sDropletFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); }
|
117
|
// prepare table mod_droplets fields
|
118
|
if( sizeof($aFileData) > 0 ) {
|
119
|
// get description, comments and oode
|
120
|
$bDescription = false;
|
121
|
$bComments = false;
|
122
|
$bCode = false;
|
123
|
$sDescription = '';
|
124
|
$sComments = '';
|
125
|
$sCode = '';
|
126
|
while ( sizeof($aFileData) > 0 ) {
|
127
|
$sSqlLine = (array_shift($aFileData));
|
128
|
$isNotCode = (bool)preg_match($sPattern, $sSqlLine);
|
129
|
if( $isNotCode==true ) {
|
130
|
// first step line is description
|
131
|
if($bDescription==false) {
|
132
|
$sDescription .= str_replace('//:','',$sSqlLine);
|
133
|
$bDescription = true;
|
134
|
} else {
|
135
|
// second step fill comments
|
136
|
$sComments .= trim(str_replace('//:','',$sSqlLine)."\n");
|
137
|
}
|
138
|
} else {
|
139
|
// third step fill code
|
140
|
$sCode .= str_replace('//:','',$sSqlLine)."\n";
|
141
|
}
|
142
|
}
|
143
|
}
|
144
|
// TODO future set parameter to class RawDropletInterface
|
145
|
$sql = 'SELECT `name` FROM `'.TABLE_PREFIX.'mod_droplets` '
|
146
|
. 'WHERE `name` LIKE \''.addcslashes($oDb->escapeString($sDropletName), '%_').'\' ';
|
147
|
if( !( $sTmpName = $oDb->get_one($sql)) )
|
148
|
{
|
149
|
$sql = 'INSERT INTO `'.TABLE_PREFIX.'mod_droplets`';
|
150
|
$sImportDroplets = $sDropletName ;
|
151
|
} elseif ($bUpdateDroplets) {
|
152
|
$sDropletName = $sTmpName;
|
153
|
$sql = 'UPDATE `'.TABLE_PREFIX.'mod_droplets` ';
|
154
|
$extraSql = 'WHERE `name` = \''.addcslashes($oDb->escapeString($sDropletName), '%_').'\' ';
|
155
|
$sImportDroplets = $sDropletName;
|
156
|
}
|
157
|
if( !isset($sTmpName) || $bUpdateDroplets) {
|
158
|
$iModifiedWhen = time();
|
159
|
$iModifiedBy = (method_exists($oApp, 'get_user_id') && ($oApp->get_user_id()!=null) ? $oApp->get_user_id() : 1);
|
160
|
$sql .= 'SET `name` =\''.$oDb->escapeString($sDropletName).'\','
|
161
|
. '`description` =\''.$oDb->escapeString($sDescription).'\','
|
162
|
. '`comments` =\''.$oDb->escapeString($sComments).'\','
|
163
|
. '`code` =\''.$oDb->escapeString($sCode).'\','
|
164
|
. '`modified_when` = '.$iModifiedWhen.','
|
165
|
. '`modified_by` = '.$iModifiedBy.','
|
166
|
. '`active` = 1'
|
167
|
. $extraSql;
|
168
|
}
|
169
|
if( $oDb->query($sql) ) {
|
170
|
} else {
|
171
|
}
|
172
|
|
173
|
return ($sImportDroplets != '') ? $sImportDroplets : false;
|
174
|
}
|
175
|
|
176
|
function insertDropletFile($aDropletFiles, $oDb, $oApp, &$msg,$bOverwriteDroplets)
|
177
|
{
|
178
|
// $oApp = new admin ('##skip##');
|
179
|
$OK = ' <span style="color:#006400; font-weight:bold;">OK</span> ';
|
180
|
$FAIL = ' <span style="color:#ff0000; font-weight:bold;">FAILED</span> ';
|
181
|
foreach ($aDropletFiles as $sDropletFile) {
|
182
|
$msgSql = '';
|
183
|
$extraSql = '';
|
184
|
$sDropletName = pathinfo ($sDropletFile, PATHINFO_FILENAME);
|
185
|
$sql = 'SELECT `name` FROM `'.TABLE_PREFIX.'mod_droplets` '
|
186
|
. 'WHERE `name` LIKE \''.addcslashes($oDb->escapeString($sDropletName), '%_').'\' ';
|
187
|
if( !( $sTmpName = $oDb->get_one($sql)) )
|
188
|
{
|
189
|
$sql = 'INSERT INTO `'.TABLE_PREFIX.'mod_droplets`';
|
190
|
$msgSql = 'INSERT Droplet `'.$oDb->escapeString($sDropletName).'` INTO`'.TABLE_PREFIX.'mod_droplets`'." $OK";
|
191
|
} elseif ($bOverwriteDroplets)
|
192
|
{
|
193
|
$sDropletName = $sTmpName;
|
194
|
$sql = 'UPDATE `'.TABLE_PREFIX.'mod_droplets` ';
|
195
|
$extraSql = 'WHERE `name` = \''.addcslashes($oDb->escapeString($sDropletName), '%_').'\' ';
|
196
|
$msgSql = 'UPDATE Droplet `'.$sDropletName.'` INTO`'.TABLE_PREFIX.'mod_droplets`'." $OK";
|
197
|
}
|
198
|
// get description, comments and oode
|
199
|
$sDropletFile = preg_replace('/^\xEF\xBB\xBF/', '', $sDropletFile);
|
200
|
if( ($msgSql!='') && ($aFileData = file($sDropletFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES))){
|
201
|
$bDescription = false;
|
202
|
$bComments = false;
|
203
|
$bCode = false;
|
204
|
$sDescription = '';
|
205
|
$sComments = '';
|
206
|
$sCode = '';
|
207
|
$sPattern = "#//:#im";
|
208
|
while ( sizeof($aFileData) > 0 ) {
|
209
|
$sSqlLine = (array_shift($aFileData));
|
210
|
$isNotCode = (bool)preg_match($sPattern, $sSqlLine);
|
211
|
if ($isNotCode==true ) {
|
212
|
// first step line is description
|
213
|
if($bDescription==false) {
|
214
|
$sDescription .= str_replace('//:','',$sSqlLine);
|
215
|
$bDescription = true;
|
216
|
} else {
|
217
|
// second step fill comments
|
218
|
$sComments .= str_replace('//:','',$sSqlLine)."\n";
|
219
|
}
|
220
|
} else {
|
221
|
// third step fill code
|
222
|
$sCode .= str_replace('//:','',$sSqlLine)."\n";
|
223
|
}
|
224
|
}
|
225
|
$iModifiedWhen = time();
|
226
|
$iModifiedBy = (method_exists($oApp, 'get_user_id') && ($oApp->get_user_id()!=null) ? $oApp->get_user_id() : 1);
|
227
|
$sql .= 'SET `name` =\''.$oDb->escapeString($sDropletName).'\','
|
228
|
. '`description` =\''.$oDb->escapeString($sDescription).'\','
|
229
|
. '`comments` =\''.$oDb->escapeString($sComments).'\','
|
230
|
. '`code` =\''.$oDb->escapeString($sCode).'\','
|
231
|
. '`modified_when` = '.$iModifiedWhen.','
|
232
|
. '`modified_by` = '.$iModifiedBy.','
|
233
|
. '`active` = 1'
|
234
|
. $extraSql;
|
235
|
}
|
236
|
if( $oDb->query($sql) ) {
|
237
|
if( $msgSql!='' ) { $msg[] = $msgSql; }
|
238
|
} else {
|
239
|
$msg[] = $oDb->get_error();
|
240
|
}
|
241
|
}
|
242
|
return;
|
243
|
}
|
244
|
/* -------------------------------------------------------- */
|
245
|
|
246
|
function isDropletFile($sFileName)
|
247
|
{
|
248
|
$bRetval = false;
|
249
|
$matches = array();
|
250
|
if(($sFileData = file_get_contents($sFileName)) !== false)
|
251
|
{
|
252
|
// $sPattern = "#(?://:)+[\w]*\w?#is";
|
253
|
// $sPattern = "#//:[\w].+#imS";
|
254
|
$sPattern = "#//:#im";
|
255
|
$bRetval = (bool)preg_match_all($sPattern, $sFileData, $matches, PREG_SET_ORDER);
|
256
|
}
|
257
|
return $bRetval;
|
258
|
}
|
259
|
|
260
|
/* -------------------------------------------------------- */
|
261
|
function getDropletFromFiles($sBaseDir)
|
262
|
{
|
263
|
$aRetval = array();
|
264
|
$oIterator = new DirectoryIterator($sBaseDir);
|
265
|
foreach ($oIterator as $fileInfo) {
|
266
|
// iterate the directory
|
267
|
if($fileInfo->isDot()) continue;
|
268
|
$sFileName = rtrim(str_replace('\\', '/', $fileInfo->getPathname()), '/');
|
269
|
if($fileInfo->isFile()) {
|
270
|
// only droplets are interesting
|
271
|
if((file_exists($sFileName) && isDropletFile($sFileName))) {
|
272
|
// if dir has no corresponding accessfile remember it
|
273
|
$aRetval[] = $sFileName;
|
274
|
}
|
275
|
}
|
276
|
}
|
277
|
return $aRetval;
|
278
|
}
|