// Require the initialisation file
require_once '../../init.php';
// Required files
require_once MAX_PATH . '/lib/OA/Dal.php';
require_once MAX_PATH . '/www/admin/config.php';
require_once MAX_PATH . '/www/admin/lib-banner.inc.php';
require_once MAX_PATH . '/www/admin/lib-storage.inc.php';
// Security check
OA_Permission::enforceAccount(OA_ACCOUNT_ADMIN);
/*-------------------------------------------------------*/
/* Main code                                             */
/*-------------------------------------------------------*/
$doBanners = OA_Dal::factoryDO('banners');
$doBanners->storagetype = 'sql';
$doBanners->find();
while ($doBanners->fetch()) {
    // Get the filename
    $filename = $doBanners->filename;
    // Copy the file
    $buffer = phpAds_ImageRetrieve('sql', $filename);
    $doBanners->filename = phpAds_ImageStore('web', $filename, $buffer);
    // TODO: Would be nice if we gave some indication to the user of success or failure!
    if ($doBanners->filename != false) {
        phpAds_ImageDelete('sql', $filename);
        $doBannersClone = clone $doBanners;
        $doBannersClone->imageurl = '';
        $doBannersClone->storagetype = 'web';
        $doBannersClone->update();
    }
}
Header("Location: maintenance-storage.php");
        echo "<img src='images/icon-banner-stored.gif' align='absmiddle'>&nbsp;<b>" . phpAds_getBannerName($bannerid) . "</b><br><br>";
        echo phpAds_buildBannerCode($bannerid) . "<br><br><br><br>";
        phpAds_ShowSections(array("4.1.3.4.5"));
    } else {
        phpAds_PageHeader("1.2.2.3");
        echo "<img src='images/icon-campaign.gif' align='absmiddle'>&nbsp;" . phpAds_getClientName($campaignid);
        echo "&nbsp;<img src='images/" . $phpAds_TextDirection . "/caret-rs.gif'>&nbsp;";
        echo "<img src='images/icon-banner-stored.gif' align='absmiddle'>&nbsp;<b>" . phpAds_getBannerName($bannerid) . "</b><br><br>";
        echo phpAds_buildBannerCode($bannerid) . "<br><br><br><br>";
        phpAds_ShowSections(array("1.2.2.3"));
    }
    $res = phpAds_dbQuery("\n\t\tSELECT\n\t\t\t*\n\t\tFROM\n\t\t\t" . $phpAds_config['tbl_banners'] . "\n\t\tWHERE\n\t\t\tbannerid = '{$bannerid}'\n\t\t") or phpAds_sqlDie();
    $row = phpAds_dbFetchArray($res);
    if ($row['contenttype'] == 'swf') {
        if ($row['storagetype'] == 'sql' || $row['storagetype'] == 'web') {
            $swf_file = phpAds_ImageRetrieve($row['storagetype'], $row['filename']);
        }
    } else {
        // Banner is not a flash banner, return to banner-edit.php
        header("Location: banner-edit.php?clientid=" . $clientid . "&campaignid=" . $campaignid . "&bannerid=" . $bannerid);
        exit;
    }
} else {
    // Banner does not exist, return to banner-edit.php
    header("Location: banner-edit.php?clientid=" . $clientid . "&campaignid=" . $campaignid);
    exit;
}
/*********************************************************/
/* Main code                                             */
/*********************************************************/
$result = phpAds_SWFInfo($swf_file);
function phpAds_upgradeDetectPluginVersion()
{
    global $phpAds_config;
    // Include swf library
    include "lib-swf.inc.php";
    // Check if plugin detection is needed
    if (!isset($phpAds_config['config_version']) || $phpAds_config['config_version'] < 200.089) {
        $banners = array();
        // Fetch all banners
        $res = phpAds_dbQuery("SELECT * FROM " . $phpAds_config['tbl_banners']);
        while ($row = phpAds_dbFetchArray($res)) {
            $banners[] = $row;
        }
        for ($i = 0; $i < count($banners); $i++) {
            if ($banners[$i]['storagetype'] == 'sql' || $banners[$i]['storagetype'] == 'web') {
                $pluginversion = 0;
                $htmltemplate = $banners[$i]['htmltemplate'];
                if ($banners[$i]['contenttype'] == 'swf') {
                    // Determine version
                    $swf_file = phpAds_ImageRetrieve($banners[$i]['storagetype'], $banners[$i]['filename']);
                    $pluginversion = phpAds_SWFVersion($swf_file);
                    // Update template
                    $htmltemplate = ereg_replace("#version=[^\\']*'", "#version={pluginversion:4,0,0,0}'", $htmltemplate);
                } elseif ($banners[$i]['contenttype'] == 'dcr') {
                    // Update template
                    $htmltemplate = ereg_replace("#version=[^\\']*'", "#version={pluginversion:8,5,0,321}'", $htmltemplate);
                }
                $htmltemplate = addslashes($htmltemplate);
                // Update the banner
                $res = phpAds_dbQuery("\n\t\t\t\t\tUPDATE\n\t\t\t\t\t\t" . $phpAds_config['tbl_banners'] . "\n\t\t\t\t\tSET\n\t\t\t\t\t\tpluginversion = '" . $pluginversion . "',\n\t\t\t\t\t\thtmltemplate = '" . $htmltemplate . "'\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tbannerid = " . $banners[$i]['bannerid'] . "\n\t\t\t\t");
            }
        }
    }
}
function phpAds_ImageDuplicate($type, $name)
{
    $aConf = $GLOBALS['_MAX']['CONF'];
    $pref = $GLOBALS['_MAX']['PREF'];
    // Strip existing path
    $name = basename($name);
    if ($type == 'web') {
        if ($aConf['store']['mode'] == 'ftp') {
            // FTP mode
            $server = array();
            $server['host'] = $aConf['store']['ftpHost'];
            $server['path'] = $aConf['store']['ftpPath'];
            if ($server['path'] != "" && substr($server['path'], 0, 1) == "/") {
                $server['path'] = substr($server['path'], 1);
            }
            $server['user'] = $aConf['store']['ftpUsername'];
            $server['pass'] = $aConf['store']['ftpPassword'];
            $server['passiv'] = !empty($aConf['store']['ftpPassive']);
            $stored_url = phpAds_FTPDuplicate($server, $name);
        } else {
            // Local mode, do nothing
            $stored_url = $name;
        }
    }
    if ($type == 'sql') {
        if ($buffer = phpAds_ImageRetrieve($type, $name)) {
            $stored_url = phpAds_ImageStore($type, $name, $buffer);
        }
    }
    if (isset($stored_url) && $stored_url != '') {
        return $stored_url;
    } else {
        return false;
    }
}
function phpAds_ImageDuplicate($storagetype, $name)
{
    global $phpAds_config;
    // Strip existing path
    $name = basename($name);
    if ($storagetype == 'web') {
        if ($phpAds_config['type_web_mode'] == 0) {
            // Local mode
            $duplicate = phpAds_LocalUniqueName($name);
            if (@copy($phpAds_config['type_web_dir'] . "/" . $name, $phpAds_config['type_web_dir'] . "/" . $duplicate)) {
                $stored_url = $duplicate;
            }
        } else {
            // FTP mode
            $server = parse_url($phpAds_config['type_web_ftp']);
            // Decode URL parts
            $server['user'] = urldecode($server['user']);
            $server['pass'] = urldecode($server['pass']);
            $server['path'] = urldecode($server['path']);
            if ($server['path'] != "" && substr($server['path'], 0, 1) == "/") {
                $server['path'] = substr($server['path'], 1);
            }
            if ($server['scheme'] == 'ftp') {
                $stored_url = phpAds_FTPDuplicate($server, $name);
            }
        }
    }
    if ($storagetype == 'sql') {
        if ($buffer = phpAds_ImageRetrieve($storagetype, $name)) {
            $stored_url = phpAds_ImageStore($storagetype, $name, $buffer);
        }
    }
    if (isset($stored_url) && $stored_url != '') {
        return $stored_url;
    } else {
        return false;
    }
}