return CRM_Utils_System::permissionDenied();
}
- $sortMapper = array();
- foreach ($_GET['columns'] as $key => $value) {
- $sortMapper[$key] = $value['data'];
- };
-
- $offset = isset($_GET['start']) ? CRM_Utils_Type::escape($_GET['start'], 'Integer') : 0;
- $rowCount = isset($_GET['length']) ? CRM_Utils_Type::escape($_GET['length'], 'Integer') : 25;
- $sort = isset($_GET['order'][0]['column']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_GET['order'][0]['column'], 'Integer'), $sortMapper) : NULL;
- $sortOrder = isset($_GET['order'][0]['dir']) ? CRM_Utils_Type::escape($_GET['order'][0]['dir'], 'String') : 'asc';
-
- $params = $_GET;
- if ($sort && $sortOrder) {
- $params['sortBy'] = $sort . ' ' . $sortOrder;
- }
-
- $params['page'] = ($offset / $rowCount) + 1;
- $params['rp'] = $rowCount;
+ $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams();
$params['contact_id'] = $contactID;
$params['context'] = $context;
CRM_Utils_System::setHttpHeader('Cache-Control', "max-age=$ttl, public");
}
+ public static function defaultSortAndPagerParams($defaultOffset = 0, $defaultRowCount = 25, $defaultSort = NULL, $defaultsortOrder = 'asc') {
+ $params = array();
+
+ $sortMapper = array();
+ foreach ($_GET['columns'] as $key => $value) {
+ $sortMapper[$key] = CRM_Utils_Type::escape($value['data'], 'MysqlColumnName');
+ };
+
+ $offset = isset($_GET['start']) ? CRM_Utils_Type::escape($_GET['start'], 'Integer') : $defaultOffset;
+ $rowCount = isset($_GET['length']) ? CRM_Utils_Type::escape($_GET['length'], 'Integer') : $defaultRowCount;
+ // Why is the number of order by columns limited to 1?
+ $sort = isset($_GET['order'][0]['column']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_GET['order'][0]['column'], 'Integer'), $sortMapper) : $defaultSort;
+ $sortOrder = isset($_GET['order'][0]['dir']) ? CRM_Utils_Type::escape($_GET['order'][0]['dir'], 'MysqlOrderByDirection') : $defaultsortOrder;
+
+ if ($sort) {
+ $params['sortBy'] = "`{$sort}` {$sortOrder}";
+ }
+
+ $params['page'] = ($offset / $rowCount) + 1;
+ $params['rp'] = $rowCount;
+
+ return $params;
+ }
+
}
return TRUE;
}
+ /**
+ * @param $str
+ *
+ * @return bool
+ */
+ public static function MysqlColumnName($str) {
+ // check the length.
+ // This check can be incorrect for the <table>.<column> format, which can be
+ // a problem.
+ if (empty($str) || strlen($str) > 64) {
+ return FALSE;
+ }
+
+ return TRUE;
+ }
+
+ /**
+ * @param $str
+ *
+ * @return bool
+ */
+ public static function MysqlColumnNameStrict($str) {
+ // check the length.
+ if (empty($str) || strlen($str) > 64) {
+ return FALSE;
+ }
+
+ // make sure it only contains valid characters (alphanumeric and underscores)
+ // This check doesn't support the <table>.<column> format, which can be
+ // a problem.
+ // @todo : check with the standards (http://dev.mysql.com/doc/refman/5.5/en/identifiers.html)
+ if (!preg_match('/^[\w_]+$/i', $str)) {
+ return FALSE;
+ }
+
+ return TRUE;
+ }
+
+ /**
+ * @param $str
+ *
+ * @return bool
+ */
+ public static function MysqlOrderByDirection($str) {
+ if (!preg_match('/^(asc|desc)$/i', $str)) {
+ return FALSE;
+ }
+
+ return TRUE;
+ }
+
/**
* @param $str
*
}
break;
+ case 'MysqlColumnName':
+ if (CRM_Utils_Rule::MysqlColumnName($data)) {
+ return str_replace('`', '', $data);
+ }
+ break;
+
+ case 'MysqlColumnNameStrict':
+ if (CRM_Utils_Rule::MysqlColumnNameStrict($data)) {
+ return $data;
+ }
+ break;
+
+ case 'MysqlOrderByDirection':
+ if (CRM_Utils_Rule::MysqlOrderByDirection($data)) {
+ return $data;
+ }
+ break;
+
default:
CRM_Core_Error::fatal(
$type . " is not a recognised (camel cased) data type."