Exemplo n.º 1
0
 /**
  * Кэширумеая функция, основной разбор.
  */
 public static function parse($src)
 {
     $args = array($src);
     $param = Cache::exec(array($src), 'docx_parse', function ($src, $re) {
         $conf = Docx::$conf;
         $imgmaxwidth = $conf['imgmaxwidth'];
         $previewlen = $conf['previewlen'];
         $cachename = Path::tofs(Path::encode($src));
         $cacheFolder = Path::mkdir(Docx::$conf['cache'] . $cachename . '/');
         //В винде ингда вылетает о шибка что нет прав удалить какой-то файл в папке и как следствие саму папку
         //Обновление страницы проходит уже нормально
         //Полагаю в линукс такой ошибки не будет хз почему возникает
         Cache::fullrmdir($cacheFolder);
         $path = Path::theme($src);
         if (!$path) {
             return array('html' => false);
         }
         $xmls = docx_getTextFromZippedXML($path, 'word/document.xml', $cacheFolder, $re);
         $rIds = array();
         $param = array('folder' => $cacheFolder, 'imgmaxwidth' => $imgmaxwidth, 'previewlen' => $previewlen, 'rIds' => $rIds);
         if ($xmls[0]) {
             $xmlar = docx_dom_to_array($xmls[0]);
             $xmlar2 = docx_dom_to_array($xmls[1]);
             foreach ($xmlar2['Relationships']['Relationship'] as $v) {
                 $rIds[$v['Id']] = $v['Target'];
             }
             $param['rIds'] = $rIds;
             $html = docx_each($xmlar, '\\infrajs\\doc\\docx_analyse', $param);
         } else {
             $param['rIds'] = array();
             $html = '';
         }
         $param['html'] = $html;
         return $param;
     }, $args, isset($_GET['re']));
     unset($param['rIds']);
     unset($param['type']);
     unset($param['imgmaxwidth']);
     unset($param['previewlen']);
     unset($param['isli']);
     unset($param['isul']);
     unset($param['imgnum']);
     unset($param['folder']);
     return $param;
 }
Exemplo n.º 2
0
function &xls_parseAll($path)
{
    $data = Cache::exec(array($path), 'xls_parseAll', function &($path) {
        $file = Path::theme($path);
        $data = array();
        if (!$file) {
            return $data;
        }
        $in = Load::srcInfo($path);
        if ($in['ext'] == 'xls') {
            require_once __DIR__ . '/excel_parser/oleread.php';
            require_once __DIR__ . '/excel_parser/reader.php';
            if (!$file) {
                return $data;
            }
            $d = new \Spreadsheet_Excel_Reader();
            $d->setOutputEncoding('utf-8');
            $d->read($file);
            Each::forr($d->boundsheets, function &($v, $k) use(&$d, &$data) {
                $data[$v['name']] =& $d->sheets[$k]['cells'];
                $r = null;
                return $r;
            });
        } elseif ($in['ext'] == 'csv') {
            $handle = fopen('php://memory', 'w+');
            fwrite($handle, Path::toutf(file_get_contents($file)));
            rewind($handle);
            $data = array();
            //Массив будет хранить данные из csv
            while (($line = fgetcsv($handle, 0, ";")) !== false) {
                //Проходим весь csv-файл, и читаем построчно. 3-ий параметр разделитель поля
                $data[] = $line;
                //Записываем строчки в массив
            }
            fclose($handle);
            foreach ($data as $k => $v) {
                foreach ($data[$k] as $kk => $vv) {
                    $vv = trim($vv);
                    if ($vv === '') {
                        unset($data[$k][$kk]);
                    } else {
                        $data[$k][$kk] = $vv;
                    }
                }
                if (!$data[$k]) {
                    unset($data[$k]);
                }
            }
            $data = array('list' => $data);
        } elseif ($in['ext'] == 'xlsx') {
            $cacheFolder = Path::resolve(Xlsx::$conf['cache']);
            //$cacheFolder .= Path::encode($path).'/';//кэш
            $cacheFolder .= md5($path) . '/';
            //кэш
            Cache::fullrmdir($cacheFolder, true);
            //удалить старый кэш
            $r = mkdir($cacheFolder);
            if (!$r) {
                echo '<pre>';
                throw new \Exception('Не удалось создать папку для кэша ' . $cacheFolder);
            }
            //разархивировать
            $zip = new \ZipArchive();
            $pathfs = Path::theme($path);
            if ((int) phpversion() > 6) {
                $zipcacheFolder = Path::tofs($cacheFolder);
                $zippathfs = Path::toutf($pathfs);
            } else {
                $zipcacheFolder = Path::tofs($cacheFolder);
                //Без кирилицы
                $zippathfs = Path::tofs($pathfs);
            }
            $r = $zip->open($zippathfs);
            if ($r === true) {
                $zip->extractTo($zipcacheFolder);
                $zip->close();
                $contents = simplexml_load_file($cacheFolder . 'xl/sharedStrings.xml');
                $contents = $contents->si;
                $workbook = simplexml_load_file($cacheFolder . 'xl/workbook.xml');
                $sheets = $workbook->sheets->sheet;
                $handle = opendir($cacheFolder . 'xl/worksheets/');
                $i = 0;
                $syms = array();
                while ($file = readdir($handle)) {
                    if ($file[0] == '.') {
                        continue;
                    }
                    $src = $cacheFolder . 'xl/worksheets/' . $file;
                    if (!is_file($src)) {
                        continue;
                    }
                    $files[] = $file;
                }
                closedir($handle);
                natsort($files);
                foreach ($files as $file) {
                    $src = $cacheFolder . 'xl/worksheets/' . $file;
                    $list = $sheets[$i];
                    ++$i;
                    $list = $list->attributes();
                    $list = (string) $list['name'];
                    $data[$list] = array();
                    $sheet = simplexml_load_file($cacheFolder . 'xl/worksheets/' . $file);
                    $rows = $sheet->sheetData->row;
                    foreach ($rows as $row) {
                        $attr = $row->attributes();
                        $r = (string) $attr['r'];
                        $data[$list][$r] = array();
                        $cells = $row->c;
                        foreach ($cells as $cell) {
                            if (!$cell->v) {
                                continue;
                            }
                            $attr = $cell->attributes();
                            if ($attr['t'] == 's') {
                                $place = (int) $cell->v;
                                if (isset($contents[$place]->r)) {
                                    $value = '';
                                    foreach ($contents[$place]->r as $con) {
                                        $value .= $con->t;
                                    }
                                } else {
                                    $value = $contents[$place]->t;
                                }
                            } else {
                                $value = $cell->v;
                                $value = (double) $value;
                            }
                            $attr = $cell->attributes();
                            $c = (string) $attr['r'];
                            //FA232
                            preg_match("/\\D+/", $c, $c);
                            $c = $c[0];
                            $syms[$c] = true;
                            $data[$list][$r][$c] = (string) $value;
                        }
                    }
                }
                $syms = array_keys($syms);
                natsort($syms);
                /*usort($syms,function($a,$b){
                			$la=strlen($a);
                			$lb=strlen($b);
                			if($la>$lb)return 1;
                			if($la<$lb)return -1;
                			if($a>$b)return 1;
                			if($a<$b)return -1;
                			return 0;
                		});*/
                $symbols = array();
                foreach ($syms as $i => $s) {
                    $symbols[$s] = $i + 1;
                }
                foreach ($data as $list => $listdata) {
                    foreach ($listdata as $row => $rowdata) {
                        $data[$list][$row] = array();
                        foreach ($rowdata as $cell => $celldata) {
                            $data[$list][$row][$symbols[$cell]] = $celldata;
                        }
                        if (!$data[$list][$row]) {
                            unset($data[$list][$row]);
                        }
                        //Пустые строки нам не нужны
                    }
                }
            }
            // Если что-то пошло не так, возвращаем пустую строку
            //return "";
            //собрать данные
        }
        return $data;
    }, array($path));
    return $data;
}