2 include_once ('../../globals.php');
5 if ($_POST['export']) {
7 if ($temp === false) {echo "<h1>" . xl("failed") . "</h1>";}
9 $query1 = "select id, category from ".mitigateSqlTableUpperCase("form_CAMOS_category");
10 $statement1 = sqlStatement($query1);
11 while ($result1 = sqlFetchArray($statement1)) {
12 $tmp = $result1['category'];
13 $tmp = "<category>$tmp</category>"."\n";
15 $query2 = "select id,subcategory from ".mitigateSqlTableUpperCase("form_CAMOS_subcategory")." where category_id=".$result1['id'];
16 $statement2 = sqlStatement($query2);
17 while ($result2 = sqlFetchArray($statement2)) {
18 $tmp = $result2['subcategory'];
19 $tmp = "<subcategory>$tmp</subcategory>"."\n";
21 $query3 = "select item, content from ".mitigateSqlTableUpperCase("form_CAMOS_item")." where subcategory_id=".$result2['id'];
22 $statement3 = sqlStatement($query3);
23 while ($result3 = sqlFetchArray($statement3)) {
24 $tmp = $result3['item'];
25 $tmp = "<item>$tmp</item>"."\n";
27 $tmp = preg_replace(array("/\n/","/\r/"),array("\\\\n","\\\\r"),$result3['content']);
28 $tmp = "<content>$tmp</content>"."\n";
34 header("Pragma: public");
36 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
37 header("Content-Type: text/plain");
38 header("Content-Disposition: attachment; filename=\"CAMOS_export.txt\"");
44 if ($_POST['import']) {
48 foreach($_FILES as $file) {
49 $fname = $file['tmp_name'];
50 // echo "<p>tmp filename: ".$file['tmp_name']."</p>";
52 $handle = @fopen
($fname,"r");
53 if ($handle === false) {
54 echo "<h1>" . xl('Error opening uploaded file for reading') . "</h1>";
63 while (!feof($handle)) {
64 $buffer = fgets($handle);
65 if (preg_match('/<category>(.*?)<\/category>/',$buffer,$matches)) {
67 $category = add_escape_custom(trim($matches[1])); //trim in case someone edited by hand and added spaces
68 $statement = sqlStatement("select id from ".mitigateSqlTableUpperCase("form_CAMOS_category")." where category like \"$category\"");
69 if ($result = sqlFetchArray($statement)) {
70 $category_id = $result['id'];
72 $query = "INSERT INTO ".mitigateSqlTableUpperCase("form_CAMOS_category")." (user, category) ".
73 "values ('".$_SESSION['authUser']."', \"$category\")";
75 $statement = sqlStatement("select id from ".mitigateSqlTableUpperCase("form_CAMOS_category")." where category like \"$category\"");
76 if ($result = sqlFetchArray($statement)) {
77 $category_id = $result['id'];
81 if (preg_match('/<subcategory>(.*?)<\/subcategory>/',$buffer,$matches)) {
83 $subcategory = add_escape_custom(trim($matches[1]));
84 $statement = sqlStatement("select id from ".mitigateSqlTableUpperCase("form_CAMOS_subcategory")." where subcategory " .
85 "like \"$subcategory\" and category_id = $category_id");
86 if ($result = sqlFetchArray($statement)) {
87 $subcategory_id = $result['id'];
89 $query = "INSERT INTO ".mitigateSqlTableUpperCase("form_CAMOS_subcategory")." (user, subcategory, category_id) ".
90 "values ('".$_SESSION['authUser']."', \"$subcategory\", $category_id)";
92 $statement = sqlStatement("select id from ".mitigateSqlTableUpperCase("form_CAMOS_subcategory")." where subcategory " .
93 "like \"$subcategory\" and category_id = $category_id");
94 if ($result = sqlFetchArray($statement)) {
95 $subcategory_id = $result['id'];
99 if ((preg_match('/<(item)>(.*?)<\/item>/',$buffer,$matches)) ||
100 (preg_match('/<(content)>(.*?)<\/content>/s',$buffer,$matches))) {
103 $value = add_escape_custom(trim($matches[2]));
105 if ($mode == 'item') {
107 $statement = sqlStatement("select id from ".mitigateSqlTableUpperCase("form_CAMOS_item")." where item like \"$value\" " .
108 "and subcategory_id = $subcategory_id");
109 if ($result = sqlFetchArray($statement)) {//let's count until we find a number available
111 $inserted_duplicate = false;
112 while ($inserted_duplicate === false) {
113 $insert_value = $value."_".$postfix;
114 $inner_statement = sqlStatement("select id from ".mitigateSqlTableUpperCase("form_CAMOS_item")." ".
115 "where item like \"$insert_value\" " .
116 "and subcategory_id = $subcategory_id");
117 if (!($inner_result = sqlFetchArray($inner_statement))) {//doesn't exist
118 $inner_query = "INSERT INTO ".mitigateSqlTableUpperCase("form_CAMOS_item")." (user, item, subcategory_id) ".
119 "values ('".$_SESSION['authUser']."', \"$insert_value\", ".
121 sqlInsert($inner_query);
122 $inserted_duplicate = true;
126 $query = "INSERT INTO ".mitigateSqlTableUpperCase("form_CAMOS_item")." (user, item, subcategory_id) ".
127 "values ('".$_SESSION['authUser']."', \"$value\", $subcategory_id)";
130 if ($postfix == 0) {$insert_value = $value;}
131 $statement = sqlStatement("select id from ".mitigateSqlTableUpperCase("form_CAMOS_item")." where item like \"$insert_value\" " .
132 "and subcategory_id = $subcategory_id");
133 if ($result = sqlFetchArray($statement)) {
134 $item_id = $result['id'];
137 elseif ($mode == 'content') {
138 $statement = sqlStatement("select content from ".mitigateSqlTableUpperCase("form_CAMOS_item")." where id = ".$item_id);
139 if ($result = sqlFetchArray($statement)) {
140 //$content = "/*old*/\n\n".$result['content']."\n\n/*new*/\n\n$value";
145 $query = "UPDATE ".mitigateSqlTableUpperCase("form_CAMOS_item")." set content = \"$content\" where id = ".$item_id;
162 <?php
xl("Click 'export' to export your Category, Subcategory, Item, Content data to a text file. Any resemblance of this file to an XML file is purely coincidental. The opening and closing tags must be on the same line, they must be lowercase with no spaces. To import, browse for a file and click 'import'. If the data is completely different, it will merge with your existing data. If there are similar item names, The old one will be kept and the new one saved with a number added to the end.","e"); ?
>
163 <?php
xl("This feature is very experimental and not fully tested. Use at your own risk!","e"); ?
>
165 <form enctype
="multipart/form-data" method
="POST">
166 <input type
="hidden" name
="MAX_FILE_SIZE" value
="12000000" />
167 <?php
xl('Send this file','e'); ?
>: <input type
="file" name
="userfile"/>
168 <input type
="submit" name
="import" value
='<?php xl("Import","e"); ?>'/>
169 <input type
="submit" name
="export" value
='<?php xl("Export","e"); ?>'/>