tools_list.php

Go to the documentation of this file.
00001 <?php
00002 
00009 class Tools_List extends oxAdminList
00010 {
00015     protected $_sThisTemplate = 'tools_list.tpl';
00016 
00022     public function performsql()
00023     {
00024         $oAuthUser = oxNew( 'oxuser' );
00025         $oAuthUser->loadAdminUser();
00026         if ( $oAuthUser->oxuser__oxrights->value != "malladmin" ) {
00027             return;
00028         }
00029 
00030         $sUpdateSQL = oxConfig::getParameter("updatesql");
00031         $sUpdateSQLFile = $this->_processFiles();
00032 
00033         if ( $sUpdateSQLFile && strlen( $sUpdateSQLFile)>0) {
00034             if ( isset( $sUpdateSQL) && strlen( $sUpdateSQL))
00035                 $sUpdateSQL .= ";\r\n".$sUpdateSQLFile;
00036             else
00037                 $sUpdateSQL  = $sUpdateSQLFile;
00038         }
00039 
00040         $sUpdateSQL = trim(stripslashes($sUpdateSQL));
00041         $iLen = strlen($sUpdateSQL);
00042         if ( $this->_prepareSQL(trim(stripslashes($sUpdateSQL)), $iLen)) {
00043             $aQueries = $this->aSQLs;
00044             $this->_aViewData["aQueries"] = array();
00045             $aPassedQueries  = array();
00046             $aQAffectedRows  = array();
00047             $aQErrorMessages = array();
00048             $aQErrorNumbers  = array();
00049 
00050             if ( count( $aQueries) > 0) {
00051                 $oDB = oxDb::getDb();
00052                 $iQueriesCounter = 0;
00053                 for ($i=0;$i<count( $aQueries);$i++) {
00054                     $sUpdateSQL = $aQueries[$i];
00055                     $sUpdateSQL = trim( $sUpdateSQL);
00056 
00057                     if ( strlen( $sUpdateSQL)>0) {
00058                         $aPassedQueries[$iQueriesCounter] = nl2br( htmlentities($sUpdateSQL));
00059                         if ( strlen( $aPassedQueries[$iQueriesCounter]) > 200)
00060                             $aPassedQueries[$iQueriesCounter] = substr( $aPassedQueries[$iQueriesCounter], 0, 200)."...";
00061 
00062                         while ( $sUpdateSQL[ strlen( $sUpdateSQL)-1] == ";") {
00063                             $sUpdateSQL = substr( $sUpdateSQL, 0, ( strlen( $sUpdateSQL)-1));
00064                         }
00065 
00066                         $oDB->execute( $sUpdateSQL);
00067 
00068                         $aQAffectedRows [$iQueriesCounter] = null;
00069                         $aQErrorMessages[$iQueriesCounter] = null;
00070                         $aQErrorNumbers [$iQueriesCounter] = null;
00071                         if ( $iAffectedRows = $oDB->affected_Rows() !== false) {
00072                             $aQAffectedRows[$iQueriesCounter] =  $iAffectedRows;
00073                         } else {
00074                             $aQErrorMessages[$iQueriesCounter] = htmlentities($oDB->errorMsg());
00075                             $aQErrorNumbers[$iQueriesCounter]  = htmlentities($oDB->errorNo());
00076                         }
00077                         $iQueriesCounter++;
00078                     }
00079                 }
00080             }
00081             $this->_aViewData["aQueries"]       = $aPassedQueries;
00082             $this->_aViewData["aAffectedRows"]  = $aQAffectedRows;
00083             $this->_aViewData["aErrorMessages"] = $aQErrorMessages;
00084             $this->_aViewData["aErrorNumbers"]  = $aQErrorNumbers;
00085         }
00086         $this->_iDefEdit = 1;
00087     }
00088 
00093     protected function _processFiles()
00094     {
00095         if ( isset( $_FILES['myfile']['name'])) {
00096             // process all files
00097             while (list($key, $value) = each($_FILES['myfile']['name'])) {
00098                 $aSource = $_FILES['myfile']['tmp_name'];
00099                 $sSource = $aSource[$key];
00100                 $aFiletype = explode( "@", $key);
00101                 $key    = $aFiletype[1];
00102                 $sType  = $aFiletype[0];
00103                 $value = strtolower( $value);
00104                 // add type to name
00105                 $aFilename = explode( ".", $value);
00106 
00107                 //hack?
00108 
00109                 $aBadFiles = array("php", "jsp", "cgi", "cmf", "exe");
00110 
00111                 if (in_array($aFilename[1], $aBadFiles))
00112                     die("We don't play this game, go away");
00113 
00114                 //reading SQL dump file
00115                 if ( $sSource) {
00116                     $rHandle   = fopen( $sSource, "r");
00117                     $sContents = fread( $rHandle, filesize ( $sSource));
00118                     fclose( $rHandle);
00119                     //reading only one SQL dump file
00120                     return $sContents;
00121                 }
00122                 return;
00123             }
00124         }
00125         return;
00126     }
00127 
00133     public function doimport()
00134     {
00135         $myConfig = $this->getConfig();
00136         $sFilepath = oxConfig::getParameter( "filepath");
00137         oxSession::setVar( "filepath", $sFilepath);
00138 
00139         $iStart = oxConfig::getParameter( "iStart");
00140         if ( !isset( $iStart))
00141             $iStart = 0;
00142 
00143         $oImex = oxNew( "oximex" );
00144 
00145         if ( !$oImex->import( $iStart, $myConfig->getConfigParam( 'iImportNrofLines' ), $sFilepath)) {
00146             oxSession::deleteVar( "imex_fnc");
00147             oxSession::deleteVar( "rStart");
00148             oxSession::deleteVar( "rparam");
00149             oxSession::deleteVar( "filepath");
00150             oxSession::deleteVar( "atables");
00151             oxSession::setVar( "finished", 2);
00152         } else {
00153              // continue
00154             $iStart += $myConfig->getConfigParam( 'iImportNrofLines' );
00155             oxSession::setVar( "rStart", $iStart);
00156             oxSession::setVar( "imex_fnc", "doimport");
00157         }
00158     }
00159 
00168     protected function _prepareSQL($sSQL, $iSQLlen)
00169     {
00170         $sChar = "";
00171         $sStrStart = "";
00172         $blString  = false;
00173 
00174         //removing "mysqldump" application comments
00175         while ( preg_match("/^\-\-.*\n/", $sSQL))
00176             $sSQL = trim(preg_replace("/^\-\-.*\n/", "", $sSQL));
00177         while ( preg_match("/\n\-\-.*\n/", $sSQL))
00178             $sSQL = trim(preg_replace("/\n\-\-.*\n/", "\n", $sSQL));
00179 
00180         for ( $iPos = 0; $iPos < $iSQLlen; ++$iPos) {
00181             $sChar = $sSQL[$iPos];
00182             if ( $blString) {
00183                 while ( true) {
00184                     $iPos = strpos( $sSQL, $sStrStart, $iPos);
00185                     //we are at the end of string ?
00186                     if (!$iPos) {
00187                         $this->aSQLs[] = $sSQL;
00188                         return true;
00189                     } elseif ( $sStrStart == '`' || $sSQL[$iPos-1] != '\\') { //found some query separators
00190                         $blString  = false;
00191                         $sStrStart = "";
00192                         break;
00193                     } else {
00194                         $iNext = 2;
00195                         $blBackslash = false;
00196                         while ( $iPos-$iNext > 0 && $sSQL[$iPos-$iNext] == '\\') {
00197                             $blBackslash = !$blBackslash;
00198                             $iNext++;
00199                         }
00200                         if ( $blBackslash) {
00201                             $blString  = false;
00202                             $sStrStart = "";
00203                             break;
00204                         } else
00205                             $iPos++;
00206                     }
00207                 }
00208             } elseif ( $sChar == ";") { // delimiter found, appending query array
00209                 $this->aSQLs[] = substr( $sSQL, 0, $iPos);
00210                 $sSQL = ltrim( substr( $sSQL, min( $iPos + 1, $iSQLlen)));
00211                 $iSQLlen = strlen( $sSQL);
00212                 if ( $iSQLlen)
00213                     $iPos      = -1;
00214                 else
00215                     return true;
00216             } elseif ( ( $sChar == '"') || ( $sChar == '\'') || ( $sChar == '`')) {
00217                 $blString  = true;
00218                 $sStrStart = $sChar;
00219             } elseif ( $sChar == "#" || ( $sChar == ' ' && $iPos > 1 && $sSQL[$iPos-2] . $sSQL[$iPos-1] == '--')) {  // removing # commented query code
00220                 $iCommStart = (( $sSQL[$iPos] == "#") ? $iPos : $iPos-2);
00221                 $iCommEnd = (strpos(' ' . $sSQL, "\012", $iPos+2))
00222                            ? strpos(' ' . $sSQL, "\012", $iPos+2)
00223                            : strpos(' ' . $sSQL, "\015", $iPos+2);
00224                 if ( !$iCommEnd) {
00225                     if ( $iCommStart > 0)
00226                         $this->aSQLs[] = trim(substr($sSQL, 0, $iCommStart));
00227                     return true;
00228                 } else {
00229                     $sSQL = substr($sSQL, 0, $iCommStart).ltrim(substr($sSQL, $iCommEnd));
00230                     $iSQLlen = strlen($sSQL);
00231                     $iPos--;
00232                 }
00233             } elseif ( 32358 < 32270 && ($sChar == '!' && $iPos > 1  && $sSQL[$iPos-2] . $sSQL[$iPos-1] == '/*'))  // removing comments like /**/
00234                 $sSQL[$iPos] = ' ';
00235         }
00236 
00237         if (!empty($sSQL) && ereg("[^[:space:]]+", $sSQL)) {
00238             $this->aSQLs[] = $sSQL;
00239         }
00240         return true;
00241     }
00242 }

Generated on Fri Dec 19 14:20:28 2008 for OXID eShop CE by  doxygen 1.5.5