/** * Add a new clause * * @param string|array $prm String for a raw clause or an array with the keys: * - field string The field on which the clause is tested (required) * - op string The operator for testing (default: =) * - val string The value to test against (required) */ public function add($prm) { if (is_array($prm) && !config::initTab($prm, array('field' => null, 'op' => '=', 'val' => null))) { return; } $this->clauses[] = $prm; }
public function check($prm) { if (is_array($prm)) { $this->setNameSpaceInArray($prm); config::initTab($prm, array( 'name'=>null, )); $name = $prm['name']; } else { $name = $prm; } return isset($_SESSION[$this->prefixNameSpace($name)]); }
/** * Get a variable * * @param string|array $prm if it's a string, it's the variable name. if it's an array, options avaiable are: * - string name: Variable name * - string nameIn: Variable name in array * - array|string method: Method where to get the variable * in order of preference (default: array('post','get')) * available are: get, post, session, files or cookie * - mixed default: Possibilities array, first values will be the default or default return, if not found * - bool trim: True if need to automaticly trim the return * @return mixed The found variable or the default value or null */ public function getVar($prm) { if (is_string($prm)) { $p = array('name' => $prm); } else { $p =& $prm; } $ret = null; if (config::initTab($p, array('name' => null, 'nameIn' => '', 'method' => array('post', 'get'), 'trim' => true))) { $matches = explode('|', str_replace(array('[]', '][', '[', ']'), array('', '|', '|', ''), $p['name'])); $name = empty($matches) ? array($p['name']) : $matches; if (is_array($p['method'])) { for ($i = 0; $i < count($p['method']) && $ret === null; $i++) { $act = '_' . strtoupper($p['method'][$i]); $ret = utils::getValInArray($GLOBALS[$act], $name); } } else { $act = '_' . strtoupper($p['method']); $ret = utils::getValInArray($GLOBALS[$act], $name); } if ($act == '_GET' && !is_null($ret)) { $ret = is_array($ret) ? array_map('urldecode', $ret) : urldecode($ret); } if ($p['trim'] && !is_null($ret) && !is_array($ret)) { $ret = trim($ret); } } $prm = array_merge(array('default' => null), $p); if (is_array($prm['default'])) { if (is_null($ret) || !in_array($ret, $prm['default'])) { $ret = $prm['default'][0]; } } else { if (is_null($ret)) { $ret = $prm['default']; } } $ret = utils::htmlIn($ret); if (is_array($ret) && !empty($p['nameIn'])) { return array_key_exists($p['nameIn'], $ret) ? $ret[$p['nameIn']] : null; } else { return $ret; } }
/** * Create and verify all the variables needed for this config. * A required variable is indicate by a value null * * @param array $prm All the variables needed for this config * @throws nExecption If one required parameter isn't present */ public function init(array $prm) { foreach ($prm as $k => $v) { if (is_null($v) && (!array_key_exists($k, $this->vars) || is_null($this->vars[$k]))) { throw new nException('Config: Need ' . $k . ' Parameter.'); } if (array_key_exists($k, $this->vars)) { if (is_null($this->vars[$k])) { $this->vars[$k] = $v; } else { if (is_array($this->vars[$k]) && is_array($v)) { config::initTab($this->vars[$k], $v); } } } else { $this->vars[$k] = $v; } } }
/** * Create an id depending on the request and options * get, post, session or cookie * * @param array $prm Configuration array with keys: * - boolean uri Indicate if the uri should be used (default true) * - array meth Which method used for creating the id (default array('get','post','session')) * @return string The id */ public static function idRequest(array $prm = array()) { $tmp = ''; config::initTab($prm, array('uri' => true, 'meth' => array('get', 'post', 'session'))); if ($prm['uri'] && !empty($_SERVER['REQUEST_URI'])) { $tmp .= $_SERVER['REQUEST_URI']; } ksort($prm['meth']); foreach ($prm['meth'] as $m) { $vars =& $GLOBALS['_' . strtoupper($m)]; if (!empty($vars)) { ksort($vars); $tmp .= '@' . $m . '='; foreach ($vars as $k => $v) { $tmp .= $k . ':' . $v . '&'; } } } if (!empty($tmp)) { return sha1($tmp); } else { return ''; } }
/** * Delete cached value. You cand define what you want. * If you define nothing, all the cache will be deleted. * * @param array $prm Parameter for the cached variable to deleted: * - string callFrom: Representing CLASS-FUNCTION name of the original calling cache * - string type: Cache type, could be 'get' or 'start' (optionnal) * - string id: Cache id (optionnal) * - array tags: Tags for the id (optionnal) * @return int|bool Number of cache deleted or false * @see get, start */ public function delete(array $prm = array()) { if (config::initTab($prm, array('callFrom' => '*', 'type' => '*', 'id' => '*', 'tags' => false, 'request' => array('uri' => false, 'meth' => array())))) { $file = $this->file($prm); $file[strlen($file) - 1] = '*'; if (!empty($prm['tags'])) { for ($i = 0; $i < count($prm['tags']); $i++) { $file = str_replace(',' . $prm['tags'][$i] . ',', '*,' . $prm['tags'][$i] . ',', $file); } } $files = glob($file); $nb = 0; foreach ($files as $f) { if (file::delete($f)) { $nb++; } } return $nb; } return 0; }
/** * Add an include file * * @param array $prm Same parameter as addCss or addJs, with adding: * - string type File type (js or css) (required) * @return bool True if addedor already added, False if not found * @throws nExecption if type or file not provided * @see addJs, addCss */ public function add(array $prm) { if (config::initTab($prm, array( 'type'=>null, 'file'=>null, 'dir'=>'nyro', 'media'=>'screen', 'condIE'=>false, 'verifExists'=>true ))) { $ret = false; $firstFile = $prm['file']; if (strpos($firstFile, 'jquery') === 0 && $firstFile != 'jquery') $this->addJS('jquery'); foreach($this->getDepend($prm['file'], $prm['type']) as $d) { if (is_array($d)) $this->add(array_merge($prm, $d)); else $this->add(array_merge($prm, array('file'=>$d))); } foreach($this->cfg->getInArray($prm['type'], 'alias') as $k=>$a) { if (strtolower($prm['file']) == strtolower($k)) $prm['file'] = $a; } $prmType = $this->cfg->get($prm['type']); $locDir = $prm['dir']; if (!array_key_exists($locDir, $this->incFiles[$prm['type']])) $locDir = 'nyro'; $fileExt = $prm['file'].'.'.$prm['type']; if ($prm['verifExists']) $fileExists = $locDir == 'web' ? file::webExists($prmType['dirWeb'].DS.$fileExt) : file::nyroExists(array( 'name'=>'module_'.nyro::getCfg()->compressModule.'_'.$prm['type'].'_'.$prm['file'], 'type'=>'tpl', 'tplExt'=>$prm['type'] )); else $fileExists = true; if ($fileExists) { if (!isset($this->incFiles[$prm['type']][$locDir][$prm['media']])) $this->incFiles[$prm['type']][$locDir][$prm['media']] = array(); $this->incFiles[$prm['type']][$locDir][$prm['media']][$prm['file']] = $prm; if ($prm['type'] == 'css') { $c = file::read($fileExists); preg_match_all('`@import (url\()?"(.+).css"\)?;`', $c, $matches); if (!empty($matches[2])) { $prefix = substr($prm['file'], 0, strpos($prm['file'], '_')+1); foreach($matches[2 ] as $m) $this->add(array_merge($prm, array('file'=>$prefix.$m))); } } $ret = true; } foreach($this->getDepend($firstFile, $prm['type'], true) as $d) { if (is_array($d)) $this->add(array_merge($prm, $d)); else $this->add(array_merge($prm, array('file'=>$d))); } return $ret; } else throw new nException('reponse::add: parameters file and/or type not provied'); }
/** * Create a Select Query * * @param array|string $prm Query as string or array: The parameter for the select query (@see selectQuery) and plus: * - int result : The result type MYSQL_ASSOC, MYSQL_NUM or MYSQL_BOTH (default: MYSQL_BOTH) * @return array Numeric array. Each line is one result */ public function select($prm) { if (is_array($prm)) { config::initTab($prm, array('bind' => array(), 'forceFetchMode' => 0)); $stmt = $this->query($this->selectQuery($prm), $prm['bind']); $fetchMode = $prm['forceFetchMode'] ? $prm['forceFetchMode'] : $this->cfg->fetchMode; } else { $stmt = $this->query($prm); $fetchMode = $this->cfg->fetchMode; } if ($fetchMode == PDO::FETCH_CLASS) { $tmp = $stmt->fetchAll(PDO::FETCH_ASSOC); return $tmp; $cfg = array('db' => $this, 'table' => $prm['table'], 'props' => array_keys($tmp[0])); foreach ($tmp as $t) { $row = factory::get($className, $cfg); $row->setValues($t); $ret[] = $row; } return $ret; } else { return $stmt->fetchAll($fetchMode); } }
/** * Crop the image * * @param array $prm The parameter for the cropping: * - int x: The X position where crop (default: -1 -> automaticly center) * - int y: The Y position where crop (default: -1 -> automaticly center) * - int w: The width result (default: 0 -> the source image width) * - int h: The height result (default: 0 -> the source image height) * @return bool True if success */ protected function crop(array $prm) { config::initTab($prm, array('x' => -1, 'y' => -1, 'w' => 0, 'h' => 0)); $x = $prm['x']; $y = $prm['y']; $w = $prm['w']; $h = $prm['h']; if ($w + $h == 0) { $w = $this->cfg->w; $h = $this->cfg->h; } else { if ($w == 0) { $w = $this->cfg->h * $h / $this->cfg->w; } if ($h == 0) { $h = $this->cfg->w * $w / $this->cfg->h; } } if ($x == -1) { $x = round($this->cfg->wAct / 2 - $w / 2); } if ($y == -1) { $y = round($this->cfg->hAct / 2 - $h / 2); } $this->imgTmp = imagecreatetruecolor($this->cfg->w, $this->cfg->h); imagecopyresampled($this->imgTmp, $this->imgAct, 0, 0, $x, $y, $this->cfg->w, $this->cfg->h, $w, $h); $this->imgAct = $this->imgTmp; return true; }
/** * Try to find the file location, in the nyro installation in this order: * my directory, plugin directory and nyro directory. * The order can be reverse by setting rtl parameter to false. * If the file is located on a subdirectory, use _ to replace /. * * @param array $prm Possible values : * - name (required) string: FileName (with _ to replace /) * - realName boolean: Indicate if the given name is real or should be parsed * - type string: class, extend, cfg, tpl, lib or other (default: class) * - rtl bool: see description (default: true) * - list bool: Search all the matched files and return the order list. (default: false) * - tplExt string: Tpl Extension (default: request::get('out')) * @return false|string|array The absolute file location, an absolute file location array or false if not found */ public static function nyroExists($prm) { if (!config::initTab($prm, array('name' => null, 'realName' => false, 'type' => 'class', 'rtl' => true, 'list' => false, 'tplExt' => ''))) { throw new nException('File - nyroExists : name to search is empty.'); } $cacheKey = implode(':', array_filter($prm, 'is_string')); if (!isset(self::$searchFiles[$cacheKey])) { $dir = explode(',', SEARCHROOT); $nameTmp = $prm['realName'] ? $prm['name'] : str_replace('_', DS, $prm['name']); $name = array(); if ($prm['type'] == 'cfg') { $ext = 'cfg'; $out = request::get('out'); $name[] = $nameTmp . '.' . $ext . '.' . EXTPHP; $name[] = $nameTmp . '.' . $out . '.' . $ext . '.' . EXTPHP; $name[] = $nameTmp . '.' . request::get('lang') . '.' . $ext . '.' . EXTPHP; $name[] = $nameTmp . '.' . request::get('lang') . '.' . $out . '.' . $ext . '.' . EXTPHP; $name[] = $nameTmp . '.' . NYROENV . '.' . $ext . '.' . EXTPHP; $name[] = $nameTmp . '.' . NYROENV . '.' . $out . '.' . $ext . '.' . EXTPHP; $name[] = $nameTmp . '.' . NYROENV . '.' . request::get('lang') . '.' . $ext . '.' . EXTPHP; $name[] = $nameTmp . '.' . NYROENV . '.' . request::get('lang') . '.' . $out . '.' . $ext . '.' . EXTPHP; } else { if ($prm['type'] == 'tpl') { $ext = $prm['tplExt'] ? $prm['tplExt'] : request::get('out'); $name[] = $nameTmp . '.' . NYROENV . '.' . request::get('lang') . '.' . $ext . '.' . EXTPHP; $name[] = $nameTmp . '.' . NYROENV . '.' . request::get('lang') . '.' . $ext; $name[] = $nameTmp . '.' . NYROENV . '.' . request::get('lang') . '.' . EXTPHP; $name[] = $nameTmp . '.' . request::get('lang') . '.' . $ext . '.' . EXTPHP; $name[] = $nameTmp . '.' . request::get('lang') . '.' . $ext; $name[] = $nameTmp . '.' . request::get('lang') . '.' . EXTPHP; $name[] = $nameTmp . '.' . NYROENV . '.' . $ext . '.' . EXTPHP; $name[] = $nameTmp . '.' . NYROENV . '.' . $ext; $name[] = $nameTmp . '.' . NYROENV . '.' . EXTPHP; $name[] = $nameTmp . '.' . $ext . '.' . EXTPHP; $name[] = $nameTmp . '.' . $ext; $name[] = $nameTmp . '.' . EXTPHP; } else { if ($prm['type'] == 'other') { $name[] = $nameTmp; } else { $name[] = $nameTmp . '.' . $prm['type'] . '.' . EXTPHP; } } } if (!$prm['rtl']) { $dir = array_reverse($dir); } if ($prm['list']) { $ret = array(); } /* array_walk($dir, create_function('&$v', '$v = substr($v, strlen(ROOT));')); $regex = str_replace('\\', '\\\\', '`('.implode('|', $dir).')('.implode('|', $name).')`'); foreach(new RegexFindFile(ROOT, $regex) as $file) { if ($prm['list']) $ret[] = $file->getPathname(); else { stEnd(); return $file->getPathname(); } } // */ //* foreach ($dir as &$d) { foreach ($name as &$n) { if (self::exists($file = $d . $n)) { if ($prm['list']) { $ret[] = $file; } else { if (!isset(self::$searchFiles[$cacheKey])) { self::$searchFiles[$cacheKey] = $file; } } } } reset($name); } // */ if ($prm['list']) { self::$searchFiles[$cacheKey] = $ret; } else { if (!isset(self::$searchFiles[$cacheKey])) { self::$searchFiles[$cacheKey] = false; } } self::$saveCacheFiles = false; } return self::$searchFiles[$cacheKey]; }
/** * Get an icon * * @param array $prm Icon configuration. Available key: * - string name: action name (required) * - string type: icon type * - bool imgTag: true if the return should be a valid html img tag. if false, will return he url * - string alt: alt text for the image, used only if imgTag = true * - array attr: attributes added to the img tag * @return unknown */ public static function getIcon(array $prm) { $ret = null; static $cfg; if (!$cfg) $cfg = factory::loadCfg('icons', false); if (config::initTab($prm, array( 'name'=>null, 'type'=>$cfg['default'], 'imgTag'=>true, 'alt'=>'', 'attr'=>array(), ))) { if (array_key_exists($prm['type'], $cfg['icons']) && is_array($cfg['icons'][$prm['type']]) && in_array($prm['name'], $cfg['icons'][$prm['type']])) { $ret = request::get('path').$cfg['dir'].'/'.$prm['type'].request::getCfg('sepParam').$prm['name'].$cfg['ext']; } else if ($prm['type'] != $cfg['default']) { $ret = self::getIcon(array('name'=>$prm['name'], 'imgTag'=>false)); } if ($ret && $prm['imgTag']) { $alt = $prm['alt']? $prm['alt'] : ucFirst($prm['name']); $ret = self::htmlTag('img', array_merge(array( 'src'=>$ret, 'alt'=>$alt ), $prm['attr'])); } } return $ret; }
/** * Create the right array paremeter for select query * * @param array $prm The initial parameter * @param array $tmpTables Array used with parseLinked */ public function selectQuery(array $prm, &$tmpTables) { config::initTab($prm, array('where' => '', 'whereOp' => 'AND', 'order' => '', 'autoJoin' => $this->cfg->autoJoin)); if (is_array($prm['where'])) { foreach ($prm['where'] as $k => $v) { if (!is_numeric($k) && strpos($k, '.') === false) { $newK = $this->rawName . '.' . $k; if (!array_key_exists($newK, $prm['where'])) { $prm['where'][$newK] = $v; unset($prm['where'][$k]); } } } } else { if (!empty($prm['where']) && !is_array($prm['where']) && !is_object($prm['where']) && (strpos($prm['where'], '=') === false && strpos($prm['where'], '<') === false && strpos($prm['where'], '>') === false && stripos($prm['where'], 'LIKE') === false && stripos($prm['where'], 'IN') === false)) { $prm['where'] = $this->rawName . '.' . $this->cfg->ident . '=' . $prm['where']; } } $prm = array_merge(array('fields' => $this->getDb()->quoteIdentifier($this->rawName) . '.*', 'table' => $this->rawName), $prm); if (is_array($prm['fields'])) { array_walk($prm['fields'], create_function('&$v', '$v = strpos($v, ".") === false? "' . $this->rawName . '.".$v: $v;')); $prm['fields'] = implode(',', $prm['fields']); } $join = isset($prm['join']) ? $prm['join'] : array(); $prm['join'] = array(); $tmpTables = array(); if (!empty($this->linkedTables) && $prm['autoJoin']) { foreach ($this->linkedTables as $f => $p) { $alias = $f; $prm['join'][] = array('table' => $p['table'], 'alias' => $alias, 'dir' => 'left outer', 'on' => $this->rawName . '.' . $f . '=' . $alias . '.' . $p['ident']); $fields = explode(',', $p['fields']); array_unshift($fields, $p['ident']); $fields = array_flip(array_flip(array_filter($fields))); $fieldsT = $fields; array_walk($fieldsT, create_function('&$v', '$v = "' . $alias . '_".$v;')); $tmpTables[$f] = $fieldsT; $tmpTables[$f]['sep'] = $p['sep']; $tmpTables[$f]['ident'] = $alias . '_' . $p['ident']; $tmp = array(); $linkedTable = db::get('table', $p['table'], array('db' => $this->getDb())); foreach ($fields as $t) { if ($linkedInfo = $linkedTable->getLinkedTableName($t)) { $aliasT = $alias . '_' . $linkedInfo['table']; $prm['join'][] = array('table' => $linkedInfo['table'], 'alias' => $aliasT, 'dir' => 'left outer', 'on' => $alias . '.' . $linkedInfo['field'] . '=' . $aliasT . '.' . $linkedInfo['ident']); $ttmp = array(); foreach (explode(',', $linkedInfo['fields']) as $tt) { $ttmp[] = $aliasT . '.' . $tt; $ttmp[] = '"' . $linkedInfo['sep'] . '"'; } array_pop($ttmp); $tmp[] = 'CONCAT(' . implode(',', $ttmp) . ') AS ' . $alias . '_' . $t; } else { $tmp[] = $alias . '.' . $t . ' AS ' . $alias . '_' . $t; } } $fields = $tmp; $prm['fields'] .= ',' . implode(',', $fields); if ($p['i18nFields']) { $fieldsI18n = array(); $i18nTableName = $p['table'] . db::getCfg('i18n'); $i18nTable = db::get('table', $i18nTableName, array('db' => $this->getDb())); $primary = $i18nTable->getPrimary(); $i18nAlias = $alias . db::getCfg('i18n'); $prm['join'][] = array('table' => $i18nTableName, 'alias' => $i18nAlias, 'dir' => 'left outer', 'on' => $alias . '.' . $p['ident'] . '=' . $i18nAlias . '.' . $primary[0] . ' AND ' . $i18nAlias . '.' . $primary[1] . '="' . request::get('lang') . '"'); $fields = explode(',', $p['i18nFields']); $fields = array_flip(array_flip(array_filter($fields))); $fieldsI18n = $fields; array_walk($fieldsI18n, create_function('&$v', '$v = "' . $alias . '_' . db::getCfg('i18n') . '".$v;')); array_walk($fields, create_function('&$v', '$v = "' . $i18nAlias . '.".$v." AS ' . $alias . '_' . db::getCfg('i18n') . '".$v;')); $tmpTables[$f] = array_merge($tmpTables[$f], $fieldsI18n); if (!empty($fields)) { $prm['fields'] .= ',' . implode(',', $fields); } } } } if (!empty($this->relatedTables) && $prm['autoJoin'] || $this->i18nTable) { foreach ($this->relatedTables as $f => $p) { $prm['join'][] = array('table' => $f, 'dir' => 'left outer', 'on' => $this->rawName . '.' . $p['fk1']['link']['ident'] . '=' . $f . '.' . $p['fk1']['name']); // related Table fields $fields = array_keys($p['fields']); $fieldsTableLink = $fields; array_walk($fieldsTableLink, create_function('&$v', '$v = "' . $f . '_".$v;')); array_walk($fields, create_function('&$v', '$v = "' . $f . '.".$v." AS ' . $f . '_".$v;')); if (!empty($fields)) { $prm['fields'] .= ',' . implode(',', $fields); } $prm['join'][] = array('table' => $p['table'], 'dir' => 'left outer', 'on' => $f . '.' . $p['fk2']['name'] . '=' . $p['table'] . '.' . $p['fk2']['link']['ident']); // related Table fields $fields = explode(',', $p['fk2']['link']['fields']); array_unshift($fields, $p['fk2']['link']['ident']); $fields = array_flip(array_flip(array_filter($fields))); $fieldsT = $fields; array_walk($fieldsT, create_function('&$v', '$v = "' . $p['table'] . '_".$v;')); array_walk($fields, create_function('&$v', '$v = "' . $p['table'] . '.".$v." AS ' . $p['table'] . '_".$v;')); if (!empty($fields)) { $prm['fields'] .= ',' . implode(',', $fields); } // i18n related Table fields if ($p['fk2']['link']['i18nFields']) { $fieldsI18n = array(); $i18nTableName = $p['table'] . db::getCfg('i18n'); $i18nTable = db::get('table', $i18nTableName, array('db' => $this->getDb())); $primary = $i18nTable->getPrimary(); $prm['join'][] = array('table' => $i18nTableName, 'dir' => 'left outer', 'on' => $f . '.' . $p['fk2']['name'] . '=' . $i18nTableName . '.' . $primary[0] . ' AND ' . $i18nTableName . '.' . $primary[1] . '="' . request::get('lang') . '"'); $fields = explode(',', $p['fk2']['link']['i18nFields']); $fields = array_flip(array_flip(array_filter($fields))); $fieldsI18n = $fields; array_walk($fieldsI18n, create_function('&$v', '$v = "' . $i18nTableName . '_".$v;')); array_walk($fields, create_function('&$v', '$v = "' . $i18nTableName . '.".$v." AS ' . $p['table'] . '_' . db::getCfg('i18n') . '".$v;')); if (!empty($fields)) { $prm['fields'] .= ',' . implode(',', $fields); } } $tmpTables['relatedTable'][$f] = array('ident' => $this->getIdent(), 'tableName' => $p['table'], 'tableLink' => $fieldsTableLink, 'table' => array_merge($fieldsT, array('field' => $p['fk2']['name'], 'sep' => $p['fk2']['link']['sep'], 'ident' => $p['table'] . '_' . $p['fk2']['link']['ident']))); } if ($this->i18nTable) { $i18nName = $this->i18nTable->getName(); $primary = $this->i18nTable->getPrimary(); $prm['join'][] = array('table' => $i18nName, 'dir' => 'left outer', 'on' => $this->rawName . '.' . $this->getIdent() . '=' . $i18nName . '.' . $primary[0]); // related Table fields $fields = array($primary[1]); foreach ($this->getI18nFields() as $f) { $fields[] = $f['name']; } $fieldsTableLink = $fields; array_walk($fieldsTableLink, create_function('&$v', '$v = "' . $i18nName . '_".$v;')); array_walk($fields, create_function('&$v', '$v = "' . $i18nName . '.".$v." AS ' . $i18nName . '_".$v;')); if (!empty($fields)) { $prm['fields'] .= ',' . implode(',', $fields); } // related Table fields $tmpTables['relatedTable'][$i18nName] = array('ident' => $this->getIdent(), 'tableName' => $i18nName, 'tableLink' => $fieldsTableLink, 'table' => array('field' => $i18nName . '_' . $primary[0], 'sep' => null, 'ident' => $i18nName . '_' . $primary[1])); } if (array_key_exists('nb', $prm)) { $tmpTables['nb'] = $prm['nb']; $tmpTables['st'] = array_key_exists('start', $prm) ? $prm['start'] : 0; unset($prm['nb']); } } $prm['join'] = array_merge($prm['join'], $join); return $prm; }