2013年7月29日星期一

[转] password strength

password strength


/**
 *
 * @param String $string
 * @return float
 *
 * Returns a float between 0 and 100. The closer the number is to 100 the
 * the stronger password is; further from 100 the weaker the password is.
 */ 
function password_strength($string){
    
$h    = 0;
    
$size = strlen($string);
    foreach(
count_chars($string, 1) as $v){
        
$p = $v / $size;
        
$h -= $p * log($p) / log(2);
    }
    
$strength = ($h / 4) * 100;
    if(
$strength > 100){
        
$strength = 100;
    }
    return 
$strength;
}
var_dump(password_strength("Correct Horse Battery Staple"));
echo 
"
"
; var_dump(password_strength("Super Monkey Ball"));
echo 
"
"
; var_dump(password_strength("Tr0ub4dor&3"));
echo 
"
"
; var_dump(password_strength("abc123"));
echo 
"
"
; var_dump(password_strength("sweet"));

2013年7月26日星期五

[总结] Library not loaded: /opt/local/lib/libiconv.2.dylib 错误解决之法

1. 背景
工作中需要用到 php 的一个图形库的扩展模块 gmagick.
而目前使用的 php 在编译时并没有将 gmagick 静态编译进去,于是通过编译扩展模块来实现.
扩展模块的编译很顺利,但挂在 php 上后发现没有生效.写的简单的脚本测试发现 php error.log 中报如下的错

PHP Warning:  PHP Startup: Unable to load dynamic library '/Users/hy0kl/php/ext/gmagick.so' - dlopen(/Users/hy0kl/php/ext/gmagick.so, 9): Library not loaded: /opt/local/lib/libiconv.2.dylib
  Referenced from: /Users/hy0kl/php/ext/gmagick.so
  Reason: Incompatible library version: gmagick.so requires version 8.0.0 or later, but libiconv.2.dylib provides version 7.0.0 in Unknown on line 0

谷歌后发现,我的机器中有两个版的 libiconv:
$ otool -LD /usr/lib/libiconv.2.dylib
/usr/lib/libiconv.2.dylib:
/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.0.0)

$ otool -LD /opt/local/lib/libiconv.2.dylib
/opt/local/lib/libiconv.2.dylib:
/opt/local/lib/libiconv.2.dylib (compatibility version 8.0.0, current version 8.1.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
初步确认是动态的加载顺序没有搞对.
多次尝试 + 不断的 google,终于发现了下面的有用帮助:
https://code.google.com/p/git-osx-installer/issues/detail?id=18

Found out that it wasn't a problem running git as root.

I had this in my personal .profile:
export DYLD_LIBRARY_PATH=/usr/lib
export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib:/sw/lib

Hashed it out and everything was fine..
Don't remember why I put it in there in the first place.. :-/


大家千万不要移除或重命名系统 /usr/lib 下的 libiconv,否则系统报各种问题,生不如死.

2. 解决方法:
$ vi ~/.bashrc

DYLD_LIBRARY_PATH=$HOME/local/lib:/usr/local/lib:/usr/local/mysql/lib
export DYLD_LIBRARY_PATH

DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib:/usr/lib
export DYLD_FALLBACK_LIBRARY_PATH

加入以后内容,通过小改变一下动态库的扫描路径的先后顺序,即可解决了

2013年7月25日星期四

[转] mac port warning

https://gist.github.com/romanr/3192047

Warning: Your developer_dir setting in macports.conf points to a non-existing directory. Since this is known to cause problems, please correct the setting or comment it and let macports auto-discover the correct path.

xcode-select --print-path

sudo nano /opt/local/etc/macports/macports.conf

[转] Building ImageMagick on OSX Lion and can't link libpng

Building ImageMagick on OSX Lion and can't link libpng

Q:
I am trying to build ImageMagick 6.8.0-5 on OSX Lion with support for libpng. I am just using the standard ./configure make make install procedure.
I succeed in compiling when I do not reference libpng.
./configure --with-png=no
make
However, I get an error in make when I try to add libpng support. (libpng-1.5.13)
./configure --with-png=yes
make
The error seems to be a linker error.
/usr/bin/nm: no name list
ld: warning: cannot export hidden symbol _SyncImagePixelCache from magick/.libs/magick_libMagickCore_la-cache.o
ld: warning: cannot export hidden symbol _ResetQuantumState from magick/.libs/magick_libMagickCore_la-quantum.o
Undefined symbols for architecture x86_64:
  "_png_set_check_for_invalid_index", referenced from:
      _WriteOnePNGImage in magick_libMagickCore_la-png.o
      _ReadOnePNGImage in magick_libMagickCore_la-png.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make[1]: *** [magick/libMagickCore.la] Error 1
make: *** [all] Error 2
I have not changed any other options to configure.
How can I fix this to add libpng support?

A:
You'll need to tell ImageMagick where to locate libpng. Luckily, libpng ships with a configuration script entitled "libpng-config".
You can get the link-library option form your local libpng install with the --L_opts flag.
libpng-config --L_opts
This will return something like -L/usr/local/lib. Set ImageMagick's LDFLAGS configuration to match your libpng library.
# Use the returned value for the LDFLAGS
./configure LDFLAGS='-L/usr/local/lib'
If libpng-config returns with a message 'command not found', you'll need to ensure your libpng is installed correctly, and can be located in your shell environment. Often, this can be as simple as adding a custom path to your PATH variable.
export PATH="$PATH:/usr/local/bin"

2013年7月23日星期二

git 如何取消跟踪某个文件呢?

$ git rm --cached FILENAME

就可以了.
该操作不会删除原来的文件,同时取消对文件的版本变化跟踪.

2013年7月21日星期日

[转] 解决 ios 使用AdMob遇到 -[GADObjectPrivate changeState:]: unrecognized selector sent to instance 问题

解决 ios 使用AdMob遇到 -[GADObjectPrivate changeState:]: unrecognized selector sent to instance 问题

iso使用AdMob时如遇到-[GADObjectPrivate changeState:]: unrecognized selector sent to instance 0x968c980情况,请在编译选项里linking中的other link flag加上-ObjC,否则蛋碎都找不到原因。

2013年7月16日星期二

[转] add Admob with Cocos2d-x on iOS

add Admob with Cocos2d-x on iOS

1. download admob, setup your account


2. add admob to xcode project. You need:
    - add libGoogleAdmobAds.a to bundle libraries
    - create a group and add admob header file

3.  in AppController.h, add member:     
@interface AppController : NSObject <
UIAccelerometerDelegate, UIAlertViewDelegate, UITextFieldDelegate,UIApplicationDelegate, GADBannerViewDelegate> {
    UIWindow *window;
    RootViewController    *viewController;
    GADBannerView* bannerView_;
}

4. in AppController.mm
    // setup admob
    
    // Create a view of the standard size at the bottom of the screen.
    // Available AdSize constants are explained in GADAdSize.h.
    bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
    bannerView_.delegate = self;
    // Specify the ad's "unit identifier." This is your AdMob Publisher ID.
    bannerView_.adUnitID = @"YOUR_ID";
    
    // Let the runtime know which UIViewController to restore after taking
    // the user wherever the ad goes and add it to the view hierarchy.
    bannerView_.rootViewController = viewController;
    [viewController.view addSubview:bannerView_];
    
    
    // Initiate a generic request to load it with an ad.
    GADRequest* adRequest = [GADRequest request];
    [bannerView_ loadRequest:adRequest];

    // start game
    cocos2d::CCApplication::sharedApplication()->run();

5. we are not over Yet!!!
    You'll have exceptions if running the app now. You must do this:
(http://stackoverflow.com/questions/12635283/admob-crashes-with-gadobjectprivate-changestate-unrecognized-selector)
You need to add -ObjC to the Other Linker Flags of your application target's build setting:

Click the blue top-level project icon in XCode
Choose your target and go to Build Settings
Under Other Linker Flags add -ObjC for both Release and Debug

Done!!!
Let's make money and go IPO :)

2013年6月27日星期四

[转] Mac 中文件的 @ 权限(属性)

mac中文件的@权限(属性)

当我在看mac下的文件属性的时候,发现一件奇怪的事情, 就是有的文件权限最后会有个@符号. 这个我从没在linux下看到过, 想必有什么特殊的含义. (不过通过google找这个问题还是有点难度的)

类似:
-rw-r--r--@  1 yarco  staff    22369877  1 13  2010 Adium_1.3.10.dmg
-rw-r--r--@  1 yarco  staff    19386869  7 14 05:22 Firefox 3.6.7.dmg
-rw-r--r--@  1 yarco  staff     6384001  7 10 02:43 Growl-1.2.1.dmg
drwxr-xr-x   3 yarco  staff         102  7 21 22:50 MacPorts-1.9.1.pkg

最后找到一个帖子 forums.macosxhints.com/showthread.php 原来man ls里是有这个解释的(谁有空去读那么一大串文档哦...)
If the file or directory has extended
attributes, the permissions field printed by the -l option is followed by
a '@' character. Otherwise, if the file or directory has extended secu-
rity information, the permissions field printed by the -l option is fol-
lowed by a '+' character.
 
@指 该文件有扩展的属性
+指该目录有扩展的安全信息

macbook:Downloads yarco$ ls -l@
total 7636752
-rw-r--r--@  1 yarco  staff    22369877  1 13  2010 Adium_1.3.10.dmg
com.apple.diskimages.fsck            20 
com.apple.diskimages.recentcksum            79 
com.apple.metadata:kMDItemWhereFroms           125 
com.apple.quarantine            74 
-rw-r--r--@  1 yarco  staff    19386869  7 14 05:22 Firefox 3.6.7.dmg
com.apple.diskimages.fsck            20 
com.apple.diskimages.recentcksum            79 
com.apple.metadata:kMDItemWhereFroms           242 
com.apple.quarantine            74 
-rw-r--r--@  1 yarco  staff     6384001  7 10 02:43 Growl-1.2.1.dmg
com.apple.diskimages.fsck            20 
com.apple.diskimages.recentcksum            80 
com.apple.metadata:kMDItemWhereFroms            88 
com.apple.quarantine            74 
drwxr-xr-x   3 yarco  staff         102  7 21 22:50 MacPorts-1.9.1.pkg

这下看清楚了吧...

也刻意通过xattr查看更多细节
$ xattr PhysicsEditor-1.0.10.dmg
com.apple.diskimages.fsck
com.apple.diskimages.recentcksum
com.apple.metadata:kMDItemWhereFroms
com.apple.quarantine

[转] Objective C 类方法

Objective C 类方法

一,Objective C  的类方法(class method),即 static 方法,是用 + 修饰的方法。类方法是属于类对象的,所以无需创建类的实例对象我们就可以直接使用它们。使用形式为:
[ClassName classMethod];
在这里 ClassName 代表的就是一个类对象,而不是作为一种类型。类名只有出现在接收消息的表达式中才代表一个类对象,其他情况下都表示一种类型。
二,类方法可以继承自基类,就像普通的类成员方法一样。有一个特别的类方法 + (void) initialize,在类接收到任何消息之前,运行时系统会向类对象发送这个消息,因此我们可以在这个函数里对类对象做一些初始化,比如初始化 static 变量等等。在 NSObject 中,该方法什么也不做。
三,当类对象接收到一个消息却没有相应的类方法响应时,运行时系统会去检查是否存在一个 root (通常是 NSObject)的实例方法能够响应,如果能够响应就会执行 root 的成员方法来响应。只有在这种情况下,类对象才能够执行实例方法!再次声明类对象执行实例方法的条件:
1,类对象没有相应的类方法来响应某个发送至它的消息;
2,root 类定义了能够响应这个消息的实例方法。
四,类方法的返回值类型默认为 id,也能接受不定参数,这些都与实例方法是一致的。例如:
+ makeGroup:group, ...;
五,在类方法中使用 self 和 super,这时 self 和 super 指代的都是类对象,而非实例对象,这一点要特别注意。推荐的工厂方法写法如下,在这里使用 self class 而不是直接写类名,是为了应对子类调用该函数的情况。
+ (id) getInstance
{
    id instance = [[[self class] alloc] init];
    return [instance autorelease];
}

2013年6月21日星期五

[转] Mac OS X 系统上类似 ldd 的工具

Mac OS X系统上类似 ldd 的工具

ldd是用来查看一个可执行文件中包括对哪些共享库的调用,但是在MAC OX X下似乎没有ldd,取而代之的是另一个叫otool的命令,使用该命令的-L命令可以实现ldd的功能。
ldd on Mac OS X
"ldd" in various flavors of Unix is used to check which dynamic libraries are used by the executable. There is no "ldd" command in Mac OS X (Convenient, huh?). Instead, Apple provides something that is talled "otool", which has many different options. One of them is "-L", which displays the linked libraries:
sparrow (13:18)$ otool -L h3dpost.x 
h3dpost.x:
/opt/local/lib/libmpi.0.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.1.3)
/opt/local/lib/libmpi_f77.0.dylib (compatibility version 1.0.0, current version 1.0.0) 
/opt/local/lib/libopen-rte.0.dylib (compatibility version 1.0.0, current version 1.0.0) 
/opt/local/lib/libopen-pal.0.dylib (compatibility version 1.0.0, current version 1.0.0) 
/usr/local/lib/libgfortran.3.dylib (compatibility version 4.0.0, current version 4.0.0) 
/usr/local/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
from: http://schumakov.info/howto-osx.php