关于闭包最牛B的解释
引用:http://www.cnblogs.com/xiaotie/archive/2011/08/03/2126145.html
Flow Asia
引用:http://www.cnblogs.com/xiaotie/archive/2011/08/03/2126145.html
直接修改php.ini和通过.htaccess的修改方法就不说了,万网的主机是不允许自行修改php.ini的参数的。以下是通过代码修改,不过注意,这样修改:
找到libraries/joomla/filter/input.php
public function clean($source, $type = 'string') { // Handle the type constraint switch (strtoupper($type)) {... } // -- ADD THIS CODE HERE // Handle magic quotes compatibility if(get_magic_quotes_gpc()) $result = self::_stripSlashesRecursive($result); }
在文件775行左右添加
/** * Strips slashes recursively on an array. * * @param array $value Array or (nested arrays) of strings. * * @return array The input array with stripslashes applied to it. * * @deprecated 12.1 - Sanctioned on 2012-10-09 thanks to Anton Wintergerst * @since 11.1 */ protected static function _stripSlashesRecursive($value) { $value = is_array($value) ? array_map(array('JFilterInput', '_stripSlashesRecursive'), $value) : stripslashes($value); return $value; }
1. 安装编译器和库文件:
#有些是中途安装AMP的过程中发现没有安装,作为总结,写在最前边 sudo yum install -y cmake.x86_64 gcc-c++.x86_64 ncurses.x86_64 ncurses-devel.x86_64 libxml2-devel.x86_64 libcurl-devel.x86_64 libjpeg-turbo-devel.x86_64 libpng-devel.x86_64 freetype-devel.x86_64 autoconf.noarch
2. 安装MySQL
2.1 下载MySQL:可以在http://dev.mysql.com/downloads/mysql/查看对应版本
wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.50.tar.gz
2.2 创建mysql用户
groupadd mysql useradd -r -g mysql mysql
2.3 创建mysql目录
mkdir /usr/local/mysql mkdir /usr/local/mysql/data chown -R mysql:mysql /usr/local/mysql/data tar -zxvf mysql-5.5.50.tar.gz cd mysql-5.5.50 cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1 make make install cp support-files/my-medium.cnf /etc/my.cnf /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data /usr/local/mysql/bin/mysqld_safe & cd /usr/local/mysql/ #重要,否则会报 Can't find a 'mysql' client in PATH or ./bin bin/mysql_secure_installation
3. 安装apache
wget http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.2.31.tar.gz wget http://apache.fayea.com//apr/apr-1.5.2.tar.gz wget http://apache.fayea.com//apr/apr-util-1.5.4.tar.gz wget http://downloads.sourceforge.net/project/pcre/pcre/8.39/pcre-8.39.zip tar -zxvf apr-1.5.2.tar.gz ./configure && make && make install tar -zxvf apr-util-1.5.4.tar.gz ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install unzip pcre-8.39.zip ./configure --prefix=/usr/local/pcre && make && make install tar -zxvf httpd-2.2.31.tar.gz ./configure --prefix=/usr/local/apache2 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre --with-included-apr --enable-deflate --enable-expires --enable-headers --enable-rewrite --enable-so make && make install
4. 安装PHP
wget http://am1.php.net/get/php-5.5.38.tar.gz/from/this/mirror tar -zxvf php-5.5.38.tar.gz ./configure --prefix=/usr/local/php5 --with-apxs2=/usr/local/apache2/bin/apxs --with-zlib --with-curl --enable-exif --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --with-gettext --with-mhash --enable-mbstring --with-mysql --with-mysqli --enable-zip --with-pear make && make install sudo cp php.ini-production /usr/local/php5/lib/php.ini
修改/usr/local/apache2/conf/httpd.conf,添加:
AddType application/x-httpd-php .php .phtml AddType application/x-httpd-php-source .phps
重启apache:
sudo /usr/local/apache2/bin/apachectl start|graceful
5. 安装xcache
wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz tar -zxvf xcache-3.2.0.tar.gz cd xcache-3.2.0 /usr/local/php5/bin/phpize ./configure --enable-xcache --enable-xcache-coverager --enable-xcache-optimizer --with-php-config=/usr/local/php5/bin/php-config make && make install
建立缓存文件
touch /tmp/xcache chmod 0777 /tmp/xcache
拷贝xcache管理后台到可访问目录
cp -R htdocs/ /var/www/html/xcache
生成密码
http://{ip}/xcache/cacher/mkpassword.php Username: xcache Password: 123456
将以下代码写入/usr/local/php/lib/php.ini
[xcache-common] extension = xcache.so [xcache.admin] xcache.admin.enable_auth = On xcache.admin.user = "xcache" xcache.admin.pass = "{generated password}" [xcache] xcache.shm_scheme = "mmap" xcache.size = 64M xcache.count = 1 xcache.slots = 8K xcache.ttl = 0 xcache.gc_interval = 0 xcache.var_size= 64M xcache.var_count = 1 xcache.var_slots = 8K xcache.var_ttl = 0 xcache.var_maxttl = 0 xcache.var_gc_interval = 300 xcache.test = Off xcache.readonly_protection = On xcache.mmap_path = "/tmp/xcache" xcache.coredump_directory = "" xcache.cacher = On xcache.stat = On xcache.optimizer = Off [xcache.coverager] xcache.coverager = On xcache.coveragedump_directory = ""
访问管理地址查看缓存使用情况
http://{ip}/xcache
在wp-admin/include/update.php最后加入以下几行代码
add_filter('pre_site_transient_update_core', create_function('$a', "return null;")); // 关闭核心提示 add_filter('pre_site_transient_update_plugins', create_function('$a', "return null;")); // 关闭插件提示 add_filter('pre_site_transient_update_themes', create_function('$a', "return null;")); // 关闭主题提示 remove_action('admin_init', '_maybe_update_core'); // 禁止 WordPress 检查更新 remove_action('admin_init', '_maybe_update_plugins'); // 禁止 WordPress 更新插件 remove_action('admin_init', '_maybe_update_themes'); // 禁止 WordPress 更新主题
最近有个项目要使用File_IMC扩展,使用两种方法都出错
[email protected]:~# pear install -f File_IMC-beta downloading File_IMC-0.5.0.tgz ... Starting to download File_IMC-0.5.0.tgz (32,082 bytes) .........done: 32,082 bytes could not extract the package.xml file from "/build/buildd/php5-5.5.9+dfsg/pear-build-download/File_IMC-0.5.0.tgz" Download of "pear/File_IMC" succeeded, but it is not a valid package archive Error: cannot download "pear/File_IMC" Download failed install failed
[email protected]:~# php pyrus.phar install File_IMC Pyrus version 2.0.0a4 SHA-1: 72271D92C3AA1FA96DF9606CD538868544609A52 Using PEAR installation found at /etc/php5/pear Sorry there was an error retrieving pear2.php.net/File_IMC from the default channel Pyrus\Channel\Exception: package File_IMC does not exist Pyrus\HTTPException: Download of http://pear2.php.net/rest/p/file_imc/info.xml failed, file does not exist
[email protected]:~# pear install -Z File_IMC-beta downloading File_IMC-0.5.0.tar ... Starting to download File_IMC-0.5.0.tar (Unknown size) ....................................done: 167,936 bytes install ok: channel://pear.php.net/File_IMC-0.5.0
1. Joomla组件渲染流程
在Joomla中,组件可以说是最大的功能模块。一个组件分为两部分:前台和后台。后台主要用于对对应内容的管理,前台主要用于前台页面的呈现和响应各种操作。其文件目录分别对应于JOOMLAROOT/administrator/components和JOOMLAROOT/components。组件有自己的命名规则,文件夹名须命名为com_组件名,组件的访问也是单文件入口,入口文件为com_组件名/组件名.php,如components/com_content/content.php。上次讲到的URL中的option参数就是定义当前页面使用的是哪个组件,如option=com_content&view=category&id=64,它会先调用content.php,再由router.php路由到category视图,再调用相应的Model层取出ID=64的分类的信息,渲染之后呈现在模板中的jdoc:include type=”component”的位置上。
2. Joomla模块
与组件(Component)不同的是,模块(Module)是不能通过URL直接访问的,而是通过后台对模块的设置,根据菜单ID(URL中的Itemid)来判断当前页面应该加载哪些模块。所以它主要用于显示内容,而一些表单提交后的处理动作一般是放在组件中去处理的。因此,模块通常都是比较简单的程序,文件结构也很清晰易懂,如modules/mod_login模块中的文件结构如下
mod_login.xml # 模块配置及安装使用的文件 mod_login.php # 模块入口文件,以mod_模块名.php命名,可以看作Controller层 helper.php # 辅助文件,通常数据操作会放在这里,可以看作Model层 tmpl/ # 模板文件夹,View层 |_ default.php # 默认模板 |_ default_logout.php # 退出登录模板
3. 模块在后台的设置
进入后台,打开Extensions > Module Manager,可以看到网站的模块列表。我们打开一个,看一下相关配置项,如Login Form(ID: 16)
4. 模块调用的另外一个参数
在模板的首页文件中,我们会看到调用模块时有如下代码
jdoc:include type="modules" name="position-7" style="well"
这里多了一个style参数,这个其实是一个显示前的预处理动作,在当前模板文件夹中的html/modules.php中定义,打开这个文件我们就能看到有一个modChrome_well的函数,程序不是很复杂,只是在显示前对html做了下预处理,大家了解一下即可。用到的时候不是很多,但是关键时候你会觉得它非常方便。
5. 模块的另外一种调用方法
有时候会需要在程序里调用一个模块来显示,可以用以下程序来调用
# 该程序会显示所有设置在position位置上的模块,当然也会根据菜单ID来判断是否加载 $modules = & JModuleHelper::getModules('position'); foreach($modules as $module){ echo JModuleHelper::renderModule($module, array('style' => 'well')) }
6. 不推荐使用的模块调用方法
如果你看其他教程,还有另外一种调用模块的方法,就是在文章里使用如下代码
{loadposition myposition} # myposition为位置 {loadmodule moduletype} # moduletype为模块类型
该方法会绕过菜单ID的限制,加载对应的模块,另外此种写法对不熟悉Joomla的内容管理人员来说相对陌生,因为不推荐使用。作为使用Joomla建站的开发人员,我们应该规划出更好的页面结构,尽可能将一些配置项转为图形界面管理,而非代码方式管理。
7. 各部分的View覆写
HTML的输出其实是依赖于当前模板的,通过刚才的讲解我们可以看到每个组件、每个模块都有自己的View层,这样其实就脱离了Joomla的模板,在更换模板时就可能会出现问题。因此推荐将各部分的View层放到Joomla的模板中覆写,这样有利于
具体方法请参照JOOMLAROOT > templates > beez3 > html
在工作中,由于某种原因,部署于正式版本的代码不是由SVN直接更新的,这样在经过几次修改提交之后再发布修改的内容时会有点麻烦,需要对每个版本修改过的文件逐个检查上传。下面这个脚本文件可以导出两个版本之间添加过修改过的文件,导出后一起上传就可以了。
在工作中,由于某种原因,部署于正式版本的代码不是由SVN直接更新的,这样在经过几次修改提交之后再发布修改的内容时会有点麻烦,需要对每个版本修改过的文件逐个检查上传。下面这个脚本文件可以导出两个版本之间添加过修改过的文件,导出后一起上传就可以了。
#!/bin/bash if [ ! $1 ] || [ ! $2 ] || [ ! $3 ] || [ ! $4 ]; then echo "Please enter a revision from, revision to, SVN repository, and target directory" exit fi # set up nice names for the incoming parameters to make the script more readable revision_from=$1 revision_to=$2 repository=$3 target_directory=$4 # the grep is needed so we only get added/modified files and not the deleted ones or anything else # if it's a modified directory it's " M" so won't show with this command (good) # if it's an added directory it's still "A" so will show with this command (not so good) for line in `svn diff --summarize -r$revision_from:$revision_to $repository | grep "^[AM]"` do # each line in the above command in the for loop is split into two: # 1) the status line (containing A, M, AM, D etc) # 2) the full repository and filename string # so only export the file when it's not the status line if [ $line != "A" ] && [ $line != "AM" ] && [ $line != "M" ]; then # use sed to remove the repository from the full repo and filename filename=`echo "$line" |sed "s|$repository||g"` # don't export if it's a directory we've already created if [ ! -d $target_directory$filename ]; then directory=`dirname $filename` mkdir -p $target_directory$directory svn export -r $revision_to $line $target_directory$filename fi fi done # to summarize any deleted files or directories at the end of the script uncomment the following line #svn diff --summarize -r$revision_from:$revision_to $repository | grep "^[D]"
将文件保存为svndiff,并将权限改为0755,使用方法:
# $from_version: 起始版本号 # $to_version : 结束版本号 # $svn_repository: svn版本库地址,也可以是本地svn路径 # $target: 导出目录 svndiff $from_version $to_version $svn_repository $target
1. 下载生成htpasswd的脚本
#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH echo "=====================================" echo "# A tool like htpasswd for Nginx #" echo "#-----------------------------------#" echo "# Author:Licess http://www.lnmp.org #" echo "=====================================" #set UserName username="" read -p "Please input UserName:" username if [ "$username" = "" ]; then echo "Error:UserName can't be NULL!" exit 1 fi echo "===========================" echo "UserName was: $username" echo "===========================" #set password unpassword="" read -p "Please input the Password:" unpassword if [ "$unpassword" = "" ]; then echo "Error:Password can't be NULL!" exit 1 fi echo "===========================" echo "Password was: $unpassword" echo "===========================" password=$(perl -e 'print crypt($ARGV[0], "pwdsalt")' $unpassword) #set htpasswd file htfile="" read -p "Please input Auth filename:" htfile if [ "$htfile" = "" ]; then echo "Error:Auth filename can't be NULL!" exit 1 fi echo "===========================" echo "Auth File: /usr/local/nginx/conf/$htfile" echo "===========================" get_char() { SAVEDSTTY=`stty -g` stty -echo stty cbreak dd if=/dev/tty bs=1 count=1 2> /dev/null stty -raw stty echo stty $SAVEDSTTY } echo "" echo "Press any key to Creat...or Press Ctrl+c to cancel" char=`get_char` if [ ! -f /usr/local/nginx/conf/$htfile.conf ]; then echo "Create Auth file......" cat >/usr/local/nginx/conf/$htfile.conf<<eof $username:$password eof echo "Create Auth file successful,auth file path:/usr/local/nginx/conf/$htfile.conf." else echo "File already exists,please run this script again." exit 1
2. 设置脚本权限为可执行权限
chmod 0755 htpasswd.sh
3. 运行脚本生成密码文件
需要输入用户名、密码和文件名,脚本会帮我们生成好密码文件。
4. 在Nginx虚拟站点的配置中加入以下两句
auth_basic "Authorized users only"; auth_basic_user_file /usr/local/nginx/conf/htpasswd.conf;
5. 重启nginx生效
sudo /usr/local/nginx/sbin/nginx -s reload
现在我们来介绍一下Joomla的模板结构,介绍之前我们先将Joomla的友好URL关掉,有助于我们理解Joomla的渲染逻辑。
1. 关闭友好URL
进入后台 > System > Global Configuration,修改SEO Settings > Search Engine Friendly URLs为No,到前台检查页面,我们发现URL变成了参数的形式。
2. 模板文件结构
我们先来看一下Joomla的模板结构,再来讲上面URL中各个参数的意思。Joomla启用的模板可以在后台Extension > Template Manager中管理,打开这个管理界面我们可以看到Joomla的模板列表,Location表示是前台(site)和后台(Administrator)模板,Default代表当前使用的是哪个模板。如现在后台使用的是isis – Default,前台使用的是protostar – Default模板,其他参数我们后续讲解。
打开文件目录JOOMLAROOT > templates > protostar,可以看到如下文件
# 目录 css/ # 模板使用的css文件 html/ # 各部分(包括组件、模块、分页)使用的模板 images/ # 模板使用的图片文件 img/ # 模板使用的图片文件(逻辑上看,这两个图片文件夹没有什么区别,暂不深究) js/ # 模板使用的javascript文件 language/ # 模板使用的语言文件 less/ # less文件夹,供css使用 # 文件 index.php # 模板首页文件,保存模板的结构 error.php # 错误文件,用于程序错误时显示 favicon.ico # favicon文件 component.php # 这个文件与index.php功能类似,只是只显示组件内容,而不包括其他模块 templateDetails.xml # 模板配置文件,调整模板参数或安装模板时用到 template_preview.png # 模板预览图 template_thumbnail.png # 模板预览缩略图
3. 模板架构
在Joomla中,一个页面内只能有一个主要内容(组件:component),其他均属于模块(module),如下图所示
4. 从代码分析模板架构
打开index.php,除了简单的HTML和php外,我们还能看到以下几类语句
jdoc:include type="head" jdoc:include type="modules" name="position-1" style="none" jdoc:include type="message" jdoc:include type="component"
这些是Joomla引入内容的方式,Joomla模板引擎会解析这些语句,抓取对应的内容渲染到模板中,组成一个页面。type指明要包含的内容的类型
head # 页面头文件(包括css/javascript/meta标签),注意这里不是指网站内容的头部 modules # 模块 message # 提示消息 component # 组件
从代码中也可以看出,页面里只有一个component,同时有许多个modules。事实上message也是一个module,只是是一个比较特殊的module。
5. 从URL分析模板内容
在Joomla的URL中,重要的信息通常包含两部分:组件信息、菜单ID(当然以后会遇到更多的情况,这里我们只重点介绍组件和菜单),如第一点我们看到的URL
option=com_content # 该页面内要使用的组件,后台对应到Components中,文件使用JOOMLAROOT > components中的文件 view=category # 组件内要使用的view id=64 # view对应的ID Itemid=411 # 该页面对应的菜单ID
所以第一点提到的URl的意思是告诉Joomla:当前页面是要显示一个文章分类页面,分类ID是64,对应的菜单ID是411,打开后台Menus > All Modules,就可以看到菜单ID为411的菜单Content Modules。
6. 模板架构、代码结构和URL之间的关系
下面我们用一张图把模板架构、代码结构和URL之间的关系表示出来,帮助大家理解。
7. 菜单ID非常重要
可能你会有疑问:每个页面要显示什么模块是怎么定义的呢?
在Joomla的每个URL中,都应该有菜单ID,它不仅仅只是一个菜单信息,而且,哪个页面要显示哪些模块就是通过这个菜单ID来判断的。下次我们准备讲一下各部分的渲染方法。
Drupal
location / { if (!-e $request_filename) { rewrite ^/(.*)$ /index.php?q=$1 last; } }
Joomla
location / { if ( $args ~ "mosConfig_[a-zA-Z_]{1,21}(=|\%3d)" ) { set $args ""; rewrite ^.*$ http://$host/index.php last; return 403; } if ( $args ~ "base64_encode.*\(.*\)") { set $args ""; rewrite ^.*$ http://$host/index.php last; return 403; } if ( $args ~ "(\<|%3C).*script.*(\>|%3E)") { set $args ""; rewrite ^.*$ http://$host/index.php last; return 403; } if ( $args ~ "GLOBALS(=|\[|\%[0-9A-Z]{0,2})") { set $args ""; rewrite ^.*$ http://$host/index.php last; return 403; } if ( $args ~ "_REQUEST(=|\[|\%[0-9A-Z]{0,2})") { set $args ""; rewrite ^.*$ http://$host/index.php last; return 403; } if (!-e $request_filename) { rewrite (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ /index.php last; break; } }
WordPress
location / { if (-f $request_filename/index.html) { rewrite (.*) $1/index.html break; } if (-f $request_filename/index.php) { rewrite (.*) $1/index.php; } if (!-f $request_filename) { rewrite (.*) /index.php; } }