PATH:
home
/
letacommog
/
letaweb
/
admin
/
models
/
mainsite_models
<?php /* Active record for site builder */ class Site extends CActiveRecord { /** * Returns the static model of the specified AR class. * * @return CActiveRecord the static model class */ public static function model($className = __CLASS__) { return parent::model($className); } /** * @return string the associated database table name */ public function tableName() { $type = Yii::app()->params['website_type']; if ($type == 'template') { return '{{template}}'; } return '{{page}}'; } public function getUniqueString() { return 'page'.$this->id; } /** * @return array validation rules for model attributes. */ public function rules() { return array( array('name', 'length', 'max' => 100), array('name', 'required'), array('name', 'unique'), array('id, name, plan_id,db_id,display_name, template, blog,ecommerce,type, category,custom_domain, published', 'safe'), ); } public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array( 'page_template' => array(self::BELONGS_TO, 'Site', 'template'), 'plan' => array(self::BELONGS_TO, 'Plan', 'plan_id'), 'website_category' => array(self::BELONGS_TO, 'WebsiteCategory', 'category'), 'db' => array(self::BELONGS_TO, 'DbList', 'db_id'), 'domain' => array(self::HAS_MANY, 'Page_Domain', 'site_id'), ); } public $custom_domain; public function search() { $criteria = new CDbCriteria(); $criteria->compare('id', $this->id); $criteria->compare('name', $this->name, true); $criteria->compare('display_name', $this->display_name, true); $criteria->compare('template', $this->template); $criteria->compare('db_id', $this->db_id); $criteria->compare('plan_id', $this->plan_id); $criteria->compare('published', $this->published); $custom_domain = trim($this->custom_domain); if (strlen($custom_domain)) { $criteria->with = ( array( 'domain' => array( 'condition' => 'domain.name like :name', 'params' => array(':name' => '%'.$this->custom_domain.'%'), ), ) ); } $criteria->together = true; return new CActiveDataProvider('Site', array( 'sort' => array( 'defaultOrder' => 'date_added desc', ), 'criteria' => $criteria, )); } public function search_template() { $criteria = new CDbCriteria(); $criteria->compare('id', $this->id); $criteria->compare('is_template', 1); $criteria->compare('name', $this->name, true); $criteria->compare('display_name', $this->display_name, true); $criteria->compare('template', $this->template); $criteria->compare('db_id', $this->db_id); $criteria->compare('category', $this->category); $criteria->compare('plan_id', $this->plan_id); $criteria->compare('published', $this->published); $custom_domain = trim($this->custom_domain); if (strlen($custom_domain)) { $criteria->with = ( array( 'domain' => array( 'condition' => 'domain.name like :name', 'params' => array(':name' => '%'.$this->custom_domain.'%'), ), ) ); } $criteria->together = true; return new CActiveDataProvider('Site', array( 'sort' => array( 'defaultOrder' => 'date_added desc', ), 'criteria' => $criteria, )); } public function search_owner() { $criteria = new CDbCriteria(); $user = Yii::app()->user->id; // $criteria->join = ' JOIN tbl_pageuser ON tbl_pageuser.user_id='.$user.' and tbl_pageuser.page_id=t.id'; //$criteria->compare('id',$user); $criteria->compare('owner_id', $user); $criteria->compare('name', $this->name, true); $criteria->compare('display_name', $this->display_name, true); $criteria->compare('template', $this->template); return new CActiveDataProvider('Site', array( 'sort' => array( 'defaultOrder' => 'date_added desc', ), 'criteria' => $criteria, )); } public function beforeDelete() { $dir = Yii::getPathofAlias('webroot.websites.'.$this->name); $this->deleteDir($dir); $this->removeDbData(); return true; } function exportSite() { $mysql = DbMigration::exportData($this); $dir = Yii::getPathofAlias('webroot.websites.'.$this->name); $mysql_file = $dir.DIRECTORY_SEPARATOR."sql.sql"; $handle = fopen($mysql_file,"w"); fwrite($handle, $mysql); fclose($handle); //$archive_file_name = Yii::getPathofAlias('webroot.websites.'.$this->name).'.zip'; $archive_file_name = $this->name.'.zip'; $zip = $this->zipDir($archive_file_name, $dir, $dir); $zip->close(); header("Content-type: application/zip"); header("Content-Disposition: attachment; filename=$archive_file_name"); header("Pragma: no-cache"); header("Expires: 0"); readfile("$archive_file_name"); } public function afterDelete() { } public function copyDbData() { DbMigration::copyData($this->page_template, $this); } public function removeDbData() { DbMigration::removeData($this); } public function getDomain() { if (isset($this->domain) && sizeof($this->domain)) { $main_domain = $this->domain[0]; return $main_domain->name; } return $this->name.'.'.Yii::app()->params['mainsite_url']; } public function getEditSiteURL() { return 'http://'.$this->getDomain().'/admin.php'; } public function getSiteURL() { return 'http://'.$this->getDomain(); } public function getPhysicalLocation() { return Yii::app()->url->get_base_absolute_path(); } public function getFilePath($rpath, $filename, $extension = '.php') { $extension = trim($extension, '.'); $extension = '.'.$extension; $dir = $this->getPhysicalLocation(); $targetDir = $dir.DIRECTORY_SEPARATOR.$rpath; if (!file_exists($targetDir)) { @mkdir($targetDir); } $filePath = $targetDir.DIRECTORY_SEPARATOR.$filename.$extension; return $filePath; } public function getUploadSize() { return $this->asset_size; } protected function getPlanConditionValue($key) { if (isset($this->plan)) { $conditions = $this->plan->getConditionIndexed(); if (isset($conditions[$key])) { return $conditions[$key]; } } return false; } public function getMaxNumberofPages() { $mb = $this->getPlanConditionValue('page_count'); if ($mb === false) { $mb = 0; } return $mb; } public function getMaxNumberofDomains() { $mb = $this->getPlanConditionValue('domain_count'); if ($mb === false) { $mb = 0; } return $mb; } public function getMaxNumberofContributors() { $mb = $this->getPlanConditionValue('contributor_count'); if ($mb === false) { $mb = 0; } return $mb; } public function getMaxNumberofPosts() { $mb = $this->getPlanConditionValue('blog_post_count'); if ($mb === false) { $mb = 0; } return $mb; } public function getMaxUploadSize() { $mb = $this->getPlanConditionValue('disk_space'); if ($mb === false) { $mb = 0; } return $mb * 1024 * 1024 * 1024; } public function updateUploadSize($size) { $this->asset_size = $size; $this->save(); } //function to write content to file located on site root folder public function writeFile($path, $filename, $content, $extension = '.php') { $filePath = $this->getFilePath($path, $filename, $extension); $out = fopen($filePath, 'wb'); fwrite($out, $content); fclose($out); return true; } //function to append content to file located on site root folder public function writeToEndFile($path, $filename, $content, $extension = '.php') { $filePath = $this->getFilePath($path, $filename, $extension); $out = fopen($filePath, 'a'); fwrite($out, $content); fclose($out); return true; } //function to delete file located on site root folder public function deleteFile($path, $filename, $extension = '.php') { $filePath = $this->getFilePath($path, $filename, $extension); if (file_exists($filePath)) { @unlink($filePath); } return true; } public function importZip($zipPath) { $zip = new ZipArchive; if ($zip->open($zipPath) === TRUE) { $dir = Yii::getPathofAlias('webroot.websites.'.$this->name); if (file_exists($dir)) { $this->deleteDir($dir); } @mkdir($dir); $zip->extractTo($dir); $zip->close(); $this->importSql($dir.DIRECTORY_SEPARATOR."sql.sql"); return true; } else { return false; } } protected function importSql($sqlPath) { $pdo = Yii::app()->db->getPdoInstance(); try { if (file_exists($sqlPath)) { $csql = ""; $this->removeDbData(); $sqlStream = file_get_contents($sqlPath); $sqlStream = rtrim($sqlStream); $newStream = preg_replace_callback("/\((.*)\)/", create_function('$matches', 'return str_replace(";"," $$$ ",$matches[0]);'), $sqlStream); $sqlArray = explode(';', $newStream); foreach ($sqlArray as $value) { if (!empty($value)) { $sql = str_replace(' $$$ ', ';', $value).';'; $sql = str_replace('{{{%site_id%}}}', $this->id, $sql).';'; $csql = $sql; $pdo->exec($sql); } } //echo "succeed to import the sql data!"; return true; } else { throw new Exception('file doesn\'t exists'); } } catch (PDOException $e) { throw new Exception($e->getMessage(). " ". $newStream); // exit; } } protected function zipDir($archive_file_name,$relativeDir, $dirPath,&$zip=NULL) { if(is_null($zip)) { $zip = new ZipArchive(); if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) { exit("cannot open <$archive_file_name>\n"); } } if (!file_exists($dirPath)) { return true; } if (!is_dir($dirPath)) { throw new InvalidArgumentException("$dirPath must be a directory"); } if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') { $dirPath .= '/'; } $files = glob($dirPath.'*', GLOB_MARK); foreach ($files as $file) { if (is_dir($file)) { self::zipDir($archive_file_name, $relativeDir,$file,$zip); } else { $relativePath = substr($file, strlen($relativeDir) + 1); $relativePath = str_replace(array("/", "\\"), "/", $relativePath); $zip->addFile($file, $relativePath); } } return $zip; } //function to delete dir located on site root folder protected function deleteDir($dirPath) { if (!file_exists($dirPath)) { return true; } if (!is_dir($dirPath)) { throw new InvalidArgumentException("$dirPath must be a directory"); } if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') { $dirPath .= '/'; } $files = glob($dirPath.'*', GLOB_MARK); foreach ($files as $file) { if (is_dir($file)) { self::deleteDir($file); } else { unlink($file); } } $h_files = glob($dirPath.'.*', GLOB_MARK); foreach ($h_files as $file) { if (is_dir($file)) { self::deleteDir($dirPath.$file); } else { unlink($file); } } rmdir($dirPath); } //function to check if file is existed on site root folder public function checkFileExist($path, $filename, $extension = '.php') { $filePath = $this->getFilePath($path, $filename, $extension); return file_exists($filePath); } public function deleteModules($modules) { foreach ($modules as $module) { $this->deleteFile('module_data', $module, '.json'); } } public function isExpired() { return $this->is_template == 0 && $this->can_expire == 1 && ($this->countDateLeft() < 0); } public function countDateLeft() { if (!$this->can_expire || $this->is_template == 1) { return -1; } $expired_date = $this->expired_date; $today = new DateTime(); $appt = new DateTime($expired_date); if ($appt < $today) { return -1; } $days_until_appt = $appt->diff($today)->days; return $days_until_appt; } public function getExpiredDate() { if (!$this->can_expire || $this->is_template == 1) { return 'Unlimited'; } return date('Y-m-d', strtotime($this->expired_date)); } protected function _extend($dstring) { $current = $this->expired_date; $today = new DateTime(); $appt = new DateTime($current); $days_until_appt = $appt->diff($today)->days; $date_added = 0; if ($today < $appt) { $date_added += $days_until_appt; } $date = date('Y-m-d', strtotime('+ '.$dstring)); $date = date($date, strtotime('+ '.$date_added.'day')); return $this->setExpiredDate($date); } protected function _reduce($dstring) { $date = date('Y-m-d', strtotime('- '.$dstring)); // $date = date($date, strtotime('+ '.$date_added.'day')); return $this->setExpiredDate($date); } public function reduce($date_number) { return $this->_reduce($date_number.' day'); } public function reduceByMonth($month) { $ms = $month > 1 ? 'months' : 'month'; return $this->_reduce($month.' '.$ms); } public function reduceByYear($year) { $ms = $year > 1 ? 'years' : 'year'; return $this->_reduce($year.' '.$ms); } public function extend($date_number) { return $this->_extend($date_number.' day'); } public function extendByMonth($month) { $ms = $month > 1 ? 'months' : 'month'; return $this->_extend($month.' '.$ms); } public function extendByYear($year) { $ms = $year > 1 ? 'years' : 'year'; return $this->_extend($year.' '.$ms); } public function setExpiredDate($date) { $this->expired_date = $date; return $this->save(); } public function changePackage($type, $bill) { $current_type = $this->type; $change = strcmp($current_type, $type) == 0 ? false : true; $this->upgrade($type); $date_number = $bill == 'month' ? 30 : 365; if ($change) { $date = date('Y-m-d', strtotime('+ '.$date_number.' day')); $this->setExpiredDate($date); } else { $this->extend($date_number); } } public function getConfiguration($key, $default = null) { $module = new SqlliteModule($this); $db = $module->getDbConnection(); $command = $db->createCommand(); $rs = SqlliteConfigurations::model()->find('name=:key', array(':key' => $key)); if (isset($rs)) { return $rs->value; } else { return $default; } } public function getViewSiteURL() { return 'site/view_page'; } protected function optimizeCSS($css, $modules) { $arr = array(); if (sizeof($modules) == 0) { $arr = array_merge($arr, $css); return $arr; } for ($i = 0; $i < sizeof($css);++$i) { $cs = $css[$i]; $selector = $cs['selector']; $flag = false; foreach ($modules as $m) { $sp = strpos($selector, $m); if ($sp !== false) { $flag = true; break; } } if ($flag === true) { $arr[] = $cs; } } return $arr; } public function clearPageData($page_name) { $valid_modules = $this->getModules($page_name); $skin_css_file_path = $this->getFilePath('css', 'skin', '.css'); $skin_css = Functions::readCSSContentFromFile($skin_css_file_path); $removed_skin_css = $this->removeCSS($skin_css, $valid_modules); $skin_css_skin_file_path = $this->getFilePath('css', 'skin', '.css_skin'); $skin_css_skin = Functions::readCSSContentFromFile($skin_css_skin_file_path); $removed_skin_css_skin = $this->removeCSS($skin_css_skin, $valid_modules); $skin_css_code = Functions::getCSSTextFormArray($removed_skin_css); $skin_css_skin_code = Functions::getCSSTextFormArray($removed_skin_css_skin); $this->writeFile('css', 'skin', $skin_css_code, '.css'); $this->writeFile('css', 'skin', $skin_css_skin_code, '.css_skin'); return $valid_modules; } public function optimize() { $module = new SqlliteModule($this); $db = $module->getDbConnection(); $command = $db->createCommand(); $rs = $db->createCommand()->select('*')->from('pages')->queryAll(); $valid_modules = array(); $header_modules = $this->getModules('_header'); $footer_modules = $this->getModules('_footer'); $valid_modules = array_merge($valid_modules, $header_modules, $footer_modules); foreach ($rs as $p) { $page_name = $p['file_name']; $pmodules = $this->getModules($page_name); $valid_modules = array_merge($valid_modules, $pmodules); } $bm = new BaseModule(0, 0, 0, 0, $this); $skin_css_file_path = $this->getFilePath('css', 'skin', '.css'); $skin_css = $bm->readCSSContentFromFile($skin_css_file_path); $optimize_skin_css = $this->optimizeCSS($skin_css, $valid_modules); $skin_css_skin_file_path = $this->getFilePath('css', 'skin', '.css_skin'); $skin_css_skin = $bm->readCSSContentFromFile($skin_css_skin_file_path); $optimize_skin_css_skin = $this->optimizeCSS($skin_css_skin, $valid_modules); return sizeof($skin_css).':'.sizeof($optimize_skin_css); } public function getBillingMethod() { $subscription = Subscription::model()->find('site_id=:site_id and is_active=1', array(':site_id' => $this->id)); return $subscription; } }
[+]
..
[-] Ticket.php
[edit]
[-] Configuration.php
[edit]
[-] PlanCondition.php
[edit]
[-] ModuleCategory.php
[edit]
[-] SamplePageCategory.php
[edit]
[-] Page_Type.php
[edit]
[-] Zone_Templates.php
[edit]
[-] User_Data.php
[edit]
[-] PlanDescription.php
[edit]
[-] UserData.php
[edit]
[-] Purchase.php
[edit]
[-] WebUser.php
[edit]
[-] Term.php
[edit]
[-] Type.php
[edit]
[-] Template.php
[edit]
[-] Site.php
[edit]
[-] Payment.php
[edit]
[+]
forms
[-] Language.php
[edit]
[-] AuthItem.php
[edit]
[-] MailTemplate.php
[edit]
[-] TicketForm.php
[edit]
[-] ModuleData.php
[edit]
[-] UserIdentity.php
[edit]
[-] Popup_Script.php
[edit]
[-] UserRecoverPassword.php
[edit]
[-] HelpCategory.php
[edit]
[-] Color.php
[edit]
[-] PlanAddition.php
[edit]
[-] Plan.php
[edit]
[-] IPNLogMessage.php
[edit]
[-] Subscription.php
[edit]
[-] SamplePage.php
[edit]
[-] ModuleList.php
[edit]
[-] User.php
[edit]
[-] Page_Domain.php
[edit]
[-] Payment_Method.php
[edit]
[-] YiiLog.php
[edit]
[-] User_Session.php
[edit]
[-] SkinPreset.php
[edit]
[-] Page_Installation.php
[edit]
[-] SiteData.php
[edit]
[-] Content.php
[edit]
[-] Library_Script.php
[edit]
[-] WebsiteCategory.php
[edit]