Cheetah
ChWsbExport.php
Go to the documentation of this file.
1 <?php
2 
8 ch_import('ChWsbExportQuery');
9 
29 {
30  protected $_aSystem = array ();
31  protected $_oQuery = null;
32  protected $_aTables = array();
33  protected $_sFilesBaseDir = '';
34  protected $_aTablesWithFiles = array();
35 
36  protected function __construct($aSystem)
37  {
38  $this->_aSystem = $aSystem;
39  $this->_oQuery = new ChWsbExportQuery($this->_aSystem);
40  }
41 
47  static public function getObjectInstance($sObject)
48  {
49  if (isset($GLOBALS['chWsbClasses']['ChWsbExport!'.$sObject]))
50  return $GLOBALS['chWsbClasses']['ChWsbExport!'.$sObject];
51 
53  if (!$aSystems || !isset($aSystems[$sObject]))
54  return false;
55 
56  $aObject = $aSystems[$sObject];
57 
58  if (!($sClass = $aObject['class_name']))
59  return false;
60 
61  if (!empty($aObject['class_file']))
62  require_once(CH_DIRECTORY_PATH_ROOT . $aObject['class_file']);
63  else
64  ch_import($sClass);
65 
66  $o = new $sClass($aObject);
67 
68  return ($GLOBALS['chWsbClasses']['ChWsbExport!'.$sObject] = $o);
69  }
70 
74  static public function & getSystems () {
75  if (!isset($GLOBALS['ch_dol_export_systems']))
76  $GLOBALS['ch_dol_export_systems'] = ChWsbExportQuery::getAllActiveSystemsFromCache ();
77  return $GLOBALS['ch_dol_export_systems'];
78  }
79 
83  static public function generateAllExports ($iProfileId)
84  {
85  $iProfileId = (int)$iProfileId;
87  return "Profile($iProfileId) doesn't exist";
88 
90  $aExports = array ();
91  foreach ($aSystems as $sSystem => $aSystem) {
92  if (!($o = self::getObjectInstance($sSystem)))
93  continue;
94 
95  $a = $o->export($iProfileId);
96  if (!empty($a['sql']) || !empty($a['files']))
97  $aExports[$sSystem] = $a;
98  }
99 
100  $sFileName = self::createZip($iProfileId, $aExports);
101  if (!$sFileName)
102  return 'Export zip file creation failed';
103 
104 
105  if (!self::sendEmailNotification($aProfile, $sFileName))
106  return "Send notification email to user($iProfileId) failed";
107 
108  return true;
109  }
110 
111  static public function sendEmailNotification($aProfile, $sFilename)
112  {
113  $oEmailTemplate = new ChWsbEmailTemplates();
114  $aTemplate = $oEmailTemplate->getTemplate('t_ExportReady', $aProfile['ID']);
115  $aTemplateVars = array (
116  'FileUrl' => CH_WSB_URL_CACHE_PUBLIC . $sFilename,
117  );
118  return sendMail($aProfile['Email'], $aTemplate['Subject'], $aTemplate['Body'], $aProfile['ID'], $aTemplateVars);
119  }
120 
121  static public function createZip($iProfileId, $aExports)
122  {
123  if (!class_exists('ZipArchive'))
124  return false;
125 
126  $sFileName = 'export-' . $iProfileId . '-' . $GLOBALS['site']['ver'] . '.' . $GLOBALS['site']['build'] . '-' . genRndPwd(8, false) . '.zip';
127  $sFilePath = CH_DIRECTORY_PATH_CACHE_PUBLIC . $sFileName;
128 
129  $oZip = new ZipArchive();
130  if ($oZip->open($sFilePath, ZipArchive::CREATE)!==TRUE)
131  return false;
132 
133  // collect data
134 
135  $sSqlDump = "-- Cheetah user data export\n";
136  $sSqlDump .= "-- Profile ID: $iProfileId\n";
137  $sSqlDump .= "-- Profile Username: " . getUsername($iProfileId) . "\n";
138  $sSqlDump .= "-- Cheetah Version: " . $GLOBALS['site']['ver'] . '.' . $GLOBALS['site']['build'] . "\n";
139  foreach ($aExports as $sSystem => $a) {
140  // sql
141  if (!empty($a['sql']))
142  $sSqlDump .= "\n\n-- " . $sSystem . "\n\n" . $a['sql'];
143 
144  // files
145  if (!empty($a['files']))
146  chdir(CH_DIRECTORY_PATH_ROOT);
147  foreach ($a['files'] as $sFile)
148  $oZip->addGlob($sFile);
149  }
150 
151  // add DB dump
152  $oZip->addFromString('/dump.sql', $sSqlDump);
153 
154  $oZip->close();
155 
156  return $sFileName;
157  }
158 
166  public function export ($iProfileId)
167  {
168  return array(
169  'sql' => $this->exportSQL($iProfileId),
170  'files' => $this->exportFiles($iProfileId),
171  );
172  }
173 
179  public function exportFiles($iProfileId)
180  {
181  $a = array();
182  foreach ($this->_aTablesWithFiles as $sTableName => $aFields)
183  $a = array_merge($a, $this->_getFiles($sTableName, $aFields, $iProfileId));
184  return $a;
185  }
186 
192  public function exportSQL($iProfileId)
193  {
194  $s = '';
195  foreach ($this->_aTables as $sTableName => $mixedCond)
196  $s .= $this->_getRows($sTableName, $mixedCond, $iProfileId);
197  return $s;
198  }
199 
200  protected function _getFiles($sTableName, $aFields, $iProfileId)
201  {
202  $mixedCond = $this->_aTables[$sTableName];
203  $sQuery = $this->_getQuery($sTableName, $mixedCond, $iProfileId);
204  $oStmt = $this->_oQuery->res($sQuery);
205  return $this->_getFilesFromStmt($sTableName, $oStmt, $aFields);
206  }
207 
208  protected function _getRows($sTableName, $mixedCond, $iProfileId)
209  {
210  $sQuery = $this->_getQuery($sTableName, $mixedCond, $iProfileId);
211  $oStmt = $this->_oQuery->res($sQuery);
212  return $this->_getRowsFromStmt($sTableName, $oStmt);
213  }
214 
215  protected function _getQuery($sTableName, $mixedCond, $iProfileId)
216  {
217  if (is_string($mixedCond)) {
218  $sWhere = str_replace('{profile_id}', $iProfileId, $mixedCond);
219  $sQuery = "SELECT * FROM `$sTableName` WHERE $sWhere";
220  }
221  elseif (is_array($mixedCond) && isset($mixedCond['query'])) {
222  $sQuery = str_replace('{profile_id}', $iProfileId, $mixedCond['query']);
223  }
224  return $sQuery;
225  }
226 
227  protected function _getFilePath($sTableName, $sField, $sFileName, $sPrefix, $sExt)
228  {
229  return $this->_sFilesBaseDir . (is_string($sPrefix) ? $sPrefix : '') . $sFileName . $sExt;
230  }
231 
232  protected function _getFilesFromStmt($sTableName, $oStmt, $aFields)
233  {
234  if (!$oStmt->rowCount())
235  return array();
236 
237  $aFiles = array();
238  while ($r = $oStmt->fetch(PDO::FETCH_ASSOC)) {
239  if(is_a($aFields, 'ChWsbExportFiles'))
240  $aFields->perform($r, $aFiles);
241  else
242  foreach ($aFields as $sField => $aPrefix2Ext) {
243  foreach ($aPrefix2Ext as $sPrefix => $sExt) {
244  $sPath = $this->_getFilePath($sTableName, $sField, $r[$sField], $sPrefix, $sExt);
245  if (file_exists($sPath))
246  $aFiles[] = $sPath;
247  }
248  }
249  }
250 
251  return $aFiles;
252  }
253 
254  protected function _getRowsFromStmt($sTableName, $oStmt)
255  {
256  if (!$oStmt->rowCount())
257  return '';
258 
259  $s .= "INSERT INTO `{$sTableName}` VALUES\n";
260  while ($r = $oStmt->fetch(PDO::FETCH_NUM)) {
261  $s .= "(";
262  for ($j = 0; $j < count($r); $j++ ) {
263  if (is_null($r[$j]))
264  $s .= "NULL, ";
265  else
266  $s .= $this->_oQuery->escape($r[$j], true) . ", ";
267  }
268  $s = trim($s, ', ');
269  $s .= "),\n";
270  }
271  $s = trim($s, ",\n");
272  $s .= ";\n\n";
273  return $s;
274  }
275 }
276 
278 {
279  protected $_sBaseDir;
280 
281  public function __construct($sBaseDir)
282  {
283  $this->_sBaseDir = $sBaseDir;
284  }
285 
286  public function perform($aRow, &$aFiles) {}
287 }
ChWsbExport\$_aTables
$_aTables
array of tables for export, where key is table name and value can be string with condition (example: ...
Definition: ChWsbExport.php:32
TRUE
URI MungeSecretKey $secret_key</pre >< p > If the output is TRUE
Definition: URI.MungeSecretKey.txt:17
ChWsbExport\createZip
static createZip($iProfileId, $aExports)
Definition: ChWsbExport.php:121
ChWsbExportFiles\perform
perform($aRow, &$aFiles)
Definition: ChWsbExport.php:286
$aSystems
$aSystems
Definition: cmts.php:20
ChWsbExport\exportSQL
exportSQL($iProfileId)
Definition: ChWsbExport.php:192
ChWsbEmailTemplates
Definition: ChWsbEmailTemplates.php:11
ch_import
ch_import($sClassName, $aModule=array())
Definition: utils.inc.php:1218
ChWsbExport\_getFilePath
_getFilePath($sTableName, $sField, $sFileName, $sPrefix, $sExt)
Definition: ChWsbExport.php:227
ChWsbExport
Definition: ChWsbExport.php:29
sendMail
sendMail( $sRecipientEmail, $sMailSubject, $sMailBody, $iRecipientID=0, $aPlus=array(), $sEmailFlag='html', $isDisableAlert=false, $bForceSend=false)
Definition: utils.inc.php:461
ChWsbExport\__construct
__construct($aSystem)
Definition: ChWsbExport.php:36
ChWsbExport\$_sFilesBaseDir
$_sFilesBaseDir
base dir for files
Definition: ChWsbExport.php:33
php
ChWsbExport\$_oQuery
$_oQuery
Definition: ChWsbExport.php:31
$sExt
$sExt
Definition: get_file.php:14
ChWsbExport\$_aSystem
$_aSystem
current export system array
Definition: ChWsbExport.php:30
ChWsbExport\getObjectInstance
static getObjectInstance($sObject)
Definition: ChWsbExport.php:47
ChWsbExport\_getFilesFromStmt
_getFilesFromStmt($sTableName, $oStmt, $aFields)
Definition: ChWsbExport.php:232
ChWsbExportQuery
Definition: ChWsbExportQuery.php:14
ChWsbExportFiles\__construct
__construct($sBaseDir)
Definition: ChWsbExport.php:281
$sFile
$sFile
Definition: index.php:20
$aFields
$aFields
Definition: preValues.php:19
ChWsbExport\exportFiles
exportFiles($iProfileId)
Definition: ChWsbExport.php:179
$aProfile
$aProfile
Definition: flash.php:14
ChWsbExport\generateAllExports
static generateAllExports($iProfileId)
Definition: ChWsbExport.php:83
ChWsbExportFiles\$_sBaseDir
$_sBaseDir
Definition: ChWsbExport.php:279
ChWsbExport\sendEmailNotification
static sendEmailNotification($aProfile, $sFilename)
Definition: ChWsbExport.php:111
ChWsbExport\_getFiles
_getFiles($sTableName, $aFields, $iProfileId)
Definition: ChWsbExport.php:200
getUsername
getUsername( $ID='')
Definition: profiles.inc.php:443
genRndPwd
genRndPwd($iLength=8, $bSpecialCharacters=true)
Definition: utils.inc.php:1618
ChWsbExportQuery\getAllActiveSystemsFromCache
static getAllActiveSystemsFromCache()
Definition: ChWsbExportQuery.php:23
$s
$s
Definition: embed.php:13
ChWsbExport\_getRows
_getRows($sTableName, $mixedCond, $iProfileId)
Definition: ChWsbExport.php:208
getProfileInfo
getProfileInfo($iProfileID=0, $checkActiveStatus=false, $forceCache=false)
Definition: profiles.inc.php:249
ChWsbExportFiles
Definition: ChWsbExport.php:278
ChWsbExport\_getQuery
_getQuery($sTableName, $mixedCond, $iProfileId)
Definition: ChWsbExport.php:215
ChWsbExport\getSystems
static & getSystems()
Definition: ChWsbExport.php:74
ChWsbExport\$_aTablesWithFiles
$_aTablesWithFiles
array of tables with files, where key is table name and value is array of fields, where key is field ...
Definition: ChWsbExport.php:34
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
$o
$o
Definition: cmd.php:193
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
ChWsbExport\export
export($iProfileId)
Definition: ChWsbExport.php:166
ChWsbExport\_getRowsFromStmt
_getRowsFromStmt($sTableName, $oStmt)
Definition: ChWsbExport.php:254
$iProfileId
if( $sMembersList) $iProfileId
Definition: communicator.php:29
$GLOBALS
$GLOBALS['iAdminPage']
Definition: advanced_settings.php:10