Archive for the 'php' Category

cakephp 2 rc1

Sunday, June 15th, 2008

At last, cakephp 1.2 rc1 is finally here.
Download it now, or go to the official website: http:www//www.cakephp.org

Force apache to download an image instead of opening it

Wednesday, February 21st, 2007

Have you ever came across a situation where you wanted an image to download instead of having the browser to display it? This is how you can do it . Enter the following code in a php file foo.php and call it like foo.php?file=myimage.jpg. Then a browser save dialog box will appear.

PHP:
  1. if(!$_GET['file']){exit;}
  2. $file = basename($_GET['file']);
  3. header("Content-Length: " . filesize($file));
  4. header('Content-Type: image/jpg');
  5. header('Content-Disposition: attachment; filename='.$file);
  6. readfile($file);

If you want to do the same with other kind of files like video etc, just replace the 'image/jpg' above with on of the following:

"pdf": "application/pdf"
"exe": "application/octet-stream"
"zip": "application/zip"
"xls": "application/vnd.ms-excel"
"ppt": "application/vnd.ms-powerpoint"
"gif": "image/gif"
"png": "image/png"
"jpg": "image/jpg"
"mp3": "audio/mpeg"
"mp3": "audio/mp3"
"wav": "audio/x-wav"
"mpe": "video/mpeg"
"mov": "video/quicktime"
"avi": "video/x-msvideo"

CakePHP used for UN data sharing

Tuesday, February 20th, 2007

BrainOff explains how he used the cakephp framework for building a simple application with CRUD administration and a Searchable front end. The application is used by the UN INSTRAW researchers.

Integrate CakePHP with Joomla!

Friday, February 9th, 2007

There were times when I wanted to use Joomla! and integrate it with new functionalities. The best option would be to create new components for joomla, but I never got to do this, did not want to invest time in this. Now, that belongs to the past. I  just discovered this article by Max aka Abhimanyu Grover. He illustrates how to install joomla! within the cakephp framework and take all the advantages of cake while at the same time you get the user authentication and content management that joomla! offers. Great article, take a look.

CakePHP - Use recursive instead of unbind to unbind all associations

Friday, February 9th, 2007

You can use recursive or unbind whenever you want to unbind associations from models. This is very useful because there are cases when you want to execute a simple query and you do not want to join other tables. Here comes unbind. Unbind allows us to unbind all but some model associations. On the other hand if you want to unbind all associations you can use recursive like this:

PHP:
  1. $this->Model->recursive = -1;

CakePHP is beautiful. I love it!

CakePHP - Clickatell api for sending sms

Friday, February 9th, 2007

I use the clickatell api for sending sms with cakephp. All I did was to grab a php class for the clickatell api which is shown below:

PHP:
  1. class sendsmsComponent extends Object
  2. {
  3.  
  4. var $api_id = "your_clickatell_api_id";
  5. var $user = "your_clickatell_username";
  6. var $password = "you_clickatell_password";
  7. var $use_ssl = false;
  8.  
  9. /**
  10. * Define SMS balance limit below class will not work
  11. * @var integer
  12. */
  13. var $balace_limit = 0;
  14.  
  15. /**
  16. * Gateway command sending method (curl,fopen)
  17. * @var mixed
  18. */
  19. var $sending_method = "fopen";
  20. var $unicode = false;
  21.  
  22. /**
  23. * Optional CURL Proxy
  24. * @var bool
  25. */
  26. var $curl_use_proxy = false;
  27. var $curl_proxy = "http://127.0.0.1:8080";
  28. var $curl_proxyuserpwd = "login:secretpass";
  29.  
  30. /**
  31. * Callback
  32. * 0 - Off
  33. * 1 - Returns only intermediate statuses
  34. * 2 - Returns only final statuses
  35. * 3 - Returns both intermediate and final statuses
  36. * @var integer
  37. */
  38. var $callback = 0;
  39. var $session;
  40. var $batch;
  41.  
  42. /**
  43. * Class constructor
  44. * Create SMS object and authenticate SMS gateway
  45. * @return object New SMS object.
  46. * @access public
  47. */
  48.  
  49. function startup(&$controller)
  50. {
  51. // This method takes a reference to the controller which is loading it.
  52. // Perform controller initialization here.
  53.  
  54. if ($this->use_ssl) {
  55. $this->base            = "http://api.clickatell.com/http";
  56. $this->base_s        = "https://api.clickatell.com/http";
  57. $this->base_batch    = "http://api.clickatell.com/http_batch";
  58. $this->base_s_batch    = "https://api.clickatell.com/http_batch";
  59.  
  60. } else {
  61. $this->base            = "http://api.clickatell.com/http";
  62. $this->base_s        = $this->base;
  63. $this->base_batch   = "http://api.clickatell.com/http_batch";
  64. $this->base_s_batch = $this->base_batch;
  65.  
  66. }
  67. $this->_auth();
  68. }
  69.  
  70. /*
  71. function sms () {
  72. if ($this->use_ssl) {
  73. $this->base   = "http://api.clickatell.com/http";
  74. $this->base_s = "https://api.clickatell.com/http";
  75. } else {
  76. $this->base   = "http://api.clickatell.com/http";
  77. $this->base_s = $this->base;
  78. }
  79. $this->_auth();
  80. }
  81. */
  82.  
  83. /**
  84. * Authenticate SMS gateway
  85. * @return mixed  "OK" or script die
  86. * @access private
  87. */
  88. function _auth() {
  89. $comm = sprintf ("%s/auth?api_id=%s&user=%s&password=%s", $this->base_s, $this->api_id, $this->user, $this->password);
  90. $this->session = $this->_parse_auth ($this->_execgw($comm));
  91. }
  92.  
  93. /**
  94. * Query SMS credis balance
  95. * @return integer  number of SMS credits
  96. * @access public
  97. */
  98. function getbalance() {
  99. $comm = sprintf ("%s/getbalance?session_id=%s", $this->base, $this->session);
  100. return $this->_parse_getbalance ($this->_execgw($comm));
  101. }
  102.  
  103. /**
  104. * Send SMS message
  105. * @param to mixed  The destination address.
  106. * @param from mixed  The source/sender address
  107. * @param text mixed  The text content of the message
  108. * @return mixed  "OK" or script die
  109. * @access public
  110. */
  111. function send($to=null, $from=null, $text=null) {
  112.  
  113. /* Check SMS credits balance */
  114. if ($this->getbalance() <$this->balace_limit) {
  115. die ("You have reach the SMS credit limit!");
  116. };
  117.  
  118. /* Check SMS $text length */
  119. if ($this->unicode == true) {
  120. $this->_chk_mbstring();
  121. if (mb_strlen ($text)> 210) {
  122. die ("Your unicode message is to long! (Current lenght=".mb_strlen ($text).")");
  123. }
  124. /* Does message need to be concatenate */
  125. if (mb_strlen ($text)> 70) {
  126. $concat = "&concat=3";
  127. } else {
  128. $concat = "";
  129. }
  130. } else {
  131. if (strlen ($text)> 465) {
  132. die ("Your message is to long! (Current lenght=".strlen ($text).")");
  133. }
  134. /* Does message need to be concatenate */
  135. if (strlen ($text)> 160) {
  136. $concat = "&concat=3";
  137. } else {
  138. $concat = "";
  139. }
  140. }
  141.  
  142. /* Check $to and $from is not empty */
  143. if (empty ($to)) {
  144. die ("You not specify destination address (TO)!");
  145. }
  146. if (empty ($from)) {
  147. die ("You not specify source address (FROM)!");
  148. }
  149. $from = $this->_custom_encode($from);
  150.  
  151. /* Reformat $to number */
  152. $cleanup_chr = array ("+", " ", "(", ")", "\r", "\n", "\r\n");
  153. $to = str_replace($cleanup_chr, "", $to);
  154.  
  155. /* Send SMS now */
  156. $comm = sprintf ("%s/sendmsg?session_id=%s&to=%s&from=%s&text=%s&callback=%s&unicode=%s%s",
  157. $this->base,
  158. $this->session,
  159. $from,
  160. $this->encode_message($text),
  161. $this->callback,
  162. $this->unicode,
  163. $concat
  164. );
  165. $myvar = $this->_parse_send ($this->_execgw($comm));
  166. return $myvar;
  167. }
  168.  
  169. function sendbatch($to=null, $from=null, $text=null){
  170. /* Check SMS credits balance */
  171. if ($this->getbalance() <$this->balace_limit) {
  172. die ("You have reach the SMS credit limit!");
  173. };
  174.  
  175. /* Check SMS $text length */
  176. if ($this->unicode == true) {
  177. $this->_chk_mbstring();
  178. if (mb_strlen ($text)> 210) {
  179. die ("Your unicode message is to long! (Current lenght=".mb_strlen ($text).")");
  180. }
  181. /* Does message need to be concatenate */
  182. if (mb_strlen ($text)> 70) {
  183. $concat = "&concat=3";
  184. } else {
  185. $concat = "";
  186. }
  187. } else {
  188. if (strlen ($text)> 465) {
  189. die ("Your message is to long! (Current lenght=".strlen ($text).")");
  190. }
  191. /* Does message need to be concatenate */
  192. if (strlen ($text)> 160) {
  193. $concat = "&concat=3";
  194. } else {
  195. $concat = "";
  196. }
  197. }
  198.  
  199. /* Check $to and $from is not empty */
  200. if (empty ($to)) {
  201. die ("You not specify destination address (TO)!");
  202. }
  203. if (empty ($from)) {
  204. die ("You not specify source address (FROM)!");
  205. }
  206. $from = $this->_custom_encode($from);
  207.  
  208. /* get batchID now */
  209. $comm = sprintf ("%s/startbatch?session_id=%s&template=%s&callback=%s&unicode=%s%s",
  210. $this->base_batch,
  211. $this->session,
  212. $this->encode_message($text),
  213. $this->callback,
  214. $this->unicode,
  215. $concat
  216. );
  217. $this->batch = $this->_batchID ($this->_execgw($comm));
  218.  
  219. $comm = sprintf ("%s/quicksend?session_id=%s&batch_id=%s&to=$to&from=$from",
  220. $this->base_batch,
  221. $this->session,
  222. $this->batch
  223. );
  224. $myvar = $this->_parse_sendbatch ($this->_execgw($comm));
  225. return $myvar;
  226. }
  227.  
  228. /**
  229. * Encode message text according to required standard
  230. * @param text mixed  Input text of message.
  231. * @return mixed  Return encoded text of message.
  232. * @access public
  233. */
  234. function encode_message ($text) {
  235. if ($this->unicode != true) {
  236. //standard encoding
  237. //return rawurlencode($text);
  238. return $this->_custom_encode($text);
  239. } else {
  240. //unicode encoding
  241. $uni_text_len = mb_strlen ($text, "UTF-8");
  242. $out_text = "";
  243.  
  244. //encode each character in text
  245. for ($i=0; $i<$uni_text_len; $i++) {
  246. $out_text .= $this->uniord(mb_substr ($text, $i, 1, "UTF-8"));
  247. }
  248.  
  249. return $out_text;
  250. }
  251. }
  252.  
  253. /**
  254. * Unicode function replacement for ord()
  255. * @param c mixed  Unicode character.
  256. * @return mixed  Return HEX value (with leading zero) of unicode character.
  257. * @access public
  258. */
  259. function uniord($c) {
  260. $ud = 0;
  261. if (ord($c{0})>=0 && ord($c{0})<=127)
  262. $ud = ord($c{0});
  263. if (ord($c{0})>=192 && ord($c{0})<=223)
  264. $ud = (ord($c{0})-192)*64 + (ord($c{1})-128);
  265. if (ord($c{0})>=224 && ord($c{0})<=239)
  266. $ud = (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
  267. if (ord($c{0})>=240 && ord($c{0})<=247)
  268. $ud = (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128);
  269. if (ord($c{0})>=248 && ord($c{0})<=251)
  270. $ud = (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128);
  271. if (ord($c{0})>=252 && ord($c{0})<=253)
  272. $ud = (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128);
  273. if (ord($c{0})>=254 && ord($c{0})<=255) //error
  274. $ud = false;
  275. return sprintf("%04x", $ud);
  276. }
  277.  
  278. /**
  279. * Spend voucher with sms credits
  280. * @param token mixed  The 16 character voucher number.
  281. * @return mixed  Status code
  282. * @access public
  283. */
  284. function token_pay ($token) {
  285. $comm = sprintf ("%s/http/token_pay?session_id=%s&token=%s",
  286. $this->base,
  287. $this->session,
  288. $token);
  289.  
  290. return $this->_execgw($comm);
  291. }
  292.  
  293. /**
  294. * Execute gateway commands