Added remote printing support.
[fa-stable.git] / update_db.php
1 <?php
2 ini_set("display_errors", "on");
3 $path_to_root = ".";
4 include_once($path_to_root. "/admin/db/maintenance_db.inc");
5 include_once($path_to_root. "/includes/db/connect_db.inc");
6
7 $js = get_js_png_fix();
8 $js .= get_js_set_focus("user");
9 $image = $path_to_root."/themes/default/images/logo_frontaccounting.png";
10 $title = "Update All Company Databases";
11
12 function get_js_png_fix()
13 {
14         $js = "<script type=\"text/javascript\">\n"
15                 . "function fixPNG(myImage)\n"
16                 . "{\n"
17                 . " var arVersion = navigator.appVersion.split(\"MSIE\")\n"
18                 . " var version = parseFloat(arVersion[1])\n"
19         . " if ((version >= 5.5) && (version < 7) && (document.body.filters))\n"
20         . " {\n"
21         . "  var imgID = (myImage.id) ? \"id='\" + myImage.id + \"' \" : \"\"\n"
22                 . "  var imgClass = (myImage.className) ? \"class='\" + myImage.className + \"' \" : \"\"\n"
23                 . "  var imgTitle = (myImage.title) ?\n"
24                 . "    \"title='\" + myImage.title  + \"' \" : \"title='\" + myImage.alt + \"' \"\n"
25                 . "  var imgStyle = \"display:inline-block;\" + myImage.style.cssText\n"
26                 . "  var strNewHTML = \"<span \" + imgID + imgClass + imgTitle\n"
27         . "    + \" style=\\\"\" + \"width:\" + myImage.width\n"
28         . "    + \"px; height:\" + myImage.height\n"
29         . "    + \"px;\" + imgStyle + \";\"\n"
30         . "    + \"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader\"\n"
31         . "    + \"(src=\'\" + myImage.src + \"\', sizingMethod='scale');\\\"></span>\"\n"
32                 . "  myImage.outerHTML = strNewHTML\n"
33         . " }\n"
34                 . "}\n"
35                 . "</script>\n";
36         return $js;
37 }
38
39 function get_js_set_focus($name)
40 {
41         $js = "\n<script type=\"text/javascript\">\n"
42                 . "<!--\n"
43                 . "function setFocus()\n"
44                 . "{\n"
45                 . "     document.forms[0].$name.focus();\n"
46                 . "}\n"
47                 . "-->\n"
48                 . "</script>\n";
49         return $js;
50 }
51
52 function display_error($msg, $center=true)
53 {
54     echo "<center><table border='1' cellpadding='3' cellspacing='0' style='border-collapse: collapse' bordercolor='#CC3300' width='50%'>
55       <tr>
56         <td  " . ($center?"align='center' ":"") . " width='100%' bgcolor='#ffcccc'><font color='#dd2200'>$msg</font></td>
57       </tr>
58     </table></center><br>\n";
59 }
60
61 function display_notification($msg, $center=true)
62 {
63     echo "<center><table border='1' cellpadding='3' cellspacing='0' style='border-collapse: collapse' bordercolor='#33cc00' width='50%'>
64       <tr>
65         <td " . ($center?"align='center' ":"") . " width='100%' bgcolor='#ccffcc'><font color='#007700'>$msg</font></td>
66       </tr>
67     </table></center><br>\n";
68 }
69
70
71 function db_open($conn)
72 {
73         $db = mysql_connect($conn["host"] ,$conn["dbuser"], $conn["dbpassword"]);
74         if (!$db)
75                 return false;
76         if (!mysql_select_db($conn["dbname"], $db))
77                 return false;
78         return $db;
79 }
80
81 echo "<html dir='ltr' >\n";
82 echo "<head><title>$title</title>\n";
83 echo "<meta http-equiv='Content-type' content='text/html'; charset='iso-8859-1'>\n";
84 echo "<link href='$path_to_root/themes/default/default.css' rel='stylesheet' type='text/css' />\n";
85 echo $js;
86 echo "</head> \n";
87 echo "<body style='background-color:#f9f9f9;' onload='setFocus();'>";
88
89 echo "<br><br><br><br>";
90 echo "<table align='center' width='50%' cellpadding=3 border=1 bordercolor='#cccccc' style='border-collapse: collapse'>\n";
91 echo "<tr><td align='center' valign='bottom'><img src='$image' alt='FrontAccounting' width='250' height='50' onload='fixPNG(this)' border='0' /></td></tr>\n";
92 echo "</table>\n";
93
94 echo "<br><br>";
95 echo "<center><span class='headingtext'>$title</span></center>\n";
96 echo "<br>";
97
98 if (isset($_POST["submit"]))
99 {
100         if (!isset($_FILES['uploadfile']['tmp_name']) || !is_uploaded_file($_FILES['uploadfile']['tmp_name']))
101         {
102                 display_error("You must select an SQL script for update");;
103         }
104         else
105         {
106                 include_once($path_to_root."/config_db.php");
107                 if (!isset($_POST['user']) || !isset($_POST['passwd']) || $_POST['user'] == "")
108                 {
109                         display_error("You must select a user name and an optional password");
110                 }
111                 else
112                 {
113                         foreach($db_connections as $id => $conn)
114                         {
115                                 $conn['dbuser'] = $_POST['user'];
116                                 $conn['dbpassword'] = $_POST['passwd'];
117                                 if (!($db = db_open($conn)))
118                                 {
119                                         display_error("Wrong user name or password - ".mysql_error());
120                                 }
121                                 else
122                                 {
123                                         if (!db_import($_FILES['uploadfile']['tmp_name'], $conn))
124                                                 display_error("Bad SQL file or you have already updated the company: "
125                                                         . $id . " " . $conn['name']." - ".mysql_error());
126                                         else
127                                                 display_notification("Database has been updated for company: "
128                                                         . $id . " " .  $conn['name']);
129                                 }
130                         }
131                 }
132         }
133 }
134 if (!isset($_POST['passwd']))
135         $_POST['passwd'] = "";
136 if (!isset($_POST['user']))
137         $_POST['user'] = "";
138
139 echo "<form enctype='multipart/form-data' method='post' action='".$_SERVER['PHP_SELF']."'>\n";
140
141 echo "<table align='center' width='50%' cellpadding=3 border=1 bordercolor='#cccccc' style='border-collapse: collapse'>\n";
142
143 echo "<tr><td>Database User</td><td><input type='text' name='user' value='".$_POST['user']."' size='20' /></td></tr>";
144 echo "<tr><td>Password</td><td><input name='passwd' type='password' value='".$_POST['passwd']."' /></td></tr>";
145 echo "<tr><td>Upload Script</td><td><input name='uploadfile' type='file' /></td></tr>";
146 echo "<tr><td>&nbsp;</td><td><input type='submit' class='inputsubmit' name='submit' value='Update' /></td></tr>";
147
148 echo "</table>\n";;
149
150 echo "<br><br>";
151 echo "<center><span>Choose from Database update scripts in SQL folder. No Datase is updated without a script.</span></center>\n";
152 echo "<br>";
153
154 echo "</form>\n";
155
156 echo "</body></html>\n";
157 ?>