Exemple #1
0
 public function bind($array, $ignore = '')
 {
     if (key_exists('params', $array) && is_array($array['params'])) {
         $registry = new MRegistry();
         $registry->loadArray($array['params']);
         $array['params'] = (string) $registry;
     }
     // Attempt to bind the data.
     $return = parent::bind($array, $ignore);
     // Load the real group data based on the bound ids.
     if ($return && !empty($this->groups)) {
         // Set the group ids.
         MArrayHelper::toInteger($this->groups);
         // Get the titles for the user groups.
         $query = $this->_db->getQuery(true);
         $query->select($this->_db->quoteName('id'));
         $query->select($this->_db->quoteName('title'));
         $query->from($this->_db->quoteName('#__usergroups'));
         $query->where($this->_db->quoteName('id') . ' = ' . implode(' OR ' . $this->_db->quoteName('id') . ' = ', $this->groups));
         $this->_db->setQuery($query);
         // Set the titles for the user groups.
         $this->groups = $this->_db->loadAssocList('id', 'id');
         // Check for a database error.
         if ($this->_db->getErrorNum()) {
             $this->setError($this->_db->getErrorMsg());
             return false;
         }
     }
     return $return;
 }
Exemple #2
0
 public function batch($commands, $pks, $contexts)
 {
     // Sanitize user ids.
     $pks = array_unique($pks);
     MArrayHelper::toInteger($pks);
     // Remove any values of zero.
     if (array_search(0, $pks, true)) {
         unset($pks[array_search(0, $pks, true)]);
     }
     if (empty($pks)) {
         $this->setError(MText::_('MGLOBAL_NO_ITEM_SELECTED'));
         return false;
     }
     $done = false;
     if (!empty($commands['category_id'])) {
         $cmd = MArrayHelper::getValue($commands, 'move_copy', 'c');
         if ($cmd == 'c') {
             $result = $this->batchCopy($commands['category_id'], $pks, $contexts);
             if (is_array($result)) {
                 $pks = $result;
             } else {
                 return false;
             }
         } elseif ($cmd == 'm' && !$this->batchMove($commands['category_id'], $pks, $contexts)) {
             return false;
         }
         $done = true;
     }
     if (!empty($commands['assetgroup_id'])) {
         if (!$this->batchAccess($commands['assetgroup_id'], $pks, $contexts)) {
             return false;
         }
         $done = true;
     }
     if (!empty($commands['language_id'])) {
         if (!$this->batchLanguage($commands['language_id'], $pks, $contexts)) {
             return false;
         }
         $done = true;
     }
     if (!$done) {
         $this->setError(MText::_('MLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION'));
         return false;
     }
     // Clear the cache
     $this->cleanCache();
     return true;
 }
Exemple #3
0
 public static function toInteger(&$array, $default = null)
 {
     if (is_array($array)) {
         foreach ($array as $i => $v) {
             $array[$i] = (int) $v;
         }
     } else {
         if ($default === null) {
             $array = array();
         } elseif (is_array($default)) {
             MArrayHelper::toInteger($default, null);
             $array = $default;
         } else {
             $array = array((int) $default);
         }
     }
 }
Exemple #4
0
 public function remove()
 {
     // Check for request forgeries
     MRequest::checkToken() or jexit('Invalid Token');
     $cid = MRequest::getVar('cid', array(), '', 'array');
     MArrayHelper::toInteger($cid);
     $msg = '';
     for ($i = 0, $n = count($cid); $i < $n; $i++) {
         $query = MTable::getInstance('Query', 'Table');
         if (!$query->delete($cid[$i])) {
             $msg .= $query->getError();
             $tom = "error";
         } else {
             $msg = MTEXT::_('COM_MIWOSQL_QUERY_DELETED');
             $tom = "";
         }
     }
     $this->setRedirect('index.php?option=com_miwosql&controller=queries', $msg, $tom);
 }
Exemple #5
0
 public static function categories($extension, $config = array('filter.published' => array(0, 1)))
 {
     $hash = md5($extension . '.' . serialize($config));
     if (!isset(self::$items[$hash])) {
         $config = (array) $config;
         $db = MFactory::getDbo();
         $query = $db->getQuery(true);
         $query->select('a.id, a.title, a.level, a.parent_id');
         $query->from('#__categories AS a');
         $query->where('a.parent_id > 0');
         // Filter on extension.
         $query->where('extension = ' . $db->quote($extension));
         // Filter on the published state
         if (isset($config['filter.published'])) {
             if (is_numeric($config['filter.published'])) {
                 $query->where('a.published = ' . (int) $config['filter.published']);
             } elseif (is_array($config['filter.published'])) {
                 MArrayHelper::toInteger($config['filter.published']);
                 $query->where('a.published IN (' . implode(',', $config['filter.published']) . ')');
             }
         }
         $query->order('a.lft');
         $db->setQuery($query);
         $items = $db->loadObjectList();
         // Assemble the list options.
         self::$items[$hash] = array();
         foreach ($items as &$item) {
             $repeat = $item->level - 1 >= 0 ? $item->level - 1 : 0;
             $item->title = str_repeat('- ', $repeat) . $item->title;
             self::$items[$hash][] = MHtml::_('select.option', $item->id, $item->title);
         }
         // Special "Add to root" option:
         self::$items[$hash][] = MHtml::_('select.option', '1', MText::_('MLIB_HTML_ADD_TO_ROOT'));
     }
     return self::$items[$hash];
 }
Exemple #6
0
 protected function filterField($element, $value)
 {
     // Make sure there is a valid SimpleXMLElement.
     if (!$element instanceof SimpleXMLElement) {
         return false;
     }
     // Get the field filter type.
     $filter = (string) $element['filter'];
     // Process the input value based on the filter.
     $return = null;
     switch (strtoupper($filter)) {
         // Access Control Rules.
         case 'RULES':
             $return = array();
             foreach ((array) $value as $action => $ids) {
                 // Build the rules array.
                 $return[$action] = array();
                 foreach ($ids as $id => $p) {
                     if ($p !== '') {
                         $return[$action][$id] = $p == '1' || $p == 'true' ? true : false;
                     }
                 }
             }
             break;
             // Do nothing, thus leaving the return value as null.
         // Do nothing, thus leaving the return value as null.
         case 'UNSET':
             break;
             // No Filter.
         // No Filter.
         case 'RAW':
             $return = $value;
             break;
             // Filter the input as an array of integers.
         // Filter the input as an array of integers.
         case 'INT_ARRAY':
             // Make sure the input is an array.
             if (is_object($value)) {
                 $value = get_object_vars($value);
             }
             $value = is_array($value) ? $value : array($value);
             MArrayHelper::toInteger($value);
             $return = $value;
             break;
             // Filter safe HTML.
         // Filter safe HTML.
         case 'SAFEHTML':
             $return = MFilterInput::getInstance(null, null, 1, 1)->clean($value, 'string');
             break;
             // Convert a date to UTC based on the server timezone offset.
         // Convert a date to UTC based on the server timezone offset.
         case 'SERVER_UTC':
             if (intval($value) > 0) {
                 // Get the server timezone setting.
                 $offset = MFactory::getConfig()->get('offset');
                 // Return an SQL formatted datetime string in UTC.
                 $return = MFactory::getDate($value, $offset)->toSql();
             } else {
                 $return = '';
             }
             break;
             // Convert a date to UTC based on the user timezone offset.
         // Convert a date to UTC based on the user timezone offset.
         case 'USER_UTC':
             if (intval($value) > 0) {
                 // Get the user timezone setting defaulting to the server timezone setting.
                 $offset = MFactory::getUser()->getParam('timezone', MFactory::getConfig()->get('offset'));
                 // Return a MySQL formatted datetime string in UTC.
                 $return = MFactory::getDate($value, $offset)->toSql();
             } else {
                 $return = '';
             }
             break;
             // Ensures a protocol is present in the saved field. Only use when
             // the only permitted protocols requre '://'. See MFormRuleUrl for list of these.
         // Ensures a protocol is present in the saved field. Only use when
         // the only permitted protocols requre '://'. See MFormRuleUrl for list of these.
         case 'URL':
             if (empty($value)) {
                 return false;
             }
             $value = MFilterInput::getInstance()->clean($value, 'html');
             $value = trim($value);
             // <>" are never valid in a uri see http://www.ietf.org/rfc/rfc1738.txt.
             $value = str_replace(array('<', '>', '"'), '', $value);
             // Check for a protocol
             $protocol = parse_url($value, PHP_URL_SCHEME);
             // If there is no protocol and the relative option is not specified,
             // we assume that it is an external URL and prepend http://.
             if ($element['type'] == 'url' && !$protocol && !$element['relative'] || !$element['type'] == 'url' && !$protocol) {
                 $protocol = 'http';
                 // If it looks like an internal link, then add the root.
                 if (substr($value, 0) == 'index.php') {
                     $value = MURI::root() . $value;
                 }
                 // Otherwise we treat it is an external link.
                 // Put the url back together.
                 $value = $protocol . '://' . $value;
             } elseif (!$protocol && $element['relative']) {
                 $host = MURI::getInstance('SERVER')->gethost();
                 // If it starts with the host string, just prepend the protocol.
                 if (substr($value, 0) == $host) {
                     $value = 'http://' . $value;
                 } else {
                     $value = MURI::root() . $value;
                 }
             }
             $return = $value;
             break;
         case 'TEL':
             $value = trim($value);
             // Does it match the NANP pattern?
             if (preg_match('/^(?:\\+?1[-. ]?)?\\(?([2-9][0-8][0-9])\\)?[-. ]?([2-9][0-9]{2})[-. ]?([0-9]{4})$/', $value) == 1) {
                 $number = (string) preg_replace('/[^\\d]/', '', $value);
                 if (substr($number, 0, 1) == 1) {
                     $number = substr($number, 1);
                 }
                 if (substr($number, 0, 2) == '+1') {
                     $number = substr($number, 2);
                 }
                 $result = '1.' . $number;
             } elseif (preg_match('/^\\+(?:[0-9] ?){6,14}[0-9]$/', $value) == 1) {
                 $countrycode = substr($value, 0, strpos($value, ' '));
                 $countrycode = (string) preg_replace('/[^\\d]/', '', $countrycode);
                 $number = strstr($value, ' ');
                 $number = (string) preg_replace('/[^\\d]/', '', $number);
                 $result = $countrycode . '.' . $number;
             } elseif (preg_match('/^\\+[0-9]{1,3}\\.[0-9]{4,14}(?:x.+)?$/', $value) == 1) {
                 if (strstr($value, 'x')) {
                     $xpos = strpos($value, 'x');
                     $value = substr($value, 0, $xpos);
                 }
                 $result = str_replace('+', '', $value);
             } elseif (preg_match('/[0-9]{1,3}\\.[0-9]{4,14}$/', $value) == 1) {
                 $result = $value;
             } else {
                 $value = (string) preg_replace('/[^\\d]/', '', $value);
                 if ($value != null && strlen($value) <= 15) {
                     $length = strlen($value);
                     // if it is fewer than 13 digits assume it is a local number
                     if ($length <= 12) {
                         $result = '.' . $value;
                     } else {
                         // If it has 13 or more digits let's make a country code.
                         $cclen = $length - 12;
                         $result = substr($value, 0, $cclen) . '.' . substr($value, $cclen);
                     }
                 } else {
                     $result = '';
                 }
             }
             $return = $result;
             break;
         default:
             // Check for a callback filter.
             if (strpos($filter, '::') !== false && is_callable(explode('::', $filter))) {
                 $return = call_user_func(explode('::', $filter), $value);
             } elseif (function_exists($filter)) {
                 $return = call_user_func($filter, $value);
             } else {
                 $return = MFilterInput::getInstance()->clean($value, $filter);
             }
             break;
     }
     return $return;
 }
Exemple #7
0
 public function publish($pks = null, $state = 1, $userId = 0)
 {
     $k = $this->_tbl_key;
     MArrayHelper::toInteger($pks);
     $userId = (int) $userId;
     $state = (int) $state;
     $compareState = $state > 1 ? 1 : $state;
     if (empty($pks)) {
         if ($this->{$k}) {
             $pks = explode(',', $this->{$k});
         } else {
             $e = new MException(MText::sprintf('MLIB_DATABASE_ERROR_NO_ROWS_SELECTED', get_class($this)));
             $this->setError($e);
             return false;
         }
     }
     $checkoutSupport = property_exists($this, 'checked_out') || property_exists($this, 'checked_out_time');
     foreach ($pks as $pk) {
         if (!($node = $this->_getNode($pk))) {
             return false;
         }
         if ($checkoutSupport) {
             $query = $this->_db->getQuery(true);
             $query->select('COUNT(' . $k . ')');
             $query->from($this->_tbl);
             $query->where('lft BETWEEN ' . (int) $node->lft . ' AND ' . (int) $node->rgt);
             $query->where('(checked_out <> 0 AND checked_out <> ' . (int) $userId . ')');
             $this->_db->setQuery($query);
             // Check for checked out children.
             if ($this->_db->loadResult()) {
                 $e = new MException(MText::sprintf('MLIB_DATABASE_ERROR_CHILD_ROWS_CHECKED_OUT', get_class($this)));
                 $this->setError($e);
                 return false;
             }
         }
         if ($node->parent_id) {
             $query = $this->_db->getQuery(true)->select('n.' . $k)->from($this->_db->quoteName($this->_tbl) . ' AS n')->where('n.lft < ' . (int) $node->lft)->where('n.rgt > ' . (int) $node->rgt)->where('n.parent_id > 0')->where('n.published < ' . (int) $compareState);
             $this->_db->setQuery($query, 0, 1);
             $rows = $this->_db->loadColumn();
             if ($this->_db->getErrorNum()) {
                 $e = new MException(MText::sprintf('MLIB_DATABASE_ERROR_PUBLISH_FAILED', get_class($this), $this->_db->getErrorMsg()));
                 $this->setError($e);
                 return false;
             }
             if (!empty($rows)) {
                 $e = new MException(MText::_('MLIB_DATABASE_ERROR_ANCESTOR_NODES_LOWER_STATE'));
                 $this->setError($e);
                 return false;
             }
         }
         $query = $this->_db->getQuery(true)->update($this->_db->quoteName($this->_tbl))->set('published = ' . (int) $state)->where('(lft > ' . (int) $node->lft . ' AND rgt < ' . (int) $node->rgt . ')' . ' OR ' . $k . ' = ' . (int) $pk);
         $this->_db->setQuery($query);
         if (!$this->_db->execute()) {
             $e = new MException(MText::sprintf('MLIB_DATABASE_ERROR_PUBLISH_FAILED', get_class($this), $this->_db->getErrorMsg()));
             $this->setError($e);
             return false;
         }
         if ($checkoutSupport) {
             $this->checkin($pk);
         }
     }
     if (in_array($this->{$k}, $pks)) {
         $this->published = $state;
     }
     $this->setError('');
     return true;
 }
Exemple #8
0
 public function publish($pks = null, $state = 1, $userId = 0)
 {
     // Initialise variables.
     $k = $this->_tbl_key;
     // Sanitize input.
     MArrayHelper::toInteger($pks);
     $userId = (int) $userId;
     $state = (int) $state;
     // If there are no primary keys set check to see if the instance key is set.
     if (empty($pks)) {
         if ($this->{$k}) {
             $pks = array($this->{$k});
         } else {
             $e = new MException(MText::_('MLIB_DATABASE_ERROR_NO_ROWS_SELECTED'));
             $this->setError($e);
             return false;
         }
     }
     // Update the publishing state for rows with the given primary keys.
     $query = $this->_db->getQuery(true);
     $query->update($this->_tbl);
     $query->set('published = ' . (int) $state);
     // Determine if there is checkin support for the table.
     if (property_exists($this, 'checked_out') || property_exists($this, 'checked_out_time')) {
         $query->where('(checked_out = 0 OR checked_out = ' . (int) $userId . ')');
         $checkin = true;
     } else {
         $checkin = false;
     }
     // Build the WHERE clause for the primary keys.
     $query->where($k . ' = ' . implode(' OR ' . $k . ' = ', $pks));
     $this->_db->setQuery($query);
     // Check for a database error.
     if (!$this->_db->execute()) {
         $e = new MException(MText::sprintf('MLIB_DATABASE_ERROR_PUBLISH_FAILED', get_class($this), $this->_db->getErrorMsg()));
         $this->setError($e);
         return false;
     }
     // If checkin is supported and all rows were adjusted, check them in.
     if ($checkin && count($pks) == $this->_db->getAffectedRows()) {
         // Checkin the rows.
         foreach ($pks as $pk) {
             $this->checkin($pk);
         }
     }
     // If the MTable instance value is in the list of primary keys that were set, set the instance.
     if (in_array($this->{$k}, $pks)) {
         $this->published = $state;
     }
     $this->setError('');
     return true;
 }