403Webshell
Server IP : 104.21.17.213  /  Your IP : 216.73.217.141
Web Server : Apache
System : Linux hosting01.arsenalhost.com 4.18.0-425.13.1.lve.el8.x86_64 #1 SMP Mon Feb 27 15:23:24 EST 2023 x86_64
User : corbizre ( 1013)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : ON
Directory :  /home/corbizre/gotofindhomes.com/components/Import/Controllers/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /home/corbizre/gotofindhomes.com/components/Import/Controllers/IndexController.php
<?php

/**
 * Documentation
 * @author leonid
 * @category
 * @package
 * @subpackage
 * @copyright  Copyright (c) 2005-2012 ITCrimea Ukraine Inc. (http://www.itcrimea.com)
 * @license
 */
class Import_IndexController extends Zend_Controller_Action {

    public function indexAction() {}

    /**
     * Import commercial real estate
     * @throws Zend_Exception 
     */
    public function commercialAction() {

        if (!defined('REAL_ESTATE_IMPORT'))
            throw new Exception('Unable to run import directly from browser');

        $cfgImport = System_Components::getComponentConfig('Import');

        ini_set('user_agent', $cfgImport->script->user_agent);
        ini_set('default_socket_timeout', $cfgImport->script->default_socket_timeout);
        if (!defined('IMPORT_ALL')) {
            set_time_limit($cfgImport->script->max_execution_time);
        } else {
            set_time_limit(432000); // 5 days
        }

        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $tblImportHouses = new Import_House_Table();
        $tblHouse = new RealEstate_House_Table();
        $tblHouseSpace = new RealEstate_House_Spaces_Table();
        $tblTypes = new RealEstate_Type_Table();
        $tblHouseSpacesTypes = new RealEstate_House_Spaces_Type_Table();
        $tblUseTypes = new RealEstate_UseType_Table();
        $tblConstruction = new RealEstate_Construction_Table();
        $tblSaleTerms = new RealEstate_House_Term_Table();
        $tblHouseFloors = new RealEstate_House_Floors_Table();
        $tblHouseTenants = new RealEstate_House_Tenant_Table();

        $objRetailType = $tblTypes->fetchRow($tblTypes->select()->where('Ownership = "Commercial"')->where('Name = "Retail"'));
        $objOfficeType = $tblTypes->fetchRow($tblTypes->select()->where('Ownership = "Commercial"')->where('Name = "Office"'));
        $objOtherType = $tblTypes->fetchRow($tblTypes->select()->where('Ownership = "Commercial"')->where('Name = "Commercial (Other)"'));

        $select = $tblImportHouses->select();
        $select->where('site_index <> 3 AND site_index <> 4');
        if (!defined('IMPORT_ALL')) {
            $select->where('last_update > ?', date('Y-m-d H:i:s', time() - $cfgImport->script->time_update));
            $select->where('last_update < ?', date('Y-m-d H:i:s'));
        }
        $select->order('last_update DESC');


        $limit_start = $cfgImport->load->start;
        $limit_len = $cfgImport->load->length;

        $select->limit($limit_len, $limit_start);
        
        $listHouses = $tblImportHouses->fetchAll($select);
        
        $error_iter = 0;

        while ($listHouses->count() > 0) {


            foreach ($listHouses as $currExternalHouse) {
                
                try {

                    $rel_house_select = $tblHouse->select();
                    $rel_house_select->where('ExternalID = ?', $currExternalHouse->url_md5);
                    $rel_house_select->where('ExternalSourceID = ?', RealEstate_House::EXTERNAL_HRPARS);
                    $objHouse = $tblHouse->fetchRow($rel_house_select);
                    
                    if (!is_object($objHouse)) {

                        //Add new house

                        $objHouse = $tblHouse->fetchNew();

                        if ($objHouse->importData($currExternalHouse)) {

                            $objHouse->save();

                            $objHouse->importType($currExternalHouse->type);

                            //get house main image
                            try {
                                if (!empty($currExternalHouse->image_path)) {
                                    $objHouse->importImage(null, $currExternalHouse->image_path);
                                } elseif (!empty($currExternalHouse->image_url)) {
                                    $upload_path = '';
                                    $protocol = substr($currExternalHouse->image_url, 0, 7);
                                    if ($protocol != 'http://' && $protocol != 'https:/') {
                                        $upload_path = 'http://' . $currExternalHouse->image_url;
                                    }
                                    if (!empty($upload_path))
                                        $objHouse->importImage(null, $upload_path);
                                }
                            } catch (Exception $e) {
                                LogSystem::addLog('RealEstate', 'import', 'commercial', $objHouse->ID, LogSystem::SEVERITY_CRIT, $e->getCode() . ': ' . $e->getMessage(), array());
                            }
                        } else {
                            LogSystem::addLog('RealEstate', 'import', 'commercial', $currExternalHouse->ID, LogSystem::SEVERITY_CRIT, 'Unable to import house '.$currExternalHouse->ID, serialize($currExternalHouse->toArray()));
                            continue;
                        }
                    } else {
                        if ($objHouse->isSame($currExternalHouse->toArray()))
                            continue;
                        //Update house info
                        if ($objHouse->importData($currExternalHouse)) {
                            $objHouse->save();
                        }
                    }

                    //check house additional images
                    $listAdditionalImages = $currExternalHouse->getAdditionalImages();
                    foreach ($listAdditionalImages as $currAditionalImage) {
                        $objHouse->importImage($currAditionalImage);
                    }

                    $listExternalHouseSpaces = $currExternalHouse->getSpaces();

                    $arrExistingSpaces = array();

                    foreach ($listExternalHouseSpaces as $currExternalSpace) {

                        $string_for_md5 = $currExternalSpace->house_id;

                        $str_area = trim($currExternalSpace->area);
                        $str_floor = trim($currExternalSpace->floor);
                        $str_suite = trim($currExternalSpace->suite);
                        $str_minimum_divisible = trim($currExternalSpace->minimum_divisible);

                        if (!empty($str_area))
                            $string_for_md5 .= $str_area;
                        if (!empty($str_floor))
                            $string_for_md5 .= $str_floor;
                        if (!empty($str_suite))
                            $string_for_md5 .= $str_suite;
                        if (!empty($str_minimum_divisible))
                            $string_for_md5 .= $str_minimum_divisible;

                        $externalHash = md5($string_for_md5);

                        $objSpace = $tblHouseSpace->fetchRow($tblHouseSpace->select()->where('ExternalHash = ?', $externalHash));

                        if (!is_object($objSpace)) {
                            $objSpace = $tblHouseSpace->createRow(array('House_ID' => $objHouse->ID, 'User_ID' => System_User::getAdminID(), 'ExternalHash' => $externalHash, 'DateAdded' => date('Y-m-d H:i:s')));
                        }
                        
                        $objSpace->Deleted = 0;

                        $objSpace->Description = (empty($currExternalSpace->description)) ? '' : (string) $currExternalSpace->description;

                        $objSpace->Description = trim($objSpace->Description . ' ' . (string) $currExternalSpace->notes);

                        if (!empty($str_suite))
                            $objSpace->Suite = ' Suite: ' . $str_suite . '.';

                        if (!empty($str_minimum_divisible))
                            $objSpace->MinDivisible = RealEstate_Size::trim($str_minimum_divisible)->Size;

                        if (!empty($currExternalSpace->max_contiguous))
                            $objSpace->MaxContiguous = RealEstate_Size::trim($currExternalSpace->max_contiguous)->Size;


                        if (!empty($currExternalSpace->date_available)) {
                            $date_parsed = strtotime($currExternalSpace->date_available);
                            $objSpace->DateAvailable = ($date_parsed > time()) ? date('Y-m-d', $date_parsed) : NULL;
                        }

                        if (!empty($currExternalSpace->floor)) {
                            $floor = trim(str_replace(array('st', 'st', 'nd', 'rd', 'th', 'd', 'Bldg.', 'Stor', 'Store'), array('', '', '', '', '', '', '', '', ''), $currExternalSpace->floor));
                            if (is_numeric($floor)) {
                                $objSpace->Floor = $floor;
                            } else {
                                $objSpace->Description .= ' Floor Details: ' . trim($currExternalSpace->floor);
                            }
                        }

                        $objSpace->Size = RealEstate_Size::trim($currExternalSpace->area)->Size;

                        //Parse price
                        $rent = str_replace(',', '', trim($currExternalSpace->rent));
                        $rent_price = '';
                        $price_per_size = NULL;
                        if (!empty($rent)) {
                            if (is_numeric($rent)) {
                                $rent_price = (strpos($currExternalHouse->url, 'mrofficespace')) ? $rent : 0;
                                $price_per_size = RealEstate_House_Spaces::RENTAL_RATE_PER_SF_MONTH;
                            } else {
                                $str_lower_rent = strtolower($rent);
                                if (strpos($str_lower_rent, 'monthly')) {
                                    if (strpos($str_lower_rent, 'psf') === false) {
                                        $price_per_size = RealEstate_House_Spaces::RENTAL_RATE_PER_AMT_MONTH;
                                    } else {
                                        $price_per_size = RealEstate_House_Spaces::RENTAL_RATE_PER_SF_MONTH;
                                    }
                                } elseif (strpos($str_lower_rent, 'annual')) {
                                    if (strpos($str_lower_rent, 'psf') === false) {
                                        $price_per_size = RealEstate_House_Spaces::RENTAL_RATE_PER_AMT_YEAR;
                                    } else {
                                        $price_per_size = RealEstate_House_Spaces::RENTAL_RATE_PER_SF_YEAR;
                                    }
                                }

                                $posDash = strpos($rent, '-');
                                $len = strlen($rent);
                                if ($posDash === false) {
                                    for ($iter = 0; $iter < $len; $iter++) {
                                        if ($rent[$iter] == '$') {
                                            continue;
                                        } elseif (is_numeric($rent[$iter]) || $rent[$iter] == '.') {
                                            $rent_price .= $rent[$iter];
                                        } else {
                                            break;
                                        }
                                    }
                                } else {
                                    $rent_price_low = '';
                                    for ($iter = 0; $iter < $posDash; $iter++) {
                                        if ($rent[$iter] == '$') {
                                            continue;
                                        } elseif (is_numeric($rent[$iter]) || $rent[$iter] == '.') {
                                            $rent_price_low .= $rent[$iter];
                                        } else {
                                            break;
                                        }
                                    }
                                    $rent_price_high = '';
                                    for ($iter = $posDash + 1; $iter < $len; $iter++) {
                                        if ($rent[$iter] == '$' || $rent[$iter] == ' ') {
                                            continue;
                                        } elseif (is_numeric($rent[$iter]) || $rent[$iter] == '.') {
                                            $rent_price_high .= $rent[$iter];
                                        } else {
                                            break;
                                        }
                                    }
                                    $rent_price = ($rent_price_low + $rent_price_high) / 2;

                                    if (!empty($price_per_size)) {
                                        $objSpace->Description .= " Rent details: $rent_price_low - $rent_price_high " . RealEstate_House_Spaces::getRentalRatePerSize($price_per_size) . '.';
                                    }
                                }

                                //Update Lease Type
                                if (strpos($str_lower_rent, 'industrial gross') != false) {
                                    $objSpace->LeaseType = RealEstate_House_Spaces::LeaseTypeIndustrialGross;
                                } elseif (strpos($str_lower_rent, 'modified gross') != false) {
                                    $objSpace->LeaseType = RealEstate_House_Spaces::LeaseTypeModifiedGross;
                                } elseif (strpos($str_lower_rent, 'nnn') != false) {
                                    $objSpace->LeaseType = RealEstate_House_Spaces::LeaseTypeNNN;
                                } elseif (strpos($str_lower_rent, 'other') != false) {
                                    $objSpace->LeaseType = RealEstate_House_Spaces::LeaseTypeOther;
                                }
                            }
                        }

                        $objSpace->Price = (empty($rent_price)) ? 0 : $rent_price;
                        $objSpace->SpacePricePerSize = (empty($price_per_size)) ? NULL : $price_per_size;
                        $objSpace->SpaceOfferType = RealEstate_House::OFFER_FOR_LEASE;

                        if (!empty($currExternalSpace->lease_term) && $currExternalSpace->lease_term != 'See Agent')
                            $objSpace->LeaseTerm = Import_House::fullTrim($currExternalSpace->lease_term);

                        if (is_numeric($currExternalSpace->conference_rooms))
                            $objSpace->ConferenceRooms = $currExternalSpace->conference_rooms;

                        $objSpace->OccupancyStatus = ($objHouse->Sold == RealEstate_House::SOLD) ? RealEstate_House_Spaces::STATUS_OCCUPIED : RealEstate_House_Spaces::STATUS_VACANT;

                        $objSpace->save();

                        if (!in_array($objSpace->ID, $arrExistingSpaces))
                            $arrExistingSpaces[] = $objSpace->ID;

                        $arrAddSpacesTypes = array();

                        if (strpos(strtolower($currExternalSpace->sublet_direct), 'office') !== false) {
                            $objSpaceType = $tblHouseSpacesTypes->fetchRow($tblHouseSpacesTypes->select()->where('Type_ID = ?', $objOfficeType->ID)->where('Space_ID = ?', $objSpace->ID));
                            if (!is_object($objSpaceType)) {
                                $objSpaceType = $tblHouseSpacesTypes->createRow(array('Type_ID' => $objOfficeType->ID, 'Space_ID' => $objSpace->ID));
                                $objSpaceType->save();
                            }
                            $arrAddSpacesTypes[] = $objSpaceType->Type_ID;
                        } elseif (strpos(strtolower($currExternalSpace->sublet_direct), 'retail') !== false) {
                            $objSpaceType = $tblHouseSpacesTypes->fetchRow($tblHouseSpacesTypes->select()->where('Type_ID = ?', $objRetailType->ID)->where('Space_ID = ?', $objSpace->ID));
                            if (!is_object($objSpaceType)) {
                                $objSpaceType = $tblHouseSpacesTypes->createRow(array('Type_ID' => $objRetailType->ID, 'Space_ID' => $objSpace->ID));
                                $objSpaceType->save();
                            }
                            $arrAddSpacesTypes[] = $objSpaceType->Type_ID;
                        } else {
                            $objSpaceType = $tblHouseSpacesTypes->fetchRow($tblHouseSpacesTypes->select()->where('Type_ID = ?', $objOtherType->ID)->where('Space_ID = ?', $objSpace->ID));
                            if (!is_object($objSpaceType)) {
                                $objSpaceType = $tblHouseSpacesTypes->createRow(array('Type_ID' => $objOtherType->ID, 'Space_ID' => $objSpace->ID));
                                $objSpaceType->save();
                            }
                            $arrAddSpacesTypes[] = $objSpaceType->Type_ID;
                        }

                        $objSpace->removeTypes($arrAddSpacesTypes);
                    }

                    $objHouse->cleanExternalSpaces($arrExistingSpaces);

                    //import use_types
                    $tblUseTypes->import($objHouse->ID, $currExternalHouse->use_type);

                    //import constructions
                    $tblConstruction->import($objHouse->ID, $currExternalHouse->construction);

                    //import sale_terms
                    $tblSaleTerms->import($objHouse->ID, $currExternalHouse->sale_terms);

                    //import house floors
                    $listExternalHouseFloors = $currExternalHouse->getFloors();
                    $floors_parsed = array();
                    foreach ($listExternalHouseFloors as $currExternalFloor) {

                        $size = array('Size' => NULL, 'Min' => NULL, 'Max' => NULL);

                        $sizepos = strpos($currExternalFloor->size, '-');
                        if ($sizepos) {
                            $min_size = substr($currExternalFloor->size, 0, $sizepos);
                            $max_size = substr($currExternalFloor->size, $sizepos + 1);

                            $min_size = RealEstate_Size::trim($min_size, 'sf')->Size;
                            $max_size = RealEstate_Size::trim($max_size, 'sf')->Size;

                            if (is_numeric($min_size) && is_numeric($max_size)) {
                                if ($min_size < $max_size) {
                                    $size['Min'] = $min_size;
                                    $size['Max'] = $max_size;
                                } else {
                                    $size['Min'] = $max_size;
                                    $size['Max'] = $min_size;
                                }
                            }
                        } else {
                            $size['Size'] = RealEstate_Size::trim($currExternalFloor->size, 'sf')->Size;
                        }

                        if (!(empty($size['Size']) && empty($size['Max']) && empty($size['Min']))) {

                            $pos = strpos($currExternalFloor->floors, '-');
                            if ($pos) {
                                $min = trim(substr($currExternalFloor->floors, 0, $pos));
                                $max = trim(substr($currExternalFloor->floors, $pos + 1));
                                if (is_numeric($min) && is_numeric($max)) {
                                    for ($l = 0; $l <= ($max - $min); $l++) {
                                        $floors_parsed[] = $min + $l;
                                    }
                                } else {
                                    $floors_parsed[] = trim($currExternalFloor->floors);
                                }
                            } else {
                                $floors_parsed[] = trim($currExternalFloor->floors);
                            }

                            foreach ($floors_parsed as $curr_floor) {
                                $curr_floor = trim($curr_floor);
                                if (!empty($curr_floor)) {
                                    $objHouseFloor = $tblHouseFloors->fetchRow($tblHouseFloors->select()->where('House_ID = ?', $objHouse->ID)->where('Floor = ?', $curr_floor));
                                    if (!is_object($objHouseFloor))
                                        $objHouseFloor = $tblHouseFloors->createRow(array('House_ID' => $objHouse->ID, 'Floor' => $curr_floor));

                                    $objHouseFloor->Size = $size['Size'];
                                    $objHouseFloor->Min = $size['Min'];
                                    $objHouseFloor->Max = $size['Max'];
                                    $objHouseFloor->save();
                                }
                            }
                        }
                    }

                    // import tenants
                    $listTenants = $currExternalHouse->findDependentRowset('Import_House_Tenant_Table');
                    foreach ($listTenants as $currTenant) {
                        $objTenant = $tblHouseTenants->fetchRow($tblHouseTenants->select()->where('House_ID = ?', $objHouse->ID)->where('Name = ?', $currTenant->name));
                        if (!is_object($objTenant)) {
                            $objTenant = $tblHouseTenants->createRow(array('House_ID' => $objHouse->ID, 'Name' => $currTenant->name, 'Type' => $currTenant->type));
                            $objTenant->save();
                        }
                    }
                } catch (Exception $e) {
                    $log = new Zend_Log(
                                    new Zend_Log_Writer_Stream(
                                            Zend_Registry::get('AppFolder') . '/logs/import-commercial.log'
                                    )
                    );
                    $strSelect = $select->assemble();
                    $log->emerg($e->getMessage() . ". Error - " . $error_iter . ".\r\n Stack trace: " . $e->getTraceAsString() . "\r\n Select:" . $strSelect . "\r\n External House Object: " . serialize($currExternalHouse->toArray()) . "\r\n\r\n");

                    $error_iter++;
                }
            }

            $limit_start = $limit_start + $limit_len;

            $select->limit($limit_len, $limit_start);


            // refresh mysql connection on server 
            $tblImportHouses = new Import_House_Table();

            $listHouses = $tblImportHouses->fetchAll($select);

            if ($error_iter > 10)
                throw new Zend_Exception('Connection failed. Too many exceptions. Try to run script later.');
        }
    }

    /**
     * Import residential real estate
     */
    public function residentialAction() {

        if (!defined('REAL_ESTATE_IMPORT'))
            throw new Exception('Unable to run import directly from browser');

        $cfgImport = System_Components::getComponentConfig('Import');

        ini_set('user_agent', $cfgImport->script->user_agent);
        ini_set('default_socket_timeout', $cfgImport->script->default_socket_timeout);
        if (!defined('IMPORT_ALL')) {
            set_time_limit($cfgImport->script->max_execution_time);
        } else {
            set_time_limit(432000); // 5 days
        }

        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $tblImportHouses = new Import_House_Table(false);
        $tblHouse = new RealEstate_House_Table();
        $tblConstruction = new RealEstate_Construction_Table();

        $select = $tblImportHouses->select();
        $select->where('true_state <> \'\'');
        $select->where('true_state IS NOT NULL');
        if (!defined('IMPORT_ALL')) {
            $select->where('last_update > ?', date('Y-m-d H:i:s', time() - $cfgImport->script->time_update));
            $select->where('last_update < ?', date('Y-m-d H:i:s'));
        }
        $select->order('last_update DESC');

        $limit_start = $cfgImport->load->start;
        $limit_len = $cfgImport->load->length;

        $select->limit($limit_len, $limit_start);

        $listHouses = $tblImportHouses->fetchAll($select);

        $error_iter = 0;

        while ($listHouses->count() > 0) {


            foreach ($listHouses as $currExternalHouse) {

                try {

                    $rel_house_select = $tblHouse->select();
                    $rel_house_select->where('ExternalID = ?', $currExternalHouse->url_md5);
                    $rel_house_select->where('ExternalSourceID = ?', RealEstate_House::EXTERNAL_HRPARS);
                    $objHouse = $tblHouse->fetchRow($rel_house_select);

                    if (!is_object($objHouse)) {

                        //Add new house

                        $objHouse = $tblHouse->fetchNew();

                        if ($objHouse->importData($currExternalHouse,RealEstate_House::OWNERSHIP_RESIDENTIAL)) {
                            
                            $objHouse->save();

                            $objHouse->importType($currExternalHouse->type);

                            //get house main image
                            try {
                                if (!empty($currExternalHouse->image_url)) {
                                    $objHouse->importImage(null, $currExternalHouse->image_url);
                                } elseif (!empty($currExternalHouse->image_url_orig)) {
                                    $upload_path = '';
                                    $protocol = substr($currExternalHouse->image_url_orig, 0, 7);
                                    if ($protocol != 'http://' && $protocol != 'https:/') {
                                        $upload_path = 'http://' . $currExternalHouse->image_url_orig;
                                    }
                                    if (!empty($upload_path))
                                        $objHouse->importImage(null, $upload_path);
                                }
                            } catch (Exception $e) {
                                LogSystem::addLog('RealEstate', 'import', 'residential', $objHouse->ID, LogSystem::SEVERITY_CRIT, $e->getCode() . ': ' . $e->getMessage(), array());
                            }
                        } else {
                            LogSystem::addLog('RealEstate', 'import', 'residential', $currExternalHouse->ID, LogSystem::SEVERITY_CRIT, 'Unable to import house '.$currExternalHouse->ID, serialize($currExternalHouse->toArray()));
                            continue;
                        }
                    } else {
                        //Update house info
                        if ($objHouse->isSame($currExternalHouse->toArray()))
                            continue;
                        if ($objHouse->importData($currExternalHouse,RealEstate_House::OWNERSHIP_RESIDENTIAL)) {
                            $objHouse->save();
                        }
                    }
                    
                    //import sewer
                    if (!empty($currExternalHouse->sewer_type)) {
                        $tblSewer = new RealEstate_Sewer_Table();
                        $tblHouseSewer = new RealEstate_House_Sewer_Table();
                        $arrSewer = explode(',',$currExternalHouse->sewer_type);
                        foreach($arrSewer as $currSewer) {
                            $currSewer = trim($currSewer);
                            $objSewer = $tblSewer->fetchRow($tblSewer->select()->where('Name = ?',$currSewer));
                            if (is_object($objSewer)) {
                                $objHouseSewer = $tblHouseSewer->fetchRow($tblHouseSewer->select()->where('House_ID = ?',$objHouse->ID)->where('Sewer_ID = ?',$objSewer->ID));
                                if (!is_object($objHouseSewer)) {
                                    $objHouseSewer = $tblHouseSewer->createRow(array('House_ID'=>$objHouse->ID,'Sewer_ID'=>$objSewer->ID));
                                    $objHouseSewer->save();
                                }
                            }
                        }
                    }
                    
                    //import utilities
                    if (!empty($currExternalHouse->utilities)) {
                        $tblUtility = new RealEstate_Utility_Table();
                        $tblHouseUtility = new RealEstate_House_Utility_Table();
                        $arrUtility = explode(',',$currExternalHouse->utilities);
                        foreach($arrUtility as $currUtility) {
                            $currUtility = trim($currUtility);
                            $objUtility = $tblUtility->fetchRow($tblUtility->select()->where('Name = ?',$currUtility));
                            if (is_object($objUtility)) {
                                $objHouseUtility = $tblHouseUtility->fetchRow($tblHouseUtility->select()->where('House_ID = ?',$objHouse->ID)->where('Utility_ID = ?',$objUtility->ID));
                                if (!is_object($objHouseUtility)) {
                                    $objHouseUtility = $tblHouseUtility->createRow(array('House_ID'=>$objHouse->ID,'Utility_ID'=>$objUtility->ID));
                                    $objHouseUtility->save();
                                }
                            }
                        }
                    }
                    
                    //import constructions
                    $tblConstruction->import($objHouse->ID, $currExternalHouse->exterior);
                    
                    //check house additional images
                    $listAdditionalImages = $currExternalHouse->getAdditionalImages(false);
                    foreach ($listAdditionalImages as $currAditionalImage) {
                        $objHouse->importImage($currAditionalImage);
                    }
//print_r($listAdditionalImages);
//exit;
                } catch (Exception $e) {
                    $log = new Zend_Log(
                                    new Zend_Log_Writer_Stream(
                                            Zend_Registry::get('AppFolder') . '/logs/import-residential.log'
                                    )
                    );
                    $strSelect = $select->assemble();
                    $log->emerg($e->getMessage() . ". Error - " . $error_iter . ".\r\n Stack trace: " . $e->getTraceAsString() . "\r\n Select:" . $strSelect . "\r\n External House Object: " . serialize($currExternalHouse->toArray()) . "\r\n\r\n");

                    $error_iter++;
                }
            }

            $limit_start = $limit_start + $limit_len;

            $select->limit($limit_len, $limit_start);


            // refresh mysql connection on server 
            $tblImportHouses = new Import_House_Table(false);

            $listHouses = $tblImportHouses->fetchAll($select);

            if ($error_iter > 10)
                throw new Zend_Exception('Connection failed. Too many exceptions. Try to run script later.');
        }
    }
    
    /**
     * Check images for existing houses
     */
    public function checkImagesAction() {
        
        if (!defined('REAL_ESTATE_IMPORT'))
            throw new Exception('Unable to run import directly from browser');
        
        $cfgImport = System_Components::getComponentConfig('Import');

        ini_set('user_agent', $cfgImport->script->user_agent);
        ini_set('default_socket_timeout', $cfgImport->script->default_socket_timeout);
        set_time_limit($cfgImport->script->max_execution_time);
        
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        
        $paramOwnership = $this->getRequest()->getParam('ownership');
        
        if ($paramOwnership == 'commercial') {
            $commercial = true;
        } else {
            $commercial = false;
        }
        
        $tblHouse = new RealEstate_House_Table();
        
        $tblImportImages = new Import_Image_Table($commercial);
        $tblImportImagesName = $tblImportImages->getTableName();
        $tblImportHouses = new Import_House_Table($commercial);
        $tblImportHousesName = $tblImportHouses->getTableName();
        
        $select = $tblImportImages->select();
        $select->from($tblImportImagesName);
        $select->setIntegrityCheck(false);
        $select->join($tblImportHousesName, $tblImportHousesName.'."ID" = '.$tblImportImagesName.'.house_id',array('url_md5'));
        
        $select->where($tblImportImagesName.'.last_update > ?',date('Y-m-d H:i:s', time() - 86400)); // 1 day
        
        if ($commercial) {
            $select->where($tblImportHousesName.'.site_index <> 3 AND '.$tblImportHousesName.'.site_index <> 4');
        } else {
            $select->where($tblImportHousesName.'.true_state <> \'\'');
            $select->where($tblImportHousesName.'.true_state IS NOT NULL');
        }
        
        $select->order(array($tblImportImagesName.'.last_update DESC','house_id'));
        
        $limit_start = $cfgImport->load->start;
        $limit_len = $cfgImport->load->length;

        $select->limit($limit_len, $limit_start);

        $listImages = $tblImportImages->fetchAll($select);
        
        $maxHouseImages = Settings::get('PhotosUploadLimit',10);

        while ($listImages->count() > 0) {

            $objHouse = false;
            $currHouseImages = 0;
            foreach ($listImages as $currExternalImage) {
                
                if (empty($currExternalImage->url_md5))
                    continue;
                
                if (!is_object($objHouse) || $currExternalImage->url_md5 != $objHouse->ExternalID) {
                    $objHouse = $tblHouse->fetchRow(
                            $tblHouse->select()->where('ExternalID = ?',$currExternalImage->url_md5)
                                ->where('ExternalSourceID = ?', RealEstate_House::EXTERNAL_HRPARS));
                    if (!is_object($objHouse)) {
                        continue;
                    } else {
                        $currHouseImages = 0;
                    }
                }
                
                if ($currHouseImages >= $maxHouseImages)
                    continue;
                    
                if ($objHouse->importImage($currExternalImage))
                    $currHouseImages++;

            }
            
            $limit_start = $limit_start + $limit_len;

            $select->limit($limit_len, $limit_start);

            // refresh mysql connection on server 
            $tblImportImages = new Import_Image_Table($commercial);

            $listImages = $tblImportImages->fetchAll($select);
        }
        
    }

    /*
     * Delete images where width = 0 or height = 0
     */
    public function deleteWrongSizeImagesAction() {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $tblImages = new Image_Table();
        $select = $tblImages->select();
        $select->where('Width = 0 OR Height = 0')->limit(500);
        $listImages = $tblImages->fetchAll($select);
        foreach ($listImages as $currImage) {
            $currImage->delete();
        }
    }

    /*
     * Remove images from db with missed files
     */
    public function deleteMissingImagesAction() {
        ini_set('max_execution_time', 200000);
        set_time_limit(200000);

        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $tblImages = new Image_Table();

        $select = $tblImages->select();

        $limit_start = 0;
        $limit_len = 100;

        $select->limit($limit_len, $limit_start);

        $listImages = $tblImages->fetchAll($select);

        while ($listImages->count() > 0) {
            foreach ($listImages as $currImage) {
                $file = Zend_Registry::get('AppFolder') . $currImage->Path . $currImage->File;
                if (!file_exists($file)) {
                    $currImage->delete();
                }
            }
            $limit_start += $limit_len;
            $select->limit($limit_len, $limit_start);
            unset($listImages);
            $listImages = $tblImages->fetchAll($select);
        }
    }
    
    public function fixWrongSizeImagesAction() {
        
        ini_set('max_execution_time', 86400);
        set_time_limit(86400);

        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $tblImages = new Image_Table();

        $select = $tblImages->select();
        $select->where('Width < 80 OR Height < 80');
        $select->where('Assign = "House"');

        $limit_start = 0;
        $limit_len = 100;

        $select->limit($limit_len, $limit_start);

        $listImages = $tblImages->fetchAll($select);

        while ($listImages->count() > 0) {
            foreach ($listImages as $currImage) {
                $file = Zend_Registry::get('AppFolder') . $currImage->Path . $currImage->File;
                if (file_exists($file)) {
                    echo $currImage->ID . ': ' . $currImage->Width . 'x' . $currImage->Height . ' -> ';
                    $Image = Image::create($file);
                    $currImage->Width = imagesx($Image);
                    $currImage->Height = imagesy($Image);
                    $currImage->save();
                    $currImage->deleteThumbs();
                    echo $currImage->Width . 'x' . $currImage->Height . "\n";
                } else {
                    $currImage->delete();
                }
            }
            $limit_start += $limit_len;
            $select->limit($limit_len, $limit_start);
            unset($listImages);
            $listImages = $tblImages->fetchAll($select);
        }
        
    }
    
    public function fromMongodbAction(){

        if (!defined('REAL_ESTATE_IMPORT'))
            throw new Exception('Unable to run import directly from browser');
        
        $cfgImport = System_Components::getComponentConfig('Import');
        set_time_limit($cfgImport->mongodb->max_execution_time);
        
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        
        $tblRealEstate = new RealEstate_House_Table();
        
        $logProgress = new Zend_Log(
            new Zend_Log_Writer_Stream(
                Zend_Registry::get('AppFolder') . '/logs/import-mongodb-progress/' . date('Y-m-d_H-i-s') . '.log'
            )
        );
        
        $logError = new Zend_Log(
            new Zend_Log_Writer_Stream(
                Zend_Registry::get('AppFolder') . '/logs/import-mongodb.log'
            )
        );
        
        $logProgress->emerg('Start mongodb import at ' . date('Y-m-d H:i:s'));
        
        try {
            $logProgress->emerg('Connect to MongoDb');
            //MongoCursor::$timeout = 300000; // 5 minutes
            $objClient = new MongoDB\Client('mongodb://' . $cfgImport->mongodb->host . ':' . $cfgImport->mongodb->port);
            $objDb = $objClient->{$cfgImport->mongodb->database};
            $logProgress->emerg('Successfully connected');
            
            $objCollectionHouse = $objDb->__get('houses');
            $objCollectionImages = $objDb->__get('images');
            Zend_Registry::set('CollectionImages', $objCollectionImages);
            $objCollectionLicensees = $objDb->__get('licensees');
            
            $page = 1;
            $limit = 500;
            $timestamp = time();
            $delay = 3600; // 1 hour
            
            if ($cfgImport->mongodb->import_all) {
                $arrFindCriteria = array(
                                    'status' => array('$in' => array(1, -1))
                                );
            } else {
                $arrFindCriteria = array(
                                    'status' => array('$in' => array(1, -1)),
                                    'updated_at' => array('$gt' => new MongoDB\BSON\UTCDateTime($timestamp - $cfgImport->mongodb->time_update - $delay)),
                                    'updated_at' => array('$lt' => new MongoDB\BSON\UTCDateTime($timestamp - $delay))
                                );
            }

            while (true) {
                $skip  = ($page - 1) * $limit;                
                $objCursor = $objCollectionHouse->find($arrFindCriteria)->skip($skip)->limit($limit)
                            ->sort(array('updated_at' => -1));
                
                if ($objCursor->count(true) == 0)
                    break;
                
                foreach ($objCursor as $arrDoc) {
                    
                    try {
                        $strExternalID = (string) $arrDoc["_id"];
                        $select = $tblRealEstate->select()->where('ExternalID = ?', $strExternalID);
                        $select->where('ExternalSourceID = ?', RealEstate_House::EXTERNAL_MONGO);
                        $objHouse = $tblRealEstate->fetchRow($select);
                        if (!is_object($objHouse)) {
                            $objHouse = $tblRealEstate->createRow();
                        } else {
                            if ($objHouse->isSame($arrDoc))
                                continue;
                        }
                        foreach ($arrDoc as $key => $val) {
                            if (gettype($val) == 'string')
                                $arrDoc[$key] = str_replace('(Sign In)', '', trim($val));
                        }
                        
                        if ($objHouse->importDataFromMongodb($arrDoc)) {
                            if ($objHouse->Sold != RealEstate_House::SOLD) {
                                //import agents
                                $AgentsInfo = array();
                                if (isset($arrDoc['licensee_ids'])) {
                                    foreach ($arrDoc['licensee_ids'] as $currLicenseeId) {
                                        $arrDocLicensee = $objCollectionLicensees->findOne(array('_id' => $currLicenseeId));
                                        if (is_array($arrDocLicensee)) {
                                            $newAgent = new Import_Agent();
                                            $newAgent->Name = isset($arrDocLicensee['name']) ? Import_House::fullTrim($arrDocLicensee['name']) : '';
                                            $newAgent->Address = isset($arrDocLicensee['address']) ? Import_House::fullTrim($arrDocLicensee['address']) : '';
                                            $newAgent->Phone = isset($arrDocLicensee['phone']) ? Import_House::fullTrim($arrDocLicensee['phone']) : '';
                                            $newAgent->Fax = isset($arrDocLicensee['fax']) ? Import_House::fullTrim($arrDocLicensee['fax']) : '';
                                            $newAgent->Email = isset($arrDocLicensee['email']) ? Import_House::fullTrim($arrDocLicensee['email']) : '';
                                            $AgentsInfo[] = $newAgent->toArray();
                                        }
                                    }
                                }
                                $objHouse->AgentsInfo = serialize($AgentsInfo);

                                $objHouse->save();

                                //import property types
                                $arrTypes = explode(',', $arrDoc['type']);
                                if (is_array($arrTypes)) {
                                    foreach ($arrTypes as $currType)
                                        $objHouse->importType(strtolower(trim($currType)));
                                } else {
                                    $objHouse->importType(strtolower(trim($arrTypes)));
                                }
                                
                                //try to import first image
                                if (isset($arrDoc['image_ids']) && is_array($arrDoc['image_ids']) && isset($arrDoc['image_ids'][0])) {
                                    if ($arrDoc['image_ids'][0] instanceof MongoId) {
                                        Import_Mongodb_Image::tryToImport($arrDoc['image_ids'][0], $objHouse->ID);
                                    }
                                }
                            }
                        } else {
                            // LogSystem::addLog('RealEstate', 'import', 'from-mongodb', $strExternalID, LogSystem::SEVERITY_CRIT, 'House ' . $arrDoc['_id'] . ' was not imported', serialize($arrDoc));
                        }
                    } catch (Exception $e) {
                        $msg = 'Fail to import house. Exception: ' . $e->getMessage() . "\n" . $e->getTraceAsString();
                        if (isset($arrDoc))
                            $msg .= "\n External House data: " . serialize($arrDoc);
                        $logError->emerg($msg);
                    }
                }
                
                $logProgress->emerg('Process page ' . $page . ' (' . ($page - 1) * $limit . ' - ' . $page * $limit . ' records) at ' . date('Y-m-d H:i:s'));
                
                $page++;
            }
            
            $logProgress->emerg('End mongodb import at ' . date('Y-m-d H:i:s'));

        } catch (Exception $e) {
            $msg = 'fromMongodbAction throws exception: ' . $e->getMessage() . "\n" . $e->getTraceAsString();
            $logProgress->emerg('Error: ' . $msg);
            if (isset($arrDoc))
                $msg .= "\n External House data: " . serialize($arrDoc);
            $logError->emerg($msg);
        }
        
    }
    
    public function checkMongodbImagesAction() {
        if (!defined('REAL_ESTATE_IMPORT'))
            throw new Exception('Unable to run import directly from browser');
        
        $time_start = time();
        $timelimit = 60 * 60 * 24 * 2; // 2 days
        set_time_limit($timelimit + 2000);
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        
        $cfgImport = System_Components::getComponentConfig('Import');
        
        //MongoCursor::$timeout = 300000; // 5 minutes
        $objClient = new MongoDB\Client('mongodb://' . $cfgImport->mongodb->host . ':' . $cfgImport->mongodb->port);
        $objDb = $objClient->{$cfgImport->mongodb->database};
        $objCollectionImages = $objDb->__get('images');
        $objCollectionHouses = $objDb->__get('houses');
        
        $tblHouses = new RealEstate_House_Table();
        
        $page = 1;
        $limit = 500;
        $timestamp = time();
        
        while (true) {
            $skip  = ($page - 1) * $limit;
            $cursorImages = $objCollectionImages->find(array('status' => 1, 'updated_at' => array('$gt' => new MongoDB\BSON\UTCDateTime($timestamp - $cfgImport->mongodb->time_update)),
                                        'updated_at' => array('$lt' => new MongoDB\BSON\UTCDateTime($timestamp))))->skip($skip)->limit($limit)
                                ->sort(array('updated_at' => -1));

            if ($cursorImages->count(true) == 0)
                break;

            if (time() - $time_start >= $timelimit) {
                break;
            }
            
            $arrImagesIds = array();
            foreach ($cursorImages as $currImage) {
                $arrImagesIds[] = $currImage['_id'];
            }

            unset($cursorImages);

            $cursorHouses = $objCollectionHouses->find(array('image_ids' => array('$in' => $arrImagesIds)));

            foreach ($cursorHouses as $currHouse) {
                if (is_array($currHouse) && isset($currHouse['image_ids'])) {
                    $objHouse = $tblHouses->fetchByExternalID((string) $currHouse['_id']);
                    if (!is_object($objHouse))
                        continue;
                    foreach ($currHouse['image_ids'] as $currImageId) {
                        if ($currImageId instanceof MongoId) {
                            $res = Import_Mongodb_Image::tryToImport($currImageId, $objHouse->ID);
                            // echo 'import image ' . $currImageId . ': ' . (($res) ? 'true' : 'false') . "\n";
                        } elseif (isset($currImageId['$each']) && is_array($currImageId['$each'])) {
                            foreach ($currImageId['$each'] as $subImageId) {
                                if ($subImageId instanceof MongoId) {
                                    $res = Import_Mongodb_Image::tryToImport($subImageId, $objHouse->ID);
                                    // echo 'import image ' . $subImageId . ': ' . (($res) ? 'true' : 'false') . "\n";
                                }
                            }
                        }
                    }
                }
            }

            unset($cursorHouses);
            
            $page++;
        }
        
    }
    
}

Youez - 2016 - github.com/yon3zu
LinuXploit