/** * Creates or updates an array of custom setting<br/> * Required permissions:<br/> * <ul> * <li>IS_AUTH</li> * <li>MANAGE_SETTINGS</li> * </ul> * @param array $values An array of objects containing a name and a value string * @return array An array of custom settings * @throws \Exception */ public function setSettings($values) { if (!$this->IS_AUTH) { throw $this->throwException(AuthenticationException::NO_USER_AUTH); } if (!$this->MANAGE_SETTINGS) { throw $this->throwException(3002); } foreach ($values as $val) { $oval = (object) $val; $this->setSetting($oval->name, $oval->value); unset($oval); } $cache = new Cache(); $cache->flushCache(); return $this->getSettings(); }
/** * Removes a user from the given group * @since 1.5 * @param int $userId the Id of the user * @param int $groupId the Id of the group * @return bool * @throws \Exception */ public function removeUserFromGroup($userId, $groupId) { // No permissions required, // First we have to find a way to gracefully by-pass // the authentication system, to allow apps to manage // users. // if(!$this -> IS_AUTH) // throw $this -> throwException(Exceptions::NO_USER_AUTH); // if(!$this -> MANAGE_USER) // throw $this -> throwException(Exceptions::MISSING_PERMISSION_USER); if (!is_numeric($userId)) { throw $this->throwException(ParameterException::INTEGER_EXCEPTION); } if (!is_numeric($groupId)) { throw $this->throwException(ParameterException::INTEGER_EXCEPTION); } $c = new Cache(); $c->deleteCacheByPrefix('user'); $sql = "DELETE FROM `userusergroups` WHERE `groupId`={$groupId} AND `userId`={$userId}"; $res = $this->_conn->deleteRow($sql) == 1; $uc = new User(); $au = $uc->getAuthUser(); // Update session if necessary if ($au->userId == $userId) { $user = $uc->getUser($userId); $_SESSION['user'] = serialize($user); } return $res; }
/** * Creates or updates a calendar event * @param OCalendarEvent $event event The event to create or update * @param boolean $executeHook * @param boolean $updateContent When false, oContent is left untouched * @throws \Exception * @return bool */ public function setEvent(OCalendarEvent $event, $executeHook = true, $updateContent = true) { $ch = null; if (!isset($event->dates) || count($event->dates) == 0) { throw $this->throwException(EventException::NOT_ENOUGH_DATES); } $c = new Cache(); $c->deleteCacheByLabel("calendaritem_{$event->calendarId}"); $c->deleteCacheByPrefix("eventsByIdRange_"); $c->deleteCacheByPrefix('calendar_getEventsByRange'); // Execute hook if present if (class_exists('CalendarHook') && $executeHook) { $ch = new \CalendarHook(); if (method_exists($ch, 'preSetEvent')) { $event = $ch->preSetEvent($event); } } // Set modification values $aid = isset($_SESSION['administratorId']) ? $_SESSION['administratorId'] : 0; $event->createdby = $aid; $event->modifiedby = $aid; $event->label = $this->generateLabel($event->label, $event->calendarId); BrightUtils::forceInt($event, array('calendarId', 'itemType', 'createdby', 'locationId', 'modifiedby')); BrightUtils::escape($event, array('recur', 'label')); $event->enabled = $event->enabled === 1 || $event->enabled === true || $event->enabled === 'true' ? 1 : 0; $until = (double) $event->until; $event->until = date(BrightUtils::$SQLDateTime, $until); $sql = "INSERT INTO calendarnew (`calendarId`, `locationId`, `itemType`, `label`, `recur`, `until`, `enabled`, `deleted`, `creationdate`, `modificationdate`, `createdby`, `modifiedby`) VALUES (\n\t\t\t\t{$event->calendarId},\n\t\t\t\t{$event->locationId},\n\t\t\t\t{$event->itemType},\n\t\t\t\t'{$event->label}',\n\t\t\t\t'{$event->recur}',\n\t\t\t\t'{$event->until}',\n\t\t\t\t{$event->enabled},\n\t\t\t\tNULL, NOW(), NOW(),\n\t\t\t\t{$event->createdby},\n\t\t\t\t{$event->modifiedby})\n\t\t\t\tON DUPLICATE KEY UPDATE\n\t\t\t\titemType = VALUES(`itemType`),\n\t\t\t\tlocationId = VALUES(`locationId`),\n\t\t\t\tlabel = VALUES(`label`),\n\t\t\t\trecur = VALUES(`recur`),\n\t\t\t\tenabled = VALUES(`enabled`),\n\t\t\t\tuntil = VALUES(`until`),\n\t\t\t\tmodificationdate = NOW(),\n\t\t\t\tmodifiedby = VALUES(`modifiedby`),\n\t\t\t\t`calendarId`=LAST_INSERT_ID(`calendarId`)"; $event->calendarId = $this->_conn->insertRow($sql); $sql = 'UPDATE `calendardates` SET `deleted`=1 WHERE `calendarId`=' . $event->calendarId; $this->_conn->updateRow($sql); $sql = 'INSERT INTO `calendardates` (`calendarId`,`starttime`, `endtime`,`allday`,`deleted`,`noend`) VALUES '; $sqla = array(); // Store also for calendarevents $evdates = array(); foreach ($event->dates as &$date) { $date = (object) $date; if ($date->endtime < $date->starttime) { // Quick fix, just add 1 day to the endtime // We could (or should) throw an exception here, since it's not a valid range... $date->endtime = strtotime('+1 day', $date->endtime); } $starttime = date(BrightUtils::$SQLDateTime, $date->starttime); $endtime = date(BrightUtils::$SQLDateTime, $date->endtime); $date->allday = $date->allday == true ? 1 : 0; $date->noend = $date->noend == true ? 1 : 0; $sqla[] = "({$event->calendarId}, '{$starttime}','{$endtime}', {$date->allday},0, {$date->noend})"; $evdates[] = "({$event->calendarId}, '{$starttime}','{$endtime}', {$date->allday}, 0, {$date->noend})"; } $sql .= implode(",\r\n", $sqla); $sql .= ' ON DUPLICATE KEY UPDATE `allday`=VALUES(`allday`), `noend`=VALUES(`noend`), `deleted`=0'; $this->_conn->insertRow($sql); $sql = "DELETE FROM calendardates WHERE deleted=1"; $this->_conn->insertRow($sql); // Delete stored dates // $sql = 'DELETE FROM calendarevents WHERE eventId=' . (int) $event -> calendarId; // $this -> _conn -> deleteRow($sql); $sql = 'UPDATE calendareventsnew SET `deleted`=1 WHERE `calendarId`=' . (int) $event->calendarId; $this->_conn->updateRow($sql); $sql = 'INSERT INTO `calendareventsnew` (`calendarId`, `starttime`, `endtime`, `allday`, `deleted`,`noend`) VALUES '; $sqla = array(); $earr = array(); if ($event->recur && $event->recur != '') { $recur = $event->recur; // Recurring event, process it and add if needed $recarr = explode(';', $recur); $freq = ''; $interval = 0; // If recur has trailing ;, pop last item if ($recarr[count($recarr) - 1] == '') { array_pop($recarr); } foreach ($recarr as $recitem) { $recitemarr = explode('=', $recitem); $key = $recitemarr[0]; $val = $recitemarr[1]; $dayarr = null; $monthrepeat = 'dom'; switch ($key) { case 'FREQ': // Frequency, valid values are: DAILY, WEEKLY, MONTHLY, YEARLY $freq = $val; break; case 'INTERVAL': $interval = (int) $val; break; case 'BYDAY': // Difference between monthly and weekly switch ($freq) { case 'WEEKLY': $days = explode(',', $val); $dayarr = array(); // Find out which days are checked foreach ($days as $day) { switch ($day) { case 'SU': $dayarr[0] = 1; break; case 'MO': $dayarr[1] = 1; break; case 'TU': $dayarr[2] = 1; break; case 'WE': $dayarr[3] = 1; break; case 'TH': $dayarr[4] = 1; break; case 'FR': $dayarr[5] = 1; break; case 'SA': $dayarr[6] = 1; break; } } break; case 'MONTHLY': $monthrepeat = 'dow'; break; } break; case 'BYMONTHDAY': // Difference between monthly and yearly /** * @todo implement Is more implementation really needed, or is this switch just useless an could it be * replaced with an if statement. */ switch ($freq) { case 'MONTHLY': $monthrepeat = 'dom'; break; } break; } } // Add dates, if event recurs, calculate all dates $sqla[] = $evdates[0]; $evstart = $event->dates[0]->starttime; $evend = $event->dates[0]->endtime; $startenddiff = $evend - $evstart; switch ($freq) { case 'DAILY': // Easy! while ($evstart < $until) { $evstart += 86400 * $interval; $evend += 86400 * $interval; $ev = new \stdClass(); $ev->starttime = $evstart; $ev->endtime = $evend; $ev->calendarId = $event->calendarId; $earr[] = $ev; } break; case 'WEEKLY': // Get timestamp of the first day of the week $fdow = date('w', $evstart); $edow = date('w', $evend); $startweek = mktime(date('H', $evstart), date('i', $evstart), date('s', $evstart), date('n', $evstart), date('j', $evstart) - $fdow, date('Y', $evstart)); $ddow = $edow - $fdow; if ($ddow < 0) { $ddow += 7; } $first = true; while ($startweek < $until) { // On the first week, skip the sunday, because, // if the sunday is checked, it is already added before $dow = $first ? $fdow + 1 : 0; $first = false; while ($dow < 7) { if (array_key_exists($dow, $dayarr)) { $ev = new \stdClass(); $ev->starttime = mktime(date('H', $evstart), date('i', $evstart), date('s', $evstart), date('n', $startweek), date('j', $startweek) + $dow, date('Y', $startweek)); // $edow = $dow + $ddow; // if($edow < 0) // $edow +=7; $ev->endtime = mktime(date('H', $evend), date('i', $evend), date('s', $evend), date('n', $startweek), date('j', $startweek) + $dow + $ddow, date('Y', $startweek)); $ev->calendarId = $event->calendarId; if ($ev->starttime < $until) { $earr[] = $ev; } } $dow++; } $startweek += $interval * 604800; } // Now add 1 * $interval weeks and start with the first available day of dayarr break; case 'MONTHLY': if ($monthrepeat == 'dom') { // Day Of Month while ($evstart < $until) { $evstart = mktime(date('H', $evstart), date('i', $evstart), date('s', $evstart), date('n', $evstart) + $interval, date('j', $evstart), date('Y', $evstart)); $evend = mktime(date('H', $evend), date('i', $evend), date('s', $evend), date('n', $evend) + $interval, date('j', $evend), date('Y', $evend)); $ev = new \stdClass(); $ev->starttime = $evstart; $ev->endtime = $evend; $ev->calendarId = $event->calendarId; $earr[] = $ev; } } else { // Day of Week // Get the day of the week (sun - sat) $dow = date('w', $evstart); // Calculate how often that day has occured in the month (eg. the 2nd monday) $nd = ceil(date('j', $evstart) / 7); $mon = date('n', $evstart); while ($evstart < $until) { // Add the interval of months $a = $evstart = strtotime("+{$interval} month", $evstart); // Check the 'new' day of the week $newdow = date('w', $evstart); $delta = $dow - $newdow; if ($delta < 0) { $delta += 7; } // And correct it to the old dow $evstart += $delta * 86400; // We've accidently moved to the next month, correct date // by removing 1 week; if (date('m', $evstart) > date('m', $a)) { $evstart -= 7 * 86400; } // Check how often that day has occured $newnd = ceil(date('j', $evstart) / 7); // And correct it to the original occurence while ($newnd < $nd) { $evstart += 604800; $newnd++; } while ($newnd > $nd) { $evstart -= 604800; $newnd--; } $evend = $evstart + $startenddiff; $ev = new \stdClass(); $ev->starttime = $evstart; $ev->endtime = $evend; $ev->calendarId = $event->calendarId; $earr[] = $ev; } } break; case 'YEARLY': while ($evstart < $until) { $evstart = mktime(date('H', $evstart), date('i', $evstart), date('s', $evstart), date('n', $evstart), date('j', $evstart), date('Y', $evstart) + $interval); $evend = mktime(date('H', $evend), date('i', $evend), date('s', $evend), date('n', $evend), date('j', $evend), date('Y', $evend) + $interval); $ev = new \stdClass(); $ev->starttime = $evstart; $ev->endtime = $evend; $ev->calendarId = $event->calendarId; $earr[] = $ev; } break; } $ad = $event->dates[0]->allday; $ne = $event->dates[0]->noend; foreach ($earr as $ev) { if ($ev->endtime < $ev->starttime) { // Quick fix, just add 1 day to the endtime $ev->endtime = strtotime('+1 day', $ev->endtime); } $sqla[] = "({$event->calendarId},\n\t\t\t\t\t\t\t'" . date(BrightUtils::$SQLDateTime, $ev->starttime) . "',\n\t\t\t\t\t\t\t'" . date(BrightUtils::$SQLDateTime, $ev->endtime) . "',\n\t\t\t\t\t\t\t{$ad},\n\t\t\t\t\t\t\t0,\n\t\t\t\t\t\t\t{$ne})"; } } else { $sqla = $evdates; } // Add dates to db $sql .= join(',', $sqla); $sql .= ' ON DUPLICATE KEY UPDATE starttime=VALUES(starttime), endtime=VALUES(endtime), `deleted`=0, allday=VALUES(`allday`), noend=VALUES(`noend`)'; $this->_conn->insertRow($sql); $sql = 'DELETE FROM calendareventsnew WHERE `deleted`=1 AND `calendarId`=' . (int) $event->calendarId; $this->_conn->deleteRow($sql); if ($updateContent) { $this->setContent($event, 'calendarcontent'); $search = BrightUtils::createSearchString($event); if ((int) $event->locationId > 0) { $search .= $this->conn->getField("SELECT search FROM gm_markers WHERE pageId={$event->locationId}"); } $search = Connection::getInstance()->escape_string($search); $sql = "INSERT INTO calendarindex (calendarId, search) VALUES ({$event->calendarId}, '{$search}') ON DUPLICATE KEY UPDATE search='{$search}' "; $this->_conn->insertRow($sql); } if (isset($ch) && method_exists($ch, 'postSetEvent')) { $ch->postSetEvent($event); } return true; }
private function _getMarker($id, $full, $enabledOnly, $byPage = false) { $id = (int) $id; $cname = 'marker_' . $id; $cname .= $full ? 1 : 0; $cname .= $enabledOnly ? 1 : 0; $cname .= $byPage ? 1 : 0; $cache = new Cache(); $result = $cache->getCache($cname); if ($result) { return $result; } $esql = $enabledOnly ? ' AND `enabled`=1' : ''; if ($byPage) { $sql = "SELECT * FROM `gm_markers` WHERE `deleted`=0 {$esql} AND pageId={$id}"; } else { $sql = "SELECT * FROM `gm_markers` WHERE `deleted`=0 {$esql} AND markerId={$id}"; } $marker = $this->_conn->getRow($sql, 'OMarker'); if (!$marker) { return null; } if (!$full) { $cache->setCache($marker, $cname, strtotime('+1 year')); return $marker; } $page = new Page(); $contents = $page->getPageById($marker->pageId, false, false); if ($contents) { foreach ($contents as $key => $value) { $marker->{$key} = $value; } $marker->_explicitType = 'OMarker'; } $cache->setCache($marker, $cname, strtotime('+1 year')); return $marker; }
/** * Updates a page * @param OPage page The page to update * @return OPage The updated page */ private function _updatePage($page) { $ap = $page->alwayspublished ? 1 : 0; $sn = $page->showinnavigation ? 1 : 0; $cachebleChanged = $this->_cachebleChanged($page); $page->label = Connection::getInstance()->escape_string($this->generateLabel($page->label, $page->pageId)); $page->modifiedby = isset($_SESSION['administratorId']) ? $_SESSION['administratorId'] : 0; BrightUtils::forceInt($page, array('publicationdate', 'expirationdate', 'itemType', 'pageId')); $sql = "UPDATE page \n\t\t\t\tSET label='{$page->label}',\n\t\t\t\titemType='{$page->itemType}', \n\t\t\t\tpublicationdate=FROM_UNIXTIME({$page->publicationdate}),\n\t\t\t\texpirationdate=FROM_UNIXTIME({$page->expirationdate}), \n\t\t\t\talwayspublished={$ap},\n\t\t\t\tshowinnavigation={$sn},\n\t\t\t\tmodificationdate=NOW(),\n\t\t\t\tmodifiedby={$page->modifiedby}\n\t\t\t\tWHERE pageId={$page->pageId}"; $this->conn->updateRow($sql); $this->setContent($page); if ($cachebleChanged) { // Flush cache $cache = new Cache(); $cache->flushCache(); $tree = new Tree(); $tree->generateSitemap(); } return $this->getPageById($page->pageId, true); }
/** * Moves an existing page in the tree<br/> * Required permissions:<br/> * <ul> * <li>IS_AUTH</li> * </ul> * @param int $treeId The id of the page to move * @param int $oldParentId The id of the current parent * @param int $newParentId The id of the new parent * @param int $oldIndex The old index * @param int $newIndex The new index * @return array The children of the new parent * @throws \Exception */ public function movePage($treeId, $oldParentId, $newParentId, $oldIndex, $newIndex) { if (!$this->IS_AUTH) { throw $this->throwException(AuthenticationException::NO_USER_AUTH); } if (!is_numeric($treeId) || !is_numeric($oldParentId) || !is_numeric($newParentId) || !is_numeric($oldIndex) || !is_numeric($newIndex)) { throw $this->throwException(ParameterException::INTEGER_EXCEPTION); } // Check if the number of children doesn't exceed the maximum if ((int) $oldParentId != (int) $newParentId) { $sql = 'SELECT `maxchildren`, (SELECT COUNT(nct.`treeId`) ' . 'FROM `tree` nct ' . 'WHERE nct.parentId=' . $newParentId . ') ' . 'AS numchildren ' . 'FROM `itemtypes` ' . 'WHERE `itemId`=(SELECT `itemType` ' . 'FROM `page` p ' . 'WHERE p.`pageId`=(SELECT t.`pageId` ' . 'FROM `tree` t ' . 'WHERE `treeId`=' . $newParentId . '))'; $ncResult = $this->_conn->getRow($sql); if ((double) $ncResult->numchildren >= (double) $ncResult->maxchildren && (double) $ncResult->maxchildren > -1) { throw $this->throwException(6001, array($ncResult->maxchildren)); } } $retObj = new \stdClass(); $cache = new Cache(); // Check if the page exists in the new parent, //only if the newparent is actually another parent if ((int) $oldParentId != (int) $newParentId) { $sql = 'SELECT pageId FROM tree WHERE treeId = ' . $treeId; $page = $this->_conn->getRow($sql); if ($this->_checkForPageExistance($page->pageId, $newParentId)) { $retObj->oldParent = $this->getChildren($oldParentId); $retObj->newParent = $this->getChildren($newParentId); $cache->flushCache(); return $retObj; } } $this->_cleanIndexes($oldParentId); $this->_cleanIndexes($newParentId); $this->_updateIndexes($oldIndex, -1, $oldParentId); $this->_updateIndexes($newIndex, 1, $newParentId); $sql = 'UPDATE tree ' . 'SET `parentId`=' . $newParentId . ', ' . '`index`=' . $newIndex . ' ' . 'WHERE `treeId`=' . $treeId; $this->_conn->updateRow($sql); $cache->flushCache(); $this->generateSitemap(); $retObj->oldParent = $this->getChildren($oldParentId); $retObj->newParent = $this->getChildren($newParentId); return $retObj; }
/** * Saves a element * @param OPage $element The element to save * @param bool $returnall * @return \stdClass An object containing element, the just saved element and elements, an array of all elements * @throws \Exception */ public function setElement($element, $returnall = true) { if (!$this->IS_AUTH) { throw $this->throwException(AuthenticationException::NO_USER_AUTH); } if (method_exists($this->_hook, 'preSetElement')) { $element = $this->_hook->preSetElement($element); } $element = $this->_page->setPage($element, false, false); if (method_exists($this->_hook, 'postSetElement')) { $this->_hook->postSetElement($element); } $c = new Cache(); $c->deleteCacheByPrefix("element_filter_"); $search = BrightUtils::createSearchString($element); $search = Connection::getInstance()->escape_string($search); $sql = "INSERT INTO pageindex (pageId, search) VALUES ({$element->pageId}, '{$search}') ON DUPLICATE KEY UPDATE search='{$search}' "; Connection::getInstance()->insertRow($sql); if (!$returnall) { return $element; } return $this->_page->getPages(4, null, true); }
/** * Deletes a user by it's e-mail and it's activationcode * @param string $email The e-mail address of the user * @param string $activationCode The activation code of the user * @since 2.2 - 28 dec 2010 * @return boolean True when successful */ public function deactivate($email, $activationCode) { $c = new Cache(); $c->deleteCacheByPrefix('user'); $sql = 'UPDATE user ' . 'SET deleted=NOW() ' . "WHERE activationcode='" . Connection::getInstance()->escape_string($activationCode) . "' " . "AND email='" . Connection::getInstance()->escape_string($email) . "'"; return $this->conn->updateRow($sql); }
/** * Inserts content into the database * @param OPage $page * @param string $table * @return int * @throws ParameterException */ protected function setContent($page, $table = 'content') { $pid = 'pageId'; switch ($table) { case 'content': $pid = 'pageId'; break; case 'userfields': $pid = 'userId'; break; case 'calendarcontent': $pid = 'calendarId'; break; } $id = (int) $page->{$pid}; $table = Connection::getInstance()->escape_string($table); $b = new Backup(); $b->setBackup($page, $table); if (!isset($page->content) || $page->content == null) { throw new ParameterException(ParameterException::OBJECT_EXCEPTION); } $sql = "UPDATE `{$table}` SET `deleted`=1 WHERE `{$pid}`={$id}"; $this->conn->updateRow($sql); $it = new Template(); $def = $it->getTemplateDefinition($page->itemType, true); $def->title = new \stdClass(); $def->title->contenttype = 'string'; $def->title->searchable = 1; $sql = "INSERT INTO `{$table}` (`{$pid}`, `lang`, `field`, `value`,`index`,`searchable`) VALUES "; $sqla = array(); foreach ($page->content as $field => $langs) { $searchable = 0; if ($field == 'title') { $searchable = 1; if ($table == 'content' && $this->_titleChanged($langs, $page->{$pid})) { $cache = new Cache(); $cache->flushCache(); } } else { $searchable = isset($def->{$field}) && $def->{$field}->searchable ? 1 : 0; } foreach ($langs as $lang => $val) { // 20130725 Fix for unknown fields $contenttype = isset($def->{$field}) && isset($def->{$field}->contenttype) ? $def->{$field}->contenttype : 'string'; switch ($contenttype) { case 'array': $index = 0; if (!is_array($val)) { $val = array($val); } foreach ($val as $listval) { $lang = BrightUtils::escapeSingle($lang); $field = BrightUtils::escapeSingle($field); if (!is_string($listval)) { //throw $this -> throwException(Exceptions::INCORRECT_PARAM_STRING, '$listval: ' . print_r($listval, true)); $listval = json_encode($listval); } $listval = BrightUtils::escapeHtml($listval); $sqlid = $page->{$pid}; $sqla[] = "({$sqlid},\n\t\t\t\t\t\t\t\t\t\t'{$lang}',\n\t\t\t\t\t\t\t\t\t\t'{$field}',\n\t\t\t\t\t\t\t\t\t\t'{$listval}', {$index}, {$searchable}) "; $index++; } break; default: $lang = BrightUtils::escapeSingle($lang); $field = BrightUtils::escapeSingle($field); if ($val !== null && $val !== false && !is_scalar($val)) { $val = json_encode($val); if ($val == '{}') { $val = ''; } //throw $this -> throwException(Exceptions::INCORRECT_PARAM_STRING, 'val: ' . print_r($val, true)); } $val = BrightUtils::escapeHtml($val); if ($val != '') { $sqlid = $page->{$pid}; $sqla[] = "({$sqlid},\n\t\t\t\t\t\t\t\t\t\t'{$lang}',\n\t\t\t\t\t\t\t\t\t\t'{$field}',\n\t\t\t\t\t\t\t\t\t\t'{$val}', 0, {$searchable}) "; } } } } if (count($sqla) > 0) { $sql .= implode(", \r\n", $sqla); $sql .= " ON DUPLICATE KEY UPDATE `value`=VALUES(`value`), `deleted`=0"; $result = $this->conn->insertRow($sql); } else { // Delete old content? uncomment line // $result = 1; } if ($result !== false && $result > 0) { // All is well, clean up old data $sql = "DELETE FROM `{$table}` WHERE `deleted`= 1 AND `{$pid}`={$id}"; $this->conn->deleteRow($sql); } return $result; }