77239666 by Marty Penner

Promoting r252-r254 to live. refs #1249

1 parent d1749c3b
1 <?php
2 namespace Tz\Wordpress\Tools\ExcelReader;
3
4 define('NUM_BIG_BLOCK_DEPOT_BLOCKS_POS', 0x2c);
5 define('SMALL_BLOCK_DEPOT_BLOCK_POS', 0x3c);
6 define('ROOT_START_BLOCK_POS', 0x30);
7 define('BIG_BLOCK_SIZE', 0x200);
8 define('SMALL_BLOCK_SIZE', 0x40);
9 define('EXTENSION_BLOCK_POS', 0x44);
10 define('NUM_EXTENSION_BLOCK_POS', 0x48);
11 define('PROPERTY_STORAGE_BLOCK_SIZE', 0x80);
12 define('BIG_BLOCK_DEPOT_BLOCKS_POS', 0x4c);
13 define('SMALL_BLOCK_THRESHOLD', 0x1000);
14 // property storage offsets
15 define('SIZE_OF_NAME_POS', 0x40);
16 define('TYPE_POS', 0x42);
17 define('START_BLOCK_POS', 0x74);
18 define('SIZE_POS', 0x78);
19 define('IDENTIFIER_OLE', pack("CCCCCCCC",0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1));
20
21 //echo 'ROOT_START_BLOCK_POS = '.ROOT_START_BLOCK_POS."\n";
22
23 //echo bin2hex($data[ROOT_START_BLOCK_POS])."\n";
24 //echo "a=";
25 //echo $data[ROOT_START_BLOCK_POS];
26 //function log
27
28 function GetInt4d($data, $pos)
29 {
30 $value = ord($data[$pos]) | (ord($data[$pos+1]) << 8) | (ord($data[$pos+2]) << 16) | (ord($data[$pos+3]) << 24);
31 if ($value>=4294967294)
32 {
33 $value=-2;
34 }
35 return $value;
36 }
37
38
39 class OLERead {
40 var $data = '';
41
42
43 function OLERead(){
44
45
46 }
47
48 function read($sFileName){
49
50 // check if file exist and is readable (Darko Miljanovic)
51 if(!is_readable($sFileName)) {
52 $this->error = 1;
53 return false;
54 }
55
56 $this->data = @file_get_contents($sFileName);
57 if (!$this->data) {
58 $this->error = 1;
59 return false;
60 }
61 //echo IDENTIFIER_OLE;
62 //echo 'start';
63 if (substr($this->data, 0, 8) != IDENTIFIER_OLE) {
64 $this->error = 1;
65 return false;
66 }
67 $this->numBigBlockDepotBlocks = GetInt4d($this->data, NUM_BIG_BLOCK_DEPOT_BLOCKS_POS);
68 $this->sbdStartBlock = GetInt4d($this->data, SMALL_BLOCK_DEPOT_BLOCK_POS);
69 $this->rootStartBlock = GetInt4d($this->data, ROOT_START_BLOCK_POS);
70 $this->extensionBlock = GetInt4d($this->data, EXTENSION_BLOCK_POS);
71 $this->numExtensionBlocks = GetInt4d($this->data, NUM_EXTENSION_BLOCK_POS);
72
73 /*
74 echo $this->numBigBlockDepotBlocks." ";
75 echo $this->sbdStartBlock." ";
76 echo $this->rootStartBlock." ";
77 echo $this->extensionBlock." ";
78 echo $this->numExtensionBlocks." ";
79 */
80 //echo "sbdStartBlock = $this->sbdStartBlock\n";
81 $bigBlockDepotBlocks = array();
82 $pos = BIG_BLOCK_DEPOT_BLOCKS_POS;
83 // echo "pos = $pos";
84 $bbdBlocks = $this->numBigBlockDepotBlocks;
85
86 if ($this->numExtensionBlocks != 0) {
87 $bbdBlocks = (BIG_BLOCK_SIZE - BIG_BLOCK_DEPOT_BLOCKS_POS)/4;
88 }
89
90 for ($i = 0; $i < $bbdBlocks; $i++) {
91 $bigBlockDepotBlocks[$i] = GetInt4d($this->data, $pos);
92 $pos += 4;
93 }
94
95
96 for ($j = 0; $j < $this->numExtensionBlocks; $j++) {
97 $pos = ($this->extensionBlock + 1) * BIG_BLOCK_SIZE;
98 $blocksToRead = min($this->numBigBlockDepotBlocks - $bbdBlocks, BIG_BLOCK_SIZE / 4 - 1);
99
100 for ($i = $bbdBlocks; $i < $bbdBlocks + $blocksToRead; $i++) {
101 $bigBlockDepotBlocks[$i] = GetInt4d($this->data, $pos);
102 $pos += 4;
103 }
104
105 $bbdBlocks += $blocksToRead;
106 if ($bbdBlocks < $this->numBigBlockDepotBlocks) {
107 $this->extensionBlock = GetInt4d($this->data, $pos);
108 }
109 }
110
111 // var_dump($bigBlockDepotBlocks);
112
113 // readBigBlockDepot
114 $pos = 0;
115 $index = 0;
116 $this->bigBlockChain = array();
117
118 for ($i = 0; $i < $this->numBigBlockDepotBlocks; $i++) {
119 $pos = ($bigBlockDepotBlocks[$i] + 1) * BIG_BLOCK_SIZE;
120 //echo "pos = $pos";
121 for ($j = 0 ; $j < BIG_BLOCK_SIZE / 4; $j++) {
122 $this->bigBlockChain[$index] = GetInt4d($this->data, $pos);
123 $pos += 4 ;
124 $index++;
125 }
126 }
127
128 //var_dump($this->bigBlockChain);
129 //echo '=====2';
130 // readSmallBlockDepot();
131 $pos = 0;
132 $index = 0;
133 $sbdBlock = $this->sbdStartBlock;
134 $this->smallBlockChain = array();
135
136 while ($sbdBlock != -2) {
137
138 $pos = ($sbdBlock + 1) * BIG_BLOCK_SIZE;
139
140 for ($j = 0; $j < BIG_BLOCK_SIZE / 4; $j++) {
141 $this->smallBlockChain[$index] = GetInt4d($this->data, $pos);
142 $pos += 4;
143 $index++;
144 }
145
146 $sbdBlock = $this->bigBlockChain[$sbdBlock];
147 }
148
149
150 // readData(rootStartBlock)
151 $block = $this->rootStartBlock;
152 $pos = 0;
153 $this->entry = $this->__readData($block);
154
155 /*
156 while ($block != -2) {
157 $pos = ($block + 1) * BIG_BLOCK_SIZE;
158 $this->entry = $this->entry.substr($this->data, $pos, BIG_BLOCK_SIZE);
159 $block = $this->bigBlockChain[$block];
160 }
161 */
162 //echo '==='.$this->entry."===";
163 $this->__readPropertySets();
164
165 }
166
167 function __readData($bl) {
168 $block = $bl;
169 $pos = 0;
170 $data = '';
171
172 while ($block != -2) {
173 $pos = ($block + 1) * BIG_BLOCK_SIZE;
174 $data = $data.substr($this->data, $pos, BIG_BLOCK_SIZE);
175 //echo "pos = $pos data=$data\n";
176 $block = $this->bigBlockChain[$block];
177 }
178 return $data;
179 }
180
181 function __readPropertySets(){
182 $offset = 0;
183 //var_dump($this->entry);
184 while ($offset < strlen($this->entry)) {
185 $d = substr($this->entry, $offset, PROPERTY_STORAGE_BLOCK_SIZE);
186
187 $nameSize = ord($d[SIZE_OF_NAME_POS]) | (ord($d[SIZE_OF_NAME_POS+1]) << 8);
188
189 $type = ord($d[TYPE_POS]);
190 //$maxBlock = strlen($d) / BIG_BLOCK_SIZE - 1;
191
192 $startBlock = GetInt4d($d, START_BLOCK_POS);
193 $size = GetInt4d($d, SIZE_POS);
194
195 $name = '';
196 for ($i = 0; $i < $nameSize ; $i++) {
197 $name .= $d[$i];
198 }
199
200 $name = str_replace("\x00", "", $name);
201
202 $this->props[] = array (
203 'name' => $name,
204 'type' => $type,
205 'startBlock' => $startBlock,
206 'size' => $size);
207
208 if (($name == "Workbook") || ($name == "Book")) {
209 $this->wrkbook = count($this->props) - 1;
210 }
211
212 if ($name == "Root Entry") {
213 $this->rootentry = count($this->props) - 1;
214 }
215
216 //echo "name ==$name=\n";
217
218
219 $offset += PROPERTY_STORAGE_BLOCK_SIZE;
220 }
221
222 }
223
224
225 function getWorkBook(){
226 if ($this->props[$this->wrkbook]['size'] < SMALL_BLOCK_THRESHOLD){
227 // getSmallBlockStream(PropertyStorage ps)
228
229 $rootdata = $this->__readData($this->props[$this->rootentry]['startBlock']);
230
231 $streamData = '';
232 $block = $this->props[$this->wrkbook]['startBlock'];
233 //$count = 0;
234 $pos = 0;
235 while ($block != -2) {
236 $pos = $block * SMALL_BLOCK_SIZE;
237 $streamData .= substr($rootdata, $pos, SMALL_BLOCK_SIZE);
238
239 $block = $this->smallBlockChain[$block];
240 }
241
242 return $streamData;
243
244
245 }else{
246
247 $numBlocks = $this->props[$this->wrkbook]['size'] / BIG_BLOCK_SIZE;
248 if ($this->props[$this->wrkbook]['size'] % BIG_BLOCK_SIZE != 0) {
249 $numBlocks++;
250 }
251
252 if ($numBlocks == 0) return '';
253
254 //echo "numBlocks = $numBlocks\n";
255 //byte[] streamData = new byte[numBlocks * BIG_BLOCK_SIZE];
256 //print_r($this->wrkbook);
257 $streamData = '';
258 $block = $this->props[$this->wrkbook]['startBlock'];
259 //$count = 0;
260 $pos = 0;
261 //echo "block = $block";
262 while ($block != -2) {
263 $pos = ($block + 1) * BIG_BLOCK_SIZE;
264 $streamData .= substr($this->data, $pos, BIG_BLOCK_SIZE);
265 $block = $this->bigBlockChain[$block];
266 }
267 //echo 'stream'.$streamData;
268 return $streamData;
269 }
270 }
271
272 }
273 ?>
...\ No newline at end of file ...\ No newline at end of file
...@@ -24,19 +24,19 @@ function select_opts(Array $options, $selected = null, $echo = true) { ...@@ -24,19 +24,19 @@ function select_opts(Array $options, $selected = null, $echo = true) {
24 24
25 function select_opts_provinces($selected = null, $echo = true) { 25 function select_opts_provinces($selected = null, $echo = true) {
26 $return = "<option>Select a State/Province</option>"; 26 $return = "<option>Select a State/Province</option>";
27 27
28 $return .= '<optgroup label="Canada">'; 28 $return .= '<optgroup label="Canada">';
29 $return .= select_opts(Vars::$provinces['CA'], $selected, false); 29 $return .= select_opts(Vars::$provinces['CA'], $selected, false);
30 $return .= '</optgroup>'; 30 $return .= '</optgroup>';
31 31
32 $return .= '<optgroup label="United States">'; 32 $return .= '<optgroup label="United States">';
33 $return .= select_opts(Vars::$provinces['US'], $selected, false); 33 $return .= select_opts(Vars::$provinces['US'], $selected, false);
34 $return .= '</optgroup>'; 34 $return .= '</optgroup>';
35 35
36 $return .= '<optgroup label="Other">'; 36 $return .= '<optgroup label="Other">';
37 $return .= '<option value="other">Outside Canada/USA</option>'; 37 $return .= '<option value="other">Outside Canada/USA</option>';
38 $return .= '</optgroup>'; 38 $return .= '</optgroup>';
39 39
40 return $return; 40 return $return;
41 } 41 }
42 42
...@@ -99,11 +99,11 @@ function validateDate( $date, $format='YYYY-MM-DD') ...@@ -99,11 +99,11 @@ function validateDate( $date, $format='YYYY-MM-DD')
99 default: 99 default:
100 throw new Exception( "Invalid Date Format" ); 100 throw new Exception( "Invalid Date Format" );
101 } 101 }
102 102
103 return checkdate( $m, $d, $y ); 103 return checkdate( $m, $d, $y );
104 104
105 105
106 106
107 } 107 }
108 108
109 function validate_creditcard($cc_num, $type) { 109 function validate_creditcard($cc_num, $type) {
...@@ -114,7 +114,7 @@ function validate_creditcard($cc_num, $type) { ...@@ -114,7 +114,7 @@ function validate_creditcard($cc_num, $type) {
114 $verified = true; 114 $verified = true;
115 } else { 115 } else {
116 $verified = false; 116 $verified = false;
117 } 117 }
118 } elseif ($type == "mc") { 118 } elseif ($type == "mc") {
119 $pattern = "/^([51|52|53|54|55]{2})([0-9]{14})$/"; //Mastercard 119 $pattern = "/^([51|52|53|54|55]{2})([0-9]{14})$/"; //Mastercard
120 if (preg_match($pattern,$cc_num)) { 120 if (preg_match($pattern,$cc_num)) {
...@@ -130,7 +130,7 @@ function validate_creditcard($cc_num, $type) { ...@@ -130,7 +130,7 @@ function validate_creditcard($cc_num, $type) {
130 $verified = false; 130 $verified = false;
131 } 131 }
132 } 132 }
133 133
134 return $verified; 134 return $verified;
135 } 135 }
136 136
...@@ -190,7 +190,7 @@ if ( ! function_exists('form_dropdown')) ...@@ -190,7 +190,7 @@ if ( ! function_exists('form_dropdown'))
190 } 190 }
191 } 191 }
192 192
193 function form_linked_dropdown($name = '', $parent = '', $options = array(), $parent_selected = '', $selected = '', $extra = '') 193 function form_linked_dropdown($name = '', $parent = '', $options = array(), $parent_selected = '', $selected = '', $extra = '', $provide_js = true)
194 { 194 {
195 if ($extra != '') $extra = ' '.$extra; 195 if ($extra != '') $extra = ' '.$extra;
196 196
...@@ -204,23 +204,24 @@ function form_linked_dropdown($name = '', $parent = '', $options = array(), $par ...@@ -204,23 +204,24 @@ function form_linked_dropdown($name = '', $parent = '', $options = array(), $par
204 204
205 $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n"; 205 $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
206 } 206 }
207 } 207 }
208 208
209 $form .= '</select> 209 if ($provide_js) {
210 $form .= '</select>
210 <script> 211 <script>
211 function update_' . $name . '(value) { 212 function update_' . $name . '(value) {
212 213
213 if (value=="") { 214 if (value=="") {
214 return; 215 return;
215 } 216 }
216 217
217 jQuery("#'.$name.'_custom").attr("disabled","disabled").hide(); 218 jQuery("#' . $name . '_custom").attr("disabled","disabled").hide();
218 jQuery("#'.$name.'").show(); 219 jQuery("#' . $name . '").show();
219 220
220 221
221 var options = new Array(); 222 var options = new Array();
222 var select = document.getElementById("' . $name . '"); 223 var select = document.getElementById("' . $name . '");
223 select.disabled = false; 224 select.disabled = false;
224 while (select.firstChild) 225 while (select.firstChild)
225 select.removeChild(select.firstChild); 226 select.removeChild(select.firstChild);
226 switch (value.toString()) {'; 227 switch (value.toString()) {';
...@@ -230,41 +231,50 @@ function form_linked_dropdown($name = '', $parent = '', $options = array(), $par ...@@ -230,41 +231,50 @@ function form_linked_dropdown($name = '', $parent = '', $options = array(), $par
230 foreach ($v as $value => $content) { 231 foreach ($v as $value => $content) {
231 $form .= '"' . $value . '","' . $content . '",'; 232 $form .= '"' . $value . '","' . $content . '",';
232 } 233 }
233 $form = substr($form,0,-1); 234 $form = substr($form, 0, -1);
234 $form .= '); 235 $form .= ');
235 break;'; 236 break;';
236 } 237 }
237 $form .= 'default: 238 $form .= 'default:
238 select.disabled = true; 239 select.disabled = true;
239 240
240 jQuery("#'.$name.'").after("<input type=\'text\' name=\''.$name.'\' id=\''.$name.'_custom\' class=\'input-field-css input-large\' value=\''.$selected.'\' />").removeAttr("disabled").show(); 241 jQuery("#' . $name . '").after("<input type=\'text\' name=\'' . $name . '\' id=\'' . $name . '_custom\' class=\'input-field-css input-large\' value=\'' . $selected . '\' />").removeAttr("disabled").show();
241 jQuery("#'.$name.'").hide(); 242 jQuery("#' . $name . '").hide();
242 //options = new Array("other","Other"); 243 //options = new Array("other","Other");
243 return; 244 return;
244 } 245 }
245 246
246 var option = document.createElement("option"); 247 var option = document.createElement("option");
247 var val = ""; 248 var val = "";
248 option.val = val; 249 option.val = val;
249 option.innerHTML = "Select..."; 250 option.innerHTML = "Select...";
250 select.appendChild(option); 251 select.appendChild(option);
251 252
252 for(var i = 0; i < options.length;) { 253 for(var i = 0; i < options.length;) {
253 var option = document.createElement("option"); 254 var option = document.createElement("option");
254 var val = options[i++]; 255 var val = options[i++];
255 option.value = val; 256 option.value = val;
256 option.innerHTML = options[i++]; 257 option.innerHTML = options[i++];
257 if (val == "'.$selected.'") { 258 if (val == "' . $selected . '") {
258 option.selected = true; 259 option.selected = true;
259 } 260 }
260 select.appendChild(option); 261 select.appendChild(option);
261 } 262 }
262 263
263 } 264 }
264 update_'.$name.'(document.getElementById("'.$parent.'").value); 265 update_' . $name . '(document.getElementById("' . $parent . '").value);
265 document.getElementById("' . $parent . '").onchange = function() { update_' . $name . '(this.value); } 266 document.getElementById("' . $parent . '").onchange = function() { update_' . $name . '(this.value); }
266 </script> 267 </script>
267 '; 268 ';
269 } else {
270 $form = '';
271 if (array_key_exists($parent_selected, $options)) {
272 foreach ($options[$parent_selected] as $name => $option) {
273 $sel = ($selected == $name) ? ' selected="selected"' : '';
274 $form .= '<option value="' . $name . '"' . $sel . '>' . $option . "</option>\n";
275 }
276 }
277 }
268 278
269 return $form; 279 return $form;
270 } 280 }
...@@ -323,7 +333,7 @@ class Vars { ...@@ -323,7 +333,7 @@ class Vars {
323 , 'Dr.' => 'Dr.' 333 , 'Dr.' => 'Dr.'
324 , 'Prof.' => 'Prof.' 334 , 'Prof.' => 'Prof.'
325 ); 335 );
326 336
327 public static $company_types = Array( 337 public static $company_types = Array(
328 'advisory_firm' => "Advisory Firm" 338 'advisory_firm' => "Advisory Firm"
329 , 'litigation_firm' => "Litigation Firm" 339 , 'litigation_firm' => "Litigation Firm"
...@@ -340,7 +350,7 @@ class Vars { ...@@ -340,7 +350,7 @@ class Vars {
340 , 'mutual_fund_co' => "Mutual Fund Management Company" 350 , 'mutual_fund_co' => "Mutual Fund Management Company"
341 , 'merchant_bank' => "Merchant Bank" 351 , 'merchant_bank' => "Merchant Bank"
342 ); 352 );
343 353
344 public static $countries = Array( 354 public static $countries = Array(
345 'CA' => 'Canada' 355 'CA' => 'Canada'
346 , 'US' => 'United States' 356 , 'US' => 'United States'
...@@ -657,13 +667,13 @@ class Vars { ...@@ -657,13 +667,13 @@ class Vars {
657 , 'WY' => 'Wyoming' 667 , 'WY' => 'Wyoming'
658 ) 668 )
659 ); 669 );
660 670
661 public static $securities = Array( 671 public static $securities = Array(
662 'S01' => "What is your Mother's maiden name?" 672 'S01' => "What is your Mother's maiden name?"
663 , 'S02' => "Who was you childhood hero?" 673 , 'S02' => "Who was you childhood hero?"
664 , 'S03' => "What is/was the name of your first pet?" 674 , 'S03' => "What is/was the name of your first pet?"
665 ); 675 );
666 676
667 public static $creditcards = Array( 677 public static $creditcards = Array(
668 'visa' => "Visa" 678 'visa' => "Visa"
669 , 'mc' => "MasterCard" 679 , 'mc' => "MasterCard"
......