Modification log added
[mediadatabase.git] / php / add.php
blob61e157a87a7b499e3376c6be5952006436653810
1 <?php
3 require('inc.header.php');
5 function showform ()
7 global $config, $self;
8 echo "<p><b>Upload .mb file</b>\n";
9 echo "<p>\n<i>\n";
10 echo " Note: Any title you enter here will override the title<br>\n";
11 echo " (if any) contained in the file you are about to upload.<br>\n";
12 echo " If no title is found, &quot;".$config['default_media_title']."&quot; is used.<br>\n</i>\n";
13 echo "<form enctype=\"multipart/form-data\" action=\"$self\" method=\"post\">\n";
14 echo " <p>Title<br>\n<input type=\"text\" name=\"add_title\">\n";
15 if ($config['use_categories']) {
16 echo " <p>Category<br>\n";
17 echo " <select name=\"add_category\">\n";
18 if (!mb_fetch_select_categories(2)) {
19 echo " <option value=\"\">ERROR</option>\n";
21 echo " </select>\n";
23 echo " <p>File<br>\n <input type=\"file\" name=\"add_file\">\n";
24 echo " <p><input type=\"submit\" value=\"Upload\">\n";
25 echo "</form>\n";
28 if (isset($_FILES['add_file']['name']) && is_uploaded_file($_FILES['add_file']['tmp_name'])) {
29 $file = file($_FILES['add_file']['tmp_name']);
30 $verstr = mb_chomp(array_shift($file));
31 if ($verstr != "MediaBase 1.2") {
32 echo "<p><b>Error:</b> Invalid header '$verstr'\n\n";
33 } else {
34 $media = array();
35 $media['files'] = array();
36 $media['tracks'] = array();
37 $media['created'] = time();
38 $media['info1'] = 0;
39 $media['info2'] = 0;
40 $errors = array();
41 $lineno = 1;
42 echo "<p>Parsing ".basename($_FILES['add_file']['name'])."...\n\n";
43 foreach ($file as $line) {
44 $lineno++;
45 $line = mb_chomp($line);
46 // remove comments and empty lines
47 if (preg_match('/^\s*(#.*?)?$/',$line)) { continue; }
49 list($key,$param) = explode("\t",$line,2);
50 switch (strtolower($key)) {
51 case 'title': $media['title'] = $param; break;
52 case 'type': switch(strtolower($param)) {
53 case 'data': $media['type'] = MB_T_DATA; break;
54 case 'audio': $media['type'] = MB_T_AUDIO; break;
55 case 'emtpy': $media['type'] = MB_T_EMPTY; break;
56 default: $errors[] = "Invalid type '$param' on line $lineno";
58 break;
59 case 'created': if (is_numeric($param)) {
60 $media['created'] = $param;
61 } else {
62 $errors[] = "Invalid timestamp '$param' on line $lineno";
64 break;
65 case 'info': list($info1,$info2) = explode("\t",$param,2);
66 if (is_numeric($info1) && is_numeric($info2)) {
67 $media['info1'] = $info1;
68 $media['info2'] = $info2;
69 } else {
70 $errors[] = "Invalid infoline '$param' on line $lineno";
72 break;
73 case 'track': if ($media['type'] != MB_T_AUDIO) {
74 $errors[] = "Invalid media type for 'track' on line $lineno";
75 } else {
76 list($track,$length,$name) = explode("\t",$param,3);
77 if (!is_numeric($track)) {
78 $errors[] = "Invalid track number '$track' for track on line $lineno";
80 if (!is_numeric($length)) {
81 $errors[] = "Invalid length '$length' for track on line $lineno";
83 $media['tracks'][] = array('track'=>$track,'length'=>$length,'name'=>$name);
85 break;
86 case 'file': if ($media['type'] != MB_T_DATA) {
87 $errors[] = "Invalid media type for 'file' on line $lineno";
88 } else {
89 list($size,$timestamp,$path,$filename) = explode("\t",$param,4);
90 if (!is_numeric($size)) {
91 $errors[] = "Invalid size '$size' for file on line $lineno";
93 if (!is_numeric($timestamp)) {
94 $errors[] = "Invalid timestamp '$timestamp' for file on line $lineno";
96 $media['files'][] = array('size'=>$size,'timestamp'=>$timestamp,'path'=>$path,'filename'=>$filename);
98 break;
99 default: $errors[] = "Invalid directive '$key' on line $lineno";
102 if (count($errors) > 0) {
103 echo "<p><b>Errors occured:</b>\n\n";
104 echo "<ul>\n";
105 foreach ($errors as $error) {
106 echo " <li>$error\n";
108 echo "</ul>\n";
109 echo "Media was not added.\n";
110 } else {
111 if (isset($_POST['add_category']) && is_numeric($_POST['add_category'])) {
112 $category = $_POST['add_category'];
113 } else {
114 $category = 0;
116 if (isset($_POST['add_title']) && strlen($_POST['add_title']) > 0) {
117 $media['title'] = $_POST['add_title'];
119 $query = "INSERT INTO ".$config['tbl_media']." VALUES (NULL,$category,";
120 $query .= $media['type'].','.$media['created'].','.$media['info1'].','.$media['info2'];
121 $query .= ",'".addslashes($media['title'])."','')";
122 if (!$res = mysql_query($query)) {
123 echo "<b>Error:</b> ".mysql_error()." <i>(".basename(__FILE__).", line ".__LINE__.")</i>\n\n";
124 } else {
125 $mediaid = mysql_insert_id();
126 $queries = array();
127 if ($media['type'] == MB_T_AUDIO) {
128 foreach ($media['tracks'] as $track) {
129 $queries[] = "INSERT INTO ".$config['tbl_tracks']." VALUES (".$track['track'].",$mediaid,'".addslashes($track['name'])."',".$track['length'].")";
131 } elseif ($media['type'] == MB_T_DATA) {
132 foreach($media['files'] as $file) {
133 $queries[] = "INSERT INTO ".$config['tbl_files']." VALUES (NULL,$mediaid,".$file['size'].','.$file['timestamp'].",'".addslashes($file['path'])."','".addslashes($file['filename'])."')";
136 $error = false;
137 foreach ($queries as $query) {
138 if (!mysql_query($query)) {
139 echo "<b>Error:</b> ".mysql_error()." <i>(".basename(__FILE__).", line ".__LINE__.")</i>\n\n";
140 echo "Query: $query";
141 $error = true;
142 break;
145 if ($error) {
146 echo "<p><b>".$media['title']."</b> was <b>not</b> added.\n\n";
147 } else {
148 echo "<p><b>".$media['title']."</b> was added.\n\n";
155 showform();
157 require('inc.footer.php');