在linux下实现doc转pdf,再转swf,实现在线预览功能

首先需要在linux系统下面安装swftools,官网:http://www.swftools.org/
安装swftools需要安装freetype和jpeglib依赖
地址:
http://download.savannah.gnu.org/releases/freetype/freetype-2.5.3.tar.gz
http://www.ijg.org/files/jpegsrc.v9a.tar.gz
然后再编译安装swftools
编辑如果出现以下出错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
jpeg.c: In functionjpeg_load_from_mem’:
jpeg.c:331:30: error: ‘TRUE’ undeclared (first use in this function)
jpeg_read_header(&cinfo, TRUE);

^
jpeg.c: In functionjpeg_load’:
jpeg.c:380:30: error: ‘TRUE’ undeclared (first use in this function)
jpeg_read_header(&cinfo, TRUE);

^
jpeg.c: In functionjpeg_get_size’:
jpeg.c:462:30: error: ‘TRUE’ undeclared (first use in this function)
jpeg_read_header(&cinfo, TRUE);

^
make[1]: *** [jpeg.o] Error 1
make[1]: Leaving directory `/home/geekz/Downloads/swftools-0.9.2/lib'
make: *** [all] Error 2

那就将源文件的lib/jpeg.c文件中的TRUE和FALSE变量分别改成1和0,又或者在文件首部加入

1
2
#define TRUE 1
#define FALSE 0

swftools中文支持

1
2
mkdir –p /usr/share/xpdf
cd /usr/share/xpdf/

下载中文支持及字体库

1
2
3
4
5
6
wget ftp://ftp.foolabs.com/pub/xpdf/xpdf-chinese-simplified.tar.gz
wget http://www.nginxs.com/download/font.zip
tar zxvf xpdf-chinese-simplified.tar.gz
unzip font.zip
mv Gbsn00lp.ttf gkai00mp.ttf xpdf-chinese-simplified/CMap/
cd /usr/share/xpdf/xpdf-chinese-simplified

编辑,加入新增的字体

1
vim add-to-xpdfrc

内容如下:

1
2
3
4
5
6
7
cidToUnicodeAdobe-GB1/usr/share/xpdf/chinese-simplified/Adobe-GB1.cidToUnicode
unicodeMapISO-2022-CN/usr/share/xpdf/chinese-simplified/ISO-2022-CN.unicodeMap
unicodeMapEUC-CN/usr/share/xpdf/chinese-simplified/EUC-CN.unicodeMap
unicodeMapGBK/usr/share/xpdf/chinese-simplified/GBK.unicodeMap
cMapDirAdobe-GB1/usr/share/xpdf/chinese-simplified/CMap
toUnicodeDir/usr/share/xpdf/chinese-simplified/CMap
displayCIDFontTT Adobe-GB1 /usr/share/xpdf/chinese-simplified/CMap/gkai00mp.ttf

保存后退出
要在浏览器里面复制中文到剪贴页不乱码,必须要在JS代码参数里面添加localeChain: ‘zh_CN’

安装unoconv:sudo apt-get install unoconv

1
exec("sudo unoconv -f pdf -o {$abPdfPath} {$abPath}", $output, $status);

需要赋予www-data对unoconv的执行权限
执行sudo visudo,在最后加上 www-data ALL=NOPASSWD:/usr/bin/unoconv
这样 www-data 就可以运行 unoconv 了

关于dedecms的问题

由于业务关系,需要维护一个用dedecms搭建的官网,一开始以为时一两个就能将迁移的事情做好,但是发现迁移后,各种问题就来了。

  1. 首先,这个官网已经时被入侵过了里面,都挂着很多游戏私服的广告,将它清理掉后,对线上的服务器针对这个官网做了些权限的限制。

2.重要的问题来了,迁移后居然发现无法编辑文章,折腾了一整天,后来才发现原来时PHP版本的问题,dedecms暂时还没有支持到PHP5.3的版本,所以在某些地方会出现问题。
例如:

htmlspecialchars在php5.4默认为utf8编码,gbk编码字符串经 htmlspecialchars 转义后的中文字符串为空,所以对于GBK的版本来说会一直返回空,导致很多文章内容都为空,解决办法时

解决办法:给htmlspecialchars添加ENT_COMPAT ,’GB2312’参数修改编码默认值。

具体修改页面:

1、dede/article_add.php 和 dede/article_edit.php

1
2
3
$title = htmlspecialchars(cn_substrR($title,$cfg_title_maxlen));
改成
$title = htmlspecialchars(cn_substrR($title,$cfg_title_maxlen),ENT_COMPAT ,'GB2312');

2、include/ckeditor/ckeditor_php5.php 138行

1
将 htmlspecialchars($value)  替换为 htmlspecialchars($value, ENT_COMPAT ,'GB2312')

测试,发布文章成功。

vps上搭建pptp的vpn

pptp配置

安装pptp

用ubuntu就是安装东西快

sudo apt-get -y update
sudo apt-get -y install pptpd
修改配置脚本

配置一下dns

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cat >/etc/ppp/options.pptpd <<END
name pptpd
refuse-pap
refuse-chap
refuse-mschap
require-mschap-v2
require-mppe-128
ms-dns 8.8.8.8
ms-dns 8.8.4.4
proxyarp
lock
nobsdcomp
novj
novjccomp
nologfd
END

说明

name pptpd(pptpd服务名,可以随便填写。)
refuse-pap(拒绝pap身份认证模式。)
refuse-chap(拒绝chap身份认证模式。)
refuse-mschap(拒绝mschap身份认证模式。)
require-mschap-v2(在端点进行连接握手时需要使用微软的 mschap-v2 进行自身验证。)
require-mppe-128(MPPE 模块使用 128 位加密。)
ms-dns 8.8.8.8 (ppp 为 Windows 客户端提供 DNS 服务器 IP 地址。)
proxyarp (建立 ARP 代理键值。)
nodefaultroute(不替换默认路由)
debug(开启调试模式,相关信息记录在 /var/logs/message 中。)
lock(锁定客户端 PTY 设备文件。)
nobsdcomp (禁用 BSD 压缩模式。)
还有ip

1
2
3
4
5
6
cat >/etc/pptpd.conf <<END
option /etc/ppp/options.pptpd
logwtmp
localip 192.168.2.1
remoteip 192.168.2.10-100
END

增加路由转发

ipv4转发

1
2
3
4
cat >> /etc/sysctl.conf <<END
net.ipv4.ip_forward=1
END
sysctl -p

修改iptables

备份当前iptables

1
iptables-save > /etc/iptables.down.rules

修改iptable NAT转发

1
iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o eth0 -j MASQUERADE

设置MTU

1
iptables -I FORWARD -s 192.168.2.0/24 -p tcp --syn -i ppp+ -j TCPMSS --set-mss 1300

保存新iptables

1
iptables-save > /etc/iptables.up.rules

重启后继续有效

1
2
3
4
cat >>/etc/ppp/pptpd-options<<EOF
pre-up iptables-restore < /etc/iptables.up.rules
post-down iptables-restore < /etc/iptables.down.rules
EOF

增加用户

增加帐号密码都是test的用户

1
2
3
cat >/etc/ppp/chap-secrets <<END
test pptpd test *
END

重启服务

1
2
/etc/init.d/pptpd restart
netstat -lntp

自动脚本

wget -c https://raw2.github.com/suyan/Scripts/master/Setup/pptp.sh

Vim+Vundle+YouCompleteMe

准备

(1)YouCompleteMe插件要求Vim 版本在7.3.584及以上,而Ubuntu12.04源中的Vim已经比较老了,所以需要编译源码手动更新VIM的版本。
(2)Vundle是一个新生代vim插件管理器,使用git进行插件安装和更新。插件管理器的好处是:使用简单,自动化管理vim的插件。让你的.vim文件夹远离混乱不堪的窘境,从此一手.vimrc走天下。
(3)Clang+LLVM是Apple公司资助的一个项目,YouCompleteMe精确的补全功能完全依赖于Clang的代码解析。我们需要安装好Clang 3.2+以便为YCM提供支持。
(4)SVN和Git。用于从代码库中检出源代码。在Ubuntu下这两个软件的安装都非常简单,使用apt-get命令即可安装完成。
(5)YouCompleteMe和Syntastic这两个插件都是配合使用的。YCM不用再说,Syntastic用于源码的实时检查。具体效果如同Eclipse一样,在代码编写的过程中即可实时报告语法错误。

  1. 编译源码更新VIM
    First, install all the prerequisite libraries, including Mercurial. For a Debian-like Linux distribution like Ubuntu, that would be the following:

sudo apt-get install libncurses5-dev libgnome2-dev libgnomeui-dev libgtk2.0-dev libatk1.0-dev libbonoboui2-dev \
libcairo2-dev libx11-dev libxpm-dev libxt-dev python-dev ruby-dev mercurial
Remove vim if you have it already,

sudo apt-get remove vim vim-runtime gvim #这一步可以不做
On Ubuntu 12.04.2 you probably have to remove these packages as well,
sudo apt-get remove vim-tiny vim-common vim-gui-common #同上一条命令,这一步可以不做
Once everything is installed, getting the source is easy. If you’re not using vim 7.3, make sure to set the VIMRUNTIMEDIR variable correctly below (for instance, with vim 7.4a, use /usr/share/vim/vim74a):

cd ~/temp
hg clone https://code.google.com/p/vim/
cd vim
./configure –with-features=huge –enable-rubyinterp –enable-pythoninterp –with-python-config-dir=/usr/lib/python2.7-config \
–enable-perlinterp –enable-gui=gtk2 –enable-cscope –prefix=/usr
make VIMRUNTIMEDIR=/usr/share/vim/vim74
sudo make install
If you wish to support plugins that require Lua, you may also wish to add,
–enable-luainterp
Set vim as your default editor with update-alternatives,

sudo update-alternatives –install /usr/bin/editor editor /usr/bin/vim 1
sudo update-alternatives –set editor /usr/bin/vim
sudo update-alternatives –install /usr/bin/vi vi /usr/bin/vim 1
sudo update-alternatives –set vi /usr/bin/vim
Double check that you are in fact running the new Vim binary by looking at the output of vim –version.

  1. 安装Vundle
    (1)安装Vundle
    Vundle托管在Github上,下载Vundle需要使用到git来检出源代码,
    git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
    以上命令直接将源代码检出到了~/.vim/bundle/vundle这个目录下。下载完了vundle后,需要配置.vimrc文件,我的配置如下(摘自github),
    p1
    配置中每个Bundle声明一个要用Vundle安装的插件。当需要的vim插件托管在Github上,例如,可写成Bundle ‘scrooloose/syntastic’;当插件在www.vim.org上面时,我们需要写明该插件的名称,还要加上.vim扩展名,否则vundle无法更新该插件。基本上这两种情况可以满足我们大部分人的需要了。
    (2)Vundle的使用——安装/卸载插件
    打开一个vim窗口,执行命令:BundleInstall,接下来Vundle会自动去网上下载.vimrc里用Bundle声明的插件,并把能安装的都安装好(安装到bundle文件夹下),在此过程中,Vundle还会自动执行:helptags命令,因此我们可以直接在vim中查看插件的帮助文档。要删除一个插件也很简单,先在~/.vimrc中移除该插件的Bundle声明,然后进入vim执行命令:BundleClean,Vundle将自动清除bundle文件夹下的该插件。

  2. 安装YouCompleteMe
    使用Vundle安装
    按照上图中的配置——在.vimrc中加入Bundle ‘Valloric/YouCompleteMe’,保存退出,执行,
    vim
    :BundleInstall
    这样Vundle就会自动去下载并安装.vimrc中Bundle声明的插件(除非已安装)。

  3. 配置YouCompleteMe
    安装好之后,首先需要做配置,
    cd ~/.vim/bundle/YouCompleteMe

./install.sh –clang-completer
这个过程会自动安装clang,从而在~/pxf/.vim/bundle/YouCompleteMe/python目录下产生libclang.so文件。
在.vimrc中配置,

1
2
3
4
5
6
7
8
9
""""""""""""""""""""""""""""""
" ycm setting
""""""""""""""""""""""""""""""
let g:ycm_global_ycm_extra_conf='~/.ycm_extra_conf.py'
let g:ycm_collect_identifiers_from_tag_files = 1
let g:ycm_seed_identifiers_with_syntax = 1
let g:ycm_confirm_extra_conf=0
let g:ycm_key_invoke_completion = '<C-/>'
nnoremap <F5> :YcmForceCompileAndDiagnostics<CR>

其中ycm_global_ycm_extra_conf非常重要,里面设定ycm的搜索头文件路径。.ycm_extra_conf.py文件可以针对具体的代码工程建一个,如果不这么做,那么vim会按照.vimrc中设定的路径去找.ycm_extra_conf.py。在这里,我将模板~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py拷贝一份到~/.ycm_extra_conf.py,日后自己的修改都在拷贝的文件中进行。
实际使用中,我们需要配置~/.ycm_extra_conf.py这个文件,需要做改动的就是文件中的flags部分——添加我们的库文件,从而提供给ycm做自动补全。使用-isystem标志添加系统的头文件进行解析,而-I标志用于添加第三方的头文件进行解析,一般要在该文件中的flags = [后面添加 ‘-isystem’,’/usr/include’,。但是模板中已经有了该配置,所以就没做改动。

Trouble Shooting
安装YCM的过程挺坎坷的,我也试过编译YCM源码的形式来安装,但安装好了却没有效果,下面是我在这个过程中碰到的一些问题,及其解决方案。
在cmake配置的时候,可能会遇到找不到PythonLibs的报错,这是因为没有安装python-dev,解决方案:
sudo apt-get install python-dev
编译llvm-clang源码的时候,很容易出错,我采用了另一种方法:
sudo apt-get install clang-3.3 clang-3.3-doc

php 两个无限位数长度数的加减乘除算法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
//用字符串存放需要计算的数字
$var1 = '1812333333333333333333333333333312312312365465115168798845654646546541654623645654654132165798498799935965878749878990802045729820438846724720077549820225200070877958874065976960871699930588877830002524992671885461625646546546546546546126154654654';
$var2 = '2364565465413216579849879812363254541654565746871239648712653441235461243465142635441623141233241132234461523446512341236454126344612534';
$result = countNum($var1, $var2);

$maxLength = count($result);
for( $num = $maxLength - 1; $num >= 0; $num-- ){
echo $result[$num];
}

/**
* @name 计算两个数的加法
* @author geekzhumail@gmail.com
* @since 2014-03-16
* @param var1 第一个数字
* @param var2 第二个数字
*/

function countNum( $var1, $var2 ){
$result = array();//结果
$var1Length = strlen( $var1 );
$var2Length = strlen( $var2 );
$maxLength = ($var1Length >= $var2Length )?$var1Length:$var2Length;
$temp = 0;
$num = 0;
while( true ){
$temp1 = ($num >= $var1Length )?0:$var1[$var1Length - $num - 1];
$temp2 = ($num >= $var2Length )?0:$var2[$var2Length - $num - 1];
$temp += $temp1 + $temp2;
$temp = str_split($temp);
//var_dump($temp);
//exit;
$result[$num] = ( count( $temp ) > 1 )?$temp[1]:$temp[0];
$temp = ( count( $temp ) > 1 )?$temp[0]:0;
$num++;
if( $num == $maxLength ){
if( $temp != 0 ) {
$result[$num] = $temp;
}
break;
}
}
return $result;
}

linux grub 修复注意

在用livecd 修复ubuntu引导的时候,

1
2
3
4
5
6
7
8
sudo -i
fdisk -l
mount /dev/sda* /mnt
grub-install --boot-directory /mnt /dev/sda
如果linux系统是分区安装的话要记录挂载所有的分区,例如
mount /dev/sda7 /mnt
mount /dev/sda3 /mnt/boot
mount /dev/sda8 /mnt/home

linux下安装小米随身wifi的驱动,用来开启无线网卡功能

随身wifi这东西说白了都是无线网卡,就是厂商改了pid让你系统不能自动识别加载驱动。导致只能用他的软件只能用来当AP。可恨的是从来不考虑linux下使用的情况,只能自己动手。
其实找到芯片就可以了。安装芯片厂商的驱动。
我手头有个小米wifi,其实买来好玩,基本从来不用。说实话弱爆了。插在usb口突出那么多,比之之前我买的tplink的无线网卡大多了。

小米的芯片也是ralink的MT7601U,去官网下载最新linux驱动就可以了。
只是在修改common/rtusb_dev_id.c文件时,请添加小米wifi的pid
{USB_DEVICE(0x2717,0x4106)},
然后

1
2
3
make
make install
modprobe mt7601Usta

就行了。

再更新:
ubuntu升级为了14.04,但是发现怎么小米 wifi 驱动用不了,编译驱动发现报错,原来驱动不兼容,所以查了查资料参考http://blog.csdn.net/michaelbaker/article/details/23597741
但是这位作者中间可能笔误写错了两行。
在前面修改PID之后,修改驱动代码里的/os/linux/rt_linux.c 的1211行和1222行的

1
2
            pOSFSInfo->fsuid = current_fsuid();
                pOSFSInfo->fsgid = current_fsgid();

把他们两个改为

1
2
3
pOSFSInfo->fsuid = *(int *)&current_fsuid();

pOSFSInfo->fsgid = *(int *)&current_fsgid();

似乎是这两个变量在新的系统里返回值不是整型的缘故(?)

然后按原来步骤在14.04下就能正常了。

使用fastcgi_finish_request提高页面响应速度

CI的email类是支持SSL加密的,只要设置 $config[‘smtp_crypto’] = ‘ssl’; 就行了,官方文档上面没有写这个,太坑了。

1
2
3
4
5
6
7
8
$config['protocol'] = 'smtp';// mail, sendmail, or smtp
$config['smtp_host'] = 'smtp.163.com';
$config['smtp_user'] = 'XXX@163.com';
$config['smtp_pass'] = 'XXX';
$config['smtp_port'] = '465';
$config['smtp_crypto'] = 'ssl';
$config['charset'] = 'UTF-8';// 默认UTF-8
$config['wordwrap'] = true;//自动换行

done!