Ejemplo n.º 1
0
function axWFactoryDomainCRUD($type = "add")
{
    global $wgRequest, $wgUser, $wgExternalSharedDB, $wgOut;
    $sDomain = $wgRequest->getVal("domain");
    $city_id = $wgRequest->getVal("cityid");
    if (!$wgUser->isAllowed('wikifactory')) {
        $wgOut->readOnlyPage();
        #--- later change to something reasonable
        return;
    }
    if (empty($city_id)) {
        $wgOut->readOnlyPage();
        #--- later change to something reasonable
        return;
    }
    $dbw = wfGetDB(DB_MASTER, array(), $wgExternalSharedDB);
    $aDomains = array();
    $aResponse = array();
    $sInfo = "";
    switch ($type) {
        case "add":
            if (!preg_match("/^[\\w\\.\\-]+\$/", $sDomain)) {
                /**
                 * check if domain is valid (a im sure that there is function
                 * somewhere for such purpose
                 */
                $sInfo .= "Error: Domain <em>{$sDomain}</em> is invalid (or empty) so it's not added.";
            } else {
                $added = WikiFactory::addDomain($city_id, $sDomain);
                if ($added) {
                    $sInfo .= "Success: Domain <em>{$sDomain}</em> added.";
                } else {
                    $sInfo .= "Error: Domain <em>{$sDomain}</em> is already used so it's not added.";
                }
            }
            break;
        case "change":
            $sNewDomain = $wgRequest->getVal("newdomain");
            #--- first, check if domain is not used
            $oRes = $dbw->select("city_domains", "count(*) as count", array("city_domain" => $sNewDomain), __METHOD__);
            $oRow = $dbw->fetchObject($oRes);
            $dbw->freeResult($oRes);
            if ($oRow->count > 0) {
                #--- domain is used already
                $sInfo .= "<strong>Error: Domain <em>{$sNewDomain}</em> is already used so no change was done.</strong>";
            } elseif (!preg_match("/^[\\w\\.\\-]+\$/", $sNewDomain)) {
                #--- check if domain is valid (a im sure that there is function
                #--- somewhere for such purpose
                $sInfo .= "<strong>Error: Domain <em>{$sNewDomain}</em> is invalid so no change was done..</strong>";
            } else {
                #--- reall change domain
                $dbw->update("city_domains", array("city_domain" => strtolower($sNewDomain)), array("city_id" => $city_id, "city_domain" => strtolower($sDomain)));
                $dbw->commit();
                $sInfo .= "Success: Domain <em>{$sDomain}</em> changed to <em>{$sNewDomain}</em>.";
            }
            break;
        case "remove":
            $removed = WikiFactory::removeDomain($city_id, $sDomain);
            if ($removed) {
                $sInfo .= "Success: Domain <em>{$sDomain}</em> removed.";
            } else {
                $sInfo .= "Failed: Domain <em>{$sDomain}</em> was not removed.";
            }
            break;
        case "status":
            $iNewStatus = $wgRequest->getVal("status");
            if (in_array($iNewStatus, array(0, 1, 2))) {
                #--- updatec city_list table
                $dbw->update("city_list", array("city_public" => $iNewStatus), array("city_id" => $city_id));
                $dbw->commit();
                switch ($iNewStatus) {
                    case 0:
                        $aResponse["div-body"] = "<strong>changed to disabled</strong>";
                        break;
                    case 1:
                        $aResponse["div-body"] = "<strong>changed to enabled</strong>";
                        break;
                    case 2:
                        $aResponse["div-body"] = "<strong>changed to redirected</strong>";
                        break;
                }
            } else {
                $aResponse["div-body"] = "wrong status number";
            }
            $aResponse["div-name"] = "wf-domain-span";
            break;
        case "cancel":
            $sInfo .= "<em>Action cancelled</em>";
            break;
        case "setmain":
            $setmain = WikiFactory::setmainDomain($city_id, $sDomain);
            if ($setmain) {
                $sInfo .= "Success: Domain <em>{$sDomain}</em> set as main.";
            } else {
                $sInfo .= "Failed: Domain <em>{$sDomain}</em> was not set as main.";
            }
            break;
    }
    #--- get actuall domain list
    $aDomains = WikiFactory::getDomains($city_id, true);
    #--- send response, return domain array
    $aResponse["domains"] = $aDomains;
    $aResponse["info"] = $sInfo;
    return json_encode($aResponse);
}