public function run() { try { $this->oRequest = $this->_run(); $oRequest =& $this->oRequest; $this->sServiceModule = $oRequest->module; $this->sServiceMethod = $oRequest->method; $this->sServiceToken = $oRequest->token; FlexiLogger::info(__METHOD__, "Running service: " . $this->sServiceModule . "::" . $this->sServiceMethod); FlexiLogger::debug(__METHOD__, "Token: " . $this->sServiceToken); //throw new Exception("Token: " . $this->sServiceToken); //if ($this->sServiceMethod!="login") throw new Exception("Token: " . $this->sServiceToken); FlexiConfig::getLoginHandler()->doLoginByToken($this->sServiceToken); //if ($this->sServiceMethod!="login") throw new Exception("Logged in as: " . FlexiConfig::getLoginHandler()->getLoggedInUserId()); FlexiLogger::debug(__METHOD__, "Logged in as: " . FlexiConfig::getLoginHandler()->getLoggedInUserId()); $this->mRequestData = array(); if (isset($oRequest->data)) { $this->mRequestData = $oRequest->data; } $mResult = FlexiController::getInstance()->runService($this->sServiceModule, $this->sServiceMethod, $this->mRequestData); unset($mResult["control"]); FlexiLogger::debug(__METHOD__, "Done service: " . $this->sServiceModule . "::" . $this->sServiceMethod); echo $this->returnResult($mResult); } catch (Exception $e) { echo $this->returnResult(array("status" => false, "return" => null, "msg" => $e->getMessage())); } }
public function onEvent($event, $oModel) { $sTable = $oModel->getMeta("type"); FlexiLogger::debug(__METHOD__, $sTable . ", Event: " . $event); $sMethod = "on" . $event; FlexiLogger::debug(__METHOD__, $sTable . ", Method: " . $sMethod); if (isset(self::$aValidator[$sTable])) { if (is_array(self::$aValidator[$sTable])) { foreach (self::$aValidator[$sTable] as $oClass) { if (method_exists($oClass, $sMethod)) { FlexiLogger::debug(__METHOD__, "Calling: " . $sTable . ": " . $sMethod); $oClass->{$sMethod}($oModel); } } //is array } else { if (method_exists(self::$aValidator[$sTable], $sMethod)) { FlexiLogger::info(__METHOD__, "Calling: " . $sTable . ": " . $sMethod); self::$aValidator[$sTable]->{$sMethod}($oModel); } } //if is direct } //if exists table event }
public function getUserID() { FlexiLogger::info(__METHOD__, ""); if (empty($this->uid)) { $this->startSession(); try { $this->uid = $this->facebook->getUser(); } catch (FacebookApiException $e) { FlexiLogger::error(__METHOD__, ": failed"); } } return $this->uid; }
public function serviceSyncTable($oData) { FlexiLogger::debug(__METHOD__, "Running"); //var_dump($aData["type"]); //FlexiLogger::error(__METHOD__, print_r($oData, true)); $aTable = $oData->tables; $sType = $oData->type; $aMessage = array(); FlexiLogger::info(__METHOD__, "Sync type: " . $sType); $oUtil = FlexiModelUtil::getInstance(); foreach ($aTable as $sTable => $aTableSetting) { $aRow = $aTableSetting->rows; $sIDField = $aTableSetting->idfield; //FlexiLogger::error(__METHOD__, $sTable); $oModel = $oUtil->getRedBeanModel($sTable); if ($sType == "fullsync") { FlexiLogger::info(__METHOD__, "Clearing table: " . $sTable); if ($oUtil->getRedBeanDB()->tableExists($sTable)) { $oUtil->getRedBeanExecute("delete from " . $sTable); } } if (count($aRow) > 0) { $sFields = implode(",", array_keys((array) $aRow[0])); } $oUtil->setRedBeanTableIdField($sTable, $sIDField); FlexiLogger::debug(__METHOD__, $sTable . ", fields: " . $sFields); foreach ($aRow as $oRow) { $oRowData = (array) $oRow; $oModel = $oUtil->getRedBeanModel($sTable); // $sPrimaryField = "id"; // if (isset($oRowData[$sPrimaryField])) { // $oModel = $oUtil->getRedBeanModel($sTable, $oRowData[$sPrimaryField]); // } else { // $oModel = $oUtil->getRedBeanModel($sTable); // } //FlexiLogger::info(__METHOD__, "Type: " . $oModel->getMeta("type")); $oModel->import($oRowData, $sFields); FlexiLogger::info(__METHOD__, "model: " . $sTable . ", values: " . print_r($oModel->export(), true)); $oUtil->insertOrUpdateRedBean($oModel); unset($oModel); } //each records $aMessage[] = "Imported: " . $sTable . ", cnt: " . count($aRow); FlexiLogger::info(__METHOD__, "Imported: " . $sTable . ", cnt: " . count($aRow)); //if (1==1) { break; } } //each table $this->unsetToken(); FlexiLogger::info(__METHOD__, "Done"); return array("msg" => implode("\r\n<br/>", $aMessage)); }
/** * get table rows * @param array $aCond ['field:condition'=>'value'], eg: 'id:=' => 1 * @param String $sOrderby * @param String $sSelect, eg: select id, name */ public function getFetchList($sTable, $aCond = array(), $sOrderby = null, $sSelect = null, $iLimit = null, $iOffset = 0) { $select = empty($sSelect) ? "select * " : $sSelect; $aWhere = FlexiService::getWhere($aCond); $sSQL = $select . " from " . FlexiService::cleanField($sTable); $aParams = array(); if (!empty($aWhere["where"])) { $aParams = $aWhere["params"]; $sSQL .= " where " . $aWhere["where"]; } if (!empty($sOrderby)) { $sSQL .= " order by " . $sOrderby; } if ($iLimit > 0) { $sSQL .= " limit " . $iLimit . " offset " . $iOffset; } FlexiLogger::info(__METHOD__, "SQL: " . $sSQL . "|" . print_r($aParams, true)); return FlexiModelUtil::getInstance()->getRedbeanFetchAll($sSQL, $aParams); }
public function hasPermission($sTitle, $asContext = null) { FlexiLogger::info(__METHOD__, "Checking permission: " . $sTitle); if ($this->isSuperUser()) { return true; } global $modx; $sContext = empty($asContext) ? $modx->context->get("key") : $context; $bResult = $modx->hasPermission($sTitle); if (!$bResult) { $oUser = $modx->getAuthenticatedUser($sContext); if (is_null($oUser)) { return false; } //FlexiLogger::info(__METHOD__, "object: " . get_class($oUser)); $aGroups = $oUser->getResourceGroups($sContext); //FlexiLogger::info(__METHOD__, $oUser->username . ", Doc Groups: " . print_r($aGroups,true)); //if empty doc group, return no permission if (count($aGroups) < 1) { return false; } $oQuery = $modx->newQuery("modResourceGroup"); $oQuery->where(array("id:in" => $aGroups)); $aListGroups = $modx->getCollection("modResourceGroup", $oQuery); $aGroupName = array(); foreach ($aListGroups as $oGroup) { $aGroupName[] = $oGroup->get("name"); } FlexiLogger::info(__METHOD__, $oUser->username . ", Result Doc names: " . print_r($aGroupName, true)); if ($aGroupName != null) { foreach ($aGroupName as $sGroup) { //echo "Doc Group: " . $sGroup . " vs " . $sTitle . "\r\n<br/>"; FlexiLogger::info(__METHOD__, "Doc Group: " . $sGroup . " vs " . $sTitle); if (strtolower(trim($sGroup)) == strtolower(trim($sTitle))) { FlexiLogger::info(__METHOD__, "Found match: " . $sGroup); $bResult = true; break; } } } else { //echo "doc group empty"; FlexiLogger::info(__METHOD__, "Doc Group empty: " . serialize($aGroups)); } } return $bResult; }
public function updateTable(FlexiObject $oObject) { $bDebug = false; $aList = FlexiModelUtil::getTableSchema($oObject->sTableName); $aFieldSQL = array(); $aPrimary = array(); $sLastField = ""; $aLastPrimary = array(); foreach ($oObject->aChild["field"] as $oFieldObject) { $oField = $oFieldObject->toArray(); //finding field by old name and new name $bHasField = false; foreach ($aList as $oTableField) { $sFindName = empty($oField["oldname"]) ? $oField["sName"] : $oField["oldname"]; if ($bDebug) { echo "[" . $oTableField["Field"] . "]vs[" . $sFindName . "]"; } if ($oTableField["Field"] == $sFindName) { $bHasField = true; break; } } //if old name not found, use new name if (!$bHasField && $oField["sName"] != $oField["oldname"]) { foreach ($aList as $oTableField) { $sFindName = $oField["sName"]; if ($bDebug) { echo "[" . $oTableField["Field"] . "]vs[" . $sFindName . "]"; } if ($oTableField["Field"] == $sFindName) { $bHasField = true; break; } } } if ($oField["iStatus"] == 1) { //update or add $sSQLDefault = FlexiModelUtil::getDefaultSQL($oField["default"], $oField["cannull"]); if ($oField["primary"]) { $aPrimary[] = $oField["sName"]; } if (strpos($oTableField["Key"], "PRI") !== false) { $aLastPrimary[] = $oTableField["Field"]; } //$this->doLog("Field: " . $oField["sName"] . ", " . $oFieldObject->options . ", enum: " . $oFieldObject->getEnum()); $sFieldTypeSQL = FlexiModelUtil::getSQLName($oField["sName"]) . " " . strtoupper($oField["dbtype"]) . "" . (empty($oField["precision"]) ? "" : "(" . $oField["precision"] . ") ") . (!empty($oField["options"]) && $oField["dbtype"] == "enum" ? "(" . $oFieldObject->getEnum() . ")" : "") . " " . $sSQLDefault . " " . ($oField["autonumber"] ? "AUTO_INCREMENT" : ""); if ($bHasField) { $sFieldSQL = "CHANGE COLUMN " . FlexiModelUtil::getSQLName($sFindName) . " " . $sFieldTypeSQL; } else { $sFieldSQL = "ADD COLUMN " . $sFieldTypeSQL; } $sFieldSQL .= empty($sLastField) ? " FIRST " : " AFTER " . FlexiModelUtil::getSQLName($sLastField); $aFieldSQL[] = $sFieldSQL; $sLastField = $oField["sName"]; } else { //do delete if ($bHasField) { $aFieldSQL[] = "DROP COLUMN " . FlexiModelUtil::getSQLName($sFindName); } } } //for $sSQL = "ALTER TABLE " . FlexiModelUtil::getSQLName($oObject->sTableName) . "\n"; $sSQL .= implode(",\n", $aFieldSQL); if (!empty($aPrimary)) { $bIsSame = false; if (!empty($aLastPrimary)) { foreach ($aPrimary as $sPKey) { $bFoundKey = false; foreach ($aLastPrimary as $sLastKey) { if ($sLastKey == $sPKey) { $bFoundKey = true; $bIsSame = true; break; } } //once found a single not same, time to redo primary key if (!$bFoundKey) { $bIsSame = false; break; } } } if (!$bIsSame) { $sPrimarySQL = FlexiModelUtil::getSQLName($aPrimary); if (!empty($aLastPrimary)) { $sSQL .= "\n,DROP PRIMARY KEY"; } $sSQL .= "\n,ADD PRIMARY KEY (" . $sPrimarySQL . ")"; } } else { //new no primary key if (!empty($aLastPrimary)) { //last has primary, time to drop it $sSQL .= "\n,DROP PRIMARY KEY"; } } //$sSQL .= ";"; FlexiLogger::info(__METHOD__, "SQL: " . $sSQL); $this->logSQL($sSQL); return FlexiModelUtil::getInstance()->getXPDOExecute($sSQL); }
public function insertOrUpdateRedBean(RedBean_OODBBean $oModel) { $writer = $this->getRedBeanWriter(); $type = $oModel->getMeta("type"); $idfield = $writer->getIDField($type); $idvalue = $oModel->{$idfield}; FlexiLogger::info(__METHOD__, "trying: " . $type . ": " . $oModel->{$idfield}); if (empty($idvalue)) { //if empty, just insert as usual return $this->insertRedBean($oModel); } else { //checking if update $bean = $this->getRedBeanModel($type, $idvalue); if ($bean != null && $bean !== false && $bean->{$idfield} == $idvalue) { // is existing record FlexiLogger::debug(__METHOD__, "Found record for " . $type . ":" . $idvalue); return $this->storeRedBean($oModel); } else { //cannot find record, time to insert, but with specified id return $this->insertRedBean($oModel); } } }
public static function _storeUploadFile($oFile, $asMovePath) { $sTempFile = $oFile["tmp_name"]; if (empty($asMovePath)) { throw new Exception("New file path not specified"); } if (empty($sTempFile)) { return false; } if (!is_uploaded_file($sTempFile)) { throw new Exception("Crack attempt!"); } //FlexiLogger::error(__METHOD__, $sFormName . ", path: " . $asMovePath); $sMovePath = $asMovePath; $aInfo = pathinfo($oFile["name"]); $aReturn = array("status" => false, "path" => $sTempFile, "size" => filesize($sTempFile), "type" => $aInfo["extension"]); if (!empty($sMovePath) && $sTempFile != $sMovePath) { $aReturn["path"] = $sMovePath; if (is_file($sMovePath)) { unlink($sMovePath); } if (move_uploaded_file($sTempFile, $sMovePath)) { FlexiLogger::info(__METHOD__, "Moved file from: " . $sTempFile . " to " . $sMovePath); $aReturn["status"] = true; $aReturn["path"] = $sMovePath; } else { throw new FlexiException("Error moving file from: " . $sTempFile . " to " . $sMovePath, ERROR_FILE_MOVE); } } return $aReturn; }
/** * Resize image to max width or height * @param int $width * @param int $height * @param String $sPath * @param int Write type * IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG * @return <type> */ public static function imageResize($aiWidth, $aiHeight, $sPath, $sNewPath = null, $aiWriteType = null) { if (empty($aiWidth) && empty($aiHeight)) { return; } list($iOriWidth, $iOriHeight, $iType) = getimagesize($sPath); if ($iOriWidth <= 0 || $iOriHeight <= 0) { throw new Exception("Unable to load image: " . $sPath); } if (!empty($aiWidth) && !empty($aiHeight)) { if ($iOriWidth > $iOriHeight) { $percentage = $aiWidth / $iOriWidth; } else { $percentage = $aiHeight / $iOriHeight; } } else { if (!empty($aiWidth)) { $percentage = $aiWidth / $iOriWidth; } else { if (!empty($aiHeight)) { $percentage = $aiHeight / $iOriHeight; } } } //gets the new value and applies the percentage, then rounds the value $width = round($iOriWidth * $percentage); $height = round($iOriHeight * $percentage); if ($width > $iOriWidth) { //too big, use original size $width = $iOriWidth; $height = $iOriHeight; } switch ($iType) { case IMAGETYPE_GIF: // gif -> jpg $image = imagecreatefromgif($sPath); break; case IMAGETYPE_JPEG: // jpeg -> jpg $image = imagecreatefromjpeg($sPath); break; case IMAGETYPE_PNG: // png -> jpg $image = imagecreatefrompng($sPath); break; default: throw new FlexiException("Unknown image type: " . $iType, ERROR_DATATYPE); } FlexiLogger::info(__METHOD__, "Resize from: " . $iOriWidth . "x" . $iOriHeight . " to " . $width . "x" . $height); $image_p = imagecreatetruecolor($width, $height); imagealphablending($image_p, false); imagesavealpha($image_p, true); //$transparent = imagecolorallocatealpha($image_p, 255, 255, 255, 127); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $iOriWidth, $iOriHeight); $sWritePath = empty($sNewPath) ? $sPath : $sNewPath; $iWriteType = empty($aiWriteType) ? $iType : (int) $aiWriteType; switch ($iWriteType) { case IMAGETYPE_GIF: $image = @imagegif($image_p, $sWritePath); break; case IMAGETYPE_JPEG: $image = @imagejpeg($image_p, $sWritePath); break; case IMAGETYPE_PNG: $image = @imagepng($image_p, $sWritePath); break; default: throw new FlexiException("Unknown image type: " . $iType, ERROR_DATATYPE); } if ($image === false) { throw new Exception("Image generation failed: " . $iWriteType . ", path: " . $sWritePath); } return array($width, $height); }