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