手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表2024年09月的文章

扩展yii的验证类

在项目中遇到文件上传的问题,这时候需要用到CFileValidator,但是官方的验证中少了一点点的处理,比如,对于图片,我只想上传尺寸正好是300x500的图片。怎么办?
所以我做了一点小小的扩展,于是我在rules里面加了这么两条:

PHP代码
  1. array('picname''application.validators.Myfile''on'=>'insert','types'=>'jpg,png','wrongType'=>'只允许上传jpg或者PNG','maxSize' => 1024*300,'tooLarge' => '图片最大只支持300K','imageSize'=>'768x1024','wrongImageSize'=>'对不起,图片尺寸只支持768*1024'),  
  2.          array('picname''application.validators.Myfile''on'=>'update','types'=>'jpg,png','wrongType'=>'只允许上传jpg或者PNG','maxSize' => 1024*300,'tooLarge' => '图片最大只支持300K','imageSize'=>'768x1024','wrongImageSize'=>'对不起,图片尺寸只支持768*1024','allowEmpty'=>true,),  

数组的第二个字段就是一个全路径,告诉rules,对于picname用的是application.validators.Myfile类。这个类其实很简单,只是简单的扩展了官方的CFileValidator类,大致如下:

PHP代码
  1. class Myfile extends CFileValidator{  
  2.     public $imageSize;  
  3.     public $wrongImageSize;  
  4.   
  5.     /** 
  6.      * @param CModel $object the object being validated 
  7.      * @param string $attribute the attribute being validated 
  8.      * @param CUploadedFile $file uploaded file passed to check against a set of rules 
  9.      * @return void 
  10.      */  
  11.     protected function validateFile($object$attribute$file){  
  12.         parent::validateFile($object,$attribute,$file);  
  13.         if($this->imageSize!=''&&strpos($this->imageSize,"x")!==false){  
  14.             list($width,$height) = @getimagesize($file->getTempName());  
  15.             $imageSize = sprintf("%sx%s",$width,$height);  
  16.             if($imageSize != $this->imageSize){  
  17.                 $message=$this->wrongImageSize!==null?$this->wrongImageSize : Yii::t('yii','The file "{file}" cannot be uploaded. Only files with these size are allowed: {imagesize}.');  
  18.                 $this->addError($object,$attribute,$message,array('{file}'=>$file->getName(), '{extensions}'=>$this->imageSize));  
  19.             }  
  20.         }  
  21.     }  
  22. }  

我这种只是简单的判断是否与指定尺寸相符,如果需要检测小于指定范围的图片,那就需要多一点的判断了,也不会太难啦。主要是一个思路(感谢烂桔提供思路)

Tags: yii

xml2array 和 array2xml

这是一个简单的方法,其实是两种方法的其中一种:xml2array 和 array2xml 中的一种啦。
但因为array2xml的时候,没有办法做到更好的把attributes做到更好,因此,就折腾了一些简单的处理方法:
array2xml是hightman的方法的简版。我自己改了一些:

PHP代码
  1. function array2xml($var$type = 'root'$tag = '') {  
  2.    $ret = '';  
  3.    if (!is_int($type)) {  
  4.        if ($tag)  
  5.            return array2xml(array($tag => $var), 0, $type); else {  
  6.            $tag .= $type;  
  7.            $type = 0;  
  8.        }  
  9.    }  
  10.    $level = $type;  
  11.    $indent = str_repeat("\t"$level);  
  12.    if (!is_array($var)) {  
  13.        $ret .= $indent . '<' . $tag;  
  14.        $var = strval($var);  
  15.        if ($var == '') {  
  16.            $ret .= ' />';  
  17.        } else if (!preg_match('/[^0-9a-zA-Z@\._:\/-]/'$var)) {  
  18.            $ret .= '>' . $var . '</' . $tag . '>';  
  19.        } else {  
  20.            $ret .= "><![CDATA[{$var}]]></{$tag}>";  
  21.        }  
  22.        //                if (strpos($var, "\n") === false){  
  23.        //                $ret .= '><![CDATA[' . $var . ']]></' . $tag . '>';  
  24.        //            } else  
  25.        $ret .= "\n";  
  26.    } else if (!(is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) && !emptyempty($var)) {  
  27.        foreach ($var as $tmp)  
  28.            $ret .= array2xml($tmp$level$tag);  
  29.    } else {  
  30.        $ret .= $indent . '<' . $tag;  
  31.        if ($level == 0)  
  32.            $ret .= '';  
  33.        if (isset($var['@attributes'])) {  
  34.            foreach ($var['@attributes'as $k => $v) {  
  35.                if (!is_array($v)) {  
  36.                    $ret .= sprintf(' %s="%s"'$k$v);  
  37.                }  
  38.            }  
  39.            unset($var['@attributes']);  
  40.        }  
  41.        $ret .= ">\n";  
  42.        foreach ($var as $key => $val) {  
  43.            $ret .= array2xml($val$level + 1, $key);  
  44.        }  
  45.        $ret .= "{$indent}</{$tag}>\n";  
  46.    }  
  47.    return $ret;  

在其中强加了attributes。比较恶心的方法啦。。
然后xml2array,其实以前写过,但写的不太好,所以我这次抄的是ibm的网站上的xml2json中的代码:

PHP代码
  1. define ("DEBUG", false);  
  2. // Maximum Recursion Depth that we can allow.  
  3. define ("MAX_RECURSION_DEPTH_ALLOWED", 25);  
  4. // An empty string  
  5. define ("EMPTY_STR""");  
  6. // SimpleXMLElement object property name for attributes  
  7. define ("SIMPLE_XML_ELEMENT_OBJECT_PROPERTY_FOR_ATTRIBUTES""@attributes");  
  8. // SimpleXMLElement object name.  
  9. define ("SIMPLE_XML_ELEMENT_PHP_CLASS""SimpleXMLElement");  
  10.     /** 
  11.      * @static 
  12.      * @param $simpleXmlElementObject 
  13.      * @param int $recursionDepth 
  14.      * @return array|null|string 
  15.      */  
  16.     public static function xml2array($simpleXmlElementObject$getAttributes = false , &$recursionDepth = 0 ) {  
  17.         // Keep an eye on how deeply we are involved in recursion.  
  18.         if ($recursionDepth > MAX_RECURSION_DEPTH_ALLOWED) {  
  19.             // Fatal error. Exit now.  
  20.             return (null);  
  21.         }  
  22.         if ($recursionDepth == 0) {  
  23.             if (get_class($simpleXmlElementObject) != SIMPLE_XML_ELEMENT_PHP_CLASS) {  
  24.                 // If the external caller doesn't call this function initially  
  25.                 // with a SimpleXMLElement object, return now.  
  26.                 return (null);  
  27.             } else {  
  28.                 // Store the original SimpleXmlElementObject sent by the caller.  
  29.                 // We will need it at the very end when we return from here for good.  
  30.                 $callerProvidedSimpleXmlElementObject = $simpleXmlElementObject;  
  31.             }  
  32.         } // End of if ($recursionDepth == 0) {  
  33.         if (get_class($simpleXmlElementObject) == SIMPLE_XML_ELEMENT_PHP_CLASS) {  
  34.             // Get a copy of the simpleXmlElementObject  
  35.             $copyOfsimpleXmlElementObject = $simpleXmlElementObject;  
  36.             // Get the object variables in the SimpleXmlElement object for us to iterate.  
  37.             $simpleXmlElementObject = get_object_vars($simpleXmlElementObject);  
  38.         }  
  39.         // It needs to be an array of object variables.  
  40.         if (is_array($simpleXmlElementObject)) {  
  41.             // Initialize the result array.  
  42.             $resultArray = array();  
  43.             // Is the input array size 0? Then, we reached the rare CDATA text if any.  
  44.             if (count($simpleXmlElementObject) <= 0) {  
  45.                 // Let us return the lonely CDATA. It could even be  
  46.                 // an empty element or just filled with whitespaces.  
  47.                 return (trim(strval($copyOfsimpleXmlElementObject)));  
  48.             }  
  49.             // Let us walk through the child elements now.  
  50.             foreach ($simpleXmlElementObject as $key => $value) {  
  51.                 // When this block of code is commented, XML attributes will be  
  52.                 // added to the result array.  
  53.                 // Uncomment the following block of code if XML attributes are  
  54.                 // NOT required to be returned as part of the result array.  
  55.                 /* 
  56.           if((is_string($key)) && ($key == SIMPLE_XML_ELEMENT_OBJECT_PROPERTY_FOR_ATTRIBUTES)) { 
  57.                   continue; 
  58.                 } 
  59.                 */  
  60.                 // Let us recursively process the current element we just visited.  
  61.                 // Increase the recursion depth by one.  
  62.                 $recursionDepth++;  
  63.                 if($key == '@attributes' && $getAttributes == true){  
  64.                     foreach(self::xml2array($value,$getAttributes$recursionDepthas $k=>$v){  
  65.                         $resultArray[$k]=$v;  
  66.                     }  
  67.                 }else{  
  68.                     $resultArray[$key] = self::xml2array($value,$getAttributes$recursionDepth);  
  69.                 }  
  70.                 // Decrease the recursion depth by one.  
  71.                 $recursionDepth--;  
  72.             } // End of foreach($simpleXmlElementObject as $key=>$value) {  
  73.             if ($recursionDepth == 0) {  
  74.                 // That is it. We are heading to the exit now.  
  75.                 // Set the XML root element name as the root [top-level] key of  
  76.                 // the associative array that we are going to return to the caller of this  
  77.                 // recursive function.  
  78.                 $tempArray = $resultArray;  
  79.                 $resultArray = array();  
  80.                 $resultArray[$callerProvidedSimpleXmlElementObject->getName()] = $tempArray;  
  81.             }  
  82.             return ($resultArray);  
  83.         } else {  
  84.             // We are now looking at either the XML attribute text or  
  85.             // the text between the XML tags.  
  86.             return (trim(strval($simpleXmlElementObject)));  
  87.         } // End of else  
  88.     }  

改过其中的几行代码,否则会报错,改了哪几行我忘了。。官网地址是:http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/

这里还有一个:http://www.zenme.org/?action=show&id=270,可以参考一下

Tags: xml2array

ec2 micro型 上国外网站

amazon的VPS是大家都知道的。其实micro是最近11爷告诉我的。说是有信用卡的话,可以免费用micro一年(关于这一块我不多介绍了,网上多的是教程)
申请了一个micro之后,听人介绍说,用它来做网站会很耗资源,于是想到的就是,可以用来做ssh tunnel,用来翻翻墙啦。
果然,我用xshell在菜单的tunneling中选择了dynmac后,自动把商品设为1080,然后就能翻墙了。
开始上上facebook了。但其实我很纠结的是,我几乎没有好友在上面,推特我的兴趣也不大,可能只是用来看看一些平时看不到的技术性文章吧。

如果你有信用卡,你也可以尝试使用一年免费的ssh上网哦。

Tags: ec2, amazon

笔记1

这段时间在和IOS开发的同事们互相配合,本来是有笔记都会记录下来的。然而那段时间网站备案出点问题。然后就没有记录下来。于是现在开始就要把当时遇到的一些问题开始记录了。
比如这个。原本程序写的时候没有考虑推送,但是在进行了一阵后,发现还是需要推送才行,但当时创建的版本是是类似xxx.xxx.*,如果用推送的话就不能xxx.xxx.*,而是要用具体的版本(因为不是我写的啦,所以说不太清楚),后来找到了官方的介绍:
http://developer.apple.com/library/ios/#qa/qa1680/_index.html

可以从这里看:

Updating from a wildcard App ID to an explicit App ID

Q:  My application is currently signed with a Provisioning Profile that uses a wildcard App ID. How do I enable my App ID to support In App Purchase or Apple Push Notification service?

A: My application is currently signed with a Provisioning Profile that uses a wildcard App ID. How do I enable my App ID to support In App Purchase or Apple Push Notification service?

In App Purchase and Apple Push Notification service require that your application is signed with a Provisioning Profile, which uses an explicit App ID such as com.mycompany.myappname. So, if you have shipped an application signed with a Provisioning Profile that uses a wildcard App ID such as com.mycompany.*, you may wonder how to enable that App ID to handle these features. The answer is to keep your project's Bundle ID the same and create a new App ID in the iPhone Portal that matches the Bundle ID you are currently using in your project. Follow the steps below to enable an App ID that supports In App Purchase or Apple Push Notification service:

  1. Identify your application's current Bundle ID

    You can find your Bundle ID by looking in your project's Info.plist file, however it is best to check what iTunes Connect says it is. Log in to iTunes Connect, navigate to the Manage Your Applications module present in the home page, then select the App Details link for the application you are updating, and copy the bundle identifier displayed in the ensuing page.

  2. Create a new App ID

    Your Team Agent or Team Admin should log in to the iPhone Portal and navigate to its App ID section to create a new App ID. In the App ID section, click on the New App ID button to navigate to the Create App ID form, which contains a Description field, a Bundle Identifier field, a Bundle Seed ID pop-up menu, and a Submit button as shown in Figure 1. Fill out the Description field with a meaningful name such as Explicit App ID for MyAppName for your App ID, select Generate New from the Bundle Seed ID pop-up menu, and paste the previously copied bundle identifier into the Bundle Identifier field. Click Submit to save the new App ID.

Figure 1  Create App ID form

大小: 91.6 K
尺寸: 500 x 329
浏览: 1545 次
点击打开新窗口浏览全图

  1. Enable the newly created App ID for In App Purchase or Apple Push Notification service

    Find your App ID in the App ID section of the Program Portal, click the Configure link next to it, and follow the instructions to enable your App ID for either In App Purchase or Apple Push Notification service.

  2. Update or create, download, and install a Provisioning Profile that uses your App ID enabled for In App Purchase or Apple Push Notifications

    Edit an existing Provisioning Profile or create a new one, and then associate it with your newly created App ID. Download and install this Provisioning Profile on your machine and select it in the Code Signing Identity section of your Target's Build pane in Xcode. Update the version number of your binary, build it, test it, and upload it to iTunes Connect for review.

看最后的两点就知道怎么操作了啦。。这些资料还是需要找英文的,MD,中文里就找不到类似的资料 。。。真TMD狗屎

Tags: 推送

终于通了。NND

历经多时,终于又通了。
6年的备案啊。。哭泣中

顺便,首页的模版被我误删除了。靠。