summaryrefslogtreecommitdiff
blob: 352e8332e4d2f0328d392a6e3ebfe9c3d9dce930 (plain)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php

/***************************************************************************
 *                                                                         *
 *   Copyright (C) 2004 Tim Yamin < plasmaroo@gentoo.org >                 *
 *                                < plasm@roo.me.uk >                      *
 *                                                                         *
 *   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 2 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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 *                                                                         *
 ***************************************************************************/

$FILEROOT='/var/www/dev.gentoo.org/glsamaker/data';
$FILEPOOLROOT='/var/www/dev.gentoo.org/glsamaker/pool';
$FEXT='xml';

require_once 'xml.glsaparser';

/*! Return an array containing a GLSA tree of the available GLSAs
    in $FILEROOT.

	@param chronologicalOrder Sort the array in earliest to last order.
	@param releaseOrder Sort the array by GLSA ID; not by modification date.
	@param treeOrder Return GLSAs as they are in the data tree.
	@return An array containg the GLSAs.

*/

function fileGrepper_parseTree($chronologicalOrder = false, $releaseOrder = false, $treeOrder = false)
{
	global $FILEROOT, $FEXT;
	if ($GLSAHandle = @opendir($FILEROOT))
	{
		$GLSAYears = array();
		while (false !== ($GLSAHandleItem = readdir($GLSAHandle)))
		{
			if ($GLSAHandleItem != '.' && $GLSAHandleItem != '..')
				$GLSAYears[$GLSAHandleItem] = $GLSAHandleItem;
		}

		// Sort and parse array
		arsort( $GLSAYears );

		foreach( $GLSAYears as $GLSAYear )
		{
			if ($GLSAHandle = @opendir($FILEROOT.'/'.$GLSAYear))
			{
				$GLSAYears[$GLSAYear] = array();
				while (false !== ($GLSAHandleItem = readdir($GLSAHandle)))
				{
					if($GLSAHandleItem != '.' && $GLSAHandleItem != '..')
						$GLSAYears[$GLSAYear][$GLSAHandleItem] = $GLSAHandleItem;
				}
				arsort($GLSAYears[$GLSAYear]);
			}

			foreach( $GLSAYears[$GLSAYear] as $GLSAMonth )
			{
				if ($GLSAHandle = @opendir($FILEROOT.'/'.$GLSAYear.'/'.$GLSAMonth))
				{
					$GLSAYears[$GLSAYear][$GLSAMonth] = array();
					while (false !== ($GLSAHandleItem = readdir($GLSAHandle)))
					{
						if($GLSAHandleItem != '.' && $GLSAHandleItem != '..' && strrchr($GLSAHandleItem, '.') == '.'.$FEXT)
						{
							$GLSAYears[$GLSAYear][$GLSAMonth][padNumber(basename($GLSAHandleItem, '.'.$FEXT))] = filemtime($FILEROOT.'/'.$GLSAYear.'/'.$GLSAMonth.'/'.$GLSAHandleItem);
						}
					}
					arsort($GLSAYears[$GLSAYear][$GLSAMonth]);
				}
			}
		}

		if($treeOrder && $releaseOrder)
		{
			foreach($GLSAYears as $GLSAYear => $GLSAYearArray)
			{
				foreach($GLSAYearArray as $GLSAMonth => $GLSAMonthArray)
				{
					$GLSAMonthArray = array_flip($GLSAMonthArray);
					arsort($GLSAMonthArray);
					$GLSAYears[$GLSAYear][$GLSAMonth] = array_flip($GLSAMonthArray);
				}
			}
			return $GLSAYears;
		}
		if($treeOrder)
			return $GLSAYears;

		// Serialize the array

		$ResultArray = array();
		$SortArray   = array();
		$MapArray    = array();

		foreach( $GLSAYears as $GLSAYear => $GLSAYearArray )
		{
			foreach( $GLSAYearArray as $GLSAMonth => $GLSAMonthArray )
			{
				if(!is_array($GLSAMonthArray))
				{
					echo '<b>Assertion from includes.IO.FileGrepper inside [[ ', __FUNCTION__, '//', __LINE__, ' ]] :: Empty monthly key $GLSAYear/$GLSAMonthArray found [ [ Possible permission ] Errors withheld! ] - no GLSAs are thus listed from that key...<br>&nbsp;</b>';
					$GLSAMonthArray = array();
				}
				foreach( $GLSAMonthArray as $GLSAHandleItem => $GLSAHandleKey )
				{
					$ResultArray[] = $GLSAYear.$GLSAMonth.'-'.$GLSAHandleItem;
					$SortArray[] = $GLSAHandleKey;
				}
			}
		}

		$i = 0;
		if($releaseOrder)
		{
			$chronologicalOrder ? asort($ResultArray) : arsort($ResultArray);
			foreach( $ResultArray as $ResultKey => $ResultItem )
			{
				$MapArray[$i++] = array($ResultItem, $SortArray[$ResultKey]);
			}
		}
		else
		{
			$chronologicalOrder ? asort($SortArray) : arsort($SortArray);
			foreach( $SortArray as $SortKey => $SortDate )
			{
				$MapArray[$i++] = array($ResultArray[$SortKey], $SortDate);
			}
		}

		// DEBUG => echo '<pre>'; print_r($MapArray); echo '</pre>';
		return $MapArray;
	}
	else
	{
		print_r('<b>Error: from Includes.IO.FileGrepper :: File tree traversal failed!</b>');
		return false;
	}

}

/*! Parse the pooled GLSAs; returning them in an array.

	@param chronologicalOrder Sort the array in earliest to last order.
	@return An array containg the GLSAs.

*/

function fileGrepper_parsePool($chronologicalOrder = false)
{
	global $FILEPOOLROOT, $FEXT;
	if ($GLSAHandle = @opendir($FILEPOOLROOT))
	{
		$GLSAs = array();
		while (false !== ($GLSAHandleItem = readdir($GLSAHandle)))
		{
			if ($GLSAHandleItem != '.' && $GLSAHandleItem != '..' && strrchr($GLSAHandleItem, '.') == '.'.$FEXT)
			{
				$GLSAs[str_replace('.'.$FEXT, '', $GLSAHandleItem)] = filemtime($FILEPOOLROOT.'/'.$GLSAHandleItem);
			}
		}

		// Sort the modification date array and map the result data
		$chronologicalOrder ? asort($GLSAs) : arsort($GLSAs);

		$i = 0;
		$MapArray = array();

		foreach( $GLSAs as $SortKey => $SortDate )
		{
			$MapArray[$i] = array($SortKey, $SortDate);
			$i++;
		}
		return $MapArray;	
	}
	else
	{
		print_r('<b>Error: from Includes.IO.FileGrepper :: File tree traversal failed!</b>');
		return false;
	}

}

/*! Retrieve the text of a GLSA; or return the filename of the specified GLSA,
    or edit the specified GLSA.

	@param year The GLSA Year component; this should be set to NULL if the
	            GLSA is to be a pooled one.
	@param month The GLSA Month component; this should be set to NULL if the
	             GLSA is to be a pooled one.
	@param id The GLSA ID component; this should be set to NULL if the GLSA is
	          to be a pooled one.
	@param override The GLSA Pool ID; this should be set to NULL if the GLSA is
	                not a pooled one.
	@param generateStringOnly Return the path to the specified GLSA rather than
	                          the text.
	@param editGLSA Edit the specified GLSA to the XML specified in this parameter.
	@return The GLSA Text or '<nodata>' in an error; the file string if
	        generateStringOnly is set, or a boolean returning the success or failure
	        or the edit write action.

*/

function fileGrepper_getGLSAText($year, $month, $id, $override = NULL, $generateStringOnly = false, $editGLSA = '')
{
	global $FILEROOT, $FILEPOOLROOT, $FEXT;
	if($override != NULL)
	{
		if($generateStringOnly)
			return($FILEPOOLROOT.'/'.$override.'.'.$FEXT);
		else if(@is_file($FILEPOOLROOT.'/'.$override.'.'.$FEXT))
		{
			$fp = fopen($FILEPOOLROOT.'/'.$override.'.'.$FEXT, $editGLSA == '' ? 'r' : 'w');
			if($editGLSA != '')
				return(fwrite($fp, $editGLSA) == strlen($editGLSA) ? true : false);
			else
				$fd = fread($fp, filesize( $FILEPOOLROOT.'/'.$override.'.'.$FEXT));

			return $fd;
		}
		return '<nodata>';
	}	

	$year  = padYear($year);
	$month = padNumber($month);
	$id    = padNumber($id);

	if($generateStringOnly === '-')
		return($FILEROOT.'/'.$year.'/'.$month);
	else if($generateStringOnly)
		return($FILEROOT.'/'.$year.'/'.$month.'/'.$id.'.'.$FEXT);
	else if(@is_file($FILEROOT.'/'.$year.'/'.$month.'/'.$id.'.'.$FEXT))
	{	
		$fp = fopen($FILEROOT.'/'.$year.'/'.$month.'/'.$id.'.'.$FEXT, $editGLSA == '' ? 'r' : 'w');
		if($editGLSA != '')
			return(fwrite($fp, $editGLSA) == strlen($editGLSA) ? true : false);
		else
			$fd = fread($fp, filesize( $FILEROOT.'/'.$year.'/'.$month.'/'.$id.'.'.$FEXT ));

		return $fd;
	}
	return '<nodata>';
}

/*! Add the GLSA to the pool.

	@param data The GLSA XML
	@param changeID This changes the string '!-- Insert ID here! --!' to
	                a hash.
	@return The GLSA ID; or false on failure.

*/

function fileGrepper_commitToPool($data, $changeID = false)
{

	global $FILEPOOLROOT, $FEXT;
	do { $hash = md5((double)microtime()*rand(1,2)); } while (@is_file($FILEPOOLROOT.'/'.$hash.'.'.$FEXT));
	if($changeID) $data=str_replace('!-- Insert ID here! --!', $hash, $data);

	if(!$fp = fopen($FILEPOOLROOT.'/'.$hash.'.'.$FEXT, 'w')) return false;
	if(fwrite($fp, $data) != strlen($data)) return false;

	return $hash;

}

/*! Move a GLSA.

	@param ID The current GLSA ID
	@param IDNew The new GLSA ID. If 'AutoMove' is specified
	             then the ID is automatically generated.
	@return The new ID; or '<error>'.

*/

function fileGrepper_moveGLSA($ID, $IDNew = '')
{
	if($IDNew = 'AutoMove')
	{
		$ID1 = date('Y');
		$ID2 = date('m');
		$ID3 = '01';
		$Tree = fileGrepper_parseTree(false, true, true);
		if(count($Tree[$ID1]) > 0)
		{
			// Get the last GLSA = {The first array item}+1
			if(count($Tree[$ID1][$ID2]) > 0)
				$ID3 = key($Tree[$ID1][$ID2])+1;
		}
		$IDNew = $ID1.$ID2.'-'.padNumber($ID3);
	}
	if(ereg('^[A-Fa-f0-9]{32}$', $ID))
		$oldFile = fileGrepper_getGLSAText(NULL, NULL, NULL, $ID, true);
	else if(sscanf($ID, '%04d%02d-%d', $ID1, $ID2, $ID3) == 3)
		$oldFile = fileGrepper_getGLSAText($ID1, $ID2, $ID3, NULL, true);
	else
	{
		echo(generateWarning('Error: from Includes.IO.FileGrepper :: Invalid source ID [ '.$ID.' ] specified!'));
		return '<error>';
	}

	if(ereg('^[A-Fa-f0-9]{32}$', $IDNew))
		$newFile = fileGrepper_getGLSAText(NULL, NULL, NULL, $IDNew, true);
	else if(sscanf($IDNew, '%04d%02d-%d', $ID1, $ID2, $ID3) == 3)
	{
		$newDirectory = fileGrepper_getGLSAText($ID1, $ID2, $ID3, NULL, '-');
		$newFile = fileGrepper_getGLSAText($ID1, $ID2, $ID3, NULL, true);
	}
	else
	{
		echo(generateWarning('Error: from Includes.IO.FileGrepper.MoveGLSA :: Invalid destination ID [ '.$IDNew.' ] specified!'));
		return '<error>';
	}

	if(@is_file($oldFile) && !@is_file($newFile))
	{
		if($newDirectory != '')
		{
			if(!@is_dir($newDirectory))
			{
				echo(generateInfo('Trying mkdir('.$newDirectory.')...'));
				mkdir($newDirectory);
			}
		}
		echo(generateInfo('Trying rename('.$oldFile.', '.$newFile.')...'));
		if(rename($oldFile, $newFile))
		{
			$Parser = new GLSAParser();
			$Parser->GLSAParse(fileGrepper_getGLSAText( $pool ? NULL: $ID1, $pool ? NULL: $ID2, $pool ? NULL: $ID3, $pool ? $ID : NULL ));

			// Update ID
			$Parser->GLSAID = $IDNew;

			// Sanitize Review Metadata
			$GLSAReviews =& $Parser->searchMetadata($Parser->GLSAMetadata, 0, 'reviews');
			$GLSAReviews = NULL;
			if($GLSAReviewsIndex = array_search(NULL, $Parser->GLSAMetadata, true))
				unset($Parser->GLSAMetadata[$GLSAReviewsIndex]);

			fileGrepper_getGLSAText( $pool ? NULL: $ID1, $pool ? NULL: $ID2, $pool ? NULL: $ID3, $pool ? $ID : NULL, false, $Parser->GLSAToXML('reset'));
			return $IDNew;
		}
		else
			return '<error>';
	}
	echo(generateWarning('Error: from Includes.IO.FileGrepper.MoveGLSA :: Missing or existing file [ '.$ID.' >> '.$IDNew.' ] specified!'));
	return '<error>';
}

/*! Pad a number with a zero to make it at least two characters long. */

function padNumber($input)
{

	if($input == '') return '00';

	if(strlen($input) == 1) return '0'.$input;
	return $input;

}

/*! Pad a year with '20' if a two-digit year is given; or return
    the current year if a null string is given. */

function padYear($input)
{

	if($input == '') return date('Y');
	if(strlen($input) == 2) return '20'.$input;
	return $input;

}

// Local Variables: ***
// truncate-lines:true ***
// End: ***

?>